blob: 18067c4b1f72ef59c9f8c6984324fad6df3344cc [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',
Michal Schmidta8d88782011-12-15 23:11:07 +010064 RECURSIVE_REMOVE_PATH = 'R',
65 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010066} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020067
68typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010069 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020070
71 char *path;
Lennart Poettering5008d582010-09-28 02:34:02 +020072 uid_t uid;
73 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020074 mode_t mode;
75 usec_t age;
76
77 bool uid_set:1;
78 bool gid_set:1;
79 bool mode_set:1;
80 bool age_set:1;
81} Item;
82
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010083static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010084static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020085
86static bool arg_create = false;
87static bool arg_clean = false;
88static bool arg_remove = false;
89
Lennart Poetteringfba6e682011-02-13 14:00:54 +010090static const char *arg_prefix = NULL;
91
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020092#define MAX_DEPTH 256
93
Michal Schmidt66ccd032011-12-15 21:31:14 +010094static bool needs_glob(ItemType t) {
Michal Schmidta8d88782011-12-15 23:11:07 +010095 return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RECURSIVE_RELABEL_PATH;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010096}
97
98static struct Item* find_glob(Hashmap *h, const char *match) {
99 Item *j;
100 Iterator i;
101
102 HASHMAP_FOREACH(j, h, i)
103 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
104 return j;
105
106 return NULL;
107}
108
Lennart Poettering17b90522011-02-14 21:55:06 +0100109static void load_unix_sockets(void) {
110 FILE *f = NULL;
111 char line[LINE_MAX];
112
113 if (unix_sockets)
114 return;
115
116 /* We maintain a cache of the sockets we found in
117 * /proc/net/unix to speed things up a little. */
118
119 if (!(unix_sockets = set_new(string_hash_func, string_compare_func)))
120 return;
121
122 if (!(f = fopen("/proc/net/unix", "re")))
123 return;
124
125 if (!(fgets(line, sizeof(line), f)))
126 goto fail;
127
128 for (;;) {
129 char *p, *s;
130 int k;
131
132 if (!(fgets(line, sizeof(line), f)))
133 break;
134
135 truncate_nl(line);
136
137 if (strlen(line) < 53)
138 continue;
139
140 p = line + 53;
141 p += strspn(p, WHITESPACE);
142 p += strcspn(p, WHITESPACE);
143 p += strspn(p, WHITESPACE);
144
145 if (*p != '/')
146 continue;
147
148 if (!(s = strdup(p)))
149 goto fail;
150
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100151 path_kill_slashes(s);
152
Lennart Poettering17b90522011-02-14 21:55:06 +0100153 if ((k = set_put(unix_sockets, s)) < 0) {
154 free(s);
155
156 if (k != -EEXIST)
157 goto fail;
158 }
159 }
160
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200161 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100162 return;
163
164fail:
165 set_free_free(unix_sockets);
166 unix_sockets = NULL;
167
168 if (f)
169 fclose(f);
170}
171
172static bool unix_socket_alive(const char *fn) {
173 assert(fn);
174
175 load_unix_sockets();
176
177 if (unix_sockets)
178 return !!set_get(unix_sockets, (char*) fn);
179
180 /* We don't know, so assume yes */
181 return true;
182}
183
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200184static int dir_cleanup(
185 const char *p,
186 DIR *d,
187 const struct stat *ds,
188 usec_t cutoff,
189 dev_t rootdev,
190 bool mountpoint,
191 int maxdepth)
192{
193 struct dirent *dent;
194 struct timespec times[2];
195 bool deleted = false;
196 char *sub_path = NULL;
197 int r = 0;
198
199 while ((dent = readdir(d))) {
200 struct stat s;
201 usec_t age;
202
203 if (streq(dent->d_name, ".") ||
204 streq(dent->d_name, ".."))
205 continue;
206
207 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
208
209 if (errno != ENOENT) {
210 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
211 r = -errno;
212 }
213
214 continue;
215 }
216
217 /* Stay on the same filesystem */
218 if (s.st_dev != rootdev)
219 continue;
220
221 /* Do not delete read-only files owned by root */
222 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
223 continue;
224
225 free(sub_path);
226 sub_path = NULL;
227
228 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
229 log_error("Out of memory");
230 r = -ENOMEM;
231 goto finish;
232 }
233
234 /* Is there an item configured for this path? */
235 if (hashmap_get(items, sub_path))
236 continue;
237
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100238 if (find_glob(globs, sub_path))
239 continue;
240
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200241 if (S_ISDIR(s.st_mode)) {
242
243 if (mountpoint &&
244 streq(dent->d_name, "lost+found") &&
245 s.st_uid == 0)
246 continue;
247
248 if (maxdepth <= 0)
249 log_warning("Reached max depth on %s.", sub_path);
250 else {
251 DIR *sub_dir;
252 int q;
253
Lennart Poetteringa2477552010-12-28 14:20:21 +0100254 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200255 if (sub_dir == NULL) {
256 if (errno != ENOENT) {
257 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
258 r = -errno;
259 }
260
261 continue;
262 }
263
264 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
265 closedir(sub_dir);
266
267 if (q < 0)
268 r = q;
269 }
270
271 /* Ignore ctime, we change it when deleting */
272 age = MAX(timespec_load(&s.st_mtim),
273 timespec_load(&s.st_atim));
274 if (age >= cutoff)
275 continue;
276
277 log_debug("rmdir '%s'\n", sub_path);
278
279 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
280 if (errno != ENOENT && errno != ENOTEMPTY) {
281 log_error("rmdir(%s): %m", sub_path);
282 r = -errno;
283 }
284 }
285
286 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100287 /* Skip files for which the sticky bit is
288 * set. These are semantics we define, and are
289 * unknown elsewhere. See XDG_RUNTIME_DIR
290 * specification for details. */
291 if (s.st_mode & S_ISVTX)
292 continue;
293
Lennart Poettering17b90522011-02-14 21:55:06 +0100294 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200295 if (streq(dent->d_name, ".journal") &&
296 s.st_uid == 0)
297 continue;
298
299 if (streq(dent->d_name, "aquota.user") ||
300 streq(dent->d_name, "aquota.group"))
301 continue;
302 }
303
Lennart Poettering17b90522011-02-14 21:55:06 +0100304 /* Ignore sockets that are listed in /proc/net/unix */
305 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
306 continue;
307
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100308 /* Ignore device nodes */
309 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
310 continue;
311
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200312 age = MAX3(timespec_load(&s.st_mtim),
313 timespec_load(&s.st_atim),
314 timespec_load(&s.st_ctim));
315
316 if (age >= cutoff)
317 continue;
318
319 log_debug("unlink '%s'\n", sub_path);
320
321 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
322 if (errno != ENOENT) {
323 log_error("unlink(%s): %m", sub_path);
324 r = -errno;
325 }
326 }
327
328 deleted = true;
329 }
330 }
331
332finish:
333 if (deleted) {
334 /* Restore original directory timestamps */
335 times[0] = ds->st_atim;
336 times[1] = ds->st_mtim;
337
338 if (futimens(dirfd(d), times) < 0)
339 log_error("utimensat(%s): %m", p);
340 }
341
342 free(sub_path);
343
344 return r;
345}
346
347static int clean_item(Item *i) {
348 DIR *d;
349 struct stat s, ps;
350 bool mountpoint;
351 int r;
352 usec_t cutoff, n;
353
354 assert(i);
355
356 if (i->type != CREATE_DIRECTORY &&
357 i->type != TRUNCATE_DIRECTORY &&
358 i->type != IGNORE_PATH)
359 return 0;
360
361 if (!i->age_set || i->age <= 0)
362 return 0;
363
364 n = now(CLOCK_REALTIME);
365 if (n < i->age)
366 return 0;
367
368 cutoff = n - i->age;
369
370 d = opendir(i->path);
371 if (!d) {
372 if (errno == ENOENT)
373 return 0;
374
375 log_error("Failed to open directory %s: %m", i->path);
376 return -errno;
377 }
378
379 if (fstat(dirfd(d), &s) < 0) {
380 log_error("stat(%s) failed: %m", i->path);
381 r = -errno;
382 goto finish;
383 }
384
385 if (!S_ISDIR(s.st_mode)) {
386 log_error("%s is not a directory.", i->path);
387 r = -ENOTDIR;
388 goto finish;
389 }
390
391 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
392 log_error("stat(%s/..) failed: %m", i->path);
393 r = -errno;
394 goto finish;
395 }
396
397 mountpoint = s.st_dev != ps.st_dev ||
398 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
399
400 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
401
402finish:
403 if (d)
404 closedir(d);
405
406 return r;
407}
408
Michal Schmidta8d88782011-12-15 23:11:07 +0100409static int recursive_relabel_children(const char *path) {
410 DIR *d;
411 int ret = 0;
412
413 /* This returns the first error we run into, but nevertheless
414 * tries to go on */
415
416 d = opendir(path);
417 if (!d)
418 return errno == ENOENT ? 0 : -errno;
419
420 for (;;) {
421 struct dirent buf, *de;
422 bool is_dir;
423 int r;
424 char *entry_path;
425
426 r = readdir_r(d, &buf, &de);
427 if (r != 0) {
428 if (ret == 0)
429 ret = -r;
430 break;
431 }
432
433 if (!de)
434 break;
435
436 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
437 continue;
438
439 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
440 if (ret == 0)
441 ret = -ENOMEM;
442 continue;
443 }
444
445 if (de->d_type == DT_UNKNOWN) {
446 struct stat st;
447
448 if (lstat(entry_path, &st) < 0) {
449 if (ret == 0 && errno != ENOENT)
450 ret = -errno;
451 free(entry_path);
452 continue;
453 }
454
455 is_dir = S_ISDIR(st.st_mode);
456
457 } else
458 is_dir = de->d_type == DT_DIR;
459
460 r = label_fix(entry_path, false);
461 if (r < 0) {
462 if (ret == 0 && r != -ENOENT)
463 ret = r;
464 free(entry_path);
465 continue;
466 }
467
468 if (is_dir) {
469 r = recursive_relabel_children(entry_path);
470 if (r < 0 && ret == 0)
471 ret = r;
472 }
473
474 free(entry_path);
475 }
476
477 closedir(d);
478
479 return ret;
480}
481
482static int recursive_relabel(Item *i, const char *path) {
483 int r;
484 struct stat st;
485
486 r = label_fix(path, false);
487 if (r < 0)
488 return r;
489
490 if (lstat(path, &st) < 0)
491 return -errno;
492
493 if (S_ISDIR(st.st_mode))
494 r = recursive_relabel_children(path);
495
496 return r;
497}
498
Michal Schmidt99e68c02011-12-15 23:45:26 +0100499static int glob_item(Item *i, int (*action)(Item *, const char *)) {
500 int r = 0, k;
501 glob_t g;
502 char **fn;
503
504 zero(g);
505
506 errno = 0;
507 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
508
509 if (k != GLOB_NOMATCH) {
510 if (errno != 0)
511 errno = EIO;
512
513 log_error("glob(%s) failed: %m", i->path);
514 return -errno;
515 }
516 }
517
518 STRV_FOREACH(fn, g.gl_pathv)
519 if ((k = action(i, *fn)) < 0)
520 r = k;
521
522 globfree(&g);
523 return r;
524}
525
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100526static int item_set_perms(Item *i) {
527 if (i->mode_set)
528 if (chmod(i->path, i->mode) < 0) {
529 log_error("chmod(%s) failed: %m", i->path);
530 return -errno;
531 }
532
533 if (i->uid_set || i->gid_set)
534 if (chown(i->path,
535 i->uid_set ? i->uid : (uid_t) -1,
536 i->gid_set ? i->gid : (gid_t) -1) < 0) {
537
538 log_error("chown(%s) failed: %m", i->path);
539 return -errno;
540 }
541
542 return label_fix(i->path, false);
543}
544
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200545static int create_item(Item *i) {
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100546 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200547 mode_t u;
548 struct stat st;
549
550 assert(i);
551
552 switch (i->type) {
553
554 case IGNORE_PATH:
555 case REMOVE_PATH:
556 case RECURSIVE_REMOVE_PATH:
557 return 0;
558
559 case CREATE_FILE:
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100560 case TRUNCATE_FILE: {
561 int fd;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200562
563 u = umask(0);
564 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
565 (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
566 umask(u);
567
568 if (fd < 0) {
569 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100570 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200571 }
572
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100573 close_nointr_nofail(fd);
574
575 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200576 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100577 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200578 }
579
580 if (!S_ISREG(st.st_mode)) {
581 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100582 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200583 }
584
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100585 r = item_set_perms(i);
586 if (r < 0)
587 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200588
589 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100590 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200591
592 case TRUNCATE_DIRECTORY:
593 case CREATE_DIRECTORY:
594
595 u = umask(0);
Kay Sievers33366862011-04-03 22:21:21 +0200596 mkdir_parents(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200597 r = mkdir(i->path, i->mode);
598 umask(u);
599
600 if (r < 0 && errno != EEXIST) {
601 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100602 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200603 }
604
605 if (stat(i->path, &st) < 0) {
606 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100607 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200608 }
609
610 if (!S_ISDIR(st.st_mode)) {
611 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100612 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200613 }
614
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100615 r = item_set_perms(i);
616 if (r < 0)
617 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200618
619 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200620
621 case CREATE_FIFO:
622
623 u = umask(0);
624 r = mkfifo(i->path, i->mode);
625 umask(u);
626
627 if (r < 0 && errno != EEXIST) {
628 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100629 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200630 }
631
632 if (stat(i->path, &st) < 0) {
633 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100634 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200635 }
636
637 if (!S_ISFIFO(st.st_mode)) {
638 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100639 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200640 }
641
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100642 r = item_set_perms(i);
643 if (r < 0)
644 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200645
646 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100647
648 case RECURSIVE_RELABEL_PATH:
649
650 r = glob_item(i, recursive_relabel);
651 if (r < 0)
652 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200653 }
654
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200655 log_debug("%s created successfully.", i->path);
656
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100657 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200658}
659
Michal Schmidta0896122011-12-15 21:32:50 +0100660static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200661 int r;
662
663 assert(i);
664
665 switch (i->type) {
666
667 case CREATE_FILE:
668 case TRUNCATE_FILE:
669 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200670 case CREATE_FIFO:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200671 case IGNORE_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100672 case RECURSIVE_RELABEL_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200673 break;
674
675 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100676 if (remove(instance) < 0 && errno != ENOENT) {
677 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200678 return -errno;
679 }
680
681 break;
682
683 case TRUNCATE_DIRECTORY:
684 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringad293f52011-08-21 20:05:51 +0200685 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false)) < 0 &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200686 r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100687 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200688 return r;
689 }
690
691 break;
692 }
693
694 return 0;
695}
696
Michal Schmidta0896122011-12-15 21:32:50 +0100697static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100698 int r = 0;
699
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100700 assert(i);
701
702 switch (i->type) {
703
704 case CREATE_FILE:
705 case TRUNCATE_FILE:
706 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200707 case CREATE_FIFO:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100708 case IGNORE_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100709 case RECURSIVE_RELABEL_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100710 break;
711
712 case REMOVE_PATH:
713 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100714 case RECURSIVE_REMOVE_PATH:
715 r = glob_item(i, remove_item_instance);
716 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100717 }
718
Michal Schmidt99e68c02011-12-15 23:45:26 +0100719 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100720}
721
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200722static int process_item(Item *i) {
723 int r, q, p;
724
725 assert(i);
726
727 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100728 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200729 p = arg_clean ? clean_item(i) : 0;
730
731 if (r < 0)
732 return r;
733
734 if (q < 0)
735 return q;
736
737 return p;
738}
739
740static void item_free(Item *i) {
741 assert(i);
742
743 free(i->path);
744 free(i);
745}
746
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200747static bool item_equal(Item *a, Item *b) {
748 assert(a);
749 assert(b);
750
751 if (!streq_ptr(a->path, b->path))
752 return false;
753
754 if (a->type != b->type)
755 return false;
756
757 if (a->uid_set != b->uid_set ||
758 (a->uid_set && a->uid != b->uid))
759 return false;
760
761 if (a->gid_set != b->gid_set ||
762 (a->gid_set && a->gid != b->gid))
763 return false;
764
765 if (a->mode_set != b->mode_set ||
766 (a->mode_set && a->mode != b->mode))
767 return false;
768
769 if (a->age_set != b->age_set ||
770 (a->age_set && a->age != b->age))
771 return false;
772
773 return true;
774}
775
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100776static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200777 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200778 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100779 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200780 Hashmap *h;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100781 int r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200782
783 assert(fname);
784 assert(line >= 1);
785 assert(buffer);
786
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200787 if (!(i = new0(Item, 1))) {
788 log_error("Out of memory");
789 return -ENOMEM;
790 }
791
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100792 if (sscanf(buffer,
793 "%c "
794 "%ms "
795 "%ms "
796 "%ms "
797 "%ms "
798 "%ms",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100799 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100800 &i->path,
801 &mode,
802 &user,
803 &group,
804 &age) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200805 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200806 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200807 goto finish;
808 }
809
Michal Schmidta8d88782011-12-15 23:11:07 +0100810 if (type != CREATE_FILE &&
811 type != TRUNCATE_FILE &&
812 type != CREATE_DIRECTORY &&
813 type != TRUNCATE_DIRECTORY &&
814 type != CREATE_FIFO &&
815 type != IGNORE_PATH &&
816 type != REMOVE_PATH &&
817 type != RECURSIVE_REMOVE_PATH &&
818 type != RECURSIVE_RELABEL_PATH) {
819 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200820 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200821 goto finish;
822 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100823 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +0200824
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200825 if (!path_is_absolute(i->path)) {
826 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
827 r = -EBADMSG;
828 goto finish;
829 }
830
831 path_kill_slashes(i->path);
832
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100833 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200834 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200835 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200836 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200837
838 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200839 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +0200840
Lennart Poettering4b678342011-07-23 01:17:59 +0200841 r = get_user_creds(&u, &i->uid, NULL, NULL);
842 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200843 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
844 goto finish;
845 }
846
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200847 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200848 }
849
850 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200851 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +0200852
Lennart Poettering4b678342011-07-23 01:17:59 +0200853 r = get_group_creds(&g, &i->gid);
854 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200855 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
856 goto finish;
857 }
858
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200859 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200860 }
861
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200862 if (mode && !streq(mode, "-")) {
863 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +0200864
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200865 if (sscanf(mode, "%o", &m) != 1) {
866 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
867 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200868 goto finish;
869 }
870
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200871 i->mode = m;
872 i->mode_set = true;
873 } else
874 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
875
876 if (age && !streq(age, "-")) {
877 if (parse_usec(age, &i->age) < 0) {
878 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
879 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200880 goto finish;
881 }
882
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200883 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200884 }
885
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200886 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +0100887
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200888 if ((existing = hashmap_get(h, i->path))) {
889
890 /* Two identical items are fine */
891 if (!item_equal(existing, i))
892 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
893
894 r = 0;
895 goto finish;
896 }
897
898 if ((r = hashmap_put(h, i->path, i)) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200899 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200900 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200901 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200902
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200903 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200904 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200905
906finish:
Lennart Poettering5008d582010-09-28 02:34:02 +0200907 free(user);
908 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200909 free(mode);
910 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +0200911
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200912 if (i)
913 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200914
915 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200916}
917
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200918static int help(void) {
919
Lennart Poettering522d4a42011-02-13 15:08:15 +0100920 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
921 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200922 " -h --help Show this help\n"
923 " --create Create marked files/directories\n"
924 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100925 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +0100926 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200927 program_invocation_short_name);
928
929 return 0;
930}
931
932static int parse_argv(int argc, char *argv[]) {
933
934 enum {
935 ARG_CREATE,
936 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100937 ARG_REMOVE,
938 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200939 };
940
941 static const struct option options[] = {
942 { "help", no_argument, NULL, 'h' },
943 { "create", no_argument, NULL, ARG_CREATE },
944 { "clean", no_argument, NULL, ARG_CLEAN },
945 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100946 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200947 { NULL, 0, NULL, 0 }
948 };
949
950 int c;
951
952 assert(argc >= 0);
953 assert(argv);
954
955 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
956
957 switch (c) {
958
959 case 'h':
960 help();
961 return 0;
962
963 case ARG_CREATE:
964 arg_create = true;
965 break;
966
967 case ARG_CLEAN:
968 arg_clean = true;
969 break;
970
971 case ARG_REMOVE:
972 arg_remove = true;
973 break;
974
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100975 case ARG_PREFIX:
976 arg_prefix = optarg;
977 break;
978
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200979 case '?':
980 return -EINVAL;
981
982 default:
983 log_error("Unknown option code %c", c);
984 return -EINVAL;
985 }
986 }
987
988 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +0100989 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200990 return -EINVAL;
991 }
992
993 return 1;
994}
995
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100996static int read_config_file(const char *fn, bool ignore_enoent) {
997 FILE *f;
998 unsigned v = 0;
999 int r = 0;
1000
1001 assert(fn);
1002
1003 if (!(f = fopen(fn, "re"))) {
1004
1005 if (ignore_enoent && errno == ENOENT)
1006 return 0;
1007
1008 log_error("Failed to open %s: %m", fn);
1009 return -errno;
1010 }
1011
Kay Sievers772f8372011-04-25 21:38:21 +02001012 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001013 for (;;) {
1014 char line[LINE_MAX], *l;
1015 int k;
1016
1017 if (!(fgets(line, sizeof(line), f)))
1018 break;
1019
1020 v++;
1021
1022 l = strstrip(line);
1023 if (*l == '#' || *l == 0)
1024 continue;
1025
1026 if ((k = parse_line(fn, v, l)) < 0)
1027 if (r == 0)
1028 r = k;
1029 }
1030
1031 if (ferror(f)) {
1032 log_error("Failed to read from file %s: %m", fn);
1033 if (r == 0)
1034 r = -EIO;
1035 }
1036
1037 fclose(f);
1038
1039 return r;
1040}
1041
Lennart Poettering5008d582010-09-28 02:34:02 +02001042int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001043 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001044 Item *i;
1045 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001046
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001047 if ((r = parse_argv(argc, argv)) <= 0)
1048 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1049
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001050 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001051 log_parse_environment();
1052 log_open();
1053
Lennart Poettering4c126262011-08-01 20:52:18 +02001054 umask(0022);
1055
Lennart Poettering5008d582010-09-28 02:34:02 +02001056 label_init();
1057
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001058 items = hashmap_new(string_hash_func, string_compare_func);
1059 globs = hashmap_new(string_hash_func, string_compare_func);
1060
1061 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001062 log_error("Out of memory");
1063 r = EXIT_FAILURE;
1064 goto finish;
1065 }
1066
Lennart Poettering5008d582010-09-28 02:34:02 +02001067 r = EXIT_SUCCESS;
1068
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001069 if (optind < argc) {
1070 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001071
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001072 for (j = optind; j < argc; j++)
1073 if (read_config_file(argv[j], false) < 0)
1074 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001075
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001076 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001077 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001078
Kay Sievers44143302011-04-28 23:51:24 +02001079 r = conf_files_list(&files, ".conf",
1080 "/run/tmpfiles.d",
1081 "/etc/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +02001082 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +02001083 "/usr/lib/tmpfiles.d",
1084 NULL);
1085 if (r < 0) {
1086 r = EXIT_FAILURE;
1087 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1088 goto finish;
1089 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001090
Kay Sievers772f8372011-04-25 21:38:21 +02001091 STRV_FOREACH(f, files) {
1092 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001093 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001094 }
1095
Kay Sievers772f8372011-04-25 21:38:21 +02001096 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001097 }
1098
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001099 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001100 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001101
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001102 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001103 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001104
Lennart Poettering5008d582010-09-28 02:34:02 +02001105finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001106 while ((i = hashmap_steal_first(items)))
1107 item_free(i);
1108
Lennart Poettering17b90522011-02-14 21:55:06 +01001109 while ((i = hashmap_steal_first(globs)))
1110 item_free(i);
1111
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001112 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001113 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001114
Lennart Poettering17b90522011-02-14 21:55:06 +01001115 set_free_free(unix_sockets);
1116
Lennart Poettering29003cf2010-10-19 19:36:45 +02001117 label_finish();
1118
Lennart Poettering5008d582010-09-28 02:34:02 +02001119 return r;
1120}