blob: 4308f7f943ed76e4456399702414e73824f8af63 [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
Michal Schmidt66ccd032011-12-15 21:31:14 +010053typedef enum ItemType {
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'
Michal Schmidt66ccd032011-12-15 21:31:14 +010065} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020066
67typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010068 ItemType 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
Michal Schmidt66ccd032011-12-15 21:31:14 +010093static bool needs_glob(ItemType t) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010094 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
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200160 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100161 return;
162
163fail:
164 set_free_free(unix_sockets);
165 unix_sockets = NULL;
166
167 if (f)
168 fclose(f);
169}
170
171static bool unix_socket_alive(const char *fn) {
172 assert(fn);
173
174 load_unix_sockets();
175
176 if (unix_sockets)
177 return !!set_get(unix_sockets, (char*) fn);
178
179 /* We don't know, so assume yes */
180 return true;
181}
182
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200183static int dir_cleanup(
184 const char *p,
185 DIR *d,
186 const struct stat *ds,
187 usec_t cutoff,
188 dev_t rootdev,
189 bool mountpoint,
190 int maxdepth)
191{
192 struct dirent *dent;
193 struct timespec times[2];
194 bool deleted = false;
195 char *sub_path = NULL;
196 int r = 0;
197
198 while ((dent = readdir(d))) {
199 struct stat s;
200 usec_t age;
201
202 if (streq(dent->d_name, ".") ||
203 streq(dent->d_name, ".."))
204 continue;
205
206 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
207
208 if (errno != ENOENT) {
209 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
210 r = -errno;
211 }
212
213 continue;
214 }
215
216 /* Stay on the same filesystem */
217 if (s.st_dev != rootdev)
218 continue;
219
220 /* Do not delete read-only files owned by root */
221 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
222 continue;
223
224 free(sub_path);
225 sub_path = NULL;
226
227 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
228 log_error("Out of memory");
229 r = -ENOMEM;
230 goto finish;
231 }
232
233 /* Is there an item configured for this path? */
234 if (hashmap_get(items, sub_path))
235 continue;
236
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100237 if (find_glob(globs, sub_path))
238 continue;
239
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200240 if (S_ISDIR(s.st_mode)) {
241
242 if (mountpoint &&
243 streq(dent->d_name, "lost+found") &&
244 s.st_uid == 0)
245 continue;
246
247 if (maxdepth <= 0)
248 log_warning("Reached max depth on %s.", sub_path);
249 else {
250 DIR *sub_dir;
251 int q;
252
Lennart Poetteringa2477552010-12-28 14:20:21 +0100253 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200254 if (sub_dir == NULL) {
255 if (errno != ENOENT) {
256 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
257 r = -errno;
258 }
259
260 continue;
261 }
262
263 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
264 closedir(sub_dir);
265
266 if (q < 0)
267 r = q;
268 }
269
270 /* Ignore ctime, we change it when deleting */
271 age = MAX(timespec_load(&s.st_mtim),
272 timespec_load(&s.st_atim));
273 if (age >= cutoff)
274 continue;
275
276 log_debug("rmdir '%s'\n", sub_path);
277
278 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
279 if (errno != ENOENT && errno != ENOTEMPTY) {
280 log_error("rmdir(%s): %m", sub_path);
281 r = -errno;
282 }
283 }
284
285 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100286 /* Skip files for which the sticky bit is
287 * set. These are semantics we define, and are
288 * unknown elsewhere. See XDG_RUNTIME_DIR
289 * specification for details. */
290 if (s.st_mode & S_ISVTX)
291 continue;
292
Lennart Poettering17b90522011-02-14 21:55:06 +0100293 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200294 if (streq(dent->d_name, ".journal") &&
295 s.st_uid == 0)
296 continue;
297
298 if (streq(dent->d_name, "aquota.user") ||
299 streq(dent->d_name, "aquota.group"))
300 continue;
301 }
302
Lennart Poettering17b90522011-02-14 21:55:06 +0100303 /* Ignore sockets that are listed in /proc/net/unix */
304 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
305 continue;
306
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100307 /* Ignore device nodes */
308 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
309 continue;
310
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200311 age = MAX3(timespec_load(&s.st_mtim),
312 timespec_load(&s.st_atim),
313 timespec_load(&s.st_ctim));
314
315 if (age >= cutoff)
316 continue;
317
318 log_debug("unlink '%s'\n", sub_path);
319
320 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
321 if (errno != ENOENT) {
322 log_error("unlink(%s): %m", sub_path);
323 r = -errno;
324 }
325 }
326
327 deleted = true;
328 }
329 }
330
331finish:
332 if (deleted) {
333 /* Restore original directory timestamps */
334 times[0] = ds->st_atim;
335 times[1] = ds->st_mtim;
336
337 if (futimens(dirfd(d), times) < 0)
338 log_error("utimensat(%s): %m", p);
339 }
340
341 free(sub_path);
342
343 return r;
344}
345
346static int clean_item(Item *i) {
347 DIR *d;
348 struct stat s, ps;
349 bool mountpoint;
350 int r;
351 usec_t cutoff, n;
352
353 assert(i);
354
355 if (i->type != CREATE_DIRECTORY &&
356 i->type != TRUNCATE_DIRECTORY &&
357 i->type != IGNORE_PATH)
358 return 0;
359
360 if (!i->age_set || i->age <= 0)
361 return 0;
362
363 n = now(CLOCK_REALTIME);
364 if (n < i->age)
365 return 0;
366
367 cutoff = n - i->age;
368
369 d = opendir(i->path);
370 if (!d) {
371 if (errno == ENOENT)
372 return 0;
373
374 log_error("Failed to open directory %s: %m", i->path);
375 return -errno;
376 }
377
378 if (fstat(dirfd(d), &s) < 0) {
379 log_error("stat(%s) failed: %m", i->path);
380 r = -errno;
381 goto finish;
382 }
383
384 if (!S_ISDIR(s.st_mode)) {
385 log_error("%s is not a directory.", i->path);
386 r = -ENOTDIR;
387 goto finish;
388 }
389
390 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
391 log_error("stat(%s/..) failed: %m", i->path);
392 r = -errno;
393 goto finish;
394 }
395
396 mountpoint = s.st_dev != ps.st_dev ||
397 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
398
399 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
400
401finish:
402 if (d)
403 closedir(d);
404
405 return r;
406}
407
408static int create_item(Item *i) {
409 int fd = -1, r;
410 mode_t u;
411 struct stat st;
412
413 assert(i);
414
415 switch (i->type) {
416
417 case IGNORE_PATH:
418 case REMOVE_PATH:
419 case RECURSIVE_REMOVE_PATH:
420 return 0;
421
422 case CREATE_FILE:
423 case TRUNCATE_FILE:
424
425 u = umask(0);
426 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
427 (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
428 umask(u);
429
430 if (fd < 0) {
431 log_error("Failed to create file %s: %m", i->path);
432 r = -errno;
433 goto finish;
434 }
435
436 if (fstat(fd, &st) < 0) {
437 log_error("stat(%s) failed: %m", i->path);
438 r = -errno;
439 goto finish;
440 }
441
442 if (!S_ISREG(st.st_mode)) {
443 log_error("%s is not a file.", i->path);
444 r = -EEXIST;
445 goto finish;
446 }
447
448 if (i->mode_set)
449 if (fchmod(fd, i->mode) < 0) {
450 log_error("chmod(%s) failed: %m", i->path);
451 r = -errno;
452 goto finish;
453 }
454
455 if (i->uid_set || i->gid_set)
456 if (fchown(fd,
457 i->uid_set ? i->uid : (uid_t) -1,
458 i->gid_set ? i->gid : (gid_t) -1) < 0) {
459 log_error("chown(%s) failed: %m", i->path);
460 r = -errno;
461 goto finish;
462 }
463
464 break;
465
466 case TRUNCATE_DIRECTORY:
467 case CREATE_DIRECTORY:
468
469 u = umask(0);
Kay Sievers33366862011-04-03 22:21:21 +0200470 mkdir_parents(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200471 r = mkdir(i->path, i->mode);
472 umask(u);
473
474 if (r < 0 && errno != EEXIST) {
475 log_error("Failed to create directory %s: %m", i->path);
476 r = -errno;
477 goto finish;
478 }
479
480 if (stat(i->path, &st) < 0) {
481 log_error("stat(%s) failed: %m", i->path);
482 r = -errno;
483 goto finish;
484 }
485
486 if (!S_ISDIR(st.st_mode)) {
487 log_error("%s is not a directory.", i->path);
488 r = -EEXIST;
489 goto finish;
490 }
491
492 if (i->mode_set)
493 if (chmod(i->path, i->mode) < 0) {
494 log_error("chmod(%s) failed: %m", i->path);
495 r = -errno;
496 goto finish;
497 }
498
499 if (i->uid_set || i->gid_set)
500 if (chown(i->path,
501 i->uid_set ? i->uid : (uid_t) -1,
502 i->gid_set ? i->gid : (gid_t) -1) < 0) {
503
504 log_error("chown(%s) failed: %m", i->path);
505 r = -errno;
506 goto finish;
507 }
508
509 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200510
511 case CREATE_FIFO:
512
513 u = umask(0);
514 r = mkfifo(i->path, i->mode);
515 umask(u);
516
517 if (r < 0 && errno != EEXIST) {
518 log_error("Failed to create fifo %s: %m", i->path);
519 r = -errno;
520 goto finish;
521 }
522
523 if (stat(i->path, &st) < 0) {
524 log_error("stat(%s) failed: %m", i->path);
525 r = -errno;
526 goto finish;
527 }
528
529 if (!S_ISFIFO(st.st_mode)) {
530 log_error("%s is not a fifo.", i->path);
531 r = -EEXIST;
532 goto finish;
533 }
534
535 if (i->mode_set)
536 if (chmod(i->path, i->mode) < 0) {
537 log_error("chmod(%s) failed: %m", i->path);
538 r = -errno;
539 goto finish;
540 }
541
542 if (i->uid_set || i->gid_set)
543 if (chown(i->path,
544 i->uid_set ? i->uid : (uid_t) -1,
545 i->gid_set ? i->gid : (gid_t) -1) < 0) {
546 log_error("chown(%s) failed: %m", i->path);
547 r = -errno;
548 goto finish;
549 }
550
551 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200552 }
553
Lennart Poetteringc904f642011-02-25 01:47:31 +0100554 if ((r = label_fix(i->path, false)) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200555 goto finish;
556
557 log_debug("%s created successfully.", i->path);
558
559finish:
560 if (fd >= 0)
561 close_nointr_nofail(fd);
562
563 return r;
564}
565
Michal Schmidta0896122011-12-15 21:32:50 +0100566static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200567 int r;
568
569 assert(i);
570
571 switch (i->type) {
572
573 case CREATE_FILE:
574 case TRUNCATE_FILE:
575 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200576 case CREATE_FIFO:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200577 case IGNORE_PATH:
578 break;
579
580 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100581 if (remove(instance) < 0 && errno != ENOENT) {
582 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200583 return -errno;
584 }
585
586 break;
587
588 case TRUNCATE_DIRECTORY:
589 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringad293f52011-08-21 20:05:51 +0200590 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false)) < 0 &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200591 r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100592 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200593 return r;
594 }
595
596 break;
597 }
598
599 return 0;
600}
601
Michal Schmidta0896122011-12-15 21:32:50 +0100602static int remove_item(Item *i) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100603 assert(i);
604
605 switch (i->type) {
606
607 case CREATE_FILE:
608 case TRUNCATE_FILE:
609 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200610 case CREATE_FIFO:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100611 case IGNORE_PATH:
612 break;
613
614 case REMOVE_PATH:
615 case TRUNCATE_DIRECTORY:
616 case RECURSIVE_REMOVE_PATH: {
617 int r = 0, k;
618 glob_t g;
619 char **fn;
620
621 zero(g);
622
623 errno = 0;
624 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
625
626 if (k != GLOB_NOMATCH) {
627 if (errno != 0)
628 errno = EIO;
629
630 log_error("glob(%s) failed: %m", i->path);
631 return -errno;
632 }
633 }
634
635 STRV_FOREACH(fn, g.gl_pathv)
Michal Schmidta0896122011-12-15 21:32:50 +0100636 if ((k = remove_item_instance(i, *fn)) < 0)
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100637 r = k;
638
639 globfree(&g);
640 return r;
641 }
642 }
643
644 return 0;
645}
646
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200647static int process_item(Item *i) {
648 int r, q, p;
649
650 assert(i);
651
652 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100653 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200654 p = arg_clean ? clean_item(i) : 0;
655
656 if (r < 0)
657 return r;
658
659 if (q < 0)
660 return q;
661
662 return p;
663}
664
665static void item_free(Item *i) {
666 assert(i);
667
668 free(i->path);
669 free(i);
670}
671
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200672static bool item_equal(Item *a, Item *b) {
673 assert(a);
674 assert(b);
675
676 if (!streq_ptr(a->path, b->path))
677 return false;
678
679 if (a->type != b->type)
680 return false;
681
682 if (a->uid_set != b->uid_set ||
683 (a->uid_set && a->uid != b->uid))
684 return false;
685
686 if (a->gid_set != b->gid_set ||
687 (a->gid_set && a->gid != b->gid))
688 return false;
689
690 if (a->mode_set != b->mode_set ||
691 (a->mode_set && a->mode != b->mode))
692 return false;
693
694 if (a->age_set != b->age_set ||
695 (a->age_set && a->age != b->age))
696 return false;
697
698 return true;
699}
700
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100701static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200702 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200703 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100704 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200705 Hashmap *h;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100706 int r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200707
708 assert(fname);
709 assert(line >= 1);
710 assert(buffer);
711
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200712 if (!(i = new0(Item, 1))) {
713 log_error("Out of memory");
714 return -ENOMEM;
715 }
716
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100717 if (sscanf(buffer,
718 "%c "
719 "%ms "
720 "%ms "
721 "%ms "
722 "%ms "
723 "%ms",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100724 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100725 &i->path,
726 &mode,
727 &user,
728 &group,
729 &age) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200730 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200731 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200732 goto finish;
733 }
Michal Schmidt66ccd032011-12-15 21:31:14 +0100734 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +0200735
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200736 if (i->type != CREATE_FILE &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200737 i->type != TRUNCATE_FILE &&
Gustavo Sverzut Barbieriabe35cc2010-10-19 23:06:34 -0200738 i->type != CREATE_DIRECTORY &&
739 i->type != TRUNCATE_DIRECTORY &&
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200740 i->type != CREATE_FIFO &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200741 i->type != IGNORE_PATH &&
742 i->type != REMOVE_PATH &&
743 i->type != RECURSIVE_REMOVE_PATH) {
744 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200745 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200746 goto finish;
747 }
748
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200749 if (!path_is_absolute(i->path)) {
750 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
751 r = -EBADMSG;
752 goto finish;
753 }
754
755 path_kill_slashes(i->path);
756
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100757 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200758 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200759 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200760 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200761
762 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200763 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +0200764
Lennart Poettering4b678342011-07-23 01:17:59 +0200765 r = get_user_creds(&u, &i->uid, NULL, NULL);
766 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200767 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
768 goto finish;
769 }
770
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200771 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200772 }
773
774 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200775 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +0200776
Lennart Poettering4b678342011-07-23 01:17:59 +0200777 r = get_group_creds(&g, &i->gid);
778 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200779 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
780 goto finish;
781 }
782
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200783 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200784 }
785
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200786 if (mode && !streq(mode, "-")) {
787 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +0200788
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200789 if (sscanf(mode, "%o", &m) != 1) {
790 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
791 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200792 goto finish;
793 }
794
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200795 i->mode = m;
796 i->mode_set = true;
797 } else
798 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
799
800 if (age && !streq(age, "-")) {
801 if (parse_usec(age, &i->age) < 0) {
802 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
803 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200804 goto finish;
805 }
806
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200807 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200808 }
809
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200810 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +0100811
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200812 if ((existing = hashmap_get(h, i->path))) {
813
814 /* Two identical items are fine */
815 if (!item_equal(existing, i))
816 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
817
818 r = 0;
819 goto finish;
820 }
821
822 if ((r = hashmap_put(h, i->path, i)) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200823 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200824 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200825 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200826
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200827 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200828 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200829
830finish:
Lennart Poettering5008d582010-09-28 02:34:02 +0200831 free(user);
832 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200833 free(mode);
834 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +0200835
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200836 if (i)
837 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200838
839 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200840}
841
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200842static int help(void) {
843
Lennart Poettering522d4a42011-02-13 15:08:15 +0100844 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
845 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200846 " -h --help Show this help\n"
847 " --create Create marked files/directories\n"
848 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100849 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +0100850 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200851 program_invocation_short_name);
852
853 return 0;
854}
855
856static int parse_argv(int argc, char *argv[]) {
857
858 enum {
859 ARG_CREATE,
860 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100861 ARG_REMOVE,
862 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200863 };
864
865 static const struct option options[] = {
866 { "help", no_argument, NULL, 'h' },
867 { "create", no_argument, NULL, ARG_CREATE },
868 { "clean", no_argument, NULL, ARG_CLEAN },
869 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100870 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200871 { NULL, 0, NULL, 0 }
872 };
873
874 int c;
875
876 assert(argc >= 0);
877 assert(argv);
878
879 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
880
881 switch (c) {
882
883 case 'h':
884 help();
885 return 0;
886
887 case ARG_CREATE:
888 arg_create = true;
889 break;
890
891 case ARG_CLEAN:
892 arg_clean = true;
893 break;
894
895 case ARG_REMOVE:
896 arg_remove = true;
897 break;
898
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100899 case ARG_PREFIX:
900 arg_prefix = optarg;
901 break;
902
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200903 case '?':
904 return -EINVAL;
905
906 default:
907 log_error("Unknown option code %c", c);
908 return -EINVAL;
909 }
910 }
911
912 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +0100913 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200914 return -EINVAL;
915 }
916
917 return 1;
918}
919
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100920static int read_config_file(const char *fn, bool ignore_enoent) {
921 FILE *f;
922 unsigned v = 0;
923 int r = 0;
924
925 assert(fn);
926
927 if (!(f = fopen(fn, "re"))) {
928
929 if (ignore_enoent && errno == ENOENT)
930 return 0;
931
932 log_error("Failed to open %s: %m", fn);
933 return -errno;
934 }
935
Kay Sievers772f8372011-04-25 21:38:21 +0200936 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100937 for (;;) {
938 char line[LINE_MAX], *l;
939 int k;
940
941 if (!(fgets(line, sizeof(line), f)))
942 break;
943
944 v++;
945
946 l = strstrip(line);
947 if (*l == '#' || *l == 0)
948 continue;
949
950 if ((k = parse_line(fn, v, l)) < 0)
951 if (r == 0)
952 r = k;
953 }
954
955 if (ferror(f)) {
956 log_error("Failed to read from file %s: %m", fn);
957 if (r == 0)
958 r = -EIO;
959 }
960
961 fclose(f);
962
963 return r;
964}
965
Lennart Poettering5008d582010-09-28 02:34:02 +0200966int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100967 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200968 Item *i;
969 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +0200970
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200971 if ((r = parse_argv(argc, argv)) <= 0)
972 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
973
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +0100974 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +0200975 log_parse_environment();
976 log_open();
977
Lennart Poettering4c126262011-08-01 20:52:18 +0200978 umask(0022);
979
Lennart Poettering5008d582010-09-28 02:34:02 +0200980 label_init();
981
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100982 items = hashmap_new(string_hash_func, string_compare_func);
983 globs = hashmap_new(string_hash_func, string_compare_func);
984
985 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200986 log_error("Out of memory");
987 r = EXIT_FAILURE;
988 goto finish;
989 }
990
Lennart Poettering5008d582010-09-28 02:34:02 +0200991 r = EXIT_SUCCESS;
992
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100993 if (optind < argc) {
994 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +0200995
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100996 for (j = optind; j < argc; j++)
997 if (read_config_file(argv[j], false) < 0)
998 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +0200999
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001000 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001001 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001002
Kay Sievers44143302011-04-28 23:51:24 +02001003 r = conf_files_list(&files, ".conf",
1004 "/run/tmpfiles.d",
1005 "/etc/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +02001006 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +02001007 "/usr/lib/tmpfiles.d",
1008 NULL);
1009 if (r < 0) {
1010 r = EXIT_FAILURE;
1011 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1012 goto finish;
1013 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001014
Kay Sievers772f8372011-04-25 21:38:21 +02001015 STRV_FOREACH(f, files) {
1016 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001017 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001018 }
1019
Kay Sievers772f8372011-04-25 21:38:21 +02001020 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001021 }
1022
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001023 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001024 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001025
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001026 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001027 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001028
Lennart Poettering5008d582010-09-28 02:34:02 +02001029finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001030 while ((i = hashmap_steal_first(items)))
1031 item_free(i);
1032
Lennart Poettering17b90522011-02-14 21:55:06 +01001033 while ((i = hashmap_steal_first(globs)))
1034 item_free(i);
1035
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001036 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001037 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001038
Lennart Poettering17b90522011-02-14 21:55:06 +01001039 set_free_free(unix_sockets);
1040
Lennart Poettering29003cf2010-10-19 19:36:45 +02001041 label_finish();
1042
Lennart Poettering5008d582010-09-28 02:34:02 +02001043 return r;
1044}