blob: 13950824e087116e0fff8a3b373e3ebbfbdc0a8c [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 Schmidt062e01b2011-12-16 18:00:11 +0100409static int item_set_perms(Item *i, const char *path) {
410 /* not using i->path directly because it may be a glob */
411 if (i->mode_set)
412 if (chmod(path, i->mode) < 0) {
413 log_error("chmod(%s) failed: %m", path);
414 return -errno;
415 }
416
417 if (i->uid_set || i->gid_set)
418 if (chown(path,
419 i->uid_set ? i->uid : (uid_t) -1,
420 i->gid_set ? i->gid : (gid_t) -1) < 0) {
421
422 log_error("chown(%s) failed: %m", path);
423 return -errno;
424 }
425
426 return label_fix(path, false);
427}
428
429static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100430 DIR *d;
431 int ret = 0;
432
433 /* This returns the first error we run into, but nevertheless
434 * tries to go on */
435
436 d = opendir(path);
437 if (!d)
438 return errno == ENOENT ? 0 : -errno;
439
440 for (;;) {
441 struct dirent buf, *de;
442 bool is_dir;
443 int r;
444 char *entry_path;
445
446 r = readdir_r(d, &buf, &de);
447 if (r != 0) {
448 if (ret == 0)
449 ret = -r;
450 break;
451 }
452
453 if (!de)
454 break;
455
456 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
457 continue;
458
459 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
460 if (ret == 0)
461 ret = -ENOMEM;
462 continue;
463 }
464
465 if (de->d_type == DT_UNKNOWN) {
466 struct stat st;
467
468 if (lstat(entry_path, &st) < 0) {
469 if (ret == 0 && errno != ENOENT)
470 ret = -errno;
471 free(entry_path);
472 continue;
473 }
474
475 is_dir = S_ISDIR(st.st_mode);
476
477 } else
478 is_dir = de->d_type == DT_DIR;
479
Michal Schmidt062e01b2011-12-16 18:00:11 +0100480 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100481 if (r < 0) {
482 if (ret == 0 && r != -ENOENT)
483 ret = r;
484 free(entry_path);
485 continue;
486 }
487
488 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100489 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100490 if (r < 0 && ret == 0)
491 ret = r;
492 }
493
494 free(entry_path);
495 }
496
497 closedir(d);
498
499 return ret;
500}
501
502static int recursive_relabel(Item *i, const char *path) {
503 int r;
504 struct stat st;
505
Michal Schmidt062e01b2011-12-16 18:00:11 +0100506 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100507 if (r < 0)
508 return r;
509
510 if (lstat(path, &st) < 0)
511 return -errno;
512
513 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100514 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100515
516 return r;
517}
518
Michal Schmidt99e68c02011-12-15 23:45:26 +0100519static int glob_item(Item *i, int (*action)(Item *, const char *)) {
520 int r = 0, k;
521 glob_t g;
522 char **fn;
523
524 zero(g);
525
526 errno = 0;
527 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
528
529 if (k != GLOB_NOMATCH) {
530 if (errno != 0)
531 errno = EIO;
532
533 log_error("glob(%s) failed: %m", i->path);
534 return -errno;
535 }
536 }
537
538 STRV_FOREACH(fn, g.gl_pathv)
539 if ((k = action(i, *fn)) < 0)
540 r = k;
541
542 globfree(&g);
543 return r;
544}
545
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200546static int create_item(Item *i) {
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100547 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200548 mode_t u;
549 struct stat st;
550
551 assert(i);
552
553 switch (i->type) {
554
555 case IGNORE_PATH:
556 case REMOVE_PATH:
557 case RECURSIVE_REMOVE_PATH:
558 return 0;
559
560 case CREATE_FILE:
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100561 case TRUNCATE_FILE: {
562 int fd;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200563
564 u = umask(0);
565 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
566 (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
567 umask(u);
568
569 if (fd < 0) {
570 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100571 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200572 }
573
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100574 close_nointr_nofail(fd);
575
576 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200577 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100578 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200579 }
580
581 if (!S_ISREG(st.st_mode)) {
582 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100583 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200584 }
585
Michal Schmidt062e01b2011-12-16 18:00:11 +0100586 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100587 if (r < 0)
588 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200589
590 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100591 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200592
593 case TRUNCATE_DIRECTORY:
594 case CREATE_DIRECTORY:
595
596 u = umask(0);
Kay Sievers33366862011-04-03 22:21:21 +0200597 mkdir_parents(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200598 r = mkdir(i->path, i->mode);
599 umask(u);
600
601 if (r < 0 && errno != EEXIST) {
602 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100603 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200604 }
605
606 if (stat(i->path, &st) < 0) {
607 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100608 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200609 }
610
611 if (!S_ISDIR(st.st_mode)) {
612 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100613 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200614 }
615
Michal Schmidt062e01b2011-12-16 18:00:11 +0100616 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100617 if (r < 0)
618 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200619
620 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200621
622 case CREATE_FIFO:
623
624 u = umask(0);
625 r = mkfifo(i->path, i->mode);
626 umask(u);
627
628 if (r < 0 && errno != EEXIST) {
629 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100630 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200631 }
632
633 if (stat(i->path, &st) < 0) {
634 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100635 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200636 }
637
638 if (!S_ISFIFO(st.st_mode)) {
639 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100640 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200641 }
642
Michal Schmidt062e01b2011-12-16 18:00:11 +0100643 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100644 if (r < 0)
645 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200646
647 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100648
649 case RECURSIVE_RELABEL_PATH:
650
651 r = glob_item(i, recursive_relabel);
652 if (r < 0)
653 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200654 }
655
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200656 log_debug("%s created successfully.", i->path);
657
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100658 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200659}
660
Michal Schmidta0896122011-12-15 21:32:50 +0100661static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200662 int r;
663
664 assert(i);
665
666 switch (i->type) {
667
668 case CREATE_FILE:
669 case TRUNCATE_FILE:
670 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200671 case CREATE_FIFO:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200672 case IGNORE_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100673 case RECURSIVE_RELABEL_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200674 break;
675
676 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100677 if (remove(instance) < 0 && errno != ENOENT) {
678 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200679 return -errno;
680 }
681
682 break;
683
684 case TRUNCATE_DIRECTORY:
685 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringad293f52011-08-21 20:05:51 +0200686 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false)) < 0 &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200687 r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100688 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200689 return r;
690 }
691
692 break;
693 }
694
695 return 0;
696}
697
Michal Schmidta0896122011-12-15 21:32:50 +0100698static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100699 int r = 0;
700
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100701 assert(i);
702
703 switch (i->type) {
704
705 case CREATE_FILE:
706 case TRUNCATE_FILE:
707 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200708 case CREATE_FIFO:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100709 case IGNORE_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100710 case RECURSIVE_RELABEL_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100711 break;
712
713 case REMOVE_PATH:
714 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100715 case RECURSIVE_REMOVE_PATH:
716 r = glob_item(i, remove_item_instance);
717 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100718 }
719
Michal Schmidt99e68c02011-12-15 23:45:26 +0100720 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100721}
722
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200723static int process_item(Item *i) {
724 int r, q, p;
725
726 assert(i);
727
728 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100729 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200730 p = arg_clean ? clean_item(i) : 0;
731
732 if (r < 0)
733 return r;
734
735 if (q < 0)
736 return q;
737
738 return p;
739}
740
741static void item_free(Item *i) {
742 assert(i);
743
744 free(i->path);
745 free(i);
746}
747
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200748static bool item_equal(Item *a, Item *b) {
749 assert(a);
750 assert(b);
751
752 if (!streq_ptr(a->path, b->path))
753 return false;
754
755 if (a->type != b->type)
756 return false;
757
758 if (a->uid_set != b->uid_set ||
759 (a->uid_set && a->uid != b->uid))
760 return false;
761
762 if (a->gid_set != b->gid_set ||
763 (a->gid_set && a->gid != b->gid))
764 return false;
765
766 if (a->mode_set != b->mode_set ||
767 (a->mode_set && a->mode != b->mode))
768 return false;
769
770 if (a->age_set != b->age_set ||
771 (a->age_set && a->age != b->age))
772 return false;
773
774 return true;
775}
776
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100777static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200778 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200779 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100780 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200781 Hashmap *h;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100782 int r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200783
784 assert(fname);
785 assert(line >= 1);
786 assert(buffer);
787
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200788 if (!(i = new0(Item, 1))) {
789 log_error("Out of memory");
790 return -ENOMEM;
791 }
792
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100793 if (sscanf(buffer,
794 "%c "
795 "%ms "
796 "%ms "
797 "%ms "
798 "%ms "
799 "%ms",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100800 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100801 &i->path,
802 &mode,
803 &user,
804 &group,
805 &age) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200806 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200807 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200808 goto finish;
809 }
810
Michal Schmidta8d88782011-12-15 23:11:07 +0100811 if (type != CREATE_FILE &&
812 type != TRUNCATE_FILE &&
813 type != CREATE_DIRECTORY &&
814 type != TRUNCATE_DIRECTORY &&
815 type != CREATE_FIFO &&
816 type != IGNORE_PATH &&
817 type != REMOVE_PATH &&
818 type != RECURSIVE_REMOVE_PATH &&
819 type != RECURSIVE_RELABEL_PATH) {
820 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200821 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200822 goto finish;
823 }
Michal Schmidta8d88782011-12-15 23:11:07 +0100824 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +0200825
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200826 if (!path_is_absolute(i->path)) {
827 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
828 r = -EBADMSG;
829 goto finish;
830 }
831
832 path_kill_slashes(i->path);
833
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100834 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200835 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200836 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200837 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200838
839 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200840 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +0200841
Lennart Poettering4b678342011-07-23 01:17:59 +0200842 r = get_user_creds(&u, &i->uid, NULL, NULL);
843 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200844 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
845 goto finish;
846 }
847
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200848 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200849 }
850
851 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200852 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +0200853
Lennart Poettering4b678342011-07-23 01:17:59 +0200854 r = get_group_creds(&g, &i->gid);
855 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200856 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
857 goto finish;
858 }
859
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200860 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200861 }
862
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200863 if (mode && !streq(mode, "-")) {
864 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +0200865
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200866 if (sscanf(mode, "%o", &m) != 1) {
867 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
868 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200869 goto finish;
870 }
871
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200872 i->mode = m;
873 i->mode_set = true;
874 } else
875 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
876
877 if (age && !streq(age, "-")) {
878 if (parse_usec(age, &i->age) < 0) {
879 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
880 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200881 goto finish;
882 }
883
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200884 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200885 }
886
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200887 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +0100888
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200889 if ((existing = hashmap_get(h, i->path))) {
890
891 /* Two identical items are fine */
892 if (!item_equal(existing, i))
893 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
894
895 r = 0;
896 goto finish;
897 }
898
899 if ((r = hashmap_put(h, i->path, i)) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200900 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200901 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200902 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200903
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200904 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200905 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200906
907finish:
Lennart Poettering5008d582010-09-28 02:34:02 +0200908 free(user);
909 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200910 free(mode);
911 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +0200912
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200913 if (i)
914 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200915
916 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200917}
918
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200919static int help(void) {
920
Lennart Poettering522d4a42011-02-13 15:08:15 +0100921 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
922 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200923 " -h --help Show this help\n"
924 " --create Create marked files/directories\n"
925 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100926 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +0100927 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200928 program_invocation_short_name);
929
930 return 0;
931}
932
933static int parse_argv(int argc, char *argv[]) {
934
935 enum {
936 ARG_CREATE,
937 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100938 ARG_REMOVE,
939 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200940 };
941
942 static const struct option options[] = {
943 { "help", no_argument, NULL, 'h' },
944 { "create", no_argument, NULL, ARG_CREATE },
945 { "clean", no_argument, NULL, ARG_CLEAN },
946 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100947 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200948 { NULL, 0, NULL, 0 }
949 };
950
951 int c;
952
953 assert(argc >= 0);
954 assert(argv);
955
956 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
957
958 switch (c) {
959
960 case 'h':
961 help();
962 return 0;
963
964 case ARG_CREATE:
965 arg_create = true;
966 break;
967
968 case ARG_CLEAN:
969 arg_clean = true;
970 break;
971
972 case ARG_REMOVE:
973 arg_remove = true;
974 break;
975
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100976 case ARG_PREFIX:
977 arg_prefix = optarg;
978 break;
979
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200980 case '?':
981 return -EINVAL;
982
983 default:
984 log_error("Unknown option code %c", c);
985 return -EINVAL;
986 }
987 }
988
989 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +0100990 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200991 return -EINVAL;
992 }
993
994 return 1;
995}
996
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100997static int read_config_file(const char *fn, bool ignore_enoent) {
998 FILE *f;
999 unsigned v = 0;
1000 int r = 0;
1001
1002 assert(fn);
1003
1004 if (!(f = fopen(fn, "re"))) {
1005
1006 if (ignore_enoent && errno == ENOENT)
1007 return 0;
1008
1009 log_error("Failed to open %s: %m", fn);
1010 return -errno;
1011 }
1012
Kay Sievers772f8372011-04-25 21:38:21 +02001013 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001014 for (;;) {
1015 char line[LINE_MAX], *l;
1016 int k;
1017
1018 if (!(fgets(line, sizeof(line), f)))
1019 break;
1020
1021 v++;
1022
1023 l = strstrip(line);
1024 if (*l == '#' || *l == 0)
1025 continue;
1026
1027 if ((k = parse_line(fn, v, l)) < 0)
1028 if (r == 0)
1029 r = k;
1030 }
1031
1032 if (ferror(f)) {
1033 log_error("Failed to read from file %s: %m", fn);
1034 if (r == 0)
1035 r = -EIO;
1036 }
1037
1038 fclose(f);
1039
1040 return r;
1041}
1042
Lennart Poettering5008d582010-09-28 02:34:02 +02001043int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001044 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001045 Item *i;
1046 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001047
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001048 if ((r = parse_argv(argc, argv)) <= 0)
1049 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1050
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001051 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001052 log_parse_environment();
1053 log_open();
1054
Lennart Poettering4c126262011-08-01 20:52:18 +02001055 umask(0022);
1056
Lennart Poettering5008d582010-09-28 02:34:02 +02001057 label_init();
1058
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001059 items = hashmap_new(string_hash_func, string_compare_func);
1060 globs = hashmap_new(string_hash_func, string_compare_func);
1061
1062 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001063 log_error("Out of memory");
1064 r = EXIT_FAILURE;
1065 goto finish;
1066 }
1067
Lennart Poettering5008d582010-09-28 02:34:02 +02001068 r = EXIT_SUCCESS;
1069
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001070 if (optind < argc) {
1071 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001072
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001073 for (j = optind; j < argc; j++)
1074 if (read_config_file(argv[j], false) < 0)
1075 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001076
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001077 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001078 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001079
Kay Sievers44143302011-04-28 23:51:24 +02001080 r = conf_files_list(&files, ".conf",
1081 "/run/tmpfiles.d",
1082 "/etc/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +02001083 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +02001084 "/usr/lib/tmpfiles.d",
1085 NULL);
1086 if (r < 0) {
1087 r = EXIT_FAILURE;
1088 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1089 goto finish;
1090 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001091
Kay Sievers772f8372011-04-25 21:38:21 +02001092 STRV_FOREACH(f, files) {
1093 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001094 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001095 }
1096
Kay Sievers772f8372011-04-25 21:38:21 +02001097 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001098 }
1099
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001100 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001101 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001102
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001103 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001104 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001105
Lennart Poettering5008d582010-09-28 02:34:02 +02001106finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001107 while ((i = hashmap_steal_first(items)))
1108 item_free(i);
1109
Lennart Poettering17b90522011-02-14 21:55:06 +01001110 while ((i = hashmap_steal_first(globs)))
1111 item_free(i);
1112
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001113 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001114 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001115
Lennart Poettering17b90522011-02-14 21:55:06 +01001116 set_free_free(unix_sockets);
1117
Lennart Poettering29003cf2010-10-19 19:36:45 +02001118 label_finish();
1119
Lennart Poettering5008d582010-09-28 02:34:02 +02001120 return r;
1121}