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
chattr_full(const char * path,int fd,unsigned value,unsigned mask,unsigned * ret_previous,unsigned * ret_final,ChattrApplyFlags flags)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 int set_flags_errno = 0;
26 struct stat st;
27
28 assert(path || fd >= 0);
29
30 if (fd < 0) {
31 fd = fd_will_close = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
32 if (fd < 0)
33 return -errno;
34 }
35
36 if (fstat(fd, &st) < 0)
37 return -errno;
38
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. */
43
44 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
45 return -ENOTTY;
46
47 if (mask == 0 && !ret_previous && !ret_final)
48 return 0;
49
50 if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
51 return -errno;
52
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;
60 }
61
62 if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) >= 0) {
63 unsigned attr;
64
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. */
68
69 if (ioctl(fd, FS_IOC_GETFLAGS, &attr) < 0)
70 return -errno;
71
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;
78 }
79
80 /* Trigger the fallback logic. */
81 errno = EINVAL;
82 }
83
84 if ((errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno)) ||
85 !FLAGS_SET(flags, CHATTR_FALLBACK_BITWISE))
86 return -errno;
87
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.
91 *
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 */
94
95 unsigned current_attr = old_attr;
96 for (unsigned i = 0; i < sizeof(unsigned) * 8; i++) {
97 unsigned new_one, mask_one = 1u << i;
98
99 if (!FLAGS_SET(mask, mask_one))
100 continue;
101
102 new_one = UPDATE_FLAG(current_attr, mask_one, FLAGS_SET(value, mask_one));
103 if (new_one == current_attr)
104 continue;
105
106 if (ioctl(fd, FS_IOC_SETFLAGS, &new_one) < 0) {
107 if (errno != EINVAL && !ERRNO_IS_NOT_SUPPORTED(errno))
108 return -errno;
109
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));
113
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;
118
119 continue;
120 }
121
122 if (ioctl(fd, FS_IOC_GETFLAGS, ¤t_attr) < 0)
123 return -errno;
124 }
125
126 if (ret_previous)
127 *ret_previous = old_attr;
128 if (ret_final)
129 *ret_final = current_attr;
130
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;
135 }
136
read_attr_fd(int fd,unsigned * ret)137 int read_attr_fd(int fd, unsigned *ret) {
138 struct stat st;
139
140 assert(fd >= 0);
141
142 if (fstat(fd, &st) < 0)
143 return -errno;
144
145 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
146 return -ENOTTY;
147
148 return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
149 }
150
read_attr_path(const char * p,unsigned * ret)151 int read_attr_path(const char *p, unsigned *ret) {
152 _cleanup_close_ int fd = -1;
153
154 assert(p);
155 assert(ret);
156
157 fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
158 if (fd < 0)
159 return -errno;
160
161 return read_attr_fd(fd, ret);
162 }
163