]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5008d581 | 2 | |
5008d581 | 3 | #include <errno.h> |
3f6fd1ba LP |
4 | #include <fcntl.h> |
5 | #include <fnmatch.h> | |
6 | #include <getopt.h> | |
5008d581 | 7 | #include <limits.h> |
3f6fd1ba LP |
8 | #include <linux/fs.h> |
9 | #include <stdbool.h> | |
10 | #include <stddef.h> | |
3b63d2d3 | 11 | #include <stdlib.h> |
52b32b2a | 12 | #include <sys/file.h> |
ebf4e801 | 13 | #include <sys/xattr.h> |
d9daae55 | 14 | #include <sysexits.h> |
3f6fd1ba LP |
15 | #include <time.h> |
16 | #include <unistd.h> | |
5008d581 | 17 | |
5a8575ef ZJS |
18 | #include "sd-path.h" |
19 | ||
3f6fd1ba | 20 | #include "acl-util.h" |
b5efdb8a | 21 | #include "alloc-util.h" |
3f6fd1ba | 22 | #include "btrfs-util.h" |
d6b4d1c7 | 23 | #include "build.h" |
430f0182 | 24 | #include "capability-util.h" |
f4351959 | 25 | #include "chase-symlinks.h" |
c8b3094d | 26 | #include "chattr-util.h" |
3f6fd1ba | 27 | #include "conf-files.h" |
28db6fbf | 28 | #include "constants.h" |
3f6fd1ba | 29 | #include "copy.h" |
1d77721f | 30 | #include "creds-util.h" |
7176f06c | 31 | #include "devnum-util.h" |
8fb3f009 | 32 | #include "dirent-util.h" |
63d3d0a5 | 33 | #include "dissect-image.h" |
4368c60c | 34 | #include "env-util.h" |
8f6fb95c | 35 | #include "errno-util.h" |
4f5dd394 | 36 | #include "escape.h" |
3ffd4af2 | 37 | #include "fd-util.h" |
0d39fa9c | 38 | #include "fileio.h" |
f97b34a6 | 39 | #include "format-util.h" |
f4f15635 | 40 | #include "fs-util.h" |
7d50b32a | 41 | #include "glob-util.h" |
708daf42 | 42 | #include "hexdecoct.h" |
c004493c | 43 | #include "io-util.h" |
3f6fd1ba | 44 | #include "label.h" |
5008d581 | 45 | #include "log.h" |
54693d9b | 46 | #include "macro.h" |
87938c3b | 47 | #include "main-func.h" |
ddb439b8 LP |
48 | #include "missing_stat.h" |
49 | #include "missing_syscall.h" | |
35cd0ba5 | 50 | #include "mkdir-label.h" |
63d3d0a5 | 51 | #include "mount-util.h" |
049af8ad | 52 | #include "mountpoint-util.h" |
a3451c2c | 53 | #include "offline-passwd.h" |
dcd5c891 | 54 | #include "pager.h" |
614b022c | 55 | #include "parse-argument.h" |
6bedfcbb | 56 | #include "parse-util.h" |
f2b5ca0e | 57 | #include "path-lookup.h" |
9eb977db | 58 | #include "path-util.h" |
294bf0c3 | 59 | #include "pretty-print.h" |
e5358401 | 60 | #include "rlimit-util.h" |
c6878637 | 61 | #include "rm-rf.h" |
d7b8eec7 | 62 | #include "selinux-util.h" |
3f6fd1ba | 63 | #include "set.h" |
760877e9 | 64 | #include "sort-util.h" |
3f6fd1ba | 65 | #include "specifier.h" |
15a5e950 | 66 | #include "stdio-util.h" |
8b43440b | 67 | #include "string-table.h" |
07630cea | 68 | #include "string-util.h" |
3f6fd1ba | 69 | #include "strv.h" |
3f532a59 | 70 | #include "terminal-util.h" |
affb60b1 | 71 | #include "umask-util.h" |
b1d4f8e1 | 72 | #include "user-util.h" |
1401882f | 73 | #include "virt.h" |
5008d581 | 74 | |
01000479 | 75 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates |
5008d581 | 76 | * them in the file system. This is intended to be used to create |
db019b8d KS |
77 | * properly owned directories beneath /tmp, /var/tmp, /run, which are |
78 | * volatile and hence need to be recreated on bootup. */ | |
5008d581 | 79 | |
1a967b6b LP |
80 | typedef enum OperationMask { |
81 | OPERATION_CREATE = 1 << 0, | |
82 | OPERATION_REMOVE = 1 << 1, | |
83 | OPERATION_CLEAN = 1 << 2, | |
84 | } OperationMask; | |
85 | ||
66ccd038 | 86 | typedef enum ItemType { |
b8bb3e8f | 87 | /* These ones take file names */ |
3b63d2d3 | 88 | CREATE_FILE = 'f', |
eccebf4b | 89 | TRUNCATE_FILE = 'F', /* deprecated: use f+ */ |
3b63d2d3 LP |
90 | CREATE_DIRECTORY = 'd', |
91 | TRUNCATE_DIRECTORY = 'D', | |
d7b8eec7 | 92 | CREATE_SUBVOLUME = 'v', |
5fb13eb5 LP |
93 | CREATE_SUBVOLUME_INHERIT_QUOTA = 'q', |
94 | CREATE_SUBVOLUME_NEW_QUOTA = 'Q', | |
ee17ee7c | 95 | CREATE_FIFO = 'p', |
468d726b LP |
96 | CREATE_SYMLINK = 'L', |
97 | CREATE_CHAR_DEVICE = 'c', | |
98 | CREATE_BLOCK_DEVICE = 'b', | |
849958d1 | 99 | COPY_FILES = 'C', |
b8bb3e8f LP |
100 | |
101 | /* These ones take globs */ | |
17493fa5 | 102 | WRITE_FILE = 'w', |
df8dee85 | 103 | EMPTY_DIRECTORY = 'e', |
b705ab6a ZJS |
104 | SET_XATTR = 't', |
105 | RECURSIVE_SET_XATTR = 'T', | |
106 | SET_ACL = 'a', | |
107 | RECURSIVE_SET_ACL = 'A', | |
17493fa5 LP |
108 | SET_ATTRIBUTE = 'h', |
109 | RECURSIVE_SET_ATTRIBUTE = 'H', | |
3b63d2d3 | 110 | IGNORE_PATH = 'x', |
78a92a5a | 111 | IGNORE_DIRECTORY_PATH = 'X', |
3b63d2d3 | 112 | REMOVE_PATH = 'r', |
a8d88783 | 113 | RECURSIVE_REMOVE_PATH = 'R', |
777b87e7 | 114 | RELABEL_PATH = 'z', |
e73a03e0 | 115 | RECURSIVE_RELABEL_PATH = 'Z', |
17493fa5 | 116 | ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */ |
66ccd038 | 117 | } ItemType; |
3b63d2d3 | 118 | |
7f7a50dd SK |
119 | typedef enum AgeBy { |
120 | AGE_BY_ATIME = 1 << 0, | |
121 | AGE_BY_BTIME = 1 << 1, | |
122 | AGE_BY_CTIME = 1 << 2, | |
123 | AGE_BY_MTIME = 1 << 3, | |
124 | ||
125 | /* All file timestamp types are checked by default. */ | |
126 | AGE_BY_DEFAULT_FILE = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_CTIME | AGE_BY_MTIME, | |
127 | AGE_BY_DEFAULT_DIR = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_MTIME | |
128 | } AgeBy; | |
129 | ||
3b63d2d3 | 130 | typedef struct Item { |
66ccd038 | 131 | ItemType type; |
3b63d2d3 LP |
132 | |
133 | char *path; | |
468d726b | 134 | char *argument; |
708daf42 LP |
135 | void *binary_argument; /* set if binary data, in which case it takes precedence over 'argument' */ |
136 | size_t binary_argument_size; | |
ebf4e801 | 137 | char **xattrs; |
349cc4a5 | 138 | #if HAVE_ACL |
f8eeeaf9 ZJS |
139 | acl_t acl_access; |
140 | acl_t acl_default; | |
141 | #endif | |
5008d581 LP |
142 | uid_t uid; |
143 | gid_t gid; | |
3b63d2d3 LP |
144 | mode_t mode; |
145 | usec_t age; | |
7f7a50dd | 146 | AgeBy age_by_file, age_by_dir; |
3b63d2d3 | 147 | |
468d726b | 148 | dev_t major_minor; |
88ec4dfa LP |
149 | unsigned attribute_value; |
150 | unsigned attribute_mask; | |
468d726b | 151 | |
3b63d2d3 LP |
152 | bool uid_set:1; |
153 | bool gid_set:1; | |
154 | bool mode_set:1; | |
cc43328c LP |
155 | bool uid_only_create:1; |
156 | bool gid_only_create:1; | |
157 | bool mode_only_create:1; | |
3b63d2d3 | 158 | bool age_set:1; |
abef3f91 | 159 | bool mask_perms:1; |
88ec4dfa | 160 | bool attribute_set:1; |
24f3a374 LP |
161 | |
162 | bool keep_first_level:1; | |
1910cd0e | 163 | |
c55ac248 | 164 | bool append_or_force:1; |
2e78fa79 | 165 | |
6d7b5433 WD |
166 | bool allow_failure:1; |
167 | ||
c46c3233 AW |
168 | bool try_replace:1; |
169 | ||
811a1587 | 170 | OperationMask done; |
3b63d2d3 LP |
171 | } Item; |
172 | ||
3f93da98 ZJS |
173 | typedef struct ItemArray { |
174 | Item *items; | |
96d10d78 | 175 | size_t n_items; |
811a1587 LP |
176 | |
177 | struct ItemArray *parent; | |
178 | Set *children; | |
3f93da98 ZJS |
179 | } ItemArray; |
180 | ||
5a8575ef | 181 | typedef enum DirectoryType { |
133bbca4 | 182 | DIRECTORY_RUNTIME, |
5a8575ef ZJS |
183 | DIRECTORY_STATE, |
184 | DIRECTORY_CACHE, | |
185 | DIRECTORY_LOGS, | |
186 | _DIRECTORY_TYPE_MAX, | |
187 | } DirectoryType; | |
188 | ||
a9bc518c LP |
189 | typedef enum { |
190 | CREATION_NORMAL, | |
191 | CREATION_EXISTING, | |
192 | CREATION_FORCE, | |
193 | _CREATION_MODE_MAX, | |
194 | _CREATION_MODE_INVALID = -EINVAL, | |
195 | } CreationMode; | |
196 | ||
ceaaeb9b | 197 | static bool arg_cat_config = false; |
f2b5ca0e | 198 | static bool arg_user = false; |
1a967b6b | 199 | static OperationMask arg_operation = 0; |
81815651 | 200 | static bool arg_boot = false; |
0221d68a | 201 | static PagerFlags arg_pager_flags = 0; |
3b63d2d3 | 202 | |
7bc040fa LP |
203 | static char **arg_include_prefixes = NULL; |
204 | static char **arg_exclude_prefixes = NULL; | |
cf9a4abd | 205 | static char *arg_root = NULL; |
63d3d0a5 | 206 | static char *arg_image = NULL; |
a6d8474f | 207 | static char *arg_replace = NULL; |
fba6e687 | 208 | |
3b63d2d3 LP |
209 | #define MAX_DEPTH 256 |
210 | ||
ef43a391 | 211 | static OrderedHashmap *items = NULL, *globs = NULL; |
7bc040fa LP |
212 | static Set *unix_sockets = NULL; |
213 | ||
87938c3b YW |
214 | STATIC_DESTRUCTOR_REGISTER(items, ordered_hashmap_freep); |
215 | STATIC_DESTRUCTOR_REGISTER(globs, ordered_hashmap_freep); | |
fe2f05b7 | 216 | STATIC_DESTRUCTOR_REGISTER(unix_sockets, set_freep); |
87938c3b YW |
217 | STATIC_DESTRUCTOR_REGISTER(arg_include_prefixes, freep); |
218 | STATIC_DESTRUCTOR_REGISTER(arg_exclude_prefixes, freep); | |
219 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
63d3d0a5 | 220 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
87938c3b | 221 | |
a9bc518c LP |
222 | static const char *const creation_mode_verb_table[_CREATION_MODE_MAX] = { |
223 | [CREATION_NORMAL] = "Created", | |
224 | [CREATION_EXISTING] = "Found existing", | |
225 | [CREATION_FORCE] = "Created replacement", | |
226 | }; | |
227 | ||
228 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode); | |
229 | ||
6db98098 ZJS |
230 | /* Different kinds of errors that mean that information is not available in the environment. */ |
231 | static inline bool ERRNO_IS_NOINFO(int r) { | |
232 | return IN_SET(abs(r), | |
233 | EUNATCH, /* os-release or machine-id missing */ | |
234 | ENOMEDIUM, /* machine-id or another file empty */ | |
235 | ENXIO); /* env var is unset */ | |
4cef1923 FB |
236 | } |
237 | ||
de61a04b | 238 | static int specifier_directory(char specifier, const void *data, const char *root, const void *userdata, char **ret) { |
5a8575ef ZJS |
239 | struct table_entry { |
240 | uint64_t type; | |
241 | const char *suffix; | |
242 | }; | |
243 | ||
244 | static const struct table_entry paths_system[] = { | |
245 | [DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME }, | |
246 | [DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE }, | |
247 | [DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE }, | |
248 | [DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS }, | |
249 | }; | |
250 | ||
251 | static const struct table_entry paths_user[] = { | |
252 | [DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME }, | |
253 | [DIRECTORY_STATE] = { SD_PATH_USER_CONFIGURATION }, | |
254 | [DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE }, | |
255 | [DIRECTORY_LOGS] = { SD_PATH_USER_CONFIGURATION, "log" }, | |
256 | }; | |
257 | ||
5a8575ef | 258 | const struct table_entry *paths; |
de61a04b LP |
259 | _cleanup_free_ char *p = NULL; |
260 | unsigned i; | |
261 | int r; | |
5a8575ef ZJS |
262 | |
263 | assert_cc(ELEMENTSOF(paths_system) == ELEMENTSOF(paths_user)); | |
264 | paths = arg_user ? paths_user : paths_system; | |
265 | ||
266 | i = PTR_TO_UINT(data); | |
267 | assert(i < ELEMENTSOF(paths_system)); | |
268 | ||
de61a04b LP |
269 | r = sd_path_lookup(paths[i].type, paths[i].suffix, &p); |
270 | if (r < 0) | |
271 | return r; | |
272 | ||
273 | if (arg_root) { | |
274 | _cleanup_free_ char *j = NULL; | |
275 | ||
276 | j = path_join(arg_root, p); | |
277 | if (!j) | |
278 | return -ENOMEM; | |
279 | ||
280 | *ret = TAKE_PTR(j); | |
281 | } else | |
282 | *ret = TAKE_PTR(p); | |
283 | ||
284 | return 0; | |
5a8575ef ZJS |
285 | } |
286 | ||
4cef1923 FB |
287 | static int log_unresolvable_specifier(const char *filename, unsigned line) { |
288 | static bool notified = false; | |
289 | ||
1401882f ZJS |
290 | /* In system mode, this is called when /etc is not fully initialized and some specifiers are |
291 | * unresolvable. In user mode, this is called when some variables are not defined. These cases are | |
292 | * not considered a fatal error, so log at LOG_NOTICE only for the first time and then downgrade this | |
293 | * to LOG_DEBUG for the rest. | |
294 | * | |
295 | * If we're running in a chroot (--root was used or sd_booted() reports that systemd is not running), | |
296 | * always use LOG_DEBUG. We may be called to initialize a chroot before booting and there is no | |
297 | * expectation that machine-id and other files will be populated. | |
298 | */ | |
299 | ||
300 | int log_level = notified || arg_root || running_in_chroot() > 0 ? | |
301 | LOG_DEBUG : LOG_NOTICE; | |
4cef1923 | 302 | |
d02933fd | 303 | log_syntax(NULL, |
1401882f | 304 | log_level, |
d02933fd | 305 | filename, line, 0, |
1401882f | 306 | "Failed to resolve specifier: %s, skipping.", |
c5be1ceb | 307 | arg_user ? "Required $XDG_... variable not defined" : "uninitialized /etc/ detected"); |
4cef1923 FB |
308 | |
309 | if (!notified) | |
1401882f ZJS |
310 | log_full(log_level, |
311 | "All rules containing unresolvable specifiers will be skipped."); | |
4cef1923 FB |
312 | |
313 | notified = true; | |
314 | return 0; | |
315 | } | |
316 | ||
f2b5ca0e ZJS |
317 | static int user_config_paths(char*** ret) { |
318 | _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; | |
319 | _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL; | |
320 | _cleanup_strv_free_ char **res = NULL; | |
321 | int r; | |
322 | ||
323 | r = xdg_user_dirs(&config_dirs, &data_dirs); | |
324 | if (r < 0) | |
325 | return r; | |
326 | ||
327 | r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d"); | |
6db98098 | 328 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
329 | return r; |
330 | ||
331 | r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d"); | |
6db98098 | 332 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
333 | return r; |
334 | ||
335 | r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d"); | |
6db98098 | 336 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
337 | return r; |
338 | ||
339 | r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d"); | |
340 | if (r < 0) | |
341 | return r; | |
342 | ||
343 | r = strv_extend(&res, persistent_config); | |
344 | if (r < 0) | |
345 | return r; | |
346 | ||
347 | r = strv_extend(&res, runtime_config); | |
348 | if (r < 0) | |
349 | return r; | |
350 | ||
351 | r = strv_extend(&res, data_home); | |
352 | if (r < 0) | |
353 | return r; | |
354 | ||
355 | r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d"); | |
356 | if (r < 0) | |
357 | return r; | |
358 | ||
359 | r = path_strv_make_absolute_cwd(res); | |
360 | if (r < 0) | |
361 | return r; | |
362 | ||
ae2a15bc | 363 | *ret = TAKE_PTR(res); |
f2b5ca0e ZJS |
364 | return 0; |
365 | } | |
366 | ||
66ccd038 | 367 | static bool needs_glob(ItemType t) { |
cde684a2 LP |
368 | return IN_SET(t, |
369 | WRITE_FILE, | |
370 | IGNORE_PATH, | |
371 | IGNORE_DIRECTORY_PATH, | |
372 | REMOVE_PATH, | |
373 | RECURSIVE_REMOVE_PATH, | |
df8dee85 | 374 | EMPTY_DIRECTORY, |
e73a03e0 | 375 | ADJUST_MODE, |
cde684a2 | 376 | RELABEL_PATH, |
b705ab6a ZJS |
377 | RECURSIVE_RELABEL_PATH, |
378 | SET_XATTR, | |
379 | RECURSIVE_SET_XATTR, | |
380 | SET_ACL, | |
34f64536 LP |
381 | RECURSIVE_SET_ACL, |
382 | SET_ATTRIBUTE, | |
383 | RECURSIVE_SET_ATTRIBUTE); | |
b8bb3e8f LP |
384 | } |
385 | ||
3f93da98 ZJS |
386 | static bool takes_ownership(ItemType t) { |
387 | return IN_SET(t, | |
388 | CREATE_FILE, | |
389 | TRUNCATE_FILE, | |
390 | CREATE_DIRECTORY, | |
df8dee85 | 391 | EMPTY_DIRECTORY, |
3f93da98 ZJS |
392 | TRUNCATE_DIRECTORY, |
393 | CREATE_SUBVOLUME, | |
5fb13eb5 LP |
394 | CREATE_SUBVOLUME_INHERIT_QUOTA, |
395 | CREATE_SUBVOLUME_NEW_QUOTA, | |
3f93da98 ZJS |
396 | CREATE_FIFO, |
397 | CREATE_SYMLINK, | |
398 | CREATE_CHAR_DEVICE, | |
399 | CREATE_BLOCK_DEVICE, | |
400 | COPY_FILES, | |
3f93da98 ZJS |
401 | WRITE_FILE, |
402 | IGNORE_PATH, | |
403 | IGNORE_DIRECTORY_PATH, | |
404 | REMOVE_PATH, | |
405 | RECURSIVE_REMOVE_PATH); | |
406 | } | |
407 | ||
ef43a391 | 408 | static struct Item* find_glob(OrderedHashmap *h, const char *match) { |
3f93da98 | 409 | ItemArray *j; |
b8bb3e8f | 410 | |
90e74a66 | 411 | ORDERED_HASHMAP_FOREACH(j, h) { |
96d10d78 | 412 | size_t n; |
3f93da98 | 413 | |
96d10d78 | 414 | for (n = 0; n < j->n_items; n++) { |
3f93da98 ZJS |
415 | Item *item = j->items + n; |
416 | ||
417 | if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0) | |
418 | return item; | |
419 | } | |
420 | } | |
b8bb3e8f LP |
421 | |
422 | return NULL; | |
423 | } | |
424 | ||
71a0be76 | 425 | static int load_unix_sockets(void) { |
fe2f05b7 | 426 | _cleanup_set_free_ Set *sockets = NULL; |
7fd1b19b | 427 | _cleanup_fclose_ FILE *f = NULL; |
f1ff734f | 428 | int r; |
17b90525 LP |
429 | |
430 | if (unix_sockets) | |
71a0be76 | 431 | return 0; |
17b90525 | 432 | |
f1ff734f | 433 | /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */ |
17b90525 | 434 | |
fdcad0c2 | 435 | f = fopen("/proc/net/unix", "re"); |
71a0be76 YW |
436 | if (!f) |
437 | return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, | |
438 | "Failed to open /proc/net/unix, ignoring: %m"); | |
17b90525 | 439 | |
fdcad0c2 | 440 | /* Skip header */ |
f1ff734f | 441 | r = read_line(f, LONG_LINE_MAX, NULL); |
71a0be76 YW |
442 | if (r < 0) |
443 | return log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m"); | |
444 | if (r == 0) | |
445 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "Premature end of file reading /proc/net/unix."); | |
17b90525 LP |
446 | |
447 | for (;;) { | |
fe2f05b7 | 448 | _cleanup_free_ char *line = NULL; |
71a0be76 | 449 | char *p; |
17b90525 | 450 | |
f1ff734f | 451 | r = read_line(f, LONG_LINE_MAX, &line); |
71a0be76 YW |
452 | if (r < 0) |
453 | return log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m"); | |
f1ff734f | 454 | if (r == 0) /* EOF */ |
17b90525 LP |
455 | break; |
456 | ||
fdcad0c2 LP |
457 | p = strchr(line, ':'); |
458 | if (!p) | |
459 | continue; | |
460 | ||
461 | if (strlen(p) < 37) | |
17b90525 LP |
462 | continue; |
463 | ||
fdcad0c2 | 464 | p += 37; |
17b90525 | 465 | p += strspn(p, WHITESPACE); |
fdcad0c2 | 466 | p += strcspn(p, WHITESPACE); /* skip one more word */ |
17b90525 LP |
467 | p += strspn(p, WHITESPACE); |
468 | ||
fe2f05b7 | 469 | if (!path_is_absolute(p)) |
17b90525 LP |
470 | continue; |
471 | ||
fe2f05b7 | 472 | r = set_put_strdup_full(&sockets, &path_hash_ops_free, p); |
71a0be76 YW |
473 | if (r < 0) |
474 | return log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m"); | |
71a0be76 | 475 | } |
17b90525 | 476 | |
71a0be76 YW |
477 | unix_sockets = TAKE_PTR(sockets); |
478 | return 1; | |
17b90525 LP |
479 | } |
480 | ||
481 | static bool unix_socket_alive(const char *fn) { | |
482 | assert(fn); | |
483 | ||
71a0be76 YW |
484 | if (load_unix_sockets() < 0) |
485 | return true; /* We don't know, so assume yes */ | |
17b90525 | 486 | |
fe2f05b7 | 487 | return set_contains(unix_sockets, fn); |
17b90525 LP |
488 | } |
489 | ||
708daf42 LP |
490 | /* Accessors for the argument in binary format */ |
491 | static const void* item_binary_argument(const Item *i) { | |
492 | assert(i); | |
493 | return i->binary_argument ?: i->argument; | |
494 | } | |
495 | ||
496 | static size_t item_binary_argument_size(const Item *i) { | |
497 | assert(i); | |
498 | return i->binary_argument ? i->binary_argument_size : strlen_ptr(i->argument); | |
499 | } | |
500 | ||
df99a9ef ZJS |
501 | static DIR* xopendirat_nomod(int dirfd, const char *path) { |
502 | DIR *dir; | |
503 | ||
504 | dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME); | |
1532227a LP |
505 | if (dir) |
506 | return dir; | |
507 | ||
508 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
509 | if (errno != EPERM) | |
510 | return NULL; | |
511 | ||
512 | dir = xopendirat(dirfd, path, O_NOFOLLOW); | |
513 | if (!dir) | |
514 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
df99a9ef ZJS |
515 | |
516 | return dir; | |
517 | } | |
518 | ||
519 | static DIR* opendir_nomod(const char *path) { | |
520 | return xopendirat_nomod(AT_FDCWD, path); | |
521 | } | |
522 | ||
ddb439b8 LP |
523 | static inline nsec_t load_statx_timestamp_nsec(const struct statx_timestamp *ts) { |
524 | assert(ts); | |
525 | ||
526 | if (ts->tv_sec < 0) | |
527 | return NSEC_INFINITY; | |
528 | ||
529 | if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC) | |
530 | return NSEC_INFINITY; | |
531 | ||
532 | return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec; | |
533 | } | |
534 | ||
7f7a50dd SK |
535 | static bool needs_cleanup( |
536 | nsec_t atime, | |
537 | nsec_t btime, | |
538 | nsec_t ctime, | |
539 | nsec_t mtime, | |
540 | nsec_t cutoff, | |
541 | const char *sub_path, | |
542 | AgeBy age_by, | |
543 | bool is_dir) { | |
544 | ||
545 | if (FLAGS_SET(age_by, AGE_BY_MTIME) && mtime != NSEC_INFINITY && mtime >= cutoff) { | |
7f7a50dd SK |
546 | /* Follows spelling in stat(1). */ |
547 | log_debug("%s \"%s\": modify time %s is too new.", | |
548 | is_dir ? "Directory" : "File", | |
549 | sub_path, | |
0086ef19 | 550 | FORMAT_TIMESTAMP_STYLE(mtime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
551 | |
552 | return false; | |
553 | } | |
554 | ||
555 | if (FLAGS_SET(age_by, AGE_BY_ATIME) && atime != NSEC_INFINITY && atime >= cutoff) { | |
7f7a50dd SK |
556 | log_debug("%s \"%s\": access time %s is too new.", |
557 | is_dir ? "Directory" : "File", | |
558 | sub_path, | |
0086ef19 | 559 | FORMAT_TIMESTAMP_STYLE(atime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
560 | |
561 | return false; | |
562 | } | |
563 | ||
564 | /* | |
565 | * Note: Unless explicitly specified by the user, "ctime" is ignored | |
566 | * by default for directories, because we change it when deleting. | |
567 | */ | |
568 | if (FLAGS_SET(age_by, AGE_BY_CTIME) && ctime != NSEC_INFINITY && ctime >= cutoff) { | |
7f7a50dd SK |
569 | log_debug("%s \"%s\": change time %s is too new.", |
570 | is_dir ? "Directory" : "File", | |
571 | sub_path, | |
0086ef19 | 572 | FORMAT_TIMESTAMP_STYLE(ctime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
573 | |
574 | return false; | |
575 | } | |
576 | ||
577 | if (FLAGS_SET(age_by, AGE_BY_BTIME) && btime != NSEC_INFINITY && btime >= cutoff) { | |
7f7a50dd SK |
578 | log_debug("%s \"%s\": birth time %s is too new.", |
579 | is_dir ? "Directory" : "File", | |
580 | sub_path, | |
0086ef19 | 581 | FORMAT_TIMESTAMP_STYLE(btime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
582 | |
583 | return false; | |
584 | } | |
585 | ||
586 | return true; | |
587 | } | |
588 | ||
3b63d2d3 | 589 | static int dir_cleanup( |
78a92a5a | 590 | Item *i, |
3b63d2d3 LP |
591 | const char *p, |
592 | DIR *d, | |
ddb439b8 LP |
593 | nsec_t self_atime_nsec, |
594 | nsec_t self_mtime_nsec, | |
595 | nsec_t cutoff_nsec, | |
596 | dev_t rootdev_major, | |
597 | dev_t rootdev_minor, | |
3b63d2d3 | 598 | bool mountpoint, |
24f3a374 | 599 | int maxdepth, |
7f7a50dd SK |
600 | bool keep_this_level, |
601 | AgeBy age_by_file, | |
602 | AgeBy age_by_dir) { | |
265ffa1e | 603 | |
3b63d2d3 | 604 | bool deleted = false; |
3b63d2d3 LP |
605 | int r = 0; |
606 | ||
c7f0d9e5 | 607 | FOREACH_DIRENT_ALL(de, d, break) { |
7fd1b19b | 608 | _cleanup_free_ char *sub_path = NULL; |
ddb439b8 | 609 | nsec_t atime_nsec, mtime_nsec, ctime_nsec, btime_nsec; |
3b63d2d3 | 610 | |
c7f0d9e5 | 611 | if (dot_or_dot_dot(de->d_name)) |
3b63d2d3 | 612 | continue; |
5008d581 | 613 | |
ca194a2a | 614 | /* If statx() is supported, use it. It's preferable over fstatat() since it tells us |
69e3234d | 615 | * explicitly where we are looking at a mount point, for free as side information. Determining |
ca194a2a LP |
616 | * the same information without statx() is hard, see the complexity of path_is_mount_point(), |
617 | * and also much slower as it requires a number of syscalls instead of just one. Hence, when | |
618 | * we have modern statx() we use it instead of fstat() and do proper mount point checks, | |
619 | * while on older kernels's well do traditional st_dev based detection of mount points. | |
620 | * | |
621 | * Using statx() for detecting mount points also has the benfit that we handle weird file | |
622 | * systems such as overlayfs better where each file is originating from a different | |
623 | * st_dev. */ | |
624 | ||
625 | STRUCT_STATX_DEFINE(sx); | |
626 | ||
627 | r = statx_fallback( | |
c7f0d9e5 | 628 | dirfd(d), de->d_name, |
ca194a2a LP |
629 | AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT, |
630 | STATX_TYPE|STATX_MODE|STATX_UID|STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_BTIME, | |
631 | &sx); | |
632 | if (r == -ENOENT) | |
633 | continue; | |
634 | if (r < 0) { | |
635 | /* FUSE, NFS mounts, SELinux might return EACCES */ | |
149e0ca6 | 636 | r = log_full_errno(r == -EACCES ? LOG_DEBUG : LOG_ERR, r, |
c7f0d9e5 | 637 | "statx(%s/%s) failed: %m", p, de->d_name); |
ca194a2a | 638 | continue; |
3b63d2d3 LP |
639 | } |
640 | ||
ca194a2a LP |
641 | if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) { |
642 | /* Yay, we have the mount point API, use it */ | |
643 | if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT)) { | |
c7f0d9e5 | 644 | log_debug("Ignoring \"%s/%s\": different mount points.", p, de->d_name); |
ddb439b8 LP |
645 | continue; |
646 | } | |
ca194a2a LP |
647 | } else { |
648 | /* So we might have statx() but the STATX_ATTR_MOUNT_ROOT flag is not supported, fall | |
649 | * back to traditional stx_dev checking. */ | |
650 | if (sx.stx_dev_major != rootdev_major || | |
651 | sx.stx_dev_minor != rootdev_minor) { | |
c7f0d9e5 | 652 | log_debug("Ignoring \"%s/%s\": different filesystem.", p, de->d_name); |
ddb439b8 LP |
653 | continue; |
654 | } | |
655 | ||
ca194a2a LP |
656 | /* Try to detect bind mounts of the same filesystem instance; they do not differ in device |
657 | * major/minors. This type of query is not supported on all kernels or filesystem types | |
658 | * though. */ | |
659 | if (S_ISDIR(sx.stx_mode)) { | |
660 | int q; | |
3b63d2d3 | 661 | |
c7f0d9e5 | 662 | q = fd_is_mount_point(dirfd(d), de->d_name, 0); |
ca194a2a | 663 | if (q < 0) |
c7f0d9e5 | 664 | log_debug_errno(q, "Failed to determine whether \"%s/%s\" is a mount point, ignoring: %m", p, de->d_name); |
ca194a2a | 665 | else if (q > 0) { |
c7f0d9e5 | 666 | log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.", p, de->d_name); |
ca194a2a LP |
667 | continue; |
668 | } | |
60bdc0ca | 669 | } |
582deb84 | 670 | } |
99d680ac | 671 | |
ca194a2a LP |
672 | atime_nsec = FLAGS_SET(sx.stx_mask, STATX_ATIME) ? load_statx_timestamp_nsec(&sx.stx_atime) : 0; |
673 | mtime_nsec = FLAGS_SET(sx.stx_mask, STATX_MTIME) ? load_statx_timestamp_nsec(&sx.stx_mtime) : 0; | |
674 | ctime_nsec = FLAGS_SET(sx.stx_mask, STATX_CTIME) ? load_statx_timestamp_nsec(&sx.stx_ctime) : 0; | |
675 | btime_nsec = FLAGS_SET(sx.stx_mask, STATX_BTIME) ? load_statx_timestamp_nsec(&sx.stx_btime) : 0; | |
676 | ||
c7f0d9e5 | 677 | sub_path = path_join(p, de->d_name); |
cde684a2 | 678 | if (!sub_path) { |
0d0f0c50 | 679 | r = log_oom(); |
3b63d2d3 LP |
680 | goto finish; |
681 | } | |
682 | ||
683 | /* Is there an item configured for this path? */ | |
ef43a391 | 684 | if (ordered_hashmap_get(items, sub_path)) { |
582deb84 | 685 | log_debug("Ignoring \"%s\": a separate entry exists.", sub_path); |
3b63d2d3 | 686 | continue; |
582deb84 | 687 | } |
3b63d2d3 | 688 | |
582deb84 ZJS |
689 | if (find_glob(globs, sub_path)) { |
690 | log_debug("Ignoring \"%s\": a separate glob exists.", sub_path); | |
b8bb3e8f | 691 | continue; |
582deb84 | 692 | } |
b8bb3e8f | 693 | |
ca194a2a | 694 | if (S_ISDIR(sx.stx_mode)) { |
52b32b2a | 695 | _cleanup_closedir_ DIR *sub_dir = NULL; |
3b63d2d3 LP |
696 | |
697 | if (mountpoint && | |
c7f0d9e5 | 698 | streq(de->d_name, "lost+found") && |
ca194a2a | 699 | sx.stx_uid == 0) { |
781bc44e | 700 | log_debug("Ignoring directory \"%s\".", sub_path); |
3b63d2d3 | 701 | continue; |
582deb84 | 702 | } |
3b63d2d3 LP |
703 | |
704 | if (maxdepth <= 0) | |
582deb84 | 705 | log_warning("Reached max depth on \"%s\".", sub_path); |
3b63d2d3 | 706 | else { |
3b63d2d3 LP |
707 | int q; |
708 | ||
c7f0d9e5 | 709 | sub_dir = xopendirat_nomod(dirfd(d), de->d_name); |
cde684a2 | 710 | if (!sub_dir) { |
582deb84 | 711 | if (errno != ENOENT) |
781bc44e | 712 | r = log_warning_errno(errno, "Opening directory \"%s\" failed, ignoring: %m", sub_path); |
3b63d2d3 LP |
713 | |
714 | continue; | |
715 | } | |
716 | ||
52b32b2a LP |
717 | if (flock(dirfd(sub_dir), LOCK_EX|LOCK_NB) < 0) { |
718 | log_debug_errno(errno, "Couldn't acquire shared BSD lock on directory \"%s\", skipping: %m", p); | |
719 | continue; | |
720 | } | |
721 | ||
ddb439b8 LP |
722 | q = dir_cleanup(i, |
723 | sub_path, sub_dir, | |
724 | atime_nsec, mtime_nsec, cutoff_nsec, | |
725 | rootdev_major, rootdev_minor, | |
7f7a50dd SK |
726 | false, maxdepth-1, false, |
727 | age_by_file, age_by_dir); | |
3b63d2d3 LP |
728 | if (q < 0) |
729 | r = q; | |
730 | } | |
731 | ||
a23fabc6 LP |
732 | /* Note: if you are wondering why we don't support the sticky bit for excluding |
733 | * directories from cleaning like we do it for other file system objects: well, the | |
734 | * sticky bit already has a meaning for directories, so we don't want to overload | |
735 | * that. */ | |
24f3a374 | 736 | |
582deb84 | 737 | if (keep_this_level) { |
781bc44e | 738 | log_debug("Keeping directory \"%s\".", sub_path); |
24f3a374 | 739 | continue; |
582deb84 | 740 | } |
24f3a374 | 741 | |
7f7a50dd SK |
742 | /* |
743 | * Check the file timestamps of an entry against the | |
744 | * given cutoff time; delete if it is older. | |
745 | */ | |
746 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, | |
747 | cutoff_nsec, sub_path, age_by_dir, true)) | |
3b63d2d3 LP |
748 | continue; |
749 | ||
61253220 | 750 | log_debug("Removing directory \"%s\".", sub_path); |
c7f0d9e5 | 751 | if (unlinkat(dirfd(d), de->d_name, AT_REMOVEDIR) < 0) |
920ce828 | 752 | if (!IN_SET(errno, ENOENT, ENOTEMPTY)) |
781bc44e | 753 | r = log_warning_errno(errno, "Failed to remove directory \"%s\", ignoring: %m", sub_path); |
3b63d2d3 LP |
754 | |
755 | } else { | |
a23fabc6 LP |
756 | /* Skip files for which the sticky bit is set. These are semantics we define, and are |
757 | * unknown elsewhere. See XDG_RUNTIME_DIR specification for details. */ | |
ca194a2a | 758 | if (sx.stx_mode & S_ISVTX) { |
582deb84 | 759 | log_debug("Skipping \"%s\": sticky bit set.", sub_path); |
9c73736d | 760 | continue; |
582deb84 | 761 | } |
9c73736d | 762 | |
20b6bb95 | 763 | if (mountpoint && |
ca194a2a LP |
764 | S_ISREG(sx.stx_mode) && |
765 | sx.stx_uid == 0 && | |
c7f0d9e5 | 766 | STR_IN_SET(de->d_name, |
20b6bb95 LP |
767 | ".journal", |
768 | "aquota.user", | |
769 | "aquota.group")) { | |
770 | log_debug("Skipping \"%s\".", sub_path); | |
771 | continue; | |
772 | } | |
3b63d2d3 | 773 | |
17b90525 | 774 | /* Ignore sockets that are listed in /proc/net/unix */ |
ca194a2a | 775 | if (S_ISSOCK(sx.stx_mode) && unix_socket_alive(sub_path)) { |
582deb84 | 776 | log_debug("Skipping \"%s\": live socket.", sub_path); |
17b90525 | 777 | continue; |
582deb84 | 778 | } |
17b90525 | 779 | |
78ab08eb | 780 | /* Ignore device nodes */ |
ca194a2a | 781 | if (S_ISCHR(sx.stx_mode) || S_ISBLK(sx.stx_mode)) { |
582deb84 | 782 | log_debug("Skipping \"%s\": a device.", sub_path); |
78ab08eb | 783 | continue; |
582deb84 | 784 | } |
78ab08eb | 785 | |
a23fabc6 | 786 | /* Keep files on this level around if this is requested */ |
582deb84 ZJS |
787 | if (keep_this_level) { |
788 | log_debug("Keeping \"%s\".", sub_path); | |
24f3a374 | 789 | continue; |
582deb84 | 790 | } |
24f3a374 | 791 | |
7f7a50dd SK |
792 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, |
793 | cutoff_nsec, sub_path, age_by_file, false)) | |
3b63d2d3 | 794 | continue; |
3b63d2d3 | 795 | |
781bc44e | 796 | log_debug("Removing \"%s\".", sub_path); |
c7f0d9e5 | 797 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) |
582deb84 | 798 | if (errno != ENOENT) |
781bc44e | 799 | r = log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", sub_path); |
582deb84 | 800 | |
3b63d2d3 LP |
801 | deleted = true; |
802 | } | |
803 | } | |
804 | ||
805 | finish: | |
806 | if (deleted) { | |
ddb439b8 | 807 | struct timespec ts[2]; |
76e15a9c | 808 | |
582deb84 ZJS |
809 | log_debug("Restoring access and modification time on \"%s\": %s, %s", |
810 | p, | |
0086ef19 ZJS |
811 | FORMAT_TIMESTAMP_STYLE(self_atime_nsec / NSEC_PER_USEC, TIMESTAMP_US), |
812 | FORMAT_TIMESTAMP_STYLE(self_mtime_nsec / NSEC_PER_USEC, TIMESTAMP_US)); | |
ddb439b8 LP |
813 | |
814 | timespec_store_nsec(ts + 0, self_atime_nsec); | |
815 | timespec_store_nsec(ts + 1, self_mtime_nsec); | |
76e15a9c LP |
816 | |
817 | /* Restore original directory timestamps */ | |
ddb439b8 | 818 | if (futimens(dirfd(d), ts) < 0) |
76e15a9c | 819 | log_warning_errno(errno, "Failed to revert timestamps of '%s', ignoring: %m", p); |
3b63d2d3 LP |
820 | } |
821 | ||
3b63d2d3 LP |
822 | return r; |
823 | } | |
824 | ||
5579f856 LP |
825 | static bool dangerous_hardlinks(void) { |
826 | _cleanup_free_ char *value = NULL; | |
827 | static int cached = -1; | |
828 | int r; | |
829 | ||
830 | /* Check whether the fs.protected_hardlinks sysctl is on. If we can't determine it we assume its off, as that's | |
831 | * what the upstream default is. */ | |
832 | ||
833 | if (cached >= 0) | |
834 | return cached; | |
835 | ||
836 | r = read_one_line_file("/proc/sys/fs/protected_hardlinks", &value); | |
837 | if (r < 0) { | |
838 | log_debug_errno(r, "Failed to read fs.protected_hardlinks sysctl: %m"); | |
839 | return true; | |
840 | } | |
841 | ||
842 | r = parse_boolean(value); | |
843 | if (r < 0) { | |
844 | log_debug_errno(r, "Failed to parse fs.protected_hardlinks sysctl: %m"); | |
845 | return true; | |
846 | } | |
847 | ||
848 | cached = r == 0; | |
849 | return cached; | |
850 | } | |
851 | ||
774f79b5 | 852 | static bool hardlink_vulnerable(const struct stat *st) { |
5579f856 LP |
853 | assert(st); |
854 | ||
855 | return !S_ISDIR(st->st_mode) && st->st_nlink > 1 && dangerous_hardlinks(); | |
856 | } | |
857 | ||
8b364a38 LP |
858 | static mode_t process_mask_perms(mode_t mode, mode_t current) { |
859 | ||
860 | if ((current & 0111) == 0) | |
861 | mode &= ~0111; | |
862 | if ((current & 0222) == 0) | |
863 | mode &= ~0222; | |
864 | if ((current & 0444) == 0) | |
865 | mode &= ~0444; | |
866 | if (!S_ISDIR(current)) | |
867 | mode &= ~07000; /* remove sticky/sgid/suid bit, unless directory */ | |
868 | ||
869 | return mode; | |
870 | } | |
871 | ||
a9bc518c LP |
872 | static int fd_set_perms( |
873 | Item *i, | |
874 | int fd, | |
875 | const char *path, | |
876 | const struct stat *st, | |
877 | CreationMode creation) { | |
878 | ||
cc43328c | 879 | bool do_chown, do_chmod; |
b206ac8e | 880 | struct stat stbuf; |
a9f55d0e | 881 | mode_t new_mode; |
cc43328c LP |
882 | uid_t new_uid; |
883 | gid_t new_gid; | |
dee00c19 | 884 | int r; |
1924a97d | 885 | |
cde684a2 | 886 | assert(i); |
b4b0f87c | 887 | assert(fd >= 0); |
4dc7bfdf | 888 | assert(path); |
48b8aaa8 | 889 | |
936f6bdb FB |
890 | if (!i->mode_set && !i->uid_set && !i->gid_set) |
891 | goto shortcut; | |
1924a97d | 892 | |
b206ac8e FB |
893 | if (!st) { |
894 | if (fstat(fd, &stbuf) < 0) | |
895 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
896 | st = &stbuf; | |
897 | } | |
898 | ||
baaa35ad ZJS |
899 | if (hardlink_vulnerable(st)) |
900 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
901 | "Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
902 | path); | |
cc43328c LP |
903 | new_uid = i->uid_set && (creation != CREATION_EXISTING || !i->uid_only_create) ? i->uid : st->st_uid; |
904 | new_gid = i->gid_set && (creation != CREATION_EXISTING || !i->gid_only_create) ? i->gid : st->st_gid; | |
5579f856 | 905 | |
a9f55d0e | 906 | /* Do we need a chown()? */ |
cc43328c | 907 | do_chown = (new_uid != st->st_uid) || (new_gid != st->st_gid); |
8b364a38 | 908 | |
a9f55d0e | 909 | /* Calculate the mode to apply */ |
cc43328c LP |
910 | new_mode = i->mode_set && (creation != CREATION_EXISTING || !i->mode_only_create) ? |
911 | (i->mask_perms ? process_mask_perms(i->mode, st->st_mode) : i->mode) : | |
912 | (st->st_mode & 07777); | |
a9f55d0e | 913 | |
cc43328c LP |
914 | do_chmod = ((new_mode ^ st->st_mode) & 07777) != 0; |
915 | ||
916 | if (do_chmod && do_chown) { | |
a9f55d0e LP |
917 | /* Before we issue the chmod() let's reduce the access mode to the common bits of the old and |
918 | * the new mode. That way there's no time window where the file exists under the old owner | |
919 | * with more than the old access modes — and not under the new owner with more than the new | |
920 | * access modes either. */ | |
8b364a38 | 921 | |
936f6bdb | 922 | if (S_ISLNK(st->st_mode)) |
a9f55d0e | 923 | log_debug("Skipping temporary mode fix for symlink %s.", path); |
51207ca1 | 924 | else { |
a9f55d0e | 925 | mode_t m = new_mode & st->st_mode; /* Mask new mode by old mode */ |
abef3f91 | 926 | |
a9f55d0e LP |
927 | if (((m ^ st->st_mode) & 07777) == 0) |
928 | log_debug("\"%s\" matches temporary mode %o already.", path, m); | |
48b8aaa8 | 929 | else { |
a9f55d0e | 930 | log_debug("Temporarily changing \"%s\" to mode %o.", path, m); |
dee00c19 LP |
931 | r = fchmod_opath(fd, m); |
932 | if (r < 0) | |
933 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
48b8aaa8 | 934 | } |
062e01bb | 935 | } |
51207ca1 | 936 | } |
062e01bb | 937 | |
a9f55d0e | 938 | if (do_chown) { |
cc43328c LP |
939 | log_debug("Changing \"%s\" to owner "UID_FMT":"GID_FMT, path, new_uid, new_gid); |
940 | ||
941 | if (fchownat(fd, "", | |
942 | new_uid != st->st_uid ? new_uid : UID_INVALID, | |
943 | new_gid != st->st_gid ? new_gid : GID_INVALID, | |
936f6bdb FB |
944 | AT_EMPTY_PATH) < 0) |
945 | return log_error_errno(errno, "fchownat() of %s failed: %m", path); | |
582deb84 | 946 | } |
062e01bb | 947 | |
a9f55d0e LP |
948 | /* Now, apply the final mode. We do this in two cases: when the user set a mode explicitly, or after a |
949 | * chown(), since chown()'s mangle the access mode in regards to sgid/suid in some conditions. */ | |
cc43328c | 950 | if (do_chmod || do_chown) { |
a9f55d0e LP |
951 | if (S_ISLNK(st->st_mode)) |
952 | log_debug("Skipping mode fix for symlink %s.", path); | |
953 | else { | |
cc43328c LP |
954 | log_debug("Changing \"%s\" to mode %o.", path, new_mode); |
955 | r = fchmod_opath(fd, new_mode); | |
956 | if (r < 0) | |
957 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
a9f55d0e LP |
958 | } |
959 | } | |
960 | ||
59793c8f | 961 | shortcut: |
03bc11d1 | 962 | return label_fix_full(fd, /* inode_path= */ NULL, /* label_path= */ path, 0); |
062e01bb MS |
963 | } |
964 | ||
5ec9d065 FB |
965 | static int path_open_parent_safe(const char *path) { |
966 | _cleanup_free_ char *dn = NULL; | |
a5648b80 | 967 | int r, fd; |
5ec9d065 | 968 | |
4609d3bb LP |
969 | if (!path_is_normalized(path)) |
970 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to open parent of '%s': path not normalized.", path); | |
5ec9d065 | 971 | |
4609d3bb LP |
972 | r = path_extract_directory(path, &dn); |
973 | if (r < 0) | |
974 | return log_error_errno(r, "Unable to determine parent directory of '%s': %m", path); | |
5ec9d065 | 975 | |
a5648b80 | 976 | r = chase_symlinks(dn, arg_root, CHASE_SAFE|CHASE_WARN, NULL, &fd); |
4609d3bb LP |
977 | if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */ |
978 | return r; | |
979 | if (r < 0) | |
980 | return log_error_errno(r, "Failed to open path '%s': %m", dn); | |
5ec9d065 | 981 | |
4609d3bb | 982 | return fd; |
5ec9d065 FB |
983 | } |
984 | ||
addc3e30 | 985 | static int path_open_safe(const char *path) { |
a5648b80 | 986 | int r, fd; |
addc3e30 FB |
987 | |
988 | /* path_open_safe() returns a file descriptor opened with O_PATH after | |
989 | * verifying that the path doesn't contain unsafe transitions, except | |
990 | * for its final component as the function does not follow symlink. */ | |
991 | ||
992 | assert(path); | |
993 | ||
baaa35ad | 994 | if (!path_is_normalized(path)) |
4609d3bb | 995 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to open invalid path '%s'.", path); |
addc3e30 | 996 | |
a5648b80 | 997 | r = chase_symlinks(path, arg_root, CHASE_SAFE|CHASE_WARN|CHASE_NOFOLLOW, NULL, &fd); |
4609d3bb LP |
998 | if (r == -ENOLINK) |
999 | return r; /* Unsafe symlink: already covered by CHASE_WARN */ | |
1000 | if (r < 0) | |
1001 | return log_error_errno(r, "Failed to open path %s: %m", path); | |
addc3e30 | 1002 | |
4609d3bb | 1003 | return fd; |
addc3e30 FB |
1004 | } |
1005 | ||
a9bc518c LP |
1006 | static int path_set_perms( |
1007 | Item *i, | |
1008 | const char *path, | |
1009 | CreationMode creation) { | |
1010 | ||
936f6bdb | 1011 | _cleanup_close_ int fd = -1; |
936f6bdb FB |
1012 | |
1013 | assert(i); | |
1014 | assert(path); | |
1015 | ||
addc3e30 | 1016 | fd = path_open_safe(path); |
54946021 | 1017 | if (fd < 0) |
addc3e30 | 1018 | return fd; |
d460ba18 | 1019 | |
a9bc518c | 1020 | return fd_set_perms(i, fd, path, /* st= */ NULL, creation); |
936f6bdb FB |
1021 | } |
1022 | ||
bd550f78 | 1023 | static int parse_xattrs_from_arg(Item *i) { |
ebf4e801 MW |
1024 | const char *p; |
1025 | int r; | |
1026 | ||
1027 | assert(i); | |
ebf4e801 | 1028 | |
3a632fc1 | 1029 | assert_se(p = i->argument); |
4034a06d | 1030 | for (;;) { |
4cef1923 | 1031 | _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL; |
4034a06d | 1032 | |
4ec85141 | 1033 | r = extract_first_word(&p, &xattr, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE); |
4034a06d | 1034 | if (r < 0) |
bd550f78 | 1035 | log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p); |
4034a06d LP |
1036 | if (r <= 0) |
1037 | break; | |
505ef0e3 | 1038 | |
4cef1923 | 1039 | r = split_pair(xattr, "=", &name, &value); |
ebf4e801 | 1040 | if (r < 0) { |
4034a06d | 1041 | log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr); |
ebf4e801 MW |
1042 | continue; |
1043 | } | |
505ef0e3 | 1044 | |
4034a06d | 1045 | if (isempty(name) || isempty(value)) { |
bd550f78 | 1046 | log_warning("Malformed extended attribute found, ignoring: %s", xattr); |
ebf4e801 MW |
1047 | continue; |
1048 | } | |
505ef0e3 | 1049 | |
4034a06d | 1050 | if (strv_push_pair(&i->xattrs, name, value) < 0) |
ebf4e801 | 1051 | return log_oom(); |
505ef0e3 | 1052 | |
4034a06d | 1053 | name = value = NULL; |
ebf4e801 MW |
1054 | } |
1055 | ||
4034a06d | 1056 | return 0; |
ebf4e801 MW |
1057 | } |
1058 | ||
a9bc518c LP |
1059 | static int fd_set_xattrs( |
1060 | Item *i, | |
1061 | int fd, | |
1062 | const char *path, | |
1063 | const struct stat *st, | |
1064 | CreationMode creation) { | |
1065 | ||
ebf4e801 | 1066 | assert(i); |
b4b0f87c | 1067 | assert(fd >= 0); |
4dc7bfdf | 1068 | assert(path); |
936f6bdb | 1069 | |
ebf4e801 | 1070 | STRV_FOREACH_PAIR(name, value, i->xattrs) { |
bd550f78 | 1071 | log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path); |
ddb6eeaf | 1072 | if (setxattr(FORMAT_PROC_FD_PATH(fd), *name, *value, strlen(*value), 0) < 0) |
25f027c5 ZJS |
1073 | return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m", |
1074 | *name, *value, path); | |
ebf4e801 MW |
1075 | } |
1076 | return 0; | |
1077 | } | |
1078 | ||
a9bc518c LP |
1079 | static int path_set_xattrs( |
1080 | Item *i, | |
1081 | const char *path, | |
1082 | CreationMode creation) { | |
1083 | ||
936f6bdb FB |
1084 | _cleanup_close_ int fd = -1; |
1085 | ||
1086 | assert(i); | |
1087 | assert(path); | |
1088 | ||
addc3e30 | 1089 | fd = path_open_safe(path); |
936f6bdb | 1090 | if (fd < 0) |
addc3e30 | 1091 | return fd; |
936f6bdb | 1092 | |
a9bc518c | 1093 | return fd_set_xattrs(i, fd, path, /* st = */ NULL, creation); |
936f6bdb FB |
1094 | } |
1095 | ||
bd550f78 | 1096 | static int parse_acls_from_arg(Item *item) { |
349cc4a5 | 1097 | #if HAVE_ACL |
f8eeeaf9 | 1098 | int r; |
f8eeeaf9 ZJS |
1099 | |
1100 | assert(item); | |
1101 | ||
c55ac248 | 1102 | /* If append_or_force (= modify) is set, we will not modify the acl |
50d9e46d | 1103 | * afterwards, so the mask can be added now if necessary. */ |
bd550f78 | 1104 | |
c55ac248 | 1105 | r = parse_acl(item->argument, &item->acl_access, &item->acl_default, !item->append_or_force); |
f8eeeaf9 | 1106 | if (r < 0) |
4034a06d | 1107 | log_warning_errno(r, "Failed to parse ACL \"%s\": %m. Ignoring", item->argument); |
f8eeeaf9 | 1108 | #else |
6008336f | 1109 | log_warning("ACLs are not supported. Ignoring."); |
f8eeeaf9 ZJS |
1110 | #endif |
1111 | ||
1112 | return 0; | |
1113 | } | |
1114 | ||
349cc4a5 | 1115 | #if HAVE_ACL |
a9bc518c LP |
1116 | static int path_set_acl( |
1117 | const char *path, | |
1118 | const char *pretty, | |
1119 | acl_type_t type, | |
1120 | acl_t acl, | |
1121 | bool modify) { | |
1122 | ||
48b8aaa8 | 1123 | _cleanup_(acl_free_charpp) char *t = NULL; |
dd4105b0 | 1124 | _cleanup_(acl_freep) acl_t dup = NULL; |
50d9e46d ZJS |
1125 | int r; |
1126 | ||
d873e877 HPD |
1127 | /* Returns 0 for success, positive error if already warned, |
1128 | * negative error otherwise. */ | |
1129 | ||
50d9e46d | 1130 | if (modify) { |
dd4105b0 | 1131 | r = acls_for_file(path, type, acl, &dup); |
50d9e46d ZJS |
1132 | if (r < 0) |
1133 | return r; | |
50d9e46d | 1134 | |
dd4105b0 ZJS |
1135 | r = calc_acl_mask_if_needed(&dup); |
1136 | if (r < 0) | |
1137 | return r; | |
1138 | } else { | |
1139 | dup = acl_dup(acl); | |
1140 | if (!dup) | |
1141 | return -errno; | |
1142 | ||
1143 | /* the mask was already added earlier if needed */ | |
1144 | } | |
1145 | ||
1146 | r = add_base_acls_if_needed(&dup, path); | |
1147 | if (r < 0) | |
1148 | return r; | |
1149 | ||
582deb84 | 1150 | t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE); |
48b8aaa8 | 1151 | log_debug("Setting %s ACL %s on %s.", |
582deb84 | 1152 | type == ACL_TYPE_ACCESS ? "access" : "default", |
48b8aaa8 | 1153 | strna(t), pretty); |
50d9e46d | 1154 | |
582deb84 | 1155 | r = acl_set_file(path, type, dup); |
3045c416 PC |
1156 | if (r < 0) { |
1157 | if (ERRNO_IS_NOT_SUPPORTED(errno)) | |
1158 | /* No error if filesystem doesn't support ACLs. Return negative. */ | |
1159 | return -errno; | |
1160 | else | |
1161 | /* Return positive to indicate we already warned */ | |
1162 | return -log_error_errno(errno, | |
1163 | "Setting %s ACL \"%s\" on %s failed: %m", | |
1164 | type == ACL_TYPE_ACCESS ? "access" : "default", | |
1165 | strna(t), pretty); | |
1166 | } | |
582deb84 | 1167 | return 0; |
50d9e46d | 1168 | } |
35888b67 | 1169 | #endif |
50d9e46d | 1170 | |
a9bc518c LP |
1171 | static int fd_set_acls( |
1172 | Item *item, | |
1173 | int fd, | |
1174 | const char *path, | |
1175 | const struct stat *st, | |
1176 | CreationMode creation) { | |
1177 | ||
d873e877 | 1178 | int r = 0; |
349cc4a5 | 1179 | #if HAVE_ACL |
b206ac8e | 1180 | struct stat stbuf; |
48b8aaa8 | 1181 | |
f8eeeaf9 | 1182 | assert(item); |
b4b0f87c | 1183 | assert(fd >= 0); |
4dc7bfdf | 1184 | assert(path); |
48b8aaa8 | 1185 | |
b206ac8e FB |
1186 | if (!st) { |
1187 | if (fstat(fd, &stbuf) < 0) | |
1188 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1189 | st = &stbuf; | |
1190 | } | |
1191 | ||
baaa35ad ZJS |
1192 | if (hardlink_vulnerable(st)) |
1193 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
1194 | "Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
1195 | path); | |
5579f856 | 1196 | |
936f6bdb | 1197 | if (S_ISLNK(st->st_mode)) { |
48b8aaa8 LP |
1198 | log_debug("Skipping ACL fix for symlink %s.", path); |
1199 | return 0; | |
1200 | } | |
1201 | ||
d873e877 | 1202 | if (item->acl_access) |
ddb6eeaf | 1203 | r = path_set_acl(FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_ACCESS, item->acl_access, item->append_or_force); |
f8eeeaf9 | 1204 | |
2667385b RM |
1205 | /* set only default acls to folders */ |
1206 | if (r == 0 && item->acl_default && S_ISDIR(st->st_mode)) | |
ddb6eeaf | 1207 | r = path_set_acl(FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_DEFAULT, item->acl_default, item->append_or_force); |
f8eeeaf9 | 1208 | |
3045c416 PC |
1209 | if (ERRNO_IS_NOT_SUPPORTED(r)) { |
1210 | log_debug_errno(r, "ACLs not supported by file system at %s", path); | |
1211 | return 0; | |
1212 | } | |
1213 | ||
d873e877 HPD |
1214 | if (r > 0) |
1215 | return -r; /* already warned */ | |
f8606626 LP |
1216 | |
1217 | /* The above procfs paths don't work if /proc is not mounted. */ | |
1218 | if (r == -ENOENT && proc_mounted() == 0) | |
1219 | r = -ENOSYS; | |
1220 | ||
936f6bdb FB |
1221 | if (r < 0) |
1222 | return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path); | |
d873e877 HPD |
1223 | #endif |
1224 | return r; | |
f8eeeaf9 ZJS |
1225 | } |
1226 | ||
a9bc518c | 1227 | static int path_set_acls(Item *item, const char *path, CreationMode creation) { |
936f6bdb | 1228 | int r = 0; |
545cdb90 | 1229 | #if HAVE_ACL |
936f6bdb | 1230 | _cleanup_close_ int fd = -1; |
936f6bdb FB |
1231 | |
1232 | assert(item); | |
1233 | assert(path); | |
1234 | ||
addc3e30 | 1235 | fd = path_open_safe(path); |
936f6bdb | 1236 | if (fd < 0) |
addc3e30 | 1237 | return fd; |
936f6bdb | 1238 | |
a9bc518c | 1239 | r = fd_set_acls(item, fd, path, /* st= */ NULL, creation); |
545cdb90 YW |
1240 | #endif |
1241 | return r; | |
1242 | } | |
936f6bdb | 1243 | |
bd550f78 | 1244 | static int parse_attribute_from_arg(Item *item) { |
88ec4dfa LP |
1245 | |
1246 | static const struct { | |
1247 | char character; | |
1248 | unsigned value; | |
1249 | } attributes[] = { | |
1250 | { 'A', FS_NOATIME_FL }, /* do not update atime */ | |
1251 | { 'S', FS_SYNC_FL }, /* Synchronous updates */ | |
1252 | { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */ | |
1253 | { 'a', FS_APPEND_FL }, /* writes to file may only append */ | |
1254 | { 'c', FS_COMPR_FL }, /* Compress file */ | |
1255 | { 'd', FS_NODUMP_FL }, /* do not dump file */ | |
8c35b2ca | 1256 | { 'e', FS_EXTENT_FL }, /* Extents */ |
88ec4dfa LP |
1257 | { 'i', FS_IMMUTABLE_FL }, /* Immutable file */ |
1258 | { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */ | |
1259 | { 's', FS_SECRM_FL }, /* Secure deletion */ | |
1260 | { 'u', FS_UNRM_FL }, /* Undelete */ | |
1261 | { 't', FS_NOTAIL_FL }, /* file tail should not be merged */ | |
13e785f7 | 1262 | { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */ |
88ec4dfa | 1263 | { 'C', FS_NOCOW_FL }, /* Do not cow file */ |
75006470 | 1264 | { 'P', FS_PROJINHERIT_FL }, /* Inherit the quota project ID */ |
22c3a6ca | 1265 | }; |
88ec4dfa | 1266 | |
22c3a6ca GB |
1267 | enum { |
1268 | MODE_ADD, | |
1269 | MODE_DEL, | |
1270 | MODE_SET | |
1271 | } mode = MODE_ADD; | |
22c3a6ca | 1272 | |
88ec4dfa LP |
1273 | unsigned value = 0, mask = 0; |
1274 | const char *p; | |
22c3a6ca | 1275 | |
88ec4dfa LP |
1276 | assert(item); |
1277 | ||
1278 | p = item->argument; | |
1279 | if (p) { | |
1280 | if (*p == '+') { | |
1281 | mode = MODE_ADD; | |
1282 | p++; | |
1283 | } else if (*p == '-') { | |
1284 | mode = MODE_DEL; | |
1285 | p++; | |
1286 | } else if (*p == '=') { | |
1287 | mode = MODE_SET; | |
1288 | p++; | |
1289 | } | |
22c3a6ca GB |
1290 | } |
1291 | ||
baaa35ad ZJS |
1292 | if (isempty(p) && mode != MODE_SET) |
1293 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1294 | "Setting file attribute on '%s' needs an attribute specification.", | |
1295 | item->path); | |
88ec4dfa LP |
1296 | |
1297 | for (; p && *p ; p++) { | |
1298 | unsigned i, v; | |
1299 | ||
1300 | for (i = 0; i < ELEMENTSOF(attributes); i++) | |
1301 | if (*p == attributes[i].character) | |
1302 | break; | |
1303 | ||
baaa35ad ZJS |
1304 | if (i >= ELEMENTSOF(attributes)) |
1305 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1306 | "Unknown file attribute '%c' on '%s'.", | |
1307 | *p, item->path); | |
88ec4dfa LP |
1308 | |
1309 | v = attributes[i].value; | |
1310 | ||
3742095b | 1311 | SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET)); |
88ec4dfa LP |
1312 | |
1313 | mask |= v; | |
22c3a6ca GB |
1314 | } |
1315 | ||
1316 | if (mode == MODE_SET) | |
d629ba70 | 1317 | mask |= CHATTR_ALL_FL; |
22c3a6ca | 1318 | |
88ec4dfa | 1319 | assert(mask != 0); |
22c3a6ca | 1320 | |
88ec4dfa LP |
1321 | item->attribute_mask = mask; |
1322 | item->attribute_value = value; | |
1323 | item->attribute_set = true; | |
22c3a6ca GB |
1324 | |
1325 | return 0; | |
22c3a6ca GB |
1326 | } |
1327 | ||
a9bc518c LP |
1328 | static int fd_set_attribute( |
1329 | Item *item, | |
1330 | int fd, | |
1331 | const char *path, | |
1332 | const struct stat *st, | |
1333 | CreationMode creation) { | |
1334 | ||
936f6bdb | 1335 | _cleanup_close_ int procfs_fd = -1; |
b206ac8e | 1336 | struct stat stbuf; |
88ec4dfa LP |
1337 | unsigned f; |
1338 | int r; | |
22c3a6ca | 1339 | |
4dc7bfdf | 1340 | assert(item); |
b4b0f87c | 1341 | assert(fd >= 0); |
4dc7bfdf FB |
1342 | assert(path); |
1343 | ||
88ec4dfa | 1344 | if (!item->attribute_set || item->attribute_mask == 0) |
22c3a6ca | 1345 | return 0; |
22c3a6ca | 1346 | |
b206ac8e FB |
1347 | if (!st) { |
1348 | if (fstat(fd, &stbuf) < 0) | |
1349 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1350 | st = &stbuf; | |
1351 | } | |
1352 | ||
c5d554aa LP |
1353 | /* Issuing the file attribute ioctls on device nodes is not safe, as that will be delivered to the |
1354 | * drivers, not the file system containing the device node. */ | |
baaa35ad ZJS |
1355 | if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) |
1356 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1357 | "Setting file flags is only supported on regular files and directories, cannot set on '%s'.", | |
1358 | path); | |
88ec4dfa LP |
1359 | |
1360 | f = item->attribute_value & item->attribute_mask; | |
1361 | ||
1362 | /* Mask away directory-specific flags */ | |
936f6bdb | 1363 | if (!S_ISDIR(st->st_mode)) |
22c3a6ca | 1364 | f &= ~FS_DIRSYNC_FL; |
88ec4dfa | 1365 | |
f2324783 | 1366 | procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME); |
936f6bdb | 1367 | if (procfs_fd < 0) |
c3e03778 | 1368 | return log_error_errno(procfs_fd, "Failed to re-open '%s': %m", path); |
936f6bdb | 1369 | |
94566540 | 1370 | unsigned previous, current; |
c1631ee1 | 1371 | r = chattr_full(NULL, procfs_fd, f, item->attribute_mask, &previous, ¤t, CHATTR_FALLBACK_BITWISE); |
94566540 | 1372 | if (r == -ENOANO) |
d1a61dfa | 1373 | log_warning("Cannot set file attributes for '%s', maybe due to incompatibility in specified attributes, " |
94566540 YW |
1374 | "previous=0x%08x, current=0x%08x, expected=0x%08x, ignoring.", |
1375 | path, previous, current, (previous & ~item->attribute_mask) | (f & item->attribute_mask)); | |
1376 | else if (r < 0) | |
1377 | log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1378 | "Cannot set file attributes for '%s', value=0x%08x, mask=0x%08x, ignoring: %m", | |
ad75a97f | 1379 | path, item->attribute_value, item->attribute_mask); |
22c3a6ca GB |
1380 | |
1381 | return 0; | |
1382 | } | |
1383 | ||
a9bc518c | 1384 | static int path_set_attribute(Item *item, const char *path, CreationMode creation) { |
936f6bdb | 1385 | _cleanup_close_ int fd = -1; |
936f6bdb FB |
1386 | |
1387 | if (!item->attribute_set || item->attribute_mask == 0) | |
1388 | return 0; | |
1389 | ||
addc3e30 | 1390 | fd = path_open_safe(path); |
936f6bdb | 1391 | if (fd < 0) |
addc3e30 | 1392 | return fd; |
936f6bdb | 1393 | |
a9bc518c | 1394 | return fd_set_attribute(item, fd, path, /* st= */ NULL, creation); |
936f6bdb FB |
1395 | } |
1396 | ||
708daf42 LP |
1397 | static int write_argument_data(Item *i, int fd, const char *path) { |
1398 | int r; | |
1399 | ||
1400 | assert(i); | |
1401 | assert(fd >= 0); | |
1402 | assert(path); | |
1403 | ||
1404 | if (item_binary_argument_size(i) == 0) | |
1405 | return 0; | |
1406 | ||
1407 | assert(item_binary_argument(i)); | |
1408 | ||
1409 | log_debug("Writing to \"%s\".", path); | |
1410 | ||
1411 | r = loop_write(fd, item_binary_argument(i), item_binary_argument_size(i), /* do_poll= */ false); | |
1412 | if (r < 0) | |
1413 | return log_error_errno(r, "Failed to write file \"%s\": %m", path); | |
1414 | ||
1415 | return 0; | |
1416 | } | |
1417 | ||
a9bc518c | 1418 | static int write_one_file(Item *i, const char *path, CreationMode creation) { |
551470ec | 1419 | _cleanup_close_ int fd = -1, dir_fd = -1; |
4609d3bb | 1420 | _cleanup_free_ char *bn = NULL; |
1baaf8aa | 1421 | int r; |
31c84ff1 FB |
1422 | |
1423 | assert(i); | |
1424 | assert(path); | |
31c84ff1 FB |
1425 | assert(i->type == WRITE_FILE); |
1426 | ||
4609d3bb LP |
1427 | r = path_extract_filename(path, &bn); |
1428 | if (r < 0) | |
1429 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1430 | if (r == O_DIRECTORY) | |
1431 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for writing, is a directory.", path); | |
1432 | ||
1433 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it | |
1434 | * can't be changed behind our back. */ | |
551470ec FB |
1435 | dir_fd = path_open_parent_safe(path); |
1436 | if (dir_fd < 0) | |
1437 | return dir_fd; | |
1438 | ||
31c84ff1 | 1439 | /* Follows symlinks */ |
1baaf8aa LP |
1440 | fd = openat(dir_fd, bn, |
1441 | O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY|(i->append_or_force ? O_APPEND : 0), | |
1442 | i->mode); | |
31c84ff1 FB |
1443 | if (fd < 0) { |
1444 | if (errno == ENOENT) { | |
1445 | log_debug_errno(errno, "Not writing missing file \"%s\": %m", path); | |
1446 | return 0; | |
1447 | } | |
1baaf8aa LP |
1448 | |
1449 | if (i->allow_failure) | |
1450 | return log_debug_errno(errno, "Failed to open file \"%s\", ignoring: %m", path); | |
1451 | ||
31c84ff1 FB |
1452 | return log_error_errno(errno, "Failed to open file \"%s\": %m", path); |
1453 | } | |
1454 | ||
1455 | /* 'w' is allowed to write into any kind of files. */ | |
31c84ff1 | 1456 | |
708daf42 | 1457 | r = write_argument_data(i, fd, path); |
31c84ff1 | 1458 | if (r < 0) |
708daf42 | 1459 | return r; |
31c84ff1 | 1460 | |
a9bc518c | 1461 | return fd_set_perms(i, fd, path, NULL, creation); |
31c84ff1 FB |
1462 | } |
1463 | ||
1464 | static int create_file(Item *i, const char *path) { | |
5ec9d065 | 1465 | _cleanup_close_ int fd = -1, dir_fd = -1; |
4609d3bb | 1466 | _cleanup_free_ char *bn = NULL; |
5ec9d065 | 1467 | struct stat stbuf, *st = NULL; |
a9bc518c | 1468 | CreationMode creation; |
5ec9d065 | 1469 | int r = 0; |
d4e9eb91 | 1470 | |
874f1947 LP |
1471 | assert(i); |
1472 | assert(path); | |
5ec9d065 FB |
1473 | assert(i->type == CREATE_FILE); |
1474 | ||
1475 | /* 'f' operates on regular files exclusively. */ | |
1476 | ||
4609d3bb LP |
1477 | r = path_extract_filename(path, &bn); |
1478 | if (r < 0) | |
1479 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1480 | if (r == O_DIRECTORY) | |
1481 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for writing, is a directory.", path); | |
1482 | ||
1483 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it | |
1484 | * can't be changed behind our back. */ | |
5ec9d065 FB |
1485 | dir_fd = path_open_parent_safe(path); |
1486 | if (dir_fd < 0) | |
1487 | return dir_fd; | |
874f1947 | 1488 | |
43ad6e31 | 1489 | RUN_WITH_UMASK(0000) { |
ecabcf8b | 1490 | mac_selinux_create_file_prepare(path, S_IFREG); |
05c4affe | 1491 | fd = RET_NERRNO(openat(dir_fd, bn, O_CREAT|O_EXCL|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); |
ecabcf8b | 1492 | mac_selinux_create_file_clear(); |
5c0d398d | 1493 | } |
d4e9eb91 DR |
1494 | |
1495 | if (fd < 0) { | |
c5d554aa LP |
1496 | /* Even on a read-only filesystem, open(2) returns EEXIST if the file already exists. It |
1497 | * returns EROFS only if it needs to create the file. */ | |
05c4affe LP |
1498 | if (fd != -EEXIST) |
1499 | return log_error_errno(fd, "Failed to create file %s: %m", path); | |
5ec9d065 | 1500 | |
c5d554aa LP |
1501 | /* Re-open the file. At that point it must exist since open(2) failed with EEXIST. We still |
1502 | * need to check if the perms/mode need to be changed. For read-only filesystems, we let | |
1503 | * fd_set_perms() report the error if the perms need to be modified. */ | |
5ec9d065 FB |
1504 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); |
1505 | if (fd < 0) | |
1506 | return log_error_errno(errno, "Failed to re-open file %s: %m", path); | |
1507 | ||
1508 | if (fstat(fd, &stbuf) < 0) | |
1509 | return log_error_errno(errno, "stat(%s) failed: %m", path); | |
1510 | ||
d7a0f1f4 FS |
1511 | if (!S_ISREG(stbuf.st_mode)) |
1512 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1513 | "%s exists and is not a regular file.", | |
1514 | path); | |
d4e9eb91 | 1515 | |
5ec9d065 | 1516 | st = &stbuf; |
a9bc518c LP |
1517 | creation = CREATION_EXISTING; |
1518 | } else { | |
8f6fb95c LP |
1519 | r = write_argument_data(i, fd, path); |
1520 | if (r < 0) | |
1521 | return r; | |
a9bc518c LP |
1522 | |
1523 | creation = CREATION_NORMAL; | |
d4e9eb91 DR |
1524 | } |
1525 | ||
a9bc518c | 1526 | return fd_set_perms(i, fd, path, st, creation); |
5ec9d065 | 1527 | } |
d4e9eb91 | 1528 | |
5ec9d065 | 1529 | static int truncate_file(Item *i, const char *path) { |
14ab804e | 1530 | _cleanup_close_ int fd = -1, dir_fd = -1; |
4609d3bb | 1531 | _cleanup_free_ char *bn = NULL; |
5ec9d065 | 1532 | struct stat stbuf, *st = NULL; |
a9bc518c | 1533 | CreationMode creation; |
5ec9d065 FB |
1534 | bool erofs = false; |
1535 | int r = 0; | |
1536 | ||
1537 | assert(i); | |
1538 | assert(path); | |
eccebf4b | 1539 | assert(i->type == TRUNCATE_FILE || (i->type == CREATE_FILE && i->append_or_force)); |
d4e9eb91 | 1540 | |
4609d3bb LP |
1541 | /* We want to operate on regular file exclusively especially since O_TRUNC is unspecified if the file |
1542 | * is neither a regular file nor a fifo nor a terminal device. Therefore we first open the file and | |
1543 | * make sure it's a regular one before truncating it. */ | |
1544 | ||
1545 | r = path_extract_filename(path, &bn); | |
1546 | if (r < 0) | |
1547 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1548 | if (r == O_DIRECTORY) | |
1549 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for truncation, is a directory.", path); | |
3612fbc1 | 1550 | |
4609d3bb LP |
1551 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it |
1552 | * can't be changed behind our back. */ | |
14ab804e FB |
1553 | dir_fd = path_open_parent_safe(path); |
1554 | if (dir_fd < 0) | |
1555 | return dir_fd; | |
1556 | ||
a9bc518c LP |
1557 | creation = CREATION_EXISTING; |
1558 | fd = RET_NERRNO(openat(dir_fd, bn, O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); | |
1559 | if (fd == -ENOENT) { | |
1560 | creation = CREATION_NORMAL; /* Didn't work without O_CREATE, try again with */ | |
1561 | ||
1562 | RUN_WITH_UMASK(0000) { | |
1563 | mac_selinux_create_file_prepare(path, S_IFREG); | |
1564 | fd = RET_NERRNO(openat(dir_fd, bn, O_CREAT|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); | |
1565 | mac_selinux_create_file_clear(); | |
1566 | } | |
5ec9d065 FB |
1567 | } |
1568 | ||
1569 | if (fd < 0) { | |
05c4affe LP |
1570 | if (fd != -EROFS) |
1571 | return log_error_errno(fd, "Failed to open/create file %s: %m", path); | |
5ec9d065 | 1572 | |
c5d554aa LP |
1573 | /* On a read-only filesystem, we don't want to fail if the target is already empty and the |
1574 | * perms are set. So we still proceed with the sanity checks and let the remaining operations | |
1575 | * fail with EROFS if they try to modify the target file. */ | |
5ec9d065 | 1576 | |
14ab804e | 1577 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); |
5ec9d065 | 1578 | if (fd < 0) { |
d7a0f1f4 FS |
1579 | if (errno == ENOENT) |
1580 | return log_error_errno(SYNTHETIC_ERRNO(EROFS), | |
1581 | "Cannot create file %s on a read-only file system.", | |
1582 | path); | |
5ec9d065 FB |
1583 | |
1584 | return log_error_errno(errno, "Failed to re-open file %s: %m", path); | |
1585 | } | |
1586 | ||
1587 | erofs = true; | |
a9bc518c | 1588 | creation = CREATION_EXISTING; |
5ec9d065 FB |
1589 | } |
1590 | ||
1591 | if (fstat(fd, &stbuf) < 0) | |
4a62c710 | 1592 | return log_error_errno(errno, "stat(%s) failed: %m", path); |
d4e9eb91 | 1593 | |
d7a0f1f4 FS |
1594 | if (!S_ISREG(stbuf.st_mode)) |
1595 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1596 | "%s exists and is not a regular file.", | |
1597 | path); | |
d4e9eb91 | 1598 | |
5ec9d065 FB |
1599 | if (stbuf.st_size > 0) { |
1600 | if (ftruncate(fd, 0) < 0) { | |
1601 | r = erofs ? -EROFS : -errno; | |
1602 | return log_error_errno(r, "Failed to truncate file %s: %m", path); | |
1603 | } | |
1604 | } else | |
1605 | st = &stbuf; | |
d4e9eb91 | 1606 | |
5ec9d065 FB |
1607 | log_debug("\"%s\" has been created.", path); |
1608 | ||
708daf42 LP |
1609 | if (item_binary_argument(i)) { |
1610 | r = write_argument_data(i, fd, path); | |
2f3745dd | 1611 | if (r < 0) |
708daf42 | 1612 | return r; |
5ec9d065 FB |
1613 | } |
1614 | ||
a9bc518c | 1615 | return fd_set_perms(i, fd, path, st, creation); |
d4e9eb91 DR |
1616 | } |
1617 | ||
b1f7b17f | 1618 | static int copy_files(Item *i) { |
16ba55ad | 1619 | _cleanup_close_ int dfd = -1, fd = -1; |
4609d3bb | 1620 | _cleanup_free_ char *bn = NULL; |
8f6fb95c | 1621 | struct stat st, a; |
b1f7b17f FB |
1622 | int r; |
1623 | ||
1624 | log_debug("Copying tree \"%s\" to \"%s\".", i->argument, i->path); | |
1625 | ||
4609d3bb LP |
1626 | r = path_extract_filename(i->path, &bn); |
1627 | if (r < 0) | |
1628 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
b1f7b17f | 1629 | |
4609d3bb LP |
1630 | /* Validate the path and use the returned directory fd for copying the target so we're sure that the |
1631 | * path can't be changed behind our back. */ | |
16ba55ad FB |
1632 | dfd = path_open_parent_safe(i->path); |
1633 | if (dfd < 0) | |
1634 | return dfd; | |
b1f7b17f | 1635 | |
16ba55ad FB |
1636 | r = copy_tree_at(AT_FDCWD, i->argument, |
1637 | dfd, bn, | |
1638 | i->uid_set ? i->uid : UID_INVALID, | |
1639 | i->gid_set ? i->gid : GID_INVALID, | |
a424958a DDM |
1640 | COPY_REFLINK | COPY_MERGE_EMPTY | COPY_MAC_CREATE | COPY_HARDLINKS, |
1641 | NULL); | |
b1f7b17f | 1642 | |
8f6fb95c LP |
1643 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
1644 | if (fd < 0) { | |
1645 | if (r < 0) /* Look at original error first */ | |
b1f7b17f FB |
1646 | return log_error_errno(r, "Failed to copy files to %s: %m", i->path); |
1647 | ||
8f6fb95c LP |
1648 | return log_error_errno(errno, "Failed to openat(%s): %m", i->path); |
1649 | } | |
1650 | ||
1651 | if (fstat(fd, &st) < 0) | |
1652 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
b1f7b17f | 1653 | |
8f6fb95c LP |
1654 | if (stat(i->argument, &a) < 0) |
1655 | return log_error_errno(errno, "Failed to stat(%s): %m", i->argument); | |
b1f7b17f | 1656 | |
8f6fb95c LP |
1657 | if (((st.st_mode ^ a.st_mode) & S_IFMT) != 0) { |
1658 | log_debug("Can't copy to %s, file exists already and is of different type", i->path); | |
1659 | return 0; | |
b1f7b17f FB |
1660 | } |
1661 | ||
8f6fb95c | 1662 | return fd_set_perms(i, fd, i->path, &st, _CREATION_MODE_INVALID); |
b1f7b17f FB |
1663 | } |
1664 | ||
a9bc518c LP |
1665 | static int create_directory_or_subvolume( |
1666 | const char *path, | |
1667 | mode_t mode, | |
1668 | bool subvol, | |
8f6fb95c | 1669 | struct stat *ret_st, |
a9bc518c | 1670 | CreationMode *ret_creation) { |
074bd73f | 1671 | |
4609d3bb | 1672 | _cleanup_free_ char *bn = NULL; |
4c39d899 | 1673 | _cleanup_close_ int pfd = -1; |
8f6fb95c LP |
1674 | CreationMode creation; |
1675 | struct stat st; | |
a9bc518c | 1676 | int r, fd; |
4ad36844 | 1677 | |
4c39d899 | 1678 | assert(path); |
4ad36844 | 1679 | |
4609d3bb LP |
1680 | r = path_extract_filename(path, &bn); |
1681 | if (r < 0) | |
1682 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1683 | ||
4c39d899 FB |
1684 | pfd = path_open_parent_safe(path); |
1685 | if (pfd < 0) | |
1686 | return pfd; | |
4ad36844 | 1687 | |
4c39d899 | 1688 | if (subvol) { |
4368c60c AV |
1689 | r = getenv_bool("SYSTEMD_TMPFILES_FORCE_SUBVOL"); |
1690 | if (r < 0) { | |
1691 | if (r != -ENXIO) /* env var is unset */ | |
1692 | log_warning_errno(r, "Cannot parse value of $SYSTEMD_TMPFILES_FORCE_SUBVOL, ignoring."); | |
1693 | r = btrfs_is_subvol(empty_to_root(arg_root)) > 0; | |
1694 | } | |
8f6fb95c | 1695 | if (r == 0) |
92631578 LP |
1696 | /* Don't create a subvolume unless the root directory is one, too. We do this under |
1697 | * the assumption that if the root directory is just a plain directory (i.e. very | |
1698 | * light-weight), we shouldn't try to split it up into subvolumes (i.e. more | |
1699 | * heavy-weight). Thus, chroot() environments and suchlike will get a full brtfs | |
1700 | * subvolume set up below their tree only if they specifically set up a btrfs | |
1701 | * subvolume for the root dir too. */ | |
4ad36844 | 1702 | |
4c39d899 | 1703 | subvol = false; |
4ad36844 | 1704 | else { |
4c39d899 | 1705 | RUN_WITH_UMASK((~mode) & 0777) |
4609d3bb | 1706 | r = btrfs_subvol_make_fd(pfd, bn); |
4ad36844 FB |
1707 | } |
1708 | } else | |
1709 | r = 0; | |
1710 | ||
8f6fb95c | 1711 | if (!subvol || ERRNO_IS_NOT_SUPPORTED(r)) |
4ad36844 | 1712 | RUN_WITH_UMASK(0000) |
4609d3bb | 1713 | r = mkdirat_label(pfd, bn, mode); |
4ad36844 | 1714 | |
8f6fb95c | 1715 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
4ad36844 | 1716 | |
8f6fb95c LP |
1717 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_DIRECTORY|O_PATH); |
1718 | if (fd < 0) { | |
1719 | /* We couldn't open it because it is not actually a directory? */ | |
1720 | if (errno == ENOTDIR) | |
1721 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "\"%s\" already exists and is not a directory.", path); | |
1722 | ||
1723 | /* Then look at the original error */ | |
1724 | if (r < 0) | |
4c39d899 | 1725 | return log_error_errno(r, "Failed to create directory or subvolume \"%s\": %m", path); |
4ad36844 | 1726 | |
8f6fb95c LP |
1727 | return log_error_errno(errno, "Failed to open directory/subvolume we just created '%s': %m", path); |
1728 | } | |
4ad36844 | 1729 | |
8f6fb95c LP |
1730 | if (fstat(fd, &st) < 0) |
1731 | return log_error_errno(errno, "Failed to fstat(%s): %m", path); | |
4ad36844 | 1732 | |
8f6fb95c | 1733 | assert(S_ISDIR(st.st_mode)); /* we used O_DIRECTORY above */ |
4c39d899 | 1734 | |
8f6fb95c | 1735 | log_debug("%s directory \"%s\".", creation_mode_verb_to_string(creation), path); |
09f12798 | 1736 | |
8f6fb95c LP |
1737 | if (ret_st) |
1738 | *ret_st = st; | |
a9bc518c | 1739 | if (ret_creation) |
8f6fb95c | 1740 | *ret_creation = creation; |
a9bc518c LP |
1741 | |
1742 | return fd; | |
4c39d899 FB |
1743 | } |
1744 | ||
1745 | static int create_directory(Item *i, const char *path) { | |
1746 | _cleanup_close_ int fd = -1; | |
a9bc518c | 1747 | CreationMode creation; |
8f6fb95c | 1748 | struct stat st; |
4c39d899 FB |
1749 | |
1750 | assert(i); | |
1751 | assert(IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY)); | |
1752 | ||
8f6fb95c | 1753 | fd = create_directory_or_subvolume(path, i->mode, /* subvol= */ false, &st, &creation); |
4c39d899 FB |
1754 | if (fd == -EEXIST) |
1755 | return 0; | |
1756 | if (fd < 0) | |
1757 | return fd; | |
1758 | ||
8f6fb95c | 1759 | return fd_set_perms(i, fd, path, &st, creation); |
4c39d899 FB |
1760 | } |
1761 | ||
1762 | static int create_subvolume(Item *i, const char *path) { | |
1763 | _cleanup_close_ int fd = -1; | |
f17a8d61 | 1764 | CreationMode creation; |
8f6fb95c | 1765 | struct stat st; |
4c39d899 FB |
1766 | int r, q = 0; |
1767 | ||
1768 | assert(i); | |
1769 | assert(IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)); | |
1770 | ||
8f6fb95c | 1771 | fd = create_directory_or_subvolume(path, i->mode, /* subvol = */ true, &st, &creation); |
4c39d899 FB |
1772 | if (fd == -EEXIST) |
1773 | return 0; | |
1774 | if (fd < 0) | |
1775 | return fd; | |
4ad36844 | 1776 | |
f17a8d61 FB |
1777 | if (creation == CREATION_NORMAL && |
1778 | IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) { | |
4c39d899 | 1779 | r = btrfs_subvol_auto_qgroup_fd(fd, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA); |
4ad36844 FB |
1780 | if (r == -ENOTTY) |
1781 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (unsupported fs or dir not a subvolume): %m", i->path); | |
1782 | else if (r == -EROFS) | |
1783 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (fs is read-only).", i->path); | |
4b019d2f | 1784 | else if (r == -ENOTCONN) |
4ad36844 FB |
1785 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (quota support is disabled).", i->path); |
1786 | else if (r < 0) | |
1787 | q = log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path); | |
1788 | else if (r > 0) | |
1789 | log_debug("Adjusted quota for subvolume \"%s\".", i->path); | |
1790 | else if (r == 0) | |
1791 | log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path); | |
1792 | } | |
1793 | ||
8f6fb95c | 1794 | r = fd_set_perms(i, fd, path, &st, creation); |
9d874aec | 1795 | if (q < 0) /* prefer the quota change error from above */ |
4ad36844 FB |
1796 | return q; |
1797 | ||
1798 | return r; | |
1799 | } | |
1800 | ||
a9bc518c | 1801 | static int empty_directory(Item *i, const char *path, CreationMode creation) { |
27f6aa0b LP |
1802 | _cleanup_close_ int fd = -1; |
1803 | struct stat st; | |
54946021 FB |
1804 | int r; |
1805 | ||
1806 | assert(i); | |
1807 | assert(i->type == EMPTY_DIRECTORY); | |
1808 | ||
27f6aa0b LP |
1809 | r = chase_symlinks(path, arg_root, CHASE_SAFE|CHASE_WARN, NULL, &fd); |
1810 | if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */ | |
e56074a2 | 1811 | return r; |
54946021 | 1812 | if (r == -ENOENT) { |
27f6aa0b LP |
1813 | /* Option "e" operates only on existing objects. Do not print errors about non-existent files |
1814 | * or directories */ | |
1815 | log_debug_errno(r, "Skipping missing directory: %s", path); | |
54946021 FB |
1816 | return 0; |
1817 | } | |
1818 | if (r < 0) | |
27f6aa0b LP |
1819 | return log_error_errno(r, "Failed to open directory '%s': %m", path); |
1820 | ||
1821 | if (fstat(fd, &st) < 0) | |
1822 | return log_error_errno(errno, "Failed to fstat(%s): %m", path); | |
1823 | if (!S_ISDIR(st.st_mode)) { | |
1824 | log_warning("'%s' already exists and is not a directory.", path); | |
b88ba6c7 ZJS |
1825 | return 0; |
1826 | } | |
54946021 | 1827 | |
27f6aa0b | 1828 | return fd_set_perms(i, fd, path, &st, creation); |
54946021 FB |
1829 | } |
1830 | ||
074bd73f | 1831 | static int create_device(Item *i, mode_t file_type) { |
c7700a77 | 1832 | _cleanup_close_ int dfd = -1, fd = -1; |
4609d3bb | 1833 | _cleanup_free_ char *bn = NULL; |
074bd73f | 1834 | CreationMode creation; |
8f6fb95c | 1835 | struct stat st; |
074bd73f FB |
1836 | int r; |
1837 | ||
1838 | assert(i); | |
a8ec0abe | 1839 | assert(IN_SET(i->type, CREATE_BLOCK_DEVICE, CREATE_CHAR_DEVICE)); |
074bd73f FB |
1840 | assert(IN_SET(file_type, S_IFBLK, S_IFCHR)); |
1841 | ||
4609d3bb LP |
1842 | r = path_extract_filename(i->path, &bn); |
1843 | if (r < 0) | |
1844 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
1845 | if (r == O_DIRECTORY) | |
1846 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for creating device node, is a directory.", i->path); | |
c7700a77 | 1847 | |
4609d3bb LP |
1848 | /* Validate the path and use the returned directory fd for copying the target so we're sure that the |
1849 | * path can't be changed behind our back. */ | |
c7700a77 FB |
1850 | dfd = path_open_parent_safe(i->path); |
1851 | if (dfd < 0) | |
1852 | return dfd; | |
1853 | ||
074bd73f FB |
1854 | RUN_WITH_UMASK(0000) { |
1855 | mac_selinux_create_file_prepare(i->path, file_type); | |
05c4affe | 1856 | r = RET_NERRNO(mknodat(dfd, bn, i->mode | file_type, i->major_minor)); |
074bd73f FB |
1857 | mac_selinux_create_file_clear(); |
1858 | } | |
8f6fb95c | 1859 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
074bd73f | 1860 | |
8f6fb95c LP |
1861 | /* Try to open the inode via O_PATH, regardless if we could create it or not. Maybe everything is in |
1862 | * order anyway and we hence can ignore the error to create the device node */ | |
1863 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
1864 | if (fd < 0) { | |
1865 | /* OK, so opening the inode failed, let's look at the original error then. */ | |
c7700a77 | 1866 | |
8f6fb95c LP |
1867 | if (r < 0) { |
1868 | if (ERRNO_IS_PRIVILEGE(r)) | |
1869 | goto handle_privilege; | |
074bd73f | 1870 | |
8f6fb95c LP |
1871 | return log_error_errno(r, "Failed to create device node '%s': %m", i->path); |
1872 | } | |
074bd73f | 1873 | |
8f6fb95c LP |
1874 | return log_error_errno(errno, "Failed to open device node '%s' we just created: %m", i->path); |
1875 | } | |
074bd73f | 1876 | |
8f6fb95c LP |
1877 | if (fstat(fd, &st) < 0) |
1878 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
074bd73f | 1879 | |
8f6fb95c | 1880 | if (((st.st_mode ^ file_type) & S_IFMT) != 0) { |
074bd73f | 1881 | |
8f6fb95c LP |
1882 | if (i->append_or_force) { |
1883 | fd = safe_close(fd); | |
074bd73f | 1884 | |
8f6fb95c LP |
1885 | RUN_WITH_UMASK(0000) { |
1886 | mac_selinux_create_file_prepare(i->path, file_type); | |
1887 | r = mknodat_atomic(dfd, bn, i->mode | file_type, i->major_minor); | |
1888 | mac_selinux_create_file_clear(); | |
1889 | } | |
1890 | if (ERRNO_IS_PRIVILEGE(r)) | |
1891 | goto handle_privilege; | |
1892 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
1893 | r = rm_rf_child(dfd, bn, REMOVE_PHYSICAL); | |
074bd73f | 1894 | if (r < 0) |
8f6fb95c LP |
1895 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); |
1896 | ||
1897 | mac_selinux_create_file_prepare(i->path, file_type); | |
1898 | r = RET_NERRNO(mknodat(dfd, bn, i->mode | file_type, i->major_minor)); | |
1899 | mac_selinux_create_file_clear(); | |
074bd73f | 1900 | } |
8f6fb95c LP |
1901 | if (r < 0) |
1902 | return log_error_errno(r, "Failed to create device node '%s': %m", i->path); | |
1903 | ||
1904 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
1905 | if (fd < 0) | |
1906 | return log_error_errno(errno, "Failed to open device node we just created '%s': %m", i->path); | |
1907 | ||
1908 | /* Validate type before change ownership below */ | |
1909 | if (fstat(fd, &st) < 0) | |
1910 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
1911 | ||
1912 | if (((st.st_mode ^ file_type) & S_IFMT) != 0) | |
1913 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Device node we just created is not a device node, refusing."); | |
1914 | ||
1915 | creation = CREATION_FORCE; | |
1916 | } else { | |
1917 | log_warning("\"%s\" already exists and is not a device node.", i->path); | |
1918 | return 0; | |
1919 | } | |
1920 | } | |
074bd73f FB |
1921 | |
1922 | log_debug("%s %s device node \"%s\" %u:%u.", | |
1923 | creation_mode_verb_to_string(creation), | |
1924 | i->type == CREATE_BLOCK_DEVICE ? "block" : "char", | |
1925 | i->path, major(i->mode), minor(i->mode)); | |
1926 | ||
8f6fb95c | 1927 | return fd_set_perms(i, fd, i->path, &st, creation); |
c7700a77 | 1928 | |
8f6fb95c LP |
1929 | handle_privilege: |
1930 | log_debug_errno(r, | |
1931 | "We lack permissions, possibly because of cgroup configuration; " | |
1932 | "skipping creation of device node '%s'.", i->path); | |
1933 | return 0; | |
074bd73f FB |
1934 | } |
1935 | ||
8f6fb95c | 1936 | static int create_fifo(Item *i) { |
7ea5a87f | 1937 | _cleanup_close_ int pfd = -1, fd = -1; |
4609d3bb | 1938 | _cleanup_free_ char *bn = NULL; |
a2fc2f8d FB |
1939 | CreationMode creation; |
1940 | struct stat st; | |
1941 | int r; | |
1942 | ||
8f6fb95c LP |
1943 | assert(i); |
1944 | assert(i->type == CREATE_FIFO); | |
1945 | ||
4609d3bb LP |
1946 | r = path_extract_filename(i->path, &bn); |
1947 | if (r < 0) | |
8f6fb95c | 1948 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); |
4609d3bb | 1949 | if (r == O_DIRECTORY) |
8f6fb95c | 1950 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for creating FIFO, is a directory.", i->path); |
4609d3bb | 1951 | |
8f6fb95c | 1952 | pfd = path_open_parent_safe(i->path); |
7ea5a87f FB |
1953 | if (pfd < 0) |
1954 | return pfd; | |
1955 | ||
a2fc2f8d | 1956 | RUN_WITH_UMASK(0000) { |
8f6fb95c | 1957 | mac_selinux_create_file_prepare(i->path, S_IFIFO); |
05c4affe | 1958 | r = RET_NERRNO(mkfifoat(pfd, bn, i->mode)); |
a2fc2f8d FB |
1959 | mac_selinux_create_file_clear(); |
1960 | } | |
1961 | ||
8f6fb95c | 1962 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
a2fc2f8d | 1963 | |
8f6fb95c LP |
1964 | /* Open the inode via O_PATH, regardless if we managed to create it or not. Maybe it is is already the FIFO we want */ |
1965 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
1966 | if (fd < 0) { | |
1967 | if (r < 0) | |
1968 | return log_error_errno(r, "Failed to create FIFO %s: %m", i->path); /* original error! */ | |
a2fc2f8d | 1969 | |
8f6fb95c LP |
1970 | return log_error_errno(errno, "Failed to open FIFO we just created %s: %m", i->path); |
1971 | } | |
a2fc2f8d | 1972 | |
8f6fb95c LP |
1973 | if (fstat(fd, &st) < 0) |
1974 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
1975 | ||
1976 | if (!S_ISFIFO(st.st_mode)) { | |
a2fc2f8d | 1977 | |
8f6fb95c LP |
1978 | if (i->append_or_force) { |
1979 | fd = safe_close(fd); | |
1980 | ||
1981 | RUN_WITH_UMASK(0000) { | |
1982 | mac_selinux_create_file_prepare(i->path, S_IFIFO); | |
1983 | r = mkfifoat_atomic(pfd, bn, i->mode); | |
1984 | mac_selinux_create_file_clear(); | |
1985 | } | |
1986 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
1987 | r = rm_rf_child(pfd, bn, REMOVE_PHYSICAL); | |
a2fc2f8d | 1988 | if (r < 0) |
8f6fb95c LP |
1989 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); |
1990 | ||
1991 | mac_selinux_create_file_prepare(i->path, S_IFIFO); | |
1992 | r = RET_NERRNO(mkfifoat(pfd, bn, i->mode)); | |
1993 | mac_selinux_create_file_clear(); | |
a2fc2f8d | 1994 | } |
8f6fb95c LP |
1995 | if (r < 0) |
1996 | return log_error_errno(r, "Failed to create FIFO %s: %m", i->path); | |
7ea5a87f | 1997 | |
8f6fb95c LP |
1998 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
1999 | if (fd < 0) | |
2000 | return log_error_errno(errno, "Failed to open FIFO we just created '%s': %m", i->path); | |
a2fc2f8d | 2001 | |
8f6fb95c LP |
2002 | /* Validate type before change ownership below */ |
2003 | if (fstat(fd, &st) < 0) | |
2004 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2005 | ||
2006 | if (!S_ISFIFO(st.st_mode)) | |
2007 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), "FIFO inode we just created is not a FIFO, refusing."); | |
2008 | ||
2009 | creation = CREATION_FORCE; | |
2010 | } else { | |
2011 | log_warning("\"%s\" already exists and is not a FIFO.", i->path); | |
2012 | return 0; | |
2013 | } | |
2014 | } | |
2015 | ||
2016 | log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), i->path); | |
7ea5a87f | 2017 | |
8f6fb95c | 2018 | return fd_set_perms(i, fd, i->path, &st, creation); |
a2fc2f8d FB |
2019 | } |
2020 | ||
9e430ce3 LP |
2021 | static int create_symlink(Item *i) { |
2022 | _cleanup_close_ int pfd = -1, fd = -1; | |
2023 | _cleanup_free_ char *bn = NULL; | |
2024 | CreationMode creation; | |
2025 | struct stat st; | |
2026 | bool good = false; | |
2027 | int r; | |
2028 | ||
2029 | assert(i); | |
2030 | ||
2031 | r = path_extract_filename(i->path, &bn); | |
2032 | if (r < 0) | |
2033 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
2034 | if (r == O_DIRECTORY) | |
2035 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for creating FIFO, is a directory.", i->path); | |
2036 | ||
2037 | pfd = path_open_parent_safe(i->path); | |
2038 | if (pfd < 0) | |
2039 | return pfd; | |
2040 | ||
2041 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2042 | r = RET_NERRNO(symlinkat(i->argument, pfd, bn)); | |
2043 | mac_selinux_create_file_clear(); | |
2044 | ||
2045 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; | |
2046 | ||
2047 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2048 | if (fd < 0) { | |
2049 | if (r < 0) | |
2050 | return log_error_errno(r, "Failed to create symlink '%s': %m", i->path); /* original error! */ | |
2051 | ||
2052 | return log_error_errno(errno, "Failed to open symlink we just created '%s': %m", i->path); | |
2053 | } | |
2054 | ||
2055 | if (fstat(fd, &st) < 0) | |
2056 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2057 | ||
2058 | if (S_ISLNK(st.st_mode)) { | |
2059 | _cleanup_free_ char *x = NULL; | |
2060 | ||
2061 | r = readlinkat_malloc(fd, "", &x); | |
2062 | if (r < 0) | |
2063 | return log_error_errno(r, "readlinkat(%s) failed: %m", i->path); | |
2064 | ||
2065 | good = streq(x, i->argument); | |
2066 | } else | |
2067 | good = false; | |
2068 | ||
2069 | if (!good) { | |
2070 | if (!i->append_or_force) { | |
2071 | log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path); | |
2072 | return 0; | |
2073 | } | |
2074 | ||
2075 | fd = safe_close(fd); | |
2076 | ||
2077 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2078 | r = symlinkat_atomic_full(i->argument, pfd, bn, /* make_relative= */ false); | |
2079 | mac_selinux_create_file_clear(); | |
2080 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
2081 | r = rm_rf_child(pfd, bn, REMOVE_PHYSICAL); | |
2082 | if (r < 0) | |
2083 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); | |
2084 | ||
2085 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2086 | r = RET_NERRNO(symlinkat(i->argument, pfd, i->path)); | |
2087 | mac_selinux_create_file_clear(); | |
2088 | } | |
2089 | if (r < 0) | |
2090 | return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path); | |
2091 | ||
2092 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2093 | if (fd < 0) | |
2094 | return log_error_errno(errno, "Failed to open symlink we just created '%s': %m", i->path); | |
2095 | ||
2096 | /* Validate type before change ownership below */ | |
2097 | if (fstat(fd, &st) < 0) | |
2098 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2099 | ||
2100 | if (!S_ISLNK(st.st_mode)) | |
2101 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Symlink we just created is not a symlink, refusing."); | |
2102 | ||
2103 | creation = CREATION_FORCE; | |
2104 | } | |
2105 | ||
2106 | log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path); | |
2107 | return fd_set_perms(i, fd, i->path, &st, creation); | |
2108 | } | |
2109 | ||
a9bc518c LP |
2110 | typedef int (*action_t)(Item *i, const char *path, CreationMode creation); |
2111 | typedef int (*fdaction_t)(Item *i, int fd, const char *path, const struct stat *st, CreationMode creation); | |
2112 | ||
2113 | static int item_do( | |
2114 | Item *i, | |
2115 | int fd, | |
2116 | const char *path, | |
2117 | CreationMode creation, | |
2118 | fdaction_t action) { | |
081043cf | 2119 | |
14f3480a | 2120 | struct stat st; |
936f6bdb | 2121 | int r = 0, q; |
e73a03e0 LP |
2122 | |
2123 | assert(i); | |
4dc7bfdf | 2124 | assert(path); |
936f6bdb | 2125 | assert(fd >= 0); |
14f3480a FB |
2126 | |
2127 | if (fstat(fd, &st) < 0) { | |
09f12798 | 2128 | r = log_error_errno(errno, "fstat() on file failed: %m"); |
14f3480a FB |
2129 | goto finish; |
2130 | } | |
a8d88783 | 2131 | |
a9bc518c LP |
2132 | /* This returns the first error we run into, but nevertheless tries to go on */ |
2133 | r = action(i, fd, path, &st, creation); | |
a8d88783 | 2134 | |
14f3480a | 2135 | if (S_ISDIR(st.st_mode)) { |
936f6bdb | 2136 | _cleanup_closedir_ DIR *d = NULL; |
a8d88783 | 2137 | |
ddb6eeaf LP |
2138 | /* The passed 'fd' was opened with O_PATH. We need to convert it into a 'regular' fd before |
2139 | * reading the directory content. */ | |
2140 | d = opendir(FORMAT_PROC_FD_PATH(fd)); | |
936f6bdb | 2141 | if (!d) { |
ddb6eeaf | 2142 | log_error_errno(errno, "Failed to opendir() '%s': %m", FORMAT_PROC_FD_PATH(fd)); |
09f12798 LP |
2143 | if (r == 0) |
2144 | r = -errno; | |
936f6bdb FB |
2145 | goto finish; |
2146 | } | |
a8d88783 | 2147 | |
936f6bdb | 2148 | FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) { |
936f6bdb | 2149 | int de_fd; |
a8d88783 | 2150 | |
936f6bdb FB |
2151 | if (dot_or_dot_dot(de->d_name)) |
2152 | continue; | |
2153 | ||
2154 | de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
9d874aec LP |
2155 | if (de_fd < 0) |
2156 | q = log_error_errno(errno, "Failed to open() file '%s': %m", de->d_name); | |
4dc7bfdf FB |
2157 | else { |
2158 | _cleanup_free_ char *de_path = NULL; | |
2159 | ||
62a85ee0 | 2160 | de_path = path_join(path, de->d_name); |
4dc7bfdf FB |
2161 | if (!de_path) |
2162 | q = log_oom(); | |
2163 | else | |
2164 | /* Pass ownership of dirent fd over */ | |
a9bc518c | 2165 | q = item_do(i, de_fd, de_path, CREATION_EXISTING, action); |
4dc7bfdf | 2166 | } |
a8d88783 | 2167 | |
e73a03e0 LP |
2168 | if (q < 0 && r == 0) |
2169 | r = q; | |
a8d88783 | 2170 | } |
a8d88783 | 2171 | } |
936f6bdb FB |
2172 | finish: |
2173 | safe_close(fd); | |
e73a03e0 | 2174 | return r; |
a8d88783 MS |
2175 | } |
2176 | ||
936f6bdb | 2177 | static int glob_item(Item *i, action_t action) { |
df99a9ef | 2178 | _cleanup_globfree_ glob_t g = { |
ebf31a1f | 2179 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
df99a9ef | 2180 | }; |
e73a03e0 | 2181 | int r = 0, k; |
99e68c0b | 2182 | |
84e72b5e ZJS |
2183 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); |
2184 | if (k < 0 && k != -ENOENT) | |
2185 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
99e68c0b | 2186 | |
c84a9488 | 2187 | STRV_FOREACH(fn, g.gl_pathv) { |
a9bc518c LP |
2188 | /* We pass CREATION_EXISTING here, since if we are globbing for it, it always has to exist */ |
2189 | k = action(i, *fn, CREATION_EXISTING); | |
e73a03e0 | 2190 | if (k < 0 && r == 0) |
99e68c0b | 2191 | r = k; |
936f6bdb FB |
2192 | } |
2193 | ||
2194 | return r; | |
2195 | } | |
081043cf | 2196 | |
936f6bdb FB |
2197 | static int glob_item_recursively(Item *i, fdaction_t action) { |
2198 | _cleanup_globfree_ glob_t g = { | |
2199 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, | |
2200 | }; | |
2201 | int r = 0, k; | |
936f6bdb FB |
2202 | |
2203 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); | |
2204 | if (k < 0 && k != -ENOENT) | |
2205 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
2206 | ||
2207 | STRV_FOREACH(fn, g.gl_pathv) { | |
2208 | _cleanup_close_ int fd = -1; | |
936f6bdb FB |
2209 | |
2210 | /* Make sure we won't trigger/follow file object (such as | |
2211 | * device nodes, automounts, ...) pointed out by 'fn' with | |
2212 | * O_PATH. Note, when O_PATH is used, flags other than | |
2213 | * O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW are ignored. */ | |
2214 | ||
2215 | fd = open(*fn, O_CLOEXEC|O_NOFOLLOW|O_PATH); | |
2216 | if (fd < 0) { | |
09f12798 LP |
2217 | log_error_errno(errno, "Opening '%s' failed: %m", *fn); |
2218 | if (r == 0) | |
2219 | r = -errno; | |
936f6bdb FB |
2220 | continue; |
2221 | } | |
2222 | ||
a9bc518c | 2223 | k = item_do(i, fd, *fn, CREATION_EXISTING, action); |
936f6bdb FB |
2224 | if (k < 0 && r == 0) |
2225 | r = k; | |
2226 | ||
2227 | /* we passed fd ownership to the previous call */ | |
2228 | fd = -1; | |
c84a9488 | 2229 | } |
99e68c0b | 2230 | |
99e68c0b MS |
2231 | return r; |
2232 | } | |
2233 | ||
c46c3233 AW |
2234 | static int rm_if_wrong_type_safe( |
2235 | mode_t mode, | |
2236 | int parent_fd, | |
21e43a7c | 2237 | const struct stat *parent_st, /* Only used if follow_links below is true. */ |
c46c3233 AW |
2238 | const char *name, |
2239 | int flags) { | |
2240 | _cleanup_free_ char *parent_name = NULL; | |
2241 | bool follow_links = !FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW); | |
2242 | struct stat st; | |
2243 | int r; | |
2244 | ||
2245 | assert(name); | |
2246 | assert((mode & ~S_IFMT) == 0); | |
2247 | assert(!follow_links || parent_st); | |
2248 | assert((flags & ~AT_SYMLINK_NOFOLLOW) == 0); | |
2249 | ||
2250 | if (!filename_is_valid(name)) | |
2251 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "\"%s\" is not a valid filename.", name); | |
2252 | ||
2253 | r = fstatat_harder(parent_fd, name, &st, flags, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2254 | if (r < 0) { | |
2255 | (void) fd_get_path(parent_fd, &parent_name); | |
2256 | return log_full_errno(r == -ENOENT? LOG_DEBUG : LOG_ERR, r, | |
2257 | "Failed to stat \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2258 | } | |
2259 | ||
2260 | /* Fail before removing anything if this is an unsafe transition. */ | |
2261 | if (follow_links && unsafe_transition(parent_st, &st)) { | |
2262 | (void) fd_get_path(parent_fd, &parent_name); | |
2263 | return log_error_errno(SYNTHETIC_ERRNO(ENOLINK), | |
2264 | "Unsafe transition from \"%s\" to \"%s\".", parent_name, name); | |
2265 | } | |
2266 | ||
2267 | if ((st.st_mode & S_IFMT) == mode) | |
2268 | return 0; | |
2269 | ||
2270 | (void) fd_get_path(parent_fd, &parent_name); | |
2271 | log_notice("Wrong file type 0x%x; rm -rf \"%s/%s\"", st.st_mode & S_IFMT, strna(parent_name), name); | |
2272 | ||
2273 | /* If the target of the symlink was the wrong type, the link needs to be removed instead of the | |
2274 | * target, so make sure it is identified as a link and not a directory. */ | |
2275 | if (follow_links) { | |
2276 | r = fstatat_harder(parent_fd, name, &st, AT_SYMLINK_NOFOLLOW, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2277 | if (r < 0) | |
2278 | return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2279 | } | |
2280 | ||
2281 | /* Do not remove mount points. */ | |
2282 | r = fd_is_mount_point(parent_fd, name, follow_links ? AT_SYMLINK_FOLLOW : 0); | |
2283 | if (r < 0) | |
2284 | (void) log_warning_errno(r, "Failed to check if \"%s/%s\" is a mount point: %m; Continuing", | |
2285 | strna(parent_name), name); | |
2286 | else if (r > 0) | |
2287 | return log_error_errno(SYNTHETIC_ERRNO(EBUSY), | |
2288 | "Not removing \"%s/%s\" because it is a mount point.", strna(parent_name), name); | |
2289 | ||
2290 | if ((st.st_mode & S_IFMT) == S_IFDIR) { | |
2291 | _cleanup_close_ int child_fd = -1; | |
2292 | ||
2293 | child_fd = openat(parent_fd, name, O_NOCTTY | O_CLOEXEC | O_DIRECTORY); | |
2294 | if (child_fd < 0) | |
2295 | return log_error_errno(errno, "Failed to open \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2296 | ||
2297 | r = rm_rf_children(TAKE_FD(child_fd), REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL, &st); | |
2298 | if (r < 0) | |
2299 | return log_error_errno(r, "Failed to remove contents of \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2300 | ||
2301 | r = unlinkat_harder(parent_fd, name, AT_REMOVEDIR, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2302 | } else | |
2303 | r = unlinkat_harder(parent_fd, name, 0, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2304 | if (r < 0) | |
2305 | return log_error_errno(r, "Failed to remove \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2306 | ||
2307 | /* This is covered by the log_notice "Wrong file type..." It is logged earlier because it gives | |
2308 | * context to other error messages that might follow. */ | |
2309 | return -ENOENT; | |
2310 | } | |
2311 | ||
2312 | /* If child_mode is non-zero, rm_if_wrong_type_safe will be executed for the last path component. */ | |
2313 | static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) { | |
21e43a7c | 2314 | _cleanup_close_ int parent_fd = -1; |
c46c3233 | 2315 | struct stat parent_st; |
c46c3233 | 2316 | size_t path_len; |
21e43a7c | 2317 | int r; |
c46c3233 AW |
2318 | |
2319 | assert(path); | |
2320 | assert((child_mode & ~S_IFMT) == 0); | |
2321 | ||
2322 | path_len = strlen(path); | |
2323 | ||
2324 | if (!is_path(path)) | |
2325 | /* rm_if_wrong_type_safe already logs errors. */ | |
2326 | return child_mode != 0 ? rm_if_wrong_type_safe(child_mode, AT_FDCWD, NULL, path, AT_SYMLINK_NOFOLLOW) : 0; | |
2327 | ||
2328 | if (child_mode != 0 && endswith(path, "/")) | |
2329 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2330 | "Trailing path separators are only allowed if child_mode is not set; got \"%s\"", path); | |
2331 | ||
2332 | /* Get the parent_fd and stat. */ | |
c68cafba YW |
2333 | parent_fd = openat(AT_FDCWD, path_is_absolute(path) ? "/" : ".", O_NOCTTY | O_CLOEXEC | O_DIRECTORY); |
2334 | if (parent_fd < 0) | |
2335 | return log_error_errno(errno, "Failed to open root: %m"); | |
2336 | ||
c46c3233 AW |
2337 | if (fstat(parent_fd, &parent_st) < 0) |
2338 | return log_error_errno(errno, "Failed to stat root: %m"); | |
2339 | ||
2340 | /* Check every parent directory in the path, except the last component */ | |
21e43a7c YW |
2341 | for (const char *e = path;;) { |
2342 | _cleanup_close_ int next_fd = -1; | |
c46c3233 | 2343 | char t[path_len + 1]; |
21e43a7c | 2344 | const char *s; |
c46c3233 AW |
2345 | |
2346 | /* Find the start of the next path component. */ | |
2347 | s = e + strspn(e, "/"); | |
2348 | /* Find the end of the next path component. */ | |
2349 | e = s + strcspn(s, "/"); | |
2350 | ||
2351 | /* Copy the path component to t so it can be a null terminated string. */ | |
2352 | *((char*) mempcpy(t, s, e - s)) = 0; | |
2353 | ||
2354 | /* Is this the last component? If so, then check the type */ | |
2355 | if (*e == 0) | |
2356 | return child_mode != 0 ? rm_if_wrong_type_safe(child_mode, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW) : 0; | |
c46c3233 | 2357 | |
21e43a7c YW |
2358 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, 0); |
2359 | /* Remove dangling symlinks. */ | |
2360 | if (r == -ENOENT) | |
2361 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW); | |
c46c3233 AW |
2362 | if (r == -ENOENT) { |
2363 | RUN_WITH_UMASK(0000) | |
2364 | r = mkdirat_label(parent_fd, t, 0755); | |
2365 | if (r < 0) { | |
21e43a7c YW |
2366 | _cleanup_free_ char *parent_name = NULL; |
2367 | ||
c46c3233 | 2368 | (void) fd_get_path(parent_fd, &parent_name); |
21e43a7c | 2369 | return log_error_errno(r, "Failed to mkdir \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 AW |
2370 | } |
2371 | } else if (r < 0) | |
2372 | /* rm_if_wrong_type_safe already logs errors. */ | |
2373 | return r; | |
2374 | ||
7c248223 | 2375 | next_fd = RET_NERRNO(openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY)); |
c46c3233 | 2376 | if (next_fd < 0) { |
21e43a7c YW |
2377 | _cleanup_free_ char *parent_name = NULL; |
2378 | ||
c46c3233 | 2379 | (void) fd_get_path(parent_fd, &parent_name); |
7c248223 | 2380 | return log_error_errno(next_fd, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 | 2381 | } |
7c248223 LP |
2382 | r = RET_NERRNO(fstat(next_fd, &parent_st)); |
2383 | if (r < 0) { | |
21e43a7c YW |
2384 | _cleanup_free_ char *parent_name = NULL; |
2385 | ||
c46c3233 | 2386 | (void) fd_get_path(parent_fd, &parent_name); |
21e43a7c | 2387 | return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 AW |
2388 | } |
2389 | ||
ee3455cf | 2390 | close_and_replace(parent_fd, next_fd); |
c46c3233 AW |
2391 | } |
2392 | } | |
2393 | ||
2394 | static int mkdir_parents_item(Item *i, mode_t child_mode) { | |
2395 | int r; | |
2396 | if (i->try_replace) { | |
2397 | r = mkdir_parents_rm_if_wrong_type(child_mode, i->path); | |
2398 | if (r < 0 && r != -ENOENT) | |
2399 | return r; | |
2400 | } else | |
2401 | RUN_WITH_UMASK(0000) | |
2402 | (void) mkdir_parents_label(i->path, 0755); | |
2403 | ||
2404 | return 0; | |
2405 | } | |
2406 | ||
3b63d2d3 | 2407 | static int create_item(Item *i) { |
9e430ce3 | 2408 | int r; |
5008d581 | 2409 | |
3b63d2d3 | 2410 | assert(i); |
5008d581 | 2411 | |
582deb84 ZJS |
2412 | log_debug("Running create action for entry %c %s", (char) i->type, i->path); |
2413 | ||
3b63d2d3 LP |
2414 | switch (i->type) { |
2415 | ||
2416 | case IGNORE_PATH: | |
78a92a5a | 2417 | case IGNORE_DIRECTORY_PATH: |
3b63d2d3 LP |
2418 | case REMOVE_PATH: |
2419 | case RECURSIVE_REMOVE_PATH: | |
2420 | return 0; | |
5008d581 | 2421 | |
eccebf4b | 2422 | case TRUNCATE_FILE: |
3b63d2d3 | 2423 | case CREATE_FILE: |
c46c3233 AW |
2424 | r = mkdir_parents_item(i, S_IFREG); |
2425 | if (r < 0) | |
2426 | return r; | |
7fa10748 | 2427 | |
eccebf4b ZS |
2428 | if ((i->type == CREATE_FILE && i->append_or_force) || i->type == TRUNCATE_FILE) |
2429 | r = truncate_file(i, i->path); | |
2430 | else | |
2431 | r = create_file(i, i->path); | |
5ec9d065 FB |
2432 | if (r < 0) |
2433 | return r; | |
2434 | break; | |
2435 | ||
7b887f22 | 2436 | case COPY_FILES: |
c46c3233 AW |
2437 | r = mkdir_parents_item(i, 0); |
2438 | if (r < 0) | |
2439 | return r; | |
7fa10748 | 2440 | |
b1f7b17f | 2441 | r = copy_files(i); |
849958d1 LP |
2442 | if (r < 0) |
2443 | return r; | |
849958d1 LP |
2444 | break; |
2445 | ||
d4e9eb91 | 2446 | case WRITE_FILE: |
936f6bdb | 2447 | r = glob_item(i, write_one_file); |
f05bc3f7 MS |
2448 | if (r < 0) |
2449 | return r; | |
5008d581 | 2450 | |
3b63d2d3 LP |
2451 | break; |
2452 | ||
3b63d2d3 | 2453 | case CREATE_DIRECTORY: |
d7b8eec7 | 2454 | case TRUNCATE_DIRECTORY: |
c46c3233 AW |
2455 | r = mkdir_parents_item(i, S_IFDIR); |
2456 | if (r < 0) | |
2457 | return r; | |
4c39d899 FB |
2458 | |
2459 | r = create_directory(i, i->path); | |
2460 | if (r < 0) | |
2461 | return r; | |
2462 | break; | |
2463 | ||
d7b8eec7 | 2464 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
2465 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
2466 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
c46c3233 AW |
2467 | r = mkdir_parents_item(i, S_IFDIR); |
2468 | if (r < 0) | |
2469 | return r; | |
d7b8eec7 | 2470 | |
4c39d899 | 2471 | r = create_subvolume(i, i->path); |
4ad36844 FB |
2472 | if (r < 0) |
2473 | return r; | |
2474 | break; | |
5fb13eb5 | 2475 | |
df8dee85 | 2476 | case EMPTY_DIRECTORY: |
54946021 | 2477 | r = glob_item(i, empty_directory); |
f05bc3f7 MS |
2478 | if (r < 0) |
2479 | return r; | |
3b63d2d3 | 2480 | break; |
ee17ee7c LP |
2481 | |
2482 | case CREATE_FIFO: | |
c46c3233 AW |
2483 | r = mkdir_parents_item(i, S_IFIFO); |
2484 | if (r < 0) | |
2485 | return r; | |
7fa10748 | 2486 | |
8f6fb95c | 2487 | r = create_fifo(i); |
f05bc3f7 MS |
2488 | if (r < 0) |
2489 | return r; | |
ee17ee7c | 2490 | break; |
a8d88783 | 2491 | |
9e430ce3 | 2492 | case CREATE_SYMLINK: |
c46c3233 AW |
2493 | r = mkdir_parents_item(i, S_IFLNK); |
2494 | if (r < 0) | |
2495 | return r; | |
7fa10748 | 2496 | |
9e430ce3 LP |
2497 | r = create_symlink(i); |
2498 | if (r < 0) | |
2499 | return r; | |
a542c4dc | 2500 | |
468d726b | 2501 | break; |
468d726b LP |
2502 | |
2503 | case CREATE_BLOCK_DEVICE: | |
074bd73f | 2504 | case CREATE_CHAR_DEVICE: |
cb7ed9df | 2505 | if (have_effective_cap(CAP_MKNOD) == 0) { |
713998cf LP |
2506 | /* In a container we lack CAP_MKNOD. We shouldn't attempt to create the device node in that |
2507 | * case to avoid noise, and we don't support virtualized devices in containers anyway. */ | |
cb7ed9df LP |
2508 | |
2509 | log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path); | |
2510 | return 0; | |
2511 | } | |
2512 | ||
c46c3233 AW |
2513 | r = mkdir_parents_item(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
2514 | if (r < 0) | |
2515 | return r; | |
7fa10748 | 2516 | |
074bd73f | 2517 | r = create_device(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
468d726b LP |
2518 | if (r < 0) |
2519 | return r; | |
2520 | ||
2521 | break; | |
468d726b | 2522 | |
e73a03e0 | 2523 | case ADJUST_MODE: |
777b87e7 | 2524 | case RELABEL_PATH: |
936f6bdb | 2525 | r = glob_item(i, path_set_perms); |
777b87e7 | 2526 | if (r < 0) |
96ca8194 | 2527 | return r; |
777b87e7 MS |
2528 | break; |
2529 | ||
a8d88783 | 2530 | case RECURSIVE_RELABEL_PATH: |
936f6bdb | 2531 | r = glob_item_recursively(i, fd_set_perms); |
a8d88783 MS |
2532 | if (r < 0) |
2533 | return r; | |
ebf4e801 | 2534 | break; |
e73a03e0 | 2535 | |
ebf4e801 | 2536 | case SET_XATTR: |
936f6bdb | 2537 | r = glob_item(i, path_set_xattrs); |
b705ab6a ZJS |
2538 | if (r < 0) |
2539 | return r; | |
2540 | break; | |
2541 | ||
2542 | case RECURSIVE_SET_XATTR: | |
936f6bdb | 2543 | r = glob_item_recursively(i, fd_set_xattrs); |
ebf4e801 MW |
2544 | if (r < 0) |
2545 | return r; | |
e73a03e0 | 2546 | break; |
f8eeeaf9 ZJS |
2547 | |
2548 | case SET_ACL: | |
936f6bdb | 2549 | r = glob_item(i, path_set_acls); |
f8eeeaf9 ZJS |
2550 | if (r < 0) |
2551 | return r; | |
b705ab6a ZJS |
2552 | break; |
2553 | ||
2554 | case RECURSIVE_SET_ACL: | |
936f6bdb | 2555 | r = glob_item_recursively(i, fd_set_acls); |
b705ab6a ZJS |
2556 | if (r < 0) |
2557 | return r; | |
2558 | break; | |
22c3a6ca | 2559 | |
88ec4dfa | 2560 | case SET_ATTRIBUTE: |
936f6bdb | 2561 | r = glob_item(i, path_set_attribute); |
22c3a6ca GB |
2562 | if (r < 0) |
2563 | return r; | |
2564 | break; | |
2565 | ||
88ec4dfa | 2566 | case RECURSIVE_SET_ATTRIBUTE: |
936f6bdb | 2567 | r = glob_item_recursively(i, fd_set_attribute); |
22c3a6ca GB |
2568 | if (r < 0) |
2569 | return r; | |
2570 | break; | |
3b63d2d3 LP |
2571 | } |
2572 | ||
f05bc3f7 | 2573 | return 0; |
3b63d2d3 LP |
2574 | } |
2575 | ||
a9bc518c | 2576 | static int remove_item_instance(Item *i, const char *instance, CreationMode creation) { |
3b63d2d3 LP |
2577 | int r; |
2578 | ||
2579 | assert(i); | |
2580 | ||
2581 | switch (i->type) { | |
2582 | ||
3b63d2d3 | 2583 | case REMOVE_PATH: |
4a62c710 | 2584 | if (remove(instance) < 0 && errno != ENOENT) |
3f93da98 | 2585 | return log_error_errno(errno, "rm(%s): %m", instance); |
3b63d2d3 LP |
2586 | |
2587 | break; | |
2588 | ||
3b63d2d3 | 2589 | case RECURSIVE_REMOVE_PATH: |
2c575977 | 2590 | /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */ |
582deb84 | 2591 | log_debug("rm -rf \"%s\"", instance); |
2c575977 | 2592 | r = rm_rf(instance, REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL); |
f647962d MS |
2593 | if (r < 0 && r != -ENOENT) |
2594 | return log_error_errno(r, "rm_rf(%s): %m", instance); | |
3b63d2d3 LP |
2595 | |
2596 | break; | |
7fcb4b9b ZJS |
2597 | |
2598 | default: | |
04499a70 | 2599 | assert_not_reached(); |
3b63d2d3 LP |
2600 | } |
2601 | ||
2602 | return 0; | |
2603 | } | |
2604 | ||
a0896123 | 2605 | static int remove_item(Item *i) { |
2c575977 LP |
2606 | int r; |
2607 | ||
b8bb3e8f LP |
2608 | assert(i); |
2609 | ||
582deb84 ZJS |
2610 | log_debug("Running remove action for entry %c %s", (char) i->type, i->path); |
2611 | ||
b8bb3e8f LP |
2612 | switch (i->type) { |
2613 | ||
b8bb3e8f | 2614 | case TRUNCATE_DIRECTORY: |
2c575977 LP |
2615 | /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */ |
2616 | log_debug("rm -rf \"%s\"", i->path); | |
2617 | r = rm_rf(i->path, REMOVE_PHYSICAL); | |
2618 | if (r < 0 && r != -ENOENT) | |
2619 | return log_error_errno(r, "rm_rf(%s): %m", i->path); | |
2620 | ||
2621 | return 0; | |
2622 | ||
2623 | case REMOVE_PATH: | |
99e68c0b | 2624 | case RECURSIVE_REMOVE_PATH: |
936f6bdb | 2625 | return glob_item(i, remove_item_instance); |
b8bb3e8f | 2626 | |
df8dee85 ZJS |
2627 | default: |
2628 | return 0; | |
2629 | } | |
b8bb3e8f LP |
2630 | } |
2631 | ||
7f7a50dd SK |
2632 | static char *age_by_to_string(AgeBy ab, bool is_dir) { |
2633 | static const char ab_map[] = { 'a', 'b', 'c', 'm' }; | |
2634 | size_t j = 0; | |
2635 | char *ret; | |
2636 | ||
2637 | ret = new(char, ELEMENTSOF(ab_map) + 1); | |
2638 | if (!ret) | |
2639 | return NULL; | |
2640 | ||
2641 | for (size_t i = 0; i < ELEMENTSOF(ab_map); i++) | |
2642 | if (FLAGS_SET(ab, 1U << i)) | |
2643 | ret[j++] = is_dir ? ascii_toupper(ab_map[i]) : ab_map[i]; | |
2644 | ||
2645 | ret[j] = 0; | |
2646 | return ret; | |
2647 | } | |
2648 | ||
a9bc518c LP |
2649 | static int clean_item_instance( |
2650 | Item *i, | |
2651 | const char* instance, | |
2652 | CreationMode creation) { | |
2653 | ||
7fd1b19b | 2654 | _cleanup_closedir_ DIR *d = NULL; |
7cd296c2 | 2655 | STRUCT_STATX_DEFINE(sx); |
ca194a2a | 2656 | int mountpoint, r; |
78a92a5a | 2657 | usec_t cutoff, n; |
ddb439b8 | 2658 | |
78a92a5a MS |
2659 | assert(i); |
2660 | ||
2661 | if (!i->age_set) | |
2662 | return 0; | |
2663 | ||
2664 | n = now(CLOCK_REALTIME); | |
2665 | if (n < i->age) | |
2666 | return 0; | |
2667 | ||
2668 | cutoff = n - i->age; | |
2669 | ||
df99a9ef | 2670 | d = opendir_nomod(instance); |
78a92a5a | 2671 | if (!d) { |
d710aaf7 | 2672 | if (IN_SET(errno, ENOENT, ENOTDIR)) { |
582deb84 | 2673 | log_debug_errno(errno, "Directory \"%s\": %m", instance); |
78a92a5a | 2674 | return 0; |
582deb84 | 2675 | } |
78a92a5a | 2676 | |
d710aaf7 | 2677 | return log_error_errno(errno, "Failed to open directory %s: %m", instance); |
78a92a5a MS |
2678 | } |
2679 | ||
ca194a2a LP |
2680 | r = statx_fallback(dirfd(d), "", AT_EMPTY_PATH, STATX_MODE|STATX_INO|STATX_ATIME|STATX_MTIME, &sx); |
2681 | if (r < 0) | |
2682 | return log_error_errno(r, "statx(%s) failed: %m", instance); | |
ddb439b8 | 2683 | |
ca194a2a LP |
2684 | if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) |
2685 | mountpoint = FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT); | |
2686 | else { | |
ddb439b8 | 2687 | struct stat ps; |
78a92a5a | 2688 | |
ddb439b8 LP |
2689 | if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) |
2690 | return log_error_errno(errno, "stat(%s/..) failed: %m", i->path); | |
78a92a5a | 2691 | |
ddb439b8 | 2692 | mountpoint = |
ca194a2a LP |
2693 | sx.stx_dev_major != major(ps.st_dev) || |
2694 | sx.stx_dev_minor != minor(ps.st_dev) || | |
2695 | sx.stx_ino != ps.st_ino; | |
ddb439b8 | 2696 | } |
78a92a5a | 2697 | |
7f7a50dd SK |
2698 | if (DEBUG_LOGGING) { |
2699 | _cleanup_free_ char *ab_f = NULL, *ab_d = NULL; | |
2700 | ||
2701 | ab_f = age_by_to_string(i->age_by_file, false); | |
2702 | if (!ab_f) | |
2703 | return log_oom(); | |
2704 | ||
2705 | ab_d = age_by_to_string(i->age_by_dir, true); | |
2706 | if (!ab_d) | |
2707 | return log_oom(); | |
2708 | ||
2709 | log_debug("Cleanup threshold for %s \"%s\" is %s; age-by: %s%s", | |
2710 | mountpoint ? "mount point" : "directory", | |
2711 | instance, | |
0086ef19 | 2712 | FORMAT_TIMESTAMP_STYLE(cutoff, TIMESTAMP_US), |
7f7a50dd SK |
2713 | ab_f, ab_d); |
2714 | } | |
582deb84 | 2715 | |
ddb439b8 | 2716 | return dir_cleanup(i, instance, d, |
ca194a2a LP |
2717 | load_statx_timestamp_nsec(&sx.stx_atime), |
2718 | load_statx_timestamp_nsec(&sx.stx_mtime), | |
2719 | cutoff * NSEC_PER_USEC, | |
2720 | sx.stx_dev_major, sx.stx_dev_minor, mountpoint, | |
7f7a50dd SK |
2721 | MAX_DEPTH, i->keep_first_level, |
2722 | i->age_by_file, i->age_by_dir); | |
78a92a5a MS |
2723 | } |
2724 | ||
2725 | static int clean_item(Item *i) { | |
78a92a5a MS |
2726 | assert(i); |
2727 | ||
582deb84 ZJS |
2728 | log_debug("Running clean action for entry %c %s", (char) i->type, i->path); |
2729 | ||
78a92a5a MS |
2730 | switch (i->type) { |
2731 | case CREATE_DIRECTORY: | |
d7b8eec7 | 2732 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
2733 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
2734 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
78a92a5a MS |
2735 | case TRUNCATE_DIRECTORY: |
2736 | case IGNORE_PATH: | |
849958d1 | 2737 | case COPY_FILES: |
a9bc518c | 2738 | clean_item_instance(i, i->path, CREATION_EXISTING); |
df8dee85 | 2739 | return 0; |
65241c14 | 2740 | case EMPTY_DIRECTORY: |
78a92a5a | 2741 | case IGNORE_DIRECTORY_PATH: |
936f6bdb | 2742 | return glob_item(i, clean_item_instance); |
78a92a5a | 2743 | default: |
df8dee85 | 2744 | return 0; |
78a92a5a | 2745 | } |
78a92a5a MS |
2746 | } |
2747 | ||
599ebe29 | 2748 | static int process_item(Item *i, OperationMask operation) { |
811a1587 | 2749 | OperationMask todo; |
bd6d28f2 ZJS |
2750 | _cleanup_free_ char *_path = NULL; |
2751 | const char *path; | |
811a1587 | 2752 | int r, q, p; |
3b63d2d3 LP |
2753 | |
2754 | assert(i); | |
2755 | ||
811a1587 LP |
2756 | todo = operation & ~i->done; |
2757 | if (todo == 0) /* Everything already done? */ | |
1910cd0e LP |
2758 | return 0; |
2759 | ||
811a1587 | 2760 | i->done |= operation; |
1910cd0e | 2761 | |
bd6d28f2 ZJS |
2762 | path = i->path; |
2763 | if (string_is_glob(path)) { | |
2764 | /* We can't easily check whether a glob matches any autofs path, so let's do the check only | |
2765 | * for the non-glob part. */ | |
2766 | ||
2767 | r = glob_non_glob_prefix(path, &_path); | |
2768 | if (r < 0 && r != -ENOENT) | |
2769 | return log_debug_errno(r, "Failed to deglob path: %m"); | |
2770 | if (r >= 0) | |
2771 | path = _path; | |
2772 | } | |
2773 | ||
2774 | r = chase_symlinks(path, arg_root, CHASE_NO_AUTOFS|CHASE_NONEXISTENT|CHASE_WARN, NULL, NULL); | |
21af3386 | 2775 | if (r == -EREMOTE) { |
bd6d28f2 | 2776 | log_notice_errno(r, "Skipping %s", i->path); /* We log the configured path, to not confuse the user. */ |
811a1587 | 2777 | return 0; |
145b8d0f FB |
2778 | } |
2779 | if (r < 0) | |
48d96904 | 2780 | log_debug_errno(r, "Failed to determine whether '%s' is below autofs, ignoring: %m", i->path); |
655f2da0 | 2781 | |
599ebe29 | 2782 | r = FLAGS_SET(operation, OPERATION_CREATE) ? create_item(i) : 0; |
6d7b5433 WD |
2783 | /* Failure can only be tolerated for create */ |
2784 | if (i->allow_failure) | |
2785 | r = 0; | |
3b63d2d3 | 2786 | |
599ebe29 LP |
2787 | q = FLAGS_SET(operation, OPERATION_REMOVE) ? remove_item(i) : 0; |
2788 | p = FLAGS_SET(operation, OPERATION_CLEAN) ? clean_item(i) : 0; | |
1a967b6b | 2789 | |
811a1587 | 2790 | return r < 0 ? r : |
1db50423 ZJS |
2791 | q < 0 ? q : |
2792 | p; | |
3b63d2d3 LP |
2793 | } |
2794 | ||
599ebe29 | 2795 | static int process_item_array(ItemArray *array, OperationMask operation) { |
96d10d78 LP |
2796 | int r = 0; |
2797 | size_t n; | |
753615e8 | 2798 | |
3f93da98 ZJS |
2799 | assert(array); |
2800 | ||
811a1587 LP |
2801 | /* Create any parent first. */ |
2802 | if (FLAGS_SET(operation, OPERATION_CREATE) && array->parent) | |
2803 | r = process_item_array(array->parent, operation & OPERATION_CREATE); | |
2804 | ||
2805 | /* Clean up all children first */ | |
2806 | if ((operation & (OPERATION_REMOVE|OPERATION_CLEAN)) && !set_isempty(array->children)) { | |
811a1587 LP |
2807 | ItemArray *c; |
2808 | ||
90e74a66 | 2809 | SET_FOREACH(c, array->children) { |
811a1587 LP |
2810 | int k; |
2811 | ||
2812 | k = process_item_array(c, operation & (OPERATION_REMOVE|OPERATION_CLEAN)); | |
2813 | if (k < 0 && r == 0) | |
2814 | r = k; | |
2815 | } | |
2816 | } | |
2817 | ||
96d10d78 LP |
2818 | for (n = 0; n < array->n_items; n++) { |
2819 | int k; | |
2820 | ||
599ebe29 | 2821 | k = process_item(array->items + n, operation); |
3f93da98 ZJS |
2822 | if (k < 0 && r == 0) |
2823 | r = k; | |
2824 | } | |
2825 | ||
2826 | return r; | |
2827 | } | |
3b63d2d3 | 2828 | |
3f93da98 ZJS |
2829 | static void item_free_contents(Item *i) { |
2830 | assert(i); | |
3b63d2d3 | 2831 | free(i->path); |
468d726b | 2832 | free(i->argument); |
708daf42 | 2833 | free(i->binary_argument); |
ebf4e801 | 2834 | strv_free(i->xattrs); |
f8eeeaf9 | 2835 | |
349cc4a5 | 2836 | #if HAVE_ACL |
f8eeeaf9 ZJS |
2837 | acl_free(i->acl_access); |
2838 | acl_free(i->acl_default); | |
2839 | #endif | |
3b63d2d3 LP |
2840 | } |
2841 | ||
87938c3b | 2842 | static ItemArray* item_array_free(ItemArray *a) { |
96d10d78 | 2843 | size_t n; |
3f93da98 ZJS |
2844 | |
2845 | if (!a) | |
87938c3b | 2846 | return NULL; |
3f93da98 | 2847 | |
96d10d78 | 2848 | for (n = 0; n < a->n_items; n++) |
3f93da98 | 2849 | item_free_contents(a->items + n); |
96d10d78 | 2850 | |
811a1587 | 2851 | set_free(a->children); |
3f93da98 | 2852 | free(a->items); |
87938c3b | 2853 | return mfree(a); |
3f93da98 | 2854 | } |
e2f2fb78 | 2855 | |
93bab288 | 2856 | static int item_compare(const Item *a, const Item *b) { |
17493fa5 LP |
2857 | /* Make sure that the ownership taking item is put first, so |
2858 | * that we first create the node, and then can adjust it */ | |
2859 | ||
93bab288 | 2860 | if (takes_ownership(a->type) && !takes_ownership(b->type)) |
17493fa5 | 2861 | return -1; |
93bab288 | 2862 | if (!takes_ownership(a->type) && takes_ownership(b->type)) |
17493fa5 LP |
2863 | return 1; |
2864 | ||
93bab288 | 2865 | return CMP(a->type, b->type); |
17493fa5 LP |
2866 | } |
2867 | ||
a6aafd6a | 2868 | static bool item_compatible(const Item *a, const Item *b) { |
bfe95f35 LP |
2869 | assert(a); |
2870 | assert(b); | |
3f93da98 | 2871 | assert(streq(a->path, b->path)); |
bfe95f35 | 2872 | |
3f93da98 ZJS |
2873 | if (takes_ownership(a->type) && takes_ownership(b->type)) |
2874 | /* check if the items are the same */ | |
708daf42 LP |
2875 | return memcmp_nn(item_binary_argument(a), item_binary_argument_size(a), |
2876 | item_binary_argument(b), item_binary_argument_size(b)) == 0 && | |
bfe95f35 | 2877 | |
3f93da98 ZJS |
2878 | a->uid_set == b->uid_set && |
2879 | a->uid == b->uid && | |
cc43328c | 2880 | a->uid_only_create == b->uid_only_create && |
bfe95f35 | 2881 | |
3f93da98 ZJS |
2882 | a->gid_set == b->gid_set && |
2883 | a->gid == b->gid && | |
cc43328c | 2884 | a->gid_only_create == b->gid_only_create && |
bfe95f35 | 2885 | |
3f93da98 ZJS |
2886 | a->mode_set == b->mode_set && |
2887 | a->mode == b->mode && | |
cc43328c | 2888 | a->mode_only_create == b->mode_only_create && |
bfe95f35 | 2889 | |
3f93da98 ZJS |
2890 | a->age_set == b->age_set && |
2891 | a->age == b->age && | |
bfe95f35 | 2892 | |
7f7a50dd SK |
2893 | a->age_by_file == b->age_by_file && |
2894 | a->age_by_dir == b->age_by_dir && | |
2895 | ||
3f93da98 | 2896 | a->mask_perms == b->mask_perms && |
bfe95f35 | 2897 | |
3f93da98 | 2898 | a->keep_first_level == b->keep_first_level && |
468d726b | 2899 | |
3f93da98 | 2900 | a->major_minor == b->major_minor; |
468d726b | 2901 | |
bfe95f35 LP |
2902 | return true; |
2903 | } | |
2904 | ||
a2aced4a | 2905 | static bool should_include_path(const char *path) { |
abef3f91 | 2906 | STRV_FOREACH(prefix, arg_exclude_prefixes) |
582deb84 ZJS |
2907 | if (path_startswith(path, *prefix)) { |
2908 | log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.", | |
2909 | path, *prefix); | |
5c795114 | 2910 | return false; |
582deb84 | 2911 | } |
a2aced4a | 2912 | |
abef3f91 | 2913 | STRV_FOREACH(prefix, arg_include_prefixes) |
582deb84 ZJS |
2914 | if (path_startswith(path, *prefix)) { |
2915 | log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix); | |
a2aced4a | 2916 | return true; |
582deb84 | 2917 | } |
a2aced4a | 2918 | |
6b000af4 | 2919 | /* no matches, so we should include this path only if we have no allow list at all */ |
7b943bb7 | 2920 | if (strv_isempty(arg_include_prefixes)) |
582deb84 ZJS |
2921 | return true; |
2922 | ||
2923 | log_debug("Entry \"%s\" does not match any include prefix, skipping.", path); | |
2924 | return false; | |
a2aced4a DR |
2925 | } |
2926 | ||
172e9cc3 | 2927 | static int specifier_expansion_from_arg(const Specifier *specifier_table, Item *i) { |
4cef1923 FB |
2928 | int r; |
2929 | ||
2930 | assert(i); | |
2931 | ||
4e361acc | 2932 | if (!i->argument) |
4cef1923 FB |
2933 | return 0; |
2934 | ||
2935 | switch (i->type) { | |
2936 | case COPY_FILES: | |
2937 | case CREATE_SYMLINK: | |
2938 | case CREATE_FILE: | |
2939 | case TRUNCATE_FILE: | |
e437538f ZJS |
2940 | case WRITE_FILE: { |
2941 | _cleanup_free_ char *unescaped = NULL, *resolved = NULL; | |
2942 | ssize_t l; | |
2943 | ||
2944 | l = cunescape(i->argument, 0, &unescaped); | |
2945 | if (l < 0) | |
2946 | return log_error_errno(l, "Failed to unescape parameter to write: %s", i->argument); | |
4cef1923 | 2947 | |
de61a04b | 2948 | r = specifier_printf(unescaped, PATH_MAX-1, specifier_table, arg_root, NULL, &resolved); |
4cef1923 FB |
2949 | if (r < 0) |
2950 | return r; | |
2951 | ||
e437538f ZJS |
2952 | return free_and_replace(i->argument, resolved); |
2953 | } | |
4cef1923 | 2954 | case SET_XATTR: |
de010b0b | 2955 | case RECURSIVE_SET_XATTR: |
dfe01841 | 2956 | STRV_FOREACH(xattr, i->xattrs) { |
e437538f ZJS |
2957 | _cleanup_free_ char *resolved = NULL; |
2958 | ||
de61a04b | 2959 | r = specifier_printf(*xattr, SIZE_MAX, specifier_table, arg_root, NULL, &resolved); |
4cef1923 FB |
2960 | if (r < 0) |
2961 | return r; | |
2962 | ||
2963 | free_and_replace(*xattr, resolved); | |
2964 | } | |
e437538f | 2965 | return 0; |
de010b0b | 2966 | |
4cef1923 | 2967 | default: |
e437538f | 2968 | return 0; |
4cef1923 | 2969 | } |
4cef1923 FB |
2970 | } |
2971 | ||
a2d1fb88 LP |
2972 | static int patch_var_run(const char *fname, unsigned line, char **path) { |
2973 | const char *k; | |
2974 | char *n; | |
2975 | ||
2976 | assert(path); | |
2977 | assert(*path); | |
2978 | ||
2979 | /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges lines in | |
2980 | * some cases and detects conflicts in others. If files/directories are specified through two equivalent lines | |
2981 | * this is problematic as neither case will be detected. Ideally we'd detect these cases by resolving symlinks | |
2982 | * early, but that's precisely not what we can do here as this code very likely is running very early on, at a | |
2983 | * time where the paths in question are not available yet, or even more importantly, our own tmpfiles rules | |
2984 | * might create the paths that are intermediary to the listed paths. We can't really cover the generic case, | |
2985 | * but the least we can do is cover the specific case of /var/run vs. /run, as /var/run is a legacy name for | |
2986 | * /run only, and we explicitly document that and require that on systemd systems the former is a symlink to | |
2987 | * the latter. Moreover files below this path are by far the primary usecase for tmpfiles.d/. */ | |
2988 | ||
2989 | k = path_startswith(*path, "/var/run/"); | |
2990 | if (isempty(k)) /* Don't complain about other paths than /var/run, and not about /var/run itself either. */ | |
2991 | return 0; | |
2992 | ||
2d9b74ba | 2993 | n = path_join("/run", k); |
a2d1fb88 LP |
2994 | if (!n) |
2995 | return log_oom(); | |
2996 | ||
2997 | /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation automatically, hence | |
2998 | * there's no immediate need for action by the user. However, in the interest of making things less confusing | |
2999 | * to the user, let's still inform the user that these snippets should really be updated. */ | |
b88ba6c7 ZJS |
3000 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, |
3001 | "Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", | |
3002 | *path, n); | |
a2d1fb88 | 3003 | |
81fa4479 | 3004 | free_and_replace(*path, n); |
a2d1fb88 LP |
3005 | |
3006 | return 0; | |
3007 | } | |
3008 | ||
a3451c2c LP |
3009 | static int find_uid(const char *user, uid_t *ret_uid, Hashmap **cache) { |
3010 | int r; | |
3011 | ||
3012 | assert(user); | |
3013 | assert(ret_uid); | |
3014 | ||
3015 | /* First: parse as numeric UID string */ | |
3016 | r = parse_uid(user, ret_uid); | |
3017 | if (r >= 0) | |
3018 | return r; | |
3019 | ||
3020 | /* Second: pass to NSS if we are running "online" */ | |
3021 | if (!arg_root) | |
3022 | return get_user_creds(&user, ret_uid, NULL, NULL, NULL, 0); | |
3023 | ||
3024 | /* Third, synthesize "root" unconditionally */ | |
3025 | if (streq(user, "root")) { | |
3026 | *ret_uid = 0; | |
3027 | return 0; | |
3028 | } | |
3029 | ||
3030 | /* Fourth: use fgetpwent() to read /etc/passwd directly, if we are "offline" */ | |
3031 | return name_to_uid_offline(arg_root, user, ret_uid, cache); | |
3032 | } | |
3033 | ||
3034 | static int find_gid(const char *group, gid_t *ret_gid, Hashmap **cache) { | |
3035 | int r; | |
3036 | ||
3037 | assert(group); | |
3038 | assert(ret_gid); | |
3039 | ||
3040 | /* First: parse as numeric GID string */ | |
3041 | r = parse_gid(group, ret_gid); | |
3042 | if (r >= 0) | |
3043 | return r; | |
3044 | ||
3045 | /* Second: pass to NSS if we are running "online" */ | |
3046 | if (!arg_root) | |
3047 | return get_group_creds(&group, ret_gid, 0); | |
3048 | ||
3049 | /* Third, synthesize "root" unconditionally */ | |
3050 | if (streq(group, "root")) { | |
3051 | *ret_gid = 0; | |
3052 | return 0; | |
3053 | } | |
3054 | ||
3055 | /* Fourth: use fgetgrent() to read /etc/group directly, if we are "offline" */ | |
3056 | return name_to_gid_offline(arg_root, group, ret_gid, cache); | |
3057 | } | |
3058 | ||
7f7a50dd SK |
3059 | static int parse_age_by_from_arg(const char *age_by_str, Item *item) { |
3060 | AgeBy ab_f = 0, ab_d = 0; | |
3061 | ||
3062 | static const struct { | |
3063 | char age_by_chr; | |
3064 | AgeBy age_by_flag; | |
3065 | } age_by_types[] = { | |
3066 | { 'a', AGE_BY_ATIME }, | |
3067 | { 'b', AGE_BY_BTIME }, | |
3068 | { 'c', AGE_BY_CTIME }, | |
3069 | { 'm', AGE_BY_MTIME }, | |
3070 | }; | |
3071 | ||
3072 | assert(age_by_str); | |
3073 | assert(item); | |
3074 | ||
3075 | if (isempty(age_by_str)) | |
3076 | return -EINVAL; | |
3077 | ||
3078 | for (const char *s = age_by_str; *s != 0; s++) { | |
3079 | size_t i; | |
3080 | ||
3081 | /* Ignore whitespace. */ | |
3082 | if (strchr(WHITESPACE, *s)) | |
3083 | continue; | |
3084 | ||
3085 | for (i = 0; i < ELEMENTSOF(age_by_types); i++) { | |
3086 | /* Check lower-case for files, upper-case for directories. */ | |
3087 | if (*s == age_by_types[i].age_by_chr) { | |
3088 | ab_f |= age_by_types[i].age_by_flag; | |
3089 | break; | |
3090 | } else if (*s == ascii_toupper(age_by_types[i].age_by_chr)) { | |
3091 | ab_d |= age_by_types[i].age_by_flag; | |
3092 | break; | |
3093 | } | |
3094 | } | |
3095 | ||
3096 | /* Invalid character. */ | |
3097 | if (i >= ELEMENTSOF(age_by_types)) | |
3098 | return -EINVAL; | |
3099 | } | |
3100 | ||
3101 | /* No match. */ | |
3102 | if (ab_f == 0 && ab_d == 0) | |
3103 | return -EINVAL; | |
3104 | ||
3105 | item->age_by_file = ab_f > 0 ? ab_f : AGE_BY_DEFAULT_FILE; | |
3106 | item->age_by_dir = ab_d > 0 ? ab_d : AGE_BY_DEFAULT_DIR; | |
3107 | ||
3108 | return 0; | |
3109 | } | |
3110 | ||
a6aafd6a | 3111 | static bool is_duplicated_item(ItemArray *existing, const Item *i) { |
9af74e0f FB |
3112 | |
3113 | assert(existing); | |
3114 | assert(i); | |
3115 | ||
3116 | for (size_t n = 0; n < existing->n_items; n++) { | |
a6aafd6a | 3117 | const Item *e = existing->items + n; |
9af74e0f FB |
3118 | |
3119 | if (item_compatible(e, i)) | |
3120 | continue; | |
3121 | ||
3122 | /* Only multiple 'w+' lines for the same path are allowed. */ | |
3123 | if (e->type != WRITE_FILE || !e->append_or_force || | |
3124 | i->type != WRITE_FILE || !i->append_or_force) | |
3125 | return true; | |
3126 | } | |
3127 | ||
3128 | return false; | |
3129 | } | |
3130 | ||
a3451c2c LP |
3131 | static int parse_line( |
3132 | const char *fname, | |
3133 | unsigned line, | |
3134 | const char *buffer, | |
3135 | bool *invalid_config, | |
3136 | Hashmap **uid_cache, | |
3137 | Hashmap **gid_cache) { | |
1731e34a | 3138 | |
cde684a2 | 3139 | _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL; |
7f7a50dd SK |
3140 | _cleanup_(item_free_contents) Item i = { |
3141 | /* The "age-by" argument considers all file timestamp types by default. */ | |
3142 | .age_by_file = AGE_BY_DEFAULT_FILE, | |
3143 | .age_by_dir = AGE_BY_DEFAULT_DIR, | |
3144 | }; | |
3f93da98 | 3145 | ItemArray *existing; |
ef43a391 | 3146 | OrderedHashmap *h; |
657cf7f4 | 3147 | int r, pos; |
e52f6f63 | 3148 | bool append_or_force = false, boot = false, allow_failure = false, try_replace = false, unbase64 = false, from_cred = false; |
3b63d2d3 LP |
3149 | |
3150 | assert(fname); | |
3151 | assert(line >= 1); | |
3152 | assert(buffer); | |
3153 | ||
172e9cc3 ZJS |
3154 | const Specifier specifier_table[] = { |
3155 | { 'a', specifier_architecture, NULL }, | |
3156 | { 'b', specifier_boot_id, NULL }, | |
3157 | { 'B', specifier_os_build_id, NULL }, | |
9a5893e9 ZJS |
3158 | { 'H', specifier_hostname, NULL }, |
3159 | { 'l', specifier_short_hostname, NULL }, | |
6db98098 | 3160 | { 'm', specifier_machine_id, NULL }, |
172e9cc3 ZJS |
3161 | { 'o', specifier_os_id, NULL }, |
3162 | { 'v', specifier_kernel_release, NULL }, | |
3163 | { 'w', specifier_os_version_id, NULL }, | |
3164 | { 'W', specifier_os_variant_id, NULL }, | |
3165 | ||
3166 | { 'h', specifier_user_home, NULL }, | |
3167 | ||
3168 | { 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) }, | |
3169 | { 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) }, | |
3170 | { 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) }, | |
3171 | { 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) }, | |
3172 | ||
b380b643 | 3173 | COMMON_CREDS_SPECIFIERS(arg_user ? LOOKUP_SCOPE_USER : LOOKUP_SCOPE_SYSTEM), |
172e9cc3 ZJS |
3174 | COMMON_TMP_SPECIFIERS, |
3175 | {} | |
3176 | }; | |
3177 | ||
68685607 | 3178 | r = extract_many_words( |
4034a06d | 3179 | &buffer, |
68685607 | 3180 | NULL, |
4ec85141 | 3181 | EXTRACT_UNQUOTE, |
4034a06d LP |
3182 | &action, |
3183 | &path, | |
3184 | &mode, | |
3185 | &user, | |
3186 | &group, | |
3187 | &age, | |
3188 | NULL); | |
d9daae55 | 3189 | if (r < 0) { |
751223fe ZJS |
3190 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3191 | /* invalid quoting and such or an unknown specifier */ | |
d9daae55 | 3192 | *invalid_config = true; |
d02933fd | 3193 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to parse line: %m"); |
4b93699d | 3194 | } else if (r < 2) { |
d9daae55 | 3195 | *invalid_config = true; |
d02933fd | 3196 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Syntax error."); |
5008d581 LP |
3197 | } |
3198 | ||
e7b88b7b | 3199 | if (!empty_or_dash(buffer)) { |
4034a06d LP |
3200 | i.argument = strdup(buffer); |
3201 | if (!i.argument) | |
3202 | return log_oom(); | |
3203 | } | |
3204 | ||
2e78fa79 | 3205 | if (isempty(action)) { |
d9daae55 | 3206 | *invalid_config = true; |
d02933fd | 3207 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Command too short '%s'.", action); |
2e78fa79 LP |
3208 | } |
3209 | ||
5f255144 ZJS |
3210 | for (pos = 1; action[pos]; pos++) { |
3211 | if (action[pos] == '!' && !boot) | |
3212 | boot = true; | |
c55ac248 ZS |
3213 | else if (action[pos] == '+' && !append_or_force) |
3214 | append_or_force = true; | |
6d7b5433 WD |
3215 | else if (action[pos] == '-' && !allow_failure) |
3216 | allow_failure = true; | |
c46c3233 AW |
3217 | else if (action[pos] == '=' && !try_replace) |
3218 | try_replace = true; | |
708daf42 LP |
3219 | else if (action[pos] == '~' && !unbase64) |
3220 | unbase64 = true; | |
e52f6f63 LP |
3221 | else if (action[pos] == '^' && !from_cred) |
3222 | from_cred = true; | |
5f255144 | 3223 | else { |
d9daae55 | 3224 | *invalid_config = true; |
d02933fd | 3225 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Unknown modifiers in command '%s'", action); |
5f255144 | 3226 | } |
2e78fa79 LP |
3227 | } |
3228 | ||
582deb84 | 3229 | if (boot && !arg_boot) { |
d02933fd | 3230 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, "Ignoring entry %s \"%s\" because --boot is not specified.", action, path); |
c4708f13 | 3231 | return 0; |
582deb84 | 3232 | } |
c4708f13 | 3233 | |
3f93da98 | 3234 | i.type = action[0]; |
c55ac248 | 3235 | i.append_or_force = append_or_force; |
6d7b5433 | 3236 | i.allow_failure = allow_failure; |
c46c3233 | 3237 | i.try_replace = try_replace; |
2e78fa79 | 3238 | |
de61a04b | 3239 | r = specifier_printf(path, PATH_MAX-1, specifier_table, arg_root, NULL, &i.path); |
6db98098 | 3240 | if (ERRNO_IS_NOINFO(r)) |
4cef1923 | 3241 | return log_unresolvable_specifier(fname, line); |
d9daae55 | 3242 | if (r < 0) { |
751223fe ZJS |
3243 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3244 | *invalid_config = true; | |
d02933fd | 3245 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to replace specifiers in '%s': %m", path); |
d9daae55 | 3246 | } |
1731e34a | 3247 | |
a2d1fb88 LP |
3248 | r = patch_var_run(fname, line, &i.path); |
3249 | if (r < 0) | |
3250 | return r; | |
3251 | ||
35c18a51 LP |
3252 | if (!path_is_absolute(i.path)) { |
3253 | *invalid_config = true; | |
3254 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), | |
3255 | "Path '%s' not absolute.", i.path); | |
3256 | } | |
3257 | ||
3258 | path_simplify(i.path); | |
3259 | ||
3f93da98 | 3260 | switch (i.type) { |
468d726b | 3261 | |
777b87e7 | 3262 | case CREATE_DIRECTORY: |
d7b8eec7 | 3263 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
3264 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
3265 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
df8dee85 | 3266 | case EMPTY_DIRECTORY: |
777b87e7 MS |
3267 | case TRUNCATE_DIRECTORY: |
3268 | case CREATE_FIFO: | |
3269 | case IGNORE_PATH: | |
78a92a5a | 3270 | case IGNORE_DIRECTORY_PATH: |
777b87e7 MS |
3271 | case REMOVE_PATH: |
3272 | case RECURSIVE_REMOVE_PATH: | |
e73a03e0 | 3273 | case ADJUST_MODE: |
777b87e7 MS |
3274 | case RELABEL_PATH: |
3275 | case RECURSIVE_RELABEL_PATH: | |
c82500c6 | 3276 | if (i.argument) |
2676befc JJ |
3277 | log_syntax(NULL, |
3278 | LOG_WARNING, | |
3279 | fname, | |
3280 | line, | |
3281 | 0, | |
3282 | "%c lines don't take argument fields, ignoring.", | |
3283 | (char) i.type); | |
c82500c6 LP |
3284 | |
3285 | break; | |
3286 | ||
3287 | case CREATE_FILE: | |
3288 | case TRUNCATE_FILE: | |
777b87e7 | 3289 | break; |
468d726b LP |
3290 | |
3291 | case CREATE_SYMLINK: | |
708daf42 LP |
3292 | if (unbase64) { |
3293 | *invalid_config = true; | |
3294 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for symlink targets."); | |
3295 | } | |
3296 | ||
3f93da98 | 3297 | if (!i.argument) { |
b910cc72 | 3298 | i.argument = path_join("/usr/share/factory", i.path); |
3f93da98 | 3299 | if (!i.argument) |
2f3b873a | 3300 | return log_oom(); |
468d726b LP |
3301 | } |
3302 | break; | |
3303 | ||
31ed59c5 | 3304 | case WRITE_FILE: |
3f93da98 | 3305 | if (!i.argument) { |
d9daae55 | 3306 | *invalid_config = true; |
d02933fd | 3307 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Write file requires argument."); |
31ed59c5 LP |
3308 | } |
3309 | break; | |
3310 | ||
849958d1 | 3311 | case COPY_FILES: |
708daf42 LP |
3312 | if (unbase64) { |
3313 | *invalid_config = true; | |
3314 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for copy sources."); | |
3315 | } | |
3316 | ||
3f93da98 | 3317 | if (!i.argument) { |
7dc6477d | 3318 | i.argument = path_join("/usr/share/factory", i.path); |
3f93da98 | 3319 | if (!i.argument) |
2f3b873a | 3320 | return log_oom(); |
3f93da98 | 3321 | } else if (!path_is_absolute(i.argument)) { |
d9daae55 | 3322 | *invalid_config = true; |
d02933fd | 3323 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Source path '%s' is not absolute.", i.argument); |
626f8d16 | 3324 | |
7dc6477d LP |
3325 | } |
3326 | ||
3327 | if (!empty_or_root(arg_root)) { | |
626f8d16 ZJS |
3328 | char *p; |
3329 | ||
d34cd571 | 3330 | p = path_join(arg_root, i.argument); |
626f8d16 ZJS |
3331 | if (!p) |
3332 | return log_oom(); | |
3333 | free_and_replace(i.argument, p); | |
849958d1 LP |
3334 | } |
3335 | ||
4ff361cc | 3336 | path_simplify(i.argument); |
ce610af1 LP |
3337 | |
3338 | if (laccess(i.argument, F_OK) == -ENOENT) { | |
3339 | /* Silently skip over lines where the source file is missing. */ | |
9109009d | 3340 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, "Copy source path '%s' does not exist, skipping line.", i.argument); |
ce610af1 LP |
3341 | return 0; |
3342 | } | |
3343 | ||
849958d1 LP |
3344 | break; |
3345 | ||
468d726b | 3346 | case CREATE_CHAR_DEVICE: |
3a47c40d | 3347 | case CREATE_BLOCK_DEVICE: |
708daf42 LP |
3348 | if (unbase64) { |
3349 | *invalid_config = true; | |
3350 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for device node creation."); | |
3351 | } | |
3352 | ||
3f93da98 | 3353 | if (!i.argument) { |
d9daae55 | 3354 | *invalid_config = true; |
d02933fd | 3355 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Device file requires argument."); |
468d726b LP |
3356 | } |
3357 | ||
7176f06c | 3358 | r = parse_devnum(i.argument, &i.major_minor); |
3a47c40d | 3359 | if (r < 0) { |
d9daae55 | 3360 | *invalid_config = true; |
d02933fd | 3361 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Can't parse device file major/minor '%s'.", i.argument); |
468d726b LP |
3362 | } |
3363 | ||
468d726b | 3364 | break; |
468d726b | 3365 | |
ebf4e801 | 3366 | case SET_XATTR: |
b705ab6a | 3367 | case RECURSIVE_SET_XATTR: |
708daf42 LP |
3368 | if (unbase64) { |
3369 | *invalid_config = true; | |
3370 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for extended attributes."); | |
3371 | } | |
3f93da98 | 3372 | if (!i.argument) { |
d9daae55 | 3373 | *invalid_config = true; |
d02933fd LP |
3374 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3375 | "Set extended attribute requires argument."); | |
ebf4e801 | 3376 | } |
bd550f78 | 3377 | r = parse_xattrs_from_arg(&i); |
ebf4e801 MW |
3378 | if (r < 0) |
3379 | return r; | |
3380 | break; | |
3381 | ||
f8eeeaf9 | 3382 | case SET_ACL: |
b705ab6a | 3383 | case RECURSIVE_SET_ACL: |
708daf42 LP |
3384 | if (unbase64) { |
3385 | *invalid_config = true; | |
3386 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for ACLs."); | |
3387 | } | |
f8eeeaf9 | 3388 | if (!i.argument) { |
d9daae55 | 3389 | *invalid_config = true; |
d02933fd LP |
3390 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3391 | "Set ACLs requires argument."); | |
f8eeeaf9 | 3392 | } |
bd550f78 | 3393 | r = parse_acls_from_arg(&i); |
f8eeeaf9 ZJS |
3394 | if (r < 0) |
3395 | return r; | |
3396 | break; | |
3397 | ||
88ec4dfa LP |
3398 | case SET_ATTRIBUTE: |
3399 | case RECURSIVE_SET_ATTRIBUTE: | |
708daf42 LP |
3400 | if (unbase64) { |
3401 | *invalid_config = true; | |
3402 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for file attributes."); | |
3403 | } | |
22c3a6ca | 3404 | if (!i.argument) { |
d9daae55 | 3405 | *invalid_config = true; |
d02933fd LP |
3406 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3407 | "Set file attribute requires argument."); | |
22c3a6ca | 3408 | } |
bd550f78 | 3409 | r = parse_attribute_from_arg(&i); |
751223fe | 3410 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
d9daae55 | 3411 | *invalid_config = true; |
22c3a6ca GB |
3412 | if (r < 0) |
3413 | return r; | |
3414 | break; | |
3415 | ||
777b87e7 | 3416 | default: |
d9daae55 | 3417 | *invalid_config = true; |
d02933fd LP |
3418 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3419 | "Unknown command type '%c'.", (char) i.type); | |
3b63d2d3 | 3420 | } |
468d726b | 3421 | |
3f93da98 | 3422 | if (!should_include_path(i.path)) |
7f2c1f4d | 3423 | return 0; |
5008d581 | 3424 | |
e52f6f63 LP |
3425 | if (!unbase64) { |
3426 | /* Do specifier expansion except if base64 mode is enabled */ | |
708daf42 | 3427 | r = specifier_expansion_from_arg(specifier_table, &i); |
6db98098 | 3428 | if (ERRNO_IS_NOINFO(r)) |
708daf42 LP |
3429 | return log_unresolvable_specifier(fname, line); |
3430 | if (r < 0) { | |
3431 | if (IN_SET(r, -EINVAL, -EBADSLT)) | |
3432 | *invalid_config = true; | |
3433 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to substitute specifiers in argument: %m"); | |
3434 | } | |
751223fe | 3435 | } |
4cef1923 | 3436 | |
e52f6f63 LP |
3437 | if (from_cred) { |
3438 | if (!i.argument) | |
3439 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL), "Reading from credential requested, but no credential name specified."); | |
3440 | if (!credential_name_valid(i.argument)) | |
3441 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL), "Credential name not valid: %s", i.argument); | |
3442 | ||
3443 | r = read_credential(i.argument, &i.binary_argument, &i.binary_argument_size); | |
3444 | if (IN_SET(r, -ENXIO, -ENOENT)) { | |
3445 | /* Silently skip over lines that have no credentials passed */ | |
9109009d | 3446 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, |
6db98098 | 3447 | "Credential '%s' not specified, skipping line.", i.argument); |
e52f6f63 LP |
3448 | return 0; |
3449 | } | |
3450 | if (r < 0) | |
3451 | return log_error_errno(r, "Failed to read credential '%s': %m", i.argument); | |
3452 | } | |
3453 | ||
3454 | /* If base64 decoding is requested, do so now */ | |
3455 | if (unbase64 && item_binary_argument(&i)) { | |
3456 | _cleanup_free_ void *data = NULL; | |
3457 | size_t data_size = 0; | |
3458 | ||
3459 | r = unbase64mem(item_binary_argument(&i), item_binary_argument_size(&i), &data, &data_size); | |
3460 | if (r < 0) | |
3461 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to base64 decode specified argument '%s': %m", i.argument); | |
3462 | ||
3463 | free_and_replace(i.binary_argument, data); | |
3464 | i.binary_argument_size = data_size; | |
3465 | } | |
3466 | ||
7dc6477d | 3467 | if (!empty_or_root(arg_root)) { |
cde684a2 LP |
3468 | char *p; |
3469 | ||
c6134d3e | 3470 | p = path_join(arg_root, i.path); |
cf9a4abd MM |
3471 | if (!p) |
3472 | return log_oom(); | |
81fa4479 | 3473 | free_and_replace(i.path, p); |
cf9a4abd MM |
3474 | } |
3475 | ||
e7b88b7b | 3476 | if (!empty_or_dash(user)) { |
cc43328c LP |
3477 | const char *u; |
3478 | ||
3479 | u = startswith(user, ":"); | |
3480 | if (u) | |
3481 | i.uid_only_create = true; | |
3482 | else | |
3483 | u = user; | |
3484 | ||
3485 | r = find_uid(u, &i.uid, uid_cache); | |
4b67834e | 3486 | if (r < 0) { |
d9daae55 | 3487 | *invalid_config = true; |
cc43328c | 3488 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve user '%s': %m", u); |
3b63d2d3 LP |
3489 | } |
3490 | ||
3f93da98 | 3491 | i.uid_set = true; |
3b63d2d3 LP |
3492 | } |
3493 | ||
e7b88b7b | 3494 | if (!empty_or_dash(group)) { |
cc43328c LP |
3495 | const char *g; |
3496 | ||
3497 | g = startswith(group, ":"); | |
3498 | if (g) | |
3499 | i.gid_only_create = true; | |
3500 | else | |
3501 | g = group; | |
3502 | ||
3503 | r = find_gid(g, &i.gid, gid_cache); | |
4b67834e | 3504 | if (r < 0) { |
d9daae55 | 3505 | *invalid_config = true; |
cc43328c | 3506 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve group '%s'.", g); |
3b63d2d3 LP |
3507 | } |
3508 | ||
3f93da98 | 3509 | i.gid_set = true; |
3b63d2d3 LP |
3510 | } |
3511 | ||
e7b88b7b | 3512 | if (!empty_or_dash(mode)) { |
cc43328c | 3513 | const char *mm; |
3b63d2d3 LP |
3514 | unsigned m; |
3515 | ||
cc43328c LP |
3516 | for (mm = mode;; mm++) { |
3517 | if (*mm == '~') | |
3518 | i.mask_perms = true; | |
3519 | else if (*mm == ':') | |
3520 | i.mode_only_create = true; | |
3521 | else | |
3522 | break; | |
abef3f91 LP |
3523 | } |
3524 | ||
d02933fd LP |
3525 | r = parse_mode(mm, &m); |
3526 | if (r < 0) { | |
d9daae55 | 3527 | *invalid_config = true; |
d02933fd | 3528 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid mode '%s'.", mode); |
3b63d2d3 LP |
3529 | } |
3530 | ||
3f93da98 ZJS |
3531 | i.mode = m; |
3532 | i.mode_set = true; | |
3b63d2d3 | 3533 | } else |
5fb13eb5 | 3534 | i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644; |
3b63d2d3 | 3535 | |
e7b88b7b | 3536 | if (!empty_or_dash(age)) { |
24f3a374 | 3537 | const char *a = age; |
7f7a50dd | 3538 | _cleanup_free_ char *seconds = NULL, *age_by = NULL; |
24f3a374 LP |
3539 | |
3540 | if (*a == '~') { | |
3f93da98 | 3541 | i.keep_first_level = true; |
24f3a374 LP |
3542 | a++; |
3543 | } | |
3544 | ||
7f7a50dd SK |
3545 | /* Format: "age-by:age"; where age-by is "[abcmABCM]+". */ |
3546 | r = split_pair(a, ":", &age_by, &seconds); | |
3547 | if (r == -ENOMEM) | |
3548 | return log_oom(); | |
3549 | if (r < 0 && r != -EINVAL) | |
3550 | return log_error_errno(r, "Failed to parse age-by for '%s': %m", age); | |
3551 | if (r >= 0) { | |
3552 | /* We found a ":", parse the "age-by" part. */ | |
3553 | r = parse_age_by_from_arg(age_by, &i); | |
3554 | if (r == -ENOMEM) | |
3555 | return log_oom(); | |
3556 | if (r < 0) { | |
3557 | *invalid_config = true; | |
3558 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age-by '%s'.", age_by); | |
3559 | } | |
3560 | ||
3561 | /* For parsing the "age" part, after the ":". */ | |
3562 | a = seconds; | |
3563 | } | |
3564 | ||
d02933fd LP |
3565 | r = parse_sec(a, &i.age); |
3566 | if (r < 0) { | |
d9daae55 | 3567 | *invalid_config = true; |
7f7a50dd | 3568 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age '%s'.", a); |
3b63d2d3 LP |
3569 | } |
3570 | ||
3f93da98 | 3571 | i.age_set = true; |
3b63d2d3 LP |
3572 | } |
3573 | ||
3f93da98 | 3574 | h = needs_glob(i.type) ? globs : items; |
bfe95f35 | 3575 | |
ef43a391 | 3576 | existing = ordered_hashmap_get(h, i.path); |
468d726b | 3577 | if (existing) { |
9af74e0f FB |
3578 | if (is_duplicated_item(existing, &i)) { |
3579 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, | |
3580 | "Duplicate line for path \"%s\", ignoring.", i.path); | |
3581 | return 0; | |
ebf4e801 MW |
3582 | } |
3583 | } else { | |
3f93da98 | 3584 | existing = new0(ItemArray, 1); |
07982ed1 LP |
3585 | if (!existing) |
3586 | return log_oom(); | |
3587 | ||
ef43a391 | 3588 | r = ordered_hashmap_put(h, i.path, existing); |
ccd114f0 LP |
3589 | if (r < 0) { |
3590 | free(existing); | |
3f93da98 | 3591 | return log_oom(); |
ccd114f0 | 3592 | } |
bfe95f35 LP |
3593 | } |
3594 | ||
319a4f4b | 3595 | if (!GREEDY_REALLOC(existing->items, existing->n_items + 1)) |
3f93da98 | 3596 | return log_oom(); |
5008d581 | 3597 | |
7ab7529d LP |
3598 | existing->items[existing->n_items++] = i; |
3599 | i = (struct Item) {}; | |
dd449aca ZJS |
3600 | |
3601 | /* Sort item array, to enforce stable ordering of application */ | |
96d10d78 | 3602 | typesafe_qsort(existing->items, existing->n_items, item_compare); |
17493fa5 | 3603 | |
7f2c1f4d | 3604 | return 0; |
5008d581 LP |
3605 | } |
3606 | ||
ceaaeb9b ZJS |
3607 | static int cat_config(char **config_dirs, char **args) { |
3608 | _cleanup_strv_free_ char **files = NULL; | |
3609 | int r; | |
3610 | ||
3611 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL); | |
3612 | if (r < 0) | |
3613 | return r; | |
3614 | ||
3615 | return cat_files(NULL, files, 0); | |
3616 | } | |
3617 | ||
dd04fb32 LP |
3618 | static int exclude_default_prefixes(void) { |
3619 | int r; | |
3620 | ||
3621 | /* Provide an easy way to exclude virtual/memory file systems from what we do here. Useful in | |
3622 | * combination with --root= where we probably don't want to apply stuff to these dirs as they are | |
3623 | * likely over-mounted if the root directory is actually used, and it wouldbe less than ideal to have | |
3624 | * all kinds of files created/adjusted underneath these mount points. */ | |
3625 | ||
3626 | r = strv_extend_strv( | |
3627 | &arg_exclude_prefixes, | |
3628 | STRV_MAKE("/dev", | |
3629 | "/proc", | |
3630 | "/run", | |
3631 | "/sys"), | |
3632 | true); | |
3633 | if (r < 0) | |
3634 | return log_oom(); | |
3635 | ||
3636 | return 0; | |
3637 | } | |
3638 | ||
37ec0fdd LP |
3639 | static int help(void) { |
3640 | _cleanup_free_ char *link = NULL; | |
3641 | int r; | |
3642 | ||
3643 | r = terminal_urlify_man("systemd-tmpfiles", "8", &link); | |
3644 | if (r < 0) | |
3645 | return log_oom(); | |
3646 | ||
3f532a59 LP |
3647 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n" |
3648 | "\n%sCreates, deletes and cleans up volatile and temporary files and directories.%s\n\n" | |
5c795114 | 3649 | " -h --help Show this help\n" |
f2b5ca0e | 3650 | " --user Execute user configuration\n" |
eb9da376 | 3651 | " --version Show package version\n" |
ceaaeb9b | 3652 | " --cat-config Show configuration files\n" |
5c795114 DR |
3653 | " --create Create marked files/directories\n" |
3654 | " --clean Clean up marked directories\n" | |
3655 | " --remove Remove marked files/directories\n" | |
81815651 | 3656 | " --boot Execute actions only safe at boot\n" |
79ca888f ZJS |
3657 | " --prefix=PATH Only apply rules with the specified prefix\n" |
3658 | " --exclude-prefix=PATH Ignore rules with the specified prefix\n" | |
dd04fb32 | 3659 | " -E Ignore rules prefixed with /dev, /proc, /run, /sys\n" |
d9daae55 | 3660 | " --root=PATH Operate on an alternate filesystem root\n" |
63d3d0a5 | 3661 | " --image=PATH Operate on disk image as filesystem root\n" |
a6d8474f | 3662 | " --replace=PATH Treat arguments as replacement for PATH\n" |
dcd5c891 | 3663 | " --no-pager Do not pipe output into a pager\n" |
bc556335 DDM |
3664 | "\nSee the %s for details.\n", |
3665 | program_invocation_short_name, | |
3666 | ansi_highlight(), | |
3667 | ansi_normal(), | |
3668 | link); | |
37ec0fdd LP |
3669 | |
3670 | return 0; | |
3b63d2d3 LP |
3671 | } |
3672 | ||
3673 | static int parse_argv(int argc, char *argv[]) { | |
3674 | ||
3675 | enum { | |
eb9da376 | 3676 | ARG_VERSION = 0x100, |
ceaaeb9b | 3677 | ARG_CAT_CONFIG, |
f2b5ca0e | 3678 | ARG_USER, |
3b63d2d3 LP |
3679 | ARG_CREATE, |
3680 | ARG_CLEAN, | |
fba6e687 | 3681 | ARG_REMOVE, |
81815651 | 3682 | ARG_BOOT, |
5c795114 DR |
3683 | ARG_PREFIX, |
3684 | ARG_EXCLUDE_PREFIX, | |
cf9a4abd | 3685 | ARG_ROOT, |
63d3d0a5 | 3686 | ARG_IMAGE, |
a6d8474f | 3687 | ARG_REPLACE, |
dcd5c891 | 3688 | ARG_NO_PAGER, |
3b63d2d3 LP |
3689 | }; |
3690 | ||
3691 | static const struct option options[] = { | |
5c795114 | 3692 | { "help", no_argument, NULL, 'h' }, |
f2b5ca0e | 3693 | { "user", no_argument, NULL, ARG_USER }, |
eb9da376 | 3694 | { "version", no_argument, NULL, ARG_VERSION }, |
ceaaeb9b | 3695 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, |
5c795114 DR |
3696 | { "create", no_argument, NULL, ARG_CREATE }, |
3697 | { "clean", no_argument, NULL, ARG_CLEAN }, | |
3698 | { "remove", no_argument, NULL, ARG_REMOVE }, | |
81815651 | 3699 | { "boot", no_argument, NULL, ARG_BOOT }, |
5c795114 DR |
3700 | { "prefix", required_argument, NULL, ARG_PREFIX }, |
3701 | { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX }, | |
cf9a4abd | 3702 | { "root", required_argument, NULL, ARG_ROOT }, |
63d3d0a5 | 3703 | { "image", required_argument, NULL, ARG_IMAGE }, |
a6d8474f | 3704 | { "replace", required_argument, NULL, ARG_REPLACE }, |
dcd5c891 | 3705 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
eb9da376 | 3706 | {} |
3b63d2d3 LP |
3707 | }; |
3708 | ||
0f474365 | 3709 | int c, r; |
3b63d2d3 LP |
3710 | |
3711 | assert(argc >= 0); | |
3712 | assert(argv); | |
3713 | ||
dd04fb32 | 3714 | while ((c = getopt_long(argc, argv, "hE", options, NULL)) >= 0) |
3b63d2d3 LP |
3715 | |
3716 | switch (c) { | |
3717 | ||
3718 | case 'h': | |
37ec0fdd | 3719 | return help(); |
eb9da376 LP |
3720 | |
3721 | case ARG_VERSION: | |
3f6fd1ba | 3722 | return version(); |
3b63d2d3 | 3723 | |
ceaaeb9b ZJS |
3724 | case ARG_CAT_CONFIG: |
3725 | arg_cat_config = true; | |
3726 | break; | |
3727 | ||
f2b5ca0e ZJS |
3728 | case ARG_USER: |
3729 | arg_user = true; | |
3730 | break; | |
3731 | ||
3b63d2d3 | 3732 | case ARG_CREATE: |
1a967b6b | 3733 | arg_operation |= OPERATION_CREATE; |
3b63d2d3 LP |
3734 | break; |
3735 | ||
3736 | case ARG_CLEAN: | |
1a967b6b | 3737 | arg_operation |= OPERATION_CLEAN; |
3b63d2d3 LP |
3738 | break; |
3739 | ||
3740 | case ARG_REMOVE: | |
1a967b6b | 3741 | arg_operation |= OPERATION_REMOVE; |
3b63d2d3 LP |
3742 | break; |
3743 | ||
81815651 ZJS |
3744 | case ARG_BOOT: |
3745 | arg_boot = true; | |
c4708f13 ZJS |
3746 | break; |
3747 | ||
fba6e687 | 3748 | case ARG_PREFIX: |
7bc040fa | 3749 | if (strv_push(&arg_include_prefixes, optarg) < 0) |
a2aced4a | 3750 | return log_oom(); |
fba6e687 LP |
3751 | break; |
3752 | ||
5c795114 | 3753 | case ARG_EXCLUDE_PREFIX: |
7bc040fa | 3754 | if (strv_push(&arg_exclude_prefixes, optarg) < 0) |
5c795114 DR |
3755 | return log_oom(); |
3756 | break; | |
3757 | ||
63d3d0a5 | 3758 | case ARG_ROOT: |
614b022c | 3759 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root); |
dd04fb32 LP |
3760 | if (r < 0) |
3761 | return r; | |
dd04fb32 LP |
3762 | break; |
3763 | ||
63d3d0a5 | 3764 | case ARG_IMAGE: |
3537577c ZJS |
3765 | #ifdef STANDALONE |
3766 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3767 | "This systemd-tmpfiles version is compiled without support for --image=."); | |
3768 | #else | |
614b022c | 3769 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image); |
63d3d0a5 LP |
3770 | if (r < 0) |
3771 | return r; | |
3537577c ZJS |
3772 | #endif |
3773 | /* Imply -E here since it makes little sense to create files persistently in the /run mountpoint of a disk image */ | |
63d3d0a5 LP |
3774 | _fallthrough_; |
3775 | ||
3776 | case 'E': | |
3777 | r = exclude_default_prefixes(); | |
0f474365 | 3778 | if (r < 0) |
0f03c2a4 | 3779 | return r; |
63d3d0a5 | 3780 | |
cf9a4abd MM |
3781 | break; |
3782 | ||
a6d8474f ZJS |
3783 | case ARG_REPLACE: |
3784 | if (!path_is_absolute(optarg) || | |
baaa35ad ZJS |
3785 | !endswith(optarg, ".conf")) |
3786 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3787 | "The argument to --replace= must an absolute path to a config file"); | |
a6d8474f ZJS |
3788 | |
3789 | arg_replace = optarg; | |
3790 | break; | |
3791 | ||
dcd5c891 | 3792 | case ARG_NO_PAGER: |
0221d68a | 3793 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
3794 | break; |
3795 | ||
3b63d2d3 LP |
3796 | case '?': |
3797 | return -EINVAL; | |
3798 | ||
3799 | default: | |
04499a70 | 3800 | assert_not_reached(); |
3b63d2d3 | 3801 | } |
3b63d2d3 | 3802 | |
baaa35ad ZJS |
3803 | if (arg_operation == 0 && !arg_cat_config) |
3804 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3805 | "You need to specify at least one of --clean, --create or --remove."); | |
3b63d2d3 | 3806 | |
baaa35ad ZJS |
3807 | if (arg_replace && arg_cat_config) |
3808 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3809 | "Option --replace= is not supported with --cat-config"); | |
ceaaeb9b | 3810 | |
baaa35ad ZJS |
3811 | if (arg_replace && optind >= argc) |
3812 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3813 | "When --replace= is given, some configuration items must be specified"); | |
a6d8474f | 3814 | |
b63aacaa LP |
3815 | if (arg_root && arg_user) |
3816 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3817 | "Combination of --user and --root= is not supported."); | |
3818 | ||
63d3d0a5 LP |
3819 | if (arg_image && arg_root) |
3820 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
3821 | ||
3b63d2d3 LP |
3822 | return 1; |
3823 | } | |
3824 | ||
1d77721f LP |
3825 | static int read_config_file( |
3826 | char **config_dirs, | |
3827 | const char *fn, | |
3828 | bool ignore_enoent, | |
3829 | bool *invalid_config) { | |
3830 | ||
a3451c2c | 3831 | _cleanup_(hashmap_freep) Hashmap *uid_cache = NULL, *gid_cache = NULL; |
f7ac1ed2 | 3832 | _cleanup_fclose_ FILE *_f = NULL; |
2708160c | 3833 | _cleanup_free_ char *pp = NULL; |
1731e34a | 3834 | unsigned v = 0; |
2a98ae4a | 3835 | FILE *f; |
bec890e3 | 3836 | ItemArray *ia; |
4e68ec18 | 3837 | int r = 0; |
fba6e687 LP |
3838 | |
3839 | assert(fn); | |
3840 | ||
f7ac1ed2 | 3841 | if (streq(fn, "-")) { |
28e5e1e9 | 3842 | log_debug("Reading config from stdin%s", special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
f7ac1ed2 ZJS |
3843 | fn = "<stdin>"; |
3844 | f = stdin; | |
3845 | } else { | |
2708160c | 3846 | r = search_and_fopen(fn, "re", arg_root, (const char**) config_dirs, &_f, &pp); |
f7ac1ed2 ZJS |
3847 | if (r < 0) { |
3848 | if (ignore_enoent && r == -ENOENT) { | |
3849 | log_debug_errno(r, "Failed to open \"%s\", ignoring: %m", fn); | |
3850 | return 0; | |
3851 | } | |
fba6e687 | 3852 | |
f7ac1ed2 ZJS |
3853 | return log_error_errno(r, "Failed to open '%s': %m", fn); |
3854 | } | |
2708160c | 3855 | |
28e5e1e9 | 3856 | log_debug("Reading config file \"%s\"%s", pp, special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
2708160c | 3857 | fn = pp; |
f7ac1ed2 | 3858 | f = _f; |
fba6e687 LP |
3859 | } |
3860 | ||
2a98ae4a LP |
3861 | for (;;) { |
3862 | _cleanup_free_ char *line = NULL; | |
3863 | bool invalid_line = false; | |
1731e34a | 3864 | char *l; |
fba6e687 | 3865 | int k; |
2a98ae4a LP |
3866 | |
3867 | k = read_line(f, LONG_LINE_MAX, &line); | |
3868 | if (k < 0) | |
3869 | return log_error_errno(k, "Failed to read '%s': %m", fn); | |
3870 | if (k == 0) | |
3871 | break; | |
fba6e687 | 3872 | |
fba6e687 LP |
3873 | v++; |
3874 | ||
3875 | l = strstrip(line); | |
4c701096 | 3876 | if (IN_SET(*l, 0, '#')) |
fba6e687 LP |
3877 | continue; |
3878 | ||
a3451c2c | 3879 | k = parse_line(fn, v, l, &invalid_line, &uid_cache, &gid_cache); |
d9daae55 ZJS |
3880 | if (k < 0) { |
3881 | if (invalid_line) | |
3882 | /* Allow reporting with a special code if the caller requested this */ | |
3883 | *invalid_config = true; | |
3884 | else if (r == 0) | |
3885 | /* The first error becomes our return value */ | |
3886 | r = k; | |
3887 | } | |
fba6e687 LP |
3888 | } |
3889 | ||
78a92a5a | 3890 | /* we have to determine age parameter for each entry of type X */ |
bec890e3 MM |
3891 | ORDERED_HASHMAP_FOREACH(ia, globs) |
3892 | for (size_t ni = 0; ni < ia->n_items; ni++) { | |
3893 | ItemArray *ja; | |
3894 | Item *i = ia->items + ni, *candidate_item = NULL; | |
78a92a5a | 3895 | |
bec890e3 | 3896 | if (i->type != IGNORE_DIRECTORY_PATH) |
78a92a5a MS |
3897 | continue; |
3898 | ||
bec890e3 MM |
3899 | ORDERED_HASHMAP_FOREACH(ja, items) |
3900 | for (size_t nj = 0; nj < ja->n_items; nj++) { | |
3901 | Item *j = ja->items + nj; | |
78a92a5a | 3902 | |
875e7b25 ZJS |
3903 | if (!IN_SET(j->type, CREATE_DIRECTORY, |
3904 | TRUNCATE_DIRECTORY, | |
3905 | CREATE_SUBVOLUME, | |
3906 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
3907 | CREATE_SUBVOLUME_NEW_QUOTA)) | |
bec890e3 | 3908 | continue; |
78a92a5a | 3909 | |
bec890e3 MM |
3910 | if (path_equal(j->path, i->path)) { |
3911 | candidate_item = j; | |
3912 | break; | |
3913 | } | |
3914 | ||
875e7b25 ZJS |
3915 | if (candidate_item |
3916 | ? (path_startswith(j->path, candidate_item->path) && fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0) | |
3917 | : path_startswith(i->path, j->path) != NULL) | |
bec890e3 MM |
3918 | candidate_item = j; |
3919 | } | |
3920 | ||
3921 | if (candidate_item && candidate_item->age_set) { | |
3922 | i->age = candidate_item->age; | |
3923 | i->age_set = true; | |
3924 | } | |
78a92a5a | 3925 | } |
78a92a5a | 3926 | |
fba6e687 | 3927 | if (ferror(f)) { |
56f64d95 | 3928 | log_error_errno(errno, "Failed to read from file %s: %m", fn); |
fba6e687 LP |
3929 | if (r == 0) |
3930 | r = -EIO; | |
3931 | } | |
3932 | ||
fba6e687 LP |
3933 | return r; |
3934 | } | |
3935 | ||
a6d8474f | 3936 | static int parse_arguments(char **config_dirs, char **args, bool *invalid_config) { |
a6d8474f ZJS |
3937 | int r; |
3938 | ||
3939 | STRV_FOREACH(arg, args) { | |
3940 | r = read_config_file(config_dirs, *arg, false, invalid_config); | |
3941 | if (r < 0) | |
3942 | return r; | |
3943 | } | |
3944 | ||
3945 | return 0; | |
3946 | } | |
3947 | ||
3948 | static int read_config_files(char **config_dirs, char **args, bool *invalid_config) { | |
3949 | _cleanup_strv_free_ char **files = NULL; | |
3950 | _cleanup_free_ char *p = NULL; | |
a6d8474f ZJS |
3951 | int r; |
3952 | ||
ceaaeb9b | 3953 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p); |
a6d8474f | 3954 | if (r < 0) |
ceaaeb9b | 3955 | return r; |
a6d8474f ZJS |
3956 | |
3957 | STRV_FOREACH(f, files) | |
3958 | if (p && path_equal(*f, p)) { | |
28e5e1e9 | 3959 | log_debug("Parsing arguments at position \"%s\"%s", *f, special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
a6d8474f ZJS |
3960 | |
3961 | r = parse_arguments(config_dirs, args, invalid_config); | |
3962 | if (r < 0) | |
3963 | return r; | |
3964 | } else | |
3965 | /* Just warn, ignore result otherwise. | |
3966 | * read_config_file() has some debug output, so no need to print anything. */ | |
3967 | (void) read_config_file(config_dirs, *f, true, invalid_config); | |
3968 | ||
3969 | return 0; | |
3970 | } | |
3971 | ||
1d77721f LP |
3972 | static int read_credential_lines(bool *invalid_config) { |
3973 | _cleanup_free_ char *j = NULL; | |
3974 | const char *d; | |
3975 | int r; | |
3976 | ||
3977 | r = get_credentials_dir(&d); | |
3978 | if (r == -ENXIO) | |
3979 | return 0; | |
3980 | if (r < 0) | |
3981 | return log_error_errno(r, "Failed to get credentials directory: %m"); | |
3982 | ||
3983 | j = path_join(d, "tmpfiles.extra"); | |
3984 | if (!j) | |
3985 | return log_oom(); | |
3986 | ||
3987 | (void) read_config_file(/* config_dirs= */ NULL, j, /* ignore_enoent= */ true, invalid_config); | |
3988 | return 0; | |
3989 | } | |
3990 | ||
811a1587 LP |
3991 | static int link_parent(ItemArray *a) { |
3992 | const char *path; | |
3993 | char *prefix; | |
3994 | int r; | |
3995 | ||
3996 | assert(a); | |
3997 | ||
09f467ac | 3998 | /* Finds the closest "parent" item array for the specified item array. Then registers the specified item array |
811a1587 LP |
3999 | * as child of it, and fills the parent in, linking them both ways. This allows us to later create parents |
4000 | * before their children, and clean up/remove children before their parents. */ | |
4001 | ||
4002 | if (a->n_items <= 0) | |
4003 | return 0; | |
4004 | ||
4005 | path = a->items[0].path; | |
6e9417f5 | 4006 | prefix = newa(char, strlen(path) + 1); |
811a1587 LP |
4007 | PATH_FOREACH_PREFIX(prefix, path) { |
4008 | ItemArray *j; | |
4009 | ||
4010 | j = ordered_hashmap_get(items, prefix); | |
bd0ce244 LP |
4011 | if (!j) |
4012 | j = ordered_hashmap_get(globs, prefix); | |
811a1587 | 4013 | if (j) { |
de7fef4b | 4014 | r = set_ensure_put(&j->children, NULL, a); |
811a1587 LP |
4015 | if (r < 0) |
4016 | return log_oom(); | |
4017 | ||
4018 | a->parent = j; | |
4019 | return 1; | |
4020 | } | |
4021 | } | |
4022 | ||
4023 | return 0; | |
4024 | } | |
4025 | ||
87938c3b YW |
4026 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_array_hash_ops, char, string_hash_func, string_compare_func, |
4027 | ItemArray, item_array_free); | |
4028 | ||
4029 | static int run(int argc, char *argv[]) { | |
3537577c | 4030 | #ifndef STANDALONE |
63d3d0a5 | 4031 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
63d3d0a5 | 4032 | _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL; |
3537577c | 4033 | #endif |
f2b5ca0e | 4034 | _cleanup_strv_free_ char **config_dirs = NULL; |
d9daae55 | 4035 | bool invalid_config = false; |
64adb379 | 4036 | ItemArray *a; |
44ac4f88 LP |
4037 | enum { |
4038 | PHASE_REMOVE_AND_CLEAN, | |
4039 | PHASE_CREATE, | |
4040 | _PHASE_MAX | |
4041 | } phase; | |
87938c3b | 4042 | int r, k; |
3b63d2d3 | 4043 | |
fdcad0c2 LP |
4044 | r = parse_argv(argc, argv); |
4045 | if (r <= 0) | |
87938c3b | 4046 | return r; |
5008d581 | 4047 | |
d2acb93d | 4048 | log_setup(); |
5008d581 | 4049 | |
01131684 LP |
4050 | /* We require /proc/ for a lot of our operations, i.e. for adjusting access modes, for anything |
4051 | * SELinux related, for recursive operation, for xattr, acl and chattr handling, for btrfs stuff and | |
4052 | * a lot more. It's probably the majority of invocations where /proc/ is required. Since people | |
4053 | * apparently invoke it without anyway and are surprised about the failures, let's catch this early | |
4054 | * and output a nice and friendly warning. */ | |
4055 | if (proc_mounted() == 0) | |
4056 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), | |
4057 | "/proc/ is not mounted, but required for successful operation of systemd-tmpfiles. " | |
4058 | "Please mount /proc/. Alternatively, consider using the --root= or --image= switches."); | |
4059 | ||
e5358401 LP |
4060 | /* Descending down file system trees might take a lot of fds */ |
4061 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
4062 | ||
f2b5ca0e ZJS |
4063 | if (arg_user) { |
4064 | r = user_config_paths(&config_dirs); | |
87938c3b YW |
4065 | if (r < 0) |
4066 | return log_error_errno(r, "Failed to initialize configuration directory list: %m"); | |
f2b5ca0e ZJS |
4067 | } else { |
4068 | config_dirs = strv_split_nulstr(CONF_PATHS_NULSTR("tmpfiles.d")); | |
87938c3b YW |
4069 | if (!config_dirs) |
4070 | return log_oom(); | |
f2b5ca0e ZJS |
4071 | } |
4072 | ||
79c91cec | 4073 | if (DEBUG_LOGGING) { |
f2b5ca0e ZJS |
4074 | _cleanup_free_ char *t = NULL; |
4075 | ||
b8e35011 LP |
4076 | STRV_FOREACH(i, config_dirs) { |
4077 | _cleanup_free_ char *j = NULL; | |
4078 | ||
4079 | j = path_join(arg_root, *i); | |
4080 | if (!j) | |
4081 | return log_oom(); | |
4082 | ||
c2bc710b | 4083 | if (!strextend(&t, "\n\t", j)) |
b8e35011 LP |
4084 | return log_oom(); |
4085 | } | |
4086 | ||
4087 | log_debug("Looking for configuration files in (higher priority first):%s", t); | |
f2b5ca0e ZJS |
4088 | } |
4089 | ||
ceaaeb9b | 4090 | if (arg_cat_config) { |
384c2c32 | 4091 | pager_open(arg_pager_flags); |
dcd5c891 | 4092 | |
87938c3b | 4093 | return cat_config(config_dirs, argv + optind); |
ceaaeb9b ZJS |
4094 | } |
4095 | ||
4096 | umask(0022); | |
4097 | ||
a9ba0e32 CG |
4098 | r = mac_selinux_init(); |
4099 | if (r < 0) | |
4100 | return r; | |
ceaaeb9b | 4101 | |
3537577c | 4102 | #ifndef STANDALONE |
63d3d0a5 LP |
4103 | if (arg_image) { |
4104 | assert(!arg_root); | |
4105 | ||
4106 | r = mount_image_privately_interactively( | |
4107 | arg_image, | |
4b5de5dd LP |
4108 | DISSECT_IMAGE_GENERIC_ROOT | |
4109 | DISSECT_IMAGE_REQUIRE_ROOT | | |
4110 | DISSECT_IMAGE_VALIDATE_OS | | |
4111 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
c65f854a LP |
4112 | DISSECT_IMAGE_FSCK | |
4113 | DISSECT_IMAGE_GROWFS, | |
63d3d0a5 | 4114 | &unlink_dir, |
e330f97a | 4115 | &loop_device); |
63d3d0a5 LP |
4116 | if (r < 0) |
4117 | return r; | |
4118 | ||
4119 | arg_root = strdup(unlink_dir); | |
4120 | if (!arg_root) | |
4121 | return log_oom(); | |
4122 | } | |
3537577c ZJS |
4123 | #else |
4124 | assert(!arg_image); | |
4125 | #endif | |
63d3d0a5 | 4126 | |
87938c3b YW |
4127 | items = ordered_hashmap_new(&item_array_hash_ops); |
4128 | globs = ordered_hashmap_new(&item_array_hash_ops); | |
4129 | if (!items || !globs) | |
4130 | return log_oom(); | |
ceaaeb9b | 4131 | |
a6d8474f ZJS |
4132 | /* If command line arguments are specified along with --replace, read all |
4133 | * configuration files and insert the positional arguments at the specified | |
4134 | * place. Otherwise, if command line arguments are specified, execute just | |
4135 | * them, and finally, without --replace= or any positional arguments, just | |
4136 | * read configuration and execute it. | |
4137 | */ | |
4138 | if (arg_replace || optind >= argc) | |
4139 | r = read_config_files(config_dirs, argv + optind, &invalid_config); | |
4140 | else | |
4141 | r = parse_arguments(config_dirs, argv + optind, &invalid_config); | |
4142 | if (r < 0) | |
87938c3b | 4143 | return r; |
5008d581 | 4144 | |
1d77721f LP |
4145 | r = read_credential_lines(&invalid_config); |
4146 | if (r < 0) | |
4147 | return r; | |
4148 | ||
811a1587 | 4149 | /* Let's now link up all child/parent relationships */ |
90e74a66 | 4150 | ORDERED_HASHMAP_FOREACH(a, items) { |
811a1587 LP |
4151 | r = link_parent(a); |
4152 | if (r < 0) | |
87938c3b | 4153 | return r; |
1db50423 | 4154 | } |
90e74a66 | 4155 | ORDERED_HASHMAP_FOREACH(a, globs) { |
811a1587 LP |
4156 | r = link_parent(a); |
4157 | if (r < 0) | |
87938c3b | 4158 | return r; |
811a1587 LP |
4159 | } |
4160 | ||
64adb379 LP |
4161 | /* If multiple operations are requested, let's first run the remove/clean operations, and only then the create |
4162 | * operations. i.e. that we first clean out the platform we then build on. */ | |
44ac4f88 | 4163 | for (phase = 0; phase < _PHASE_MAX; phase++) { |
64adb379 | 4164 | OperationMask op; |
b8bb3e8f | 4165 | |
44ac4f88 | 4166 | if (phase == PHASE_REMOVE_AND_CLEAN) |
64adb379 | 4167 | op = arg_operation & (OPERATION_REMOVE|OPERATION_CLEAN); |
44ac4f88 | 4168 | else if (phase == PHASE_CREATE) |
64adb379 LP |
4169 | op = arg_operation & OPERATION_CREATE; |
4170 | else | |
04499a70 | 4171 | assert_not_reached(); |
64adb379 LP |
4172 | |
4173 | if (op == 0) /* Nothing requested in this phase */ | |
4174 | continue; | |
4175 | ||
4176 | /* The non-globbing ones usually create things, hence we apply them first */ | |
90e74a66 | 4177 | ORDERED_HASHMAP_FOREACH(a, items) { |
64adb379 | 4178 | k = process_item_array(a, op); |
87938c3b YW |
4179 | if (k < 0 && r >= 0) |
4180 | r = k; | |
64adb379 LP |
4181 | } |
4182 | ||
4183 | /* The globbing ones usually alter things, hence we apply them second. */ | |
90e74a66 | 4184 | ORDERED_HASHMAP_FOREACH(a, globs) { |
64adb379 | 4185 | k = process_item_array(a, op); |
87938c3b YW |
4186 | if (k < 0 && r >= 0) |
4187 | r = k; | |
64adb379 | 4188 | } |
1db50423 | 4189 | } |
3b63d2d3 | 4190 | |
95d2155a | 4191 | if (ERRNO_IS_RESOURCE(r)) |
87938c3b YW |
4192 | return r; |
4193 | if (invalid_config) | |
d9daae55 | 4194 | return EX_DATAERR; |
87938c3b | 4195 | if (r < 0) |
bb9947be | 4196 | return EX_CANTCREAT; |
87938c3b | 4197 | return 0; |
5008d581 | 4198 | } |
87938c3b YW |
4199 | |
4200 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |