blob: e70332ca0635fceab007e3633913297578033655 [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 Poettering5008d582010-09-28 02:34:02 +020041
42#include "log.h"
43#include "util.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020044#include "mkdir.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020045#include "path-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020046#include "strv.h"
47#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020048#include "set.h"
Kay Sievers2c210442012-05-07 18:55:45 +020049#include "conf-files.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020050
Andreas Jaeger01000472010-09-29 10:08:24 +020051/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020052 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020053 * properly owned directories beneath /tmp, /var/tmp, /run, which are
54 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020055
Michal Schmidt66ccd032011-12-15 21:31:14 +010056typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010057 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020058 CREATE_FILE = 'f',
59 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010060 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020061 CREATE_DIRECTORY = 'd',
62 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020063 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010064 CREATE_SYMLINK = 'L',
65 CREATE_CHAR_DEVICE = 'c',
66 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010067
68 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020069 IGNORE_PATH = 'x',
70 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010071 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010072 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010073 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010074} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020075
76typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010077 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020078
79 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010080 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020081 uid_t uid;
82 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020083 mode_t mode;
84 usec_t age;
85
Lennart Poettering468d7262012-01-17 15:04:12 +010086 dev_t major_minor;
87
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020088 bool uid_set:1;
89 bool gid_set:1;
90 bool mode_set:1;
91 bool age_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +020092
93 bool keep_first_level:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020094} Item;
95
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010096static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010097static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020098
99static bool arg_create = false;
100static bool arg_clean = false;
101static bool arg_remove = false;
102
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100103static const char *arg_prefix = NULL;
104
Lennart Poettering24f3a372012-06-20 09:05:50 +0200105static const char * const conf_file_dirs[] = {
Dave Reisner91256702012-06-08 22:31:19 -0400106 "/etc/tmpfiles.d",
107 "/run/tmpfiles.d",
108 "/usr/local/lib/tmpfiles.d",
109 "/usr/lib/tmpfiles.d",
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200110#ifdef HAVE_SPLIT_USR
111 "/lib/tmpfiles.d",
112#endif
Dave Reisner91256702012-06-08 22:31:19 -0400113 NULL
114};
115
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200116#define MAX_DEPTH 256
117
Michal Schmidt66ccd032011-12-15 21:31:14 +0100118static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100119 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 +0100120}
121
122static struct Item* find_glob(Hashmap *h, const char *match) {
123 Item *j;
124 Iterator i;
125
126 HASHMAP_FOREACH(j, h, i)
127 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
128 return j;
129
130 return NULL;
131}
132
Lennart Poettering17b90522011-02-14 21:55:06 +0100133static void load_unix_sockets(void) {
134 FILE *f = NULL;
135 char line[LINE_MAX];
136
137 if (unix_sockets)
138 return;
139
140 /* We maintain a cache of the sockets we found in
141 * /proc/net/unix to speed things up a little. */
142
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100143 unix_sockets = set_new(string_hash_func, string_compare_func);
144 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100145 return;
146
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100147 f = fopen("/proc/net/unix", "re");
148 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100149 return;
150
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100151 /* Skip header */
152 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100153 goto fail;
154
155 for (;;) {
156 char *p, *s;
157 int k;
158
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100159 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100160 break;
161
162 truncate_nl(line);
163
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100164 p = strchr(line, ':');
165 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100166 continue;
167
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100168 if (strlen(p) < 37)
169 continue;
170
171 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100172 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100173 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100174 p += strspn(p, WHITESPACE);
175
176 if (*p != '/')
177 continue;
178
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100179 s = strdup(p);
180 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100181 goto fail;
182
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100183 path_kill_slashes(s);
184
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100185 k = set_put(unix_sockets, s);
186 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100187 free(s);
188
189 if (k != -EEXIST)
190 goto fail;
191 }
192 }
193
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200194 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100195 return;
196
197fail:
198 set_free_free(unix_sockets);
199 unix_sockets = NULL;
200
201 if (f)
202 fclose(f);
203}
204
205static bool unix_socket_alive(const char *fn) {
206 assert(fn);
207
208 load_unix_sockets();
209
210 if (unix_sockets)
211 return !!set_get(unix_sockets, (char*) fn);
212
213 /* We don't know, so assume yes */
214 return true;
215}
216
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200217static int dir_cleanup(
218 const char *p,
219 DIR *d,
220 const struct stat *ds,
221 usec_t cutoff,
222 dev_t rootdev,
223 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200224 int maxdepth,
225 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200226{
227 struct dirent *dent;
228 struct timespec times[2];
229 bool deleted = false;
230 char *sub_path = NULL;
231 int r = 0;
232
233 while ((dent = readdir(d))) {
234 struct stat s;
235 usec_t age;
236
237 if (streq(dent->d_name, ".") ||
238 streq(dent->d_name, ".."))
239 continue;
240
241 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
242
243 if (errno != ENOENT) {
244 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
245 r = -errno;
246 }
247
248 continue;
249 }
250
251 /* Stay on the same filesystem */
252 if (s.st_dev != rootdev)
253 continue;
254
255 /* Do not delete read-only files owned by root */
256 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
257 continue;
258
259 free(sub_path);
260 sub_path = NULL;
261
262 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700263 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200264 goto finish;
265 }
266
267 /* Is there an item configured for this path? */
268 if (hashmap_get(items, sub_path))
269 continue;
270
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100271 if (find_glob(globs, sub_path))
272 continue;
273
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200274 if (S_ISDIR(s.st_mode)) {
275
276 if (mountpoint &&
277 streq(dent->d_name, "lost+found") &&
278 s.st_uid == 0)
279 continue;
280
281 if (maxdepth <= 0)
282 log_warning("Reached max depth on %s.", sub_path);
283 else {
284 DIR *sub_dir;
285 int q;
286
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200287 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200288 if (sub_dir == NULL) {
289 if (errno != ENOENT) {
290 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
291 r = -errno;
292 }
293
294 continue;
295 }
296
Lennart Poettering24f3a372012-06-20 09:05:50 +0200297 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200298 closedir(sub_dir);
299
300 if (q < 0)
301 r = q;
302 }
303
Lennart Poettering24f3a372012-06-20 09:05:50 +0200304 /* Note: if you are wondering why we don't
305 * support the sticky bit for excluding
306 * directories from cleaning like we do it for
307 * other file system objects: well, the sticky
308 * bit already has a meaning for directories,
309 * so we don't want to overload that. */
310
311 if (keep_this_level)
312 continue;
313
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200314 /* Ignore ctime, we change it when deleting */
315 age = MAX(timespec_load(&s.st_mtim),
316 timespec_load(&s.st_atim));
317 if (age >= cutoff)
318 continue;
319
320 log_debug("rmdir '%s'\n", sub_path);
321
322 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
323 if (errno != ENOENT && errno != ENOTEMPTY) {
324 log_error("rmdir(%s): %m", sub_path);
325 r = -errno;
326 }
327 }
328
329 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100330 /* Skip files for which the sticky bit is
331 * set. These are semantics we define, and are
332 * unknown elsewhere. See XDG_RUNTIME_DIR
333 * specification for details. */
334 if (s.st_mode & S_ISVTX)
335 continue;
336
Lennart Poettering17b90522011-02-14 21:55:06 +0100337 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200338 if (streq(dent->d_name, ".journal") &&
339 s.st_uid == 0)
340 continue;
341
342 if (streq(dent->d_name, "aquota.user") ||
343 streq(dent->d_name, "aquota.group"))
344 continue;
345 }
346
Lennart Poettering17b90522011-02-14 21:55:06 +0100347 /* Ignore sockets that are listed in /proc/net/unix */
348 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
349 continue;
350
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100351 /* Ignore device nodes */
352 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
353 continue;
354
Lennart Poettering24f3a372012-06-20 09:05:50 +0200355 /* Keep files on this level around if this is
356 * requested */
357 if (keep_this_level)
358 continue;
359
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200360 age = MAX3(timespec_load(&s.st_mtim),
361 timespec_load(&s.st_atim),
362 timespec_load(&s.st_ctim));
363
364 if (age >= cutoff)
365 continue;
366
367 log_debug("unlink '%s'\n", sub_path);
368
369 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
370 if (errno != ENOENT) {
371 log_error("unlink(%s): %m", sub_path);
372 r = -errno;
373 }
374 }
375
376 deleted = true;
377 }
378 }
379
380finish:
381 if (deleted) {
382 /* Restore original directory timestamps */
383 times[0] = ds->st_atim;
384 times[1] = ds->st_mtim;
385
386 if (futimens(dirfd(d), times) < 0)
387 log_error("utimensat(%s): %m", p);
388 }
389
390 free(sub_path);
391
392 return r;
393}
394
395static int clean_item(Item *i) {
396 DIR *d;
397 struct stat s, ps;
398 bool mountpoint;
399 int r;
400 usec_t cutoff, n;
401
402 assert(i);
403
404 if (i->type != CREATE_DIRECTORY &&
405 i->type != TRUNCATE_DIRECTORY &&
406 i->type != IGNORE_PATH)
407 return 0;
408
409 if (!i->age_set || i->age <= 0)
410 return 0;
411
412 n = now(CLOCK_REALTIME);
413 if (n < i->age)
414 return 0;
415
416 cutoff = n - i->age;
417
418 d = opendir(i->path);
419 if (!d) {
420 if (errno == ENOENT)
421 return 0;
422
423 log_error("Failed to open directory %s: %m", i->path);
424 return -errno;
425 }
426
427 if (fstat(dirfd(d), &s) < 0) {
428 log_error("stat(%s) failed: %m", i->path);
429 r = -errno;
430 goto finish;
431 }
432
433 if (!S_ISDIR(s.st_mode)) {
434 log_error("%s is not a directory.", i->path);
435 r = -ENOTDIR;
436 goto finish;
437 }
438
439 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
440 log_error("stat(%s/..) failed: %m", i->path);
441 r = -errno;
442 goto finish;
443 }
444
445 mountpoint = s.st_dev != ps.st_dev ||
446 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
447
Lennart Poettering24f3a372012-06-20 09:05:50 +0200448 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 +0200449
450finish:
451 if (d)
452 closedir(d);
453
454 return r;
455}
456
Michal Schmidt062e01b2011-12-16 18:00:11 +0100457static int item_set_perms(Item *i, const char *path) {
458 /* not using i->path directly because it may be a glob */
459 if (i->mode_set)
460 if (chmod(path, i->mode) < 0) {
461 log_error("chmod(%s) failed: %m", path);
462 return -errno;
463 }
464
465 if (i->uid_set || i->gid_set)
466 if (chown(path,
467 i->uid_set ? i->uid : (uid_t) -1,
468 i->gid_set ? i->gid : (gid_t) -1) < 0) {
469
470 log_error("chown(%s) failed: %m", path);
471 return -errno;
472 }
473
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200474 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100475}
476
477static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100478 DIR *d;
479 int ret = 0;
480
481 /* This returns the first error we run into, but nevertheless
482 * tries to go on */
483
484 d = opendir(path);
485 if (!d)
486 return errno == ENOENT ? 0 : -errno;
487
488 for (;;) {
489 struct dirent buf, *de;
490 bool is_dir;
491 int r;
492 char *entry_path;
493
494 r = readdir_r(d, &buf, &de);
495 if (r != 0) {
496 if (ret == 0)
497 ret = -r;
498 break;
499 }
500
501 if (!de)
502 break;
503
504 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
505 continue;
506
507 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
508 if (ret == 0)
509 ret = -ENOMEM;
510 continue;
511 }
512
513 if (de->d_type == DT_UNKNOWN) {
514 struct stat st;
515
516 if (lstat(entry_path, &st) < 0) {
517 if (ret == 0 && errno != ENOENT)
518 ret = -errno;
519 free(entry_path);
520 continue;
521 }
522
523 is_dir = S_ISDIR(st.st_mode);
524
525 } else
526 is_dir = de->d_type == DT_DIR;
527
Michal Schmidt062e01b2011-12-16 18:00:11 +0100528 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100529 if (r < 0) {
530 if (ret == 0 && r != -ENOENT)
531 ret = r;
532 free(entry_path);
533 continue;
534 }
535
536 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100537 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100538 if (r < 0 && ret == 0)
539 ret = r;
540 }
541
542 free(entry_path);
543 }
544
545 closedir(d);
546
547 return ret;
548}
549
550static int recursive_relabel(Item *i, const char *path) {
551 int r;
552 struct stat st;
553
Michal Schmidt062e01b2011-12-16 18:00:11 +0100554 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100555 if (r < 0)
556 return r;
557
558 if (lstat(path, &st) < 0)
559 return -errno;
560
561 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100562 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100563
564 return r;
565}
566
Michal Schmidt99e68c02011-12-15 23:45:26 +0100567static int glob_item(Item *i, int (*action)(Item *, const char *)) {
568 int r = 0, k;
569 glob_t g;
570 char **fn;
571
572 zero(g);
573
574 errno = 0;
575 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
576
577 if (k != GLOB_NOMATCH) {
578 if (errno != 0)
579 errno = EIO;
580
581 log_error("glob(%s) failed: %m", i->path);
582 return -errno;
583 }
584 }
585
586 STRV_FOREACH(fn, g.gl_pathv)
587 if ((k = action(i, *fn)) < 0)
588 r = k;
589
590 globfree(&g);
591 return r;
592}
593
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200594static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200595 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200596 mode_t u;
597 struct stat st;
598
599 assert(i);
600
601 switch (i->type) {
602
603 case IGNORE_PATH:
604 case REMOVE_PATH:
605 case RECURSIVE_REMOVE_PATH:
606 return 0;
607
608 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100609 case TRUNCATE_FILE:
610 case WRITE_FILE: {
611 int fd, flags;
612
613 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
614 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200615
616 u = umask(0);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200617 label_context_set(i->path, S_IFREG);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100618 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200619 e = errno;
620 label_context_clear();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200621 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200622 errno = e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200623
624 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100625 if (i->type == WRITE_FILE && errno == ENOENT)
626 break;
627
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200628 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100629 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200630 }
631
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100632 if (i->argument) {
633 ssize_t n;
634 size_t l;
635 struct iovec iovec[2];
636 static const char new_line = '\n';
637
638 l = strlen(i->argument);
639
640 zero(iovec);
641 iovec[0].iov_base = i->argument;
642 iovec[0].iov_len = l;
643
644 iovec[1].iov_base = (void*) &new_line;
645 iovec[1].iov_len = 1;
646
647 n = writev(fd, iovec, 2);
Lennart Poettering03ad1132012-05-15 14:34:33 +0200648
649 /* It's OK if we don't write the trailing
650 * newline, hence we check for l, instead of
651 * l+1 here. Files in /sys often refuse
652 * writing of the trailing newline. */
653 if (n < 0 || (size_t) n < l) {
654 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100655 close_nointr_nofail(fd);
656 return n < 0 ? n : -EIO;
657 }
658 }
659
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100660 close_nointr_nofail(fd);
661
662 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200663 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100664 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200665 }
666
667 if (!S_ISREG(st.st_mode)) {
668 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100669 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200670 }
671
Michal Schmidt062e01b2011-12-16 18:00:11 +0100672 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100673 if (r < 0)
674 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200675
676 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100677 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200678
679 case TRUNCATE_DIRECTORY:
680 case CREATE_DIRECTORY:
681
682 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200683 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200684 r = mkdir(i->path, i->mode);
685 umask(u);
686
687 if (r < 0 && errno != EEXIST) {
688 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100689 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200690 }
691
692 if (stat(i->path, &st) < 0) {
693 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100694 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200695 }
696
697 if (!S_ISDIR(st.st_mode)) {
698 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100699 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200700 }
701
Michal Schmidt062e01b2011-12-16 18:00:11 +0100702 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100703 if (r < 0)
704 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200705
706 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200707
708 case CREATE_FIFO:
709
710 u = umask(0);
711 r = mkfifo(i->path, i->mode);
712 umask(u);
713
714 if (r < 0 && errno != EEXIST) {
715 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100716 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200717 }
718
719 if (stat(i->path, &st) < 0) {
720 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100721 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200722 }
723
724 if (!S_ISFIFO(st.st_mode)) {
725 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100726 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200727 }
728
Michal Schmidt062e01b2011-12-16 18:00:11 +0100729 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100730 if (r < 0)
731 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200732
733 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100734
Lennart Poettering468d7262012-01-17 15:04:12 +0100735 case CREATE_SYMLINK: {
736 char *x;
737
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200738 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100739 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200740 e = errno;
741 label_context_clear();
742 errno = e;
743
Lennart Poettering468d7262012-01-17 15:04:12 +0100744 if (r < 0 && errno != EEXIST) {
745 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
746 return -errno;
747 }
748
749 r = readlink_malloc(i->path, &x);
750 if (r < 0) {
751 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
752 return -errno;
753 }
754
755 if (!streq(i->argument, x)) {
756 free(x);
757 log_error("%s is not the right symlinks.", i->path);
758 return -EEXIST;
759 }
760
761 free(x);
762 break;
763 }
764
765 case CREATE_BLOCK_DEVICE:
766 case CREATE_CHAR_DEVICE: {
Michal Schmidte7aee752012-06-14 16:01:19 +0200767 mode_t file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100768
769 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200770 label_context_set(i->path, file_type);
771 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200772 e = errno;
773 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100774 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200775 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100776
777 if (r < 0 && errno != EEXIST) {
778 log_error("Failed to create device node %s: %m", i->path);
779 return -errno;
780 }
781
782 if (stat(i->path, &st) < 0) {
783 log_error("stat(%s) failed: %m", i->path);
784 return -errno;
785 }
786
Michal Schmidte7aee752012-06-14 16:01:19 +0200787 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100788 log_error("%s is not a device node.", i->path);
789 return -EEXIST;
790 }
791
792 r = item_set_perms(i, i->path);
793 if (r < 0)
794 return r;
795
796 break;
797 }
798
Michal Schmidt777b87e2011-12-16 18:27:35 +0100799 case RELABEL_PATH:
800
801 r = glob_item(i, item_set_perms);
802 if (r < 0)
803 return 0;
804 break;
805
Michal Schmidta8d88782011-12-15 23:11:07 +0100806 case RECURSIVE_RELABEL_PATH:
807
808 r = glob_item(i, recursive_relabel);
809 if (r < 0)
810 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200811 }
812
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200813 log_debug("%s created successfully.", i->path);
814
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100815 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200816}
817
Michal Schmidta0896122011-12-15 21:32:50 +0100818static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200819 int r;
820
821 assert(i);
822
823 switch (i->type) {
824
825 case CREATE_FILE:
826 case TRUNCATE_FILE:
827 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200828 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100829 case CREATE_SYMLINK:
830 case CREATE_BLOCK_DEVICE:
831 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200832 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100833 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100834 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100835 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200836 break;
837
838 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100839 if (remove(instance) < 0 && errno != ENOENT) {
840 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200841 return -errno;
842 }
843
844 break;
845
846 case TRUNCATE_DIRECTORY:
847 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200848 /* FIXME: we probably should use dir_cleanup() here
849 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200850 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100851 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100852 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200853 return r;
854 }
855
856 break;
857 }
858
859 return 0;
860}
861
Michal Schmidta0896122011-12-15 21:32:50 +0100862static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100863 int r = 0;
864
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100865 assert(i);
866
867 switch (i->type) {
868
869 case CREATE_FILE:
870 case TRUNCATE_FILE:
871 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200872 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100873 case CREATE_SYMLINK:
874 case CREATE_CHAR_DEVICE:
875 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100876 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100877 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100878 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100879 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100880 break;
881
882 case REMOVE_PATH:
883 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100884 case RECURSIVE_REMOVE_PATH:
885 r = glob_item(i, remove_item_instance);
886 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100887 }
888
Michal Schmidt99e68c02011-12-15 23:45:26 +0100889 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100890}
891
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200892static int process_item(Item *i) {
893 int r, q, p;
894
895 assert(i);
896
897 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100898 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200899 p = arg_clean ? clean_item(i) : 0;
900
901 if (r < 0)
902 return r;
903
904 if (q < 0)
905 return q;
906
907 return p;
908}
909
910static void item_free(Item *i) {
911 assert(i);
912
913 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100914 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200915 free(i);
916}
917
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200918static bool item_equal(Item *a, Item *b) {
919 assert(a);
920 assert(b);
921
922 if (!streq_ptr(a->path, b->path))
923 return false;
924
925 if (a->type != b->type)
926 return false;
927
928 if (a->uid_set != b->uid_set ||
929 (a->uid_set && a->uid != b->uid))
930 return false;
931
932 if (a->gid_set != b->gid_set ||
933 (a->gid_set && a->gid != b->gid))
934 return false;
935
936 if (a->mode_set != b->mode_set ||
937 (a->mode_set && a->mode != b->mode))
938 return false;
939
940 if (a->age_set != b->age_set ||
941 (a->age_set && a->age != b->age))
942 return false;
943
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100944 if ((a->type == CREATE_FILE ||
945 a->type == TRUNCATE_FILE ||
946 a->type == WRITE_FILE ||
947 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100948 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100949 return false;
950
951 if ((a->type == CREATE_CHAR_DEVICE ||
952 a->type == CREATE_BLOCK_DEVICE) &&
953 a->major_minor != b->major_minor)
954 return false;
955
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200956 return true;
957}
958
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100959static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200960 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200961 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100962 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200963 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100964 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200965
966 assert(fname);
967 assert(line >= 1);
968 assert(buffer);
969
Lennart Poettering468d7262012-01-17 15:04:12 +0100970 i = new0(Item, 1);
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700971 if (!i)
972 return log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200973
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100974 if (sscanf(buffer,
975 "%c "
976 "%ms "
977 "%ms "
978 "%ms "
979 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100980 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100981 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100982 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100983 &i->path,
984 &mode,
985 &user,
986 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +0100987 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100988 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200989 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200990 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200991 goto finish;
992 }
993
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100994 if (n >= 0) {
995 n += strspn(buffer+n, WHITESPACE);
996 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
997 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700998 if (!i->argument)
999 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001000 }
1001 }
1002
Michal Schmidt777b87e2011-12-16 18:27:35 +01001003 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001004
Michal Schmidt777b87e2011-12-16 18:27:35 +01001005 case CREATE_FILE:
1006 case TRUNCATE_FILE:
1007 case CREATE_DIRECTORY:
1008 case TRUNCATE_DIRECTORY:
1009 case CREATE_FIFO:
1010 case IGNORE_PATH:
1011 case REMOVE_PATH:
1012 case RECURSIVE_REMOVE_PATH:
1013 case RELABEL_PATH:
1014 case RECURSIVE_RELABEL_PATH:
1015 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001016
1017 case CREATE_SYMLINK:
1018 if (!i->argument) {
1019 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1020 r = -EBADMSG;
1021 goto finish;
1022 }
1023 break;
1024
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001025 case WRITE_FILE:
1026 if (!i->argument) {
1027 log_error("[%s:%u] Write file requires argument.", fname, line);
1028 r = -EBADMSG;
1029 goto finish;
1030 }
1031 break;
1032
Lennart Poettering468d7262012-01-17 15:04:12 +01001033 case CREATE_CHAR_DEVICE:
1034 case CREATE_BLOCK_DEVICE: {
1035 unsigned major, minor;
1036
1037 if (!i->argument) {
1038 log_error("[%s:%u] Device file requires argument.", fname, line);
1039 r = -EBADMSG;
1040 goto finish;
1041 }
1042
1043 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1044 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1045 r = -EBADMSG;
1046 goto finish;
1047 }
1048
1049 i->major_minor = makedev(major, minor);
1050 break;
1051 }
1052
Michal Schmidt777b87e2011-12-16 18:27:35 +01001053 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001054 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001055 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001056 goto finish;
1057 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001058
Michal Schmidta8d88782011-12-15 23:11:07 +01001059 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001060
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001061 if (!path_is_absolute(i->path)) {
1062 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1063 r = -EBADMSG;
1064 goto finish;
1065 }
1066
1067 path_kill_slashes(i->path);
1068
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001069 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001070 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001071 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001072 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001073
1074 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001075 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001076
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001077 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001078 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001079 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1080 goto finish;
1081 }
1082
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001083 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001084 }
1085
1086 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001087 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001088
Lennart Poettering4b678342011-07-23 01:17:59 +02001089 r = get_group_creds(&g, &i->gid);
1090 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001091 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1092 goto finish;
1093 }
1094
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001095 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001096 }
1097
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001098 if (mode && !streq(mode, "-")) {
1099 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001100
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001101 if (sscanf(mode, "%o", &m) != 1) {
1102 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1103 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001104 goto finish;
1105 }
1106
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001107 i->mode = m;
1108 i->mode_set = true;
1109 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001110 i->mode =
1111 i->type == CREATE_DIRECTORY ||
1112 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001113
1114 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001115 const char *a = age;
1116
1117 if (*a == '~') {
1118 i->keep_first_level = true;
1119 a++;
1120 }
1121
1122 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001123 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1124 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001125 goto finish;
1126 }
1127
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001128 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001129 }
1130
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001131 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001132
Lennart Poettering468d7262012-01-17 15:04:12 +01001133 existing = hashmap_get(h, i->path);
1134 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001135
1136 /* Two identical items are fine */
1137 if (!item_equal(existing, i))
1138 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1139
1140 r = 0;
1141 goto finish;
1142 }
1143
Lennart Poettering468d7262012-01-17 15:04:12 +01001144 r = hashmap_put(h, i->path, i);
1145 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001146 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001147 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001148 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001149
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001150 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001151 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001152
1153finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001154 free(user);
1155 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001156 free(mode);
1157 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001158
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001159 if (i)
1160 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001161
1162 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001163}
1164
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001165static int help(void) {
1166
Lennart Poettering522d4a42011-02-13 15:08:15 +01001167 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1168 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001169 " -h --help Show this help\n"
1170 " --create Create marked files/directories\n"
1171 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001172 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001173 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001174 program_invocation_short_name);
1175
1176 return 0;
1177}
1178
1179static int parse_argv(int argc, char *argv[]) {
1180
1181 enum {
1182 ARG_CREATE,
1183 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001184 ARG_REMOVE,
1185 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001186 };
1187
1188 static const struct option options[] = {
1189 { "help", no_argument, NULL, 'h' },
1190 { "create", no_argument, NULL, ARG_CREATE },
1191 { "clean", no_argument, NULL, ARG_CLEAN },
1192 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001193 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001194 { NULL, 0, NULL, 0 }
1195 };
1196
1197 int c;
1198
1199 assert(argc >= 0);
1200 assert(argv);
1201
1202 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1203
1204 switch (c) {
1205
1206 case 'h':
1207 help();
1208 return 0;
1209
1210 case ARG_CREATE:
1211 arg_create = true;
1212 break;
1213
1214 case ARG_CLEAN:
1215 arg_clean = true;
1216 break;
1217
1218 case ARG_REMOVE:
1219 arg_remove = true;
1220 break;
1221
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001222 case ARG_PREFIX:
1223 arg_prefix = optarg;
1224 break;
1225
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001226 case '?':
1227 return -EINVAL;
1228
1229 default:
1230 log_error("Unknown option code %c", c);
1231 return -EINVAL;
1232 }
1233 }
1234
1235 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001236 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001237 return -EINVAL;
1238 }
1239
1240 return 1;
1241}
1242
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001243static int read_config_file(const char *fn, bool ignore_enoent) {
1244 FILE *f;
1245 unsigned v = 0;
1246 int r = 0;
1247
1248 assert(fn);
1249
Lennart Poettering468d7262012-01-17 15:04:12 +01001250 f = fopen(fn, "re");
1251 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001252
1253 if (ignore_enoent && errno == ENOENT)
1254 return 0;
1255
1256 log_error("Failed to open %s: %m", fn);
1257 return -errno;
1258 }
1259
Kay Sievers772f8372011-04-25 21:38:21 +02001260 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001261 for (;;) {
1262 char line[LINE_MAX], *l;
1263 int k;
1264
1265 if (!(fgets(line, sizeof(line), f)))
1266 break;
1267
1268 v++;
1269
1270 l = strstrip(line);
1271 if (*l == '#' || *l == 0)
1272 continue;
1273
1274 if ((k = parse_line(fn, v, l)) < 0)
1275 if (r == 0)
1276 r = k;
1277 }
1278
1279 if (ferror(f)) {
1280 log_error("Failed to read from file %s: %m", fn);
1281 if (r == 0)
1282 r = -EIO;
1283 }
1284
1285 fclose(f);
1286
1287 return r;
1288}
1289
Dave Reisner91256702012-06-08 22:31:19 -04001290static char *resolve_fragment(const char *fragment, const char **search_paths) {
1291 const char **p;
1292 char *resolved_path;
1293
1294 if (is_path(fragment))
1295 return strdup(fragment);
1296
1297 STRV_FOREACH(p, search_paths) {
Lennart Poetteringb7def682012-07-13 13:41:01 +02001298 resolved_path = strjoin(*p, "/", fragment, NULL);
Dave Reisner91256702012-06-08 22:31:19 -04001299 if (resolved_path == NULL) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001300 log_oom();
Dave Reisner91256702012-06-08 22:31:19 -04001301 return NULL;
1302 }
1303
1304 if (access(resolved_path, F_OK) == 0)
1305 return resolved_path;
1306
1307 free(resolved_path);
1308 }
1309
Kay Sieversca2e8942012-06-10 19:21:50 +02001310 errno = ENOENT;
Dave Reisner91256702012-06-08 22:31:19 -04001311 return NULL;
1312}
1313
Lennart Poettering5008d582010-09-28 02:34:02 +02001314int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001315 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001316 Item *i;
1317 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001318
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001319 r = parse_argv(argc, argv);
1320 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001321 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1322
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001323 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001324 log_parse_environment();
1325 log_open();
1326
Lennart Poettering4c126262011-08-01 20:52:18 +02001327 umask(0022);
1328
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001329 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001330
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001331 items = hashmap_new(string_hash_func, string_compare_func);
1332 globs = hashmap_new(string_hash_func, string_compare_func);
1333
1334 if (!items || !globs) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001335 log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001336 r = EXIT_FAILURE;
1337 goto finish;
1338 }
1339
Lennart Poettering5008d582010-09-28 02:34:02 +02001340 r = EXIT_SUCCESS;
1341
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001342 if (optind < argc) {
1343 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001344
Dave Reisner91256702012-06-08 22:31:19 -04001345 for (j = optind; j < argc; j++) {
Kay Sieversca2e8942012-06-10 19:21:50 +02001346 char *fragment;
1347
Lennart Poettering24f3a372012-06-20 09:05:50 +02001348 fragment = resolve_fragment(argv[j], (const char**) conf_file_dirs);
Kay Sieversca2e8942012-06-10 19:21:50 +02001349 if (!fragment) {
Kay Sievers94f7a712012-06-10 19:31:39 +02001350 log_error("Failed to find a %s file: %m", argv[j]);
Kay Sieversca2e8942012-06-10 19:21:50 +02001351 r = EXIT_FAILURE;
1352 goto finish;
1353 }
Dave Reisner91256702012-06-08 22:31:19 -04001354 if (read_config_file(fragment, false) < 0)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001355 r = EXIT_FAILURE;
Dave Reisner91256702012-06-08 22:31:19 -04001356 free(fragment);
1357 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001358
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001359 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001360 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001361
Dave Reisner91256702012-06-08 22:31:19 -04001362 r = conf_files_list_strv(&files, ".conf",
Lennart Poettering24f3a372012-06-20 09:05:50 +02001363 (const char **) conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001364 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001365 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001366 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001367 goto finish;
1368 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001369
Kay Sievers772f8372011-04-25 21:38:21 +02001370 STRV_FOREACH(f, files) {
1371 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001372 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001373 }
1374
Kay Sievers772f8372011-04-25 21:38:21 +02001375 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001376 }
1377
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001378 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001379 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001380
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001381 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001382 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001383
Lennart Poettering5008d582010-09-28 02:34:02 +02001384finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001385 while ((i = hashmap_steal_first(items)))
1386 item_free(i);
1387
Lennart Poettering17b90522011-02-14 21:55:06 +01001388 while ((i = hashmap_steal_first(globs)))
1389 item_free(i);
1390
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001391 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001392 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001393
Lennart Poettering17b90522011-02-14 21:55:06 +01001394 set_free_free(unix_sockets);
1395
Lennart Poettering29003cf2010-10-19 19:36:45 +02001396 label_finish();
1397
Lennart Poettering5008d582010-09-28 02:34:02 +02001398 return r;
1399}