chiark / gitweb /
logind: add more necessary caps to the service
[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 enum {
54         /* These ones take file names */
55         CREATE_FILE = 'f',
56         TRUNCATE_FILE = 'F',
57         CREATE_DIRECTORY = 'd',
58         TRUNCATE_DIRECTORY = 'D',
59
60         /* These ones take globs */
61         IGNORE_PATH = 'x',
62         REMOVE_PATH = 'r',
63         RECURSIVE_REMOVE_PATH = 'R'
64 };
65
66 typedef struct Item {
67         char type;
68
69         char *path;
70         uid_t uid;
71         gid_t gid;
72         mode_t mode;
73         usec_t age;
74
75         bool uid_set:1;
76         bool gid_set:1;
77         bool mode_set:1;
78         bool age_set:1;
79 } Item;
80
81 static Hashmap *items = NULL, *globs = NULL;
82 static Set *unix_sockets = NULL;
83
84 static bool arg_create = false;
85 static bool arg_clean = false;
86 static bool arg_remove = false;
87
88 static const char *arg_prefix = NULL;
89
90 #define MAX_DEPTH 256
91
92 static bool needs_glob(int t) {
93         return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH;
94 }
95
96 static struct Item* find_glob(Hashmap *h, const char *match) {
97         Item *j;
98         Iterator i;
99
100         HASHMAP_FOREACH(j, h, i)
101                 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
102                         return j;
103
104         return NULL;
105 }
106
107 static void load_unix_sockets(void) {
108         FILE *f = NULL;
109         char line[LINE_MAX];
110
111         if (unix_sockets)
112                 return;
113
114         /* We maintain a cache of the sockets we found in
115          * /proc/net/unix to speed things up a little. */
116
117         if (!(unix_sockets = set_new(string_hash_func, string_compare_func)))
118                 return;
119
120         if (!(f = fopen("/proc/net/unix", "re")))
121                 return;
122
123         if (!(fgets(line, sizeof(line), f)))
124                 goto fail;
125
126         for (;;) {
127                 char *p, *s;
128                 int k;
129
130                 if (!(fgets(line, sizeof(line), f)))
131                         break;
132
133                 truncate_nl(line);
134
135                 if (strlen(line) < 53)
136                         continue;
137
138                 p = line + 53;
139                 p += strspn(p, WHITESPACE);
140                 p += strcspn(p, WHITESPACE);
141                 p += strspn(p, WHITESPACE);
142
143                 if (*p != '/')
144                         continue;
145
146                 if (!(s = strdup(p)))
147                         goto fail;
148
149                 path_kill_slashes(s);
150
151                 if ((k = set_put(unix_sockets, s)) < 0) {
152                         free(s);
153
154                         if (k != -EEXIST)
155                                 goto fail;
156                 }
157         }
158
159         return;
160
161 fail:
162         set_free_free(unix_sockets);
163         unix_sockets = NULL;
164
165         if (f)
166                 fclose(f);
167 }
168
169 static bool unix_socket_alive(const char *fn) {
170         assert(fn);
171
172         load_unix_sockets();
173
174         if (unix_sockets)
175                 return !!set_get(unix_sockets, (char*) fn);
176
177         /* We don't know, so assume yes */
178         return true;
179 }
180
181 static int dir_cleanup(
182                 const char *p,
183                 DIR *d,
184                 const struct stat *ds,
185                 usec_t cutoff,
186                 dev_t rootdev,
187                 bool mountpoint,
188                 int maxdepth)
189 {
190         struct dirent *dent;
191         struct timespec times[2];
192         bool deleted = false;
193         char *sub_path = NULL;
194         int r = 0;
195
196         while ((dent = readdir(d))) {
197                 struct stat s;
198                 usec_t age;
199
200                 if (streq(dent->d_name, ".") ||
201                     streq(dent->d_name, ".."))
202                         continue;
203
204                 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
205
206                         if (errno != ENOENT) {
207                                 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
208                                 r = -errno;
209                         }
210
211                         continue;
212                 }
213
214                 /* Stay on the same filesystem */
215                 if (s.st_dev != rootdev)
216                         continue;
217
218                 /* Do not delete read-only files owned by root */
219                 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
220                         continue;
221
222                 free(sub_path);
223                 sub_path = NULL;
224
225                 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
226                         log_error("Out of memory");
227                         r = -ENOMEM;
228                         goto finish;
229                 }
230
231                 /* Is there an item configured for this path? */
232                 if (hashmap_get(items, sub_path))
233                         continue;
234
235                 if (find_glob(globs, sub_path))
236                         continue;
237
238                 if (S_ISDIR(s.st_mode)) {
239
240                         if (mountpoint &&
241                             streq(dent->d_name, "lost+found") &&
242                             s.st_uid == 0)
243                                 continue;
244
245                         if (maxdepth <= 0)
246                                 log_warning("Reached max depth on %s.", sub_path);
247                         else {
248                                 DIR *sub_dir;
249                                 int q;
250
251                                 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
252                                 if (sub_dir == NULL) {
253                                         if (errno != ENOENT) {
254                                                 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
255                                                 r = -errno;
256                                         }
257
258                                         continue;
259                                 }
260
261                                 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
262                                 closedir(sub_dir);
263
264                                 if (q < 0)
265                                         r = q;
266                         }
267
268                         /* Ignore ctime, we change it when deleting */
269                         age = MAX(timespec_load(&s.st_mtim),
270                                   timespec_load(&s.st_atim));
271                         if (age >= cutoff)
272                                 continue;
273
274                         log_debug("rmdir '%s'\n", sub_path);
275
276                         if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
277                                 if (errno != ENOENT && errno != ENOTEMPTY) {
278                                         log_error("rmdir(%s): %m", sub_path);
279                                         r = -errno;
280                                 }
281                         }
282
283                 } else {
284                         /* Skip files for which the sticky bit is
285                          * set. These are semantics we define, and are
286                          * unknown elsewhere. See XDG_RUNTIME_DIR
287                          * specification for details. */
288                         if (s.st_mode & S_ISVTX)
289                                 continue;
290
291                         if (mountpoint && S_ISREG(s.st_mode)) {
292                                 if (streq(dent->d_name, ".journal") &&
293                                     s.st_uid == 0)
294                                         continue;
295
296                                 if (streq(dent->d_name, "aquota.user") ||
297                                     streq(dent->d_name, "aquota.group"))
298                                         continue;
299                         }
300
301                         /* Ignore sockets that are listed in /proc/net/unix */
302                         if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
303                                 continue;
304
305                         /* Ignore device nodes */
306                         if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
307                                 continue;
308
309                         age = MAX3(timespec_load(&s.st_mtim),
310                                    timespec_load(&s.st_atim),
311                                    timespec_load(&s.st_ctim));
312
313                         if (age >= cutoff)
314                                 continue;
315
316                         log_debug("unlink '%s'\n", sub_path);
317
318                         if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
319                                 if (errno != ENOENT) {
320                                         log_error("unlink(%s): %m", sub_path);
321                                         r = -errno;
322                                 }
323                         }
324
325                         deleted = true;
326                 }
327         }
328
329 finish:
330         if (deleted) {
331                 /* Restore original directory timestamps */
332                 times[0] = ds->st_atim;
333                 times[1] = ds->st_mtim;
334
335                 if (futimens(dirfd(d), times) < 0)
336                         log_error("utimensat(%s): %m", p);
337         }
338
339         free(sub_path);
340
341         return r;
342 }
343
344 static int clean_item(Item *i) {
345         DIR *d;
346         struct stat s, ps;
347         bool mountpoint;
348         int r;
349         usec_t cutoff, n;
350
351         assert(i);
352
353         if (i->type != CREATE_DIRECTORY &&
354             i->type != TRUNCATE_DIRECTORY &&
355             i->type != IGNORE_PATH)
356                 return 0;
357
358         if (!i->age_set || i->age <= 0)
359                 return 0;
360
361         n = now(CLOCK_REALTIME);
362         if (n < i->age)
363                 return 0;
364
365         cutoff = n - i->age;
366
367         d = opendir(i->path);
368         if (!d) {
369                 if (errno == ENOENT)
370                         return 0;
371
372                 log_error("Failed to open directory %s: %m", i->path);
373                 return -errno;
374         }
375
376         if (fstat(dirfd(d), &s) < 0) {
377                 log_error("stat(%s) failed: %m", i->path);
378                 r = -errno;
379                 goto finish;
380         }
381
382         if (!S_ISDIR(s.st_mode)) {
383                 log_error("%s is not a directory.", i->path);
384                 r = -ENOTDIR;
385                 goto finish;
386         }
387
388         if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
389                 log_error("stat(%s/..) failed: %m", i->path);
390                 r = -errno;
391                 goto finish;
392         }
393
394         mountpoint = s.st_dev != ps.st_dev ||
395                      (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
396
397         r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
398
399 finish:
400         if (d)
401                 closedir(d);
402
403         return r;
404 }
405
406 static int create_item(Item *i) {
407         int fd = -1, r;
408         mode_t u;
409         struct stat st;
410
411         assert(i);
412
413         switch (i->type) {
414
415         case IGNORE_PATH:
416         case REMOVE_PATH:
417         case RECURSIVE_REMOVE_PATH:
418                 return 0;
419
420         case CREATE_FILE:
421         case TRUNCATE_FILE:
422
423                 u = umask(0);
424                 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
425                           (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
426                 umask(u);
427
428                 if (fd < 0) {
429                         log_error("Failed to create file %s: %m", i->path);
430                         r = -errno;
431                         goto finish;
432                 }
433
434                 if (fstat(fd, &st) < 0) {
435                         log_error("stat(%s) failed: %m", i->path);
436                         r = -errno;
437                         goto finish;
438                 }
439
440                 if (!S_ISREG(st.st_mode)) {
441                         log_error("%s is not a file.", i->path);
442                         r = -EEXIST;
443                         goto finish;
444                 }
445
446                 if (i->mode_set)
447                         if (fchmod(fd, i->mode) < 0) {
448                                 log_error("chmod(%s) failed: %m", i->path);
449                                 r = -errno;
450                                 goto finish;
451                         }
452
453                 if (i->uid_set || i->gid_set)
454                         if (fchown(fd,
455                                    i->uid_set ? i->uid : (uid_t) -1,
456                                    i->gid_set ? i->gid : (gid_t) -1) < 0) {
457                                 log_error("chown(%s) failed: %m", i->path);
458                                 r = -errno;
459                                 goto finish;
460                         }
461
462                 break;
463
464         case TRUNCATE_DIRECTORY:
465         case CREATE_DIRECTORY:
466
467                 u = umask(0);
468                 mkdir_parents(i->path, 0755);
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 bool item_equal(Item *a, Item *b) {
627         assert(a);
628         assert(b);
629
630         if (!streq_ptr(a->path, b->path))
631                 return false;
632
633         if (a->type != b->type)
634                 return false;
635
636         if (a->uid_set != b->uid_set ||
637             (a->uid_set && a->uid != b->uid))
638             return false;
639
640         if (a->gid_set != b->gid_set ||
641             (a->gid_set && a->gid != b->gid))
642             return false;
643
644         if (a->mode_set != b->mode_set ||
645             (a->mode_set && a->mode != b->mode))
646             return false;
647
648         if (a->age_set != b->age_set ||
649             (a->age_set && a->age != b->age))
650             return false;
651
652         return true;
653 }
654
655 static int parse_line(const char *fname, unsigned line, const char *buffer) {
656         Item *i, *existing;
657         char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
658         Hashmap *h;
659         int r;
660
661         assert(fname);
662         assert(line >= 1);
663         assert(buffer);
664
665         if (!(i = new0(Item, 1))) {
666                 log_error("Out of memory");
667                 return -ENOMEM;
668         }
669
670         if (sscanf(buffer,
671                    "%c "
672                    "%ms "
673                    "%ms "
674                    "%ms "
675                    "%ms "
676                    "%ms",
677                    &i->type,
678                    &i->path,
679                    &mode,
680                    &user,
681                    &group,
682                    &age) < 2) {
683                 log_error("[%s:%u] Syntax error.", fname, line);
684                 r = -EIO;
685                 goto finish;
686         }
687
688         if (i->type != CREATE_FILE &&
689             i->type != TRUNCATE_FILE &&
690             i->type != CREATE_DIRECTORY &&
691             i->type != TRUNCATE_DIRECTORY &&
692             i->type != IGNORE_PATH &&
693             i->type != REMOVE_PATH &&
694             i->type != RECURSIVE_REMOVE_PATH) {
695                 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
696                 r = -EBADMSG;
697                 goto finish;
698         }
699
700         if (!path_is_absolute(i->path)) {
701                 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
702                 r = -EBADMSG;
703                 goto finish;
704         }
705
706         path_kill_slashes(i->path);
707
708         if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
709                 r = 0;
710                 goto finish;
711         }
712
713         if (user && !streq(user, "-")) {
714                 unsigned long lu;
715                 struct passwd *p;
716
717                 if (streq(user, "root") || streq(user, "0"))
718                         i->uid = 0;
719                 else if (safe_atolu(user, &lu) >= 0)
720                         i->uid = (uid_t) lu;
721                 else if ((p = getpwnam(user)))
722                         i->uid = p->pw_uid;
723                 else {
724                         log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
725                         r = -ENOENT;
726                         goto finish;
727                 }
728
729                 i->uid_set = true;
730         }
731
732         if (group && !streq(group, "-")) {
733                 unsigned long lu;
734                 struct group *g;
735
736                 if (streq(group, "root") || streq(group, "0"))
737                         i->gid = 0;
738                 else if (safe_atolu(group, &lu) >= 0)
739                         i->gid = (gid_t) lu;
740                 else if ((g = getgrnam(group)))
741                         i->gid = g->gr_gid;
742                 else {
743                         log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
744                         r = -ENOENT;
745                         goto finish;
746                 }
747
748                 i->gid_set = true;
749         }
750
751         if (mode && !streq(mode, "-")) {
752                 unsigned m;
753
754                 if (sscanf(mode, "%o", &m) != 1) {
755                         log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
756                         r = -ENOENT;
757                         goto finish;
758                 }
759
760                 i->mode = m;
761                 i->mode_set = true;
762         } else
763                 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
764
765         if (age && !streq(age, "-")) {
766                 if (parse_usec(age, &i->age) < 0) {
767                         log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
768                         r = -EBADMSG;
769                         goto finish;
770                 }
771
772                 i->age_set = true;
773         }
774
775         h = needs_glob(i->type) ? globs : items;
776
777         if ((existing = hashmap_get(h, i->path))) {
778
779                 /* Two identical items are fine */
780                 if (!item_equal(existing, i))
781                         log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
782
783                 r = 0;
784                 goto finish;
785         }
786
787         if ((r = hashmap_put(h, i->path, i)) < 0) {
788                 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
789                 goto finish;
790         }
791
792         i = NULL;
793         r = 0;
794
795 finish:
796         free(user);
797         free(group);
798         free(mode);
799         free(age);
800
801         if (i)
802                 item_free(i);
803
804         return r;
805 }
806
807 static int help(void) {
808
809         printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
810                "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
811                "  -h --help             Show this help\n"
812                "     --create           Create marked files/directories\n"
813                "     --clean            Clean up marked directories\n"
814                "     --remove           Remove marked files/directories\n"
815                "     --prefix=PATH      Only apply rules that apply to paths with the specified prefix\n",
816                program_invocation_short_name);
817
818         return 0;
819 }
820
821 static int parse_argv(int argc, char *argv[]) {
822
823         enum {
824                 ARG_CREATE,
825                 ARG_CLEAN,
826                 ARG_REMOVE,
827                 ARG_PREFIX
828         };
829
830         static const struct option options[] = {
831                 { "help",      no_argument,       NULL, 'h'           },
832                 { "create",    no_argument,       NULL, ARG_CREATE    },
833                 { "clean",     no_argument,       NULL, ARG_CLEAN     },
834                 { "remove",    no_argument,       NULL, ARG_REMOVE    },
835                 { "prefix",    required_argument, NULL, ARG_PREFIX    },
836                 { NULL,        0,                 NULL, 0             }
837         };
838
839         int c;
840
841         assert(argc >= 0);
842         assert(argv);
843
844         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
845
846                 switch (c) {
847
848                 case 'h':
849                         help();
850                         return 0;
851
852                 case ARG_CREATE:
853                         arg_create = true;
854                         break;
855
856                 case ARG_CLEAN:
857                         arg_clean = true;
858                         break;
859
860                 case ARG_REMOVE:
861                         arg_remove = true;
862                         break;
863
864                 case ARG_PREFIX:
865                         arg_prefix = optarg;
866                         break;
867
868                 case '?':
869                         return -EINVAL;
870
871                 default:
872                         log_error("Unknown option code %c", c);
873                         return -EINVAL;
874                 }
875         }
876
877         if (!arg_clean && !arg_create && !arg_remove) {
878                 log_error("You need to specify at least one of --clean, --create or --remove.");
879                 return -EINVAL;
880         }
881
882         return 1;
883 }
884
885 static int read_config_file(const char *fn, bool ignore_enoent) {
886         FILE *f;
887         unsigned v = 0;
888         int r = 0;
889
890         assert(fn);
891
892         if (!(f = fopen(fn, "re"))) {
893
894                 if (ignore_enoent && errno == ENOENT)
895                         return 0;
896
897                 log_error("Failed to open %s: %m", fn);
898                 return -errno;
899         }
900
901         log_debug("apply: %s\n", fn);
902         for (;;) {
903                 char line[LINE_MAX], *l;
904                 int k;
905
906                 if (!(fgets(line, sizeof(line), f)))
907                         break;
908
909                 v++;
910
911                 l = strstrip(line);
912                 if (*l == '#' || *l == 0)
913                         continue;
914
915                 if ((k = parse_line(fn, v, l)) < 0)
916                         if (r == 0)
917                                 r = k;
918         }
919
920         if (ferror(f)) {
921                 log_error("Failed to read from file %s: %m", fn);
922                 if (r == 0)
923                         r = -EIO;
924         }
925
926         fclose(f);
927
928         return r;
929 }
930
931 int main(int argc, char *argv[]) {
932         int r;
933         Item *i;
934         Iterator iterator;
935
936         if ((r = parse_argv(argc, argv)) <= 0)
937                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
938
939         log_set_target(LOG_TARGET_AUTO);
940         log_parse_environment();
941         log_open();
942
943         label_init();
944
945         items = hashmap_new(string_hash_func, string_compare_func);
946         globs = hashmap_new(string_hash_func, string_compare_func);
947
948         if (!items || !globs) {
949                 log_error("Out of memory");
950                 r = EXIT_FAILURE;
951                 goto finish;
952         }
953
954         r = EXIT_SUCCESS;
955
956         if (optind < argc) {
957                 int j;
958
959                 for (j = optind; j < argc; j++)
960                         if (read_config_file(argv[j], false) < 0)
961                                 r = EXIT_FAILURE;
962
963         } else {
964                 char **files, **f;
965
966                 r = conf_files_list(&files, ".conf",
967                                     "/run/tmpfiles.d",
968                                     "/etc/tmpfiles.d",
969                                     "/usr/local/lib/tmpfiles.d",
970                                     "/usr/lib/tmpfiles.d",
971                                     NULL);
972                 if (r < 0) {
973                         r = EXIT_FAILURE;
974                         log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
975                         goto finish;
976                 }
977
978                 STRV_FOREACH(f, files) {
979                         if (read_config_file(*f, true) < 0)
980                                 r = EXIT_FAILURE;
981                 }
982
983                 strv_free(files);
984         }
985
986
987
988         HASHMAP_FOREACH(i, globs, iterator)
989                 if (process_item(i) < 0)
990                         r = EXIT_FAILURE;
991
992         HASHMAP_FOREACH(i, items, iterator)
993                 if (process_item(i) < 0)
994                         r = EXIT_FAILURE;
995
996 finish:
997         while ((i = hashmap_steal_first(items)))
998                 item_free(i);
999
1000         while ((i = hashmap_steal_first(globs)))
1001                 item_free(i);
1002
1003         hashmap_free(items);
1004         hashmap_free(globs);
1005
1006         set_free_free(unix_sockets);
1007
1008         label_finish();
1009
1010         return r;
1011 }