udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / chattr-util.c
blobfe8b9abf91fc01dcda79456cbd125b82f5368b66
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6 #include <sys/stat.h>
7 #include <linux/fs.h>
9 #include "chattr-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "macro.h"
14 #include "string-util.h"
16 int chattr_full(
17 int dir_fd,
18 const char *path,
19 unsigned value,
20 unsigned mask,
21 unsigned *ret_previous,
22 unsigned *ret_final,
23 ChattrApplyFlags flags) {
25 _cleanup_close_ int fd = -EBADF;
26 unsigned old_attr, new_attr;
27 int set_flags_errno = 0;
28 struct stat st;
30 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
32 fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, /* xopen_flags = */ 0, /* mode = */ 0);
33 if (fd < 0)
34 return fd;
36 if (fstat(fd, &st) < 0)
37 return -errno;
39 /* Explicitly check whether this is a regular file or directory. If it is anything else (such
40 * as a device node or fifo), then the ioctl will not hit the file systems but possibly
41 * drivers, where the ioctl might have different effects. Notably, DRM is using the same
42 * ioctl() number. */
44 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
45 return -ENOTTY;
47 if (mask == 0 && !ret_previous && !ret_final)
48 return 0;
50 if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
51 return -errno;
53 new_attr = (old_attr & ~mask) | (value & mask);
54 if (new_attr == old_attr) {
55 if (ret_previous)
56 *ret_previous = old_attr;
57 if (ret_final)
58 *ret_final = old_attr;
59 return 0;
62 if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) >= 0) {
63 unsigned attr;
65 /* Some filesystems (BTRFS) silently fail when a flag cannot be set. Let's make sure our
66 * changes actually went through by querying the flags again and verifying they're equal to
67 * the flags we tried to configure. */
69 if (ioctl(fd, FS_IOC_GETFLAGS, &attr) < 0)
70 return -errno;
72 if (new_attr == attr) {
73 if (ret_previous)
74 *ret_previous = old_attr;
75 if (ret_final)
76 *ret_final = new_attr;
77 return 1;
80 /* Trigger the fallback logic. */
81 errno = EINVAL;
84 if ((errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno)) ||
85 !FLAGS_SET(flags, CHATTR_FALLBACK_BITWISE))
86 return -errno;
88 /* When -EINVAL is returned, we assume that incompatible attributes are simultaneously
89 * specified. E.g., compress(c) and nocow(C) attributes cannot be set to files on btrfs.
90 * As a fallback, let's try to set attributes one by one.
92 * Also, when we get EOPNOTSUPP (or a similar error code) we assume a flag might just not be
93 * supported, and we can ignore it too */
95 unsigned current_attr = old_attr;
96 for (unsigned i = 0; i < sizeof(unsigned) * 8; i++) {
97 unsigned new_one, mask_one = 1u << i;
99 if (!FLAGS_SET(mask, mask_one))
100 continue;
102 new_one = UPDATE_FLAG(current_attr, mask_one, FLAGS_SET(value, mask_one));
103 if (new_one == current_attr)
104 continue;
106 if (ioctl(fd, FS_IOC_SETFLAGS, &new_one) < 0) {
107 if (errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno))
108 return -errno;
110 log_full_errno(FLAGS_SET(flags, CHATTR_WARN_UNSUPPORTED_FLAGS) ? LOG_WARNING : LOG_DEBUG,
111 errno,
112 "Unable to set file attribute 0x%x on %s, ignoring: %m", mask_one, strna(path));
114 /* Ensures that we record whether only EOPNOTSUPP&friends are encountered, or if a more serious
115 * error (thus worth logging at a different level, etc) was seen too. */
116 if (set_flags_errno == 0 || !ERRNO_IS_NOT_SUPPORTED(errno))
117 set_flags_errno = -errno;
119 continue;
122 if (ioctl(fd, FS_IOC_GETFLAGS, &current_attr) < 0)
123 return -errno;
126 if (ret_previous)
127 *ret_previous = old_attr;
128 if (ret_final)
129 *ret_final = current_attr;
131 /* -ENOANO indicates that some attributes cannot be set. ERRNO_IS_NOT_SUPPORTED indicates that all
132 * encountered failures were due to flags not supported by the FS, so return a specific error in
133 * that case, so callers can handle it properly (e.g.: tmpfiles.d can use debug level logging). */
134 return current_attr == new_attr ? 1 : ERRNO_IS_NOT_SUPPORTED(set_flags_errno) ? set_flags_errno : -ENOANO;
137 int read_attr_fd(int fd, unsigned *ret) {
138 struct stat st;
140 assert(fd >= 0);
142 if (fstat(fd, &st) < 0)
143 return -errno;
145 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
146 return -ENOTTY;
148 return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
151 int read_attr_path(const char *p, unsigned *ret) {
152 _cleanup_close_ int fd = -EBADF;
154 assert(p);
155 assert(ret);
157 fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
158 if (fd < 0)
159 return -errno;
161 return read_attr_fd(fd, ret);