chiark / gitweb /
tmpfiles: use a common function to set owner/group/mode/label
[elogind.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, /run, which are
51  * volatile and hence need to be recreated on bootup. */
52
53 typedef enum ItemType {
54         /* These ones take file names */
55         CREATE_FILE = 'f',
56         TRUNCATE_FILE = 'F',
57         CREATE_DIRECTORY = 'd',
58         TRUNCATE_DIRECTORY = 'D',
59         CREATE_FIFO = 'p',
60
61         /* These ones take globs */
62         IGNORE_PATH = 'x',
63         REMOVE_PATH = 'r',
64         RECURSIVE_REMOVE_PATH = 'R'
65 } ItemType;
66
67 typedef struct Item {
68         ItemType 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(ItemType 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         fclose(f);
161         return;
162
163 fail:
164         set_free_free(unix_sockets);
165         unix_sockets = NULL;
166
167         if (f)
168                 fclose(f);
169 }
170
171 static bool unix_socket_alive(const char *fn) {
172         assert(fn);
173
174         load_unix_sockets();
175
176         if (unix_sockets)
177                 return !!set_get(unix_sockets, (char*) fn);
178
179         /* We don't know, so assume yes */
180         return true;
181 }
182
183 static int dir_cleanup(
184                 const char *p,
185                 DIR *d,
186                 const struct stat *ds,
187                 usec_t cutoff,
188                 dev_t rootdev,
189                 bool mountpoint,
190                 int maxdepth)
191 {
192         struct dirent *dent;
193         struct timespec times[2];
194         bool deleted = false;
195         char *sub_path = NULL;
196         int r = 0;
197
198         while ((dent = readdir(d))) {
199                 struct stat s;
200                 usec_t age;
201
202                 if (streq(dent->d_name, ".") ||
203                     streq(dent->d_name, ".."))
204                         continue;
205
206                 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
207
208                         if (errno != ENOENT) {
209                                 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
210                                 r = -errno;
211                         }
212
213                         continue;
214                 }
215
216                 /* Stay on the same filesystem */
217                 if (s.st_dev != rootdev)
218                         continue;
219
220                 /* Do not delete read-only files owned by root */
221                 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
222                         continue;
223
224                 free(sub_path);
225                 sub_path = NULL;
226
227                 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
228                         log_error("Out of memory");
229                         r = -ENOMEM;
230                         goto finish;
231                 }
232
233                 /* Is there an item configured for this path? */
234                 if (hashmap_get(items, sub_path))
235                         continue;
236
237                 if (find_glob(globs, sub_path))
238                         continue;
239
240                 if (S_ISDIR(s.st_mode)) {
241
242                         if (mountpoint &&
243                             streq(dent->d_name, "lost+found") &&
244                             s.st_uid == 0)
245                                 continue;
246
247                         if (maxdepth <= 0)
248                                 log_warning("Reached max depth on %s.", sub_path);
249                         else {
250                                 DIR *sub_dir;
251                                 int q;
252
253                                 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
254                                 if (sub_dir == NULL) {
255                                         if (errno != ENOENT) {
256                                                 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
257                                                 r = -errno;
258                                         }
259
260                                         continue;
261                                 }
262
263                                 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
264                                 closedir(sub_dir);
265
266                                 if (q < 0)
267                                         r = q;
268                         }
269
270                         /* Ignore ctime, we change it when deleting */
271                         age = MAX(timespec_load(&s.st_mtim),
272                                   timespec_load(&s.st_atim));
273                         if (age >= cutoff)
274                                 continue;
275
276                         log_debug("rmdir '%s'\n", sub_path);
277
278                         if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
279                                 if (errno != ENOENT && errno != ENOTEMPTY) {
280                                         log_error("rmdir(%s): %m", sub_path);
281                                         r = -errno;
282                                 }
283                         }
284
285                 } else {
286                         /* Skip files for which the sticky bit is
287                          * set. These are semantics we define, and are
288                          * unknown elsewhere. See XDG_RUNTIME_DIR
289                          * specification for details. */
290                         if (s.st_mode & S_ISVTX)
291                                 continue;
292
293                         if (mountpoint && S_ISREG(s.st_mode)) {
294                                 if (streq(dent->d_name, ".journal") &&
295                                     s.st_uid == 0)
296                                         continue;
297
298                                 if (streq(dent->d_name, "aquota.user") ||
299                                     streq(dent->d_name, "aquota.group"))
300                                         continue;
301                         }
302
303                         /* Ignore sockets that are listed in /proc/net/unix */
304                         if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
305                                 continue;
306
307                         /* Ignore device nodes */
308                         if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
309                                 continue;
310
311                         age = MAX3(timespec_load(&s.st_mtim),
312                                    timespec_load(&s.st_atim),
313                                    timespec_load(&s.st_ctim));
314
315                         if (age >= cutoff)
316                                 continue;
317
318                         log_debug("unlink '%s'\n", sub_path);
319
320                         if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
321                                 if (errno != ENOENT) {
322                                         log_error("unlink(%s): %m", sub_path);
323                                         r = -errno;
324                                 }
325                         }
326
327                         deleted = true;
328                 }
329         }
330
331 finish:
332         if (deleted) {
333                 /* Restore original directory timestamps */
334                 times[0] = ds->st_atim;
335                 times[1] = ds->st_mtim;
336
337                 if (futimens(dirfd(d), times) < 0)
338                         log_error("utimensat(%s): %m", p);
339         }
340
341         free(sub_path);
342
343         return r;
344 }
345
346 static int clean_item(Item *i) {
347         DIR *d;
348         struct stat s, ps;
349         bool mountpoint;
350         int r;
351         usec_t cutoff, n;
352
353         assert(i);
354
355         if (i->type != CREATE_DIRECTORY &&
356             i->type != TRUNCATE_DIRECTORY &&
357             i->type != IGNORE_PATH)
358                 return 0;
359
360         if (!i->age_set || i->age <= 0)
361                 return 0;
362
363         n = now(CLOCK_REALTIME);
364         if (n < i->age)
365                 return 0;
366
367         cutoff = n - i->age;
368
369         d = opendir(i->path);
370         if (!d) {
371                 if (errno == ENOENT)
372                         return 0;
373
374                 log_error("Failed to open directory %s: %m", i->path);
375                 return -errno;
376         }
377
378         if (fstat(dirfd(d), &s) < 0) {
379                 log_error("stat(%s) failed: %m", i->path);
380                 r = -errno;
381                 goto finish;
382         }
383
384         if (!S_ISDIR(s.st_mode)) {
385                 log_error("%s is not a directory.", i->path);
386                 r = -ENOTDIR;
387                 goto finish;
388         }
389
390         if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
391                 log_error("stat(%s/..) failed: %m", i->path);
392                 r = -errno;
393                 goto finish;
394         }
395
396         mountpoint = s.st_dev != ps.st_dev ||
397                      (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
398
399         r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
400
401 finish:
402         if (d)
403                 closedir(d);
404
405         return r;
406 }
407
408 static int item_set_perms(Item *i) {
409         if (i->mode_set)
410                 if (chmod(i->path, i->mode) < 0) {
411                         log_error("chmod(%s) failed: %m", i->path);
412                         return -errno;
413                 }
414
415         if (i->uid_set || i->gid_set)
416                 if (chown(i->path,
417                           i->uid_set ? i->uid : (uid_t) -1,
418                           i->gid_set ? i->gid : (gid_t) -1) < 0) {
419
420                         log_error("chown(%s) failed: %m", i->path);
421                         return -errno;
422                 }
423
424         return label_fix(i->path, false);
425 }
426
427 static int create_item(Item *i) {
428         int r;
429         mode_t u;
430         struct stat st;
431
432         assert(i);
433
434         switch (i->type) {
435
436         case IGNORE_PATH:
437         case REMOVE_PATH:
438         case RECURSIVE_REMOVE_PATH:
439                 return 0;
440
441         case CREATE_FILE:
442         case TRUNCATE_FILE: {
443                 int fd;
444
445                 u = umask(0);
446                 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
447                           (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
448                 umask(u);
449
450                 if (fd < 0) {
451                         log_error("Failed to create file %s: %m", i->path);
452                         return -errno;
453                 }
454
455                 close_nointr_nofail(fd);
456
457                 if (stat(i->path, &st) < 0) {
458                         log_error("stat(%s) failed: %m", i->path);
459                         return -errno;
460                 }
461
462                 if (!S_ISREG(st.st_mode)) {
463                         log_error("%s is not a file.", i->path);
464                         return -EEXIST;
465                 }
466
467                 r = item_set_perms(i);
468                 if (r < 0)
469                         return r;
470
471                 break;
472         }
473
474         case TRUNCATE_DIRECTORY:
475         case CREATE_DIRECTORY:
476
477                 u = umask(0);
478                 mkdir_parents(i->path, 0755);
479                 r = mkdir(i->path, i->mode);
480                 umask(u);
481
482                 if (r < 0 && errno != EEXIST) {
483                         log_error("Failed to create directory %s: %m", i->path);
484                         return -errno;
485                 }
486
487                 if (stat(i->path, &st) < 0) {
488                         log_error("stat(%s) failed: %m", i->path);
489                         return -errno;
490                 }
491
492                 if (!S_ISDIR(st.st_mode)) {
493                         log_error("%s is not a directory.", i->path);
494                         return -EEXIST;
495                 }
496
497                 r = item_set_perms(i);
498                 if (r < 0)
499                         return r;
500
501                 break;
502
503         case CREATE_FIFO:
504
505                 u = umask(0);
506                 r = mkfifo(i->path, i->mode);
507                 umask(u);
508
509                 if (r < 0 && errno != EEXIST) {
510                         log_error("Failed to create fifo %s: %m", i->path);
511                         return -errno;
512                 }
513
514                 if (stat(i->path, &st) < 0) {
515                         log_error("stat(%s) failed: %m", i->path);
516                         return -errno;
517                 }
518
519                 if (!S_ISFIFO(st.st_mode)) {
520                         log_error("%s is not a fifo.", i->path);
521                         return -EEXIST;
522                 }
523
524                 r = item_set_perms(i);
525                 if (r < 0)
526                         return r;
527
528                 break;
529         }
530
531         log_debug("%s created successfully.", i->path);
532
533         return 0;
534 }
535
536 static int remove_item_instance(Item *i, const char *instance) {
537         int r;
538
539         assert(i);
540
541         switch (i->type) {
542
543         case CREATE_FILE:
544         case TRUNCATE_FILE:
545         case CREATE_DIRECTORY:
546         case CREATE_FIFO:
547         case IGNORE_PATH:
548                 break;
549
550         case REMOVE_PATH:
551                 if (remove(instance) < 0 && errno != ENOENT) {
552                         log_error("remove(%s): %m", instance);
553                         return -errno;
554                 }
555
556                 break;
557
558         case TRUNCATE_DIRECTORY:
559         case RECURSIVE_REMOVE_PATH:
560                 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false)) < 0 &&
561                     r != -ENOENT) {
562                         log_error("rm_rf(%s): %s", instance, strerror(-r));
563                         return r;
564                 }
565
566                 break;
567         }
568
569         return 0;
570 }
571
572 static int remove_item(Item *i) {
573         assert(i);
574
575         switch (i->type) {
576
577         case CREATE_FILE:
578         case TRUNCATE_FILE:
579         case CREATE_DIRECTORY:
580         case CREATE_FIFO:
581         case IGNORE_PATH:
582                 break;
583
584         case REMOVE_PATH:
585         case TRUNCATE_DIRECTORY:
586         case RECURSIVE_REMOVE_PATH: {
587                 int r = 0, k;
588                 glob_t g;
589                 char **fn;
590
591                 zero(g);
592
593                 errno = 0;
594                 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
595
596                         if (k != GLOB_NOMATCH) {
597                                 if (errno != 0)
598                                         errno = EIO;
599
600                                 log_error("glob(%s) failed: %m", i->path);
601                                 return -errno;
602                         }
603                 }
604
605                 STRV_FOREACH(fn, g.gl_pathv)
606                         if ((k = remove_item_instance(i, *fn)) < 0)
607                                 r = k;
608
609                 globfree(&g);
610                 return r;
611         }
612         }
613
614         return 0;
615 }
616
617 static int process_item(Item *i) {
618         int r, q, p;
619
620         assert(i);
621
622         r = arg_create ? create_item(i) : 0;
623         q = arg_remove ? remove_item(i) : 0;
624         p = arg_clean ? clean_item(i) : 0;
625
626         if (r < 0)
627                 return r;
628
629         if (q < 0)
630                 return q;
631
632         return p;
633 }
634
635 static void item_free(Item *i) {
636         assert(i);
637
638         free(i->path);
639         free(i);
640 }
641
642 static bool item_equal(Item *a, Item *b) {
643         assert(a);
644         assert(b);
645
646         if (!streq_ptr(a->path, b->path))
647                 return false;
648
649         if (a->type != b->type)
650                 return false;
651
652         if (a->uid_set != b->uid_set ||
653             (a->uid_set && a->uid != b->uid))
654             return false;
655
656         if (a->gid_set != b->gid_set ||
657             (a->gid_set && a->gid != b->gid))
658             return false;
659
660         if (a->mode_set != b->mode_set ||
661             (a->mode_set && a->mode != b->mode))
662             return false;
663
664         if (a->age_set != b->age_set ||
665             (a->age_set && a->age != b->age))
666             return false;
667
668         return true;
669 }
670
671 static int parse_line(const char *fname, unsigned line, const char *buffer) {
672         Item *i, *existing;
673         char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
674         char type;
675         Hashmap *h;
676         int r;
677
678         assert(fname);
679         assert(line >= 1);
680         assert(buffer);
681
682         if (!(i = new0(Item, 1))) {
683                 log_error("Out of memory");
684                 return -ENOMEM;
685         }
686
687         if (sscanf(buffer,
688                    "%c "
689                    "%ms "
690                    "%ms "
691                    "%ms "
692                    "%ms "
693                    "%ms",
694                    &type,
695                    &i->path,
696                    &mode,
697                    &user,
698                    &group,
699                    &age) < 2) {
700                 log_error("[%s:%u] Syntax error.", fname, line);
701                 r = -EIO;
702                 goto finish;
703         }
704         i->type = type;
705
706         if (i->type != CREATE_FILE &&
707             i->type != TRUNCATE_FILE &&
708             i->type != CREATE_DIRECTORY &&
709             i->type != TRUNCATE_DIRECTORY &&
710             i->type != CREATE_FIFO &&
711             i->type != IGNORE_PATH &&
712             i->type != REMOVE_PATH &&
713             i->type != RECURSIVE_REMOVE_PATH) {
714                 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
715                 r = -EBADMSG;
716                 goto finish;
717         }
718
719         if (!path_is_absolute(i->path)) {
720                 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
721                 r = -EBADMSG;
722                 goto finish;
723         }
724
725         path_kill_slashes(i->path);
726
727         if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
728                 r = 0;
729                 goto finish;
730         }
731
732         if (user && !streq(user, "-")) {
733                 const char *u = user;
734
735                 r = get_user_creds(&u, &i->uid, NULL, NULL);
736                 if (r < 0) {
737                         log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
738                         goto finish;
739                 }
740
741                 i->uid_set = true;
742         }
743
744         if (group && !streq(group, "-")) {
745                 const char *g = group;
746
747                 r = get_group_creds(&g, &i->gid);
748                 if (r < 0) {
749                         log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
750                         goto finish;
751                 }
752
753                 i->gid_set = true;
754         }
755
756         if (mode && !streq(mode, "-")) {
757                 unsigned m;
758
759                 if (sscanf(mode, "%o", &m) != 1) {
760                         log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
761                         r = -ENOENT;
762                         goto finish;
763                 }
764
765                 i->mode = m;
766                 i->mode_set = true;
767         } else
768                 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
769
770         if (age && !streq(age, "-")) {
771                 if (parse_usec(age, &i->age) < 0) {
772                         log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
773                         r = -EBADMSG;
774                         goto finish;
775                 }
776
777                 i->age_set = true;
778         }
779
780         h = needs_glob(i->type) ? globs : items;
781
782         if ((existing = hashmap_get(h, i->path))) {
783
784                 /* Two identical items are fine */
785                 if (!item_equal(existing, i))
786                         log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
787
788                 r = 0;
789                 goto finish;
790         }
791
792         if ((r = hashmap_put(h, i->path, i)) < 0) {
793                 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
794                 goto finish;
795         }
796
797         i = NULL;
798         r = 0;
799
800 finish:
801         free(user);
802         free(group);
803         free(mode);
804         free(age);
805
806         if (i)
807                 item_free(i);
808
809         return r;
810 }
811
812 static int help(void) {
813
814         printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
815                "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
816                "  -h --help             Show this help\n"
817                "     --create           Create marked files/directories\n"
818                "     --clean            Clean up marked directories\n"
819                "     --remove           Remove marked files/directories\n"
820                "     --prefix=PATH      Only apply rules that apply to paths with the specified prefix\n",
821                program_invocation_short_name);
822
823         return 0;
824 }
825
826 static int parse_argv(int argc, char *argv[]) {
827
828         enum {
829                 ARG_CREATE,
830                 ARG_CLEAN,
831                 ARG_REMOVE,
832                 ARG_PREFIX
833         };
834
835         static const struct option options[] = {
836                 { "help",      no_argument,       NULL, 'h'           },
837                 { "create",    no_argument,       NULL, ARG_CREATE    },
838                 { "clean",     no_argument,       NULL, ARG_CLEAN     },
839                 { "remove",    no_argument,       NULL, ARG_REMOVE    },
840                 { "prefix",    required_argument, NULL, ARG_PREFIX    },
841                 { NULL,        0,                 NULL, 0             }
842         };
843
844         int c;
845
846         assert(argc >= 0);
847         assert(argv);
848
849         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
850
851                 switch (c) {
852
853                 case 'h':
854                         help();
855                         return 0;
856
857                 case ARG_CREATE:
858                         arg_create = true;
859                         break;
860
861                 case ARG_CLEAN:
862                         arg_clean = true;
863                         break;
864
865                 case ARG_REMOVE:
866                         arg_remove = true;
867                         break;
868
869                 case ARG_PREFIX:
870                         arg_prefix = optarg;
871                         break;
872
873                 case '?':
874                         return -EINVAL;
875
876                 default:
877                         log_error("Unknown option code %c", c);
878                         return -EINVAL;
879                 }
880         }
881
882         if (!arg_clean && !arg_create && !arg_remove) {
883                 log_error("You need to specify at least one of --clean, --create or --remove.");
884                 return -EINVAL;
885         }
886
887         return 1;
888 }
889
890 static int read_config_file(const char *fn, bool ignore_enoent) {
891         FILE *f;
892         unsigned v = 0;
893         int r = 0;
894
895         assert(fn);
896
897         if (!(f = fopen(fn, "re"))) {
898
899                 if (ignore_enoent && errno == ENOENT)
900                         return 0;
901
902                 log_error("Failed to open %s: %m", fn);
903                 return -errno;
904         }
905
906         log_debug("apply: %s\n", fn);
907         for (;;) {
908                 char line[LINE_MAX], *l;
909                 int k;
910
911                 if (!(fgets(line, sizeof(line), f)))
912                         break;
913
914                 v++;
915
916                 l = strstrip(line);
917                 if (*l == '#' || *l == 0)
918                         continue;
919
920                 if ((k = parse_line(fn, v, l)) < 0)
921                         if (r == 0)
922                                 r = k;
923         }
924
925         if (ferror(f)) {
926                 log_error("Failed to read from file %s: %m", fn);
927                 if (r == 0)
928                         r = -EIO;
929         }
930
931         fclose(f);
932
933         return r;
934 }
935
936 int main(int argc, char *argv[]) {
937         int r;
938         Item *i;
939         Iterator iterator;
940
941         if ((r = parse_argv(argc, argv)) <= 0)
942                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
943
944         log_set_target(LOG_TARGET_AUTO);
945         log_parse_environment();
946         log_open();
947
948         umask(0022);
949
950         label_init();
951
952         items = hashmap_new(string_hash_func, string_compare_func);
953         globs = hashmap_new(string_hash_func, string_compare_func);
954
955         if (!items || !globs) {
956                 log_error("Out of memory");
957                 r = EXIT_FAILURE;
958                 goto finish;
959         }
960
961         r = EXIT_SUCCESS;
962
963         if (optind < argc) {
964                 int j;
965
966                 for (j = optind; j < argc; j++)
967                         if (read_config_file(argv[j], false) < 0)
968                                 r = EXIT_FAILURE;
969
970         } else {
971                 char **files, **f;
972
973                 r = conf_files_list(&files, ".conf",
974                                     "/run/tmpfiles.d",
975                                     "/etc/tmpfiles.d",
976                                     "/usr/local/lib/tmpfiles.d",
977                                     "/usr/lib/tmpfiles.d",
978                                     NULL);
979                 if (r < 0) {
980                         r = EXIT_FAILURE;
981                         log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
982                         goto finish;
983                 }
984
985                 STRV_FOREACH(f, files) {
986                         if (read_config_file(*f, true) < 0)
987                                 r = EXIT_FAILURE;
988                 }
989
990                 strv_free(files);
991         }
992
993         HASHMAP_FOREACH(i, globs, iterator)
994                 process_item(i);
995
996         HASHMAP_FOREACH(i, items, iterator)
997                 process_item(i);
998
999 finish:
1000         while ((i = hashmap_steal_first(items)))
1001                 item_free(i);
1002
1003         while ((i = hashmap_steal_first(globs)))
1004                 item_free(i);
1005
1006         hashmap_free(items);
1007         hashmap_free(globs);
1008
1009         set_free_free(unix_sockets);
1010
1011         label_finish();
1012
1013         return r;
1014 }