blob: feb2a214638f3264083cf653a949508ea4340dff [file] [log] [blame]
Lennart Poettering5008d582010-09-28 02:34:02 +02001/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02006 Copyright 2010 Lennart Poettering, Kay Sievers
Lennart Poettering5008d582010-09-28 02:34:02 +02007
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>
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020031#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>
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010039#include <glob.h>
40#include <fnmatch.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020041
42#include "log.h"
43#include "util.h"
44#include "strv.h"
45#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020046#include "set.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020047
Andreas Jaeger01000472010-09-29 10:08:24 +020048/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020049 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020050 * properly owned directories beneath /tmp, /var/tmp, /run, which are
51 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020052
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020053enum {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010054 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020055 CREATE_FILE = 'f',
56 TRUNCATE_FILE = 'F',
57 CREATE_DIRECTORY = 'd',
58 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020059 CREATE_FIFO = 'p',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010060
61 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020062 IGNORE_PATH = 'x',
63 REMOVE_PATH = 'r',
64 RECURSIVE_REMOVE_PATH = 'R'
65};
66
67typedef struct Item {
Lennart Poettering5008d582010-09-28 02:34:02 +020068 char type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020069
70 char *path;
Lennart Poettering5008d582010-09-28 02:34:02 +020071 uid_t uid;
72 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020073 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
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010082static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010083static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020084
85static bool arg_create = false;
86static bool arg_clean = false;
87static bool arg_remove = false;
88
Lennart Poetteringfba6e682011-02-13 14:00:54 +010089static const char *arg_prefix = NULL;
90
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020091#define MAX_DEPTH 256
92
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010093static 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
Lennart Poettering17b90522011-02-14 21:55:06 +0100108static 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
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100150 path_kill_slashes(s);
151
Lennart Poettering17b90522011-02-14 21:55:06 +0100152 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
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200182static int dir_cleanup(
183 const char *p,
184 DIR *d,
185 const struct stat *ds,
186 usec_t cutoff,
187 dev_t rootdev,
188 bool mountpoint,
189 int maxdepth)
190{
191 struct dirent *dent;
192 struct timespec times[2];
193 bool deleted = false;
194 char *sub_path = NULL;
195 int r = 0;
196
197 while ((dent = readdir(d))) {
198 struct stat s;
199 usec_t age;
200
201 if (streq(dent->d_name, ".") ||
202 streq(dent->d_name, ".."))
203 continue;
204
205 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
206
207 if (errno != ENOENT) {
208 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
209 r = -errno;
210 }
211
212 continue;
213 }
214
215 /* Stay on the same filesystem */
216 if (s.st_dev != rootdev)
217 continue;
218
219 /* Do not delete read-only files owned by root */
220 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
221 continue;
222
223 free(sub_path);
224 sub_path = NULL;
225
226 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
227 log_error("Out of memory");
228 r = -ENOMEM;
229 goto finish;
230 }
231
232 /* Is there an item configured for this path? */
233 if (hashmap_get(items, sub_path))
234 continue;
235
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100236 if (find_glob(globs, sub_path))
237 continue;
238
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200239 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
Lennart Poetteringa2477552010-12-28 14:20:21 +0100252 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200253 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 {
Lennart Poettering9c737362010-11-14 20:12:51 +0100285 /* 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
Lennart Poettering17b90522011-02-14 21:55:06 +0100292 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200293 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
Lennart Poettering17b90522011-02-14 21:55:06 +0100302 /* Ignore sockets that are listed in /proc/net/unix */
303 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
304 continue;
305
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100306 /* Ignore device nodes */
307 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
308 continue;
309
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200310 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;
380 goto finish;
381 }
382
383 if (!S_ISDIR(s.st_mode)) {
384 log_error("%s is not a directory.", i->path);
385 r = -ENOTDIR;
386 goto finish;
387 }
388
389 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
390 log_error("stat(%s/..) failed: %m", i->path);
391 r = -errno;
392 goto finish;
393 }
394
395 mountpoint = s.st_dev != ps.st_dev ||
396 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
397
398 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
399
400finish:
401 if (d)
402 closedir(d);
403
404 return r;
405}
406
407static int create_item(Item *i) {
408 int fd = -1, r;
409 mode_t u;
410 struct stat st;
411
412 assert(i);
413
414 switch (i->type) {
415
416 case IGNORE_PATH:
417 case REMOVE_PATH:
418 case RECURSIVE_REMOVE_PATH:
419 return 0;
420
421 case CREATE_FILE:
422 case TRUNCATE_FILE:
423
424 u = umask(0);
425 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
426 (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
427 umask(u);
428
429 if (fd < 0) {
430 log_error("Failed to create file %s: %m", i->path);
431 r = -errno;
432 goto finish;
433 }
434
435 if (fstat(fd, &st) < 0) {
436 log_error("stat(%s) failed: %m", i->path);
437 r = -errno;
438 goto finish;
439 }
440
441 if (!S_ISREG(st.st_mode)) {
442 log_error("%s is not a file.", i->path);
443 r = -EEXIST;
444 goto finish;
445 }
446
447 if (i->mode_set)
448 if (fchmod(fd, i->mode) < 0) {
449 log_error("chmod(%s) failed: %m", i->path);
450 r = -errno;
451 goto finish;
452 }
453
454 if (i->uid_set || i->gid_set)
455 if (fchown(fd,
456 i->uid_set ? i->uid : (uid_t) -1,
457 i->gid_set ? i->gid : (gid_t) -1) < 0) {
458 log_error("chown(%s) failed: %m", i->path);
459 r = -errno;
460 goto finish;
461 }
462
463 break;
464
465 case TRUNCATE_DIRECTORY:
466 case CREATE_DIRECTORY:
467
468 u = umask(0);
Kay Sievers33366862011-04-03 22:21:21 +0200469 mkdir_parents(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200470 r = mkdir(i->path, i->mode);
471 umask(u);
472
473 if (r < 0 && errno != EEXIST) {
474 log_error("Failed to create directory %s: %m", i->path);
475 r = -errno;
476 goto finish;
477 }
478
479 if (stat(i->path, &st) < 0) {
480 log_error("stat(%s) failed: %m", i->path);
481 r = -errno;
482 goto finish;
483 }
484
485 if (!S_ISDIR(st.st_mode)) {
486 log_error("%s is not a directory.", i->path);
487 r = -EEXIST;
488 goto finish;
489 }
490
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 }
497
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) {
502
503 log_error("chown(%s) failed: %m", i->path);
504 r = -errno;
505 goto finish;
506 }
507
508 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200509
510 case CREATE_FIFO:
511
512 u = umask(0);
513 r = mkfifo(i->path, i->mode);
514 umask(u);
515
516 if (r < 0 && errno != EEXIST) {
517 log_error("Failed to create fifo %s: %m", i->path);
518 r = -errno;
519 goto finish;
520 }
521
522 if (stat(i->path, &st) < 0) {
523 log_error("stat(%s) failed: %m", i->path);
524 r = -errno;
525 goto finish;
526 }
527
528 if (!S_ISFIFO(st.st_mode)) {
529 log_error("%s is not a fifo.", i->path);
530 r = -EEXIST;
531 goto finish;
532 }
533
534 if (i->mode_set)
535 if (chmod(i->path, i->mode) < 0) {
536 log_error("chmod(%s) failed: %m", i->path);
537 r = -errno;
538 goto finish;
539 }
540
541 if (i->uid_set || i->gid_set)
542 if (chown(i->path,
543 i->uid_set ? i->uid : (uid_t) -1,
544 i->gid_set ? i->gid : (gid_t) -1) < 0) {
545 log_error("chown(%s) failed: %m", i->path);
546 r = -errno;
547 goto finish;
548 }
549
550 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200551 }
552
Lennart Poetteringc904f642011-02-25 01:47:31 +0100553 if ((r = label_fix(i->path, false)) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200554 goto finish;
555
556 log_debug("%s created successfully.", i->path);
557
558finish:
559 if (fd >= 0)
560 close_nointr_nofail(fd);
561
562 return r;
563}
564
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100565static int remove_item(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200566 int r;
567
568 assert(i);
569
570 switch (i->type) {
571
572 case CREATE_FILE:
573 case TRUNCATE_FILE:
574 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200575 case CREATE_FIFO:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200576 case IGNORE_PATH:
577 break;
578
579 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100580 if (remove(instance) < 0 && errno != ENOENT) {
581 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200582 return -errno;
583 }
584
585 break;
586
587 case TRUNCATE_DIRECTORY:
588 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100589 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200590 r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100591 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200592 return r;
593 }
594
595 break;
596 }
597
598 return 0;
599}
600
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100601static int remove_item_glob(Item *i) {
602 assert(i);
603
604 switch (i->type) {
605
606 case CREATE_FILE:
607 case TRUNCATE_FILE:
608 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200609 case CREATE_FIFO:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100610 case IGNORE_PATH:
611 break;
612
613 case REMOVE_PATH:
614 case TRUNCATE_DIRECTORY:
615 case RECURSIVE_REMOVE_PATH: {
616 int r = 0, k;
617 glob_t g;
618 char **fn;
619
620 zero(g);
621
622 errno = 0;
623 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
624
625 if (k != GLOB_NOMATCH) {
626 if (errno != 0)
627 errno = EIO;
628
629 log_error("glob(%s) failed: %m", i->path);
630 return -errno;
631 }
632 }
633
634 STRV_FOREACH(fn, g.gl_pathv)
635 if ((k = remove_item(i, *fn)) < 0)
636 r = k;
637
638 globfree(&g);
639 return r;
640 }
641 }
642
643 return 0;
644}
645
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200646static int process_item(Item *i) {
647 int r, q, p;
648
649 assert(i);
650
651 r = arg_create ? create_item(i) : 0;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100652 q = arg_remove ? remove_item_glob(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200653 p = arg_clean ? clean_item(i) : 0;
654
655 if (r < 0)
656 return r;
657
658 if (q < 0)
659 return q;
660
661 return p;
662}
663
664static void item_free(Item *i) {
665 assert(i);
666
667 free(i->path);
668 free(i);
669}
670
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200671static bool item_equal(Item *a, Item *b) {
672 assert(a);
673 assert(b);
674
675 if (!streq_ptr(a->path, b->path))
676 return false;
677
678 if (a->type != b->type)
679 return false;
680
681 if (a->uid_set != b->uid_set ||
682 (a->uid_set && a->uid != b->uid))
683 return false;
684
685 if (a->gid_set != b->gid_set ||
686 (a->gid_set && a->gid != b->gid))
687 return false;
688
689 if (a->mode_set != b->mode_set ||
690 (a->mode_set && a->mode != b->mode))
691 return false;
692
693 if (a->age_set != b->age_set ||
694 (a->age_set && a->age != b->age))
695 return false;
696
697 return true;
698}
699
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100700static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200701 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200702 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200703 Hashmap *h;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100704 int r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200705
706 assert(fname);
707 assert(line >= 1);
708 assert(buffer);
709
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200710 if (!(i = new0(Item, 1))) {
711 log_error("Out of memory");
712 return -ENOMEM;
713 }
714
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100715 if (sscanf(buffer,
716 "%c "
717 "%ms "
718 "%ms "
719 "%ms "
720 "%ms "
721 "%ms",
722 &i->type,
723 &i->path,
724 &mode,
725 &user,
726 &group,
727 &age) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200728 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200729 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200730 goto finish;
731 }
732
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200733 if (i->type != CREATE_FILE &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200734 i->type != TRUNCATE_FILE &&
Gustavo Sverzut Barbieriabe35cc2010-10-19 23:06:34 -0200735 i->type != CREATE_DIRECTORY &&
736 i->type != TRUNCATE_DIRECTORY &&
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200737 i->type != CREATE_FIFO &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200738 i->type != IGNORE_PATH &&
739 i->type != REMOVE_PATH &&
740 i->type != RECURSIVE_REMOVE_PATH) {
741 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200742 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200743 goto finish;
744 }
745
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200746 if (!path_is_absolute(i->path)) {
747 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
748 r = -EBADMSG;
749 goto finish;
750 }
751
752 path_kill_slashes(i->path);
753
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100754 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200755 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200756 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200757 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200758
759 if (user && !streq(user, "-")) {
760 unsigned long lu;
761 struct passwd *p;
762
763 if (streq(user, "root") || streq(user, "0"))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200764 i->uid = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200765 else if (safe_atolu(user, &lu) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200766 i->uid = (uid_t) lu;
Lennart Poettering5008d582010-09-28 02:34:02 +0200767 else if ((p = getpwnam(user)))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200768 i->uid = p->pw_uid;
Lennart Poettering5008d582010-09-28 02:34:02 +0200769 else {
770 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200771 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200772 goto finish;
773 }
774
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200775 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200776 }
777
778 if (group && !streq(group, "-")) {
779 unsigned long lu;
780 struct group *g;
781
782 if (streq(group, "root") || streq(group, "0"))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200783 i->gid = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200784 else if (safe_atolu(group, &lu) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200785 i->gid = (gid_t) lu;
Lennart Poettering5008d582010-09-28 02:34:02 +0200786 else if ((g = getgrnam(group)))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200787 i->gid = g->gr_gid;
Lennart Poettering5008d582010-09-28 02:34:02 +0200788 else {
789 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200790 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200791 goto finish;
792 }
793
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200794 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200795 }
796
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200797 if (mode && !streq(mode, "-")) {
798 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +0200799
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200800 if (sscanf(mode, "%o", &m) != 1) {
801 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
802 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200803 goto finish;
804 }
805
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200806 i->mode = m;
807 i->mode_set = true;
808 } else
809 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
810
811 if (age && !streq(age, "-")) {
812 if (parse_usec(age, &i->age) < 0) {
813 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
814 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200815 goto finish;
816 }
817
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200818 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200819 }
820
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200821 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +0100822
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200823 if ((existing = hashmap_get(h, i->path))) {
824
825 /* Two identical items are fine */
826 if (!item_equal(existing, i))
827 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
828
829 r = 0;
830 goto finish;
831 }
832
833 if ((r = hashmap_put(h, i->path, i)) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200834 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200835 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200836 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200837
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200838 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200839 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200840
841finish:
Lennart Poettering5008d582010-09-28 02:34:02 +0200842 free(user);
843 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200844 free(mode);
845 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +0200846
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200847 if (i)
848 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200849
850 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200851}
852
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200853static int help(void) {
854
Lennart Poettering522d4a42011-02-13 15:08:15 +0100855 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
856 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200857 " -h --help Show this help\n"
858 " --create Create marked files/directories\n"
859 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100860 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +0100861 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200862 program_invocation_short_name);
863
864 return 0;
865}
866
867static int parse_argv(int argc, char *argv[]) {
868
869 enum {
870 ARG_CREATE,
871 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100872 ARG_REMOVE,
873 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200874 };
875
876 static const struct option options[] = {
877 { "help", no_argument, NULL, 'h' },
878 { "create", no_argument, NULL, ARG_CREATE },
879 { "clean", no_argument, NULL, ARG_CLEAN },
880 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100881 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200882 { NULL, 0, NULL, 0 }
883 };
884
885 int c;
886
887 assert(argc >= 0);
888 assert(argv);
889
890 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
891
892 switch (c) {
893
894 case 'h':
895 help();
896 return 0;
897
898 case ARG_CREATE:
899 arg_create = true;
900 break;
901
902 case ARG_CLEAN:
903 arg_clean = true;
904 break;
905
906 case ARG_REMOVE:
907 arg_remove = true;
908 break;
909
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100910 case ARG_PREFIX:
911 arg_prefix = optarg;
912 break;
913
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200914 case '?':
915 return -EINVAL;
916
917 default:
918 log_error("Unknown option code %c", c);
919 return -EINVAL;
920 }
921 }
922
923 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +0100924 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200925 return -EINVAL;
926 }
927
928 return 1;
929}
930
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100931static int read_config_file(const char *fn, bool ignore_enoent) {
932 FILE *f;
933 unsigned v = 0;
934 int r = 0;
935
936 assert(fn);
937
938 if (!(f = fopen(fn, "re"))) {
939
940 if (ignore_enoent && errno == ENOENT)
941 return 0;
942
943 log_error("Failed to open %s: %m", fn);
944 return -errno;
945 }
946
Kay Sievers772f8372011-04-25 21:38:21 +0200947 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100948 for (;;) {
949 char line[LINE_MAX], *l;
950 int k;
951
952 if (!(fgets(line, sizeof(line), f)))
953 break;
954
955 v++;
956
957 l = strstrip(line);
958 if (*l == '#' || *l == 0)
959 continue;
960
961 if ((k = parse_line(fn, v, l)) < 0)
962 if (r == 0)
963 r = k;
964 }
965
966 if (ferror(f)) {
967 log_error("Failed to read from file %s: %m", fn);
968 if (r == 0)
969 r = -EIO;
970 }
971
972 fclose(f);
973
974 return r;
975}
976
Lennart Poettering5008d582010-09-28 02:34:02 +0200977int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100978 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200979 Item *i;
980 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +0200981
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200982 if ((r = parse_argv(argc, argv)) <= 0)
983 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
984
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +0100985 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +0200986 log_parse_environment();
987 log_open();
988
989 label_init();
990
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100991 items = hashmap_new(string_hash_func, string_compare_func);
992 globs = hashmap_new(string_hash_func, string_compare_func);
993
994 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200995 log_error("Out of memory");
996 r = EXIT_FAILURE;
997 goto finish;
998 }
999
Lennart Poettering5008d582010-09-28 02:34:02 +02001000 r = EXIT_SUCCESS;
1001
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001002 if (optind < argc) {
1003 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001004
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001005 for (j = optind; j < argc; j++)
1006 if (read_config_file(argv[j], false) < 0)
1007 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001008
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001009 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001010 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001011
Kay Sievers44143302011-04-28 23:51:24 +02001012 r = conf_files_list(&files, ".conf",
1013 "/run/tmpfiles.d",
1014 "/etc/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +02001015 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +02001016 "/usr/lib/tmpfiles.d",
1017 NULL);
1018 if (r < 0) {
1019 r = EXIT_FAILURE;
1020 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1021 goto finish;
1022 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001023
Kay Sievers772f8372011-04-25 21:38:21 +02001024 STRV_FOREACH(f, files) {
1025 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001026 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001027 }
1028
Kay Sievers772f8372011-04-25 21:38:21 +02001029 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001030 }
1031
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001032 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001033 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001034
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001035 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001036 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001037
Lennart Poettering5008d582010-09-28 02:34:02 +02001038finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001039 while ((i = hashmap_steal_first(items)))
1040 item_free(i);
1041
Lennart Poettering17b90522011-02-14 21:55:06 +01001042 while ((i = hashmap_steal_first(globs)))
1043 item_free(i);
1044
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001045 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001046 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001047
Lennart Poettering17b90522011-02-14 21:55:06 +01001048 set_free_free(unix_sockets);
1049
Lennart Poettering29003cf2010-10-19 19:36:45 +02001050 label_finish();
1051
Lennart Poettering5008d582010-09-28 02:34:02 +02001052 return r;
1053}