]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/tmpfiles.c
bash: fix typo
[thirdparty/systemd.git] / src / tmpfiles.c
CommitLineData
5008d581
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
3b63d2d3 6 Copyright 2010 Lennart Poettering, Kay Sievers
5008d581
LP
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>
3b63d2d3
LP
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>
b8bb3e8f
LP
39#include <glob.h>
40#include <fnmatch.h>
5008d581
LP
41
42#include "log.h"
43#include "util.h"
44#include "strv.h"
45#include "label.h"
3b63d2d3 46#include "set.h"
5008d581 47
01000479 48/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
5008d581 49 * them in the file system. This is intended to be used to create
db019b8d
KS
50 * properly owned directories beneath /tmp, /var/tmp, /run, which are
51 * volatile and hence need to be recreated on bootup. */
5008d581 52
3b63d2d3 53enum {
b8bb3e8f 54 /* These ones take file names */
3b63d2d3
LP
55 CREATE_FILE = 'f',
56 TRUNCATE_FILE = 'F',
57 CREATE_DIRECTORY = 'd',
58 TRUNCATE_DIRECTORY = 'D',
b8bb3e8f
LP
59
60 /* These ones take globs */
3b63d2d3
LP
61 IGNORE_PATH = 'x',
62 REMOVE_PATH = 'r',
63 RECURSIVE_REMOVE_PATH = 'R'
64};
65
66typedef struct Item {
5008d581 67 char type;
3b63d2d3
LP
68
69 char *path;
5008d581
LP
70 uid_t uid;
71 gid_t gid;
3b63d2d3
LP
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
b8bb3e8f 81static Hashmap *items = NULL, *globs = NULL;
17b90525 82static Set *unix_sockets = NULL;
3b63d2d3
LP
83
84static bool arg_create = false;
85static bool arg_clean = false;
86static bool arg_remove = false;
87
fba6e687
LP
88static const char *arg_prefix = NULL;
89
3b63d2d3
LP
90#define MAX_DEPTH 256
91
b8bb3e8f
LP
92static bool needs_glob(int t) {
93 return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH;
94}
95
96static 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
17b90525
LP
107static 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
4ff21d85
LP
149 path_kill_slashes(s);
150
17b90525
LP
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
161fail:
162 set_free_free(unix_sockets);
163 unix_sockets = NULL;
164
165 if (f)
166 fclose(f);
167}
168
169static 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
3b63d2d3
LP
181static 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;
5008d581 203
3b63d2d3 204 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
5008d581 205
3b63d2d3
LP
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
b8bb3e8f
LP
235 if (find_glob(globs, sub_path))
236 continue;
237
3b63d2d3
LP
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
a247755d 251 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
3b63d2d3
LP
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 {
9c73736d
LP
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
17b90525 291 if (mountpoint && S_ISREG(s.st_mode)) {
3b63d2d3
LP
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
17b90525
LP
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
78ab08eb
LP
305 /* Ignore device nodes */
306 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
307 continue;
308
3b63d2d3
LP
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
329finish:
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
344static 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;
5008d581
LP
379 goto finish;
380 }
381
3b63d2d3
LP
382 if (!S_ISDIR(s.st_mode)) {
383 log_error("%s is not a directory.", i->path);
384 r = -ENOTDIR;
5008d581
LP
385 goto finish;
386 }
387
3b63d2d3
LP
388 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
389 log_error("stat(%s/..) failed: %m", i->path);
390 r = -errno;
5008d581 391 goto finish;
4aa8b15b 392 }
5008d581 393
3b63d2d3
LP
394 mountpoint = s.st_dev != ps.st_dev ||
395 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
5008d581 396
3b63d2d3 397 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
5008d581 398
3b63d2d3
LP
399finish:
400 if (d)
401 closedir(d);
5008d581 402
3b63d2d3
LP
403 return r;
404}
5008d581 405
3b63d2d3
LP
406static int create_item(Item *i) {
407 int fd = -1, r;
408 mode_t u;
409 struct stat st;
5008d581 410
3b63d2d3 411 assert(i);
5008d581 412
3b63d2d3
LP
413 switch (i->type) {
414
415 case IGNORE_PATH:
416 case REMOVE_PATH:
417 case RECURSIVE_REMOVE_PATH:
418 return 0;
5008d581 419
3b63d2d3
LP
420 case CREATE_FILE:
421 case TRUNCATE_FILE:
5008d581
LP
422
423 u = umask(0);
3b63d2d3
LP
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);
5008d581
LP
426 umask(u);
427
428 if (fd < 0) {
3b63d2d3 429 log_error("Failed to create file %s: %m", i->path);
4aa8b15b 430 r = -errno;
5008d581
LP
431 goto finish;
432 }
433
434 if (fstat(fd, &st) < 0) {
3b63d2d3 435 log_error("stat(%s) failed: %m", i->path);
4aa8b15b 436 r = -errno;
5008d581
LP
437 goto finish;
438 }
439
440 if (!S_ISREG(st.st_mode)) {
3b63d2d3 441 log_error("%s is not a file.", i->path);
4aa8b15b 442 r = -EEXIST;
5008d581
LP
443 goto finish;
444 }
445
3b63d2d3
LP
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 }
5008d581 452
3b63d2d3 453 if (i->uid_set || i->gid_set)
5008d581 454 if (fchown(fd,
3b63d2d3
LP
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);
4aa8b15b 458 r = -errno;
5008d581
LP
459 goto finish;
460 }
5008d581 461
3b63d2d3
LP
462 break;
463
464 case TRUNCATE_DIRECTORY:
465 case CREATE_DIRECTORY:
5008d581
LP
466
467 u = umask(0);
33366862 468 mkdir_parents(i->path, 0755);
3b63d2d3 469 r = mkdir(i->path, i->mode);
5008d581
LP
470 umask(u);
471
472 if (r < 0 && errno != EEXIST) {
3b63d2d3 473 log_error("Failed to create directory %s: %m", i->path);
4aa8b15b 474 r = -errno;
5008d581
LP
475 goto finish;
476 }
477
3b63d2d3
LP
478 if (stat(i->path, &st) < 0) {
479 log_error("stat(%s) failed: %m", i->path);
4aa8b15b 480 r = -errno;
5008d581
LP
481 goto finish;
482 }
483
484 if (!S_ISDIR(st.st_mode)) {
3b63d2d3 485 log_error("%s is not a directory.", i->path);
4aa8b15b 486 r = -EEXIST;
5008d581
LP
487 goto finish;
488 }
489
3b63d2d3
LP
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 }
5008d581 496
3b63d2d3
LP
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) {
5008d581 501
3b63d2d3 502 log_error("chown(%s) failed: %m", i->path);
4aa8b15b 503 r = -errno;
5008d581
LP
504 goto finish;
505 }
3b63d2d3
LP
506
507 break;
508 }
509
c904f64d 510 if ((r = label_fix(i->path, false)) < 0)
3b63d2d3
LP
511 goto finish;
512
513 log_debug("%s created successfully.", i->path);
514
515finish:
516 if (fd >= 0)
517 close_nointr_nofail(fd);
518
519 return r;
520}
521
b8bb3e8f 522static int remove_item(Item *i, const char *instance) {
3b63d2d3
LP
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:
b8bb3e8f
LP
536 if (remove(instance) < 0 && errno != ENOENT) {
537 log_error("remove(%s): %m", instance);
3b63d2d3 538 return -errno;
5008d581 539 }
3b63d2d3
LP
540
541 break;
542
543 case TRUNCATE_DIRECTORY:
544 case RECURSIVE_REMOVE_PATH:
b8bb3e8f 545 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
3b63d2d3 546 r != -ENOENT) {
b8bb3e8f 547 log_error("rm_rf(%s): %s", instance, strerror(-r));
3b63d2d3
LP
548 return r;
549 }
550
551 break;
552 }
553
554 return 0;
555}
556
b8bb3e8f
LP
557static 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
3b63d2d3
LP
601static int process_item(Item *i) {
602 int r, q, p;
603
604 assert(i);
605
606 r = arg_create ? create_item(i) : 0;
b8bb3e8f 607 q = arg_remove ? remove_item_glob(i) : 0;
3b63d2d3
LP
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
619static void item_free(Item *i) {
620 assert(i);
621
622 free(i->path);
623 free(i);
624}
625
fba6e687 626static int parse_line(const char *fname, unsigned line, const char *buffer) {
3b63d2d3
LP
627 Item *i;
628 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
bd40a2d8 629 int r;
3b63d2d3
LP
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
bd40a2d8
LP
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) {
3b63d2d3
LP
653 log_error("[%s:%u] Syntax error.", fname, line);
654 r = -EIO;
655 goto finish;
5008d581
LP
656 }
657
3b63d2d3 658 if (i->type != CREATE_FILE &&
3b63d2d3 659 i->type != TRUNCATE_FILE &&
abe35cc2
GSB
660 i->type != CREATE_DIRECTORY &&
661 i->type != TRUNCATE_DIRECTORY &&
3b63d2d3
LP
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;
4aa8b15b 667 goto finish;
3b63d2d3
LP
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
fba6e687 678 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
3b63d2d3
LP
679 r = 0;
680 goto finish;
681 }
5008d581 682
3b63d2d3
LP
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
b8bb3e8f 745 if ((r = hashmap_put(needs_glob(i->type) ? globs : items, i->path, i)) < 0) {
022707d9
LP
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
3b63d2d3
LP
752 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
753 goto finish;
754 }
755
756 i = NULL;
4aa8b15b 757 r = 0;
5008d581
LP
758
759finish:
5008d581
LP
760 free(user);
761 free(group);
3b63d2d3
LP
762 free(mode);
763 free(age);
5008d581 764
3b63d2d3
LP
765 if (i)
766 item_free(i);
4aa8b15b
LP
767
768 return r;
5008d581
LP
769}
770
771static 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 &&
1a6f4df6
LP
778 d->d_type != DT_LNK &&
779 d->d_type != DT_UNKNOWN)
5008d581
LP
780 return 0;
781
782 return endswith(d->d_name, ".conf");
783}
784
3b63d2d3
LP
785static int help(void) {
786
522d4a49
LP
787 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
788 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
3b63d2d3
LP
789 " -h --help Show this help\n"
790 " --create Create marked files/directories\n"
791 " --clean Clean up marked directories\n"
fba6e687 792 " --remove Remove marked files/directories\n"
522d4a49 793 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
3b63d2d3
LP
794 program_invocation_short_name);
795
796 return 0;
797}
798
799static int parse_argv(int argc, char *argv[]) {
800
801 enum {
802 ARG_CREATE,
803 ARG_CLEAN,
fba6e687
LP
804 ARG_REMOVE,
805 ARG_PREFIX
3b63d2d3
LP
806 };
807
808 static const struct option options[] = {
809 { "help", no_argument, NULL, 'h' },
810 { "create", no_argument, NULL, ARG_CREATE },
811 { "clean", no_argument, NULL, ARG_CLEAN },
812 { "remove", no_argument, NULL, ARG_REMOVE },
fba6e687 813 { "prefix", required_argument, NULL, ARG_PREFIX },
3b63d2d3
LP
814 { NULL, 0, NULL, 0 }
815 };
816
817 int c;
818
819 assert(argc >= 0);
820 assert(argv);
821
822 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
823
824 switch (c) {
825
826 case 'h':
827 help();
828 return 0;
829
830 case ARG_CREATE:
831 arg_create = true;
832 break;
833
834 case ARG_CLEAN:
835 arg_clean = true;
836 break;
837
838 case ARG_REMOVE:
839 arg_remove = true;
840 break;
841
fba6e687
LP
842 case ARG_PREFIX:
843 arg_prefix = optarg;
844 break;
845
3b63d2d3
LP
846 case '?':
847 return -EINVAL;
848
849 default:
850 log_error("Unknown option code %c", c);
851 return -EINVAL;
852 }
853 }
854
855 if (!arg_clean && !arg_create && !arg_remove) {
35b8ca3a 856 log_error("You need to specify at least one of --clean, --create or --remove.");
3b63d2d3
LP
857 return -EINVAL;
858 }
859
860 return 1;
861}
862
fba6e687
LP
863static int read_config_file(const char *fn, bool ignore_enoent) {
864 FILE *f;
865 unsigned v = 0;
866 int r = 0;
867
868 assert(fn);
869
870 if (!(f = fopen(fn, "re"))) {
871
872 if (ignore_enoent && errno == ENOENT)
873 return 0;
874
875 log_error("Failed to open %s: %m", fn);
876 return -errno;
877 }
878
879 for (;;) {
880 char line[LINE_MAX], *l;
881 int k;
882
883 if (!(fgets(line, sizeof(line), f)))
884 break;
885
886 v++;
887
888 l = strstrip(line);
889 if (*l == '#' || *l == 0)
890 continue;
891
892 if ((k = parse_line(fn, v, l)) < 0)
893 if (r == 0)
894 r = k;
895 }
896
897 if (ferror(f)) {
898 log_error("Failed to read from file %s: %m", fn);
899 if (r == 0)
900 r = -EIO;
901 }
902
903 fclose(f);
904
905 return r;
906}
907
5008d581 908int main(int argc, char *argv[]) {
fba6e687 909 int r;
3b63d2d3
LP
910 Item *i;
911 Iterator iterator;
912
913 if ((r = parse_argv(argc, argv)) <= 0)
914 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
5008d581 915
eb0ca9eb 916 log_set_target(LOG_TARGET_AUTO);
5008d581
LP
917 log_parse_environment();
918 log_open();
919
920 label_init();
921
b8bb3e8f
LP
922 items = hashmap_new(string_hash_func, string_compare_func);
923 globs = hashmap_new(string_hash_func, string_compare_func);
924
925 if (!items || !globs) {
3b63d2d3
LP
926 log_error("Out of memory");
927 r = EXIT_FAILURE;
928 goto finish;
929 }
930
5008d581
LP
931 r = EXIT_SUCCESS;
932
fba6e687
LP
933 if (optind < argc) {
934 int j;
5008d581 935
fba6e687
LP
936 for (j = optind; j < argc; j++)
937 if (read_config_file(argv[j], false) < 0)
938 r = EXIT_FAILURE;
5008d581 939
fba6e687
LP
940 } else {
941 int n, j;
942 struct dirent **de = NULL;
5008d581 943
fba6e687 944 if ((n = scandir("/etc/tmpfiles.d/", &de, scandir_filter, alphasort)) < 0) {
3b63d2d3
LP
945
946 if (errno != ENOENT) {
fba6e687 947 log_error("Failed to enumerate /etc/tmpfiles.d/ files: %m");
3b63d2d3
LP
948 r = EXIT_FAILURE;
949 }
950
fba6e687 951 goto finish;
5008d581
LP
952 }
953
fba6e687
LP
954 for (j = 0; j < n; j++) {
955 int k;
956 char *fn;
5008d581 957
fba6e687
LP
958 k = asprintf(&fn, "/etc/tmpfiles.d/%s", de[j]->d_name);
959 free(de[j]);
5008d581 960
fba6e687
LP
961 if (k < 0) {
962 log_error("Failed to allocate file name.");
963 r = EXIT_FAILURE;
5008d581 964 continue;
fba6e687 965 }
5008d581 966
fba6e687 967 if (read_config_file(fn, true) < 0)
4aa8b15b 968 r = EXIT_FAILURE;
5008d581 969
fba6e687 970 free(fn);
5008d581
LP
971 }
972
fba6e687 973 free(de);
5008d581
LP
974 }
975
b8bb3e8f
LP
976 HASHMAP_FOREACH(i, globs, iterator)
977 if (process_item(i) < 0)
978 r = EXIT_FAILURE;
979
3b63d2d3
LP
980 HASHMAP_FOREACH(i, items, iterator)
981 if (process_item(i) < 0)
982 r = EXIT_FAILURE;
983
5008d581 984finish:
3b63d2d3
LP
985 while ((i = hashmap_steal_first(items)))
986 item_free(i);
987
17b90525
LP
988 while ((i = hashmap_steal_first(globs)))
989 item_free(i);
990
3b63d2d3 991 hashmap_free(items);
b8bb3e8f 992 hashmap_free(globs);
5008d581 993
17b90525
LP
994 set_free_free(unix_sockets);
995
29003cff
LP
996 label_finish();
997
5008d581
LP
998 return r;
999}