blob: 2d5d90d265a9d24a111bdd6242da2398a31aef75 [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",
110 NULL
111};
112
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200113#define MAX_DEPTH 256
114
Michal Schmidt66ccd032011-12-15 21:31:14 +0100115static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100116 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 +0100117}
118
119static struct Item* find_glob(Hashmap *h, const char *match) {
120 Item *j;
121 Iterator i;
122
123 HASHMAP_FOREACH(j, h, i)
124 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
125 return j;
126
127 return NULL;
128}
129
Lennart Poettering17b90522011-02-14 21:55:06 +0100130static void load_unix_sockets(void) {
131 FILE *f = NULL;
132 char line[LINE_MAX];
133
134 if (unix_sockets)
135 return;
136
137 /* We maintain a cache of the sockets we found in
138 * /proc/net/unix to speed things up a little. */
139
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100140 unix_sockets = set_new(string_hash_func, string_compare_func);
141 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100142 return;
143
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100144 f = fopen("/proc/net/unix", "re");
145 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100146 return;
147
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100148 /* Skip header */
149 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100150 goto fail;
151
152 for (;;) {
153 char *p, *s;
154 int k;
155
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100156 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100157 break;
158
159 truncate_nl(line);
160
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100161 p = strchr(line, ':');
162 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100163 continue;
164
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100165 if (strlen(p) < 37)
166 continue;
167
168 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100169 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100171 p += strspn(p, WHITESPACE);
172
173 if (*p != '/')
174 continue;
175
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100176 s = strdup(p);
177 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100178 goto fail;
179
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100180 path_kill_slashes(s);
181
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100182 k = set_put(unix_sockets, s);
183 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100184 free(s);
185
186 if (k != -EEXIST)
187 goto fail;
188 }
189 }
190
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200191 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100192 return;
193
194fail:
195 set_free_free(unix_sockets);
196 unix_sockets = NULL;
197
198 if (f)
199 fclose(f);
200}
201
202static bool unix_socket_alive(const char *fn) {
203 assert(fn);
204
205 load_unix_sockets();
206
207 if (unix_sockets)
208 return !!set_get(unix_sockets, (char*) fn);
209
210 /* We don't know, so assume yes */
211 return true;
212}
213
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200214static int dir_cleanup(
215 const char *p,
216 DIR *d,
217 const struct stat *ds,
218 usec_t cutoff,
219 dev_t rootdev,
220 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200221 int maxdepth,
222 bool keep_this_level)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200223{
224 struct dirent *dent;
225 struct timespec times[2];
226 bool deleted = false;
227 char *sub_path = NULL;
228 int r = 0;
229
230 while ((dent = readdir(d))) {
231 struct stat s;
232 usec_t age;
233
234 if (streq(dent->d_name, ".") ||
235 streq(dent->d_name, ".."))
236 continue;
237
238 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
239
240 if (errno != ENOENT) {
241 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
242 r = -errno;
243 }
244
245 continue;
246 }
247
248 /* Stay on the same filesystem */
249 if (s.st_dev != rootdev)
250 continue;
251
252 /* Do not delete read-only files owned by root */
253 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
254 continue;
255
256 free(sub_path);
257 sub_path = NULL;
258
259 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
260 log_error("Out of memory");
261 r = -ENOMEM;
262 goto finish;
263 }
264
265 /* Is there an item configured for this path? */
266 if (hashmap_get(items, sub_path))
267 continue;
268
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100269 if (find_glob(globs, sub_path))
270 continue;
271
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200272 if (S_ISDIR(s.st_mode)) {
273
274 if (mountpoint &&
275 streq(dent->d_name, "lost+found") &&
276 s.st_uid == 0)
277 continue;
278
279 if (maxdepth <= 0)
280 log_warning("Reached max depth on %s.", sub_path);
281 else {
282 DIR *sub_dir;
283 int q;
284
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200285 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200286 if (sub_dir == NULL) {
287 if (errno != ENOENT) {
288 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
289 r = -errno;
290 }
291
292 continue;
293 }
294
Lennart Poettering24f3a372012-06-20 09:05:50 +0200295 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200296 closedir(sub_dir);
297
298 if (q < 0)
299 r = q;
300 }
301
Lennart Poettering24f3a372012-06-20 09:05:50 +0200302 /* Note: if you are wondering why we don't
303 * support the sticky bit for excluding
304 * directories from cleaning like we do it for
305 * other file system objects: well, the sticky
306 * bit already has a meaning for directories,
307 * so we don't want to overload that. */
308
309 if (keep_this_level)
310 continue;
311
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200312 /* Ignore ctime, we change it when deleting */
313 age = MAX(timespec_load(&s.st_mtim),
314 timespec_load(&s.st_atim));
315 if (age >= cutoff)
316 continue;
317
318 log_debug("rmdir '%s'\n", sub_path);
319
320 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
321 if (errno != ENOENT && errno != ENOTEMPTY) {
322 log_error("rmdir(%s): %m", sub_path);
323 r = -errno;
324 }
325 }
326
327 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100328 /* Skip files for which the sticky bit is
329 * set. These are semantics we define, and are
330 * unknown elsewhere. See XDG_RUNTIME_DIR
331 * specification for details. */
332 if (s.st_mode & S_ISVTX)
333 continue;
334
Lennart Poettering17b90522011-02-14 21:55:06 +0100335 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200336 if (streq(dent->d_name, ".journal") &&
337 s.st_uid == 0)
338 continue;
339
340 if (streq(dent->d_name, "aquota.user") ||
341 streq(dent->d_name, "aquota.group"))
342 continue;
343 }
344
Lennart Poettering17b90522011-02-14 21:55:06 +0100345 /* Ignore sockets that are listed in /proc/net/unix */
346 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
347 continue;
348
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100349 /* Ignore device nodes */
350 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
351 continue;
352
Lennart Poettering24f3a372012-06-20 09:05:50 +0200353 /* Keep files on this level around if this is
354 * requested */
355 if (keep_this_level)
356 continue;
357
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200358 age = MAX3(timespec_load(&s.st_mtim),
359 timespec_load(&s.st_atim),
360 timespec_load(&s.st_ctim));
361
362 if (age >= cutoff)
363 continue;
364
365 log_debug("unlink '%s'\n", sub_path);
366
367 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
368 if (errno != ENOENT) {
369 log_error("unlink(%s): %m", sub_path);
370 r = -errno;
371 }
372 }
373
374 deleted = true;
375 }
376 }
377
378finish:
379 if (deleted) {
380 /* Restore original directory timestamps */
381 times[0] = ds->st_atim;
382 times[1] = ds->st_mtim;
383
384 if (futimens(dirfd(d), times) < 0)
385 log_error("utimensat(%s): %m", p);
386 }
387
388 free(sub_path);
389
390 return r;
391}
392
393static int clean_item(Item *i) {
394 DIR *d;
395 struct stat s, ps;
396 bool mountpoint;
397 int r;
398 usec_t cutoff, n;
399
400 assert(i);
401
402 if (i->type != CREATE_DIRECTORY &&
403 i->type != TRUNCATE_DIRECTORY &&
404 i->type != IGNORE_PATH)
405 return 0;
406
407 if (!i->age_set || i->age <= 0)
408 return 0;
409
410 n = now(CLOCK_REALTIME);
411 if (n < i->age)
412 return 0;
413
414 cutoff = n - i->age;
415
416 d = opendir(i->path);
417 if (!d) {
418 if (errno == ENOENT)
419 return 0;
420
421 log_error("Failed to open directory %s: %m", i->path);
422 return -errno;
423 }
424
425 if (fstat(dirfd(d), &s) < 0) {
426 log_error("stat(%s) failed: %m", i->path);
427 r = -errno;
428 goto finish;
429 }
430
431 if (!S_ISDIR(s.st_mode)) {
432 log_error("%s is not a directory.", i->path);
433 r = -ENOTDIR;
434 goto finish;
435 }
436
437 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
438 log_error("stat(%s/..) failed: %m", i->path);
439 r = -errno;
440 goto finish;
441 }
442
443 mountpoint = s.st_dev != ps.st_dev ||
444 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
445
Lennart Poettering24f3a372012-06-20 09:05:50 +0200446 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 +0200447
448finish:
449 if (d)
450 closedir(d);
451
452 return r;
453}
454
Michal Schmidt062e01b2011-12-16 18:00:11 +0100455static int item_set_perms(Item *i, const char *path) {
456 /* not using i->path directly because it may be a glob */
457 if (i->mode_set)
458 if (chmod(path, i->mode) < 0) {
459 log_error("chmod(%s) failed: %m", path);
460 return -errno;
461 }
462
463 if (i->uid_set || i->gid_set)
464 if (chown(path,
465 i->uid_set ? i->uid : (uid_t) -1,
466 i->gid_set ? i->gid : (gid_t) -1) < 0) {
467
468 log_error("chown(%s) failed: %m", path);
469 return -errno;
470 }
471
Lennart Poetteringc9bc0762012-07-03 16:25:50 +0200472 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100473}
474
475static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100476 DIR *d;
477 int ret = 0;
478
479 /* This returns the first error we run into, but nevertheless
480 * tries to go on */
481
482 d = opendir(path);
483 if (!d)
484 return errno == ENOENT ? 0 : -errno;
485
486 for (;;) {
487 struct dirent buf, *de;
488 bool is_dir;
489 int r;
490 char *entry_path;
491
492 r = readdir_r(d, &buf, &de);
493 if (r != 0) {
494 if (ret == 0)
495 ret = -r;
496 break;
497 }
498
499 if (!de)
500 break;
501
502 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
503 continue;
504
505 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
506 if (ret == 0)
507 ret = -ENOMEM;
508 continue;
509 }
510
511 if (de->d_type == DT_UNKNOWN) {
512 struct stat st;
513
514 if (lstat(entry_path, &st) < 0) {
515 if (ret == 0 && errno != ENOENT)
516 ret = -errno;
517 free(entry_path);
518 continue;
519 }
520
521 is_dir = S_ISDIR(st.st_mode);
522
523 } else
524 is_dir = de->d_type == DT_DIR;
525
Michal Schmidt062e01b2011-12-16 18:00:11 +0100526 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100527 if (r < 0) {
528 if (ret == 0 && r != -ENOENT)
529 ret = r;
530 free(entry_path);
531 continue;
532 }
533
534 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100535 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100536 if (r < 0 && ret == 0)
537 ret = r;
538 }
539
540 free(entry_path);
541 }
542
543 closedir(d);
544
545 return ret;
546}
547
548static int recursive_relabel(Item *i, const char *path) {
549 int r;
550 struct stat st;
551
Michal Schmidt062e01b2011-12-16 18:00:11 +0100552 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100553 if (r < 0)
554 return r;
555
556 if (lstat(path, &st) < 0)
557 return -errno;
558
559 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100560 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100561
562 return r;
563}
564
Michal Schmidt99e68c02011-12-15 23:45:26 +0100565static int glob_item(Item *i, int (*action)(Item *, const char *)) {
566 int r = 0, k;
567 glob_t g;
568 char **fn;
569
570 zero(g);
571
572 errno = 0;
573 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
574
575 if (k != GLOB_NOMATCH) {
576 if (errno != 0)
577 errno = EIO;
578
579 log_error("glob(%s) failed: %m", i->path);
580 return -errno;
581 }
582 }
583
584 STRV_FOREACH(fn, g.gl_pathv)
585 if ((k = action(i, *fn)) < 0)
586 r = k;
587
588 globfree(&g);
589 return r;
590}
591
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200592static int create_item(Item *i) {
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200593 int r, e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200594 mode_t u;
595 struct stat st;
596
597 assert(i);
598
599 switch (i->type) {
600
601 case IGNORE_PATH:
602 case REMOVE_PATH:
603 case RECURSIVE_REMOVE_PATH:
604 return 0;
605
606 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100607 case TRUNCATE_FILE:
608 case WRITE_FILE: {
609 int fd, flags;
610
611 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
612 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200613
614 u = umask(0);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200615 label_context_set(i->path, S_IFREG);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100616 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200617 e = errno;
618 label_context_clear();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200619 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200620 errno = e;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200621
622 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100623 if (i->type == WRITE_FILE && errno == ENOENT)
624 break;
625
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200626 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100627 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200628 }
629
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100630 if (i->argument) {
631 ssize_t n;
632 size_t l;
633 struct iovec iovec[2];
634 static const char new_line = '\n';
635
636 l = strlen(i->argument);
637
638 zero(iovec);
639 iovec[0].iov_base = i->argument;
640 iovec[0].iov_len = l;
641
642 iovec[1].iov_base = (void*) &new_line;
643 iovec[1].iov_len = 1;
644
645 n = writev(fd, iovec, 2);
Lennart Poettering03ad1132012-05-15 14:34:33 +0200646
647 /* It's OK if we don't write the trailing
648 * newline, hence we check for l, instead of
649 * l+1 here. Files in /sys often refuse
650 * writing of the trailing newline. */
651 if (n < 0 || (size_t) n < l) {
652 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short write");
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100653 close_nointr_nofail(fd);
654 return n < 0 ? n : -EIO;
655 }
656 }
657
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100658 close_nointr_nofail(fd);
659
660 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200661 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100662 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200663 }
664
665 if (!S_ISREG(st.st_mode)) {
666 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100667 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200668 }
669
Michal Schmidt062e01b2011-12-16 18:00:11 +0100670 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100671 if (r < 0)
672 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200673
674 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100675 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200676
677 case TRUNCATE_DIRECTORY:
678 case CREATE_DIRECTORY:
679
680 u = umask(0);
Kay Sieversd2e54fa2012-05-31 12:40:20 +0200681 mkdir_parents_label(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200682 r = mkdir(i->path, i->mode);
683 umask(u);
684
685 if (r < 0 && errno != EEXIST) {
686 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100687 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200688 }
689
690 if (stat(i->path, &st) < 0) {
691 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100692 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200693 }
694
695 if (!S_ISDIR(st.st_mode)) {
696 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100697 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200698 }
699
Michal Schmidt062e01b2011-12-16 18:00:11 +0100700 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100701 if (r < 0)
702 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200703
704 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200705
706 case CREATE_FIFO:
707
708 u = umask(0);
709 r = mkfifo(i->path, i->mode);
710 umask(u);
711
712 if (r < 0 && errno != EEXIST) {
713 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100714 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200715 }
716
717 if (stat(i->path, &st) < 0) {
718 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100719 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200720 }
721
722 if (!S_ISFIFO(st.st_mode)) {
723 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100724 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200725 }
726
Michal Schmidt062e01b2011-12-16 18:00:11 +0100727 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100728 if (r < 0)
729 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200730
731 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100732
Lennart Poettering468d7262012-01-17 15:04:12 +0100733 case CREATE_SYMLINK: {
734 char *x;
735
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200736 label_context_set(i->path, S_IFLNK);
Lennart Poettering468d7262012-01-17 15:04:12 +0100737 r = symlink(i->argument, i->path);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200738 e = errno;
739 label_context_clear();
740 errno = e;
741
Lennart Poettering468d7262012-01-17 15:04:12 +0100742 if (r < 0 && errno != EEXIST) {
743 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
744 return -errno;
745 }
746
747 r = readlink_malloc(i->path, &x);
748 if (r < 0) {
749 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
750 return -errno;
751 }
752
753 if (!streq(i->argument, x)) {
754 free(x);
755 log_error("%s is not the right symlinks.", i->path);
756 return -EEXIST;
757 }
758
759 free(x);
760 break;
761 }
762
763 case CREATE_BLOCK_DEVICE:
764 case CREATE_CHAR_DEVICE: {
Michal Schmidte7aee752012-06-14 16:01:19 +0200765 mode_t file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +0100766
767 u = umask(0);
Michal Schmidte7aee752012-06-14 16:01:19 +0200768 label_context_set(i->path, file_type);
769 r = mknod(i->path, i->mode | file_type, i->major_minor);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200770 e = errno;
771 label_context_clear();
Lennart Poettering468d7262012-01-17 15:04:12 +0100772 umask(u);
Kay Sieverse9a5ef72012-04-17 16:05:03 +0200773 errno = e;
Lennart Poettering468d7262012-01-17 15:04:12 +0100774
775 if (r < 0 && errno != EEXIST) {
776 log_error("Failed to create device node %s: %m", i->path);
777 return -errno;
778 }
779
780 if (stat(i->path, &st) < 0) {
781 log_error("stat(%s) failed: %m", i->path);
782 return -errno;
783 }
784
Michal Schmidte7aee752012-06-14 16:01:19 +0200785 if ((st.st_mode & S_IFMT) != file_type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100786 log_error("%s is not a device node.", i->path);
787 return -EEXIST;
788 }
789
790 r = item_set_perms(i, i->path);
791 if (r < 0)
792 return r;
793
794 break;
795 }
796
Michal Schmidt777b87e2011-12-16 18:27:35 +0100797 case RELABEL_PATH:
798
799 r = glob_item(i, item_set_perms);
800 if (r < 0)
801 return 0;
802 break;
803
Michal Schmidta8d88782011-12-15 23:11:07 +0100804 case RECURSIVE_RELABEL_PATH:
805
806 r = glob_item(i, recursive_relabel);
807 if (r < 0)
808 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200809 }
810
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200811 log_debug("%s created successfully.", i->path);
812
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100813 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200814}
815
Michal Schmidta0896122011-12-15 21:32:50 +0100816static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200817 int r;
818
819 assert(i);
820
821 switch (i->type) {
822
823 case CREATE_FILE:
824 case TRUNCATE_FILE:
825 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200826 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100827 case CREATE_SYMLINK:
828 case CREATE_BLOCK_DEVICE:
829 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200830 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100831 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100832 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100833 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200834 break;
835
836 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100837 if (remove(instance) < 0 && errno != ENOENT) {
838 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200839 return -errno;
840 }
841
842 break;
843
844 case TRUNCATE_DIRECTORY:
845 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +0200846 /* FIXME: we probably should use dir_cleanup() here
847 * instead of rm_rf() so that 'x' is honoured. */
Lennart Poetteringf56d5db2012-07-10 19:05:58 +0200848 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
Lennart Poettering468d7262012-01-17 15:04:12 +0100849 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100850 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200851 return r;
852 }
853
854 break;
855 }
856
857 return 0;
858}
859
Michal Schmidta0896122011-12-15 21:32:50 +0100860static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100861 int r = 0;
862
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100863 assert(i);
864
865 switch (i->type) {
866
867 case CREATE_FILE:
868 case TRUNCATE_FILE:
869 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200870 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100871 case CREATE_SYMLINK:
872 case CREATE_CHAR_DEVICE:
873 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100874 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100875 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100876 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100877 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100878 break;
879
880 case REMOVE_PATH:
881 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100882 case RECURSIVE_REMOVE_PATH:
883 r = glob_item(i, remove_item_instance);
884 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100885 }
886
Michal Schmidt99e68c02011-12-15 23:45:26 +0100887 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100888}
889
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200890static int process_item(Item *i) {
891 int r, q, p;
892
893 assert(i);
894
895 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100896 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200897 p = arg_clean ? clean_item(i) : 0;
898
899 if (r < 0)
900 return r;
901
902 if (q < 0)
903 return q;
904
905 return p;
906}
907
908static void item_free(Item *i) {
909 assert(i);
910
911 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100912 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200913 free(i);
914}
915
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200916static bool item_equal(Item *a, Item *b) {
917 assert(a);
918 assert(b);
919
920 if (!streq_ptr(a->path, b->path))
921 return false;
922
923 if (a->type != b->type)
924 return false;
925
926 if (a->uid_set != b->uid_set ||
927 (a->uid_set && a->uid != b->uid))
928 return false;
929
930 if (a->gid_set != b->gid_set ||
931 (a->gid_set && a->gid != b->gid))
932 return false;
933
934 if (a->mode_set != b->mode_set ||
935 (a->mode_set && a->mode != b->mode))
936 return false;
937
938 if (a->age_set != b->age_set ||
939 (a->age_set && a->age != b->age))
940 return false;
941
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100942 if ((a->type == CREATE_FILE ||
943 a->type == TRUNCATE_FILE ||
944 a->type == WRITE_FILE ||
945 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100946 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100947 return false;
948
949 if ((a->type == CREATE_CHAR_DEVICE ||
950 a->type == CREATE_BLOCK_DEVICE) &&
951 a->major_minor != b->major_minor)
952 return false;
953
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200954 return true;
955}
956
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100957static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200958 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200959 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100960 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200961 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100962 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200963
964 assert(fname);
965 assert(line >= 1);
966 assert(buffer);
967
Lennart Poettering468d7262012-01-17 15:04:12 +0100968 i = new0(Item, 1);
969 if (!i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200970 log_error("Out of memory");
971 return -ENOMEM;
972 }
973
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100974 if (sscanf(buffer,
975 "%c "
976 "%ms "
977 "%ms "
978 "%ms "
979 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100980 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100981 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100982 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100983 &i->path,
984 &mode,
985 &user,
986 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +0100987 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100988 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200989 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200990 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200991 goto finish;
992 }
993
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100994 if (n >= 0) {
995 n += strspn(buffer+n, WHITESPACE);
996 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
997 i->argument = unquote(buffer+n, "\"");
998 if (!i->argument) {
999 log_error("Out of memory");
1000 return -ENOMEM;
1001 }
1002 }
1003 }
1004
Michal Schmidt777b87e2011-12-16 18:27:35 +01001005 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001006
Michal Schmidt777b87e2011-12-16 18:27:35 +01001007 case CREATE_FILE:
1008 case TRUNCATE_FILE:
1009 case CREATE_DIRECTORY:
1010 case TRUNCATE_DIRECTORY:
1011 case CREATE_FIFO:
1012 case IGNORE_PATH:
1013 case REMOVE_PATH:
1014 case RECURSIVE_REMOVE_PATH:
1015 case RELABEL_PATH:
1016 case RECURSIVE_RELABEL_PATH:
1017 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001018
1019 case CREATE_SYMLINK:
1020 if (!i->argument) {
1021 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1022 r = -EBADMSG;
1023 goto finish;
1024 }
1025 break;
1026
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001027 case WRITE_FILE:
1028 if (!i->argument) {
1029 log_error("[%s:%u] Write file requires argument.", fname, line);
1030 r = -EBADMSG;
1031 goto finish;
1032 }
1033 break;
1034
Lennart Poettering468d7262012-01-17 15:04:12 +01001035 case CREATE_CHAR_DEVICE:
1036 case CREATE_BLOCK_DEVICE: {
1037 unsigned major, minor;
1038
1039 if (!i->argument) {
1040 log_error("[%s:%u] Device file requires argument.", fname, line);
1041 r = -EBADMSG;
1042 goto finish;
1043 }
1044
1045 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1046 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1047 r = -EBADMSG;
1048 goto finish;
1049 }
1050
1051 i->major_minor = makedev(major, minor);
1052 break;
1053 }
1054
Michal Schmidt777b87e2011-12-16 18:27:35 +01001055 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001056 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001057 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001058 goto finish;
1059 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001060
Michal Schmidta8d88782011-12-15 23:11:07 +01001061 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001062
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001063 if (!path_is_absolute(i->path)) {
1064 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1065 r = -EBADMSG;
1066 goto finish;
1067 }
1068
1069 path_kill_slashes(i->path);
1070
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001071 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001072 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001073 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001074 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001075
1076 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001077 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001078
Lennart Poettering4b678342011-07-23 01:17:59 +02001079 r = get_user_creds(&u, &i->uid, NULL, NULL);
1080 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001081 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1082 goto finish;
1083 }
1084
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001085 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001086 }
1087
1088 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001089 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001090
Lennart Poettering4b678342011-07-23 01:17:59 +02001091 r = get_group_creds(&g, &i->gid);
1092 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001093 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1094 goto finish;
1095 }
1096
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001097 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001098 }
1099
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001100 if (mode && !streq(mode, "-")) {
1101 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001102
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001103 if (sscanf(mode, "%o", &m) != 1) {
1104 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1105 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001106 goto finish;
1107 }
1108
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001109 i->mode = m;
1110 i->mode_set = true;
1111 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001112 i->mode =
1113 i->type == CREATE_DIRECTORY ||
1114 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001115
1116 if (age && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02001117 const char *a = age;
1118
1119 if (*a == '~') {
1120 i->keep_first_level = true;
1121 a++;
1122 }
1123
1124 if (parse_usec(a, &i->age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001125 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1126 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001127 goto finish;
1128 }
1129
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001130 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001131 }
1132
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001133 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001134
Lennart Poettering468d7262012-01-17 15:04:12 +01001135 existing = hashmap_get(h, i->path);
1136 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001137
1138 /* Two identical items are fine */
1139 if (!item_equal(existing, i))
1140 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1141
1142 r = 0;
1143 goto finish;
1144 }
1145
Lennart Poettering468d7262012-01-17 15:04:12 +01001146 r = hashmap_put(h, i->path, i);
1147 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001148 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001149 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001150 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001151
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001152 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001153 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001154
1155finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001156 free(user);
1157 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001158 free(mode);
1159 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001160
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001161 if (i)
1162 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001163
1164 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001165}
1166
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001167static int help(void) {
1168
Lennart Poettering522d4a42011-02-13 15:08:15 +01001169 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1170 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001171 " -h --help Show this help\n"
1172 " --create Create marked files/directories\n"
1173 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001174 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001175 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001176 program_invocation_short_name);
1177
1178 return 0;
1179}
1180
1181static int parse_argv(int argc, char *argv[]) {
1182
1183 enum {
1184 ARG_CREATE,
1185 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001186 ARG_REMOVE,
1187 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001188 };
1189
1190 static const struct option options[] = {
1191 { "help", no_argument, NULL, 'h' },
1192 { "create", no_argument, NULL, ARG_CREATE },
1193 { "clean", no_argument, NULL, ARG_CLEAN },
1194 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001195 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001196 { NULL, 0, NULL, 0 }
1197 };
1198
1199 int c;
1200
1201 assert(argc >= 0);
1202 assert(argv);
1203
1204 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1205
1206 switch (c) {
1207
1208 case 'h':
1209 help();
1210 return 0;
1211
1212 case ARG_CREATE:
1213 arg_create = true;
1214 break;
1215
1216 case ARG_CLEAN:
1217 arg_clean = true;
1218 break;
1219
1220 case ARG_REMOVE:
1221 arg_remove = true;
1222 break;
1223
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001224 case ARG_PREFIX:
1225 arg_prefix = optarg;
1226 break;
1227
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001228 case '?':
1229 return -EINVAL;
1230
1231 default:
1232 log_error("Unknown option code %c", c);
1233 return -EINVAL;
1234 }
1235 }
1236
1237 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001238 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001239 return -EINVAL;
1240 }
1241
1242 return 1;
1243}
1244
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001245static int read_config_file(const char *fn, bool ignore_enoent) {
1246 FILE *f;
1247 unsigned v = 0;
1248 int r = 0;
1249
1250 assert(fn);
1251
Lennart Poettering468d7262012-01-17 15:04:12 +01001252 f = fopen(fn, "re");
1253 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001254
1255 if (ignore_enoent && errno == ENOENT)
1256 return 0;
1257
1258 log_error("Failed to open %s: %m", fn);
1259 return -errno;
1260 }
1261
Kay Sievers772f8372011-04-25 21:38:21 +02001262 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001263 for (;;) {
1264 char line[LINE_MAX], *l;
1265 int k;
1266
1267 if (!(fgets(line, sizeof(line), f)))
1268 break;
1269
1270 v++;
1271
1272 l = strstrip(line);
1273 if (*l == '#' || *l == 0)
1274 continue;
1275
1276 if ((k = parse_line(fn, v, l)) < 0)
1277 if (r == 0)
1278 r = k;
1279 }
1280
1281 if (ferror(f)) {
1282 log_error("Failed to read from file %s: %m", fn);
1283 if (r == 0)
1284 r = -EIO;
1285 }
1286
1287 fclose(f);
1288
1289 return r;
1290}
1291
Dave Reisner91256702012-06-08 22:31:19 -04001292static char *resolve_fragment(const char *fragment, const char **search_paths) {
1293 const char **p;
1294 char *resolved_path;
1295
1296 if (is_path(fragment))
1297 return strdup(fragment);
1298
1299 STRV_FOREACH(p, search_paths) {
1300 resolved_path = join(*p, "/", fragment, NULL);
1301 if (resolved_path == NULL) {
1302 log_error("Out of memory");
1303 return NULL;
1304 }
1305
1306 if (access(resolved_path, F_OK) == 0)
1307 return resolved_path;
1308
1309 free(resolved_path);
1310 }
1311
Kay Sieversca2e8942012-06-10 19:21:50 +02001312 errno = ENOENT;
Dave Reisner91256702012-06-08 22:31:19 -04001313 return NULL;
1314}
1315
Lennart Poettering5008d582010-09-28 02:34:02 +02001316int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001317 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001318 Item *i;
1319 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001320
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001321 r = parse_argv(argc, argv);
1322 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001323 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1324
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001325 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001326 log_parse_environment();
1327 log_open();
1328
Lennart Poettering4c126262011-08-01 20:52:18 +02001329 umask(0022);
1330
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001331 label_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02001332
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001333 items = hashmap_new(string_hash_func, string_compare_func);
1334 globs = hashmap_new(string_hash_func, string_compare_func);
1335
1336 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001337 log_error("Out of memory");
1338 r = EXIT_FAILURE;
1339 goto finish;
1340 }
1341
Lennart Poettering5008d582010-09-28 02:34:02 +02001342 r = EXIT_SUCCESS;
1343
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001344 if (optind < argc) {
1345 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001346
Dave Reisner91256702012-06-08 22:31:19 -04001347 for (j = optind; j < argc; j++) {
Kay Sieversca2e8942012-06-10 19:21:50 +02001348 char *fragment;
1349
Lennart Poettering24f3a372012-06-20 09:05:50 +02001350 fragment = resolve_fragment(argv[j], (const char**) conf_file_dirs);
Kay Sieversca2e8942012-06-10 19:21:50 +02001351 if (!fragment) {
Kay Sievers94f7a712012-06-10 19:31:39 +02001352 log_error("Failed to find a %s file: %m", argv[j]);
Kay Sieversca2e8942012-06-10 19:21:50 +02001353 r = EXIT_FAILURE;
1354 goto finish;
1355 }
Dave Reisner91256702012-06-08 22:31:19 -04001356 if (read_config_file(fragment, false) < 0)
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001357 r = EXIT_FAILURE;
Dave Reisner91256702012-06-08 22:31:19 -04001358 free(fragment);
1359 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001360
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001361 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001362 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001363
Dave Reisner91256702012-06-08 22:31:19 -04001364 r = conf_files_list_strv(&files, ".conf",
Lennart Poettering24f3a372012-06-20 09:05:50 +02001365 (const char **) conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02001366 if (r < 0) {
Kay Sievers44143302011-04-28 23:51:24 +02001367 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
Michal Schmidta48f3d12012-04-17 22:53:35 +02001368 r = EXIT_FAILURE;
Kay Sievers44143302011-04-28 23:51:24 +02001369 goto finish;
1370 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001371
Kay Sievers772f8372011-04-25 21:38:21 +02001372 STRV_FOREACH(f, files) {
1373 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001374 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001375 }
1376
Kay Sievers772f8372011-04-25 21:38:21 +02001377 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001378 }
1379
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001380 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001381 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001382
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001383 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001384 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001385
Lennart Poettering5008d582010-09-28 02:34:02 +02001386finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001387 while ((i = hashmap_steal_first(items)))
1388 item_free(i);
1389
Lennart Poettering17b90522011-02-14 21:55:06 +01001390 while ((i = hashmap_steal_first(globs)))
1391 item_free(i);
1392
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001393 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001394 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001395
Lennart Poettering17b90522011-02-14 21:55:06 +01001396 set_free_free(unix_sockets);
1397
Lennart Poettering29003cf2010-10-19 19:36:45 +02001398 label_finish();
1399
Lennart Poettering5008d582010-09-28 02:34:02 +02001400 return r;
1401}