blob: ed51ec8a7f8dd8fc719045625b701f12a212e6d1 [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
Dave Reisner3612fbc2012-09-12 16:21:00 -0400531 close_nointr_nofail(fd);
532
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400533 if (stat(path, &st) < 0) {
534 log_error("stat(%s) failed: %m", path);
535 return -errno;
536 }
537
538 if (!S_ISREG(st.st_mode)) {
539 log_error("%s is not a file.", path);
540 return -EEXIST;
541 }
542
543 r = item_set_perms(i, path);
544 if (r < 0)
545 return r;
546
547 return 0;
548}
549
Michal Schmidt062e01b2011-12-16 18:00:11 +0100550static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100551 DIR *d;
552 int ret = 0;
553
554 /* This returns the first error we run into, but nevertheless
555 * tries to go on */
556
557 d = opendir(path);
558 if (!d)
559 return errno == ENOENT ? 0 : -errno;
560
561 for (;;) {
562 struct dirent buf, *de;
563 bool is_dir;
564 int r;
565 char *entry_path;
566
567 r = readdir_r(d, &buf, &de);
568 if (r != 0) {
569 if (ret == 0)
570 ret = -r;
571 break;
572 }
573
574 if (!de)
575 break;
576
577 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
578 continue;
579
580 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
581 if (ret == 0)
582 ret = -ENOMEM;
583 continue;
584 }
585
586 if (de->d_type == DT_UNKNOWN) {
587 struct stat st;
588
589 if (lstat(entry_path, &st) < 0) {
590 if (ret == 0 && errno != ENOENT)
591 ret = -errno;
592 free(entry_path);
593 continue;
594 }
595
596 is_dir = S_ISDIR(st.st_mode);
597
598 } else
599 is_dir = de->d_type == DT_DIR;
600
Michal Schmidt062e01b2011-12-16 18:00:11 +0100601 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100602 if (r < 0) {
603 if (ret == 0 && r != -ENOENT)
604 ret = r;
605 free(entry_path);
606 continue;
607 }
608
609 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100610 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100611 if (r < 0 && ret == 0)
612 ret = r;
613 }
614
615 free(entry_path);
616 }
617
618 closedir(d);
619
620 return ret;
621}
622
623static int recursive_relabel(Item *i, const char *path) {
624 int r;
625 struct stat st;
626
Michal Schmidt062e01b2011-12-16 18:00:11 +0100627 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100628 if (r < 0)
629 return r;
630
631 if (lstat(path, &st) < 0)
632 return -errno;
633
634 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100635 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100636
637 return r;
638}
639
Michal Schmidt99e68c02011-12-15 23:45:26 +0100640static int glob_item(Item *i, int (*action)(Item *, const char *)) {
641 int r = 0, k;
642 glob_t g;
643 char **fn;
644
645 zero(g);
646
647 errno = 0;
648 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
649
650 if (k != GLOB_NOMATCH) {
651 if (errno != 0)
652 errno = EIO;
653
654 log_error("glob(%s) failed: %m", i->path);
655 return -errno;
656 }
657 }
658
659 STRV_FOREACH(fn, g.gl_pathv)
660 if ((k = action(i, *fn)) < 0)
661 r = k;
662
663 globfree(&g);
664 return r;
665}
666
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200667static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200668 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200669 mode_t u;
670 struct stat st;
671
672 assert(i);
673
674 switch (i->type) {
675
676 case IGNORE_PATH:
677 case REMOVE_PATH:
678 case RECURSIVE_REMOVE_PATH:
679 return 0;
680
681 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100682 case TRUNCATE_FILE:
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400683 case WRITE_FILE:
684 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100685 if (r < 0)
686 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200687
688 break;
689
690 case TRUNCATE_DIRECTORY:
691 case CREATE_DIRECTORY:
692
693 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200694 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200695 r = mkdir(i->path, i->mode);
696 umask(u);
697
698 if (r < 0 && errno != EEXIST) {
699 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100700 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200701 }
702
703 if (stat(i->path, &st) < 0) {
704 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100705 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200706 }
707
708 if (!S_ISDIR(st.st_mode)) {
709 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100710 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200711 }
712
Michal Schmidt062e01b2011-12-16 18:00:11 +0100713 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100714 if (r < 0)
715 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200716
717 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200718
719 case CREATE_FIFO:
720
721 u = umask(0);
722 r = mkfifo(i->path, i->mode);
723 umask(u);
724
725 if (r < 0 && errno != EEXIST) {
726 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100727 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200728 }
729
730 if (stat(i->path, &st) < 0) {
731 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100732 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200733 }
734
735 if (!S_ISFIFO(st.st_mode)) {
736 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100737 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200738 }
739
Michal Schmidt062e01b2011-12-16 18:00:11 +0100740 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100741 if (r < 0)
742 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200743
744 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100745
Lennart Poettering468d7262012-01-17 15:04:12 +0100746 case CREATE_SYMLINK: {
747 char *x;
748
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200749 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100750 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200751 e = errno;
752 label_context_clear();
753 errno = e;
754
Lennart Poettering468d7262012-01-17 15:04:12 +0100755 if (r < 0 && errno != EEXIST) {
756 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
757 return -errno;
758 }
759
760 r = readlink_malloc(i->path, &x);
761 if (r < 0) {
762 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
763 return -errno;
764 }
765
766 if (!streq(i->argument, x)) {
767 free(x);
768 log_error("%s is not the right symlinks.", i->path);
769 return -EEXIST;
770 }
771
772 free(x);
773 break;
774 }
775
776 case CREATE_BLOCK_DEVICE:
777 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700778 mode_t file_type;
779
780 if (have_effective_cap(CAP_MKNOD) == 0) {
781 /* In a container we lack CAP_MKNOD. We
782 shouldnt attempt to create the device node in
783 that case to avoid noise, and we don't support
784 virtualized devices in containers anyway. */
785
786 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
787 return 0;
788 }
789
790 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100791
792 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200793 label_context_set(i->path, file_type);
794 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200795 e = errno;
796 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100797 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200798 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100799
800 if (r < 0 && errno != EEXIST) {
801 log_error("Failed to create device node %s: %m", i->path);
802 return -errno;
803 }
804
805 if (stat(i->path, &st) < 0) {
806 log_error("stat(%s) failed: %m", i->path);
807 return -errno;
808 }
809
Michal Schmidte7aee752012-06-14 16:01:19 +0200810 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100811 log_error("%s is not a device node.", i->path);
812 return -EEXIST;
813 }
814
815 r = item_set_perms(i, i->path);
816 if (r < 0)
817 return r;
818
819 break;
820 }
821
Michal Schmidt777b87e2011-12-16 18:27:35 +0100822 case RELABEL_PATH:
823
824 r = glob_item(i, item_set_perms);
825 if (r < 0)
826 return 0;
827 break;
828
Michal Schmidta8d88782011-12-15 23:11:07 +0100829 case RECURSIVE_RELABEL_PATH:
830
831 r = glob_item(i, recursive_relabel);
832 if (r < 0)
833 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200834 }
835
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200836 log_debug("%s created successfully.", i->path);
837
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100838 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200839}
840
Michal Schmidta0896122011-12-15 21:32:50 +0100841static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200842 int r;
843
844 assert(i);
845
846 switch (i->type) {
847
848 case CREATE_FILE:
849 case TRUNCATE_FILE:
850 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200851 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100852 case CREATE_SYMLINK:
853 case CREATE_BLOCK_DEVICE:
854 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200855 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100856 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100857 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100858 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200859 break;
860
861 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100862 if (remove(instance) < 0 && errno != ENOENT) {
863 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200864 return -errno;
865 }
866
867 break;
868
869 case TRUNCATE_DIRECTORY:
870 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200871 /* FIXME: we probably should use dir_cleanup() here
872 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200873 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100874 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100875 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200876 return r;
877 }
878
879 break;
880 }
881
882 return 0;
883}
884
Michal Schmidta0896122011-12-15 21:32:50 +0100885static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100886 int r = 0;
887
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100888 assert(i);
889
890 switch (i->type) {
891
892 case CREATE_FILE:
893 case TRUNCATE_FILE:
894 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200895 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100896 case CREATE_SYMLINK:
897 case CREATE_CHAR_DEVICE:
898 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100899 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100900 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100901 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100902 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100903 break;
904
905 case REMOVE_PATH:
906 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100907 case RECURSIVE_REMOVE_PATH:
908 r = glob_item(i, remove_item_instance);
909 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100910 }
911
Michal Schmidt99e68c02011-12-15 23:45:26 +0100912 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100913}
914
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200915static int process_item(Item *i) {
916 int r, q, p;
917
918 assert(i);
919
920 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100921 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200922 p = arg_clean ? clean_item(i) : 0;
923
924 if (r < 0)
925 return r;
926
927 if (q < 0)
928 return q;
929
930 return p;
931}
932
933static void item_free(Item *i) {
934 assert(i);
935
936 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100937 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200938 free(i);
939}
940
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200941static bool item_equal(Item *a, Item *b) {
942 assert(a);
943 assert(b);
944
945 if (!streq_ptr(a->path, b->path))
946 return false;
947
948 if (a->type != b->type)
949 return false;
950
951 if (a->uid_set != b->uid_set ||
952 (a->uid_set && a->uid != b->uid))
953 return false;
954
955 if (a->gid_set != b->gid_set ||
956 (a->gid_set && a->gid != b->gid))
957 return false;
958
959 if (a->mode_set != b->mode_set ||
960 (a->mode_set && a->mode != b->mode))
961 return false;
962
963 if (a->age_set != b->age_set ||
964 (a->age_set && a->age != b->age))
965 return false;
966
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100967 if ((a->type == CREATE_FILE ||
968 a->type == TRUNCATE_FILE ||
969 a->type == WRITE_FILE ||
970 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100971 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100972 return false;
973
974 if ((a->type == CREATE_CHAR_DEVICE ||
975 a->type == CREATE_BLOCK_DEVICE) &&
976 a->major_minor != b->major_minor)
977 return false;
978
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200979 return true;
980}
981
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100982static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200983 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200984 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100985 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200986 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100987 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200988
989 assert(fname);
990 assert(line >= 1);
991 assert(buffer);
992
Lennart Poettering468d7262012-01-17 15:04:12 +0100993 i = new0(Item, 1);
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700994 if (!i)
995 return log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200996
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100997 if (sscanf(buffer,
998 "%c "
999 "%ms "
1000 "%ms "
1001 "%ms "
1002 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +01001003 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001004 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +01001005 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001006 &i->path,
1007 &mode,
1008 &user,
1009 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001010 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001011 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001012 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001013 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001014 goto finish;
1015 }
1016
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001017 if (n >= 0) {
1018 n += strspn(buffer+n, WHITESPACE);
1019 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1020 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001021 if (!i->argument)
1022 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001023 }
1024 }
1025
Michal Schmidt777b87e2011-12-16 18:27:35 +01001026 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001027
Michal Schmidt777b87e2011-12-16 18:27:35 +01001028 case CREATE_FILE:
1029 case TRUNCATE_FILE:
1030 case CREATE_DIRECTORY:
1031 case TRUNCATE_DIRECTORY:
1032 case CREATE_FIFO:
1033 case IGNORE_PATH:
1034 case REMOVE_PATH:
1035 case RECURSIVE_REMOVE_PATH:
1036 case RELABEL_PATH:
1037 case RECURSIVE_RELABEL_PATH:
1038 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001039
1040 case CREATE_SYMLINK:
1041 if (!i->argument) {
1042 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1043 r = -EBADMSG;
1044 goto finish;
1045 }
1046 break;
1047
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001048 case WRITE_FILE:
1049 if (!i->argument) {
1050 log_error("[%s:%u] Write file requires argument.", fname, line);
1051 r = -EBADMSG;
1052 goto finish;
1053 }
1054 break;
1055
Lennart Poettering468d7262012-01-17 15:04:12 +01001056 case CREATE_CHAR_DEVICE:
1057 case CREATE_BLOCK_DEVICE: {
1058 unsigned major, minor;
1059
1060 if (!i->argument) {
1061 log_error("[%s:%u] Device file requires argument.", fname, line);
1062 r = -EBADMSG;
1063 goto finish;
1064 }
1065
1066 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1067 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1068 r = -EBADMSG;
1069 goto finish;
1070 }
1071
1072 i->major_minor = makedev(major, minor);
1073 break;
1074 }
1075
Michal Schmidt777b87e2011-12-16 18:27:35 +01001076 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001077 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001078 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001079 goto finish;
1080 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001081
Michal Schmidta8d88782011-12-15 23:11:07 +01001082 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001083
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001084 if (!path_is_absolute(i->path)) {
1085 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1086 r = -EBADMSG;
1087 goto finish;
1088 }
1089
1090 path_kill_slashes(i->path);
1091
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001092 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001093 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001094 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001095 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001096
1097 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001098 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001099
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001100 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001101 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001102 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1103 goto finish;
1104 }
1105
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001106 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001107 }
1108
1109 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001110 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001111
Lennart Poettering4b678342011-07-23 01:17:59 +02001112 r = get_group_creds(&g, &i->gid);
1113 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001114 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1115 goto finish;
1116 }
1117
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001118 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001119 }
1120
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001121 if (mode && !streq(mode, "-")) {
1122 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001123
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001124 if (sscanf(mode, "%o", &m) != 1) {
1125 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1126 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001127 goto finish;
1128 }
1129
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001130 i->mode = m;
1131 i->mode_set = true;
1132 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001133 i->mode =
1134 i->type == CREATE_DIRECTORY ||
1135 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001136
1137 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001138 const char *a = age;
1139
1140 if (*a == '~') {
1141 i->keep_first_level = true;
1142 a++;
1143 }
1144
1145 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001146 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1147 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001148 goto finish;
1149 }
1150
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001151 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001152 }
1153
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001154 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001155
Lennart Poettering468d7262012-01-17 15:04:12 +01001156 existing = hashmap_get(h, i->path);
1157 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001158
1159 /* Two identical items are fine */
1160 if (!item_equal(existing, i))
1161 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1162
1163 r = 0;
1164 goto finish;
1165 }
1166
Lennart Poettering468d7262012-01-17 15:04:12 +01001167 r = hashmap_put(h, i->path, i);
1168 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001169 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001170 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001171 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001172
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001173 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001174 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001175
1176finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001177 free(user);
1178 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001179 free(mode);
1180 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001181
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001182 if (i)
1183 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001184
1185 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001186}
1187
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001188static int help(void) {
1189
Lennart Poettering522d4a42011-02-13 15:08:15 +01001190 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1191 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001192 " -h --help Show this help\n"
1193 " --create Create marked files/directories\n"
1194 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001195 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001196 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001197 program_invocation_short_name);
1198
1199 return 0;
1200}
1201
1202static int parse_argv(int argc, char *argv[]) {
1203
1204 enum {
1205 ARG_CREATE,
1206 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001207 ARG_REMOVE,
1208 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001209 };
1210
1211 static const struct option options[] = {
1212 { "help", no_argument, NULL, 'h' },
1213 { "create", no_argument, NULL, ARG_CREATE },
1214 { "clean", no_argument, NULL, ARG_CLEAN },
1215 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001216 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001217 { NULL, 0, NULL, 0 }
1218 };
1219
1220 int c;
1221
1222 assert(argc >= 0);
1223 assert(argv);
1224
1225 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1226
1227 switch (c) {
1228
1229 case 'h':
1230 help();
1231 return 0;
1232
1233 case ARG_CREATE:
1234 arg_create = true;
1235 break;
1236
1237 case ARG_CLEAN:
1238 arg_clean = true;
1239 break;
1240
1241 case ARG_REMOVE:
1242 arg_remove = true;
1243 break;
1244
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001245 case ARG_PREFIX:
1246 arg_prefix = optarg;
1247 break;
1248
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001249 case '?':
1250 return -EINVAL;
1251
1252 default:
1253 log_error("Unknown option code %c", c);
1254 return -EINVAL;
1255 }
1256 }
1257
1258 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001259 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001260 return -EINVAL;
1261 }
1262
1263 return 1;
1264}
1265
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001266static int read_config_file(const char *fn, bool ignore_enoent) {
1267 FILE *f;
1268 unsigned v = 0;
1269 int r = 0;
1270
1271 assert(fn);
1272
Lennart Poettering468d7262012-01-17 15:04:12 +01001273 f = fopen(fn, "re");
1274 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001275
1276 if (ignore_enoent && errno == ENOENT)
1277 return 0;
1278
1279 log_error("Failed to open %s: %m", fn);
1280 return -errno;
1281 }
1282
Kay Sievers772f8372011-04-25 21:38:21 +02001283 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001284 for (;;) {
1285 char line[LINE_MAX], *l;
1286 int k;
1287
1288 if (!(fgets(line, sizeof(line), f)))
1289 break;
1290
1291 v++;
1292
1293 l = strstrip(line);
1294 if (*l == '#' || *l == 0)
1295 continue;
1296
1297 if ((k = parse_line(fn, v, l)) < 0)
1298 if (r == 0)
1299 r = k;
1300 }
1301
1302 if (ferror(f)) {
1303 log_error("Failed to read from file %s: %m", fn);
1304 if (r == 0)
1305 r = -EIO;
1306 }
1307
1308 fclose(f);
1309
1310 return r;
1311}
1312
Dave Reisner91256702012-06-08 22:31:19 -04001313static char *resolve_fragment(const char *fragment, const char **search_paths) {
1314 const char **p;
1315 char *resolved_path;
1316
1317 if (is_path(fragment))
1318 return strdup(fragment);
1319
1320 STRV_FOREACH(p, search_paths) {
Lennart Poetteringb7def682012-07-13 13:41:01 +02001321 resolved_path = strjoin(*p, "/", fragment, NULL);
Dave Reisner91256702012-06-08 22:31:19 -04001322 if (resolved_path == NULL) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001323 log_oom();
Dave Reisner91256702012-06-08 22:31:19 -04001324 return NULL;
1325 }
1326
1327 if (access(resolved_path, F_OK) == 0)
1328 return resolved_path;
1329
1330 free(resolved_path);
1331 }
1332
Kay Sieversca2e8942012-06-10 19:21:50 +02001333 errno = ENOENT;
Dave Reisner91256702012-06-08 22:31:19 -04001334 return NULL;
1335}
1336
Lennart Poettering5008d582010-09-28 02:34:02 +02001337int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001338 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001339 Item *i;
1340 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001341
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001342 r = parse_argv(argc, argv);
1343 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001344 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1345
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001346 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001347 log_parse_environment();
1348 log_open();
1349
Lennart Poettering4c126262011-08-01 20:52:18 +02001350 umask(0022);
1351
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001352 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001353
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001354 items = hashmap_new(string_hash_func, string_compare_func);
1355 globs = hashmap_new(string_hash_func, string_compare_func);
1356
1357 if (!items || !globs) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001358 log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001359 r = EXIT_FAILURE;
1360 goto finish;
1361 }
1362
Lennart Poettering5008d582010-09-28 02:34:02 +02001363 r = EXIT_SUCCESS;
1364
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001365 if (optind < argc) {
1366 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001367
Dave Reisner91256702012-06-08 22:31:19 -04001368 for (j = optind; j < argc; j++) {
Kay Sieversca2e8942012-06-10 19:21:50 +02001369 char *fragment;
1370
Lennart Poettering24f3a372012-06-20 09:05:50 +02001371 fragment = resolve_fragment(argv[j], (const char**) conf_file_dirs);
Kay Sieversca2e8942012-06-10 19:21:50 +02001372 if (!fragment) {
Kay Sievers94f7a712012-06-10 19:31:39 +02001373 log_error("Failed to find a %s file: %m", argv[j]);
Kay Sieversca2e8942012-06-10 19:21:50 +02001374 r = EXIT_FAILURE;
1375 goto finish;
1376 }
Dave Reisner91256702012-06-08 22:31:19 -04001377 if (read_config_file(fragment, false) < 0)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001378 r = EXIT_FAILURE;
Dave Reisner91256702012-06-08 22:31:19 -04001379 free(fragment);
1380 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001381
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001382 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001383 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001384
Dave Reisner91256702012-06-08 22:31:19 -04001385 r = conf_files_list_strv(&files, ".conf",
Lennart Poettering24f3a372012-06-20 09:05:50 +02001386 (const char **) conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001387 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001388 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001389 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001390 goto finish;
1391 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001392
Kay Sievers772f8372011-04-25 21:38:21 +02001393 STRV_FOREACH(f, files) {
1394 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001395 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001396 }
1397
Kay Sievers772f8372011-04-25 21:38:21 +02001398 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001399 }
1400
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001401 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001402 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001403
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001404 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001405 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001406
Lennart Poettering5008d582010-09-28 02:34:02 +02001407finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001408 while ((i = hashmap_steal_first(items)))
1409 item_free(i);
1410
Lennart Poettering17b90522011-02-14 21:55:06 +01001411 while ((i = hashmap_steal_first(globs)))
1412 item_free(i);
1413
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001414 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001415 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001416
Lennart Poettering17b90522011-02-14 21:55:06 +01001417 set_free_free(unix_sockets);
1418
Lennart Poettering29003cf2010-10-19 19:36:45 +02001419 label_finish();
1420
Lennart Poettering5008d582010-09-28 02:34:02 +02001421 return r;
1422}