blob: d655bc359465576c970b5faa0579e42778557709 [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
Michal Schmidt99e68c02011-12-15 23:45:26 +0100408static int glob_item(Item *i, int (*action)(Item *, const char *)) {
409 int r = 0, k;
410 glob_t g;
411 char **fn;
412
413 zero(g);
414
415 errno = 0;
416 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
417
418 if (k != GLOB_NOMATCH) {
419 if (errno != 0)
420 errno = EIO;
421
422 log_error("glob(%s) failed: %m", i->path);
423 return -errno;
424 }
425 }
426
427 STRV_FOREACH(fn, g.gl_pathv)
428 if ((k = action(i, *fn)) < 0)
429 r = k;
430
431 globfree(&g);
432 return r;
433}
434
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100435static int item_set_perms(Item *i) {
436 if (i->mode_set)
437 if (chmod(i->path, i->mode) < 0) {
438 log_error("chmod(%s) failed: %m", i->path);
439 return -errno;
440 }
441
442 if (i->uid_set || i->gid_set)
443 if (chown(i->path,
444 i->uid_set ? i->uid : (uid_t) -1,
445 i->gid_set ? i->gid : (gid_t) -1) < 0) {
446
447 log_error("chown(%s) failed: %m", i->path);
448 return -errno;
449 }
450
451 return label_fix(i->path, false);
452}
453
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200454static int create_item(Item *i) {
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100455 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200456 mode_t u;
457 struct stat st;
458
459 assert(i);
460
461 switch (i->type) {
462
463 case IGNORE_PATH:
464 case REMOVE_PATH:
465 case RECURSIVE_REMOVE_PATH:
466 return 0;
467
468 case CREATE_FILE:
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100469 case TRUNCATE_FILE: {
470 int fd;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200471
472 u = umask(0);
473 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
474 (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
475 umask(u);
476
477 if (fd < 0) {
478 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100479 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200480 }
481
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100482 close_nointr_nofail(fd);
483
484 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200485 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100486 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200487 }
488
489 if (!S_ISREG(st.st_mode)) {
490 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100491 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200492 }
493
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100494 r = item_set_perms(i);
495 if (r < 0)
496 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200497
498 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100499 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200500
501 case TRUNCATE_DIRECTORY:
502 case CREATE_DIRECTORY:
503
504 u = umask(0);
Kay Sievers33366862011-04-03 22:21:21 +0200505 mkdir_parents(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200506 r = mkdir(i->path, i->mode);
507 umask(u);
508
509 if (r < 0 && errno != EEXIST) {
510 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100511 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200512 }
513
514 if (stat(i->path, &st) < 0) {
515 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100516 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200517 }
518
519 if (!S_ISDIR(st.st_mode)) {
520 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100521 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200522 }
523
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100524 r = item_set_perms(i);
525 if (r < 0)
526 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200527
528 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200529
530 case CREATE_FIFO:
531
532 u = umask(0);
533 r = mkfifo(i->path, i->mode);
534 umask(u);
535
536 if (r < 0 && errno != EEXIST) {
537 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100538 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200539 }
540
541 if (stat(i->path, &st) < 0) {
542 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100543 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200544 }
545
546 if (!S_ISFIFO(st.st_mode)) {
547 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100548 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200549 }
550
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100551 r = item_set_perms(i);
552 if (r < 0)
553 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200554
555 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200556 }
557
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200558 log_debug("%s created successfully.", i->path);
559
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100560 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200561}
562
Michal Schmidta0896122011-12-15 21:32:50 +0100563static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200564 int r;
565
566 assert(i);
567
568 switch (i->type) {
569
570 case CREATE_FILE:
571 case TRUNCATE_FILE:
572 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200573 case CREATE_FIFO:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200574 case IGNORE_PATH:
575 break;
576
577 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100578 if (remove(instance) < 0 && errno != ENOENT) {
579 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200580 return -errno;
581 }
582
583 break;
584
585 case TRUNCATE_DIRECTORY:
586 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringad293f52011-08-21 20:05:51 +0200587 if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false)) < 0 &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200588 r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100589 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200590 return r;
591 }
592
593 break;
594 }
595
596 return 0;
597}
598
Michal Schmidta0896122011-12-15 21:32:50 +0100599static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100600 int r = 0;
601
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100602 assert(i);
603
604 switch (i->type) {
605
606 case CREATE_FILE:
607 case TRUNCATE_FILE:
608 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200609 case CREATE_FIFO:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100610 case IGNORE_PATH:
611 break;
612
613 case REMOVE_PATH:
614 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100615 case RECURSIVE_REMOVE_PATH:
616 r = glob_item(i, remove_item_instance);
617 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100618 }
619
Michal Schmidt99e68c02011-12-15 23:45:26 +0100620 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100621}
622
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200623static int process_item(Item *i) {
624 int r, q, p;
625
626 assert(i);
627
628 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100629 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200630 p = arg_clean ? clean_item(i) : 0;
631
632 if (r < 0)
633 return r;
634
635 if (q < 0)
636 return q;
637
638 return p;
639}
640
641static void item_free(Item *i) {
642 assert(i);
643
644 free(i->path);
645 free(i);
646}
647
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200648static bool item_equal(Item *a, Item *b) {
649 assert(a);
650 assert(b);
651
652 if (!streq_ptr(a->path, b->path))
653 return false;
654
655 if (a->type != b->type)
656 return false;
657
658 if (a->uid_set != b->uid_set ||
659 (a->uid_set && a->uid != b->uid))
660 return false;
661
662 if (a->gid_set != b->gid_set ||
663 (a->gid_set && a->gid != b->gid))
664 return false;
665
666 if (a->mode_set != b->mode_set ||
667 (a->mode_set && a->mode != b->mode))
668 return false;
669
670 if (a->age_set != b->age_set ||
671 (a->age_set && a->age != b->age))
672 return false;
673
674 return true;
675}
676
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100677static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200678 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200679 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100680 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200681 Hashmap *h;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100682 int r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200683
684 assert(fname);
685 assert(line >= 1);
686 assert(buffer);
687
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200688 if (!(i = new0(Item, 1))) {
689 log_error("Out of memory");
690 return -ENOMEM;
691 }
692
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100693 if (sscanf(buffer,
694 "%c "
695 "%ms "
696 "%ms "
697 "%ms "
698 "%ms "
699 "%ms",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100700 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100701 &i->path,
702 &mode,
703 &user,
704 &group,
705 &age) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200706 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200707 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200708 goto finish;
709 }
Michal Schmidt66ccd032011-12-15 21:31:14 +0100710 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +0200711
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200712 if (i->type != CREATE_FILE &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200713 i->type != TRUNCATE_FILE &&
Gustavo Sverzut Barbieriabe35cc2010-10-19 23:06:34 -0200714 i->type != CREATE_DIRECTORY &&
715 i->type != TRUNCATE_DIRECTORY &&
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200716 i->type != CREATE_FIFO &&
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200717 i->type != IGNORE_PATH &&
718 i->type != REMOVE_PATH &&
719 i->type != RECURSIVE_REMOVE_PATH) {
720 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200721 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200722 goto finish;
723 }
724
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200725 if (!path_is_absolute(i->path)) {
726 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
727 r = -EBADMSG;
728 goto finish;
729 }
730
731 path_kill_slashes(i->path);
732
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100733 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200734 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200735 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200736 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200737
738 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200739 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +0200740
Lennart Poettering4b678342011-07-23 01:17:59 +0200741 r = get_user_creds(&u, &i->uid, NULL, NULL);
742 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200743 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
744 goto finish;
745 }
746
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200747 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200748 }
749
750 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +0200751 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +0200752
Lennart Poettering4b678342011-07-23 01:17:59 +0200753 r = get_group_creds(&g, &i->gid);
754 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200755 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
756 goto finish;
757 }
758
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200759 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200760 }
761
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200762 if (mode && !streq(mode, "-")) {
763 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +0200764
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200765 if (sscanf(mode, "%o", &m) != 1) {
766 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
767 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200768 goto finish;
769 }
770
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200771 i->mode = m;
772 i->mode_set = true;
773 } else
774 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
775
776 if (age && !streq(age, "-")) {
777 if (parse_usec(age, &i->age) < 0) {
778 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
779 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200780 goto finish;
781 }
782
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200783 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200784 }
785
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200786 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +0100787
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200788 if ((existing = hashmap_get(h, i->path))) {
789
790 /* Two identical items are fine */
791 if (!item_equal(existing, i))
792 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
793
794 r = 0;
795 goto finish;
796 }
797
798 if ((r = hashmap_put(h, i->path, i)) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200799 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200800 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200801 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200802
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200803 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200804 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200805
806finish:
Lennart Poettering5008d582010-09-28 02:34:02 +0200807 free(user);
808 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200809 free(mode);
810 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +0200811
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200812 if (i)
813 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200814
815 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200816}
817
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200818static int help(void) {
819
Lennart Poettering522d4a42011-02-13 15:08:15 +0100820 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
821 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200822 " -h --help Show this help\n"
823 " --create Create marked files/directories\n"
824 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100825 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +0100826 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200827 program_invocation_short_name);
828
829 return 0;
830}
831
832static int parse_argv(int argc, char *argv[]) {
833
834 enum {
835 ARG_CREATE,
836 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100837 ARG_REMOVE,
838 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200839 };
840
841 static const struct option options[] = {
842 { "help", no_argument, NULL, 'h' },
843 { "create", no_argument, NULL, ARG_CREATE },
844 { "clean", no_argument, NULL, ARG_CLEAN },
845 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100846 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200847 { NULL, 0, NULL, 0 }
848 };
849
850 int c;
851
852 assert(argc >= 0);
853 assert(argv);
854
855 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
856
857 switch (c) {
858
859 case 'h':
860 help();
861 return 0;
862
863 case ARG_CREATE:
864 arg_create = true;
865 break;
866
867 case ARG_CLEAN:
868 arg_clean = true;
869 break;
870
871 case ARG_REMOVE:
872 arg_remove = true;
873 break;
874
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100875 case ARG_PREFIX:
876 arg_prefix = optarg;
877 break;
878
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200879 case '?':
880 return -EINVAL;
881
882 default:
883 log_error("Unknown option code %c", c);
884 return -EINVAL;
885 }
886 }
887
888 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +0100889 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200890 return -EINVAL;
891 }
892
893 return 1;
894}
895
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100896static int read_config_file(const char *fn, bool ignore_enoent) {
897 FILE *f;
898 unsigned v = 0;
899 int r = 0;
900
901 assert(fn);
902
903 if (!(f = fopen(fn, "re"))) {
904
905 if (ignore_enoent && errno == ENOENT)
906 return 0;
907
908 log_error("Failed to open %s: %m", fn);
909 return -errno;
910 }
911
Kay Sievers772f8372011-04-25 21:38:21 +0200912 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100913 for (;;) {
914 char line[LINE_MAX], *l;
915 int k;
916
917 if (!(fgets(line, sizeof(line), f)))
918 break;
919
920 v++;
921
922 l = strstrip(line);
923 if (*l == '#' || *l == 0)
924 continue;
925
926 if ((k = parse_line(fn, v, l)) < 0)
927 if (r == 0)
928 r = k;
929 }
930
931 if (ferror(f)) {
932 log_error("Failed to read from file %s: %m", fn);
933 if (r == 0)
934 r = -EIO;
935 }
936
937 fclose(f);
938
939 return r;
940}
941
Lennart Poettering5008d582010-09-28 02:34:02 +0200942int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100943 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200944 Item *i;
945 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +0200946
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200947 if ((r = parse_argv(argc, argv)) <= 0)
948 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
949
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +0100950 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +0200951 log_parse_environment();
952 log_open();
953
Lennart Poettering4c126262011-08-01 20:52:18 +0200954 umask(0022);
955
Lennart Poettering5008d582010-09-28 02:34:02 +0200956 label_init();
957
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100958 items = hashmap_new(string_hash_func, string_compare_func);
959 globs = hashmap_new(string_hash_func, string_compare_func);
960
961 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200962 log_error("Out of memory");
963 r = EXIT_FAILURE;
964 goto finish;
965 }
966
Lennart Poettering5008d582010-09-28 02:34:02 +0200967 r = EXIT_SUCCESS;
968
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100969 if (optind < argc) {
970 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +0200971
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100972 for (j = optind; j < argc; j++)
973 if (read_config_file(argv[j], false) < 0)
974 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +0200975
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100976 } else {
Kay Sievers772f8372011-04-25 21:38:21 +0200977 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +0200978
Kay Sievers44143302011-04-28 23:51:24 +0200979 r = conf_files_list(&files, ".conf",
980 "/run/tmpfiles.d",
981 "/etc/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +0200982 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +0200983 "/usr/lib/tmpfiles.d",
984 NULL);
985 if (r < 0) {
986 r = EXIT_FAILURE;
987 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
988 goto finish;
989 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200990
Kay Sievers772f8372011-04-25 21:38:21 +0200991 STRV_FOREACH(f, files) {
992 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200993 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +0200994 }
995
Kay Sievers772f8372011-04-25 21:38:21 +0200996 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +0200997 }
998
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100999 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001000 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001001
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001002 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001003 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001004
Lennart Poettering5008d582010-09-28 02:34:02 +02001005finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001006 while ((i = hashmap_steal_first(items)))
1007 item_free(i);
1008
Lennart Poettering17b90522011-02-14 21:55:06 +01001009 while ((i = hashmap_steal_first(globs)))
1010 item_free(i);
1011
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001012 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001013 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001014
Lennart Poettering17b90522011-02-14 21:55:06 +01001015 set_free_free(unix_sockets);
1016
Lennart Poettering29003cf2010-10-19 19:36:45 +02001017 label_finish();
1018
Lennart Poettering5008d582010-09-28 02:34:02 +02001019 return r;
1020}