]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/tmpfiles.c
units: move user units from /usr/share to /usr/lib since they might be arch-dependent
[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
bb29785e 50 * properly owned directories beneath /tmp, /var/tmp, /run and
5008d581
LP
51 * /var/lock which are volatile and hence need to be recreated on
52 * bootup. */
53
3b63d2d3 54enum {
b8bb3e8f 55 /* These ones take file names */
3b63d2d3
LP
56 CREATE_FILE = 'f',
57 TRUNCATE_FILE = 'F',
58 CREATE_DIRECTORY = 'd',
59 TRUNCATE_DIRECTORY = 'D',
b8bb3e8f
LP
60
61 /* These ones take globs */
3b63d2d3
LP
62 IGNORE_PATH = 'x',
63 REMOVE_PATH = 'r',
64 RECURSIVE_REMOVE_PATH = 'R'
65};
66
67typedef struct Item {
5008d581 68 char type;
3b63d2d3
LP
69
70 char *path;
5008d581
LP
71 uid_t uid;
72 gid_t gid;
3b63d2d3
LP
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
b8bb3e8f 82static Hashmap *items = NULL, *globs = NULL;
17b90525 83static Set *unix_sockets = NULL;
3b63d2d3
LP
84
85static bool arg_create = false;
86static bool arg_clean = false;
87static bool arg_remove = false;
88
fba6e687
LP
89static const char *arg_prefix = NULL;
90
3b63d2d3
LP
91#define MAX_DEPTH 256
92
b8bb3e8f
LP
93static bool needs_glob(int t) {
94 return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH;
95}
96
97static 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
17b90525
LP
108static 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
4ff21d85
LP
150 path_kill_slashes(s);
151
17b90525
LP
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
162fail:
163 set_free_free(unix_sockets);
164 unix_sockets = NULL;
165
166 if (f)
167 fclose(f);
168}
169
170static 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
3b63d2d3
LP
182static 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;
5008d581 204
3b63d2d3 205 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
5008d581 206
3b63d2d3
LP
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
b8bb3e8f
LP
236 if (find_glob(globs, sub_path))
237 continue;
238
3b63d2d3
LP
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
a247755d 252 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
3b63d2d3
LP
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 {
9c73736d
LP
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
17b90525 292 if (mountpoint && S_ISREG(s.st_mode)) {
3b63d2d3
LP
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
17b90525
LP
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
78ab08eb
LP
306 /* Ignore device nodes */
307 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
308 continue;
309
3b63d2d3
LP
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
330finish:
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
345static 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;
5008d581
LP
380 goto finish;
381 }
382
3b63d2d3
LP
383 if (!S_ISDIR(s.st_mode)) {
384 log_error("%s is not a directory.", i->path);
385 r = -ENOTDIR;
5008d581
LP
386 goto finish;
387 }
388
3b63d2d3
LP
389 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
390 log_error("stat(%s/..) failed: %m", i->path);
391 r = -errno;
5008d581 392 goto finish;
4aa8b15b 393 }
5008d581 394
3b63d2d3
LP
395 mountpoint = s.st_dev != ps.st_dev ||
396 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
5008d581 397
3b63d2d3 398 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
5008d581 399
3b63d2d3
LP
400finish:
401 if (d)
402 closedir(d);
5008d581 403
3b63d2d3
LP
404 return r;
405}
5008d581 406
3b63d2d3
LP
407static int create_item(Item *i) {
408 int fd = -1, r;
409 mode_t u;
410 struct stat st;
5008d581 411
3b63d2d3 412 assert(i);
5008d581 413
3b63d2d3
LP
414 switch (i->type) {
415
416 case IGNORE_PATH:
417 case REMOVE_PATH:
418 case RECURSIVE_REMOVE_PATH:
419 return 0;
5008d581 420
3b63d2d3
LP
421 case CREATE_FILE:
422 case TRUNCATE_FILE:
5008d581
LP
423
424 u = umask(0);
3b63d2d3
LP
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);
5008d581
LP
427 umask(u);
428
429 if (fd < 0) {
3b63d2d3 430 log_error("Failed to create file %s: %m", i->path);
4aa8b15b 431 r = -errno;
5008d581
LP
432 goto finish;
433 }
434
435 if (fstat(fd, &st) < 0) {
3b63d2d3 436 log_error("stat(%s) failed: %m", i->path);
4aa8b15b 437 r = -errno;
5008d581
LP
438 goto finish;
439 }
440
441 if (!S_ISREG(st.st_mode)) {
3b63d2d3 442 log_error("%s is not a file.", i->path);
4aa8b15b 443 r = -EEXIST;
5008d581
LP
444 goto finish;
445 }
446
3b63d2d3
LP
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 }
5008d581 453
3b63d2d3 454 if (i->uid_set || i->gid_set)
5008d581 455 if (fchown(fd,
3b63d2d3
LP
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);
4aa8b15b 459 r = -errno;
5008d581
LP
460 goto finish;
461 }
5008d581 462
3b63d2d3
LP
463 break;
464
465 case TRUNCATE_DIRECTORY:
466 case CREATE_DIRECTORY:
5008d581
LP
467
468 u = umask(0);
33366862 469 mkdir_parents(i->path, 0755);
3b63d2d3 470 r = mkdir(i->path, i->mode);
5008d581
LP
471 umask(u);
472
473 if (r < 0 && errno != EEXIST) {
3b63d2d3 474 log_error("Failed to create directory %s: %m", i->path);
4aa8b15b 475 r = -errno;
5008d581
LP
476 goto finish;
477 }
478
3b63d2d3
LP
479 if (stat(i->path, &st) < 0) {
480 log_error("stat(%s) failed: %m", i->path);
4aa8b15b 481 r = -errno;
5008d581
LP
482 goto finish;
483 }
484
485 if (!S_ISDIR(st.st_mode)) {
3b63d2d3 486 log_error("%s is not a directory.", i->path);
4aa8b15b 487 r = -EEXIST;
5008d581
LP
488 goto finish;
489 }
490
3b63d2d3
LP
491 if (i->mode_set)
492 if (chmod(i->path, i->mode) < 0) {
493 log_error("chmod(%s) failed: %m", i->path);
494 r = -errno;
495 goto finish;
496 }
5008d581 497
3b63d2d3
LP
498 if (i->uid_set || i->gid_set)
499 if (chown(i->path,
500 i->uid_set ? i->uid : (uid_t) -1,
501 i->gid_set ? i->gid : (gid_t) -1) < 0) {
5008d581 502
3b63d2d3 503 log_error("chown(%s) failed: %m", i->path);
4aa8b15b 504 r = -errno;
5008d581
LP
505 goto finish;
506 }
3b63d2d3
LP
507
508 break;
509 }
510
c904f64d 511 if ((r = label_fix(i->path, false)) < 0)
3b63d2d3
LP
512 goto finish;
513
514 log_debug("%s created successfully.", i->path);
515
516finish:
517 if (fd >= 0)
518 close_nointr_nofail(fd);
519
520 return r;
521}
522
b8bb3e8f 523static int remove_item(Item *i, const char *instance) {
3b63d2d3
LP
524 int r;
525
526 assert(i);
527
528 switch (i->type) {
529
530 case CREATE_FILE:
531 case TRUNCATE_FILE:
532 case CREATE_DIRECTORY:
533 case IGNORE_PATH:
534 break;
535
536 case REMOVE_PATH:
b8bb3e8f
LP
537 if (remove(instance) < 0 && errno != ENOENT) {
538 log_error("remove(%s): %m", instance);
3b63d2d3 539 return -errno;
5008d581 540 }
3b63d2d3
LP
541
542 break;
543
544 case TRUNCATE_DIRECTORY:
545 case RECURSIVE_REMOVE_PATH:
b8bb3e8f 546 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
3b63d2d3 547 r != -ENOENT) {
b8bb3e8f 548 log_error("rm_rf(%s): %s", instance, strerror(-r));
3b63d2d3
LP
549 return r;
550 }
551
552 break;
553 }
554
555 return 0;
556}
557
b8bb3e8f
LP
558static int remove_item_glob(Item *i) {
559 assert(i);
560
561 switch (i->type) {
562
563 case CREATE_FILE:
564 case TRUNCATE_FILE:
565 case CREATE_DIRECTORY:
566 case IGNORE_PATH:
567 break;
568
569 case REMOVE_PATH:
570 case TRUNCATE_DIRECTORY:
571 case RECURSIVE_REMOVE_PATH: {
572 int r = 0, k;
573 glob_t g;
574 char **fn;
575
576 zero(g);
577
578 errno = 0;
579 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
580
581 if (k != GLOB_NOMATCH) {
582 if (errno != 0)
583 errno = EIO;
584
585 log_error("glob(%s) failed: %m", i->path);
586 return -errno;
587 }
588 }
589
590 STRV_FOREACH(fn, g.gl_pathv)
591 if ((k = remove_item(i, *fn)) < 0)
592 r = k;
593
594 globfree(&g);
595 return r;
596 }
597 }
598
599 return 0;
600}
601
3b63d2d3
LP
602static int process_item(Item *i) {
603 int r, q, p;
604
605 assert(i);
606
607 r = arg_create ? create_item(i) : 0;
b8bb3e8f 608 q = arg_remove ? remove_item_glob(i) : 0;
3b63d2d3
LP
609 p = arg_clean ? clean_item(i) : 0;
610
611 if (r < 0)
612 return r;
613
614 if (q < 0)
615 return q;
616
617 return p;
618}
619
620static void item_free(Item *i) {
621 assert(i);
622
623 free(i->path);
624 free(i);
625}
626
fba6e687 627static int parse_line(const char *fname, unsigned line, const char *buffer) {
3b63d2d3
LP
628 Item *i;
629 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
bd40a2d8 630 int r;
3b63d2d3
LP
631
632 assert(fname);
633 assert(line >= 1);
634 assert(buffer);
635
636 if (!(i = new0(Item, 1))) {
637 log_error("Out of memory");
638 return -ENOMEM;
639 }
640
bd40a2d8
LP
641 if (sscanf(buffer,
642 "%c "
643 "%ms "
644 "%ms "
645 "%ms "
646 "%ms "
647 "%ms",
648 &i->type,
649 &i->path,
650 &mode,
651 &user,
652 &group,
653 &age) < 2) {
3b63d2d3
LP
654 log_error("[%s:%u] Syntax error.", fname, line);
655 r = -EIO;
656 goto finish;
5008d581
LP
657 }
658
3b63d2d3 659 if (i->type != CREATE_FILE &&
3b63d2d3 660 i->type != TRUNCATE_FILE &&
abe35cc2
GSB
661 i->type != CREATE_DIRECTORY &&
662 i->type != TRUNCATE_DIRECTORY &&
3b63d2d3
LP
663 i->type != IGNORE_PATH &&
664 i->type != REMOVE_PATH &&
665 i->type != RECURSIVE_REMOVE_PATH) {
666 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
667 r = -EBADMSG;
4aa8b15b 668 goto finish;
3b63d2d3
LP
669 }
670
671 if (!path_is_absolute(i->path)) {
672 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
673 r = -EBADMSG;
674 goto finish;
675 }
676
677 path_kill_slashes(i->path);
678
fba6e687 679 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
3b63d2d3
LP
680 r = 0;
681 goto finish;
682 }
5008d581 683
3b63d2d3
LP
684 if (user && !streq(user, "-")) {
685 unsigned long lu;
686 struct passwd *p;
687
688 if (streq(user, "root") || streq(user, "0"))
689 i->uid = 0;
690 else if (safe_atolu(user, &lu) >= 0)
691 i->uid = (uid_t) lu;
692 else if ((p = getpwnam(user)))
693 i->uid = p->pw_uid;
694 else {
695 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
696 r = -ENOENT;
697 goto finish;
698 }
699
700 i->uid_set = true;
701 }
702
703 if (group && !streq(group, "-")) {
704 unsigned long lu;
705 struct group *g;
706
707 if (streq(group, "root") || streq(group, "0"))
708 i->gid = 0;
709 else if (safe_atolu(group, &lu) >= 0)
710 i->gid = (gid_t) lu;
711 else if ((g = getgrnam(group)))
712 i->gid = g->gr_gid;
713 else {
714 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
715 r = -ENOENT;
716 goto finish;
717 }
718
719 i->gid_set = true;
720 }
721
722 if (mode && !streq(mode, "-")) {
723 unsigned m;
724
725 if (sscanf(mode, "%o", &m) != 1) {
726 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
727 r = -ENOENT;
728 goto finish;
729 }
730
731 i->mode = m;
732 i->mode_set = true;
733 } else
734 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
735
736 if (age && !streq(age, "-")) {
737 if (parse_usec(age, &i->age) < 0) {
738 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
739 r = -EBADMSG;
740 goto finish;
741 }
742
743 i->age_set = true;
744 }
745
b8bb3e8f 746 if ((r = hashmap_put(needs_glob(i->type) ? globs : items, i->path, i)) < 0) {
022707d9
LP
747 if (r == -EEXIST) {
748 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
749 r = 0;
750 goto finish;
751 }
752
3b63d2d3
LP
753 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
754 goto finish;
755 }
756
757 i = NULL;
4aa8b15b 758 r = 0;
5008d581
LP
759
760finish:
5008d581
LP
761 free(user);
762 free(group);
3b63d2d3
LP
763 free(mode);
764 free(age);
5008d581 765
3b63d2d3
LP
766 if (i)
767 item_free(i);
4aa8b15b
LP
768
769 return r;
5008d581
LP
770}
771
772static int scandir_filter(const struct dirent *d) {
773 assert(d);
774
775 if (ignore_file(d->d_name))
776 return 0;
777
778 if (d->d_type != DT_REG &&
1a6f4df6
LP
779 d->d_type != DT_LNK &&
780 d->d_type != DT_UNKNOWN)
5008d581
LP
781 return 0;
782
783 return endswith(d->d_name, ".conf");
784}
785
3b63d2d3
LP
786static int help(void) {
787
522d4a49
LP
788 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
789 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
3b63d2d3
LP
790 " -h --help Show this help\n"
791 " --create Create marked files/directories\n"
792 " --clean Clean up marked directories\n"
fba6e687 793 " --remove Remove marked files/directories\n"
522d4a49 794 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
3b63d2d3
LP
795 program_invocation_short_name);
796
797 return 0;
798}
799
800static int parse_argv(int argc, char *argv[]) {
801
802 enum {
803 ARG_CREATE,
804 ARG_CLEAN,
fba6e687
LP
805 ARG_REMOVE,
806 ARG_PREFIX
3b63d2d3
LP
807 };
808
809 static const struct option options[] = {
810 { "help", no_argument, NULL, 'h' },
811 { "create", no_argument, NULL, ARG_CREATE },
812 { "clean", no_argument, NULL, ARG_CLEAN },
813 { "remove", no_argument, NULL, ARG_REMOVE },
fba6e687 814 { "prefix", required_argument, NULL, ARG_PREFIX },
3b63d2d3
LP
815 { NULL, 0, NULL, 0 }
816 };
817
818 int c;
819
820 assert(argc >= 0);
821 assert(argv);
822
823 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
824
825 switch (c) {
826
827 case 'h':
828 help();
829 return 0;
830
831 case ARG_CREATE:
832 arg_create = true;
833 break;
834
835 case ARG_CLEAN:
836 arg_clean = true;
837 break;
838
839 case ARG_REMOVE:
840 arg_remove = true;
841 break;
842
fba6e687
LP
843 case ARG_PREFIX:
844 arg_prefix = optarg;
845 break;
846
3b63d2d3
LP
847 case '?':
848 return -EINVAL;
849
850 default:
851 log_error("Unknown option code %c", c);
852 return -EINVAL;
853 }
854 }
855
856 if (!arg_clean && !arg_create && !arg_remove) {
35b8ca3a 857 log_error("You need to specify at least one of --clean, --create or --remove.");
3b63d2d3
LP
858 return -EINVAL;
859 }
860
861 return 1;
862}
863
fba6e687
LP
864static int read_config_file(const char *fn, bool ignore_enoent) {
865 FILE *f;
866 unsigned v = 0;
867 int r = 0;
868
869 assert(fn);
870
871 if (!(f = fopen(fn, "re"))) {
872
873 if (ignore_enoent && errno == ENOENT)
874 return 0;
875
876 log_error("Failed to open %s: %m", fn);
877 return -errno;
878 }
879
880 for (;;) {
881 char line[LINE_MAX], *l;
882 int k;
883
884 if (!(fgets(line, sizeof(line), f)))
885 break;
886
887 v++;
888
889 l = strstrip(line);
890 if (*l == '#' || *l == 0)
891 continue;
892
893 if ((k = parse_line(fn, v, l)) < 0)
894 if (r == 0)
895 r = k;
896 }
897
898 if (ferror(f)) {
899 log_error("Failed to read from file %s: %m", fn);
900 if (r == 0)
901 r = -EIO;
902 }
903
904 fclose(f);
905
906 return r;
907}
908
5008d581 909int main(int argc, char *argv[]) {
fba6e687 910 int r;
3b63d2d3
LP
911 Item *i;
912 Iterator iterator;
913
914 if ((r = parse_argv(argc, argv)) <= 0)
915 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
5008d581 916
eb0ca9eb 917 log_set_target(LOG_TARGET_AUTO);
5008d581
LP
918 log_parse_environment();
919 log_open();
920
921 label_init();
922
b8bb3e8f
LP
923 items = hashmap_new(string_hash_func, string_compare_func);
924 globs = hashmap_new(string_hash_func, string_compare_func);
925
926 if (!items || !globs) {
3b63d2d3
LP
927 log_error("Out of memory");
928 r = EXIT_FAILURE;
929 goto finish;
930 }
931
5008d581
LP
932 r = EXIT_SUCCESS;
933
fba6e687
LP
934 if (optind < argc) {
935 int j;
5008d581 936
fba6e687
LP
937 for (j = optind; j < argc; j++)
938 if (read_config_file(argv[j], false) < 0)
939 r = EXIT_FAILURE;
5008d581 940
fba6e687
LP
941 } else {
942 int n, j;
943 struct dirent **de = NULL;
5008d581 944
fba6e687 945 if ((n = scandir("/etc/tmpfiles.d/", &de, scandir_filter, alphasort)) < 0) {
3b63d2d3
LP
946
947 if (errno != ENOENT) {
fba6e687 948 log_error("Failed to enumerate /etc/tmpfiles.d/ files: %m");
3b63d2d3
LP
949 r = EXIT_FAILURE;
950 }
951
fba6e687 952 goto finish;
5008d581
LP
953 }
954
fba6e687
LP
955 for (j = 0; j < n; j++) {
956 int k;
957 char *fn;
5008d581 958
fba6e687
LP
959 k = asprintf(&fn, "/etc/tmpfiles.d/%s", de[j]->d_name);
960 free(de[j]);
5008d581 961
fba6e687
LP
962 if (k < 0) {
963 log_error("Failed to allocate file name.");
964 r = EXIT_FAILURE;
5008d581 965 continue;
fba6e687 966 }
5008d581 967
fba6e687 968 if (read_config_file(fn, true) < 0)
4aa8b15b 969 r = EXIT_FAILURE;
5008d581 970
fba6e687 971 free(fn);
5008d581
LP
972 }
973
fba6e687 974 free(de);
5008d581
LP
975 }
976
b8bb3e8f
LP
977 HASHMAP_FOREACH(i, globs, iterator)
978 if (process_item(i) < 0)
979 r = EXIT_FAILURE;
980
3b63d2d3
LP
981 HASHMAP_FOREACH(i, items, iterator)
982 if (process_item(i) < 0)
983 r = EXIT_FAILURE;
984
5008d581 985finish:
3b63d2d3
LP
986 while ((i = hashmap_steal_first(items)))
987 item_free(i);
988
17b90525
LP
989 while ((i = hashmap_steal_first(globs)))
990 item_free(i);
991
3b63d2d3 992 hashmap_free(items);
b8bb3e8f 993 hashmap_free(globs);
5008d581 994
17b90525
LP
995 set_free_free(unix_sockets);
996
29003cff
LP
997 label_finish();
998
5008d581
LP
999 return r;
1000}