]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/tmpfiles/tmpfiles.c
tmpfiles: stat file in item_do() rather than in its callers
[thirdparty/systemd.git] / src / tmpfiles / tmpfiles.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
5008d581 2
5008d581 3#include <errno.h>
3f6fd1ba
LP
4#include <fcntl.h>
5#include <fnmatch.h>
6#include <getopt.h>
7#include <glob.h>
5008d581 8#include <limits.h>
3f6fd1ba
LP
9#include <linux/fs.h>
10#include <stdbool.h>
11#include <stddef.h>
3b63d2d3
LP
12#include <stdio.h>
13#include <stdlib.h>
3f6fd1ba 14#include <string.h>
505ef0e3 15#include <sys/stat.h>
ebf4e801 16#include <sys/xattr.h>
d9daae55 17#include <sysexits.h>
3f6fd1ba
LP
18#include <time.h>
19#include <unistd.h>
5008d581 20
5a8575ef
ZJS
21#include "sd-path.h"
22
3f6fd1ba 23#include "acl-util.h"
b5efdb8a 24#include "alloc-util.h"
3f6fd1ba 25#include "btrfs-util.h"
430f0182 26#include "capability-util.h"
c8b3094d 27#include "chattr-util.h"
3f6fd1ba
LP
28#include "conf-files.h"
29#include "copy.h"
a0f29c76 30#include "def.h"
8fb3f009 31#include "dirent-util.h"
4f5dd394 32#include "escape.h"
3ffd4af2 33#include "fd-util.h"
0d39fa9c 34#include "fileio.h"
f97b34a6 35#include "format-util.h"
f4f15635 36#include "fs-util.h"
7d50b32a 37#include "glob-util.h"
c004493c 38#include "io-util.h"
3f6fd1ba 39#include "label.h"
5008d581 40#include "log.h"
54693d9b 41#include "macro.h"
d39efe74 42#include "missing.h"
49e942b2 43#include "mkdir.h"
4e036b7a 44#include "mount-util.h"
dcd5c891 45#include "pager.h"
6bedfcbb 46#include "parse-util.h"
f2b5ca0e 47#include "path-lookup.h"
9eb977db 48#include "path-util.h"
c6878637 49#include "rm-rf.h"
d7b8eec7 50#include "selinux-util.h"
3f6fd1ba
LP
51#include "set.h"
52#include "specifier.h"
8fcde012 53#include "stat-util.h"
15a5e950 54#include "stdio-util.h"
8b43440b 55#include "string-table.h"
07630cea 56#include "string-util.h"
3f6fd1ba 57#include "strv.h"
ceaaeb9b 58#include "terminal-util.h"
affb60b1 59#include "umask-util.h"
b1d4f8e1 60#include "user-util.h"
3f6fd1ba 61#include "util.h"
5008d581 62
01000479 63/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
5008d581 64 * them in the file system. This is intended to be used to create
db019b8d
KS
65 * properly owned directories beneath /tmp, /var/tmp, /run, which are
66 * volatile and hence need to be recreated on bootup. */
5008d581 67
66ccd038 68typedef enum ItemType {
b8bb3e8f 69 /* These ones take file names */
3b63d2d3
LP
70 CREATE_FILE = 'f',
71 TRUNCATE_FILE = 'F',
72 CREATE_DIRECTORY = 'd',
73 TRUNCATE_DIRECTORY = 'D',
d7b8eec7 74 CREATE_SUBVOLUME = 'v',
5fb13eb5
LP
75 CREATE_SUBVOLUME_INHERIT_QUOTA = 'q',
76 CREATE_SUBVOLUME_NEW_QUOTA = 'Q',
ee17ee7c 77 CREATE_FIFO = 'p',
468d726b
LP
78 CREATE_SYMLINK = 'L',
79 CREATE_CHAR_DEVICE = 'c',
80 CREATE_BLOCK_DEVICE = 'b',
849958d1 81 COPY_FILES = 'C',
b8bb3e8f
LP
82
83 /* These ones take globs */
17493fa5 84 WRITE_FILE = 'w',
df8dee85 85 EMPTY_DIRECTORY = 'e',
b705ab6a
ZJS
86 SET_XATTR = 't',
87 RECURSIVE_SET_XATTR = 'T',
88 SET_ACL = 'a',
89 RECURSIVE_SET_ACL = 'A',
17493fa5
LP
90 SET_ATTRIBUTE = 'h',
91 RECURSIVE_SET_ATTRIBUTE = 'H',
3b63d2d3 92 IGNORE_PATH = 'x',
78a92a5a 93 IGNORE_DIRECTORY_PATH = 'X',
3b63d2d3 94 REMOVE_PATH = 'r',
a8d88783 95 RECURSIVE_REMOVE_PATH = 'R',
777b87e7 96 RELABEL_PATH = 'z',
e73a03e0 97 RECURSIVE_RELABEL_PATH = 'Z',
17493fa5 98 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
66ccd038 99} ItemType;
3b63d2d3
LP
100
101typedef struct Item {
66ccd038 102 ItemType type;
3b63d2d3
LP
103
104 char *path;
468d726b 105 char *argument;
ebf4e801 106 char **xattrs;
349cc4a5 107#if HAVE_ACL
f8eeeaf9
ZJS
108 acl_t acl_access;
109 acl_t acl_default;
110#endif
5008d581
LP
111 uid_t uid;
112 gid_t gid;
3b63d2d3
LP
113 mode_t mode;
114 usec_t age;
115
468d726b 116 dev_t major_minor;
88ec4dfa
LP
117 unsigned attribute_value;
118 unsigned attribute_mask;
468d726b 119
3b63d2d3
LP
120 bool uid_set:1;
121 bool gid_set:1;
122 bool mode_set:1;
123 bool age_set:1;
abef3f91 124 bool mask_perms:1;
88ec4dfa 125 bool attribute_set:1;
24f3a374
LP
126
127 bool keep_first_level:1;
1910cd0e 128
2e78fa79
LP
129 bool force:1;
130
1910cd0e 131 bool done:1;
3b63d2d3
LP
132} Item;
133
3f93da98
ZJS
134typedef struct ItemArray {
135 Item *items;
136 size_t count;
137 size_t size;
138} ItemArray;
139
5a8575ef
ZJS
140typedef enum DirectoryType {
141 DIRECTORY_RUNTIME = 0,
142 DIRECTORY_STATE,
143 DIRECTORY_CACHE,
144 DIRECTORY_LOGS,
145 _DIRECTORY_TYPE_MAX,
146} DirectoryType;
147
ceaaeb9b 148static bool arg_cat_config = false;
f2b5ca0e 149static bool arg_user = false;
3b63d2d3
LP
150static bool arg_create = false;
151static bool arg_clean = false;
152static bool arg_remove = false;
81815651 153static bool arg_boot = false;
dcd5c891 154static bool arg_no_pager = false;
3b63d2d3 155
7bc040fa
LP
156static char **arg_include_prefixes = NULL;
157static char **arg_exclude_prefixes = NULL;
cf9a4abd 158static char *arg_root = NULL;
a6d8474f 159static char *arg_replace = NULL;
fba6e687 160
3b63d2d3
LP
161#define MAX_DEPTH 256
162
ef43a391 163static OrderedHashmap *items = NULL, *globs = NULL;
7bc040fa
LP
164static Set *unix_sockets = NULL;
165
4cef1923 166static int specifier_machine_id_safe(char specifier, void *data, void *userdata, char **ret);
5a8575ef 167static int specifier_directory(char specifier, void *data, void *userdata, char **ret);
4cef1923 168
bd550f78 169static const Specifier specifier_table[] = {
4cef1923 170 { 'm', specifier_machine_id_safe, NULL },
5a8575ef
ZJS
171 { 'b', specifier_boot_id, NULL },
172 { 'H', specifier_host_name, NULL },
173 { 'v', specifier_kernel_release, NULL },
174
175 { 'U', specifier_user_id, NULL },
176 { 'u', specifier_user_name, NULL },
177 { 'h', specifier_user_home, NULL },
b294e594 178
5a8575ef
ZJS
179 { 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) },
180 { 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) },
181 { 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) },
182 { 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) },
b294e594
LP
183 { 'T', specifier_tmp_dir, NULL },
184 { 'V', specifier_var_tmp_dir, NULL },
bd550f78
LP
185 {}
186};
187
4cef1923
FB
188static int specifier_machine_id_safe(char specifier, void *data, void *userdata, char **ret) {
189 int r;
190
d8dab757
FB
191 /* If /etc/machine_id is missing or empty (e.g. in a chroot environment)
192 * return a recognizable error so that the caller can skip the rule
4cef1923
FB
193 * gracefully. */
194
195 r = specifier_machine_id(specifier, data, userdata, ret);
d8dab757 196 if (IN_SET(r, -ENOENT, -ENOMEDIUM))
5a8575ef 197 return -ENXIO;
4cef1923
FB
198
199 return r;
200}
201
5a8575ef
ZJS
202static int specifier_directory(char specifier, void *data, void *userdata, char **ret) {
203 struct table_entry {
204 uint64_t type;
205 const char *suffix;
206 };
207
208 static const struct table_entry paths_system[] = {
209 [DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME },
210 [DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE },
211 [DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE },
212 [DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS },
213 };
214
215 static const struct table_entry paths_user[] = {
216 [DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME },
217 [DIRECTORY_STATE] = { SD_PATH_USER_CONFIGURATION },
218 [DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE },
219 [DIRECTORY_LOGS] = { SD_PATH_USER_CONFIGURATION, "log" },
220 };
221
222 unsigned i;
223 const struct table_entry *paths;
224
225 assert_cc(ELEMENTSOF(paths_system) == ELEMENTSOF(paths_user));
226 paths = arg_user ? paths_user : paths_system;
227
228 i = PTR_TO_UINT(data);
229 assert(i < ELEMENTSOF(paths_system));
230
231 return sd_path_home(paths[i].type, paths[i].suffix, ret);
232}
233
4cef1923
FB
234static int log_unresolvable_specifier(const char *filename, unsigned line) {
235 static bool notified = false;
236
5a8575ef
ZJS
237 /* In system mode, this is called when /etc is not fully initialized (e.g.
238 * in a chroot environment) where some specifiers are unresolvable. In user
239 * mode, this is called when some variables are not defined. These cases are
240 * not considered as an error so log at LOG_NOTICE only for the first time
241 * and then downgrade this to LOG_DEBUG for the rest. */
4cef1923
FB
242
243 log_full(notified ? LOG_DEBUG : LOG_NOTICE,
5a8575ef
ZJS
244 "[%s:%u] Failed to resolve specifier: %s, skipping",
245 filename, line,
246 arg_user ? "Required $XDG_... variable not defined" : "uninitialized /etc detected");
4cef1923
FB
247
248 if (!notified)
249 log_notice("All rules containing unresolvable specifiers will be skipped.");
250
251 notified = true;
252 return 0;
253}
254
f2b5ca0e
ZJS
255static int user_config_paths(char*** ret) {
256 _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
257 _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL;
258 _cleanup_strv_free_ char **res = NULL;
259 int r;
260
261 r = xdg_user_dirs(&config_dirs, &data_dirs);
262 if (r < 0)
263 return r;
264
265 r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d");
266 if (r < 0 && r != -ENXIO)
267 return r;
268
269 r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d");
270 if (r < 0 && r != -ENXIO)
271 return r;
272
273 r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d");
274 if (r < 0 && r != -ENXIO)
275 return r;
276
277 r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d");
278 if (r < 0)
279 return r;
280
281 r = strv_extend(&res, persistent_config);
282 if (r < 0)
283 return r;
284
285 r = strv_extend(&res, runtime_config);
286 if (r < 0)
287 return r;
288
289 r = strv_extend(&res, data_home);
290 if (r < 0)
291 return r;
292
293 r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d");
294 if (r < 0)
295 return r;
296
297 r = path_strv_make_absolute_cwd(res);
298 if (r < 0)
299 return r;
300
ae2a15bc 301 *ret = TAKE_PTR(res);
f2b5ca0e
ZJS
302 return 0;
303}
304
66ccd038 305static bool needs_glob(ItemType t) {
cde684a2
LP
306 return IN_SET(t,
307 WRITE_FILE,
308 IGNORE_PATH,
309 IGNORE_DIRECTORY_PATH,
310 REMOVE_PATH,
311 RECURSIVE_REMOVE_PATH,
df8dee85 312 EMPTY_DIRECTORY,
e73a03e0 313 ADJUST_MODE,
cde684a2 314 RELABEL_PATH,
b705ab6a
ZJS
315 RECURSIVE_RELABEL_PATH,
316 SET_XATTR,
317 RECURSIVE_SET_XATTR,
318 SET_ACL,
34f64536
LP
319 RECURSIVE_SET_ACL,
320 SET_ATTRIBUTE,
321 RECURSIVE_SET_ATTRIBUTE);
b8bb3e8f
LP
322}
323
3f93da98
ZJS
324static bool takes_ownership(ItemType t) {
325 return IN_SET(t,
326 CREATE_FILE,
327 TRUNCATE_FILE,
328 CREATE_DIRECTORY,
df8dee85 329 EMPTY_DIRECTORY,
3f93da98
ZJS
330 TRUNCATE_DIRECTORY,
331 CREATE_SUBVOLUME,
5fb13eb5
LP
332 CREATE_SUBVOLUME_INHERIT_QUOTA,
333 CREATE_SUBVOLUME_NEW_QUOTA,
3f93da98
ZJS
334 CREATE_FIFO,
335 CREATE_SYMLINK,
336 CREATE_CHAR_DEVICE,
337 CREATE_BLOCK_DEVICE,
338 COPY_FILES,
3f93da98
ZJS
339 WRITE_FILE,
340 IGNORE_PATH,
341 IGNORE_DIRECTORY_PATH,
342 REMOVE_PATH,
343 RECURSIVE_REMOVE_PATH);
344}
345
ef43a391 346static struct Item* find_glob(OrderedHashmap *h, const char *match) {
3f93da98 347 ItemArray *j;
b8bb3e8f
LP
348 Iterator i;
349
ef43a391 350 ORDERED_HASHMAP_FOREACH(j, h, i) {
3f93da98
ZJS
351 unsigned n;
352
353 for (n = 0; n < j->count; n++) {
354 Item *item = j->items + n;
355
356 if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
357 return item;
358 }
359 }
b8bb3e8f
LP
360
361 return NULL;
362}
363
17b90525 364static void load_unix_sockets(void) {
7fd1b19b 365 _cleanup_fclose_ FILE *f = NULL;
f1ff734f 366 int r;
17b90525
LP
367
368 if (unix_sockets)
369 return;
370
f1ff734f 371 /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */
17b90525 372
548f6937
LP
373 unix_sockets = set_new(&path_hash_ops);
374 if (!unix_sockets) {
375 log_oom();
17b90525 376 return;
548f6937 377 }
17b90525 378
fdcad0c2 379 f = fopen("/proc/net/unix", "re");
f1ff734f
LP
380 if (!f) {
381 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
382 "Failed to open /proc/net/unix, ignoring: %m");
383 goto fail;
384 }
17b90525 385
fdcad0c2 386 /* Skip header */
f1ff734f
LP
387 r = read_line(f, LONG_LINE_MAX, NULL);
388 if (r < 0) {
389 log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m");
390 goto fail;
391 }
392 if (r == 0) {
393 log_warning("Premature end of file reading /proc/net/unix.");
17b90525 394 goto fail;
f1ff734f 395 }
17b90525
LP
396
397 for (;;) {
f1ff734f 398 _cleanup_free_ char *line = NULL;
17b90525 399 char *p, *s;
17b90525 400
f1ff734f
LP
401 r = read_line(f, LONG_LINE_MAX, &line);
402 if (r < 0) {
403 log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m");
404 goto fail;
405 }
406 if (r == 0) /* EOF */
17b90525
LP
407 break;
408
fdcad0c2
LP
409 p = strchr(line, ':');
410 if (!p)
411 continue;
412
413 if (strlen(p) < 37)
17b90525
LP
414 continue;
415
fdcad0c2 416 p += 37;
17b90525 417 p += strspn(p, WHITESPACE);
fdcad0c2 418 p += strcspn(p, WHITESPACE); /* skip one more word */
17b90525
LP
419 p += strspn(p, WHITESPACE);
420
421 if (*p != '/')
422 continue;
423
fdcad0c2 424 s = strdup(p);
f1ff734f
LP
425 if (!s) {
426 log_oom();
17b90525 427 goto fail;
f1ff734f 428 }
17b90525 429
858d36c1 430 path_simplify(s, false);
4ff21d85 431
f1ff734f
LP
432 r = set_consume(unix_sockets, s);
433 if (r < 0 && r != -EEXIST) {
434 log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m");
ef42202a 435 goto fail;
f1ff734f 436 }
17b90525
LP
437 }
438
439 return;
440
441fail:
f1ff734f 442 unix_sockets = set_free_free(unix_sockets);
17b90525
LP
443}
444
445static bool unix_socket_alive(const char *fn) {
446 assert(fn);
447
448 load_unix_sockets();
449
450 if (unix_sockets)
451 return !!set_get(unix_sockets, (char*) fn);
452
453 /* We don't know, so assume yes */
454 return true;
455}
456
99d680ac 457static int dir_is_mount_point(DIR *d, const char *subdir) {
cde684a2 458
99d680ac
KS
459 int mount_id_parent, mount_id;
460 int r_p, r;
461
cbfb8679 462 r_p = name_to_handle_at_loop(dirfd(d), ".", NULL, &mount_id_parent, 0);
99d680ac
KS
463 if (r_p < 0)
464 r_p = -errno;
465
cbfb8679 466 r = name_to_handle_at_loop(dirfd(d), subdir, NULL, &mount_id, 0);
99d680ac
KS
467 if (r < 0)
468 r = -errno;
469
470 /* got no handle; make no assumptions, return error */
471 if (r_p < 0 && r < 0)
472 return r_p;
473
474 /* got both handles; if they differ, it is a mount point */
475 if (r_p >= 0 && r >= 0)
476 return mount_id_parent != mount_id;
477
478 /* got only one handle; assume different mount points if one
479 * of both queries was not supported by the filesystem */
4c701096 480 if (IN_SET(r_p, -ENOSYS, -EOPNOTSUPP) || IN_SET(r, -ENOSYS, -EOPNOTSUPP))
99d680ac
KS
481 return true;
482
483 /* return error */
484 if (r_p < 0)
485 return r_p;
486 return r;
487}
488
df99a9ef
ZJS
489static DIR* xopendirat_nomod(int dirfd, const char *path) {
490 DIR *dir;
491
492 dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME);
1532227a
LP
493 if (dir)
494 return dir;
495
496 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
497 if (errno != EPERM)
498 return NULL;
499
500 dir = xopendirat(dirfd, path, O_NOFOLLOW);
501 if (!dir)
502 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
df99a9ef
ZJS
503
504 return dir;
505}
506
507static DIR* opendir_nomod(const char *path) {
508 return xopendirat_nomod(AT_FDCWD, path);
509}
510
3b63d2d3 511static int dir_cleanup(
78a92a5a 512 Item *i,
3b63d2d3
LP
513 const char *p,
514 DIR *d,
515 const struct stat *ds,
516 usec_t cutoff,
517 dev_t rootdev,
518 bool mountpoint,
24f3a374 519 int maxdepth,
265ffa1e
LP
520 bool keep_this_level) {
521
3b63d2d3
LP
522 struct dirent *dent;
523 struct timespec times[2];
524 bool deleted = false;
3b63d2d3
LP
525 int r = 0;
526
8fb3f009 527 FOREACH_DIRENT_ALL(dent, d, break) {
3b63d2d3
LP
528 struct stat s;
529 usec_t age;
7fd1b19b 530 _cleanup_free_ char *sub_path = NULL;
3b63d2d3 531
49bfc877 532 if (dot_or_dot_dot(dent->d_name))
3b63d2d3 533 continue;
5008d581 534
3b63d2d3 535 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
ca2f4176
KS
536 if (errno == ENOENT)
537 continue;
5008d581 538
ca2f4176 539 /* FUSE, NFS mounts, SELinux might return EACCES */
ecc1709c
LP
540 r = log_full_errno(errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
541 "stat(%s/%s) failed: %m", p, dent->d_name);
3b63d2d3
LP
542 continue;
543 }
544
545 /* Stay on the same filesystem */
582deb84
ZJS
546 if (s.st_dev != rootdev) {
547 log_debug("Ignoring \"%s/%s\": different filesystem.", p, dent->d_name);
3b63d2d3 548 continue;
582deb84 549 }
3b63d2d3 550
99d680ac
KS
551 /* Try to detect bind mounts of the same filesystem instance; they
552 * do not differ in device major/minors. This type of query is not
553 * supported on all kernels or filesystem types though. */
582deb84
ZJS
554 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0) {
555 log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.",
556 p, dent->d_name);
99d680ac 557 continue;
582deb84 558 }
99d680ac 559
605405c6 560 sub_path = strjoin(p, "/", dent->d_name);
cde684a2 561 if (!sub_path) {
0d0f0c50 562 r = log_oom();
3b63d2d3
LP
563 goto finish;
564 }
565
566 /* Is there an item configured for this path? */
ef43a391 567 if (ordered_hashmap_get(items, sub_path)) {
582deb84 568 log_debug("Ignoring \"%s\": a separate entry exists.", sub_path);
3b63d2d3 569 continue;
582deb84 570 }
3b63d2d3 571
582deb84
ZJS
572 if (find_glob(globs, sub_path)) {
573 log_debug("Ignoring \"%s\": a separate glob exists.", sub_path);
b8bb3e8f 574 continue;
582deb84 575 }
b8bb3e8f 576
3b63d2d3
LP
577 if (S_ISDIR(s.st_mode)) {
578
579 if (mountpoint &&
580 streq(dent->d_name, "lost+found") &&
582deb84
ZJS
581 s.st_uid == 0) {
582 log_debug("Ignoring \"%s\".", sub_path);
3b63d2d3 583 continue;
582deb84 584 }
3b63d2d3
LP
585
586 if (maxdepth <= 0)
582deb84 587 log_warning("Reached max depth on \"%s\".", sub_path);
3b63d2d3 588 else {
7fd1b19b 589 _cleanup_closedir_ DIR *sub_dir;
3b63d2d3
LP
590 int q;
591
df99a9ef 592 sub_dir = xopendirat_nomod(dirfd(d), dent->d_name);
cde684a2 593 if (!sub_dir) {
582deb84
ZJS
594 if (errno != ENOENT)
595 r = log_error_errno(errno, "opendir(%s) failed: %m", sub_path);
3b63d2d3
LP
596
597 continue;
598 }
599
78a92a5a 600 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
3b63d2d3
LP
601 if (q < 0)
602 r = q;
603 }
604
24f3a374
LP
605 /* Note: if you are wondering why we don't
606 * support the sticky bit for excluding
607 * directories from cleaning like we do it for
608 * other file system objects: well, the sticky
609 * bit already has a meaning for directories,
610 * so we don't want to overload that. */
611
582deb84
ZJS
612 if (keep_this_level) {
613 log_debug("Keeping \"%s\".", sub_path);
24f3a374 614 continue;
582deb84 615 }
24f3a374 616
3b63d2d3 617 /* Ignore ctime, we change it when deleting */
582deb84
ZJS
618 age = timespec_load(&s.st_mtim);
619 if (age >= cutoff) {
620 char a[FORMAT_TIMESTAMP_MAX];
621 /* Follows spelling in stat(1). */
622 log_debug("Directory \"%s\": modify time %s is too new.",
623 sub_path,
624 format_timestamp_us(a, sizeof(a), age));
3b63d2d3 625 continue;
582deb84
ZJS
626 }
627
628 age = timespec_load(&s.st_atim);
629 if (age >= cutoff) {
630 char a[FORMAT_TIMESTAMP_MAX];
631 log_debug("Directory \"%s\": access time %s is too new.",
632 sub_path,
633 format_timestamp_us(a, sizeof(a), age));
634 continue;
635 }
3b63d2d3 636
61253220
ZJS
637 log_debug("Removing directory \"%s\".", sub_path);
638 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0)
920ce828
LP
639 if (!IN_SET(errno, ENOENT, ENOTEMPTY))
640 r = log_error_errno(errno, "rmdir(%s): %m", sub_path);
3b63d2d3
LP
641
642 } else {
9c73736d
LP
643 /* Skip files for which the sticky bit is
644 * set. These are semantics we define, and are
645 * unknown elsewhere. See XDG_RUNTIME_DIR
646 * specification for details. */
582deb84
ZJS
647 if (s.st_mode & S_ISVTX) {
648 log_debug("Skipping \"%s\": sticky bit set.", sub_path);
9c73736d 649 continue;
582deb84 650 }
9c73736d 651
582deb84 652 if (mountpoint && S_ISREG(s.st_mode))
1542c01b
ZJS
653 if (s.st_uid == 0 && STR_IN_SET(dent->d_name,
654 ".journal",
655 "aquota.user",
656 "aquota.group")) {
582deb84 657 log_debug("Skipping \"%s\".", sub_path);
3b63d2d3 658 continue;
582deb84 659 }
3b63d2d3 660
17b90525 661 /* Ignore sockets that are listed in /proc/net/unix */
582deb84
ZJS
662 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path)) {
663 log_debug("Skipping \"%s\": live socket.", sub_path);
17b90525 664 continue;
582deb84 665 }
17b90525 666
78ab08eb 667 /* Ignore device nodes */
582deb84
ZJS
668 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode)) {
669 log_debug("Skipping \"%s\": a device.", sub_path);
78ab08eb 670 continue;
582deb84 671 }
78ab08eb 672
24f3a374
LP
673 /* Keep files on this level around if this is
674 * requested */
582deb84
ZJS
675 if (keep_this_level) {
676 log_debug("Keeping \"%s\".", sub_path);
24f3a374 677 continue;
582deb84 678 }
24f3a374 679
582deb84
ZJS
680 age = timespec_load(&s.st_mtim);
681 if (age >= cutoff) {
682 char a[FORMAT_TIMESTAMP_MAX];
683 /* Follows spelling in stat(1). */
684 log_debug("File \"%s\": modify time %s is too new.",
685 sub_path,
686 format_timestamp_us(a, sizeof(a), age));
3b63d2d3 687 continue;
582deb84 688 }
3b63d2d3 689
582deb84
ZJS
690 age = timespec_load(&s.st_atim);
691 if (age >= cutoff) {
692 char a[FORMAT_TIMESTAMP_MAX];
693 log_debug("File \"%s\": access time %s is too new.",
694 sub_path,
695 format_timestamp_us(a, sizeof(a), age));
696 continue;
697 }
3b63d2d3 698
582deb84
ZJS
699 age = timespec_load(&s.st_ctim);
700 if (age >= cutoff) {
701 char a[FORMAT_TIMESTAMP_MAX];
702 log_debug("File \"%s\": change time %s is too new.",
703 sub_path,
704 format_timestamp_us(a, sizeof(a), age));
705 continue;
3b63d2d3
LP
706 }
707
582deb84
ZJS
708 log_debug("unlink \"%s\"", sub_path);
709
710 if (unlinkat(dirfd(d), dent->d_name, 0) < 0)
711 if (errno != ENOENT)
712 r = log_error_errno(errno, "unlink(%s): %m", sub_path);
713
3b63d2d3
LP
714 deleted = true;
715 }
716 }
717
718finish:
719 if (deleted) {
582deb84
ZJS
720 usec_t age1, age2;
721 char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX];
722
3b63d2d3
LP
723 /* Restore original directory timestamps */
724 times[0] = ds->st_atim;
725 times[1] = ds->st_mtim;
726
582deb84
ZJS
727 age1 = timespec_load(&ds->st_atim);
728 age2 = timespec_load(&ds->st_mtim);
729 log_debug("Restoring access and modification time on \"%s\": %s, %s",
730 p,
731 format_timestamp_us(a, sizeof(a), age1),
732 format_timestamp_us(b, sizeof(b), age2));
3b63d2d3 733 if (futimens(dirfd(d), times) < 0)
56f64d95 734 log_error_errno(errno, "utimensat(%s): %m", p);
3b63d2d3
LP
735 }
736
3b63d2d3
LP
737 return r;
738}
739
5579f856
LP
740static bool dangerous_hardlinks(void) {
741 _cleanup_free_ char *value = NULL;
742 static int cached = -1;
743 int r;
744
745 /* Check whether the fs.protected_hardlinks sysctl is on. If we can't determine it we assume its off, as that's
746 * what the upstream default is. */
747
748 if (cached >= 0)
749 return cached;
750
751 r = read_one_line_file("/proc/sys/fs/protected_hardlinks", &value);
752 if (r < 0) {
753 log_debug_errno(r, "Failed to read fs.protected_hardlinks sysctl: %m");
754 return true;
755 }
756
757 r = parse_boolean(value);
758 if (r < 0) {
759 log_debug_errno(r, "Failed to parse fs.protected_hardlinks sysctl: %m");
760 return true;
761 }
762
763 cached = r == 0;
764 return cached;
765}
766
774f79b5 767static bool hardlink_vulnerable(const struct stat *st) {
5579f856
LP
768 assert(st);
769
770 return !S_ISDIR(st->st_mode) && st->st_nlink > 1 && dangerous_hardlinks();
771}
772
936f6bdb
FB
773static int fd_set_perms(Item *i, int fd, const struct stat *st) {
774 _cleanup_free_ char *path = NULL;
775 int r;
1924a97d 776
cde684a2 777 assert(i);
936f6bdb 778 assert(fd);
780e2ee1 779
936f6bdb
FB
780 r = fd_get_path(fd, &path);
781 if (r < 0)
780e2ee1 782 return r;
48b8aaa8 783
936f6bdb
FB
784 if (!i->mode_set && !i->uid_set && !i->gid_set)
785 goto shortcut;
1924a97d 786
936f6bdb 787 if (hardlink_vulnerable(st)) {
5579f856
LP
788 log_error("Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", path);
789 return -EPERM;
790 }
791
51207ca1 792 if (i->mode_set) {
936f6bdb 793 if (S_ISLNK(st->st_mode))
51207ca1
LP
794 log_debug("Skipping mode fix for symlink %s.", path);
795 else {
48b8aaa8
LP
796 mode_t m = i->mode;
797
798 if (i->mask_perms) {
936f6bdb 799 if (!(st->st_mode & 0111))
48b8aaa8 800 m &= ~0111;
936f6bdb 801 if (!(st->st_mode & 0222))
7d6884b6 802 m &= ~0222;
936f6bdb 803 if (!(st->st_mode & 0444))
48b8aaa8 804 m &= ~0444;
936f6bdb 805 if (!S_ISDIR(st->st_mode))
48b8aaa8
LP
806 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
807 }
abef3f91 808
936f6bdb
FB
809 if (m == (st->st_mode & 07777))
810 log_debug("\"%s\" has correct mode %o already.", path, st->st_mode);
48b8aaa8 811 else {
51207ca1 812 log_debug("Changing \"%s\" to mode %o.", path, m);
4dfaa528
FB
813 if (fchmod_opath(fd, m) < 0)
814 return log_error_errno(errno, "fchmod() of %s failed: %m", path);
48b8aaa8 815 }
062e01bb 816 }
51207ca1 817 }
062e01bb 818
936f6bdb
FB
819 if ((i->uid_set && i->uid != st->st_uid) ||
820 (i->gid_set && i->gid != st->st_gid)) {
51207ca1
LP
821 log_debug("Changing \"%s\" to owner "UID_FMT":"GID_FMT,
822 path,
823 i->uid_set ? i->uid : UID_INVALID,
824 i->gid_set ? i->gid : GID_INVALID);
825
936f6bdb
FB
826 if (fchownat(fd,
827 "",
828 i->uid_set ? i->uid : UID_INVALID,
829 i->gid_set ? i->gid : GID_INVALID,
830 AT_EMPTY_PATH) < 0)
831 return log_error_errno(errno, "fchownat() of %s failed: %m", path);
582deb84 832 }
062e01bb 833
59793c8f 834shortcut:
08c84981 835 return label_fix(path, 0);
062e01bb
MS
836}
837
936f6bdb
FB
838static int path_set_perms(Item *i, const char *path) {
839 _cleanup_close_ int fd = -1;
840 struct stat st;
841
842 assert(i);
843 assert(path);
844
845 fd = open(path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
846 if (fd < 0) {
847 int level = LOG_ERR, r = -errno;
848
849 /* Option "e" operates only on existing objects. Do not
850 * print errors about non-existent files or directories */
851 if (i->type == EMPTY_DIRECTORY && errno == ENOENT) {
852 level = LOG_DEBUG;
853 r = 0;
854 }
855
856 log_full_errno(level, errno, "Adjusting owner and mode for %s failed: %m", path);
857 return r;
858 }
859
860 if (fstat(fd, &st) < 0)
861 return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
862
d460ba18
FB
863 if (i->type == EMPTY_DIRECTORY && !S_ISDIR(st.st_mode))
864 return log_error_errno(EEXIST, "'%s' already exists and is not a directory. ", path);
865
936f6bdb
FB
866 return fd_set_perms(i, fd, &st);
867}
868
bd550f78 869static int parse_xattrs_from_arg(Item *i) {
ebf4e801
MW
870 const char *p;
871 int r;
872
873 assert(i);
505ef0e3 874 assert(i->argument);
ebf4e801 875
ebf4e801
MW
876 p = i->argument;
877
4034a06d 878 for (;;) {
4cef1923 879 _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL;
4034a06d 880
12ba2c44 881 r = extract_first_word(&p, &xattr, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE);
4034a06d 882 if (r < 0)
bd550f78 883 log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p);
4034a06d
LP
884 if (r <= 0)
885 break;
505ef0e3 886
4cef1923 887 r = split_pair(xattr, "=", &name, &value);
ebf4e801 888 if (r < 0) {
4034a06d 889 log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr);
ebf4e801
MW
890 continue;
891 }
505ef0e3 892
4034a06d 893 if (isempty(name) || isempty(value)) {
bd550f78 894 log_warning("Malformed extended attribute found, ignoring: %s", xattr);
ebf4e801
MW
895 continue;
896 }
505ef0e3 897
4034a06d 898 if (strv_push_pair(&i->xattrs, name, value) < 0)
ebf4e801 899 return log_oom();
505ef0e3 900
4034a06d 901 name = value = NULL;
ebf4e801
MW
902 }
903
4034a06d 904 return 0;
ebf4e801
MW
905}
906
936f6bdb 907static int fd_set_xattrs(Item *i, int fd, const struct stat *st) {
34d26777 908 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
936f6bdb 909 _cleanup_free_ char *path = NULL;
ebf4e801 910 char **name, **value;
936f6bdb 911 int r;
ebf4e801
MW
912
913 assert(i);
936f6bdb
FB
914 assert(fd);
915
916 r = fd_get_path(fd, &path);
917 if (r < 0)
918 return r;
919
920 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
ebf4e801 921
ebf4e801 922 STRV_FOREACH_PAIR(name, value, i->xattrs) {
bd550f78 923 log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path);
936f6bdb 924 if (setxattr(procfs_path, *name, *value, strlen(*value), 0) < 0)
25f027c5
ZJS
925 return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m",
926 *name, *value, path);
ebf4e801
MW
927 }
928 return 0;
929}
930
936f6bdb
FB
931static int path_set_xattrs(Item *i, const char *path) {
932 _cleanup_close_ int fd = -1;
933
934 assert(i);
935 assert(path);
936
937 fd = open(path, O_CLOEXEC|O_NOFOLLOW|O_PATH);
938 if (fd < 0)
939 return log_error_errno(errno, "Cannot open '%s': %m", path);
940
941 return fd_set_xattrs(i, fd, NULL);
942}
943
bd550f78 944static int parse_acls_from_arg(Item *item) {
349cc4a5 945#if HAVE_ACL
f8eeeaf9 946 int r;
f8eeeaf9
ZJS
947
948 assert(item);
949
50d9e46d
ZJS
950 /* If force (= modify) is set, we will not modify the acl
951 * afterwards, so the mask can be added now if necessary. */
bd550f78 952
50d9e46d 953 r = parse_acl(item->argument, &item->acl_access, &item->acl_default, !item->force);
f8eeeaf9 954 if (r < 0)
4034a06d 955 log_warning_errno(r, "Failed to parse ACL \"%s\": %m. Ignoring", item->argument);
f8eeeaf9
ZJS
956#else
957 log_warning_errno(ENOSYS, "ACLs are not supported. Ignoring");
958#endif
959
960 return 0;
961}
962
349cc4a5 963#if HAVE_ACL
48b8aaa8
LP
964static int path_set_acl(const char *path, const char *pretty, acl_type_t type, acl_t acl, bool modify) {
965 _cleanup_(acl_free_charpp) char *t = NULL;
dd4105b0 966 _cleanup_(acl_freep) acl_t dup = NULL;
50d9e46d
ZJS
967 int r;
968
d873e877
HPD
969 /* Returns 0 for success, positive error if already warned,
970 * negative error otherwise. */
971
50d9e46d 972 if (modify) {
dd4105b0 973 r = acls_for_file(path, type, acl, &dup);
50d9e46d
ZJS
974 if (r < 0)
975 return r;
50d9e46d 976
dd4105b0
ZJS
977 r = calc_acl_mask_if_needed(&dup);
978 if (r < 0)
979 return r;
980 } else {
981 dup = acl_dup(acl);
982 if (!dup)
983 return -errno;
984
985 /* the mask was already added earlier if needed */
986 }
987
988 r = add_base_acls_if_needed(&dup, path);
989 if (r < 0)
990 return r;
991
582deb84 992 t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE);
48b8aaa8 993 log_debug("Setting %s ACL %s on %s.",
582deb84 994 type == ACL_TYPE_ACCESS ? "access" : "default",
48b8aaa8 995 strna(t), pretty);
50d9e46d 996
582deb84
ZJS
997 r = acl_set_file(path, type, dup);
998 if (r < 0)
3ea40b78
LP
999 /* Return positive to indicate we already warned */
1000 return -log_error_errno(errno,
1001 "Setting %s ACL \"%s\" on %s failed: %m",
1002 type == ACL_TYPE_ACCESS ? "access" : "default",
48b8aaa8 1003 strna(t), pretty);
d873e877 1004
582deb84 1005 return 0;
50d9e46d 1006}
35888b67 1007#endif
50d9e46d 1008
936f6bdb 1009static int fd_set_acls(Item *item, int fd, const struct stat *st) {
d873e877 1010 int r = 0;
349cc4a5 1011#if HAVE_ACL
34d26777 1012 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
936f6bdb 1013 _cleanup_free_ char *path = NULL;
48b8aaa8 1014
f8eeeaf9 1015 assert(item);
936f6bdb
FB
1016 assert(fd);
1017 assert(st);
48b8aaa8 1018
936f6bdb
FB
1019 r = fd_get_path(fd, &path);
1020 if (r < 0)
1021 return r;
48b8aaa8 1022
936f6bdb 1023 if (hardlink_vulnerable(st)) {
5579f856
LP
1024 log_error("Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", path);
1025 return -EPERM;
1026 }
1027
936f6bdb 1028 if (S_ISLNK(st->st_mode)) {
48b8aaa8
LP
1029 log_debug("Skipping ACL fix for symlink %s.", path);
1030 return 0;
1031 }
1032
936f6bdb 1033 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
48b8aaa8 1034
d873e877 1035 if (item->acl_access)
936f6bdb 1036 r = path_set_acl(procfs_path, path, ACL_TYPE_ACCESS, item->acl_access, item->force);
f8eeeaf9 1037
2667385b
RM
1038 /* set only default acls to folders */
1039 if (r == 0 && item->acl_default && S_ISDIR(st->st_mode))
936f6bdb 1040 r = path_set_acl(procfs_path, path, ACL_TYPE_DEFAULT, item->acl_default, item->force);
f8eeeaf9 1041
d873e877
HPD
1042 if (r > 0)
1043 return -r; /* already warned */
936f6bdb 1044 if (r == -EOPNOTSUPP) {
d873e877
HPD
1045 log_debug_errno(r, "ACLs not supported by file system at %s", path);
1046 return 0;
936f6bdb
FB
1047 }
1048 if (r < 0)
1049 return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path);
d873e877
HPD
1050#endif
1051 return r;
f8eeeaf9
ZJS
1052}
1053
936f6bdb
FB
1054static int path_set_acls(Item *item, const char *path) {
1055 int r = 0;
545cdb90 1056#if HAVE_ACL
936f6bdb
FB
1057 _cleanup_close_ int fd = -1;
1058 struct stat st;
1059
1060 assert(item);
1061 assert(path);
1062
1063 fd = open(path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
1064 if (fd < 0)
1065 return log_error_errno(errno, "Adjusting ACL of %s failed: %m", path);
1066
1067 if (fstat(fd, &st) < 0)
1068 return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
1069
1070 r = fd_set_acls(item, fd, &st);
545cdb90
YW
1071#endif
1072 return r;
1073}
936f6bdb 1074
88ec4dfa
LP
1075#define ATTRIBUTES_ALL \
1076 (FS_NOATIME_FL | \
1077 FS_SYNC_FL | \
1078 FS_DIRSYNC_FL | \
1079 FS_APPEND_FL | \
1080 FS_COMPR_FL | \
1081 FS_NODUMP_FL | \
1082 FS_EXTENT_FL | \
1083 FS_IMMUTABLE_FL | \
1084 FS_JOURNAL_DATA_FL | \
1085 FS_SECRM_FL | \
1086 FS_UNRM_FL | \
1087 FS_NOTAIL_FL | \
1088 FS_TOPDIR_FL | \
1089 FS_NOCOW_FL)
1090
bd550f78 1091static int parse_attribute_from_arg(Item *item) {
88ec4dfa
LP
1092
1093 static const struct {
1094 char character;
1095 unsigned value;
1096 } attributes[] = {
1097 { 'A', FS_NOATIME_FL }, /* do not update atime */
1098 { 'S', FS_SYNC_FL }, /* Synchronous updates */
1099 { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */
1100 { 'a', FS_APPEND_FL }, /* writes to file may only append */
1101 { 'c', FS_COMPR_FL }, /* Compress file */
1102 { 'd', FS_NODUMP_FL }, /* do not dump file */
8c35b2ca 1103 { 'e', FS_EXTENT_FL }, /* Extents */
88ec4dfa
LP
1104 { 'i', FS_IMMUTABLE_FL }, /* Immutable file */
1105 { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */
1106 { 's', FS_SECRM_FL }, /* Secure deletion */
1107 { 'u', FS_UNRM_FL }, /* Undelete */
1108 { 't', FS_NOTAIL_FL }, /* file tail should not be merged */
13e785f7 1109 { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */
88ec4dfa 1110 { 'C', FS_NOCOW_FL }, /* Do not cow file */
22c3a6ca 1111 };
88ec4dfa 1112
22c3a6ca
GB
1113 enum {
1114 MODE_ADD,
1115 MODE_DEL,
1116 MODE_SET
1117 } mode = MODE_ADD;
22c3a6ca 1118
88ec4dfa
LP
1119 unsigned value = 0, mask = 0;
1120 const char *p;
22c3a6ca 1121
88ec4dfa
LP
1122 assert(item);
1123
1124 p = item->argument;
1125 if (p) {
1126 if (*p == '+') {
1127 mode = MODE_ADD;
1128 p++;
1129 } else if (*p == '-') {
1130 mode = MODE_DEL;
1131 p++;
1132 } else if (*p == '=') {
1133 mode = MODE_SET;
1134 p++;
1135 }
22c3a6ca
GB
1136 }
1137
88ec4dfa
LP
1138 if (isempty(p) && mode != MODE_SET) {
1139 log_error("Setting file attribute on '%s' needs an attribute specification.", item->path);
22c3a6ca
GB
1140 return -EINVAL;
1141 }
88ec4dfa
LP
1142
1143 for (; p && *p ; p++) {
1144 unsigned i, v;
1145
1146 for (i = 0; i < ELEMENTSOF(attributes); i++)
1147 if (*p == attributes[i].character)
1148 break;
1149
1150 if (i >= ELEMENTSOF(attributes)) {
1151 log_error("Unknown file attribute '%c' on '%s'.", *p, item->path);
22c3a6ca
GB
1152 return -EINVAL;
1153 }
88ec4dfa
LP
1154
1155 v = attributes[i].value;
1156
3742095b 1157 SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET));
88ec4dfa
LP
1158
1159 mask |= v;
22c3a6ca
GB
1160 }
1161
1162 if (mode == MODE_SET)
88ec4dfa 1163 mask |= ATTRIBUTES_ALL;
22c3a6ca 1164
88ec4dfa 1165 assert(mask != 0);
22c3a6ca 1166
88ec4dfa
LP
1167 item->attribute_mask = mask;
1168 item->attribute_value = value;
1169 item->attribute_set = true;
22c3a6ca
GB
1170
1171 return 0;
22c3a6ca
GB
1172}
1173
936f6bdb 1174static int fd_set_attribute(Item *item, int fd, const struct stat *st) {
936f6bdb
FB
1175 _cleanup_close_ int procfs_fd = -1;
1176 _cleanup_free_ char *path = NULL;
88ec4dfa
LP
1177 unsigned f;
1178 int r;
22c3a6ca 1179
88ec4dfa 1180 if (!item->attribute_set || item->attribute_mask == 0)
22c3a6ca 1181 return 0;
22c3a6ca 1182
936f6bdb
FB
1183 r = fd_get_path(fd, &path);
1184 if (r < 0)
1185 return r;
88ec4dfa
LP
1186
1187 /* Issuing the file attribute ioctls on device nodes is not
1188 * safe, as that will be delivered to the drivers, not the
1189 * file system containing the device node. */
936f6bdb 1190 if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) {
88ec4dfa
LP
1191 log_error("Setting file flags is only supported on regular files and directories, cannot set on '%s'.", path);
1192 return -EINVAL;
1193 }
1194
1195 f = item->attribute_value & item->attribute_mask;
1196
1197 /* Mask away directory-specific flags */
936f6bdb 1198 if (!S_ISDIR(st->st_mode))
22c3a6ca 1199 f &= ~FS_DIRSYNC_FL;
88ec4dfa 1200
f2324783 1201 procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME);
936f6bdb
FB
1202 if (procfs_fd < 0)
1203 return -errno;
1204
1205 r = chattr_fd(procfs_fd, f, item->attribute_mask);
22c3a6ca 1206 if (r < 0)
4c701096 1207 log_full_errno(IN_SET(r, -ENOTTY, -EOPNOTSUPP) ? LOG_DEBUG : LOG_WARNING,
ad75a97f
LP
1208 r,
1209 "Cannot set file attribute for '%s', value=0x%08x, mask=0x%08x: %m",
1210 path, item->attribute_value, item->attribute_mask);
22c3a6ca
GB
1211
1212 return 0;
1213}
1214
936f6bdb
FB
1215static int path_set_attribute(Item *item, const char *path) {
1216 _cleanup_close_ int fd = -1;
1217 struct stat st;
1218
1219 if (!item->attribute_set || item->attribute_mask == 0)
1220 return 0;
1221
1222 fd = open(path, O_CLOEXEC|O_NOFOLLOW|O_PATH);
1223 if (fd < 0)
1224 return log_error_errno(errno, "Cannot open '%s': %m", path);
1225
1226 if (fstat(fd, &st) < 0)
1227 return log_error_errno(errno, "Cannot stat '%s': %m", path);
1228
1229 return fd_set_attribute(item, fd, &st);
1230}
1231
d4e9eb91 1232static int write_one_file(Item *i, const char *path) {
43ad6e31
LP
1233 _cleanup_close_ int fd = -1;
1234 int flags, r = 0;
d4e9eb91 1235 struct stat st;
d4e9eb91 1236
874f1947
LP
1237 assert(i);
1238 assert(path);
1239
49e87292 1240 flags = i->type == CREATE_FILE ? O_CREAT|O_EXCL|O_NOFOLLOW :
43ad6e31 1241 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC|O_NOFOLLOW : 0;
d4e9eb91 1242
43ad6e31 1243 RUN_WITH_UMASK(0000) {
ecabcf8b 1244 mac_selinux_create_file_prepare(path, S_IFREG);
669fc4e5 1245 fd = open(path, flags|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
ecabcf8b 1246 mac_selinux_create_file_clear();
5c0d398d 1247 }
d4e9eb91
DR
1248
1249 if (fd < 0) {
582deb84 1250 if (i->type == WRITE_FILE && errno == ENOENT) {
49e87292 1251 log_debug_errno(errno, "Not writing missing file \"%s\": %m", path);
d4e9eb91 1252 return 0;
582deb84 1253 }
49e87292
LP
1254 if (i->type == CREATE_FILE && errno == EEXIST) {
1255 log_debug_errno(errno, "Not writing to pre-existing file \"%s\": %m", path);
1256 goto done;
1257 }
d4e9eb91 1258
f44b28fd
MO
1259 r = -errno;
1260 if (!i->argument && errno == EROFS && stat(path, &st) == 0 &&
1261 (i->type == CREATE_FILE || st.st_size == 0))
1262 goto check_mode;
1263
1264 return log_error_errno(r, "Failed to create file %s: %m", path);
d4e9eb91
DR
1265 }
1266
1267 if (i->argument) {
4034a06d 1268 log_debug("%s to \"%s\".", i->type == CREATE_FILE ? "Appending" : "Writing", path);
d4e9eb91 1269
4cef1923 1270 r = loop_write(fd, i->argument, strlen(i->argument), false);
582deb84
ZJS
1271 if (r < 0)
1272 return log_error_errno(r, "Failed to write file \"%s\": %m", path);
1273 } else
1274 log_debug("\"%s\" has been created.", path);
d4e9eb91 1275
43ad6e31 1276 fd = safe_close(fd);
3612fbc1 1277
bb9947be 1278 done:
4a62c710
MS
1279 if (stat(path, &st) < 0)
1280 return log_error_errno(errno, "stat(%s) failed: %m", path);
d4e9eb91 1281
f44b28fd 1282 check_mode:
d4e9eb91
DR
1283 if (!S_ISREG(st.st_mode)) {
1284 log_error("%s is not a file.", path);
1285 return -EEXIST;
1286 }
1287
b705ab6a 1288 r = path_set_perms(i, path);
d4e9eb91
DR
1289 if (r < 0)
1290 return r;
1291
1292 return 0;
1293}
1294
081043cf 1295typedef int (*action_t)(Item *, const char *);
936f6bdb 1296typedef int (*fdaction_t)(Item *, int fd, const struct stat *st);
081043cf 1297
14f3480a
FB
1298static int item_do(Item *i, int fd, fdaction_t action) {
1299 struct stat st;
936f6bdb 1300 int r = 0, q;
e73a03e0
LP
1301
1302 assert(i);
936f6bdb 1303 assert(fd >= 0);
14f3480a
FB
1304
1305 if (fstat(fd, &st) < 0) {
1306 r = -errno;
1307 goto finish;
1308 }
a8d88783
MS
1309
1310 /* This returns the first error we run into, but nevertheless
1311 * tries to go on */
14f3480a 1312 r = action(i, fd, &st);
a8d88783 1313
14f3480a 1314 if (S_ISDIR(st.st_mode)) {
22dd8d35 1315 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
936f6bdb
FB
1316 _cleanup_closedir_ DIR *d = NULL;
1317 struct dirent *de;
a8d88783 1318
936f6bdb
FB
1319 /* The passed 'fd' was opened with O_PATH. We need to convert
1320 * it into a 'regular' fd before reading the directory content. */
1321 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
a8d88783 1322
936f6bdb
FB
1323 d = opendir(procfs_path);
1324 if (!d) {
1325 r = r ?: -errno;
1326 goto finish;
1327 }
a8d88783 1328
936f6bdb 1329 FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) {
936f6bdb 1330 int de_fd;
a8d88783 1331
936f6bdb
FB
1332 if (dot_or_dot_dot(de->d_name))
1333 continue;
1334
1335 de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH);
14f3480a 1336 if (de_fd >= 0)
936f6bdb 1337 /* pass ownership of dirent fd over */
14f3480a 1338 q = item_do(i, de_fd, action);
936f6bdb
FB
1339 else
1340 q = -errno;
a8d88783 1341
e73a03e0
LP
1342 if (q < 0 && r == 0)
1343 r = q;
a8d88783 1344 }
a8d88783 1345 }
936f6bdb
FB
1346finish:
1347 safe_close(fd);
e73a03e0 1348 return r;
a8d88783
MS
1349}
1350
936f6bdb 1351static int glob_item(Item *i, action_t action) {
df99a9ef 1352 _cleanup_globfree_ glob_t g = {
ebf31a1f 1353 .gl_opendir = (void *(*)(const char *)) opendir_nomod,
df99a9ef 1354 };
e73a03e0 1355 int r = 0, k;
99e68c0b
MS
1356 char **fn;
1357
84e72b5e
ZJS
1358 k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
1359 if (k < 0 && k != -ENOENT)
1360 return log_error_errno(k, "glob(%s) failed: %m", i->path);
99e68c0b 1361
c84a9488
ZJS
1362 STRV_FOREACH(fn, g.gl_pathv) {
1363 k = action(i, *fn);
e73a03e0 1364 if (k < 0 && r == 0)
99e68c0b 1365 r = k;
936f6bdb
FB
1366 }
1367
1368 return r;
1369}
081043cf 1370
936f6bdb
FB
1371static int glob_item_recursively(Item *i, fdaction_t action) {
1372 _cleanup_globfree_ glob_t g = {
1373 .gl_opendir = (void *(*)(const char *)) opendir_nomod,
1374 };
1375 int r = 0, k;
1376 char **fn;
1377
1378 k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
1379 if (k < 0 && k != -ENOENT)
1380 return log_error_errno(k, "glob(%s) failed: %m", i->path);
1381
1382 STRV_FOREACH(fn, g.gl_pathv) {
1383 _cleanup_close_ int fd = -1;
936f6bdb
FB
1384
1385 /* Make sure we won't trigger/follow file object (such as
1386 * device nodes, automounts, ...) pointed out by 'fn' with
1387 * O_PATH. Note, when O_PATH is used, flags other than
1388 * O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW are ignored. */
1389
1390 fd = open(*fn, O_CLOEXEC|O_NOFOLLOW|O_PATH);
1391 if (fd < 0) {
1392 r = r ?: -errno;
1393 continue;
1394 }
1395
14f3480a 1396 k = item_do(i, fd, action);
936f6bdb
FB
1397 if (k < 0 && r == 0)
1398 r = k;
1399
1400 /* we passed fd ownership to the previous call */
1401 fd = -1;
c84a9488 1402 }
99e68c0b 1403
99e68c0b
MS
1404 return r;
1405}
1406
294929f8
ZJS
1407typedef enum {
1408 CREATION_NORMAL,
1409 CREATION_EXISTING,
1410 CREATION_FORCE,
7a7d5db7
LP
1411 _CREATION_MODE_MAX,
1412 _CREATION_MODE_INVALID = -1
294929f8
ZJS
1413} CreationMode;
1414
7a7d5db7
LP
1415static const char *creation_mode_verb_table[_CREATION_MODE_MAX] = {
1416 [CREATION_NORMAL] = "Created",
1417 [CREATION_EXISTING] = "Found existing",
1418 [CREATION_FORCE] = "Created replacement",
1419};
1420
1421DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode);
294929f8 1422
3b63d2d3 1423static int create_item(Item *i) {
3b63d2d3 1424 struct stat st;
df28bc08 1425 int r = 0;
114a17d2 1426 int q = 0;
294929f8 1427 CreationMode creation;
5008d581 1428
3b63d2d3 1429 assert(i);
5008d581 1430
582deb84
ZJS
1431 log_debug("Running create action for entry %c %s", (char) i->type, i->path);
1432
3b63d2d3
LP
1433 switch (i->type) {
1434
1435 case IGNORE_PATH:
78a92a5a 1436 case IGNORE_DIRECTORY_PATH:
3b63d2d3
LP
1437 case REMOVE_PATH:
1438 case RECURSIVE_REMOVE_PATH:
1439 return 0;
5008d581 1440
3b63d2d3 1441 case CREATE_FILE:
31ed59c5 1442 case TRUNCATE_FILE:
7fa10748
LP
1443 RUN_WITH_UMASK(0000)
1444 (void) mkdir_parents_label(i->path, 0755);
1445
1845fdd9
DR
1446 r = write_one_file(i, i->path);
1447 if (r < 0)
1448 return r;
1449 break;
265ffa1e 1450
7b887f22 1451 case COPY_FILES:
7fa10748
LP
1452 RUN_WITH_UMASK(0000)
1453 (void) mkdir_parents_label(i->path, 0755);
1454
4cef1923 1455 log_debug("Copying tree \"%s\" to \"%s\".", i->argument, i->path);
7fa10748
LP
1456 r = copy_tree(i->argument, i->path,
1457 i->uid_set ? i->uid : UID_INVALID,
1458 i->gid_set ? i->gid : GID_INVALID,
1459 COPY_REFLINK);
f44b28fd
MO
1460
1461 if (r == -EROFS && stat(i->path, &st) == 0)
1462 r = -EEXIST;
1463
849958d1 1464 if (r < 0) {
e156347e
LP
1465 struct stat a, b;
1466
8d3d7072
MS
1467 if (r != -EEXIST)
1468 return log_error_errno(r, "Failed to copy files to %s: %m", i->path);
e156347e 1469
4cef1923
FB
1470 if (stat(i->argument, &a) < 0)
1471 return log_error_errno(errno, "stat(%s) failed: %m", i->argument);
e156347e 1472
4a62c710
MS
1473 if (stat(i->path, &b) < 0)
1474 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
e156347e
LP
1475
1476 if ((a.st_mode ^ b.st_mode) & S_IFMT) {
1477 log_debug("Can't copy to %s, file exists already and is of different type", i->path);
1478 return 0;
1479 }
849958d1
LP
1480 }
1481
b705ab6a 1482 r = path_set_perms(i, i->path);
849958d1
LP
1483 if (r < 0)
1484 return r;
1485
1486 break;
1487
d4e9eb91 1488 case WRITE_FILE:
936f6bdb 1489 r = glob_item(i, write_one_file);
f05bc3f7
MS
1490 if (r < 0)
1491 return r;
5008d581 1492
3b63d2d3
LP
1493 break;
1494
3b63d2d3 1495 case CREATE_DIRECTORY:
d7b8eec7
LP
1496 case TRUNCATE_DIRECTORY:
1497 case CREATE_SUBVOLUME:
5fb13eb5
LP
1498 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1499 case CREATE_SUBVOLUME_NEW_QUOTA:
d7b8eec7 1500 RUN_WITH_UMASK(0000)
7fa10748 1501 (void) mkdir_parents_label(i->path, 0755);
d7b8eec7 1502
5fb13eb5 1503 if (IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA)) {
2904e949 1504
945403e6 1505 if (btrfs_is_subvol(empty_to_root(arg_root)) <= 0)
2904e949
LP
1506
1507 /* Don't create a subvolume unless the
1508 * root directory is one, too. We do
1509 * this under the assumption that if
1510 * the root directory is just a plain
1511 * directory (i.e. very light-weight),
1512 * we shouldn't try to split it up
1513 * into subvolumes (i.e. more
1514 * heavy-weight). Thus, chroot()
1515 * environments and suchlike will get
1516 * a full brtfs subvolume set up below
1517 * their tree only if they
1518 * specifically set up a btrfs
1519 * subvolume for the root dir too. */
1520
1521 r = -ENOTTY;
1522 else {
1523 RUN_WITH_UMASK((~i->mode) & 0777)
1524 r = btrfs_subvol_make(i->path);
1525 }
5fb13eb5 1526 } else
d7b8eec7
LP
1527 r = 0;
1528
582deb84 1529 if (IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY) || r == -ENOTTY)
d7b8eec7
LP
1530 RUN_WITH_UMASK(0000)
1531 r = mkdir_label(i->path, i->mode);
5008d581 1532
e156347e 1533 if (r < 0) {
7b135a73 1534 int k;
5008d581 1535
4c701096 1536 if (!IN_SET(r, -EEXIST, -EROFS))
7b135a73 1537 return log_error_errno(r, "Failed to create directory or subvolume \"%s\": %m", i->path);
5008d581 1538
7b135a73
LP
1539 k = is_dir(i->path, false);
1540 if (k == -ENOENT && r == -EROFS)
1541 return log_error_errno(r, "%s does not exist and cannot be created as the file system is read-only.", i->path);
1542 if (k < 0)
1543 return log_error_errno(k, "Failed to check if %s exists: %m", i->path);
1544 if (!k) {
1545 log_warning("\"%s\" already exists and is not a directory.", i->path);
e156347e
LP
1546 return 0;
1547 }
294929f8
ZJS
1548
1549 creation = CREATION_EXISTING;
1550 } else
1551 creation = CREATION_NORMAL;
7b135a73 1552
7a7d5db7 1553 log_debug("%s directory \"%s\".", creation_mode_verb_to_string(creation), i->path);
5008d581 1554
5fb13eb5
LP
1555 if (IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) {
1556 r = btrfs_subvol_auto_qgroup(i->path, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA);
114a17d2 1557 if (r == -ENOTTY)
022ffe4c 1558 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (unsupported fs or dir not a subvolume): %m", i->path);
114a17d2 1559 else if (r == -EROFS)
022ffe4c 1560 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (fs is read-only).", i->path);
114a17d2 1561 else if (r == -ENOPROTOOPT)
022ffe4c 1562 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (quota support is disabled).", i->path);
114a17d2
EV
1563 else if (r < 0)
1564 q = log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path);
1565 else if (r > 0)
5fb13eb5 1566 log_debug("Adjusted quota for subvolume \"%s\".", i->path);
114a17d2 1567 else if (r == 0)
5fb13eb5
LP
1568 log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path);
1569 }
1570
4831981d 1571 _fallthrough_;
df8dee85 1572 case EMPTY_DIRECTORY:
939ca213 1573 r = glob_item(i, path_set_perms);
114a17d2
EV
1574 if (q < 0)
1575 return q;
f05bc3f7
MS
1576 if (r < 0)
1577 return r;
3b63d2d3
LP
1578
1579 break;
ee17ee7c
LP
1580
1581 case CREATE_FIFO:
5c0d398d 1582 RUN_WITH_UMASK(0000) {
7fa10748
LP
1583 (void) mkdir_parents_label(i->path, 0755);
1584
ecabcf8b 1585 mac_selinux_create_file_prepare(i->path, S_IFIFO);
5c0d398d 1586 r = mkfifo(i->path, i->mode);
ecabcf8b 1587 mac_selinux_create_file_clear();
5c0d398d 1588 }
ee17ee7c 1589
1554afae 1590 if (r < 0) {
4a62c710
MS
1591 if (errno != EEXIST)
1592 return log_error_errno(errno, "Failed to create fifo %s: %m", i->path);
ee17ee7c 1593
a542c4dc 1594 if (lstat(i->path, &st) < 0)
4a62c710 1595 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
ee17ee7c 1596
1554afae
LP
1597 if (!S_ISFIFO(st.st_mode)) {
1598
1599 if (i->force) {
1554afae 1600 RUN_WITH_UMASK(0000) {
ecabcf8b 1601 mac_selinux_create_file_prepare(i->path, S_IFIFO);
1554afae 1602 r = mkfifo_atomic(i->path, i->mode);
ecabcf8b 1603 mac_selinux_create_file_clear();
1554afae
LP
1604 }
1605
f647962d
MS
1606 if (r < 0)
1607 return log_error_errno(r, "Failed to create fifo %s: %m", i->path);
294929f8 1608 creation = CREATION_FORCE;
1554afae 1609 } else {
a542c4dc 1610 log_warning("\"%s\" already exists and is not a fifo.", i->path);
1554afae
LP
1611 return 0;
1612 }
294929f8
ZJS
1613 } else
1614 creation = CREATION_EXISTING;
1615 } else
1616 creation = CREATION_NORMAL;
7a7d5db7 1617 log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), i->path);
ee17ee7c 1618
b705ab6a 1619 r = path_set_perms(i, i->path);
f05bc3f7
MS
1620 if (r < 0)
1621 return r;
ee17ee7c
LP
1622
1623 break;
a8d88783 1624
5c5ccf12 1625 case CREATE_SYMLINK: {
7fa10748
LP
1626 RUN_WITH_UMASK(0000)
1627 (void) mkdir_parents_label(i->path, 0755);
1628
ecabcf8b 1629 mac_selinux_create_file_prepare(i->path, S_IFLNK);
4cef1923 1630 r = symlink(i->argument, i->path);
ecabcf8b 1631 mac_selinux_create_file_clear();
e9a5ef7c 1632
468d726b 1633 if (r < 0) {
2e78fa79 1634 _cleanup_free_ char *x = NULL;
468d726b 1635
4a62c710 1636 if (errno != EEXIST)
4cef1923 1637 return log_error_errno(errno, "symlink(%s, %s) failed: %m", i->argument, i->path);
2e78fa79
LP
1638
1639 r = readlink_malloc(i->path, &x);
4cef1923 1640 if (r < 0 || !streq(i->argument, x)) {
2e78fa79
LP
1641
1642 if (i->force) {
ecabcf8b 1643 mac_selinux_create_file_prepare(i->path, S_IFLNK);
4cef1923 1644 r = symlink_atomic(i->argument, i->path);
ecabcf8b 1645 mac_selinux_create_file_clear();
2e78fa79 1646
2ef5de1b 1647 if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) {
b3f5897f
WD
1648 r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL);
1649 if (r < 0)
1650 return log_error_errno(r, "rm -fr %s failed: %m", i->path);
1651
1652 mac_selinux_create_file_prepare(i->path, S_IFLNK);
4cef1923 1653 r = symlink(i->argument, i->path) < 0 ? -errno : 0;
b3f5897f
WD
1654 mac_selinux_create_file_clear();
1655 }
f647962d 1656 if (r < 0)
4cef1923 1657 return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path);
a542c4dc 1658
294929f8 1659 creation = CREATION_FORCE;
1554afae 1660 } else {
582deb84 1661 log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path);
1554afae
LP
1662 return 0;
1663 }
294929f8
ZJS
1664 } else
1665 creation = CREATION_EXISTING;
1666 } else
a542c4dc 1667
294929f8 1668 creation = CREATION_NORMAL;
7a7d5db7 1669 log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path);
468d726b 1670 break;
5c5ccf12 1671 }
468d726b
LP
1672
1673 case CREATE_BLOCK_DEVICE:
1674 case CREATE_CHAR_DEVICE: {
cb7ed9df
LP
1675 mode_t file_type;
1676
1677 if (have_effective_cap(CAP_MKNOD) == 0) {
1678 /* In a container we lack CAP_MKNOD. We
ab06eef8 1679 shouldn't attempt to create the device node in
cb7ed9df
LP
1680 that case to avoid noise, and we don't support
1681 virtualized devices in containers anyway. */
1682
1683 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
1684 return 0;
1685 }
1686
7fa10748
LP
1687 RUN_WITH_UMASK(0000)
1688 (void) mkdir_parents_label(i->path, 0755);
1689
1554afae 1690 file_type = i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR;
468d726b 1691
5c0d398d 1692 RUN_WITH_UMASK(0000) {
ecabcf8b 1693 mac_selinux_create_file_prepare(i->path, file_type);
5c0d398d 1694 r = mknod(i->path, i->mode | file_type, i->major_minor);
ecabcf8b 1695 mac_selinux_create_file_clear();
5c0d398d 1696 }
468d726b 1697
6555ad8e
KS
1698 if (r < 0) {
1699 if (errno == EPERM) {
1700 log_debug("We lack permissions, possibly because of cgroup configuration; "
1701 "skipping creation of device node %s.", i->path);
1702 return 0;
1703 }
1704
4a62c710
MS
1705 if (errno != EEXIST)
1706 return log_error_errno(errno, "Failed to create device node %s: %m", i->path);
468d726b 1707
a542c4dc 1708 if (lstat(i->path, &st) < 0)
4a62c710 1709 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
468d726b 1710
1554afae
LP
1711 if ((st.st_mode & S_IFMT) != file_type) {
1712
1713 if (i->force) {
1714
1715 RUN_WITH_UMASK(0000) {
ecabcf8b 1716 mac_selinux_create_file_prepare(i->path, file_type);
1554afae 1717 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
ecabcf8b 1718 mac_selinux_create_file_clear();
1554afae
LP
1719 }
1720
f647962d 1721 if (r < 0)
294929f8
ZJS
1722 return log_error_errno(r, "Failed to create device node \"%s\": %m", i->path);
1723 creation = CREATION_FORCE;
1554afae
LP
1724 } else {
1725 log_debug("%s is not a device node.", i->path);
1726 return 0;
1727 }
294929f8
ZJS
1728 } else
1729 creation = CREATION_EXISTING;
1730 } else
1731 creation = CREATION_NORMAL;
a542c4dc 1732
294929f8 1733 log_debug("%s %s device node \"%s\" %u:%u.",
7a7d5db7 1734 creation_mode_verb_to_string(creation),
582deb84
ZJS
1735 i->type == CREATE_BLOCK_DEVICE ? "block" : "char",
1736 i->path, major(i->mode), minor(i->mode));
468d726b 1737
b705ab6a 1738 r = path_set_perms(i, i->path);
468d726b
LP
1739 if (r < 0)
1740 return r;
1741
1742 break;
1743 }
1744
e73a03e0 1745 case ADJUST_MODE:
777b87e7 1746 case RELABEL_PATH:
936f6bdb 1747 r = glob_item(i, path_set_perms);
777b87e7 1748 if (r < 0)
96ca8194 1749 return r;
777b87e7
MS
1750 break;
1751
a8d88783 1752 case RECURSIVE_RELABEL_PATH:
936f6bdb 1753 r = glob_item_recursively(i, fd_set_perms);
a8d88783
MS
1754 if (r < 0)
1755 return r;
ebf4e801 1756 break;
e73a03e0 1757
ebf4e801 1758 case SET_XATTR:
936f6bdb 1759 r = glob_item(i, path_set_xattrs);
b705ab6a
ZJS
1760 if (r < 0)
1761 return r;
1762 break;
1763
1764 case RECURSIVE_SET_XATTR:
936f6bdb 1765 r = glob_item_recursively(i, fd_set_xattrs);
ebf4e801
MW
1766 if (r < 0)
1767 return r;
e73a03e0 1768 break;
f8eeeaf9
ZJS
1769
1770 case SET_ACL:
936f6bdb 1771 r = glob_item(i, path_set_acls);
f8eeeaf9
ZJS
1772 if (r < 0)
1773 return r;
b705ab6a
ZJS
1774 break;
1775
1776 case RECURSIVE_SET_ACL:
936f6bdb 1777 r = glob_item_recursively(i, fd_set_acls);
b705ab6a
ZJS
1778 if (r < 0)
1779 return r;
1780 break;
22c3a6ca 1781
88ec4dfa 1782 case SET_ATTRIBUTE:
936f6bdb 1783 r = glob_item(i, path_set_attribute);
22c3a6ca
GB
1784 if (r < 0)
1785 return r;
1786 break;
1787
88ec4dfa 1788 case RECURSIVE_SET_ATTRIBUTE:
936f6bdb 1789 r = glob_item_recursively(i, fd_set_attribute);
22c3a6ca
GB
1790 if (r < 0)
1791 return r;
1792 break;
3b63d2d3
LP
1793 }
1794
f05bc3f7 1795 return 0;
3b63d2d3
LP
1796}
1797
a0896123 1798static int remove_item_instance(Item *i, const char *instance) {
3b63d2d3
LP
1799 int r;
1800
1801 assert(i);
1802
1803 switch (i->type) {
1804
3b63d2d3 1805 case REMOVE_PATH:
4a62c710 1806 if (remove(instance) < 0 && errno != ENOENT)
3f93da98 1807 return log_error_errno(errno, "rm(%s): %m", instance);
3b63d2d3
LP
1808
1809 break;
1810
1811 case TRUNCATE_DIRECTORY:
1812 case RECURSIVE_REMOVE_PATH:
d139b24a
LP
1813 /* FIXME: we probably should use dir_cleanup() here
1814 * instead of rm_rf() so that 'x' is honoured. */
582deb84 1815 log_debug("rm -rf \"%s\"", instance);
1b26f09e 1816 r = rm_rf(instance, (i->type == RECURSIVE_REMOVE_PATH ? REMOVE_ROOT|REMOVE_SUBVOLUME : 0) | REMOVE_PHYSICAL);
f647962d
MS
1817 if (r < 0 && r != -ENOENT)
1818 return log_error_errno(r, "rm_rf(%s): %m", instance);
3b63d2d3
LP
1819
1820 break;
7fcb4b9b
ZJS
1821
1822 default:
1823 assert_not_reached("wut?");
3b63d2d3
LP
1824 }
1825
1826 return 0;
1827}
1828
a0896123 1829static int remove_item(Item *i) {
b8bb3e8f
LP
1830 assert(i);
1831
582deb84
ZJS
1832 log_debug("Running remove action for entry %c %s", (char) i->type, i->path);
1833
b8bb3e8f
LP
1834 switch (i->type) {
1835
b8bb3e8f
LP
1836 case REMOVE_PATH:
1837 case TRUNCATE_DIRECTORY:
99e68c0b 1838 case RECURSIVE_REMOVE_PATH:
936f6bdb 1839 return glob_item(i, remove_item_instance);
b8bb3e8f 1840
df8dee85
ZJS
1841 default:
1842 return 0;
1843 }
b8bb3e8f
LP
1844}
1845
78a92a5a 1846static int clean_item_instance(Item *i, const char* instance) {
7fd1b19b 1847 _cleanup_closedir_ DIR *d = NULL;
78a92a5a
MS
1848 struct stat s, ps;
1849 bool mountpoint;
78a92a5a 1850 usec_t cutoff, n;
582deb84 1851 char timestamp[FORMAT_TIMESTAMP_MAX];
78a92a5a
MS
1852
1853 assert(i);
1854
1855 if (!i->age_set)
1856 return 0;
1857
1858 n = now(CLOCK_REALTIME);
1859 if (n < i->age)
1860 return 0;
1861
1862 cutoff = n - i->age;
1863
df99a9ef 1864 d = opendir_nomod(instance);
78a92a5a 1865 if (!d) {
d710aaf7 1866 if (IN_SET(errno, ENOENT, ENOTDIR)) {
582deb84 1867 log_debug_errno(errno, "Directory \"%s\": %m", instance);
78a92a5a 1868 return 0;
582deb84 1869 }
78a92a5a 1870
d710aaf7 1871 return log_error_errno(errno, "Failed to open directory %s: %m", instance);
78a92a5a
MS
1872 }
1873
4a62c710
MS
1874 if (fstat(dirfd(d), &s) < 0)
1875 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
78a92a5a
MS
1876
1877 if (!S_ISDIR(s.st_mode)) {
1878 log_error("%s is not a directory.", i->path);
19fbec19 1879 return -ENOTDIR;
78a92a5a
MS
1880 }
1881
4a62c710
MS
1882 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0)
1883 return log_error_errno(errno, "stat(%s/..) failed: %m", i->path);
78a92a5a 1884
2bb158c1 1885 mountpoint = s.st_dev != ps.st_dev || s.st_ino == ps.st_ino;
78a92a5a 1886
582deb84
ZJS
1887 log_debug("Cleanup threshold for %s \"%s\" is %s",
1888 mountpoint ? "mount point" : "directory",
1889 instance,
1890 format_timestamp_us(timestamp, sizeof(timestamp), cutoff));
1891
1892 return dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
1893 MAX_DEPTH, i->keep_first_level);
78a92a5a
MS
1894}
1895
1896static int clean_item(Item *i) {
78a92a5a
MS
1897 assert(i);
1898
582deb84
ZJS
1899 log_debug("Running clean action for entry %c %s", (char) i->type, i->path);
1900
78a92a5a
MS
1901 switch (i->type) {
1902 case CREATE_DIRECTORY:
d7b8eec7 1903 case CREATE_SUBVOLUME:
5fb13eb5
LP
1904 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1905 case CREATE_SUBVOLUME_NEW_QUOTA:
78a92a5a
MS
1906 case TRUNCATE_DIRECTORY:
1907 case IGNORE_PATH:
849958d1 1908 case COPY_FILES:
78a92a5a 1909 clean_item_instance(i, i->path);
df8dee85 1910 return 0;
65241c14 1911 case EMPTY_DIRECTORY:
78a92a5a 1912 case IGNORE_DIRECTORY_PATH:
936f6bdb 1913 return glob_item(i, clean_item_instance);
78a92a5a 1914 default:
df8dee85 1915 return 0;
78a92a5a 1916 }
78a92a5a
MS
1917}
1918
3f93da98
ZJS
1919static int process_item_array(ItemArray *array);
1920
3b63d2d3 1921static int process_item(Item *i) {
1e95893a 1922 int r, q, p, t = 0;
9348f0e6 1923 _cleanup_free_ char *prefix = NULL;
3b63d2d3
LP
1924
1925 assert(i);
1926
1910cd0e
LP
1927 if (i->done)
1928 return 0;
1929
1930 i->done = true;
1931
9348f0e6
ZJS
1932 prefix = malloc(strlen(i->path) + 1);
1933 if (!prefix)
1934 return log_oom();
1935
1910cd0e 1936 PATH_FOREACH_PREFIX(prefix, i->path) {
3f93da98 1937 ItemArray *j;
1910cd0e 1938
ef43a391 1939 j = ordered_hashmap_get(items, prefix);
1db50423
ZJS
1940 if (j) {
1941 int s;
1942
3f93da98 1943 s = process_item_array(j);
1db50423
ZJS
1944 if (s < 0 && t == 0)
1945 t = s;
1946 }
1910cd0e
LP
1947 }
1948
655f2da0
N
1949 if (chase_symlinks(i->path, NULL, CHASE_NO_AUTOFS, NULL) == -EREMOTE)
1950 return t;
1951
3b63d2d3 1952 r = arg_create ? create_item(i) : 0;
a0896123 1953 q = arg_remove ? remove_item(i) : 0;
3b63d2d3
LP
1954 p = arg_clean ? clean_item(i) : 0;
1955
1db50423
ZJS
1956 return t < 0 ? t :
1957 r < 0 ? r :
1958 q < 0 ? q :
1959 p;
3b63d2d3
LP
1960}
1961
3f93da98
ZJS
1962static int process_item_array(ItemArray *array) {
1963 unsigned n;
1964 int r = 0, k;
753615e8 1965
3f93da98
ZJS
1966 assert(array);
1967
1968 for (n = 0; n < array->count; n++) {
1969 k = process_item(array->items + n);
1970 if (k < 0 && r == 0)
1971 r = k;
1972 }
1973
1974 return r;
1975}
3b63d2d3 1976
3f93da98
ZJS
1977static void item_free_contents(Item *i) {
1978 assert(i);
3b63d2d3 1979 free(i->path);
468d726b 1980 free(i->argument);
ebf4e801 1981 strv_free(i->xattrs);
f8eeeaf9 1982
349cc4a5 1983#if HAVE_ACL
f8eeeaf9
ZJS
1984 acl_free(i->acl_access);
1985 acl_free(i->acl_default);
1986#endif
3b63d2d3
LP
1987}
1988
3f93da98
ZJS
1989static void item_array_free(ItemArray *a) {
1990 unsigned n;
1991
1992 if (!a)
1993 return;
1994
1995 for (n = 0; n < a->count; n++)
1996 item_free_contents(a->items + n);
1997 free(a->items);
1998 free(a);
1999}
e2f2fb78 2000
17493fa5
LP
2001static int item_compare(const void *a, const void *b) {
2002 const Item *x = a, *y = b;
2003
2004 /* Make sure that the ownership taking item is put first, so
2005 * that we first create the node, and then can adjust it */
2006
2007 if (takes_ownership(x->type) && !takes_ownership(y->type))
2008 return -1;
2009 if (!takes_ownership(x->type) && takes_ownership(y->type))
2010 return 1;
2011
2012 return (int) x->type - (int) y->type;
2013}
2014
3f93da98 2015static bool item_compatible(Item *a, Item *b) {
bfe95f35
LP
2016 assert(a);
2017 assert(b);
3f93da98 2018 assert(streq(a->path, b->path));
bfe95f35 2019
3f93da98
ZJS
2020 if (takes_ownership(a->type) && takes_ownership(b->type))
2021 /* check if the items are the same */
2022 return streq_ptr(a->argument, b->argument) &&
bfe95f35 2023
3f93da98
ZJS
2024 a->uid_set == b->uid_set &&
2025 a->uid == b->uid &&
bfe95f35 2026
3f93da98
ZJS
2027 a->gid_set == b->gid_set &&
2028 a->gid == b->gid &&
bfe95f35 2029
3f93da98
ZJS
2030 a->mode_set == b->mode_set &&
2031 a->mode == b->mode &&
bfe95f35 2032
3f93da98
ZJS
2033 a->age_set == b->age_set &&
2034 a->age == b->age &&
bfe95f35 2035
3f93da98 2036 a->mask_perms == b->mask_perms &&
bfe95f35 2037
3f93da98 2038 a->keep_first_level == b->keep_first_level &&
468d726b 2039
3f93da98 2040 a->major_minor == b->major_minor;
468d726b 2041
bfe95f35
LP
2042 return true;
2043}
2044
a2aced4a
DR
2045static bool should_include_path(const char *path) {
2046 char **prefix;
2047
abef3f91 2048 STRV_FOREACH(prefix, arg_exclude_prefixes)
582deb84
ZJS
2049 if (path_startswith(path, *prefix)) {
2050 log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.",
2051 path, *prefix);
5c795114 2052 return false;
582deb84 2053 }
a2aced4a 2054
abef3f91 2055 STRV_FOREACH(prefix, arg_include_prefixes)
582deb84
ZJS
2056 if (path_startswith(path, *prefix)) {
2057 log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix);
a2aced4a 2058 return true;
582deb84 2059 }
a2aced4a 2060
5c795114
DR
2061 /* no matches, so we should include this path only if we
2062 * have no whitelist at all */
7b943bb7 2063 if (strv_isempty(arg_include_prefixes))
582deb84
ZJS
2064 return true;
2065
2066 log_debug("Entry \"%s\" does not match any include prefix, skipping.", path);
2067 return false;
a2aced4a
DR
2068}
2069
4cef1923
FB
2070static int specifier_expansion_from_arg(Item *i) {
2071 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
2072 char **xattr;
2073 int r;
2074
2075 assert(i);
2076
2077 if (i->argument == NULL)
2078 return 0;
2079
2080 switch (i->type) {
2081 case COPY_FILES:
2082 case CREATE_SYMLINK:
2083 case CREATE_FILE:
2084 case TRUNCATE_FILE:
2085 case WRITE_FILE:
2086 r = cunescape(i->argument, 0, &unescaped);
2087 if (r < 0)
2088 return log_error_errno(r, "Failed to unescape parameter to write: %s", i->argument);
2089
2090 r = specifier_printf(unescaped, specifier_table, NULL, &resolved);
2091 if (r < 0)
2092 return r;
2093
2094 free_and_replace(i->argument, resolved);
2095 break;
2096
2097 case SET_XATTR:
2098 case RECURSIVE_SET_XATTR:
2099 assert(i->xattrs);
2100
2101 STRV_FOREACH (xattr, i->xattrs) {
2102 r = specifier_printf(*xattr, specifier_table, NULL, &resolved);
2103 if (r < 0)
2104 return r;
2105
2106 free_and_replace(*xattr, resolved);
2107 }
2108 break;
2109
2110 default:
2111 break;
2112 }
2113 return 0;
2114}
2115
a2d1fb88
LP
2116static int patch_var_run(const char *fname, unsigned line, char **path) {
2117 const char *k;
2118 char *n;
2119
2120 assert(path);
2121 assert(*path);
2122
2123 /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges lines in
2124 * some cases and detects conflicts in others. If files/directories are specified through two equivalent lines
2125 * this is problematic as neither case will be detected. Ideally we'd detect these cases by resolving symlinks
2126 * early, but that's precisely not what we can do here as this code very likely is running very early on, at a
2127 * time where the paths in question are not available yet, or even more importantly, our own tmpfiles rules
2128 * might create the paths that are intermediary to the listed paths. We can't really cover the generic case,
2129 * but the least we can do is cover the specific case of /var/run vs. /run, as /var/run is a legacy name for
2130 * /run only, and we explicitly document that and require that on systemd systems the former is a symlink to
2131 * the latter. Moreover files below this path are by far the primary usecase for tmpfiles.d/. */
2132
2133 k = path_startswith(*path, "/var/run/");
2134 if (isempty(k)) /* Don't complain about other paths than /var/run, and not about /var/run itself either. */
2135 return 0;
2136
2137 n = strjoin("/run/", k);
2138 if (!n)
2139 return log_oom();
2140
2141 /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation automatically, hence
2142 * there's no immediate need for action by the user. However, in the interest of making things less confusing
2143 * to the user, let's still inform the user that these snippets should really be updated. */
2144
2145 log_notice("[%s:%u] Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", fname, line, *path, n);
2146
2147 free(*path);
2148 *path = n;
2149
2150 return 0;
2151}
2152
d9daae55 2153static int parse_line(const char *fname, unsigned line, const char *buffer, bool *invalid_config) {
1731e34a 2154
cde684a2 2155 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
3f93da98
ZJS
2156 _cleanup_(item_free_contents) Item i = {};
2157 ItemArray *existing;
ef43a391 2158 OrderedHashmap *h;
657cf7f4 2159 int r, pos;
5f255144 2160 bool force = false, boot = false;
3b63d2d3
LP
2161
2162 assert(fname);
2163 assert(line >= 1);
2164 assert(buffer);
2165
68685607 2166 r = extract_many_words(
4034a06d 2167 &buffer,
68685607 2168 NULL,
12ba2c44 2169 EXTRACT_QUOTES,
4034a06d
LP
2170 &action,
2171 &path,
2172 &mode,
2173 &user,
2174 &group,
2175 &age,
2176 NULL);
d9daae55 2177 if (r < 0) {
751223fe
ZJS
2178 if (IN_SET(r, -EINVAL, -EBADSLT))
2179 /* invalid quoting and such or an unknown specifier */
d9daae55 2180 *invalid_config = true;
657cf7f4 2181 return log_error_errno(r, "[%s:%u] Failed to parse line: %m", fname, line);
4b93699d 2182 } else if (r < 2) {
d9daae55 2183 *invalid_config = true;
3b63d2d3 2184 log_error("[%s:%u] Syntax error.", fname, line);
7f2c1f4d 2185 return -EIO;
5008d581
LP
2186 }
2187
aa5f6817 2188 if (!isempty(buffer) && !streq(buffer, "-")) {
4034a06d
LP
2189 i.argument = strdup(buffer);
2190 if (!i.argument)
2191 return log_oom();
2192 }
2193
2e78fa79 2194 if (isempty(action)) {
d9daae55 2195 *invalid_config = true;
2e78fa79 2196 log_error("[%s:%u] Command too short '%s'.", fname, line, action);
c4708f13 2197 return -EINVAL;
2e78fa79
LP
2198 }
2199
5f255144
ZJS
2200 for (pos = 1; action[pos]; pos++) {
2201 if (action[pos] == '!' && !boot)
2202 boot = true;
2203 else if (action[pos] == '+' && !force)
2204 force = true;
2205 else {
d9daae55 2206 *invalid_config = true;
5f255144
ZJS
2207 log_error("[%s:%u] Unknown modifiers in command '%s'",
2208 fname, line, action);
2209 return -EINVAL;
2210 }
2e78fa79
LP
2211 }
2212
582deb84
ZJS
2213 if (boot && !arg_boot) {
2214 log_debug("Ignoring entry %s \"%s\" because --boot is not specified.",
2215 action, path);
c4708f13 2216 return 0;
582deb84 2217 }
c4708f13 2218
3f93da98
ZJS
2219 i.type = action[0];
2220 i.force = force;
2e78fa79 2221
3f93da98 2222 r = specifier_printf(path, specifier_table, NULL, &i.path);
5a8575ef 2223 if (r == -ENXIO)
4cef1923 2224 return log_unresolvable_specifier(fname, line);
d9daae55 2225 if (r < 0) {
751223fe
ZJS
2226 if (IN_SET(r, -EINVAL, -EBADSLT))
2227 *invalid_config = true;
4cef1923 2228 return log_error_errno(r, "[%s:%u] Failed to replace specifiers: %s", fname, line, path);
d9daae55 2229 }
1731e34a 2230
a2d1fb88
LP
2231 r = patch_var_run(fname, line, &i.path);
2232 if (r < 0)
2233 return r;
2234
3f93da98 2235 switch (i.type) {
468d726b 2236
777b87e7 2237 case CREATE_DIRECTORY:
d7b8eec7 2238 case CREATE_SUBVOLUME:
5fb13eb5
LP
2239 case CREATE_SUBVOLUME_INHERIT_QUOTA:
2240 case CREATE_SUBVOLUME_NEW_QUOTA:
df8dee85 2241 case EMPTY_DIRECTORY:
777b87e7
MS
2242 case TRUNCATE_DIRECTORY:
2243 case CREATE_FIFO:
2244 case IGNORE_PATH:
78a92a5a 2245 case IGNORE_DIRECTORY_PATH:
777b87e7
MS
2246 case REMOVE_PATH:
2247 case RECURSIVE_REMOVE_PATH:
e73a03e0 2248 case ADJUST_MODE:
777b87e7
MS
2249 case RELABEL_PATH:
2250 case RECURSIVE_RELABEL_PATH:
c82500c6 2251 if (i.argument)
bd550f78 2252 log_warning("[%s:%u] %c lines don't take argument fields, ignoring.", fname, line, i.type);
c82500c6
LP
2253
2254 break;
2255
2256 case CREATE_FILE:
2257 case TRUNCATE_FILE:
777b87e7 2258 break;
468d726b
LP
2259
2260 case CREATE_SYMLINK:
3f93da98
ZJS
2261 if (!i.argument) {
2262 i.argument = strappend("/usr/share/factory/", i.path);
2263 if (!i.argument)
2f3b873a 2264 return log_oom();
468d726b
LP
2265 }
2266 break;
2267
31ed59c5 2268 case WRITE_FILE:
3f93da98 2269 if (!i.argument) {
d9daae55 2270 *invalid_config = true;
31ed59c5 2271 log_error("[%s:%u] Write file requires argument.", fname, line);
7f2c1f4d 2272 return -EBADMSG;
31ed59c5
LP
2273 }
2274 break;
2275
849958d1 2276 case COPY_FILES:
3f93da98
ZJS
2277 if (!i.argument) {
2278 i.argument = strappend("/usr/share/factory/", i.path);
2279 if (!i.argument)
2f3b873a 2280 return log_oom();
3f93da98 2281 } else if (!path_is_absolute(i.argument)) {
d9daae55 2282 *invalid_config = true;
849958d1
LP
2283 log_error("[%s:%u] Source path is not absolute.", fname, line);
2284 return -EBADMSG;
2285 }
2286
858d36c1 2287 path_simplify(i.argument, false);
849958d1
LP
2288 break;
2289
468d726b
LP
2290 case CREATE_CHAR_DEVICE:
2291 case CREATE_BLOCK_DEVICE: {
2292 unsigned major, minor;
2293
3f93da98 2294 if (!i.argument) {
d9daae55 2295 *invalid_config = true;
468d726b 2296 log_error("[%s:%u] Device file requires argument.", fname, line);
7f2c1f4d 2297 return -EBADMSG;
468d726b
LP
2298 }
2299
3f93da98 2300 if (sscanf(i.argument, "%u:%u", &major, &minor) != 2) {
d9daae55 2301 *invalid_config = true;
3f93da98 2302 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i.argument);
7f2c1f4d 2303 return -EBADMSG;
468d726b
LP
2304 }
2305
3f93da98 2306 i.major_minor = makedev(major, minor);
468d726b
LP
2307 break;
2308 }
2309
ebf4e801 2310 case SET_XATTR:
b705ab6a 2311 case RECURSIVE_SET_XATTR:
3f93da98 2312 if (!i.argument) {
d9daae55 2313 *invalid_config = true;
ebf4e801
MW
2314 log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
2315 return -EBADMSG;
2316 }
bd550f78 2317 r = parse_xattrs_from_arg(&i);
ebf4e801
MW
2318 if (r < 0)
2319 return r;
2320 break;
2321
f8eeeaf9 2322 case SET_ACL:
b705ab6a 2323 case RECURSIVE_SET_ACL:
f8eeeaf9 2324 if (!i.argument) {
d9daae55 2325 *invalid_config = true;
f8eeeaf9
ZJS
2326 log_error("[%s:%u] Set ACLs requires argument.", fname, line);
2327 return -EBADMSG;
2328 }
bd550f78 2329 r = parse_acls_from_arg(&i);
f8eeeaf9
ZJS
2330 if (r < 0)
2331 return r;
2332 break;
2333
88ec4dfa
LP
2334 case SET_ATTRIBUTE:
2335 case RECURSIVE_SET_ATTRIBUTE:
22c3a6ca 2336 if (!i.argument) {
d9daae55 2337 *invalid_config = true;
88ec4dfa 2338 log_error("[%s:%u] Set file attribute requires argument.", fname, line);
22c3a6ca
GB
2339 return -EBADMSG;
2340 }
bd550f78 2341 r = parse_attribute_from_arg(&i);
751223fe 2342 if (IN_SET(r, -EINVAL, -EBADSLT))
d9daae55 2343 *invalid_config = true;
22c3a6ca
GB
2344 if (r < 0)
2345 return r;
2346 break;
2347
777b87e7 2348 default:
582deb84 2349 log_error("[%s:%u] Unknown command type '%c'.", fname, line, (char) i.type);
d9daae55 2350 *invalid_config = true;
7f2c1f4d 2351 return -EBADMSG;
3b63d2d3 2352 }
468d726b 2353
3f93da98
ZJS
2354 if (!path_is_absolute(i.path)) {
2355 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i.path);
d9daae55 2356 *invalid_config = true;
7f2c1f4d 2357 return -EBADMSG;
3b63d2d3
LP
2358 }
2359
858d36c1 2360 path_simplify(i.path, false);
3b63d2d3 2361
3f93da98 2362 if (!should_include_path(i.path))
7f2c1f4d 2363 return 0;
5008d581 2364
4cef1923 2365 r = specifier_expansion_from_arg(&i);
5a8575ef 2366 if (r == -ENXIO)
4cef1923 2367 return log_unresolvable_specifier(fname, line);
751223fe
ZJS
2368 if (r < 0) {
2369 if (IN_SET(r, -EINVAL, -EBADSLT))
2370 *invalid_config = true;
4cef1923
FB
2371 return log_error_errno(r, "[%s:%u] Failed to substitute specifiers in argument: %m",
2372 fname, line);
751223fe 2373 }
4cef1923 2374
cf9a4abd 2375 if (arg_root) {
cde684a2
LP
2376 char *p;
2377
1d13f648 2378 p = prefix_root(arg_root, i.path);
cf9a4abd
MM
2379 if (!p)
2380 return log_oom();
2381
3f93da98
ZJS
2382 free(i.path);
2383 i.path = p;
cf9a4abd
MM
2384 }
2385
90937fe3 2386 if (!isempty(user) && !streq(user, "-")) {
4b67834e
LP
2387 const char *u = user;
2388
3f93da98 2389 r = get_user_creds(&u, &i.uid, NULL, NULL, NULL);
4b67834e 2390 if (r < 0) {
d9daae55
ZJS
2391 *invalid_config = true;
2392 return log_error_errno(r, "[%s:%u] Unknown user '%s'.", fname, line, user);
3b63d2d3
LP
2393 }
2394
3f93da98 2395 i.uid_set = true;
3b63d2d3
LP
2396 }
2397
90937fe3 2398 if (!isempty(group) && !streq(group, "-")) {
4b67834e
LP
2399 const char *g = group;
2400
3f93da98 2401 r = get_group_creds(&g, &i.gid);
4b67834e 2402 if (r < 0) {
d9daae55 2403 *invalid_config = true;
3b63d2d3 2404 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
7f2c1f4d 2405 return r;
3b63d2d3
LP
2406 }
2407
3f93da98 2408 i.gid_set = true;
3b63d2d3
LP
2409 }
2410
90937fe3 2411 if (!isempty(mode) && !streq(mode, "-")) {
abef3f91 2412 const char *mm = mode;
3b63d2d3
LP
2413 unsigned m;
2414
abef3f91 2415 if (*mm == '~') {
3f93da98 2416 i.mask_perms = true;
abef3f91
LP
2417 mm++;
2418 }
2419
2ff7b0a5 2420 if (parse_mode(mm, &m) < 0) {
d9daae55 2421 *invalid_config = true;
3b63d2d3 2422 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
2ff7b0a5 2423 return -EBADMSG;
3b63d2d3
LP
2424 }
2425
3f93da98
ZJS
2426 i.mode = m;
2427 i.mode_set = true;
3b63d2d3 2428 } else
5fb13eb5 2429 i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644;
3b63d2d3 2430
90937fe3 2431 if (!isempty(age) && !streq(age, "-")) {
24f3a374
LP
2432 const char *a = age;
2433
2434 if (*a == '~') {
3f93da98 2435 i.keep_first_level = true;
24f3a374
LP
2436 a++;
2437 }
2438
3f93da98 2439 if (parse_sec(a, &i.age) < 0) {
d9daae55 2440 *invalid_config = true;
3b63d2d3 2441 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
7f2c1f4d 2442 return -EBADMSG;
3b63d2d3
LP
2443 }
2444
3f93da98 2445 i.age_set = true;
3b63d2d3
LP
2446 }
2447
3f93da98 2448 h = needs_glob(i.type) ? globs : items;
bfe95f35 2449
ef43a391 2450 existing = ordered_hashmap_get(h, i.path);
468d726b 2451 if (existing) {
3f93da98
ZJS
2452 unsigned n;
2453
2454 for (n = 0; n < existing->count; n++) {
6487ada8 2455 if (!item_compatible(existing->items + n, &i)) {
e286dbaf
ZJS
2456 log_notice("[%s:%u] Duplicate line for path \"%s\", ignoring.",
2457 fname, line, i.path);
6487ada8
MP
2458 return 0;
2459 }
ebf4e801
MW
2460 }
2461 } else {
3f93da98 2462 existing = new0(ItemArray, 1);
07982ed1
LP
2463 if (!existing)
2464 return log_oom();
2465
ef43a391 2466 r = ordered_hashmap_put(h, i.path, existing);
3f93da98
ZJS
2467 if (r < 0)
2468 return log_oom();
bfe95f35
LP
2469 }
2470
3f93da98
ZJS
2471 if (!GREEDY_REALLOC(existing->items, existing->size, existing->count + 1))
2472 return log_oom();
5008d581 2473
3f93da98 2474 memcpy(existing->items + existing->count++, &i, sizeof(i));
dd449aca
ZJS
2475
2476 /* Sort item array, to enforce stable ordering of application */
2477 qsort_safe(existing->items, existing->count, sizeof(Item), item_compare);
17493fa5 2478
3f93da98 2479 zero(i);
7f2c1f4d 2480 return 0;
5008d581
LP
2481}
2482
ceaaeb9b
ZJS
2483static int cat_config(char **config_dirs, char **args) {
2484 _cleanup_strv_free_ char **files = NULL;
2485 int r;
2486
2487 r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL);
2488 if (r < 0)
2489 return r;
2490
2491 return cat_files(NULL, files, 0);
2492}
2493
601185b4 2494static void help(void) {
522d4a49
LP
2495 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
2496 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
5c795114 2497 " -h --help Show this help\n"
f2b5ca0e 2498 " --user Execute user configuration\n"
eb9da376 2499 " --version Show package version\n"
ceaaeb9b 2500 " --cat-config Show configuration files\n"
5c795114
DR
2501 " --create Create marked files/directories\n"
2502 " --clean Clean up marked directories\n"
2503 " --remove Remove marked files/directories\n"
81815651 2504 " --boot Execute actions only safe at boot\n"
79ca888f
ZJS
2505 " --prefix=PATH Only apply rules with the specified prefix\n"
2506 " --exclude-prefix=PATH Ignore rules with the specified prefix\n"
d9daae55 2507 " --root=PATH Operate on an alternate filesystem root\n"
a6d8474f 2508 " --replace=PATH Treat arguments as replacement for PATH\n"
dcd5c891 2509 " --no-pager Do not pipe output into a pager\n"
d9daae55 2510 , program_invocation_short_name);
3b63d2d3
LP
2511}
2512
2513static int parse_argv(int argc, char *argv[]) {
2514
2515 enum {
eb9da376 2516 ARG_VERSION = 0x100,
ceaaeb9b 2517 ARG_CAT_CONFIG,
f2b5ca0e 2518 ARG_USER,
3b63d2d3
LP
2519 ARG_CREATE,
2520 ARG_CLEAN,
fba6e687 2521 ARG_REMOVE,
81815651 2522 ARG_BOOT,
5c795114
DR
2523 ARG_PREFIX,
2524 ARG_EXCLUDE_PREFIX,
cf9a4abd 2525 ARG_ROOT,
a6d8474f 2526 ARG_REPLACE,
dcd5c891 2527 ARG_NO_PAGER,
3b63d2d3
LP
2528 };
2529
2530 static const struct option options[] = {
5c795114 2531 { "help", no_argument, NULL, 'h' },
f2b5ca0e 2532 { "user", no_argument, NULL, ARG_USER },
eb9da376 2533 { "version", no_argument, NULL, ARG_VERSION },
ceaaeb9b 2534 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
5c795114
DR
2535 { "create", no_argument, NULL, ARG_CREATE },
2536 { "clean", no_argument, NULL, ARG_CLEAN },
2537 { "remove", no_argument, NULL, ARG_REMOVE },
81815651 2538 { "boot", no_argument, NULL, ARG_BOOT },
5c795114
DR
2539 { "prefix", required_argument, NULL, ARG_PREFIX },
2540 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
cf9a4abd 2541 { "root", required_argument, NULL, ARG_ROOT },
a6d8474f 2542 { "replace", required_argument, NULL, ARG_REPLACE },
dcd5c891 2543 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
eb9da376 2544 {}
3b63d2d3
LP
2545 };
2546
0f474365 2547 int c, r;
3b63d2d3
LP
2548
2549 assert(argc >= 0);
2550 assert(argv);
2551
601185b4 2552 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
3b63d2d3
LP
2553
2554 switch (c) {
2555
2556 case 'h':
601185b4
ZJS
2557 help();
2558 return 0;
eb9da376
LP
2559
2560 case ARG_VERSION:
3f6fd1ba 2561 return version();
3b63d2d3 2562
ceaaeb9b
ZJS
2563 case ARG_CAT_CONFIG:
2564 arg_cat_config = true;
2565 break;
2566
f2b5ca0e
ZJS
2567 case ARG_USER:
2568 arg_user = true;
2569 break;
2570
3b63d2d3
LP
2571 case ARG_CREATE:
2572 arg_create = true;
2573 break;
2574
2575 case ARG_CLEAN:
2576 arg_clean = true;
2577 break;
2578
2579 case ARG_REMOVE:
2580 arg_remove = true;
2581 break;
2582
81815651
ZJS
2583 case ARG_BOOT:
2584 arg_boot = true;
c4708f13
ZJS
2585 break;
2586
fba6e687 2587 case ARG_PREFIX:
7bc040fa 2588 if (strv_push(&arg_include_prefixes, optarg) < 0)
a2aced4a 2589 return log_oom();
fba6e687
LP
2590 break;
2591
5c795114 2592 case ARG_EXCLUDE_PREFIX:
7bc040fa 2593 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
5c795114
DR
2594 return log_oom();
2595 break;
2596
cf9a4abd 2597 case ARG_ROOT:
0f03c2a4 2598 r = parse_path_argument_and_warn(optarg, true, &arg_root);
0f474365 2599 if (r < 0)
0f03c2a4 2600 return r;
cf9a4abd
MM
2601 break;
2602
a6d8474f
ZJS
2603 case ARG_REPLACE:
2604 if (!path_is_absolute(optarg) ||
2605 !endswith(optarg, ".conf")) {
2606 log_error("The argument to --replace= must an absolute path to a config file");
2607 return -EINVAL;
2608 }
2609
2610 arg_replace = optarg;
2611 break;
2612
dcd5c891
LP
2613 case ARG_NO_PAGER:
2614 arg_no_pager = true;
2615 break;
2616
3b63d2d3
LP
2617 case '?':
2618 return -EINVAL;
2619
2620 default:
eb9da376 2621 assert_not_reached("Unhandled option");
3b63d2d3 2622 }
3b63d2d3 2623
ceaaeb9b 2624 if (!arg_clean && !arg_create && !arg_remove && !arg_cat_config) {
35b8ca3a 2625 log_error("You need to specify at least one of --clean, --create or --remove.");
3b63d2d3
LP
2626 return -EINVAL;
2627 }
2628
ceaaeb9b
ZJS
2629 if (arg_replace && arg_cat_config) {
2630 log_error("Option --replace= is not supported with --cat-config");
2631 return -EINVAL;
2632 }
2633
a6d8474f
ZJS
2634 if (arg_replace && optind >= argc) {
2635 log_error("When --replace= is given, some configuration items must be specified");
2636 return -EINVAL;
2637 }
2638
3b63d2d3
LP
2639 return 1;
2640}
2641
a6d8474f 2642static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoent, bool *invalid_config) {
f7ac1ed2
ZJS
2643 _cleanup_fclose_ FILE *_f = NULL;
2644 FILE *f;
1731e34a 2645 char line[LINE_MAX];
78a92a5a 2646 Iterator iterator;
1731e34a 2647 unsigned v = 0;
78a92a5a 2648 Item *i;
4e68ec18 2649 int r = 0;
fba6e687
LP
2650
2651 assert(fn);
2652
f7ac1ed2 2653 if (streq(fn, "-")) {
a6d8474f 2654 log_debug("Reading config from stdin…");
f7ac1ed2
ZJS
2655 fn = "<stdin>";
2656 f = stdin;
2657 } else {
a6d8474f 2658 r = search_and_fopen(fn, "re", arg_root, (const char**) config_dirs, &_f);
f7ac1ed2
ZJS
2659 if (r < 0) {
2660 if (ignore_enoent && r == -ENOENT) {
2661 log_debug_errno(r, "Failed to open \"%s\", ignoring: %m", fn);
2662 return 0;
2663 }
fba6e687 2664
f7ac1ed2
ZJS
2665 return log_error_errno(r, "Failed to open '%s': %m", fn);
2666 }
a6d8474f 2667 log_debug("Reading config file \"%s\"…", fn);
f7ac1ed2 2668 f = _f;
fba6e687
LP
2669 }
2670
1731e34a
LP
2671 FOREACH_LINE(line, f, break) {
2672 char *l;
fba6e687 2673 int k;
d9daae55 2674 bool invalid_line = false;
fba6e687 2675
fba6e687
LP
2676 v++;
2677
2678 l = strstrip(line);
4c701096 2679 if (IN_SET(*l, 0, '#'))
fba6e687
LP
2680 continue;
2681
d9daae55
ZJS
2682 k = parse_line(fn, v, l, &invalid_line);
2683 if (k < 0) {
2684 if (invalid_line)
2685 /* Allow reporting with a special code if the caller requested this */
2686 *invalid_config = true;
2687 else if (r == 0)
2688 /* The first error becomes our return value */
2689 r = k;
2690 }
fba6e687
LP
2691 }
2692
78a92a5a 2693 /* we have to determine age parameter for each entry of type X */
ef43a391 2694 ORDERED_HASHMAP_FOREACH(i, globs, iterator) {
78a92a5a
MS
2695 Iterator iter;
2696 Item *j, *candidate_item = NULL;
2697
2698 if (i->type != IGNORE_DIRECTORY_PATH)
2699 continue;
2700
ef43a391 2701 ORDERED_HASHMAP_FOREACH(j, items, iter) {
5fb13eb5 2702 if (!IN_SET(j->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA))
78a92a5a
MS
2703 continue;
2704
2705 if (path_equal(j->path, i->path)) {
2706 candidate_item = j;
2707 break;
2708 }
2709
2710 if ((!candidate_item && path_startswith(i->path, j->path)) ||
2711 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
2712 candidate_item = j;
2713 }
2714
9ed2a35e 2715 if (candidate_item && candidate_item->age_set) {
78a92a5a
MS
2716 i->age = candidate_item->age;
2717 i->age_set = true;
2718 }
2719 }
2720
fba6e687 2721 if (ferror(f)) {
56f64d95 2722 log_error_errno(errno, "Failed to read from file %s: %m", fn);
fba6e687
LP
2723 if (r == 0)
2724 r = -EIO;
2725 }
2726
fba6e687
LP
2727 return r;
2728}
2729
a6d8474f
ZJS
2730static int parse_arguments(char **config_dirs, char **args, bool *invalid_config) {
2731 char **arg;
2732 int r;
2733
2734 STRV_FOREACH(arg, args) {
2735 r = read_config_file(config_dirs, *arg, false, invalid_config);
2736 if (r < 0)
2737 return r;
2738 }
2739
2740 return 0;
2741}
2742
2743static int read_config_files(char **config_dirs, char **args, bool *invalid_config) {
2744 _cleanup_strv_free_ char **files = NULL;
2745 _cleanup_free_ char *p = NULL;
2746 char **f;
2747 int r;
2748
ceaaeb9b 2749 r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p);
a6d8474f 2750 if (r < 0)
ceaaeb9b 2751 return r;
a6d8474f
ZJS
2752
2753 STRV_FOREACH(f, files)
2754 if (p && path_equal(*f, p)) {
2755 log_debug("Parsing arguments at position \"%s\"…", *f);
2756
2757 r = parse_arguments(config_dirs, args, invalid_config);
2758 if (r < 0)
2759 return r;
2760 } else
2761 /* Just warn, ignore result otherwise.
2762 * read_config_file() has some debug output, so no need to print anything. */
2763 (void) read_config_file(config_dirs, *f, true, invalid_config);
2764
2765 return 0;
2766}
2767
5008d581 2768int main(int argc, char *argv[]) {
bb9947be 2769 int r, k, r_process = 0;
3f93da98 2770 ItemArray *a;
3b63d2d3 2771 Iterator iterator;
f2b5ca0e 2772 _cleanup_strv_free_ char **config_dirs = NULL;
d9daae55 2773 bool invalid_config = false;
3b63d2d3 2774
fdcad0c2
LP
2775 r = parse_argv(argc, argv);
2776 if (r <= 0)
753615e8 2777 goto finish;
5008d581 2778
eb0ca9eb 2779 log_set_target(LOG_TARGET_AUTO);
5008d581
LP
2780 log_parse_environment();
2781 log_open();
2782
f2b5ca0e
ZJS
2783 if (arg_user) {
2784 r = user_config_paths(&config_dirs);
2785 if (r < 0) {
2786 log_error_errno(r, "Failed to initialize configuration directory list: %m");
2787 goto finish;
2788 }
2789 } else {
2790 config_dirs = strv_split_nulstr(CONF_PATHS_NULSTR("tmpfiles.d"));
2791 if (!config_dirs) {
2792 r = log_oom();
2793 goto finish;
2794 }
2795 }
2796
79c91cec 2797 if (DEBUG_LOGGING) {
f2b5ca0e
ZJS
2798 _cleanup_free_ char *t = NULL;
2799
2800 t = strv_join(config_dirs, "\n\t");
2801 if (t)
bb9947be 2802 log_debug("Looking for configuration files in (higher priority first):\n\t%s", t);
f2b5ca0e
ZJS
2803 }
2804
ceaaeb9b 2805 if (arg_cat_config) {
dcd5c891
LP
2806 (void) pager_open(arg_no_pager, false);
2807
ceaaeb9b
ZJS
2808 r = cat_config(config_dirs, argv + optind);
2809 goto finish;
2810 }
2811
2812 umask(0022);
2813
2814 mac_selinux_init();
2815
2816 items = ordered_hashmap_new(&string_hash_ops);
2817 globs = ordered_hashmap_new(&string_hash_ops);
2818
2819 if (!items || !globs) {
2820 r = log_oom();
2821 goto finish;
2822 }
2823
a6d8474f
ZJS
2824 /* If command line arguments are specified along with --replace, read all
2825 * configuration files and insert the positional arguments at the specified
2826 * place. Otherwise, if command line arguments are specified, execute just
2827 * them, and finally, without --replace= or any positional arguments, just
2828 * read configuration and execute it.
2829 */
2830 if (arg_replace || optind >= argc)
2831 r = read_config_files(config_dirs, argv + optind, &invalid_config);
2832 else
2833 r = parse_arguments(config_dirs, argv + optind, &invalid_config);
2834 if (r < 0)
2835 goto finish;
5008d581 2836
17493fa5
LP
2837 /* The non-globbing ones usually create things, hence we apply
2838 * them first */
ef43a391 2839 ORDERED_HASHMAP_FOREACH(a, items, iterator) {
3f93da98 2840 k = process_item_array(a);
bb9947be
ZJS
2841 if (k < 0 && r_process == 0)
2842 r_process = k;
1db50423 2843 }
b8bb3e8f 2844
17493fa5
LP
2845 /* The globbing ones usually alter things, hence we apply them
2846 * second. */
ef43a391 2847 ORDERED_HASHMAP_FOREACH(a, globs, iterator) {
3f93da98 2848 k = process_item_array(a);
bb9947be
ZJS
2849 if (k < 0 && r_process == 0)
2850 r_process = k;
1db50423 2851 }
3b63d2d3 2852
5008d581 2853finish:
dcd5c891
LP
2854 pager_close();
2855
224b0e7a
ZJS
2856 ordered_hashmap_free_with_destructor(items, item_array_free);
2857 ordered_hashmap_free_with_destructor(globs, item_array_free);
5008d581 2858
7bc040fa
LP
2859 free(arg_include_prefixes);
2860 free(arg_exclude_prefixes);
cf9a4abd 2861 free(arg_root);
a2aced4a 2862
17b90525
LP
2863 set_free_free(unix_sockets);
2864
cc56fafe 2865 mac_selinux_finish();
29003cff 2866
bb9947be 2867 if (r < 0 || ERRNO_IS_RESOURCE(-r_process))
d9daae55
ZJS
2868 return EXIT_FAILURE;
2869 else if (invalid_config)
2870 return EX_DATAERR;
bb9947be
ZJS
2871 else if (r_process < 0)
2872 return EX_CANTCREAT;
d9daae55
ZJS
2873 else
2874 return EXIT_SUCCESS;
5008d581 2875}