]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/tmpfiles.c
Spelling Corrections
[thirdparty/systemd.git] / src / tmpfiles.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering, Kay Sievers
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <grp.h>
30 #include <pwd.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <getopt.h>
35 #include <stdbool.h>
36 #include <time.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <glob.h>
40 #include <fnmatch.h>
41
42 #include "log.h"
43 #include "util.h"
44 #include "strv.h"
45 #include "label.h"
46 #include "set.h"
47
48 /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
49 * them in the file system. This is intended to be used to create
50 * properly owned directories beneath /tmp, /var/tmp, /var/run and
51 * /var/lock which are volatile and hence need to be recreated on
52 * bootup. */
53
54 enum {
55 /* These ones take file names */
56 CREATE_FILE = 'f',
57 TRUNCATE_FILE = 'F',
58 CREATE_DIRECTORY = 'd',
59 TRUNCATE_DIRECTORY = 'D',
60
61 /* These ones take globs */
62 IGNORE_PATH = 'x',
63 REMOVE_PATH = 'r',
64 RECURSIVE_REMOVE_PATH = 'R'
65 };
66
67 typedef struct Item {
68 char type;
69
70 char *path;
71 uid_t uid;
72 gid_t gid;
73 mode_t mode;
74 usec_t age;
75
76 bool uid_set:1;
77 bool gid_set:1;
78 bool mode_set:1;
79 bool age_set:1;
80 } Item;
81
82 static Hashmap *items = NULL, *globs = NULL;
83 static Set *unix_sockets = NULL;
84
85 static bool arg_create = false;
86 static bool arg_clean = false;
87 static bool arg_remove = false;
88
89 static const char *arg_prefix = NULL;
90
91 #define MAX_DEPTH 256
92
93 static bool needs_glob(int t) {
94 return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH;
95 }
96
97 static struct Item* find_glob(Hashmap *h, const char *match) {
98 Item *j;
99 Iterator i;
100
101 HASHMAP_FOREACH(j, h, i)
102 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
103 return j;
104
105 return NULL;
106 }
107
108 static void load_unix_sockets(void) {
109 FILE *f = NULL;
110 char line[LINE_MAX];
111
112 if (unix_sockets)
113 return;
114
115 /* We maintain a cache of the sockets we found in
116 * /proc/net/unix to speed things up a little. */
117
118 if (!(unix_sockets = set_new(string_hash_func, string_compare_func)))
119 return;
120
121 if (!(f = fopen("/proc/net/unix", "re")))
122 return;
123
124 if (!(fgets(line, sizeof(line), f)))
125 goto fail;
126
127 for (;;) {
128 char *p, *s;
129 int k;
130
131 if (!(fgets(line, sizeof(line), f)))
132 break;
133
134 truncate_nl(line);
135
136 if (strlen(line) < 53)
137 continue;
138
139 p = line + 53;
140 p += strspn(p, WHITESPACE);
141 p += strcspn(p, WHITESPACE);
142 p += strspn(p, WHITESPACE);
143
144 if (*p != '/')
145 continue;
146
147 if (!(s = strdup(p)))
148 goto fail;
149
150 path_kill_slashes(s);
151
152 if ((k = set_put(unix_sockets, s)) < 0) {
153 free(s);
154
155 if (k != -EEXIST)
156 goto fail;
157 }
158 }
159
160 return;
161
162 fail:
163 set_free_free(unix_sockets);
164 unix_sockets = NULL;
165
166 if (f)
167 fclose(f);
168 }
169
170 static bool unix_socket_alive(const char *fn) {
171 assert(fn);
172
173 load_unix_sockets();
174
175 if (unix_sockets)
176 return !!set_get(unix_sockets, (char*) fn);
177
178 /* We don't know, so assume yes */
179 return true;
180 }
181
182 static int dir_cleanup(
183 const char *p,
184 DIR *d,
185 const struct stat *ds,
186 usec_t cutoff,
187 dev_t rootdev,
188 bool mountpoint,
189 int maxdepth)
190 {
191 struct dirent *dent;
192 struct timespec times[2];
193 bool deleted = false;
194 char *sub_path = NULL;
195 int r = 0;
196
197 while ((dent = readdir(d))) {
198 struct stat s;
199 usec_t age;
200
201 if (streq(dent->d_name, ".") ||
202 streq(dent->d_name, ".."))
203 continue;
204
205 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
206
207 if (errno != ENOENT) {
208 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
209 r = -errno;
210 }
211
212 continue;
213 }
214
215 /* Stay on the same filesystem */
216 if (s.st_dev != rootdev)
217 continue;
218
219 /* Do not delete read-only files owned by root */
220 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
221 continue;
222
223 free(sub_path);
224 sub_path = NULL;
225
226 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
227 log_error("Out of memory");
228 r = -ENOMEM;
229 goto finish;
230 }
231
232 /* Is there an item configured for this path? */
233 if (hashmap_get(items, sub_path))
234 continue;
235
236 if (find_glob(globs, sub_path))
237 continue;
238
239 if (S_ISDIR(s.st_mode)) {
240
241 if (mountpoint &&
242 streq(dent->d_name, "lost+found") &&
243 s.st_uid == 0)
244 continue;
245
246 if (maxdepth <= 0)
247 log_warning("Reached max depth on %s.", sub_path);
248 else {
249 DIR *sub_dir;
250 int q;
251
252 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
253 if (sub_dir == NULL) {
254 if (errno != ENOENT) {
255 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
256 r = -errno;
257 }
258
259 continue;
260 }
261
262 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
263 closedir(sub_dir);
264
265 if (q < 0)
266 r = q;
267 }
268
269 /* Ignore ctime, we change it when deleting */
270 age = MAX(timespec_load(&s.st_mtim),
271 timespec_load(&s.st_atim));
272 if (age >= cutoff)
273 continue;
274
275 log_debug("rmdir '%s'\n", sub_path);
276
277 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
278 if (errno != ENOENT && errno != ENOTEMPTY) {
279 log_error("rmdir(%s): %m", sub_path);
280 r = -errno;
281 }
282 }
283
284 } else {
285 /* Skip files for which the sticky bit is
286 * set. These are semantics we define, and are
287 * unknown elsewhere. See XDG_RUNTIME_DIR
288 * specification for details. */
289 if (s.st_mode & S_ISVTX)
290 continue;
291
292 if (mountpoint && S_ISREG(s.st_mode)) {
293 if (streq(dent->d_name, ".journal") &&
294 s.st_uid == 0)
295 continue;
296
297 if (streq(dent->d_name, "aquota.user") ||
298 streq(dent->d_name, "aquota.group"))
299 continue;
300 }
301
302 /* Ignore sockets that are listed in /proc/net/unix */
303 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
304 continue;
305
306 /* Ignore device nodes */
307 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
308 continue;
309
310 age = MAX3(timespec_load(&s.st_mtim),
311 timespec_load(&s.st_atim),
312 timespec_load(&s.st_ctim));
313
314 if (age >= cutoff)
315 continue;
316
317 log_debug("unlink '%s'\n", sub_path);
318
319 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
320 if (errno != ENOENT) {
321 log_error("unlink(%s): %m", sub_path);
322 r = -errno;
323 }
324 }
325
326 deleted = true;
327 }
328 }
329
330 finish:
331 if (deleted) {
332 /* Restore original directory timestamps */
333 times[0] = ds->st_atim;
334 times[1] = ds->st_mtim;
335
336 if (futimens(dirfd(d), times) < 0)
337 log_error("utimensat(%s): %m", p);
338 }
339
340 free(sub_path);
341
342 return r;
343 }
344
345 static int clean_item(Item *i) {
346 DIR *d;
347 struct stat s, ps;
348 bool mountpoint;
349 int r;
350 usec_t cutoff, n;
351
352 assert(i);
353
354 if (i->type != CREATE_DIRECTORY &&
355 i->type != TRUNCATE_DIRECTORY &&
356 i->type != IGNORE_PATH)
357 return 0;
358
359 if (!i->age_set || i->age <= 0)
360 return 0;
361
362 n = now(CLOCK_REALTIME);
363 if (n < i->age)
364 return 0;
365
366 cutoff = n - i->age;
367
368 d = opendir(i->path);
369 if (!d) {
370 if (errno == ENOENT)
371 return 0;
372
373 log_error("Failed to open directory %s: %m", i->path);
374 return -errno;
375 }
376
377 if (fstat(dirfd(d), &s) < 0) {
378 log_error("stat(%s) failed: %m", i->path);
379 r = -errno;
380 goto finish;
381 }
382
383 if (!S_ISDIR(s.st_mode)) {
384 log_error("%s is not a directory.", i->path);
385 r = -ENOTDIR;
386 goto finish;
387 }
388
389 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
390 log_error("stat(%s/..) failed: %m", i->path);
391 r = -errno;
392 goto finish;
393 }
394
395 mountpoint = s.st_dev != ps.st_dev ||
396 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
397
398 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
399
400 finish:
401 if (d)
402 closedir(d);
403
404 return r;
405 }
406
407 static int create_item(Item *i) {
408 int fd = -1, r;
409 mode_t u;
410 struct stat st;
411
412 assert(i);
413
414 switch (i->type) {
415
416 case IGNORE_PATH:
417 case REMOVE_PATH:
418 case RECURSIVE_REMOVE_PATH:
419 return 0;
420
421 case CREATE_FILE:
422 case TRUNCATE_FILE:
423
424 u = umask(0);
425 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
426 (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
427 umask(u);
428
429 if (fd < 0) {
430 log_error("Failed to create file %s: %m", i->path);
431 r = -errno;
432 goto finish;
433 }
434
435 if (fstat(fd, &st) < 0) {
436 log_error("stat(%s) failed: %m", i->path);
437 r = -errno;
438 goto finish;
439 }
440
441 if (!S_ISREG(st.st_mode)) {
442 log_error("%s is not a file.", i->path);
443 r = -EEXIST;
444 goto finish;
445 }
446
447 if (i->mode_set)
448 if (fchmod(fd, i->mode) < 0) {
449 log_error("chmod(%s) failed: %m", i->path);
450 r = -errno;
451 goto finish;
452 }
453
454 if (i->uid_set || i->gid_set)
455 if (fchown(fd,
456 i->uid_set ? i->uid : (uid_t) -1,
457 i->gid_set ? i->gid : (gid_t) -1) < 0) {
458 log_error("chown(%s) failed: %m", i->path);
459 r = -errno;
460 goto finish;
461 }
462
463 break;
464
465 case TRUNCATE_DIRECTORY:
466 case CREATE_DIRECTORY:
467
468 u = umask(0);
469 r = mkdir(i->path, i->mode);
470 umask(u);
471
472 if (r < 0 && errno != EEXIST) {
473 log_error("Failed to create directory %s: %m", i->path);
474 r = -errno;
475 goto finish;
476 }
477
478 if (stat(i->path, &st) < 0) {
479 log_error("stat(%s) failed: %m", i->path);
480 r = -errno;
481 goto finish;
482 }
483
484 if (!S_ISDIR(st.st_mode)) {
485 log_error("%s is not a directory.", i->path);
486 r = -EEXIST;
487 goto finish;
488 }
489
490 if (i->mode_set)
491 if (chmod(i->path, i->mode) < 0) {
492 log_error("chmod(%s) failed: %m", i->path);
493 r = -errno;
494 goto finish;
495 }
496
497 if (i->uid_set || i->gid_set)
498 if (chown(i->path,
499 i->uid_set ? i->uid : (uid_t) -1,
500 i->gid_set ? i->gid : (gid_t) -1) < 0) {
501
502 log_error("chown(%s) failed: %m", i->path);
503 r = -errno;
504 goto finish;
505 }
506
507 break;
508 }
509
510 if ((r = label_fix(i->path, false)) < 0)
511 goto finish;
512
513 log_debug("%s created successfully.", i->path);
514
515 finish:
516 if (fd >= 0)
517 close_nointr_nofail(fd);
518
519 return r;
520 }
521
522 static int remove_item(Item *i, const char *instance) {
523 int r;
524
525 assert(i);
526
527 switch (i->type) {
528
529 case CREATE_FILE:
530 case TRUNCATE_FILE:
531 case CREATE_DIRECTORY:
532 case IGNORE_PATH:
533 break;
534
535 case REMOVE_PATH:
536 if (remove(instance) < 0 && errno != ENOENT) {
537 log_error("remove(%s): %m", instance);
538 return -errno;
539 }
540
541 break;
542
543 case TRUNCATE_DIRECTORY:
544 case RECURSIVE_REMOVE_PATH:
545 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
546 r != -ENOENT) {
547 log_error("rm_rf(%s): %s", instance, strerror(-r));
548 return r;
549 }
550
551 break;
552 }
553
554 return 0;
555 }
556
557 static int remove_item_glob(Item *i) {
558 assert(i);
559
560 switch (i->type) {
561
562 case CREATE_FILE:
563 case TRUNCATE_FILE:
564 case CREATE_DIRECTORY:
565 case IGNORE_PATH:
566 break;
567
568 case REMOVE_PATH:
569 case TRUNCATE_DIRECTORY:
570 case RECURSIVE_REMOVE_PATH: {
571 int r = 0, k;
572 glob_t g;
573 char **fn;
574
575 zero(g);
576
577 errno = 0;
578 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
579
580 if (k != GLOB_NOMATCH) {
581 if (errno != 0)
582 errno = EIO;
583
584 log_error("glob(%s) failed: %m", i->path);
585 return -errno;
586 }
587 }
588
589 STRV_FOREACH(fn, g.gl_pathv)
590 if ((k = remove_item(i, *fn)) < 0)
591 r = k;
592
593 globfree(&g);
594 return r;
595 }
596 }
597
598 return 0;
599 }
600
601 static int process_item(Item *i) {
602 int r, q, p;
603
604 assert(i);
605
606 r = arg_create ? create_item(i) : 0;
607 q = arg_remove ? remove_item_glob(i) : 0;
608 p = arg_clean ? clean_item(i) : 0;
609
610 if (r < 0)
611 return r;
612
613 if (q < 0)
614 return q;
615
616 return p;
617 }
618
619 static void item_free(Item *i) {
620 assert(i);
621
622 free(i->path);
623 free(i);
624 }
625
626 static int parse_line(const char *fname, unsigned line, const char *buffer) {
627 Item *i;
628 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
629 int r;
630
631 assert(fname);
632 assert(line >= 1);
633 assert(buffer);
634
635 if (!(i = new0(Item, 1))) {
636 log_error("Out of memory");
637 return -ENOMEM;
638 }
639
640 if (sscanf(buffer,
641 "%c "
642 "%ms "
643 "%ms "
644 "%ms "
645 "%ms "
646 "%ms",
647 &i->type,
648 &i->path,
649 &mode,
650 &user,
651 &group,
652 &age) < 2) {
653 log_error("[%s:%u] Syntax error.", fname, line);
654 r = -EIO;
655 goto finish;
656 }
657
658 if (i->type != CREATE_FILE &&
659 i->type != TRUNCATE_FILE &&
660 i->type != CREATE_DIRECTORY &&
661 i->type != TRUNCATE_DIRECTORY &&
662 i->type != IGNORE_PATH &&
663 i->type != REMOVE_PATH &&
664 i->type != RECURSIVE_REMOVE_PATH) {
665 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
666 r = -EBADMSG;
667 goto finish;
668 }
669
670 if (!path_is_absolute(i->path)) {
671 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
672 r = -EBADMSG;
673 goto finish;
674 }
675
676 path_kill_slashes(i->path);
677
678 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
679 r = 0;
680 goto finish;
681 }
682
683 if (user && !streq(user, "-")) {
684 unsigned long lu;
685 struct passwd *p;
686
687 if (streq(user, "root") || streq(user, "0"))
688 i->uid = 0;
689 else if (safe_atolu(user, &lu) >= 0)
690 i->uid = (uid_t) lu;
691 else if ((p = getpwnam(user)))
692 i->uid = p->pw_uid;
693 else {
694 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
695 r = -ENOENT;
696 goto finish;
697 }
698
699 i->uid_set = true;
700 }
701
702 if (group && !streq(group, "-")) {
703 unsigned long lu;
704 struct group *g;
705
706 if (streq(group, "root") || streq(group, "0"))
707 i->gid = 0;
708 else if (safe_atolu(group, &lu) >= 0)
709 i->gid = (gid_t) lu;
710 else if ((g = getgrnam(group)))
711 i->gid = g->gr_gid;
712 else {
713 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
714 r = -ENOENT;
715 goto finish;
716 }
717
718 i->gid_set = true;
719 }
720
721 if (mode && !streq(mode, "-")) {
722 unsigned m;
723
724 if (sscanf(mode, "%o", &m) != 1) {
725 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
726 r = -ENOENT;
727 goto finish;
728 }
729
730 i->mode = m;
731 i->mode_set = true;
732 } else
733 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
734
735 if (age && !streq(age, "-")) {
736 if (parse_usec(age, &i->age) < 0) {
737 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
738 r = -EBADMSG;
739 goto finish;
740 }
741
742 i->age_set = true;
743 }
744
745 if ((r = hashmap_put(needs_glob(i->type) ? globs : items, i->path, i)) < 0) {
746 if (r == -EEXIST) {
747 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
748 r = 0;
749 goto finish;
750 }
751
752 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
753 goto finish;
754 }
755
756 i = NULL;
757 r = 0;
758
759 finish:
760 free(user);
761 free(group);
762 free(mode);
763 free(age);
764
765 if (i)
766 item_free(i);
767
768 return r;
769 }
770
771 static int scandir_filter(const struct dirent *d) {
772 assert(d);
773
774 if (ignore_file(d->d_name))
775 return 0;
776
777 if (d->d_type != DT_REG &&
778 d->d_type != DT_LNK)
779 return 0;
780
781 return endswith(d->d_name, ".conf");
782 }
783
784 static int help(void) {
785
786 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
787 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
788 " -h --help Show this help\n"
789 " --create Create marked files/directories\n"
790 " --clean Clean up marked directories\n"
791 " --remove Remove marked files/directories\n"
792 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
793 program_invocation_short_name);
794
795 return 0;
796 }
797
798 static int parse_argv(int argc, char *argv[]) {
799
800 enum {
801 ARG_CREATE,
802 ARG_CLEAN,
803 ARG_REMOVE,
804 ARG_PREFIX
805 };
806
807 static const struct option options[] = {
808 { "help", no_argument, NULL, 'h' },
809 { "create", no_argument, NULL, ARG_CREATE },
810 { "clean", no_argument, NULL, ARG_CLEAN },
811 { "remove", no_argument, NULL, ARG_REMOVE },
812 { "prefix", required_argument, NULL, ARG_PREFIX },
813 { NULL, 0, NULL, 0 }
814 };
815
816 int c;
817
818 assert(argc >= 0);
819 assert(argv);
820
821 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
822
823 switch (c) {
824
825 case 'h':
826 help();
827 return 0;
828
829 case ARG_CREATE:
830 arg_create = true;
831 break;
832
833 case ARG_CLEAN:
834 arg_clean = true;
835 break;
836
837 case ARG_REMOVE:
838 arg_remove = true;
839 break;
840
841 case ARG_PREFIX:
842 arg_prefix = optarg;
843 break;
844
845 case '?':
846 return -EINVAL;
847
848 default:
849 log_error("Unknown option code %c", c);
850 return -EINVAL;
851 }
852 }
853
854 if (!arg_clean && !arg_create && !arg_remove) {
855 log_error("You need to specify at least one of --clean, --create or --remove.");
856 return -EINVAL;
857 }
858
859 return 1;
860 }
861
862 static int read_config_file(const char *fn, bool ignore_enoent) {
863 FILE *f;
864 unsigned v = 0;
865 int r = 0;
866
867 assert(fn);
868
869 if (!(f = fopen(fn, "re"))) {
870
871 if (ignore_enoent && errno == ENOENT)
872 return 0;
873
874 log_error("Failed to open %s: %m", fn);
875 return -errno;
876 }
877
878 for (;;) {
879 char line[LINE_MAX], *l;
880 int k;
881
882 if (!(fgets(line, sizeof(line), f)))
883 break;
884
885 v++;
886
887 l = strstrip(line);
888 if (*l == '#' || *l == 0)
889 continue;
890
891 if ((k = parse_line(fn, v, l)) < 0)
892 if (r == 0)
893 r = k;
894 }
895
896 if (ferror(f)) {
897 log_error("Failed to read from file %s: %m", fn);
898 if (r == 0)
899 r = -EIO;
900 }
901
902 fclose(f);
903
904 return r;
905 }
906
907 int main(int argc, char *argv[]) {
908 int r;
909 Item *i;
910 Iterator iterator;
911
912 if ((r = parse_argv(argc, argv)) <= 0)
913 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
914
915 log_set_target(LOG_TARGET_AUTO);
916 log_parse_environment();
917 log_open();
918
919 label_init();
920
921 items = hashmap_new(string_hash_func, string_compare_func);
922 globs = hashmap_new(string_hash_func, string_compare_func);
923
924 if (!items || !globs) {
925 log_error("Out of memory");
926 r = EXIT_FAILURE;
927 goto finish;
928 }
929
930 r = EXIT_SUCCESS;
931
932 if (optind < argc) {
933 int j;
934
935 for (j = optind; j < argc; j++)
936 if (read_config_file(argv[j], false) < 0)
937 r = EXIT_FAILURE;
938
939 } else {
940 int n, j;
941 struct dirent **de = NULL;
942
943 if ((n = scandir("/etc/tmpfiles.d/", &de, scandir_filter, alphasort)) < 0) {
944
945 if (errno != ENOENT) {
946 log_error("Failed to enumerate /etc/tmpfiles.d/ files: %m");
947 r = EXIT_FAILURE;
948 }
949
950 goto finish;
951 }
952
953 for (j = 0; j < n; j++) {
954 int k;
955 char *fn;
956
957 k = asprintf(&fn, "/etc/tmpfiles.d/%s", de[j]->d_name);
958 free(de[j]);
959
960 if (k < 0) {
961 log_error("Failed to allocate file name.");
962 r = EXIT_FAILURE;
963 continue;
964 }
965
966 if (read_config_file(fn, true) < 0)
967 r = EXIT_FAILURE;
968
969 free(fn);
970 }
971
972 free(de);
973 }
974
975 HASHMAP_FOREACH(i, globs, iterator)
976 if (process_item(i) < 0)
977 r = EXIT_FAILURE;
978
979 HASHMAP_FOREACH(i, items, iterator)
980 if (process_item(i) < 0)
981 r = EXIT_FAILURE;
982
983 finish:
984 while ((i = hashmap_steal_first(items)))
985 item_free(i);
986
987 while ((i = hashmap_steal_first(globs)))
988 item_free(i);
989
990 hashmap_free(items);
991 hashmap_free(globs);
992
993 set_free_free(unix_sockets);
994
995 label_finish();
996
997 return r;
998 }