]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/chattr-util.c
Merge pull request #21383 from yuwata/network-address-scope
[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 if (ret_previous)
63 *ret_previous = old_attr;
64 if (ret_final)
65 *ret_final = new_attr;
66 return 1;
67 }
68
69 if ((errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno)) ||
70 !FLAGS_SET(flags, CHATTR_FALLBACK_BITWISE))
71 return -errno;
72
73 /* When -EINVAL is returned, we assume that incompatible attributes are simultaneously
74 * specified. E.g., compress(c) and nocow(C) attributes cannot be set to files on btrfs.
75 * As a fallback, let's try to set attributes one by one.
76 *
77 * Also, when we get EOPNOTSUPP (or a similar error code) we assume a flag might just not be
78 * supported, and we can ignore it too */
79
80 unsigned current_attr = old_attr;
81 for (unsigned i = 0; i < sizeof(unsigned) * 8; i++) {
82 unsigned new_one, mask_one = 1u << i;
83
84 if (!FLAGS_SET(mask, mask_one))
85 continue;
86
87 new_one = UPDATE_FLAG(current_attr, mask_one, FLAGS_SET(value, mask_one));
88 if (new_one == current_attr)
89 continue;
90
91 if (ioctl(fd, FS_IOC_SETFLAGS, &new_one) < 0) {
92 if (errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno))
93 return -errno;
94
95 log_full_errno(FLAGS_SET(flags, CHATTR_WARN_UNSUPPORTED_FLAGS) ? LOG_WARNING : LOG_DEBUG,
96 errno,
97 "Unable to set file attribute 0x%x on %s, ignoring: %m", mask_one, strna(path));
98 continue;
99 }
100
101 if (ioctl(fd, FS_IOC_GETFLAGS, &current_attr) < 0)
102 return -errno;
103 }
104
105 if (ret_previous)
106 *ret_previous = old_attr;
107 if (ret_final)
108 *ret_final = current_attr;
109
110 return current_attr == new_attr ? 1 : -ENOANO; /* -ENOANO indicates that some attributes cannot be set. */
111 }
112
113 int read_attr_fd(int fd, unsigned *ret) {
114 struct stat st;
115
116 assert(fd >= 0);
117
118 if (fstat(fd, &st) < 0)
119 return -errno;
120
121 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
122 return -ENOTTY;
123
124 return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
125 }
126
127 int read_attr_path(const char *p, unsigned *ret) {
128 _cleanup_close_ int fd = -1;
129
130 assert(p);
131 assert(ret);
132
133 fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
134 if (fd < 0)
135 return -errno;
136
137 return read_attr_fd(fd, ret);
138 }