summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-06-01 14:52:56 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-06-06 14:42:03 +0200
commitcf91b9155c20a57bfc756b2b7e1a8f401f2bf16d (patch)
treea14294799dbf38768992cffd2a12926b1d7d4b2c /src
parentbtrfs-util: Add btrfs_subvol_set_read_only_at() (diff)
downloadsystemd-cf91b9155c20a57bfc756b2b7e1a8f401f2bf16d.tar.xz
systemd-cf91b9155c20a57bfc756b2b7e1a8f401f2bf16d.zip
chattr-util: Make chattr_full() an openat() style function
Diffstat (limited to 'src')
-rw-r--r--src/basic/chattr-util.c28
-rw-r--r--src/basic/chattr-util.h13
-rw-r--r--src/home/homework-luks.c2
-rw-r--r--src/tmpfiles/tmpfiles.c2
4 files changed, 24 insertions, 21 deletions
diff --git a/src/basic/chattr-util.c b/src/basic/chattr-util.c
index 3c66a3e0c8..c59fb8a84e 100644
--- a/src/basic/chattr-util.c
+++ b/src/basic/chattr-util.c
@@ -9,29 +9,29 @@
#include "chattr-util.h"
#include "errno-util.h"
#include "fd-util.h"
+#include "fs-util.h"
#include "macro.h"
#include "string-util.h"
-int chattr_full(const char *path,
- int fd,
- unsigned value,
- unsigned mask,
- unsigned *ret_previous,
- unsigned *ret_final,
- ChattrApplyFlags flags) {
+int chattr_full(
+ int dir_fd,
+ const char *path,
+ unsigned value,
+ unsigned mask,
+ unsigned *ret_previous,
+ unsigned *ret_final,
+ ChattrApplyFlags flags) {
- _cleanup_close_ int fd_will_close = -EBADF;
+ _cleanup_close_ int fd = -EBADF;
unsigned old_attr, new_attr;
int set_flags_errno = 0;
struct stat st;
- assert(path || fd >= 0);
+ assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
- if (fd < 0) {
- fd = fd_will_close = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
- if (fd < 0)
- return -errno;
- }
+ fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, /* xopen_flags = */ 0, /* mode = */ 0);
+ if (fd < 0)
+ return -errno;
if (fstat(fd, &st) < 0)
return -errno;
diff --git a/src/basic/chattr-util.h b/src/basic/chattr-util.h
index 82f91c66d9..c1ee63b4fa 100644
--- a/src/basic/chattr-util.h
+++ b/src/basic/chattr-util.h
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
+#include <fcntl.h>
#include <linux/fs.h>
#include <stdbool.h>
#include <stddef.h>
@@ -39,13 +40,15 @@ typedef enum ChattrApplyFlags {
CHATTR_WARN_UNSUPPORTED_FLAGS = 1 << 1,
} ChattrApplyFlags;
-int chattr_full(const char *path, int fd, unsigned value, unsigned mask, unsigned *ret_previous, unsigned *ret_final, ChattrApplyFlags flags);
-
+int chattr_full(int dir_fd, const char *path, unsigned value, unsigned mask, unsigned *ret_previous, unsigned *ret_final, ChattrApplyFlags flags);
+static inline int chattr_at(int dir_fd, const char *path, unsigned value, unsigned mask, unsigned *previous) {
+ return chattr_full(dir_fd, path, value, mask, previous, NULL, 0);
+}
static inline int chattr_fd(int fd, unsigned value, unsigned mask, unsigned *previous) {
- return chattr_full(NULL, fd, value, mask, previous, NULL, 0);
+ return chattr_full(fd, NULL, value, mask, previous, NULL, 0);
}
static inline int chattr_path(const char *path, unsigned value, unsigned mask, unsigned *previous) {
- return chattr_full(path, -1, value, mask, previous, NULL, 0);
+ return chattr_full(AT_FDCWD, path, value, mask, previous, NULL, 0);
}
int read_attr_fd(int fd, unsigned *ret);
@@ -57,5 +60,5 @@ int read_attr_path(const char *p, unsigned *ret);
#define CHATTR_SECRET_FLAGS (FS_SECRM_FL|FS_NODUMP_FL|FS_SYNC_FL|FS_NOCOW_FL)
static inline int chattr_secret(int fd, ChattrApplyFlags flags) {
- return chattr_full(NULL, fd, CHATTR_SECRET_FLAGS, CHATTR_SECRET_FLAGS, NULL, NULL, flags|CHATTR_FALLBACK_BITWISE);
+ return chattr_full(fd, NULL, CHATTR_SECRET_FLAGS, CHATTR_SECRET_FLAGS, NULL, NULL, flags|CHATTR_FALLBACK_BITWISE);
}
diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c
index e1f0ff1dcb..2359699801 100644
--- a/src/home/homework-luks.c
+++ b/src/home/homework-luks.c
@@ -2281,7 +2281,7 @@ int home_create_luks(
setup->temporary_image_path = TAKE_PTR(t);
- r = chattr_full(t, setup->image_fd, FS_NOCOW_FL|FS_NOCOMP_FL, FS_NOCOW_FL|FS_NOCOMP_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE);
+ r = chattr_full(setup->image_fd, NULL, FS_NOCOW_FL|FS_NOCOMP_FL, FS_NOCOW_FL|FS_NOCOMP_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE);
if (r < 0 && r != -ENOANO) /* ENOANO → some bits didn't work; which we skip logging about because chattr_full() already debug logs about those flags */
log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r,
"Failed to set file attributes on %s, ignoring: %m", setup->temporary_image_path);
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index af1b904d32..e0087d997b 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -1540,7 +1540,7 @@ static int fd_set_attribute(
return log_error_errno(procfs_fd, "Failed to re-open '%s': %m", path);
unsigned previous, current;
- r = chattr_full(NULL, procfs_fd, f, item->attribute_mask, &previous, &current, CHATTR_FALLBACK_BITWISE);
+ r = chattr_full(procfs_fd, NULL, f, item->attribute_mask, &previous, &current, CHATTR_FALLBACK_BITWISE);
if (r == -ENOANO)
log_warning("Cannot set file attributes for '%s', maybe due to incompatibility in specified attributes, "
"previous=0x%08x, current=0x%08x, expected=0x%08x, ignoring.",