Merge pull request #26726 from DaanDeMeyer/cleanups
Various small cleanups
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
/* 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;
|
||||
int r;
|
||||
|
||||
assert(p);
|
||||
assert(IN_SET(operation & ~LOCK_NB, LOCK_EX, LOCK_SH));
|
||||
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 stat st;
|
||||
|
||||
fd = open(p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
r = unposix_lock(fd, operation);
|
||||
if (r < 0)
|
||||
return r == -EAGAIN ? -EBUSY : r;
|
||||
|
||||
/* 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 &&
|
||||
unposix_lock(f->fd, LOCK_EX|LOCK_NB) >= 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;
|
||||
}
|
||||
@@ -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)
|
||||
@@ -133,3 +133,34 @@ int memfd_new_and_map(const char *name, size_t sz, void **p) {
|
||||
|
||||
return TAKE_FD(fd);
|
||||
}
|
||||
|
||||
int memfd_new_and_seal(const char *name, const void *data, size_t sz) {
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
ssize_t n;
|
||||
off_t f;
|
||||
int r;
|
||||
|
||||
assert(data || sz == 0);
|
||||
|
||||
fd = memfd_new(name);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
if (sz > 0) {
|
||||
n = write(fd, data, sz);
|
||||
if (n < 0)
|
||||
return -errno;
|
||||
if ((size_t) n != sz)
|
||||
return -EIO;
|
||||
|
||||
f = lseek(fd, 0, SEEK_SET);
|
||||
if (f != 0)
|
||||
return -errno;
|
||||
}
|
||||
|
||||
r = memfd_set_sealed(fd);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return TAKE_FD(fd);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
int memfd_new(const char *name);
|
||||
int memfd_new_and_map(const char *name, size_t sz, void **p);
|
||||
int memfd_new_and_seal(const char *name, const void *data, size_t sz);
|
||||
|
||||
int memfd_map(int fd, uint64_t offset, size_t size, void **p);
|
||||
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "format-util.h"
|
||||
#include "lock-util.h"
|
||||
#include "macro.h"
|
||||
#include "mkdir.h"
|
||||
#include "parse-util.h"
|
||||
@@ -671,12 +672,7 @@ int reset_uid_gid(void) {
|
||||
}
|
||||
|
||||
int take_etc_passwd_lock(const char *root) {
|
||||
struct flock flock = {
|
||||
.l_type = F_WRLCK,
|
||||
.l_whence = SEEK_SET,
|
||||
.l_start = 0,
|
||||
.l_len = 0,
|
||||
};
|
||||
int r;
|
||||
|
||||
/* This is roughly the same as lckpwdf(), but not as awful. We don't want to use alarm() and signals,
|
||||
* hence we implement our own trivial version of this.
|
||||
@@ -695,8 +691,9 @@ int take_etc_passwd_lock(const char *root) {
|
||||
if (fd < 0)
|
||||
return log_debug_errno(errno, "Cannot open %s: %m", path);
|
||||
|
||||
if (fcntl(fd, F_SETLKW, &flock) < 0)
|
||||
return log_debug_errno(errno, "Locking %s failed: %m", path);
|
||||
r = unposix_lock(fd, LOCK_EX);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Locking %s failed: %m", path);
|
||||
|
||||
return TAKE_FD(fd);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user