blob: 323781f9737ac547aac6ff5693f7626d94f6f6ff [file] [log] [blame]
Lennart Poettering5008d582010-09-28 02:34:02 +02001/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02006 Copyright 2010 Lennart Poettering, Kay Sievers
Lennart Poettering5008d582010-09-28 02:34:02 +02007
8 systemd is free software; you can redistribute it and/or modify it
Lennart Poettering5430f7f2012-04-12 00:20:58 +02009 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
Lennart Poettering5008d582010-09-28 02:34:02 +020011 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lennart Poettering5430f7f2012-04-12 00:20:58 +020016 Lesser General Public License for more details.
Lennart Poettering5008d582010-09-28 02:34:02 +020017
Lennart Poettering5430f7f2012-04-12 00:20:58 +020018 You should have received a copy of the GNU Lesser General Public License
Lennart Poettering5008d582010-09-28 02:34:02 +020019 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <limits.h>
28#include <dirent.h>
29#include <grp.h>
30#include <pwd.h>
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020031#include <stdio.h>
32#include <stdlib.h>
33#include <stddef.h>
34#include <getopt.h>
35#include <stdbool.h>
36#include <time.h>
37#include <sys/types.h>
38#include <sys/param.h>
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010039#include <glob.h>
40#include <fnmatch.h>
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -070041#include <sys/capability.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020042
43#include "log.h"
44#include "util.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020045#include "mkdir.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020046#include "path-util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020047#include "strv.h"
48#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020049#include "set.h"
Kay Sievers2c210442012-05-07 18:55:45 +020050#include "conf-files.h"
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -070051#include "capability.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020052
Andreas Jaeger01000472010-09-29 10:08:24 +020053/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020054 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020055 * properly owned directories beneath /tmp, /var/tmp, /run, which are
56 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020057
Michal Schmidt66ccd032011-12-15 21:31:14 +010058typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010059 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020060 CREATE_FILE = 'f',
61 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010062 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020063 CREATE_DIRECTORY = 'd',
64 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020065 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010066 CREATE_SYMLINK = 'L',
67 CREATE_CHAR_DEVICE = 'c',
68 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010069
70 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020071 IGNORE_PATH = 'x',
72 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010073 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010074 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010075 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010076} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020077
78typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010079 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020080
81 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010082 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020083 uid_t uid;
84 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020085 mode_t mode;
86 usec_t age;
87
Lennart Poettering468d7262012-01-17 15:04:12 +010088 dev_t major_minor;
89
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020090 bool uid_set:1;
91 bool gid_set:1;
92 bool mode_set:1;
93 bool age_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +020094
95 bool keep_first_level:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020096} Item;
97
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010098static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010099static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200100
101static bool arg_create = false;
102static bool arg_clean = false;
103static bool arg_remove = false;
104
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100105static const char *arg_prefix = NULL;
106
Lennart Poettering24f3a372012-06-20 09:05:50 +0200107static const char * const conf_file_dirs[] = {
Dave Reisner91256702012-06-08 22:31:19 -0400108 "/etc/tmpfiles.d",
109 "/run/tmpfiles.d",
110 "/usr/local/lib/tmpfiles.d",
111 "/usr/lib/tmpfiles.d",
Lennart Poettering3f2afb22012-07-20 16:24:55 +0200112#ifdef HAVE_SPLIT_USR
113 "/lib/tmpfiles.d",
114#endif
Dave Reisner91256702012-06-08 22:31:19 -0400115 NULL
116};
117
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200118#define MAX_DEPTH 256
119
Michal Schmidt66ccd032011-12-15 21:31:14 +0100120static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100121 return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RELABEL_PATH || t == RECURSIVE_RELABEL_PATH;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100122}
123
124static struct Item* find_glob(Hashmap *h, const char *match) {
125 Item *j;
126 Iterator i;
127
128 HASHMAP_FOREACH(j, h, i)
129 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
130 return j;
131
132 return NULL;
133}
134
Lennart Poettering17b90522011-02-14 21:55:06 +0100135static void load_unix_sockets(void) {
136 FILE *f = NULL;
137 char line[LINE_MAX];
138
139 if (unix_sockets)
140 return;
141
142 /* We maintain a cache of the sockets we found in
143 * /proc/net/unix to speed things up a little. */
144
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100145 unix_sockets = set_new(string_hash_func, string_compare_func);
146 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100147 return;
148
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100149 f = fopen("/proc/net/unix", "re");
150 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100151 return;
152
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100153 /* Skip header */
154 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100155 goto fail;
156
157 for (;;) {
158 char *p, *s;
159 int k;
160
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100161 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100162 break;
163
164 truncate_nl(line);
165
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100166 p = strchr(line, ':');
167 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100168 continue;
169
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 if (strlen(p) < 37)
171 continue;
172
173 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100174 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100175 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100176 p += strspn(p, WHITESPACE);
177
178 if (*p != '/')
179 continue;
180
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100181 s = strdup(p);
182 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100183 goto fail;
184
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100185 path_kill_slashes(s);
186
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100187 k = set_put(unix_sockets, s);
188 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100189 free(s);
190
191 if (k != -EEXIST)
192 goto fail;
193 }
194 }
195
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200196 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100197 return;
198
199fail:
200 set_free_free(unix_sockets);
201 unix_sockets = NULL;
202
203 if (f)
204 fclose(f);
205}
206
207static bool unix_socket_alive(const char *fn) {
208 assert(fn);
209
210 load_unix_sockets();
211
212 if (unix_sockets)
213 return !!set_get(unix_sockets, (char*) fn);
214
215 /* We don't know, so assume yes */
216 return true;
217}
218
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200219static int dir_cleanup(
220 const char *p,
221 DIR *d,
222 const struct stat *ds,
223 usec_t cutoff,
224 dev_t rootdev,
225 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200226 int maxdepth,
227 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200228{
229 struct dirent *dent;
230 struct timespec times[2];
231 bool deleted = false;
232 char *sub_path = NULL;
233 int r = 0;
234
235 while ((dent = readdir(d))) {
236 struct stat s;
237 usec_t age;
238
239 if (streq(dent->d_name, ".") ||
240 streq(dent->d_name, ".."))
241 continue;
242
243 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
244
245 if (errno != ENOENT) {
246 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
247 r = -errno;
248 }
249
250 continue;
251 }
252
253 /* Stay on the same filesystem */
254 if (s.st_dev != rootdev)
255 continue;
256
257 /* Do not delete read-only files owned by root */
258 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
259 continue;
260
261 free(sub_path);
262 sub_path = NULL;
263
264 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700265 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200266 goto finish;
267 }
268
269 /* Is there an item configured for this path? */
270 if (hashmap_get(items, sub_path))
271 continue;
272
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100273 if (find_glob(globs, sub_path))
274 continue;
275
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200276 if (S_ISDIR(s.st_mode)) {
277
278 if (mountpoint &&
279 streq(dent->d_name, "lost+found") &&
280 s.st_uid == 0)
281 continue;
282
283 if (maxdepth <= 0)
284 log_warning("Reached max depth on %s.", sub_path);
285 else {
286 DIR *sub_dir;
287 int q;
288
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200289 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200290 if (sub_dir == NULL) {
291 if (errno != ENOENT) {
292 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
293 r = -errno;
294 }
295
296 continue;
297 }
298
Lennart Poettering24f3a372012-06-20 09:05:50 +0200299 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200300 closedir(sub_dir);
301
302 if (q < 0)
303 r = q;
304 }
305
Lennart Poettering24f3a372012-06-20 09:05:50 +0200306 /* Note: if you are wondering why we don't
307 * support the sticky bit for excluding
308 * directories from cleaning like we do it for
309 * other file system objects: well, the sticky
310 * bit already has a meaning for directories,
311 * so we don't want to overload that. */
312
313 if (keep_this_level)
314 continue;
315
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200316 /* Ignore ctime, we change it when deleting */
317 age = MAX(timespec_load(&s.st_mtim),
318 timespec_load(&s.st_atim));
319 if (age >= cutoff)
320 continue;
321
322 log_debug("rmdir '%s'\n", sub_path);
323
324 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
325 if (errno != ENOENT && errno != ENOTEMPTY) {
326 log_error("rmdir(%s): %m", sub_path);
327 r = -errno;
328 }
329 }
330
331 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100332 /* Skip files for which the sticky bit is
333 * set. These are semantics we define, and are
334 * unknown elsewhere. See XDG_RUNTIME_DIR
335 * specification for details. */
336 if (s.st_mode & S_ISVTX)
337 continue;
338
Lennart Poettering17b90522011-02-14 21:55:06 +0100339 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200340 if (streq(dent->d_name, ".journal") &&
341 s.st_uid == 0)
342 continue;
343
344 if (streq(dent->d_name, "aquota.user") ||
345 streq(dent->d_name, "aquota.group"))
346 continue;
347 }
348
Lennart Poettering17b90522011-02-14 21:55:06 +0100349 /* Ignore sockets that are listed in /proc/net/unix */
350 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
351 continue;
352
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100353 /* Ignore device nodes */
354 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
355 continue;
356
Lennart Poettering24f3a372012-06-20 09:05:50 +0200357 /* Keep files on this level around if this is
358 * requested */
359 if (keep_this_level)
360 continue;
361
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200362 age = MAX3(timespec_load(&s.st_mtim),
363 timespec_load(&s.st_atim),
364 timespec_load(&s.st_ctim));
365
366 if (age >= cutoff)
367 continue;
368
369 log_debug("unlink '%s'\n", sub_path);
370
371 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
372 if (errno != ENOENT) {
373 log_error("unlink(%s): %m", sub_path);
374 r = -errno;
375 }
376 }
377
378 deleted = true;
379 }
380 }
381
382finish:
383 if (deleted) {
384 /* Restore original directory timestamps */
385 times[0] = ds->st_atim;
386 times[1] = ds->st_mtim;
387
388 if (futimens(dirfd(d), times) < 0)
389 log_error("utimensat(%s): %m", p);
390 }
391
392 free(sub_path);
393
394 return r;
395}
396
397static int clean_item(Item *i) {
398 DIR *d;
399 struct stat s, ps;
400 bool mountpoint;
401 int r;
402 usec_t cutoff, n;
403
404 assert(i);
405
406 if (i->type != CREATE_DIRECTORY &&
407 i->type != TRUNCATE_DIRECTORY &&
408 i->type != IGNORE_PATH)
409 return 0;
410
411 if (!i->age_set || i->age <= 0)
412 return 0;
413
414 n = now(CLOCK_REALTIME);
415 if (n < i->age)
416 return 0;
417
418 cutoff = n - i->age;
419
420 d = opendir(i->path);
421 if (!d) {
422 if (errno == ENOENT)
423 return 0;
424
425 log_error("Failed to open directory %s: %m", i->path);
426 return -errno;
427 }
428
429 if (fstat(dirfd(d), &s) < 0) {
430 log_error("stat(%s) failed: %m", i->path);
431 r = -errno;
432 goto finish;
433 }
434
435 if (!S_ISDIR(s.st_mode)) {
436 log_error("%s is not a directory.", i->path);
437 r = -ENOTDIR;
438 goto finish;
439 }
440
441 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
442 log_error("stat(%s/..) failed: %m", i->path);
443 r = -errno;
444 goto finish;
445 }
446
447 mountpoint = s.st_dev != ps.st_dev ||
448 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
449
Lennart Poettering24f3a372012-06-20 09:05:50 +0200450 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH, i->keep_first_level);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200451
452finish:
453 if (d)
454 closedir(d);
455
456 return r;
457}
458
Michal Schmidt062e01b2011-12-16 18:00:11 +0100459static int item_set_perms(Item *i, const char *path) {
460 /* not using i->path directly because it may be a glob */
461 if (i->mode_set)
462 if (chmod(path, i->mode) < 0) {
463 log_error("chmod(%s) failed: %m", path);
464 return -errno;
465 }
466
467 if (i->uid_set || i->gid_set)
468 if (chown(path,
469 i->uid_set ? i->uid : (uid_t) -1,
470 i->gid_set ? i->gid : (gid_t) -1) < 0) {
471
472 log_error("chown(%s) failed: %m", path);
473 return -errno;
474 }
475
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200476 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100477}
478
479static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100480 DIR *d;
481 int ret = 0;
482
483 /* This returns the first error we run into, but nevertheless
484 * tries to go on */
485
486 d = opendir(path);
487 if (!d)
488 return errno == ENOENT ? 0 : -errno;
489
490 for (;;) {
491 struct dirent buf, *de;
492 bool is_dir;
493 int r;
494 char *entry_path;
495
496 r = readdir_r(d, &buf, &de);
497 if (r != 0) {
498 if (ret == 0)
499 ret = -r;
500 break;
501 }
502
503 if (!de)
504 break;
505
506 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
507 continue;
508
509 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
510 if (ret == 0)
511 ret = -ENOMEM;
512 continue;
513 }
514
515 if (de->d_type == DT_UNKNOWN) {
516 struct stat st;
517
518 if (lstat(entry_path, &st) < 0) {
519 if (ret == 0 && errno != ENOENT)
520 ret = -errno;
521 free(entry_path);
522 continue;
523 }
524
525 is_dir = S_ISDIR(st.st_mode);
526
527 } else
528 is_dir = de->d_type == DT_DIR;
529
Michal Schmidt062e01b2011-12-16 18:00:11 +0100530 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100531 if (r < 0) {
532 if (ret == 0 && r != -ENOENT)
533 ret = r;
534 free(entry_path);
535 continue;
536 }
537
538 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100539 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100540 if (r < 0 && ret == 0)
541 ret = r;
542 }
543
544 free(entry_path);
545 }
546
547 closedir(d);
548
549 return ret;
550}
551
552static int recursive_relabel(Item *i, const char *path) {
553 int r;
554 struct stat st;
555
Michal Schmidt062e01b2011-12-16 18:00:11 +0100556 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100557 if (r < 0)
558 return r;
559
560 if (lstat(path, &st) < 0)
561 return -errno;
562
563 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100564 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100565
566 return r;
567}
568
Michal Schmidt99e68c02011-12-15 23:45:26 +0100569static int glob_item(Item *i, int (*action)(Item *, const char *)) {
570 int r = 0, k;
571 glob_t g;
572 char **fn;
573
574 zero(g);
575
576 errno = 0;
577 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
578
579 if (k != GLOB_NOMATCH) {
580 if (errno != 0)
581 errno = EIO;
582
583 log_error("glob(%s) failed: %m", i->path);
584 return -errno;
585 }
586 }
587
588 STRV_FOREACH(fn, g.gl_pathv)
589 if ((k = action(i, *fn)) < 0)
590 r = k;
591
592 globfree(&g);
593 return r;
594}
595
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200596static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200597 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200598 mode_t u;
599 struct stat st;
600
601 assert(i);
602
603 switch (i->type) {
604
605 case IGNORE_PATH:
606 case REMOVE_PATH:
607 case RECURSIVE_REMOVE_PATH:
608 return 0;
609
610 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100611 case TRUNCATE_FILE:
612 case WRITE_FILE: {
613 int fd, flags;
614
615 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
616 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200617
618 u = umask(0);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200619 label_context_set(i->path, S_IFREG);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100620 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200621 e = errno;
622 label_context_clear();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200623 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200624 errno = e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200625
626 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100627 if (i->type == WRITE_FILE && errno == ENOENT)
628 break;
629
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200630 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100631 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200632 }
633
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100634 if (i->argument) {
635 ssize_t n;
636 size_t l;
637 struct iovec iovec[2];
638 static const char new_line = '\n';
639
640 l = strlen(i->argument);
641
642 zero(iovec);
643 iovec[0].iov_base = i->argument;
644 iovec[0].iov_len = l;
645
646 iovec[1].iov_base = (void*) &new_line;
647 iovec[1].iov_len = 1;
648
649 n = writev(fd, iovec, 2);
Lennart Poettering03ad1132012-05-15 14:34:33 +0200650
651 /* It's OK if we don't write the trailing
652 * newline, hence we check for l, instead of
653 * l+1 here. Files in /sys often refuse
654 * writing of the trailing newline. */
655 if (n < 0 || (size_t) n < l) {
656 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100657 close_nointr_nofail(fd);
658 return n < 0 ? n : -EIO;
659 }
660 }
661
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100662 close_nointr_nofail(fd);
663
664 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200665 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100666 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200667 }
668
669 if (!S_ISREG(st.st_mode)) {
670 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100671 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200672 }
673
Michal Schmidt062e01b2011-12-16 18:00:11 +0100674 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100675 if (r < 0)
676 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200677
678 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100679 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200680
681 case TRUNCATE_DIRECTORY:
682 case CREATE_DIRECTORY:
683
684 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200685 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200686 r = mkdir(i->path, i->mode);
687 umask(u);
688
689 if (r < 0 && errno != EEXIST) {
690 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100691 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200692 }
693
694 if (stat(i->path, &st) < 0) {
695 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100696 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200697 }
698
699 if (!S_ISDIR(st.st_mode)) {
700 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100701 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200702 }
703
Michal Schmidt062e01b2011-12-16 18:00:11 +0100704 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100705 if (r < 0)
706 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200707
708 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200709
710 case CREATE_FIFO:
711
712 u = umask(0);
713 r = mkfifo(i->path, i->mode);
714 umask(u);
715
716 if (r < 0 && errno != EEXIST) {
717 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100718 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200719 }
720
721 if (stat(i->path, &st) < 0) {
722 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100723 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200724 }
725
726 if (!S_ISFIFO(st.st_mode)) {
727 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100728 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200729 }
730
Michal Schmidt062e01b2011-12-16 18:00:11 +0100731 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100732 if (r < 0)
733 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200734
735 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100736
Lennart Poettering468d7262012-01-17 15:04:12 +0100737 case CREATE_SYMLINK: {
738 char *x;
739
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200740 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100741 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200742 e = errno;
743 label_context_clear();
744 errno = e;
745
Lennart Poettering468d7262012-01-17 15:04:12 +0100746 if (r < 0 && errno != EEXIST) {
747 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
748 return -errno;
749 }
750
751 r = readlink_malloc(i->path, &x);
752 if (r < 0) {
753 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
754 return -errno;
755 }
756
757 if (!streq(i->argument, x)) {
758 free(x);
759 log_error("%s is not the right symlinks.", i->path);
760 return -EEXIST;
761 }
762
763 free(x);
764 break;
765 }
766
767 case CREATE_BLOCK_DEVICE:
768 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -0700769 mode_t file_type;
770
771 if (have_effective_cap(CAP_MKNOD) == 0) {
772 /* In a container we lack CAP_MKNOD. We
773 shouldnt attempt to create the device node in
774 that case to avoid noise, and we don't support
775 virtualized devices in containers anyway. */
776
777 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
778 return 0;
779 }
780
781 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100782
783 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200784 label_context_set(i->path, file_type);
785 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200786 e = errno;
787 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100788 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200789 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100790
791 if (r < 0 && errno != EEXIST) {
792 log_error("Failed to create device node %s: %m", i->path);
793 return -errno;
794 }
795
796 if (stat(i->path, &st) < 0) {
797 log_error("stat(%s) failed: %m", i->path);
798 return -errno;
799 }
800
Michal Schmidte7aee752012-06-14 16:01:19 +0200801 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100802 log_error("%s is not a device node.", i->path);
803 return -EEXIST;
804 }
805
806 r = item_set_perms(i, i->path);
807 if (r < 0)
808 return r;
809
810 break;
811 }
812
Michal Schmidt777b87e2011-12-16 18:27:35 +0100813 case RELABEL_PATH:
814
815 r = glob_item(i, item_set_perms);
816 if (r < 0)
817 return 0;
818 break;
819
Michal Schmidta8d88782011-12-15 23:11:07 +0100820 case RECURSIVE_RELABEL_PATH:
821
822 r = glob_item(i, recursive_relabel);
823 if (r < 0)
824 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200825 }
826
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200827 log_debug("%s created successfully.", i->path);
828
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100829 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200830}
831
Michal Schmidta0896122011-12-15 21:32:50 +0100832static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200833 int r;
834
835 assert(i);
836
837 switch (i->type) {
838
839 case CREATE_FILE:
840 case TRUNCATE_FILE:
841 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200842 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100843 case CREATE_SYMLINK:
844 case CREATE_BLOCK_DEVICE:
845 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200846 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100847 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100848 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100849 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200850 break;
851
852 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100853 if (remove(instance) < 0 && errno != ENOENT) {
854 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200855 return -errno;
856 }
857
858 break;
859
860 case TRUNCATE_DIRECTORY:
861 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200862 /* FIXME: we probably should use dir_cleanup() here
863 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200864 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100865 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100866 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200867 return r;
868 }
869
870 break;
871 }
872
873 return 0;
874}
875
Michal Schmidta0896122011-12-15 21:32:50 +0100876static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100877 int r = 0;
878
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100879 assert(i);
880
881 switch (i->type) {
882
883 case CREATE_FILE:
884 case TRUNCATE_FILE:
885 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200886 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100887 case CREATE_SYMLINK:
888 case CREATE_CHAR_DEVICE:
889 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100890 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100891 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100892 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100893 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100894 break;
895
896 case REMOVE_PATH:
897 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100898 case RECURSIVE_REMOVE_PATH:
899 r = glob_item(i, remove_item_instance);
900 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100901 }
902
Michal Schmidt99e68c02011-12-15 23:45:26 +0100903 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100904}
905
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200906static int process_item(Item *i) {
907 int r, q, p;
908
909 assert(i);
910
911 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100912 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200913 p = arg_clean ? clean_item(i) : 0;
914
915 if (r < 0)
916 return r;
917
918 if (q < 0)
919 return q;
920
921 return p;
922}
923
924static void item_free(Item *i) {
925 assert(i);
926
927 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100928 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200929 free(i);
930}
931
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200932static bool item_equal(Item *a, Item *b) {
933 assert(a);
934 assert(b);
935
936 if (!streq_ptr(a->path, b->path))
937 return false;
938
939 if (a->type != b->type)
940 return false;
941
942 if (a->uid_set != b->uid_set ||
943 (a->uid_set && a->uid != b->uid))
944 return false;
945
946 if (a->gid_set != b->gid_set ||
947 (a->gid_set && a->gid != b->gid))
948 return false;
949
950 if (a->mode_set != b->mode_set ||
951 (a->mode_set && a->mode != b->mode))
952 return false;
953
954 if (a->age_set != b->age_set ||
955 (a->age_set && a->age != b->age))
956 return false;
957
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100958 if ((a->type == CREATE_FILE ||
959 a->type == TRUNCATE_FILE ||
960 a->type == WRITE_FILE ||
961 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100962 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100963 return false;
964
965 if ((a->type == CREATE_CHAR_DEVICE ||
966 a->type == CREATE_BLOCK_DEVICE) &&
967 a->major_minor != b->major_minor)
968 return false;
969
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200970 return true;
971}
972
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100973static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200974 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200975 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100976 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200977 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100978 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200979
980 assert(fname);
981 assert(line >= 1);
982 assert(buffer);
983
Lennart Poettering468d7262012-01-17 15:04:12 +0100984 i = new0(Item, 1);
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700985 if (!i)
986 return log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200987
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100988 if (sscanf(buffer,
989 "%c "
990 "%ms "
991 "%ms "
992 "%ms "
993 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100994 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100995 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100996 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100997 &i->path,
998 &mode,
999 &user,
1000 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +01001001 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001002 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001003 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001004 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001005 goto finish;
1006 }
1007
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001008 if (n >= 0) {
1009 n += strspn(buffer+n, WHITESPACE);
1010 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1011 i->argument = unquote(buffer+n, "\"");
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001012 if (!i->argument)
1013 return log_oom();
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001014 }
1015 }
1016
Michal Schmidt777b87e2011-12-16 18:27:35 +01001017 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001018
Michal Schmidt777b87e2011-12-16 18:27:35 +01001019 case CREATE_FILE:
1020 case TRUNCATE_FILE:
1021 case CREATE_DIRECTORY:
1022 case TRUNCATE_DIRECTORY:
1023 case CREATE_FIFO:
1024 case IGNORE_PATH:
1025 case REMOVE_PATH:
1026 case RECURSIVE_REMOVE_PATH:
1027 case RELABEL_PATH:
1028 case RECURSIVE_RELABEL_PATH:
1029 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001030
1031 case CREATE_SYMLINK:
1032 if (!i->argument) {
1033 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1034 r = -EBADMSG;
1035 goto finish;
1036 }
1037 break;
1038
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001039 case WRITE_FILE:
1040 if (!i->argument) {
1041 log_error("[%s:%u] Write file requires argument.", fname, line);
1042 r = -EBADMSG;
1043 goto finish;
1044 }
1045 break;
1046
Lennart Poettering468d7262012-01-17 15:04:12 +01001047 case CREATE_CHAR_DEVICE:
1048 case CREATE_BLOCK_DEVICE: {
1049 unsigned major, minor;
1050
1051 if (!i->argument) {
1052 log_error("[%s:%u] Device file requires argument.", fname, line);
1053 r = -EBADMSG;
1054 goto finish;
1055 }
1056
1057 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1058 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1059 r = -EBADMSG;
1060 goto finish;
1061 }
1062
1063 i->major_minor = makedev(major, minor);
1064 break;
1065 }
1066
Michal Schmidt777b87e2011-12-16 18:27:35 +01001067 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001068 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001069 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001070 goto finish;
1071 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001072
Michal Schmidta8d88782011-12-15 23:11:07 +01001073 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001074
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001075 if (!path_is_absolute(i->path)) {
1076 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1077 r = -EBADMSG;
1078 goto finish;
1079 }
1080
1081 path_kill_slashes(i->path);
1082
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001083 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001084 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001085 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001086 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001087
1088 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001089 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001090
Lennart Poetteringd05c5032012-07-16 12:34:54 +02001091 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001092 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001093 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1094 goto finish;
1095 }
1096
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001097 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001098 }
1099
1100 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001101 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001102
Lennart Poettering4b678342011-07-23 01:17:59 +02001103 r = get_group_creds(&g, &i->gid);
1104 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001105 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1106 goto finish;
1107 }
1108
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001109 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001110 }
1111
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001112 if (mode && !streq(mode, "-")) {
1113 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001114
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001115 if (sscanf(mode, "%o", &m) != 1) {
1116 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1117 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001118 goto finish;
1119 }
1120
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001121 i->mode = m;
1122 i->mode_set = true;
1123 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001124 i->mode =
1125 i->type == CREATE_DIRECTORY ||
1126 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001127
1128 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001129 const char *a = age;
1130
1131 if (*a == '~') {
1132 i->keep_first_level = true;
1133 a++;
1134 }
1135
1136 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001137 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1138 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001139 goto finish;
1140 }
1141
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001142 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001143 }
1144
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001145 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001146
Lennart Poettering468d7262012-01-17 15:04:12 +01001147 existing = hashmap_get(h, i->path);
1148 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001149
1150 /* Two identical items are fine */
1151 if (!item_equal(existing, i))
1152 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1153
1154 r = 0;
1155 goto finish;
1156 }
1157
Lennart Poettering468d7262012-01-17 15:04:12 +01001158 r = hashmap_put(h, i->path, i);
1159 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001160 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001161 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001162 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001163
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001164 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001165 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001166
1167finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001168 free(user);
1169 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001170 free(mode);
1171 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001172
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001173 if (i)
1174 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001175
1176 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001177}
1178
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001179static int help(void) {
1180
Lennart Poettering522d4a42011-02-13 15:08:15 +01001181 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1182 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001183 " -h --help Show this help\n"
1184 " --create Create marked files/directories\n"
1185 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001186 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001187 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001188 program_invocation_short_name);
1189
1190 return 0;
1191}
1192
1193static int parse_argv(int argc, char *argv[]) {
1194
1195 enum {
1196 ARG_CREATE,
1197 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001198 ARG_REMOVE,
1199 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001200 };
1201
1202 static const struct option options[] = {
1203 { "help", no_argument, NULL, 'h' },
1204 { "create", no_argument, NULL, ARG_CREATE },
1205 { "clean", no_argument, NULL, ARG_CLEAN },
1206 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001207 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001208 { NULL, 0, NULL, 0 }
1209 };
1210
1211 int c;
1212
1213 assert(argc >= 0);
1214 assert(argv);
1215
1216 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1217
1218 switch (c) {
1219
1220 case 'h':
1221 help();
1222 return 0;
1223
1224 case ARG_CREATE:
1225 arg_create = true;
1226 break;
1227
1228 case ARG_CLEAN:
1229 arg_clean = true;
1230 break;
1231
1232 case ARG_REMOVE:
1233 arg_remove = true;
1234 break;
1235
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001236 case ARG_PREFIX:
1237 arg_prefix = optarg;
1238 break;
1239
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001240 case '?':
1241 return -EINVAL;
1242
1243 default:
1244 log_error("Unknown option code %c", c);
1245 return -EINVAL;
1246 }
1247 }
1248
1249 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001250 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001251 return -EINVAL;
1252 }
1253
1254 return 1;
1255}
1256
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001257static int read_config_file(const char *fn, bool ignore_enoent) {
1258 FILE *f;
1259 unsigned v = 0;
1260 int r = 0;
1261
1262 assert(fn);
1263
Lennart Poettering468d7262012-01-17 15:04:12 +01001264 f = fopen(fn, "re");
1265 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001266
1267 if (ignore_enoent && errno == ENOENT)
1268 return 0;
1269
1270 log_error("Failed to open %s: %m", fn);
1271 return -errno;
1272 }
1273
Kay Sievers772f8372011-04-25 21:38:21 +02001274 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001275 for (;;) {
1276 char line[LINE_MAX], *l;
1277 int k;
1278
1279 if (!(fgets(line, sizeof(line), f)))
1280 break;
1281
1282 v++;
1283
1284 l = strstrip(line);
1285 if (*l == '#' || *l == 0)
1286 continue;
1287
1288 if ((k = parse_line(fn, v, l)) < 0)
1289 if (r == 0)
1290 r = k;
1291 }
1292
1293 if (ferror(f)) {
1294 log_error("Failed to read from file %s: %m", fn);
1295 if (r == 0)
1296 r = -EIO;
1297 }
1298
1299 fclose(f);
1300
1301 return r;
1302}
1303
Dave Reisner91256702012-06-08 22:31:19 -04001304static char *resolve_fragment(const char *fragment, const char **search_paths) {
1305 const char **p;
1306 char *resolved_path;
1307
1308 if (is_path(fragment))
1309 return strdup(fragment);
1310
1311 STRV_FOREACH(p, search_paths) {
Lennart Poetteringb7def682012-07-13 13:41:01 +02001312 resolved_path = strjoin(*p, "/", fragment, NULL);
Dave Reisner91256702012-06-08 22:31:19 -04001313 if (resolved_path == NULL) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001314 log_oom();
Dave Reisner91256702012-06-08 22:31:19 -04001315 return NULL;
1316 }
1317
1318 if (access(resolved_path, F_OK) == 0)
1319 return resolved_path;
1320
1321 free(resolved_path);
1322 }
1323
Kay Sieversca2e8942012-06-10 19:21:50 +02001324 errno = ENOENT;
Dave Reisner91256702012-06-08 22:31:19 -04001325 return NULL;
1326}
1327
Lennart Poettering5008d582010-09-28 02:34:02 +02001328int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001329 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001330 Item *i;
1331 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001332
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001333 r = parse_argv(argc, argv);
1334 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001335 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1336
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001337 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001338 log_parse_environment();
1339 log_open();
1340
Lennart Poettering4c126262011-08-01 20:52:18 +02001341 umask(0022);
1342
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001343 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001344
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001345 items = hashmap_new(string_hash_func, string_compare_func);
1346 globs = hashmap_new(string_hash_func, string_compare_func);
1347
1348 if (!items || !globs) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -07001349 log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001350 r = EXIT_FAILURE;
1351 goto finish;
1352 }
1353
Lennart Poettering5008d582010-09-28 02:34:02 +02001354 r = EXIT_SUCCESS;
1355
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001356 if (optind < argc) {
1357 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001358
Dave Reisner91256702012-06-08 22:31:19 -04001359 for (j = optind; j < argc; j++) {
Kay Sieversca2e8942012-06-10 19:21:50 +02001360 char *fragment;
1361
Lennart Poettering24f3a372012-06-20 09:05:50 +02001362 fragment = resolve_fragment(argv[j], (const char**) conf_file_dirs);
Kay Sieversca2e8942012-06-10 19:21:50 +02001363 if (!fragment) {
Kay Sievers94f7a712012-06-10 19:31:39 +02001364 log_error("Failed to find a %s file: %m", argv[j]);
Kay Sieversca2e8942012-06-10 19:21:50 +02001365 r = EXIT_FAILURE;
1366 goto finish;
1367 }
Dave Reisner91256702012-06-08 22:31:19 -04001368 if (read_config_file(fragment, false) < 0)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001369 r = EXIT_FAILURE;
Dave Reisner91256702012-06-08 22:31:19 -04001370 free(fragment);
1371 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001372
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001373 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001374 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001375
Dave Reisner91256702012-06-08 22:31:19 -04001376 r = conf_files_list_strv(&files, ".conf",
Lennart Poettering24f3a372012-06-20 09:05:50 +02001377 (const char **) conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001378 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001379 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001380 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001381 goto finish;
1382 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001383
Kay Sievers772f8372011-04-25 21:38:21 +02001384 STRV_FOREACH(f, files) {
1385 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001386 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001387 }
1388
Kay Sievers772f8372011-04-25 21:38:21 +02001389 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001390 }
1391
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001392 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001393 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001394
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001395 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001396 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001397
Lennart Poettering5008d582010-09-28 02:34:02 +02001398finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001399 while ((i = hashmap_steal_first(items)))
1400 item_free(i);
1401
Lennart Poettering17b90522011-02-14 21:55:06 +01001402 while ((i = hashmap_steal_first(globs)))
1403 item_free(i);
1404
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001405 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001406 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001407
Lennart Poettering17b90522011-02-14 21:55:06 +01001408 set_free_free(unix_sockets);
1409
Lennart Poettering29003cf2010-10-19 19:36:45 +02001410 label_finish();
1411
Lennart Poettering5008d582010-09-28 02:34:02 +02001412 return r;
1413}