]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/chattr-util.c
Merge pull request #21264 from medhefgo/boot-lto
[thirdparty/systemd.git] / src / basic / chattr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6 #include <sys/stat.h>
7 #include <linux/fs.h>
8
9 #include "chattr-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "macro.h"
13 #include "string-util.h"
14
15 int chattr_full(const char *path,
16 int fd,
17 unsigned value,
18 unsigned mask,
19 unsigned *ret_previous,
20 unsigned *ret_final,
21 ChattrApplyFlags flags) {
22
23 _cleanup_close_ int fd_will_close = -1;
24 unsigned old_attr, new_attr;
25 struct stat st;
26
27 assert(path || fd >= 0);
28
29 if (fd < 0) {
30 fd = fd_will_close = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
31 if (fd < 0)
32 return -errno;
33 }
34
35 if (fstat(fd, &st) < 0)
36 return -errno;
37
38 /* Explicitly check whether this is a regular file or directory. If it is anything else (such
39 * as a device node or fifo), then the ioctl will not hit the file systems but possibly
40 * drivers, where the ioctl might have different effects. Notably, DRM is using the same
41 * ioctl() number. */
42
43 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
44 return -ENOTTY;
45
46 if (mask == 0 && !ret_previous && !ret_final)
47 return 0;
48
49 if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
50 return -errno;
51
52 new_attr = (old_attr & ~mask) | (value & mask);
53 if (new_attr == old_attr) {
54 if (ret_previous)
55 *ret_previous = old_attr;
56 if (ret_final)
57 *ret_final = old_attr;
58 return 0;
59 }
60
61 if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) >= 0) {
62 unsigned attr;
63
64 /* Some filesystems (BTRFS) silently fail when a flag cannot be set. Let's make sure our
65 * changes actually went through by querying the flags again and verifying they're equal to
66 * the flags we tried to configure. */
67
68 if (ioctl(fd, FS_IOC_GETFLAGS, &attr) < 0)
69 return -errno;
70
71 if (new_attr == attr) {
72 if (ret_previous)
73 *ret_previous = old_attr;
74 if (ret_final)
75 *ret_final = new_attr;
76 return 1;
77 }
78
79 /* Trigger the fallback logic. */
80 errno = EINVAL;
81 }
82
83 if ((errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno)) ||
84 !FLAGS_SET(flags, CHATTR_FALLBACK_BITWISE))
85 return -errno;
86
87 /* When -EINVAL is returned, we assume that incompatible attributes are simultaneously
88 * specified. E.g., compress(c) and nocow(C) attributes cannot be set to files on btrfs.
89 * As a fallback, let's try to set attributes one by one.
90 *
91 * Also, when we get EOPNOTSUPP (or a similar error code) we assume a flag might just not be
92 * supported, and we can ignore it too */
93
94 unsigned current_attr = old_attr;
95 for (unsigned i = 0; i < sizeof(unsigned) * 8; i++) {
96 unsigned new_one, mask_one = 1u << i;
97
98 if (!FLAGS_SET(mask, mask_one))
99 continue;
100
101 new_one = UPDATE_FLAG(current_attr, mask_one, FLAGS_SET(value, mask_one));
102 if (new_one == current_attr)
103 continue;
104
105 if (ioctl(fd, FS_IOC_SETFLAGS, &new_one) < 0) {
106 if (errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno))
107 return -errno;
108
109 log_full_errno(FLAGS_SET(flags, CHATTR_WARN_UNSUPPORTED_FLAGS) ? LOG_WARNING : LOG_DEBUG,
110 errno,
111 "Unable to set file attribute 0x%x on %s, ignoring: %m", mask_one, strna(path));
112 continue;
113 }
114
115 if (ioctl(fd, FS_IOC_GETFLAGS, &current_attr) < 0)
116 return -errno;
117 }
118
119 if (ret_previous)
120 *ret_previous = old_attr;
121 if (ret_final)
122 *ret_final = current_attr;
123
124 return current_attr == new_attr ? 1 : -ENOANO; /* -ENOANO indicates that some attributes cannot be set. */
125 }
126
127 int read_attr_fd(int fd, unsigned *ret) {
128 struct stat st;
129
130 assert(fd >= 0);
131
132 if (fstat(fd, &st) < 0)
133 return -errno;
134
135 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
136 return -ENOTTY;
137
138 return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
139 }
140
141 int read_attr_path(const char *p, unsigned *ret) {
142 _cleanup_close_ int fd = -1;
143
144 assert(p);
145 assert(ret);
146
147 fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
148 if (fd < 0)
149 return -errno;
150
151 return read_attr_fd(fd, ret);
152 }