blob: 70de06b20b55adb47270381455f94812ae196f19 [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
Lennart Poettering5430f7f2012-04-12 00:20:58 +02009 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
Lennart Poettering5008d582010-09-28 02:34:02 +020011 (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
Lennart Poettering5430f7f2012-04-12 00:20:58 +020016 Lesser General Public License for more details.
Lennart Poettering5008d582010-09-28 02:34:02 +020017
Lennart Poettering5430f7f2012-04-12 00:20:58 +020018 You should have received a copy of the GNU Lesser General Public License
Lennart Poettering5008d582010-09-28 02:34:02 +020019 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 Poetteringcb7ed9d2012-09-05 23:39:55 -070041#include <sys/capability.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020042
43#include "log.h"
44#include "util.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020045#include "mkdir.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020046#include "path-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020047#include "strv.h"
48#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020049#include "set.h"
Kay Sievers2c210442012-05-07 18:55:45 +020050#include "conf-files.h"
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -070051#include "capability.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020052
Andreas Jaeger01000472010-09-29 10:08:24 +020053/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020054 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020055 * properly owned directories beneath /tmp, /var/tmp, /run, which are
56 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020057
Michal Schmidt66ccd032011-12-15 21:31:14 +010058typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010059 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020060 CREATE_FILE = 'f',
61 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010062 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020063 CREATE_DIRECTORY = 'd',
64 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020065 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010066 CREATE_SYMLINK = 'L',
67 CREATE_CHAR_DEVICE = 'c',
68 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010069
70 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020071 IGNORE_PATH = 'x',
72 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010073 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010074 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010075 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010076} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020077
78typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010079 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020080
81 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010082 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020083 uid_t uid;
84 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020085 mode_t mode;
86 usec_t age;
87
Lennart Poettering468d7262012-01-17 15:04:12 +010088 dev_t major_minor;
89
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020090 bool uid_set:1;
91 bool gid_set:1;
92 bool mode_set:1;
93 bool age_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +020094
95 bool keep_first_level:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020096} Item;
97
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010098static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010099static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200100
101static bool arg_create = false;
102static bool arg_clean = false;
103static bool arg_remove = false;
104
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100105static const char *arg_prefix = NULL;
106
Lennart Poettering24f3a372012-06-20 09:05:50 +0200107static const char * const conf_file_dirs[] = {
Dave Reisner91256702012-06-08 22:31:19 -0400108 "/etc/tmpfiles.d",
109 "/run/tmpfiles.d",
110 "/usr/local/lib/tmpfiles.d",
111 "/usr/lib/tmpfiles.d",
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200112#ifdef HAVE_SPLIT_USR
113 "/lib/tmpfiles.d",
114#endif
Dave Reisner91256702012-06-08 22:31:19 -0400115 NULL
116};
117
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200118#define MAX_DEPTH 256
119
Michal Schmidt66ccd032011-12-15 21:31:14 +0100120static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100121 return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RELABEL_PATH || t == RECURSIVE_RELABEL_PATH;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100122}
123
124static struct Item* find_glob(Hashmap *h, const char *match) {
125 Item *j;
126 Iterator i;
127
128 HASHMAP_FOREACH(j, h, i)
129 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
130 return j;
131
132 return NULL;
133}
134
Lennart Poettering17b90522011-02-14 21:55:06 +0100135static void load_unix_sockets(void) {
136 FILE *f = NULL;
137 char line[LINE_MAX];
138
139 if (unix_sockets)
140 return;
141
142 /* We maintain a cache of the sockets we found in
143 * /proc/net/unix to speed things up a little. */
144
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100145 unix_sockets = set_new(string_hash_func, string_compare_func);
146 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100147 return;
148
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100149 f = fopen("/proc/net/unix", "re");
150 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100151 return;
152
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100153 /* Skip header */
154 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100155 goto fail;
156
157 for (;;) {
158 char *p, *s;
159 int k;
160
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100161 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100162 break;
163
164 truncate_nl(line);
165
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100166 p = strchr(line, ':');
167 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100168 continue;
169
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 if (strlen(p) < 37)
171 continue;
172
173 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100174 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100175 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100176 p += strspn(p, WHITESPACE);
177
178 if (*p != '/')
179 continue;
180
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100181 s = strdup(p);
182 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100183 goto fail;
184
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100185 path_kill_slashes(s);
186
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100187 k = set_put(unix_sockets, s);
188 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100189 free(s);
190
191 if (k != -EEXIST)
192 goto fail;
193 }
194 }
195
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200196 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100197 return;
198
199fail:
200 set_free_free(unix_sockets);
201 unix_sockets = NULL;
202
203 if (f)
204 fclose(f);
205}
206
207static bool unix_socket_alive(const char *fn) {
208 assert(fn);
209
210 load_unix_sockets();
211
212 if (unix_sockets)
213 return !!set_get(unix_sockets, (char*) fn);
214
215 /* We don't know, so assume yes */
216 return true;
217}
218
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200219static int dir_cleanup(
220 const char *p,
221 DIR *d,
222 const struct stat *ds,
223 usec_t cutoff,
224 dev_t rootdev,
225 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200226 int maxdepth,
227 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200228{
229 struct dirent *dent;
230 struct timespec times[2];
231 bool deleted = false;
232 char *sub_path = NULL;
233 int r = 0;
234
235 while ((dent = readdir(d))) {
236 struct stat s;
237 usec_t age;
238
239 if (streq(dent->d_name, ".") ||
240 streq(dent->d_name, ".."))
241 continue;
242
243 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
244
245 if (errno != ENOENT) {
246 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
247 r = -errno;
248 }
249
250 continue;
251 }
252
253 /* Stay on the same filesystem */
254 if (s.st_dev != rootdev)
255 continue;
256
257 /* Do not delete read-only files owned by root */
258 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
259 continue;
260
261 free(sub_path);
262 sub_path = NULL;
263
264 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700265 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200266 goto finish;
267 }
268
269 /* Is there an item configured for this path? */
270 if (hashmap_get(items, sub_path))
271 continue;
272
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100273 if (find_glob(globs, sub_path))
274 continue;
275
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200276 if (S_ISDIR(s.st_mode)) {
277
278 if (mountpoint &&
279 streq(dent->d_name, "lost+found") &&
280 s.st_uid == 0)
281 continue;
282
283 if (maxdepth <= 0)
284 log_warning("Reached max depth on %s.", sub_path);
285 else {
286 DIR *sub_dir;
287 int q;
288
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200289 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200290 if (sub_dir == NULL) {
291 if (errno != ENOENT) {
292 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
293 r = -errno;
294 }
295
296 continue;
297 }
298
Lennart Poettering24f3a372012-06-20 09:05:50 +0200299 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200300 closedir(sub_dir);
301
302 if (q < 0)
303 r = q;
304 }
305
Lennart Poettering24f3a372012-06-20 09:05:50 +0200306 /* Note: if you are wondering why we don't
307 * support the sticky bit for excluding
308 * directories from cleaning like we do it for
309 * other file system objects: well, the sticky
310 * bit already has a meaning for directories,
311 * so we don't want to overload that. */
312
313 if (keep_this_level)
314 continue;
315
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200316 /* Ignore ctime, we change it when deleting */
317 age = MAX(timespec_load(&s.st_mtim),
318 timespec_load(&s.st_atim));
319 if (age >= cutoff)
320 continue;
321
322 log_debug("rmdir '%s'\n", sub_path);
323
324 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
325 if (errno != ENOENT && errno != ENOTEMPTY) {
326 log_error("rmdir(%s): %m", sub_path);
327 r = -errno;
328 }
329 }
330
331 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100332 /* Skip files for which the sticky bit is
333 * set. These are semantics we define, and are
334 * unknown elsewhere. See XDG_RUNTIME_DIR
335 * specification for details. */
336 if (s.st_mode & S_ISVTX)
337 continue;
338
Lennart Poettering17b90522011-02-14 21:55:06 +0100339 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200340 if (streq(dent->d_name, ".journal") &&
341 s.st_uid == 0)
342 continue;
343
344 if (streq(dent->d_name, "aquota.user") ||
345 streq(dent->d_name, "aquota.group"))
346 continue;
347 }
348
Lennart Poettering17b90522011-02-14 21:55:06 +0100349 /* Ignore sockets that are listed in /proc/net/unix */
350 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
351 continue;
352
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100353 /* Ignore device nodes */
354 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
355 continue;
356
Lennart Poettering24f3a372012-06-20 09:05:50 +0200357 /* Keep files on this level around if this is
358 * requested */
359 if (keep_this_level)
360 continue;
361
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200362 age = MAX3(timespec_load(&s.st_mtim),
363 timespec_load(&s.st_atim),
364 timespec_load(&s.st_ctim));
365
366 if (age >= cutoff)
367 continue;
368
369 log_debug("unlink '%s'\n", sub_path);
370
371 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
372 if (errno != ENOENT) {
373 log_error("unlink(%s): %m", sub_path);
374 r = -errno;
375 }
376 }
377
378 deleted = true;
379 }
380 }
381
382finish:
383 if (deleted) {
384 /* Restore original directory timestamps */
385 times[0] = ds->st_atim;
386 times[1] = ds->st_mtim;
387
388 if (futimens(dirfd(d), times) < 0)
389 log_error("utimensat(%s): %m", p);
390 }
391
392 free(sub_path);
393
394 return r;
395}
396
397static int clean_item(Item *i) {
398 DIR *d;
399 struct stat s, ps;
400 bool mountpoint;
401 int r;
402 usec_t cutoff, n;
403
404 assert(i);
405
406 if (i->type != CREATE_DIRECTORY &&
407 i->type != TRUNCATE_DIRECTORY &&
408 i->type != IGNORE_PATH)
409 return 0;
410
411 if (!i->age_set || i->age <= 0)
412 return 0;
413
414 n = now(CLOCK_REALTIME);
415 if (n < i->age)
416 return 0;
417
418 cutoff = n - i->age;
419
420 d = opendir(i->path);
421 if (!d) {
422 if (errno == ENOENT)
423 return 0;
424
425 log_error("Failed to open directory %s: %m", i->path);
426 return -errno;
427 }
428
429 if (fstat(dirfd(d), &s) < 0) {
430 log_error("stat(%s) failed: %m", i->path);
431 r = -errno;
432 goto finish;
433 }
434
435 if (!S_ISDIR(s.st_mode)) {
436 log_error("%s is not a directory.", i->path);
437 r = -ENOTDIR;
438 goto finish;
439 }
440
441 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
442 log_error("stat(%s/..) failed: %m", i->path);
443 r = -errno;
444 goto finish;
445 }
446
447 mountpoint = s.st_dev != ps.st_dev ||
448 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
449
Lennart Poettering24f3a372012-06-20 09:05:50 +0200450 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH, i->keep_first_level);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200451
452finish:
453 if (d)
454 closedir(d);
455
456 return r;
457}
458
Michal Schmidt062e01b2011-12-16 18:00:11 +0100459static int item_set_perms(Item *i, const char *path) {
460 /* not using i->path directly because it may be a glob */
461 if (i->mode_set)
462 if (chmod(path, i->mode) < 0) {
463 log_error("chmod(%s) failed: %m", path);
464 return -errno;
465 }
466
467 if (i->uid_set || i->gid_set)
468 if (chown(path,
469 i->uid_set ? i->uid : (uid_t) -1,
470 i->gid_set ? i->gid : (gid_t) -1) < 0) {
471
472 log_error("chown(%s) failed: %m", path);
473 return -errno;
474 }
475
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200476 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100477}
478
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400479static int write_one_file(Item *i, const char *path) {
480 int r, e, fd, flags;
481 struct stat st;
482 mode_t u;
483
484 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
485 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
486
487 u = umask(0);
488 label_context_set(path, S_IFREG);
489 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
490 e = errno;
491 label_context_clear();
492 umask(u);
493 errno = e;
494
495 if (fd < 0) {
496 if (i->type == WRITE_FILE && errno == ENOENT)
497 return 0;
498
499 log_error("Failed to create file %s: %m", path);
500 return -errno;
501 }
502
503 if (i->argument) {
504 ssize_t n;
505 size_t l;
506 struct iovec iovec[2];
507 static const char new_line = '\n';
508
509 l = strlen(i->argument);
510
511 zero(iovec);
512 iovec[0].iov_base = i->argument;
513 iovec[0].iov_len = l;
514
515 iovec[1].iov_base = (void*) &new_line;
516 iovec[1].iov_len = 1;
517
518 n = writev(fd, iovec, 2);
519
520 /* It's OK if we don't write the trailing
521 * newline, hence we check for l, instead of
522 * l+1 here. Files in /sys often refuse
523 * writing of the trailing newline. */
524 if (n < 0 || (size_t) n < l) {
525 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
526 close_nointr_nofail(fd);
527 return n < 0 ? n : -EIO;
528 }
529 }
530
531 if (stat(path, &st) < 0) {
532 log_error("stat(%s) failed: %m", path);
533 return -errno;
534 }
535
536 if (!S_ISREG(st.st_mode)) {
537 log_error("%s is not a file.", path);
538 return -EEXIST;
539 }
540
541 r = item_set_perms(i, path);
542 if (r < 0)
543 return r;
544
545 return 0;
546}
547
Michal Schmidt062e01b2011-12-16 18:00:11 +0100548static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100549 DIR *d;
550 int ret = 0;
551
552 /* This returns the first error we run into, but nevertheless
553 * tries to go on */
554
555 d = opendir(path);
556 if (!d)
557 return errno == ENOENT ? 0 : -errno;
558
559 for (;;) {
560 struct dirent buf, *de;
561 bool is_dir;
562 int r;
563 char *entry_path;
564
565 r = readdir_r(d, &buf, &de);
566 if (r != 0) {
567 if (ret == 0)
568 ret = -r;
569 break;
570 }
571
572 if (!de)
573 break;
574
575 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
576 continue;
577
578 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
579 if (ret == 0)
580 ret = -ENOMEM;
581 continue;
582 }
583
584 if (de->d_type == DT_UNKNOWN) {
585 struct stat st;
586
587 if (lstat(entry_path, &st) < 0) {
588 if (ret == 0 && errno != ENOENT)
589 ret = -errno;
590 free(entry_path);
591 continue;
592 }
593
594 is_dir = S_ISDIR(st.st_mode);
595
596 } else
597 is_dir = de->d_type == DT_DIR;
598
Michal Schmidt062e01b2011-12-16 18:00:11 +0100599 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100600 if (r < 0) {
601 if (ret == 0 && r != -ENOENT)
602 ret = r;
603 free(entry_path);
604 continue;
605 }
606
607 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100608 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100609 if (r < 0 && ret == 0)
610 ret = r;
611 }
612
613 free(entry_path);
614 }
615
616 closedir(d);
617
618 return ret;
619}
620
621static int recursive_relabel(Item *i, const char *path) {
622 int r;
623 struct stat st;
624
Michal Schmidt062e01b2011-12-16 18:00:11 +0100625 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100626 if (r < 0)
627 return r;
628
629 if (lstat(path, &st) < 0)
630 return -errno;
631
632 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100633 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100634
635 return r;
636}
637
Michal Schmidt99e68c02011-12-15 23:45:26 +0100638static int glob_item(Item *i, int (*action)(Item *, const char *)) {
639 int r = 0, k;
640 glob_t g;
641 char **fn;
642
643 zero(g);
644
645 errno = 0;
646 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
647
648 if (k != GLOB_NOMATCH) {
649 if (errno != 0)
650 errno = EIO;
651
652 log_error("glob(%s) failed: %m", i->path);
653 return -errno;
654 }
655 }
656
657 STRV_FOREACH(fn, g.gl_pathv)
658 if ((k = action(i, *fn)) < 0)
659 r = k;
660
661 globfree(&g);
662 return r;
663}
664
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200665static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200666 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200667 mode_t u;
668 struct stat st;
669
670 assert(i);
671
672 switch (i->type) {
673
674 case IGNORE_PATH:
675 case REMOVE_PATH:
676 case RECURSIVE_REMOVE_PATH:
677 return 0;
678
679 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100680 case TRUNCATE_FILE:
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400681 case WRITE_FILE:
682 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100683 if (r < 0)
684 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200685
686 break;
687
688 case TRUNCATE_DIRECTORY:
689 case CREATE_DIRECTORY:
690
691 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200692 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200693 r = mkdir(i->path, i->mode);
694 umask(u);
695
696 if (r < 0 && errno != EEXIST) {
697 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100698 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200699 }
700
701 if (stat(i->path, &st) < 0) {
702 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100703 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200704 }
705
706 if (!S_ISDIR(st.st_mode)) {
707 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100708 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200709 }
710
Michal Schmidt062e01b2011-12-16 18:00:11 +0100711 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100712 if (r < 0)
713 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200714
715 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200716
717 case CREATE_FIFO:
718
719 u = umask(0);
720 r = mkfifo(i->path, i->mode);
721 umask(u);
722
723 if (r < 0 && errno != EEXIST) {
724 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100725 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200726 }
727
728 if (stat(i->path, &st) < 0) {
729 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100730 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200731 }
732
733 if (!S_ISFIFO(st.st_mode)) {
734 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100735 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200736 }
737
Michal Schmidt062e01b2011-12-16 18:00:11 +0100738 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100739 if (r < 0)
740 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200741
742 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100743
Lennart Poettering468d7262012-01-17 15:04:12 +0100744 case CREATE_SYMLINK: {
745 char *x;
746
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200747 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100748 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200749 e = errno;
750 label_context_clear();
751 errno = e;
752
Lennart Poettering468d7262012-01-17 15:04:12 +0100753 if (r < 0 && errno != EEXIST) {
754 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
755 return -errno;
756 }
757
758 r = readlink_malloc(i->path, &x);
759 if (r < 0) {
760 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
761 return -errno;
762 }
763
764 if (!streq(i->argument, x)) {
765 free(x);
766 log_error("%s is not the right symlinks.", i->path);
767 return -EEXIST;
768 }
769
770 free(x);
771 break;
772 }
773
774 case CREATE_BLOCK_DEVICE:
775 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700776 mode_t file_type;
777
778 if (have_effective_cap(CAP_MKNOD) == 0) {
779 /* In a container we lack CAP_MKNOD. We
780 shouldnt attempt to create the device node in
781 that case to avoid noise, and we don't support
782 virtualized devices in containers anyway. */
783
784 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
785 return 0;
786 }
787
788 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100789
790 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200791 label_context_set(i->path, file_type);
792 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200793 e = errno;
794 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100795 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200796 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100797
798 if (r < 0 && errno != EEXIST) {
799 log_error("Failed to create device node %s: %m", i->path);
800 return -errno;
801 }
802
803 if (stat(i->path, &st) < 0) {
804 log_error("stat(%s) failed: %m", i->path);
805 return -errno;
806 }
807
Michal Schmidte7aee752012-06-14 16:01:19 +0200808 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100809 log_error("%s is not a device node.", i->path);
810 return -EEXIST;
811 }
812
813 r = item_set_perms(i, i->path);
814 if (r < 0)
815 return r;
816
817 break;
818 }
819
Michal Schmidt777b87e2011-12-16 18:27:35 +0100820 case RELABEL_PATH:
821
822 r = glob_item(i, item_set_perms);
823 if (r < 0)
824 return 0;
825 break;
826
Michal Schmidta8d88782011-12-15 23:11:07 +0100827 case RECURSIVE_RELABEL_PATH:
828
829 r = glob_item(i, recursive_relabel);
830 if (r < 0)
831 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200832 }
833
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200834 log_debug("%s created successfully.", i->path);
835
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100836 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200837}
838
Michal Schmidta0896122011-12-15 21:32:50 +0100839static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200840 int r;
841
842 assert(i);
843
844 switch (i->type) {
845
846 case CREATE_FILE:
847 case TRUNCATE_FILE:
848 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200849 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100850 case CREATE_SYMLINK:
851 case CREATE_BLOCK_DEVICE:
852 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200853 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100854 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100855 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100856 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200857 break;
858
859 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100860 if (remove(instance) < 0 && errno != ENOENT) {
861 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200862 return -errno;
863 }
864
865 break;
866
867 case TRUNCATE_DIRECTORY:
868 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200869 /* FIXME: we probably should use dir_cleanup() here
870 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200871 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100872 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100873 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200874 return r;
875 }
876
877 break;
878 }
879
880 return 0;
881}
882
Michal Schmidta0896122011-12-15 21:32:50 +0100883static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100884 int r = 0;
885
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100886 assert(i);
887
888 switch (i->type) {
889
890 case CREATE_FILE:
891 case TRUNCATE_FILE:
892 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200893 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100894 case CREATE_SYMLINK:
895 case CREATE_CHAR_DEVICE:
896 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100897 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100898 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100899 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100900 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100901 break;
902
903 case REMOVE_PATH:
904 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100905 case RECURSIVE_REMOVE_PATH:
906 r = glob_item(i, remove_item_instance);
907 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100908 }
909
Michal Schmidt99e68c02011-12-15 23:45:26 +0100910 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100911}
912
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200913static int process_item(Item *i) {
914 int r, q, p;
915
916 assert(i);
917
918 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100919 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200920 p = arg_clean ? clean_item(i) : 0;
921
922 if (r < 0)
923 return r;
924
925 if (q < 0)
926 return q;
927
928 return p;
929}
930
931static void item_free(Item *i) {
932 assert(i);
933
934 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100935 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200936 free(i);
937}
938
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200939static bool item_equal(Item *a, Item *b) {
940 assert(a);
941 assert(b);
942
943 if (!streq_ptr(a->path, b->path))
944 return false;
945
946 if (a->type != b->type)
947 return false;
948
949 if (a->uid_set != b->uid_set ||
950 (a->uid_set && a->uid != b->uid))
951 return false;
952
953 if (a->gid_set != b->gid_set ||
954 (a->gid_set && a->gid != b->gid))
955 return false;
956
957 if (a->mode_set != b->mode_set ||
958 (a->mode_set && a->mode != b->mode))
959 return false;
960
961 if (a->age_set != b->age_set ||
962 (a->age_set && a->age != b->age))
963 return false;
964
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100965 if ((a->type == CREATE_FILE ||
966 a->type == TRUNCATE_FILE ||
967 a->type == WRITE_FILE ||
968 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100969 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100970 return false;
971
972 if ((a->type == CREATE_CHAR_DEVICE ||
973 a->type == CREATE_BLOCK_DEVICE) &&
974 a->major_minor != b->major_minor)
975 return false;
976
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200977 return true;
978}
979
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100980static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200981 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200982 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100983 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200984 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100985 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200986
987 assert(fname);
988 assert(line >= 1);
989 assert(buffer);
990
Lennart Poettering468d7262012-01-17 15:04:12 +0100991 i = new0(Item, 1);
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700992 if (!i)
993 return log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200994
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100995 if (sscanf(buffer,
996 "%c "
997 "%ms "
998 "%ms "
999 "%ms "
1000 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +01001001 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001002 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +01001003 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001004 &i->path,
1005 &mode,
1006 &user,
1007 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001008 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001009 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001010 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001011 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001012 goto finish;
1013 }
1014
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001015 if (n >= 0) {
1016 n += strspn(buffer+n, WHITESPACE);
1017 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1018 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001019 if (!i->argument)
1020 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001021 }
1022 }
1023
Michal Schmidt777b87e2011-12-16 18:27:35 +01001024 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001025
Michal Schmidt777b87e2011-12-16 18:27:35 +01001026 case CREATE_FILE:
1027 case TRUNCATE_FILE:
1028 case CREATE_DIRECTORY:
1029 case TRUNCATE_DIRECTORY:
1030 case CREATE_FIFO:
1031 case IGNORE_PATH:
1032 case REMOVE_PATH:
1033 case RECURSIVE_REMOVE_PATH:
1034 case RELABEL_PATH:
1035 case RECURSIVE_RELABEL_PATH:
1036 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001037
1038 case CREATE_SYMLINK:
1039 if (!i->argument) {
1040 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1041 r = -EBADMSG;
1042 goto finish;
1043 }
1044 break;
1045
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001046 case WRITE_FILE:
1047 if (!i->argument) {
1048 log_error("[%s:%u] Write file requires argument.", fname, line);
1049 r = -EBADMSG;
1050 goto finish;
1051 }
1052 break;
1053
Lennart Poettering468d7262012-01-17 15:04:12 +01001054 case CREATE_CHAR_DEVICE:
1055 case CREATE_BLOCK_DEVICE: {
1056 unsigned major, minor;
1057
1058 if (!i->argument) {
1059 log_error("[%s:%u] Device file requires argument.", fname, line);
1060 r = -EBADMSG;
1061 goto finish;
1062 }
1063
1064 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1065 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1066 r = -EBADMSG;
1067 goto finish;
1068 }
1069
1070 i->major_minor = makedev(major, minor);
1071 break;
1072 }
1073
Michal Schmidt777b87e2011-12-16 18:27:35 +01001074 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001075 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001076 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001077 goto finish;
1078 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001079
Michal Schmidta8d88782011-12-15 23:11:07 +01001080 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001081
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001082 if (!path_is_absolute(i->path)) {
1083 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1084 r = -EBADMSG;
1085 goto finish;
1086 }
1087
1088 path_kill_slashes(i->path);
1089
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001090 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001091 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001092 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001093 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001094
1095 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001096 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001097
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001098 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001099 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001100 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1101 goto finish;
1102 }
1103
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001104 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001105 }
1106
1107 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001108 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001109
Lennart Poettering4b678342011-07-23 01:17:59 +02001110 r = get_group_creds(&g, &i->gid);
1111 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001112 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1113 goto finish;
1114 }
1115
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001116 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001117 }
1118
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001119 if (mode && !streq(mode, "-")) {
1120 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001121
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001122 if (sscanf(mode, "%o", &m) != 1) {
1123 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1124 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001125 goto finish;
1126 }
1127
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001128 i->mode = m;
1129 i->mode_set = true;
1130 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001131 i->mode =
1132 i->type == CREATE_DIRECTORY ||
1133 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001134
1135 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001136 const char *a = age;
1137
1138 if (*a == '~') {
1139 i->keep_first_level = true;
1140 a++;
1141 }
1142
1143 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001144 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1145 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001146 goto finish;
1147 }
1148
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001149 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001150 }
1151
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001152 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001153
Lennart Poettering468d7262012-01-17 15:04:12 +01001154 existing = hashmap_get(h, i->path);
1155 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001156
1157 /* Two identical items are fine */
1158 if (!item_equal(existing, i))
1159 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1160
1161 r = 0;
1162 goto finish;
1163 }
1164
Lennart Poettering468d7262012-01-17 15:04:12 +01001165 r = hashmap_put(h, i->path, i);
1166 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001167 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001168 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001169 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001170
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001171 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001172 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001173
1174finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001175 free(user);
1176 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001177 free(mode);
1178 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001179
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001180 if (i)
1181 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001182
1183 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001184}
1185
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001186static int help(void) {
1187
Lennart Poettering522d4a42011-02-13 15:08:15 +01001188 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1189 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001190 " -h --help Show this help\n"
1191 " --create Create marked files/directories\n"
1192 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001193 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001194 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001195 program_invocation_short_name);
1196
1197 return 0;
1198}
1199
1200static int parse_argv(int argc, char *argv[]) {
1201
1202 enum {
1203 ARG_CREATE,
1204 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001205 ARG_REMOVE,
1206 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001207 };
1208
1209 static const struct option options[] = {
1210 { "help", no_argument, NULL, 'h' },
1211 { "create", no_argument, NULL, ARG_CREATE },
1212 { "clean", no_argument, NULL, ARG_CLEAN },
1213 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001214 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001215 { NULL, 0, NULL, 0 }
1216 };
1217
1218 int c;
1219
1220 assert(argc >= 0);
1221 assert(argv);
1222
1223 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1224
1225 switch (c) {
1226
1227 case 'h':
1228 help();
1229 return 0;
1230
1231 case ARG_CREATE:
1232 arg_create = true;
1233 break;
1234
1235 case ARG_CLEAN:
1236 arg_clean = true;
1237 break;
1238
1239 case ARG_REMOVE:
1240 arg_remove = true;
1241 break;
1242
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001243 case ARG_PREFIX:
1244 arg_prefix = optarg;
1245 break;
1246
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001247 case '?':
1248 return -EINVAL;
1249
1250 default:
1251 log_error("Unknown option code %c", c);
1252 return -EINVAL;
1253 }
1254 }
1255
1256 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001257 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001258 return -EINVAL;
1259 }
1260
1261 return 1;
1262}
1263
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001264static int read_config_file(const char *fn, bool ignore_enoent) {
1265 FILE *f;
1266 unsigned v = 0;
1267 int r = 0;
1268
1269 assert(fn);
1270
Lennart Poettering468d7262012-01-17 15:04:12 +01001271 f = fopen(fn, "re");
1272 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001273
1274 if (ignore_enoent && errno == ENOENT)
1275 return 0;
1276
1277 log_error("Failed to open %s: %m", fn);
1278 return -errno;
1279 }
1280
Kay Sievers772f8372011-04-25 21:38:21 +02001281 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001282 for (;;) {
1283 char line[LINE_MAX], *l;
1284 int k;
1285
1286 if (!(fgets(line, sizeof(line), f)))
1287 break;
1288
1289 v++;
1290
1291 l = strstrip(line);
1292 if (*l == '#' || *l == 0)
1293 continue;
1294
1295 if ((k = parse_line(fn, v, l)) < 0)
1296 if (r == 0)
1297 r = k;
1298 }
1299
1300 if (ferror(f)) {
1301 log_error("Failed to read from file %s: %m", fn);
1302 if (r == 0)
1303 r = -EIO;
1304 }
1305
1306 fclose(f);
1307
1308 return r;
1309}
1310
Dave Reisner91256702012-06-08 22:31:19 -04001311static char *resolve_fragment(const char *fragment, const char **search_paths) {
1312 const char **p;
1313 char *resolved_path;
1314
1315 if (is_path(fragment))
1316 return strdup(fragment);
1317
1318 STRV_FOREACH(p, search_paths) {
Lennart Poetteringb7def682012-07-13 13:41:01 +02001319 resolved_path = strjoin(*p, "/", fragment, NULL);
Dave Reisner91256702012-06-08 22:31:19 -04001320 if (resolved_path == NULL) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001321 log_oom();
Dave Reisner91256702012-06-08 22:31:19 -04001322 return NULL;
1323 }
1324
1325 if (access(resolved_path, F_OK) == 0)
1326 return resolved_path;
1327
1328 free(resolved_path);
1329 }
1330
Kay Sieversca2e8942012-06-10 19:21:50 +02001331 errno = ENOENT;
Dave Reisner91256702012-06-08 22:31:19 -04001332 return NULL;
1333}
1334
Lennart Poettering5008d582010-09-28 02:34:02 +02001335int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001336 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001337 Item *i;
1338 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001339
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001340 r = parse_argv(argc, argv);
1341 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001342 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1343
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001344 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001345 log_parse_environment();
1346 log_open();
1347
Lennart Poettering4c126262011-08-01 20:52:18 +02001348 umask(0022);
1349
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001350 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001351
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001352 items = hashmap_new(string_hash_func, string_compare_func);
1353 globs = hashmap_new(string_hash_func, string_compare_func);
1354
1355 if (!items || !globs) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001356 log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001357 r = EXIT_FAILURE;
1358 goto finish;
1359 }
1360
Lennart Poettering5008d582010-09-28 02:34:02 +02001361 r = EXIT_SUCCESS;
1362
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001363 if (optind < argc) {
1364 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001365
Dave Reisner91256702012-06-08 22:31:19 -04001366 for (j = optind; j < argc; j++) {
Kay Sieversca2e8942012-06-10 19:21:50 +02001367 char *fragment;
1368
Lennart Poettering24f3a372012-06-20 09:05:50 +02001369 fragment = resolve_fragment(argv[j], (const char**) conf_file_dirs);
Kay Sieversca2e8942012-06-10 19:21:50 +02001370 if (!fragment) {
Kay Sievers94f7a712012-06-10 19:31:39 +02001371 log_error("Failed to find a %s file: %m", argv[j]);
Kay Sieversca2e8942012-06-10 19:21:50 +02001372 r = EXIT_FAILURE;
1373 goto finish;
1374 }
Dave Reisner91256702012-06-08 22:31:19 -04001375 if (read_config_file(fragment, false) < 0)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001376 r = EXIT_FAILURE;
Dave Reisner91256702012-06-08 22:31:19 -04001377 free(fragment);
1378 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001379
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001380 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001381 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001382
Dave Reisner91256702012-06-08 22:31:19 -04001383 r = conf_files_list_strv(&files, ".conf",
Lennart Poettering24f3a372012-06-20 09:05:50 +02001384 (const char **) conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001385 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001386 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001387 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001388 goto finish;
1389 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001390
Kay Sievers772f8372011-04-25 21:38:21 +02001391 STRV_FOREACH(f, files) {
1392 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001393 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001394 }
1395
Kay Sievers772f8372011-04-25 21:38:21 +02001396 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001397 }
1398
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001399 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001400 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001401
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001402 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001403 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001404
Lennart Poettering5008d582010-09-28 02:34:02 +02001405finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001406 while ((i = hashmap_steal_first(items)))
1407 item_free(i);
1408
Lennart Poettering17b90522011-02-14 21:55:06 +01001409 while ((i = hashmap_steal_first(globs)))
1410 item_free(i);
1411
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001412 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001413 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001414
Lennart Poettering17b90522011-02-14 21:55:06 +01001415 set_free_free(unix_sockets);
1416
Lennart Poettering29003cf2010-10-19 19:36:45 +02001417 label_finish();
1418
Lennart Poettering5008d582010-09-28 02:34:02 +02001419 return r;
1420}