summaryrefslogtreecommitdiff
path: root/src/basic/chattr-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-06-21 11:17:10 +0200
committerLennart Poettering <lennart@poettering.net>2021-07-08 09:29:48 +0200
commitc1631ee124a30abfb9c71e2a1534b8afffc3b6a7 (patch)
tree4fe9ef14fe914074a39ab6d129e704f6fca62af6 /src/basic/chattr-util.c
parent91358db9dcb752f7ff5aac6638c6c7462d75001a (diff)
downloadsystemd-c1631ee124a30abfb9c71e2a1534b8afffc3b6a7.tar.gz
chattr-util: generalize chattr manipulation for files with secrets from journalctl
This moves the code for setting chattr file attributes appropriate for "secrets" files from journalctl into generic chattr-util.c code so that we can use it elsewhere. Also, let's reuse the "bitwise" logic already implemented in the chattr code, instead of doing it again.
Diffstat (limited to 'src/basic/chattr-util.c')
-rw-r--r--src/basic/chattr-util.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/basic/chattr-util.c b/src/basic/chattr-util.c
index 10e59875ad..b5658754a5 100644
--- a/src/basic/chattr-util.c
+++ b/src/basic/chattr-util.c
@@ -7,10 +7,19 @@
#include <linux/fs.h>
#include "chattr-util.h"
+#include "errno-util.h"
#include "fd-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(const char *path, int fd, unsigned value, unsigned mask, unsigned *ret_previous, unsigned *ret_final, bool fallback) {
_cleanup_close_ int fd_will_close = -1;
unsigned old_attr, new_attr;
struct stat st;
@@ -57,12 +66,16 @@ int chattr_full(const char *path, int fd, unsigned value, unsigned mask, unsigne
return 1;
}
- if (errno != EINVAL || !fallback)
+ if ((errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno)) ||
+ !FLAGS_SET(flags, CHATTR_FALLBACK_BITWISE))
return -errno;
/* When -EINVAL is returned, we assume that incompatible attributes are simultaneously
* specified. E.g., compress(c) and nocow(C) attributes cannot be set to files on btrfs.
- * As a fallback, let's try to set attributes one by one. */
+ * As a fallback, let's try to set attributes one by one.
+ *
+ * Also, when we get EOPNOTSUPP (or a similar error code) we assume a flag might just not be
+ * supported, and we can ignore it too */
unsigned current_attr = old_attr;
for (unsigned i = 0; i < sizeof(unsigned) * 8; i++) {
@@ -76,8 +89,12 @@ int chattr_full(const char *path, int fd, unsigned value, unsigned mask, unsigne
continue;
if (ioctl(fd, FS_IOC_SETFLAGS, &new_one) < 0) {
- if (errno != EINVAL)
+ if (errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno))
return -errno;
+
+ log_full_errno(FLAGS_SET(flags, CHATTR_WARN_UNSUPPORTED_FLAGS) ? LOG_WARNING : LOG_DEBUG,
+ errno,
+ "Unable to set file attribute 0x%x on %s, ignoring: %m", mask_one, strna(path));
continue;
}