blob: f8d89218c9a4f3825a2b3c1c087b58a8ce546949 [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 Landden669241a2012-07-24 21:12:43 -0700263 log_error("Out of memory.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200264 r = -ENOMEM;
265 goto finish;
266 }
267
268 /* Is there an item configured for this path? */
269 if (hashmap_get(items, sub_path))
270 continue;
271
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100272 if (find_glob(globs, sub_path))
273 continue;
274
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200275 if (S_ISDIR(s.st_mode)) {
276
277 if (mountpoint &&
278 streq(dent->d_name, "lost+found") &&
279 s.st_uid == 0)
280 continue;
281
282 if (maxdepth <= 0)
283 log_warning("Reached max depth on %s.", sub_path);
284 else {
285 DIR *sub_dir;
286 int q;
287
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200288 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200289 if (sub_dir == NULL) {
290 if (errno != ENOENT) {
291 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
292 r = -errno;
293 }
294
295 continue;
296 }
297
Lennart Poettering24f3a372012-06-20 09:05:50 +0200298 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200299 closedir(sub_dir);
300
301 if (q < 0)
302 r = q;
303 }
304
Lennart Poettering24f3a372012-06-20 09:05:50 +0200305 /* Note: if you are wondering why we don't
306 * support the sticky bit for excluding
307 * directories from cleaning like we do it for
308 * other file system objects: well, the sticky
309 * bit already has a meaning for directories,
310 * so we don't want to overload that. */
311
312 if (keep_this_level)
313 continue;
314
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200315 /* Ignore ctime, we change it when deleting */
316 age = MAX(timespec_load(&s.st_mtim),
317 timespec_load(&s.st_atim));
318 if (age >= cutoff)
319 continue;
320
321 log_debug("rmdir '%s'\n", sub_path);
322
323 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
324 if (errno != ENOENT && errno != ENOTEMPTY) {
325 log_error("rmdir(%s): %m", sub_path);
326 r = -errno;
327 }
328 }
329
330 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100331 /* Skip files for which the sticky bit is
332 * set. These are semantics we define, and are
333 * unknown elsewhere. See XDG_RUNTIME_DIR
334 * specification for details. */
335 if (s.st_mode & S_ISVTX)
336 continue;
337
Lennart Poettering17b90522011-02-14 21:55:06 +0100338 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200339 if (streq(dent->d_name, ".journal") &&
340 s.st_uid == 0)
341 continue;
342
343 if (streq(dent->d_name, "aquota.user") ||
344 streq(dent->d_name, "aquota.group"))
345 continue;
346 }
347
Lennart Poettering17b90522011-02-14 21:55:06 +0100348 /* Ignore sockets that are listed in /proc/net/unix */
349 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
350 continue;
351
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100352 /* Ignore device nodes */
353 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
354 continue;
355
Lennart Poettering24f3a372012-06-20 09:05:50 +0200356 /* Keep files on this level around if this is
357 * requested */
358 if (keep_this_level)
359 continue;
360
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200361 age = MAX3(timespec_load(&s.st_mtim),
362 timespec_load(&s.st_atim),
363 timespec_load(&s.st_ctim));
364
365 if (age >= cutoff)
366 continue;
367
368 log_debug("unlink '%s'\n", sub_path);
369
370 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
371 if (errno != ENOENT) {
372 log_error("unlink(%s): %m", sub_path);
373 r = -errno;
374 }
375 }
376
377 deleted = true;
378 }
379 }
380
381finish:
382 if (deleted) {
383 /* Restore original directory timestamps */
384 times[0] = ds->st_atim;
385 times[1] = ds->st_mtim;
386
387 if (futimens(dirfd(d), times) < 0)
388 log_error("utimensat(%s): %m", p);
389 }
390
391 free(sub_path);
392
393 return r;
394}
395
396static int clean_item(Item *i) {
397 DIR *d;
398 struct stat s, ps;
399 bool mountpoint;
400 int r;
401 usec_t cutoff, n;
402
403 assert(i);
404
405 if (i->type != CREATE_DIRECTORY &&
406 i->type != TRUNCATE_DIRECTORY &&
407 i->type != IGNORE_PATH)
408 return 0;
409
410 if (!i->age_set || i->age <= 0)
411 return 0;
412
413 n = now(CLOCK_REALTIME);
414 if (n < i->age)
415 return 0;
416
417 cutoff = n - i->age;
418
419 d = opendir(i->path);
420 if (!d) {
421 if (errno == ENOENT)
422 return 0;
423
424 log_error("Failed to open directory %s: %m", i->path);
425 return -errno;
426 }
427
428 if (fstat(dirfd(d), &s) < 0) {
429 log_error("stat(%s) failed: %m", i->path);
430 r = -errno;
431 goto finish;
432 }
433
434 if (!S_ISDIR(s.st_mode)) {
435 log_error("%s is not a directory.", i->path);
436 r = -ENOTDIR;
437 goto finish;
438 }
439
440 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
441 log_error("stat(%s/..) failed: %m", i->path);
442 r = -errno;
443 goto finish;
444 }
445
446 mountpoint = s.st_dev != ps.st_dev ||
447 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
448
Lennart Poettering24f3a372012-06-20 09:05:50 +0200449 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 +0200450
451finish:
452 if (d)
453 closedir(d);
454
455 return r;
456}
457
Michal Schmidt062e01b2011-12-16 18:00:11 +0100458static int item_set_perms(Item *i, const char *path) {
459 /* not using i->path directly because it may be a glob */
460 if (i->mode_set)
461 if (chmod(path, i->mode) < 0) {
462 log_error("chmod(%s) failed: %m", path);
463 return -errno;
464 }
465
466 if (i->uid_set || i->gid_set)
467 if (chown(path,
468 i->uid_set ? i->uid : (uid_t) -1,
469 i->gid_set ? i->gid : (gid_t) -1) < 0) {
470
471 log_error("chown(%s) failed: %m", path);
472 return -errno;
473 }
474
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200475 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100476}
477
478static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100479 DIR *d;
480 int ret = 0;
481
482 /* This returns the first error we run into, but nevertheless
483 * tries to go on */
484
485 d = opendir(path);
486 if (!d)
487 return errno == ENOENT ? 0 : -errno;
488
489 for (;;) {
490 struct dirent buf, *de;
491 bool is_dir;
492 int r;
493 char *entry_path;
494
495 r = readdir_r(d, &buf, &de);
496 if (r != 0) {
497 if (ret == 0)
498 ret = -r;
499 break;
500 }
501
502 if (!de)
503 break;
504
505 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
506 continue;
507
508 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
509 if (ret == 0)
510 ret = -ENOMEM;
511 continue;
512 }
513
514 if (de->d_type == DT_UNKNOWN) {
515 struct stat st;
516
517 if (lstat(entry_path, &st) < 0) {
518 if (ret == 0 && errno != ENOENT)
519 ret = -errno;
520 free(entry_path);
521 continue;
522 }
523
524 is_dir = S_ISDIR(st.st_mode);
525
526 } else
527 is_dir = de->d_type == DT_DIR;
528
Michal Schmidt062e01b2011-12-16 18:00:11 +0100529 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100530 if (r < 0) {
531 if (ret == 0 && r != -ENOENT)
532 ret = r;
533 free(entry_path);
534 continue;
535 }
536
537 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100538 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100539 if (r < 0 && ret == 0)
540 ret = r;
541 }
542
543 free(entry_path);
544 }
545
546 closedir(d);
547
548 return ret;
549}
550
551static int recursive_relabel(Item *i, const char *path) {
552 int r;
553 struct stat st;
554
Michal Schmidt062e01b2011-12-16 18:00:11 +0100555 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100556 if (r < 0)
557 return r;
558
559 if (lstat(path, &st) < 0)
560 return -errno;
561
562 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100563 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100564
565 return r;
566}
567
Michal Schmidt99e68c02011-12-15 23:45:26 +0100568static int glob_item(Item *i, int (*action)(Item *, const char *)) {
569 int r = 0, k;
570 glob_t g;
571 char **fn;
572
573 zero(g);
574
575 errno = 0;
576 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
577
578 if (k != GLOB_NOMATCH) {
579 if (errno != 0)
580 errno = EIO;
581
582 log_error("glob(%s) failed: %m", i->path);
583 return -errno;
584 }
585 }
586
587 STRV_FOREACH(fn, g.gl_pathv)
588 if ((k = action(i, *fn)) < 0)
589 r = k;
590
591 globfree(&g);
592 return r;
593}
594
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200595static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200596 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200597 mode_t u;
598 struct stat st;
599
600 assert(i);
601
602 switch (i->type) {
603
604 case IGNORE_PATH:
605 case REMOVE_PATH:
606 case RECURSIVE_REMOVE_PATH:
607 return 0;
608
609 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100610 case TRUNCATE_FILE:
611 case WRITE_FILE: {
612 int fd, flags;
613
614 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
615 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200616
617 u = umask(0);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200618 label_context_set(i->path, S_IFREG);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100619 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200620 e = errno;
621 label_context_clear();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200622 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200623 errno = e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200624
625 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100626 if (i->type == WRITE_FILE && errno == ENOENT)
627 break;
628
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200629 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100630 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200631 }
632
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100633 if (i->argument) {
634 ssize_t n;
635 size_t l;
636 struct iovec iovec[2];
637 static const char new_line = '\n';
638
639 l = strlen(i->argument);
640
641 zero(iovec);
642 iovec[0].iov_base = i->argument;
643 iovec[0].iov_len = l;
644
645 iovec[1].iov_base = (void*) &new_line;
646 iovec[1].iov_len = 1;
647
648 n = writev(fd, iovec, 2);
Lennart Poettering03ad1132012-05-15 14:34:33 +0200649
650 /* It's OK if we don't write the trailing
651 * newline, hence we check for l, instead of
652 * l+1 here. Files in /sys often refuse
653 * writing of the trailing newline. */
654 if (n < 0 || (size_t) n < l) {
655 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100656 close_nointr_nofail(fd);
657 return n < 0 ? n : -EIO;
658 }
659 }
660
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100661 close_nointr_nofail(fd);
662
663 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200664 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100665 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200666 }
667
668 if (!S_ISREG(st.st_mode)) {
669 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100670 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200671 }
672
Michal Schmidt062e01b2011-12-16 18:00:11 +0100673 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100674 if (r < 0)
675 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200676
677 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100678 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200679
680 case TRUNCATE_DIRECTORY:
681 case CREATE_DIRECTORY:
682
683 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200684 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200685 r = mkdir(i->path, i->mode);
686 umask(u);
687
688 if (r < 0 && errno != EEXIST) {
689 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100690 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200691 }
692
693 if (stat(i->path, &st) < 0) {
694 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100695 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200696 }
697
698 if (!S_ISDIR(st.st_mode)) {
699 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100700 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200701 }
702
Michal Schmidt062e01b2011-12-16 18:00:11 +0100703 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100704 if (r < 0)
705 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200706
707 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200708
709 case CREATE_FIFO:
710
711 u = umask(0);
712 r = mkfifo(i->path, i->mode);
713 umask(u);
714
715 if (r < 0 && errno != EEXIST) {
716 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100717 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200718 }
719
720 if (stat(i->path, &st) < 0) {
721 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100722 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200723 }
724
725 if (!S_ISFIFO(st.st_mode)) {
726 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100727 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200728 }
729
Michal Schmidt062e01b2011-12-16 18:00:11 +0100730 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100731 if (r < 0)
732 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200733
734 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100735
Lennart Poettering468d7262012-01-17 15:04:12 +0100736 case CREATE_SYMLINK: {
737 char *x;
738
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200739 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100740 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200741 e = errno;
742 label_context_clear();
743 errno = e;
744
Lennart Poettering468d7262012-01-17 15:04:12 +0100745 if (r < 0 && errno != EEXIST) {
746 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
747 return -errno;
748 }
749
750 r = readlink_malloc(i->path, &x);
751 if (r < 0) {
752 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
753 return -errno;
754 }
755
756 if (!streq(i->argument, x)) {
757 free(x);
758 log_error("%s is not the right symlinks.", i->path);
759 return -EEXIST;
760 }
761
762 free(x);
763 break;
764 }
765
766 case CREATE_BLOCK_DEVICE:
767 case CREATE_CHAR_DEVICE: {
Michal Schmidte7aee752012-06-14 16:01:19 +0200768 mode_t file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100769
770 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200771 label_context_set(i->path, file_type);
772 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200773 e = errno;
774 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100775 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200776 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100777
778 if (r < 0 && errno != EEXIST) {
779 log_error("Failed to create device node %s: %m", i->path);
780 return -errno;
781 }
782
783 if (stat(i->path, &st) < 0) {
784 log_error("stat(%s) failed: %m", i->path);
785 return -errno;
786 }
787
Michal Schmidte7aee752012-06-14 16:01:19 +0200788 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100789 log_error("%s is not a device node.", i->path);
790 return -EEXIST;
791 }
792
793 r = item_set_perms(i, i->path);
794 if (r < 0)
795 return r;
796
797 break;
798 }
799
Michal Schmidt777b87e2011-12-16 18:27:35 +0100800 case RELABEL_PATH:
801
802 r = glob_item(i, item_set_perms);
803 if (r < 0)
804 return 0;
805 break;
806
Michal Schmidta8d88782011-12-15 23:11:07 +0100807 case RECURSIVE_RELABEL_PATH:
808
809 r = glob_item(i, recursive_relabel);
810 if (r < 0)
811 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200812 }
813
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200814 log_debug("%s created successfully.", i->path);
815
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100816 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200817}
818
Michal Schmidta0896122011-12-15 21:32:50 +0100819static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200820 int r;
821
822 assert(i);
823
824 switch (i->type) {
825
826 case CREATE_FILE:
827 case TRUNCATE_FILE:
828 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200829 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100830 case CREATE_SYMLINK:
831 case CREATE_BLOCK_DEVICE:
832 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200833 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100834 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100835 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100836 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200837 break;
838
839 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100840 if (remove(instance) < 0 && errno != ENOENT) {
841 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200842 return -errno;
843 }
844
845 break;
846
847 case TRUNCATE_DIRECTORY:
848 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200849 /* FIXME: we probably should use dir_cleanup() here
850 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200851 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100852 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100853 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200854 return r;
855 }
856
857 break;
858 }
859
860 return 0;
861}
862
Michal Schmidta0896122011-12-15 21:32:50 +0100863static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100864 int r = 0;
865
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100866 assert(i);
867
868 switch (i->type) {
869
870 case CREATE_FILE:
871 case TRUNCATE_FILE:
872 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200873 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100874 case CREATE_SYMLINK:
875 case CREATE_CHAR_DEVICE:
876 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100877 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100878 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100879 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100880 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100881 break;
882
883 case REMOVE_PATH:
884 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100885 case RECURSIVE_REMOVE_PATH:
886 r = glob_item(i, remove_item_instance);
887 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100888 }
889
Michal Schmidt99e68c02011-12-15 23:45:26 +0100890 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100891}
892
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200893static int process_item(Item *i) {
894 int r, q, p;
895
896 assert(i);
897
898 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100899 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200900 p = arg_clean ? clean_item(i) : 0;
901
902 if (r < 0)
903 return r;
904
905 if (q < 0)
906 return q;
907
908 return p;
909}
910
911static void item_free(Item *i) {
912 assert(i);
913
914 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100915 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200916 free(i);
917}
918
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200919static bool item_equal(Item *a, Item *b) {
920 assert(a);
921 assert(b);
922
923 if (!streq_ptr(a->path, b->path))
924 return false;
925
926 if (a->type != b->type)
927 return false;
928
929 if (a->uid_set != b->uid_set ||
930 (a->uid_set && a->uid != b->uid))
931 return false;
932
933 if (a->gid_set != b->gid_set ||
934 (a->gid_set && a->gid != b->gid))
935 return false;
936
937 if (a->mode_set != b->mode_set ||
938 (a->mode_set && a->mode != b->mode))
939 return false;
940
941 if (a->age_set != b->age_set ||
942 (a->age_set && a->age != b->age))
943 return false;
944
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100945 if ((a->type == CREATE_FILE ||
946 a->type == TRUNCATE_FILE ||
947 a->type == WRITE_FILE ||
948 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100949 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100950 return false;
951
952 if ((a->type == CREATE_CHAR_DEVICE ||
953 a->type == CREATE_BLOCK_DEVICE) &&
954 a->major_minor != b->major_minor)
955 return false;
956
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200957 return true;
958}
959
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100960static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200961 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200962 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100963 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200964 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100965 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200966
967 assert(fname);
968 assert(line >= 1);
969 assert(buffer);
970
Lennart Poettering468d7262012-01-17 15:04:12 +0100971 i = new0(Item, 1);
972 if (!i) {
Shawn Landden669241a2012-07-24 21:12:43 -0700973 log_error("Out of memory.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200974 return -ENOMEM;
975 }
976
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100977 if (sscanf(buffer,
978 "%c "
979 "%ms "
980 "%ms "
981 "%ms "
982 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100983 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100984 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100985 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100986 &i->path,
987 &mode,
988 &user,
989 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +0100990 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100991 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200992 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200993 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200994 goto finish;
995 }
996
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100997 if (n >= 0) {
998 n += strspn(buffer+n, WHITESPACE);
999 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1000 i->argument = unquote(buffer+n, "\"");
1001 if (!i->argument) {
Shawn Landden669241a2012-07-24 21:12:43 -07001002 log_error("Out of memory.");
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001003 return -ENOMEM;
1004 }
1005 }
1006 }
1007
Michal Schmidt777b87e2011-12-16 18:27:35 +01001008 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001009
Michal Schmidt777b87e2011-12-16 18:27:35 +01001010 case CREATE_FILE:
1011 case TRUNCATE_FILE:
1012 case CREATE_DIRECTORY:
1013 case TRUNCATE_DIRECTORY:
1014 case CREATE_FIFO:
1015 case IGNORE_PATH:
1016 case REMOVE_PATH:
1017 case RECURSIVE_REMOVE_PATH:
1018 case RELABEL_PATH:
1019 case RECURSIVE_RELABEL_PATH:
1020 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001021
1022 case CREATE_SYMLINK:
1023 if (!i->argument) {
1024 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1025 r = -EBADMSG;
1026 goto finish;
1027 }
1028 break;
1029
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001030 case WRITE_FILE:
1031 if (!i->argument) {
1032 log_error("[%s:%u] Write file requires argument.", fname, line);
1033 r = -EBADMSG;
1034 goto finish;
1035 }
1036 break;
1037
Lennart Poettering468d7262012-01-17 15:04:12 +01001038 case CREATE_CHAR_DEVICE:
1039 case CREATE_BLOCK_DEVICE: {
1040 unsigned major, minor;
1041
1042 if (!i->argument) {
1043 log_error("[%s:%u] Device file requires argument.", fname, line);
1044 r = -EBADMSG;
1045 goto finish;
1046 }
1047
1048 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1049 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1050 r = -EBADMSG;
1051 goto finish;
1052 }
1053
1054 i->major_minor = makedev(major, minor);
1055 break;
1056 }
1057
Michal Schmidt777b87e2011-12-16 18:27:35 +01001058 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001059 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001060 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001061 goto finish;
1062 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001063
Michal Schmidta8d88782011-12-15 23:11:07 +01001064 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001065
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001066 if (!path_is_absolute(i->path)) {
1067 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1068 r = -EBADMSG;
1069 goto finish;
1070 }
1071
1072 path_kill_slashes(i->path);
1073
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001074 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001075 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001076 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001077 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001078
1079 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001080 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001081
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001082 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001083 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001084 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1085 goto finish;
1086 }
1087
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001088 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001089 }
1090
1091 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001092 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001093
Lennart Poettering4b678342011-07-23 01:17:59 +02001094 r = get_group_creds(&g, &i->gid);
1095 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001096 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1097 goto finish;
1098 }
1099
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001100 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001101 }
1102
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001103 if (mode && !streq(mode, "-")) {
1104 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001105
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001106 if (sscanf(mode, "%o", &m) != 1) {
1107 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1108 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001109 goto finish;
1110 }
1111
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001112 i->mode = m;
1113 i->mode_set = true;
1114 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001115 i->mode =
1116 i->type == CREATE_DIRECTORY ||
1117 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001118
1119 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001120 const char *a = age;
1121
1122 if (*a == '~') {
1123 i->keep_first_level = true;
1124 a++;
1125 }
1126
1127 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001128 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1129 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001130 goto finish;
1131 }
1132
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001133 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001134 }
1135
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001136 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001137
Lennart Poettering468d7262012-01-17 15:04:12 +01001138 existing = hashmap_get(h, i->path);
1139 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001140
1141 /* Two identical items are fine */
1142 if (!item_equal(existing, i))
1143 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1144
1145 r = 0;
1146 goto finish;
1147 }
1148
Lennart Poettering468d7262012-01-17 15:04:12 +01001149 r = hashmap_put(h, i->path, i);
1150 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001151 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001152 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001153 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001154
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001155 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001156 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001157
1158finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001159 free(user);
1160 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001161 free(mode);
1162 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001163
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001164 if (i)
1165 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001166
1167 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001168}
1169
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001170static int help(void) {
1171
Lennart Poettering522d4a42011-02-13 15:08:15 +01001172 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1173 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001174 " -h --help Show this help\n"
1175 " --create Create marked files/directories\n"
1176 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001177 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001178 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001179 program_invocation_short_name);
1180
1181 return 0;
1182}
1183
1184static int parse_argv(int argc, char *argv[]) {
1185
1186 enum {
1187 ARG_CREATE,
1188 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001189 ARG_REMOVE,
1190 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001191 };
1192
1193 static const struct option options[] = {
1194 { "help", no_argument, NULL, 'h' },
1195 { "create", no_argument, NULL, ARG_CREATE },
1196 { "clean", no_argument, NULL, ARG_CLEAN },
1197 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001198 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001199 { NULL, 0, NULL, 0 }
1200 };
1201
1202 int c;
1203
1204 assert(argc >= 0);
1205 assert(argv);
1206
1207 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1208
1209 switch (c) {
1210
1211 case 'h':
1212 help();
1213 return 0;
1214
1215 case ARG_CREATE:
1216 arg_create = true;
1217 break;
1218
1219 case ARG_CLEAN:
1220 arg_clean = true;
1221 break;
1222
1223 case ARG_REMOVE:
1224 arg_remove = true;
1225 break;
1226
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001227 case ARG_PREFIX:
1228 arg_prefix = optarg;
1229 break;
1230
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001231 case '?':
1232 return -EINVAL;
1233
1234 default:
1235 log_error("Unknown option code %c", c);
1236 return -EINVAL;
1237 }
1238 }
1239
1240 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001241 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001242 return -EINVAL;
1243 }
1244
1245 return 1;
1246}
1247
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001248static int read_config_file(const char *fn, bool ignore_enoent) {
1249 FILE *f;
1250 unsigned v = 0;
1251 int r = 0;
1252
1253 assert(fn);
1254
Lennart Poettering468d7262012-01-17 15:04:12 +01001255 f = fopen(fn, "re");
1256 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001257
1258 if (ignore_enoent && errno == ENOENT)
1259 return 0;
1260
1261 log_error("Failed to open %s: %m", fn);
1262 return -errno;
1263 }
1264
Kay Sievers772f8372011-04-25 21:38:21 +02001265 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001266 for (;;) {
1267 char line[LINE_MAX], *l;
1268 int k;
1269
1270 if (!(fgets(line, sizeof(line), f)))
1271 break;
1272
1273 v++;
1274
1275 l = strstrip(line);
1276 if (*l == '#' || *l == 0)
1277 continue;
1278
1279 if ((k = parse_line(fn, v, l)) < 0)
1280 if (r == 0)
1281 r = k;
1282 }
1283
1284 if (ferror(f)) {
1285 log_error("Failed to read from file %s: %m", fn);
1286 if (r == 0)
1287 r = -EIO;
1288 }
1289
1290 fclose(f);
1291
1292 return r;
1293}
1294
Dave Reisner91256702012-06-08 22:31:19 -04001295static char *resolve_fragment(const char *fragment, const char **search_paths) {
1296 const char **p;
1297 char *resolved_path;
1298
1299 if (is_path(fragment))
1300 return strdup(fragment);
1301
1302 STRV_FOREACH(p, search_paths) {
Lennart Poetteringb7def682012-07-13 13:41:01 +02001303 resolved_path = strjoin(*p, "/", fragment, NULL);
Dave Reisner91256702012-06-08 22:31:19 -04001304 if (resolved_path == NULL) {
Shawn Landden669241a2012-07-24 21:12:43 -07001305 log_error("Out of memory.");
Dave Reisner91256702012-06-08 22:31:19 -04001306 return NULL;
1307 }
1308
1309 if (access(resolved_path, F_OK) == 0)
1310 return resolved_path;
1311
1312 free(resolved_path);
1313 }
1314
Kay Sieversca2e8942012-06-10 19:21:50 +02001315 errno = ENOENT;
Dave Reisner91256702012-06-08 22:31:19 -04001316 return NULL;
1317}
1318
Lennart Poettering5008d582010-09-28 02:34:02 +02001319int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001320 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001321 Item *i;
1322 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001323
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001324 r = parse_argv(argc, argv);
1325 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001326 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1327
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001328 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001329 log_parse_environment();
1330 log_open();
1331
Lennart Poettering4c126262011-08-01 20:52:18 +02001332 umask(0022);
1333
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001334 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001335
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001336 items = hashmap_new(string_hash_func, string_compare_func);
1337 globs = hashmap_new(string_hash_func, string_compare_func);
1338
1339 if (!items || !globs) {
Shawn Landden669241a2012-07-24 21:12:43 -07001340 log_error("Out of memory.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001341 r = EXIT_FAILURE;
1342 goto finish;
1343 }
1344
Lennart Poettering5008d582010-09-28 02:34:02 +02001345 r = EXIT_SUCCESS;
1346
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001347 if (optind < argc) {
1348 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001349
Dave Reisner91256702012-06-08 22:31:19 -04001350 for (j = optind; j < argc; j++) {
Kay Sieversca2e8942012-06-10 19:21:50 +02001351 char *fragment;
1352
Lennart Poettering24f3a372012-06-20 09:05:50 +02001353 fragment = resolve_fragment(argv[j], (const char**) conf_file_dirs);
Kay Sieversca2e8942012-06-10 19:21:50 +02001354 if (!fragment) {
Kay Sievers94f7a712012-06-10 19:31:39 +02001355 log_error("Failed to find a %s file: %m", argv[j]);
Kay Sieversca2e8942012-06-10 19:21:50 +02001356 r = EXIT_FAILURE;
1357 goto finish;
1358 }
Dave Reisner91256702012-06-08 22:31:19 -04001359 if (read_config_file(fragment, false) < 0)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001360 r = EXIT_FAILURE;
Dave Reisner91256702012-06-08 22:31:19 -04001361 free(fragment);
1362 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001363
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001364 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001365 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001366
Dave Reisner91256702012-06-08 22:31:19 -04001367 r = conf_files_list_strv(&files, ".conf",
Lennart Poettering24f3a372012-06-20 09:05:50 +02001368 (const char **) conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001369 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001370 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001371 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001372 goto finish;
1373 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001374
Kay Sievers772f8372011-04-25 21:38:21 +02001375 STRV_FOREACH(f, files) {
1376 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001377 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001378 }
1379
Kay Sievers772f8372011-04-25 21:38:21 +02001380 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001381 }
1382
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001383 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001384 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001385
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001386 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001387 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001388
Lennart Poettering5008d582010-09-28 02:34:02 +02001389finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001390 while ((i = hashmap_steal_first(items)))
1391 item_free(i);
1392
Lennart Poettering17b90522011-02-14 21:55:06 +01001393 while ((i = hashmap_steal_first(globs)))
1394 item_free(i);
1395
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001396 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001397 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001398
Lennart Poettering17b90522011-02-14 21:55:06 +01001399 set_free_free(unix_sockets);
1400
Lennart Poettering29003cf2010-10-19 19:36:45 +02001401 label_finish();
1402
Lennart Poettering5008d582010-09-28 02:34:02 +02001403 return r;
1404}