lock-util: Move to src/basic

This commit is contained in:
Daan De Meyer
2023-03-10 11:57:43 +01:00
parent b4cbfa5f1b
commit 4dd888dc92
4 changed files with 1 additions and 1 deletions
+161
View File
@@ -0,0 +1,161 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "lock-util.h"
#include "macro.h"
#include "missing_fcntl.h"
#include "path-util.h"
int make_lock_file(const char *p, int operation, LockFile *ret) {
_cleanup_close_ int fd = -EBADF;
_cleanup_free_ char *t = NULL;
assert(p);
assert(ret);
/* We use UNPOSIX locks as they have nice semantics, and are mostly compatible with NFS. */
t = strdup(p);
if (!t)
return -ENOMEM;
for (;;) {
struct flock fl = {
.l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
.l_whence = SEEK_SET,
};
struct stat st;
fd = open(p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
if (fd < 0)
return -errno;
if (fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl) < 0)
return errno == EAGAIN ? -EBUSY : -errno;
/* If we acquired the lock, let's check if the file still exists in the file system. If not,
* then the previous exclusive owner removed it and then closed it. In such a case our
* acquired lock is worthless, hence try again. */
if (fstat(fd, &st) < 0)
return -errno;
if (st.st_nlink > 0)
break;
fd = safe_close(fd);
}
*ret = (LockFile) {
.path = TAKE_PTR(t),
.fd = TAKE_FD(fd),
.operation = operation,
};
return 0;
}
int make_lock_file_for(const char *p, int operation, LockFile *ret) {
_cleanup_free_ char *fn = NULL, *dn = NULL, *t = NULL;
int r;
assert(p);
assert(ret);
r = path_extract_filename(p, &fn);
if (r < 0)
return r;
r = path_extract_directory(p, &dn);
if (r < 0)
return r;
t = strjoin(dn, "/.#", fn, ".lck");
if (!t)
return -ENOMEM;
return make_lock_file(t, operation, ret);
}
void release_lock_file(LockFile *f) {
if (!f)
return;
if (f->path) {
/* If we are the exclusive owner we can safely delete
* the lock file itself. If we are not the exclusive
* owner, we can try becoming it. */
if (f->fd >= 0 &&
(f->operation & ~LOCK_NB) == LOCK_SH) {
static const struct flock fl = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
};
if (fcntl(f->fd, F_OFD_SETLK, &fl) >= 0)
f->operation = LOCK_EX|LOCK_NB;
}
if ((f->operation & ~LOCK_NB) == LOCK_EX)
unlink_noerrno(f->path);
f->path = mfree(f->path);
}
f->fd = safe_close(f->fd);
f->operation = 0;
}
int unposix_lock(int fd, int operation) {
int cmd, type, r;
assert(fd >= 0);
cmd = (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW;
switch (operation & ~LOCK_NB) {
case LOCK_EX:
type = F_WRLCK;
break;
case LOCK_SH:
type = F_RDLCK;
break;
case LOCK_UN:
type = F_UNLCK;
break;
default:
assert_not_reached();
}
r = RET_NERRNO(fcntl(fd, cmd, &(struct flock) {
.l_type = type,
.l_whence = SEEK_SET,
.l_start = 0,
.l_len = 0,
}));
if (r == -EACCES) /* Treat EACCESS/EAGAIN the same as per man page. */
r = -EAGAIN;
return r;
}
void unposix_unlockpp(int **fd) {
assert(fd);
if (!*fd || **fd < 0)
return;
(void) unposix_lock(**fd, LOCK_UN);
*fd = NULL;
}
+22
View File
@@ -0,0 +1,22 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
typedef struct LockFile {
char *path;
int fd;
int operation;
} LockFile;
int make_lock_file(const char *p, int operation, LockFile *ret);
int make_lock_file_for(const char *p, int operation, LockFile *ret);
void release_lock_file(LockFile *f);
#define LOCK_FILE_INIT { .fd = -EBADF, .path = NULL }
/* Open File Description locks with the same interface as flock(). */
int unposix_lock(int fd, int operation);
void unposix_unlockpp(int **fd);
#define CLEANUP_UNPOSIX_UNLOCK(fd) \
_cleanup_(unposix_unlockpp) _unused_ int *CONCATENATE(_cleanup_unposix_unlock_, UNIQ) = &(fd)
+1
View File
@@ -46,6 +46,7 @@ basic_sources = files(
'ioprio-util.c',
'limits-util.c',
'locale-util.c',
'lock-util.c',
'log.c',
'login-util.c',
'memfd-util.c',