blob: 15913089ba44c576c6aec8f14da4b04c1b7a8bf7 [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"
Lennart Poettering5008d582010-09-28 02:34:02 +020045#include "strv.h"
46#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020047#include "set.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020048
Andreas Jaeger01000472010-09-29 10:08:24 +020049/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020050 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020051 * properly owned directories beneath /tmp, /var/tmp, /run, which are
52 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020053
Michal Schmidt66ccd032011-12-15 21:31:14 +010054typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010055 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020056 CREATE_FILE = 'f',
57 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010058 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020059 CREATE_DIRECTORY = 'd',
60 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020061 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010062 CREATE_SYMLINK = 'L',
63 CREATE_CHAR_DEVICE = 'c',
64 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010065
66 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020067 IGNORE_PATH = 'x',
68 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010069 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010070 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010071 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010072} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020073
74typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010075 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020076
77 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010078 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020079 uid_t uid;
80 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020081 mode_t mode;
82 usec_t age;
83
Lennart Poettering468d7262012-01-17 15:04:12 +010084 dev_t major_minor;
85
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020086 bool uid_set:1;
87 bool gid_set:1;
88 bool mode_set:1;
89 bool age_set:1;
90} Item;
91
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010092static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010093static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020094
95static bool arg_create = false;
96static bool arg_clean = false;
97static bool arg_remove = false;
98
Lennart Poetteringfba6e682011-02-13 14:00:54 +010099static const char *arg_prefix = NULL;
100
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200101#define MAX_DEPTH 256
102
Michal Schmidt66ccd032011-12-15 21:31:14 +0100103static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100104 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 +0100105}
106
107static struct Item* find_glob(Hashmap *h, const char *match) {
108 Item *j;
109 Iterator i;
110
111 HASHMAP_FOREACH(j, h, i)
112 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
113 return j;
114
115 return NULL;
116}
117
Lennart Poettering17b90522011-02-14 21:55:06 +0100118static void load_unix_sockets(void) {
119 FILE *f = NULL;
120 char line[LINE_MAX];
121
122 if (unix_sockets)
123 return;
124
125 /* We maintain a cache of the sockets we found in
126 * /proc/net/unix to speed things up a little. */
127
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100128 unix_sockets = set_new(string_hash_func, string_compare_func);
129 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100130 return;
131
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100132 f = fopen("/proc/net/unix", "re");
133 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100134 return;
135
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100136 /* Skip header */
137 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100138 goto fail;
139
140 for (;;) {
141 char *p, *s;
142 int k;
143
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100144 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100145 break;
146
147 truncate_nl(line);
148
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100149 p = strchr(line, ':');
150 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100151 continue;
152
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100153 if (strlen(p) < 37)
154 continue;
155
156 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100157 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100158 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100159 p += strspn(p, WHITESPACE);
160
161 if (*p != '/')
162 continue;
163
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100164 s = strdup(p);
165 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100166 goto fail;
167
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100168 path_kill_slashes(s);
169
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100170 k = set_put(unix_sockets, s);
171 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100172 free(s);
173
174 if (k != -EEXIST)
175 goto fail;
176 }
177 }
178
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200179 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100180 return;
181
182fail:
183 set_free_free(unix_sockets);
184 unix_sockets = NULL;
185
186 if (f)
187 fclose(f);
188}
189
190static bool unix_socket_alive(const char *fn) {
191 assert(fn);
192
193 load_unix_sockets();
194
195 if (unix_sockets)
196 return !!set_get(unix_sockets, (char*) fn);
197
198 /* We don't know, so assume yes */
199 return true;
200}
201
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200202static int dir_cleanup(
203 const char *p,
204 DIR *d,
205 const struct stat *ds,
206 usec_t cutoff,
207 dev_t rootdev,
208 bool mountpoint,
209 int maxdepth)
210{
211 struct dirent *dent;
212 struct timespec times[2];
213 bool deleted = false;
214 char *sub_path = NULL;
215 int r = 0;
216
217 while ((dent = readdir(d))) {
218 struct stat s;
219 usec_t age;
220
221 if (streq(dent->d_name, ".") ||
222 streq(dent->d_name, ".."))
223 continue;
224
225 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
226
227 if (errno != ENOENT) {
228 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
229 r = -errno;
230 }
231
232 continue;
233 }
234
235 /* Stay on the same filesystem */
236 if (s.st_dev != rootdev)
237 continue;
238
239 /* Do not delete read-only files owned by root */
240 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
241 continue;
242
243 free(sub_path);
244 sub_path = NULL;
245
246 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
247 log_error("Out of memory");
248 r = -ENOMEM;
249 goto finish;
250 }
251
252 /* Is there an item configured for this path? */
253 if (hashmap_get(items, sub_path))
254 continue;
255
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100256 if (find_glob(globs, sub_path))
257 continue;
258
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200259 if (S_ISDIR(s.st_mode)) {
260
261 if (mountpoint &&
262 streq(dent->d_name, "lost+found") &&
263 s.st_uid == 0)
264 continue;
265
266 if (maxdepth <= 0)
267 log_warning("Reached max depth on %s.", sub_path);
268 else {
269 DIR *sub_dir;
270 int q;
271
Kay Sieverse5f3d1b2012-04-11 21:33:12 +0200272 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200273 if (sub_dir == NULL) {
274 if (errno != ENOENT) {
275 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
276 r = -errno;
277 }
278
279 continue;
280 }
281
282 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
283 closedir(sub_dir);
284
285 if (q < 0)
286 r = q;
287 }
288
289 /* Ignore ctime, we change it when deleting */
290 age = MAX(timespec_load(&s.st_mtim),
291 timespec_load(&s.st_atim));
292 if (age >= cutoff)
293 continue;
294
295 log_debug("rmdir '%s'\n", sub_path);
296
297 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
298 if (errno != ENOENT && errno != ENOTEMPTY) {
299 log_error("rmdir(%s): %m", sub_path);
300 r = -errno;
301 }
302 }
303
304 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100305 /* Skip files for which the sticky bit is
306 * set. These are semantics we define, and are
307 * unknown elsewhere. See XDG_RUNTIME_DIR
308 * specification for details. */
309 if (s.st_mode & S_ISVTX)
310 continue;
311
Lennart Poettering17b90522011-02-14 21:55:06 +0100312 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200313 if (streq(dent->d_name, ".journal") &&
314 s.st_uid == 0)
315 continue;
316
317 if (streq(dent->d_name, "aquota.user") ||
318 streq(dent->d_name, "aquota.group"))
319 continue;
320 }
321
Lennart Poettering17b90522011-02-14 21:55:06 +0100322 /* Ignore sockets that are listed in /proc/net/unix */
323 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
324 continue;
325
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100326 /* Ignore device nodes */
327 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
328 continue;
329
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200330 age = MAX3(timespec_load(&s.st_mtim),
331 timespec_load(&s.st_atim),
332 timespec_load(&s.st_ctim));
333
334 if (age >= cutoff)
335 continue;
336
337 log_debug("unlink '%s'\n", sub_path);
338
339 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
340 if (errno != ENOENT) {
341 log_error("unlink(%s): %m", sub_path);
342 r = -errno;
343 }
344 }
345
346 deleted = true;
347 }
348 }
349
350finish:
351 if (deleted) {
352 /* Restore original directory timestamps */
353 times[0] = ds->st_atim;
354 times[1] = ds->st_mtim;
355
356 if (futimens(dirfd(d), times) < 0)
357 log_error("utimensat(%s): %m", p);
358 }
359
360 free(sub_path);
361
362 return r;
363}
364
365static int clean_item(Item *i) {
366 DIR *d;
367 struct stat s, ps;
368 bool mountpoint;
369 int r;
370 usec_t cutoff, n;
371
372 assert(i);
373
374 if (i->type != CREATE_DIRECTORY &&
375 i->type != TRUNCATE_DIRECTORY &&
376 i->type != IGNORE_PATH)
377 return 0;
378
379 if (!i->age_set || i->age <= 0)
380 return 0;
381
382 n = now(CLOCK_REALTIME);
383 if (n < i->age)
384 return 0;
385
386 cutoff = n - i->age;
387
388 d = opendir(i->path);
389 if (!d) {
390 if (errno == ENOENT)
391 return 0;
392
393 log_error("Failed to open directory %s: %m", i->path);
394 return -errno;
395 }
396
397 if (fstat(dirfd(d), &s) < 0) {
398 log_error("stat(%s) failed: %m", i->path);
399 r = -errno;
400 goto finish;
401 }
402
403 if (!S_ISDIR(s.st_mode)) {
404 log_error("%s is not a directory.", i->path);
405 r = -ENOTDIR;
406 goto finish;
407 }
408
409 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
410 log_error("stat(%s/..) failed: %m", i->path);
411 r = -errno;
412 goto finish;
413 }
414
415 mountpoint = s.st_dev != ps.st_dev ||
416 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
417
418 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
419
420finish:
421 if (d)
422 closedir(d);
423
424 return r;
425}
426
Michal Schmidt062e01b2011-12-16 18:00:11 +0100427static int item_set_perms(Item *i, const char *path) {
428 /* not using i->path directly because it may be a glob */
429 if (i->mode_set)
430 if (chmod(path, i->mode) < 0) {
431 log_error("chmod(%s) failed: %m", path);
432 return -errno;
433 }
434
435 if (i->uid_set || i->gid_set)
436 if (chown(path,
437 i->uid_set ? i->uid : (uid_t) -1,
438 i->gid_set ? i->gid : (gid_t) -1) < 0) {
439
440 log_error("chown(%s) failed: %m", path);
441 return -errno;
442 }
443
444 return label_fix(path, false);
445}
446
447static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100448 DIR *d;
449 int ret = 0;
450
451 /* This returns the first error we run into, but nevertheless
452 * tries to go on */
453
454 d = opendir(path);
455 if (!d)
456 return errno == ENOENT ? 0 : -errno;
457
458 for (;;) {
459 struct dirent buf, *de;
460 bool is_dir;
461 int r;
462 char *entry_path;
463
464 r = readdir_r(d, &buf, &de);
465 if (r != 0) {
466 if (ret == 0)
467 ret = -r;
468 break;
469 }
470
471 if (!de)
472 break;
473
474 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
475 continue;
476
477 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
478 if (ret == 0)
479 ret = -ENOMEM;
480 continue;
481 }
482
483 if (de->d_type == DT_UNKNOWN) {
484 struct stat st;
485
486 if (lstat(entry_path, &st) < 0) {
487 if (ret == 0 && errno != ENOENT)
488 ret = -errno;
489 free(entry_path);
490 continue;
491 }
492
493 is_dir = S_ISDIR(st.st_mode);
494
495 } else
496 is_dir = de->d_type == DT_DIR;
497
Michal Schmidt062e01b2011-12-16 18:00:11 +0100498 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100499 if (r < 0) {
500 if (ret == 0 && r != -ENOENT)
501 ret = r;
502 free(entry_path);
503 continue;
504 }
505
506 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100507 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100508 if (r < 0 && ret == 0)
509 ret = r;
510 }
511
512 free(entry_path);
513 }
514
515 closedir(d);
516
517 return ret;
518}
519
520static int recursive_relabel(Item *i, const char *path) {
521 int r;
522 struct stat st;
523
Michal Schmidt062e01b2011-12-16 18:00:11 +0100524 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100525 if (r < 0)
526 return r;
527
528 if (lstat(path, &st) < 0)
529 return -errno;
530
531 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100532 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100533
534 return r;
535}
536
Michal Schmidt99e68c02011-12-15 23:45:26 +0100537static int glob_item(Item *i, int (*action)(Item *, const char *)) {
538 int r = 0, k;
539 glob_t g;
540 char **fn;
541
542 zero(g);
543
544 errno = 0;
545 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
546
547 if (k != GLOB_NOMATCH) {
548 if (errno != 0)
549 errno = EIO;
550
551 log_error("glob(%s) failed: %m", i->path);
552 return -errno;
553 }
554 }
555
556 STRV_FOREACH(fn, g.gl_pathv)
557 if ((k = action(i, *fn)) < 0)
558 r = k;
559
560 globfree(&g);
561 return r;
562}
563
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200564static int create_item(Item *i) {
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100565 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200566 mode_t u;
567 struct stat st;
568
569 assert(i);
570
571 switch (i->type) {
572
573 case IGNORE_PATH:
574 case REMOVE_PATH:
575 case RECURSIVE_REMOVE_PATH:
576 return 0;
577
578 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100579 case TRUNCATE_FILE:
580 case WRITE_FILE: {
581 int fd, flags;
582
583 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
584 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200585
586 u = umask(0);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100587 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200588 umask(u);
589
590 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100591 if (i->type == WRITE_FILE && errno == ENOENT)
592 break;
593
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200594 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100595 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200596 }
597
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100598 if (i->argument) {
599 ssize_t n;
600 size_t l;
601 struct iovec iovec[2];
602 static const char new_line = '\n';
603
604 l = strlen(i->argument);
605
606 zero(iovec);
607 iovec[0].iov_base = i->argument;
608 iovec[0].iov_len = l;
609
610 iovec[1].iov_base = (void*) &new_line;
611 iovec[1].iov_len = 1;
612
613 n = writev(fd, iovec, 2);
614 if (n < 0 || (size_t) n != l+1) {
615 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short");
616 close_nointr_nofail(fd);
617 return n < 0 ? n : -EIO;
618 }
619 }
620
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100621 close_nointr_nofail(fd);
622
623 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200624 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100625 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200626 }
627
628 if (!S_ISREG(st.st_mode)) {
629 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100630 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200631 }
632
Michal Schmidt062e01b2011-12-16 18:00:11 +0100633 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100634 if (r < 0)
635 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200636
637 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100638 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200639
640 case TRUNCATE_DIRECTORY:
641 case CREATE_DIRECTORY:
642
643 u = umask(0);
Kay Sievers33366862011-04-03 22:21:21 +0200644 mkdir_parents(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200645 r = mkdir(i->path, i->mode);
646 umask(u);
647
648 if (r < 0 && errno != EEXIST) {
649 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100650 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200651 }
652
653 if (stat(i->path, &st) < 0) {
654 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100655 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200656 }
657
658 if (!S_ISDIR(st.st_mode)) {
659 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100660 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200661 }
662
Michal Schmidt062e01b2011-12-16 18:00:11 +0100663 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100664 if (r < 0)
665 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200666
667 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200668
669 case CREATE_FIFO:
670
671 u = umask(0);
672 r = mkfifo(i->path, i->mode);
673 umask(u);
674
675 if (r < 0 && errno != EEXIST) {
676 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100677 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200678 }
679
680 if (stat(i->path, &st) < 0) {
681 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100682 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200683 }
684
685 if (!S_ISFIFO(st.st_mode)) {
686 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100687 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200688 }
689
Michal Schmidt062e01b2011-12-16 18:00:11 +0100690 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100691 if (r < 0)
692 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200693
694 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100695
Lennart Poettering468d7262012-01-17 15:04:12 +0100696 case CREATE_SYMLINK: {
697 char *x;
698
699 r = symlink(i->argument, i->path);
700 if (r < 0 && errno != EEXIST) {
701 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
702 return -errno;
703 }
704
705 r = readlink_malloc(i->path, &x);
706 if (r < 0) {
707 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
708 return -errno;
709 }
710
711 if (!streq(i->argument, x)) {
712 free(x);
713 log_error("%s is not the right symlinks.", i->path);
714 return -EEXIST;
715 }
716
717 free(x);
718 break;
719 }
720
721 case CREATE_BLOCK_DEVICE:
722 case CREATE_CHAR_DEVICE: {
723
724 u = umask(0);
725 r = mknod(i->path, i->mode | (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR), i->major_minor);
726 umask(u);
727
728 if (r < 0 && errno != EEXIST) {
729 log_error("Failed to create device node %s: %m", i->path);
730 return -errno;
731 }
732
733 if (stat(i->path, &st) < 0) {
734 log_error("stat(%s) failed: %m", i->path);
735 return -errno;
736 }
737
738 if (i->type == CREATE_BLOCK_DEVICE ? !S_ISBLK(st.st_mode) : !S_ISCHR(st.st_mode)) {
739 log_error("%s is not a device node.", i->path);
740 return -EEXIST;
741 }
742
743 r = item_set_perms(i, i->path);
744 if (r < 0)
745 return r;
746
747 break;
748 }
749
Michal Schmidt777b87e2011-12-16 18:27:35 +0100750 case RELABEL_PATH:
751
752 r = glob_item(i, item_set_perms);
753 if (r < 0)
754 return 0;
755 break;
756
Michal Schmidta8d88782011-12-15 23:11:07 +0100757 case RECURSIVE_RELABEL_PATH:
758
759 r = glob_item(i, recursive_relabel);
760 if (r < 0)
761 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200762 }
763
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200764 log_debug("%s created successfully.", i->path);
765
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100766 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200767}
768
Michal Schmidta0896122011-12-15 21:32:50 +0100769static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200770 int r;
771
772 assert(i);
773
774 switch (i->type) {
775
776 case CREATE_FILE:
777 case TRUNCATE_FILE:
778 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200779 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100780 case CREATE_SYMLINK:
781 case CREATE_BLOCK_DEVICE:
782 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200783 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100784 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100785 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100786 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200787 break;
788
789 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100790 if (remove(instance) < 0 && errno != ENOENT) {
791 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200792 return -errno;
793 }
794
795 break;
796
797 case TRUNCATE_DIRECTORY:
798 case RECURSIVE_REMOVE_PATH:
Lennart Poettering468d7262012-01-17 15:04:12 +0100799 r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
800 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100801 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200802 return r;
803 }
804
805 break;
806 }
807
808 return 0;
809}
810
Michal Schmidta0896122011-12-15 21:32:50 +0100811static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100812 int r = 0;
813
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100814 assert(i);
815
816 switch (i->type) {
817
818 case CREATE_FILE:
819 case TRUNCATE_FILE:
820 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200821 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100822 case CREATE_SYMLINK:
823 case CREATE_CHAR_DEVICE:
824 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100825 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100826 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100827 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100828 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100829 break;
830
831 case REMOVE_PATH:
832 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100833 case RECURSIVE_REMOVE_PATH:
834 r = glob_item(i, remove_item_instance);
835 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100836 }
837
Michal Schmidt99e68c02011-12-15 23:45:26 +0100838 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100839}
840
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200841static int process_item(Item *i) {
842 int r, q, p;
843
844 assert(i);
845
846 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100847 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200848 p = arg_clean ? clean_item(i) : 0;
849
850 if (r < 0)
851 return r;
852
853 if (q < 0)
854 return q;
855
856 return p;
857}
858
859static void item_free(Item *i) {
860 assert(i);
861
862 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100863 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200864 free(i);
865}
866
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200867static bool item_equal(Item *a, Item *b) {
868 assert(a);
869 assert(b);
870
871 if (!streq_ptr(a->path, b->path))
872 return false;
873
874 if (a->type != b->type)
875 return false;
876
877 if (a->uid_set != b->uid_set ||
878 (a->uid_set && a->uid != b->uid))
879 return false;
880
881 if (a->gid_set != b->gid_set ||
882 (a->gid_set && a->gid != b->gid))
883 return false;
884
885 if (a->mode_set != b->mode_set ||
886 (a->mode_set && a->mode != b->mode))
887 return false;
888
889 if (a->age_set != b->age_set ||
890 (a->age_set && a->age != b->age))
891 return false;
892
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100893 if ((a->type == CREATE_FILE ||
894 a->type == TRUNCATE_FILE ||
895 a->type == WRITE_FILE ||
896 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100897 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100898 return false;
899
900 if ((a->type == CREATE_CHAR_DEVICE ||
901 a->type == CREATE_BLOCK_DEVICE) &&
902 a->major_minor != b->major_minor)
903 return false;
904
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200905 return true;
906}
907
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100908static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200909 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200910 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100911 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200912 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100913 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200914
915 assert(fname);
916 assert(line >= 1);
917 assert(buffer);
918
Lennart Poettering468d7262012-01-17 15:04:12 +0100919 i = new0(Item, 1);
920 if (!i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200921 log_error("Out of memory");
922 return -ENOMEM;
923 }
924
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100925 if (sscanf(buffer,
926 "%c "
927 "%ms "
928 "%ms "
929 "%ms "
930 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100931 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100932 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100933 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100934 &i->path,
935 &mode,
936 &user,
937 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +0100938 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100939 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200940 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200941 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200942 goto finish;
943 }
944
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100945 if (n >= 0) {
946 n += strspn(buffer+n, WHITESPACE);
947 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
948 i->argument = unquote(buffer+n, "\"");
949 if (!i->argument) {
950 log_error("Out of memory");
951 return -ENOMEM;
952 }
953 }
954 }
955
Michal Schmidt777b87e2011-12-16 18:27:35 +0100956 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100957
Michal Schmidt777b87e2011-12-16 18:27:35 +0100958 case CREATE_FILE:
959 case TRUNCATE_FILE:
960 case CREATE_DIRECTORY:
961 case TRUNCATE_DIRECTORY:
962 case CREATE_FIFO:
963 case IGNORE_PATH:
964 case REMOVE_PATH:
965 case RECURSIVE_REMOVE_PATH:
966 case RELABEL_PATH:
967 case RECURSIVE_RELABEL_PATH:
968 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100969
970 case CREATE_SYMLINK:
971 if (!i->argument) {
972 log_error("[%s:%u] Symlink file requires argument.", fname, line);
973 r = -EBADMSG;
974 goto finish;
975 }
976 break;
977
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100978 case WRITE_FILE:
979 if (!i->argument) {
980 log_error("[%s:%u] Write file requires argument.", fname, line);
981 r = -EBADMSG;
982 goto finish;
983 }
984 break;
985
Lennart Poettering468d7262012-01-17 15:04:12 +0100986 case CREATE_CHAR_DEVICE:
987 case CREATE_BLOCK_DEVICE: {
988 unsigned major, minor;
989
990 if (!i->argument) {
991 log_error("[%s:%u] Device file requires argument.", fname, line);
992 r = -EBADMSG;
993 goto finish;
994 }
995
996 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
997 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
998 r = -EBADMSG;
999 goto finish;
1000 }
1001
1002 i->major_minor = makedev(major, minor);
1003 break;
1004 }
1005
Michal Schmidt777b87e2011-12-16 18:27:35 +01001006 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001007 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001008 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001009 goto finish;
1010 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001011
Michal Schmidta8d88782011-12-15 23:11:07 +01001012 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001013
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001014 if (!path_is_absolute(i->path)) {
1015 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1016 r = -EBADMSG;
1017 goto finish;
1018 }
1019
1020 path_kill_slashes(i->path);
1021
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001022 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001023 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001024 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001025 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001026
1027 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001028 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001029
Lennart Poettering4b678342011-07-23 01:17:59 +02001030 r = get_user_creds(&u, &i->uid, NULL, NULL);
1031 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001032 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1033 goto finish;
1034 }
1035
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001036 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001037 }
1038
1039 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001040 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001041
Lennart Poettering4b678342011-07-23 01:17:59 +02001042 r = get_group_creds(&g, &i->gid);
1043 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001044 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1045 goto finish;
1046 }
1047
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001048 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001049 }
1050
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001051 if (mode && !streq(mode, "-")) {
1052 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001053
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001054 if (sscanf(mode, "%o", &m) != 1) {
1055 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1056 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001057 goto finish;
1058 }
1059
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001060 i->mode = m;
1061 i->mode_set = true;
1062 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001063 i->mode =
1064 i->type == CREATE_DIRECTORY ||
1065 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001066
1067 if (age && !streq(age, "-")) {
1068 if (parse_usec(age, &i->age) < 0) {
1069 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1070 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001071 goto finish;
1072 }
1073
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001074 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001075 }
1076
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001077 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001078
Lennart Poettering468d7262012-01-17 15:04:12 +01001079 existing = hashmap_get(h, i->path);
1080 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001081
1082 /* Two identical items are fine */
1083 if (!item_equal(existing, i))
1084 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1085
1086 r = 0;
1087 goto finish;
1088 }
1089
Lennart Poettering468d7262012-01-17 15:04:12 +01001090 r = hashmap_put(h, i->path, i);
1091 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001092 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001093 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001094 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001095
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001096 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001097 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001098
1099finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001100 free(user);
1101 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001102 free(mode);
1103 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001104
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001105 if (i)
1106 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001107
1108 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001109}
1110
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001111static int help(void) {
1112
Lennart Poettering522d4a42011-02-13 15:08:15 +01001113 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1114 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001115 " -h --help Show this help\n"
1116 " --create Create marked files/directories\n"
1117 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001118 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001119 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001120 program_invocation_short_name);
1121
1122 return 0;
1123}
1124
1125static int parse_argv(int argc, char *argv[]) {
1126
1127 enum {
1128 ARG_CREATE,
1129 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001130 ARG_REMOVE,
1131 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001132 };
1133
1134 static const struct option options[] = {
1135 { "help", no_argument, NULL, 'h' },
1136 { "create", no_argument, NULL, ARG_CREATE },
1137 { "clean", no_argument, NULL, ARG_CLEAN },
1138 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001139 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001140 { NULL, 0, NULL, 0 }
1141 };
1142
1143 int c;
1144
1145 assert(argc >= 0);
1146 assert(argv);
1147
1148 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1149
1150 switch (c) {
1151
1152 case 'h':
1153 help();
1154 return 0;
1155
1156 case ARG_CREATE:
1157 arg_create = true;
1158 break;
1159
1160 case ARG_CLEAN:
1161 arg_clean = true;
1162 break;
1163
1164 case ARG_REMOVE:
1165 arg_remove = true;
1166 break;
1167
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001168 case ARG_PREFIX:
1169 arg_prefix = optarg;
1170 break;
1171
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001172 case '?':
1173 return -EINVAL;
1174
1175 default:
1176 log_error("Unknown option code %c", c);
1177 return -EINVAL;
1178 }
1179 }
1180
1181 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001182 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001183 return -EINVAL;
1184 }
1185
1186 return 1;
1187}
1188
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001189static int read_config_file(const char *fn, bool ignore_enoent) {
1190 FILE *f;
1191 unsigned v = 0;
1192 int r = 0;
1193
1194 assert(fn);
1195
Lennart Poettering468d7262012-01-17 15:04:12 +01001196 f = fopen(fn, "re");
1197 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001198
1199 if (ignore_enoent && errno == ENOENT)
1200 return 0;
1201
1202 log_error("Failed to open %s: %m", fn);
1203 return -errno;
1204 }
1205
Kay Sievers772f8372011-04-25 21:38:21 +02001206 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001207 for (;;) {
1208 char line[LINE_MAX], *l;
1209 int k;
1210
1211 if (!(fgets(line, sizeof(line), f)))
1212 break;
1213
1214 v++;
1215
1216 l = strstrip(line);
1217 if (*l == '#' || *l == 0)
1218 continue;
1219
1220 if ((k = parse_line(fn, v, l)) < 0)
1221 if (r == 0)
1222 r = k;
1223 }
1224
1225 if (ferror(f)) {
1226 log_error("Failed to read from file %s: %m", fn);
1227 if (r == 0)
1228 r = -EIO;
1229 }
1230
1231 fclose(f);
1232
1233 return r;
1234}
1235
Lennart Poettering5008d582010-09-28 02:34:02 +02001236int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001237 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001238 Item *i;
1239 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001240
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001241 r = parse_argv(argc, argv);
1242 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001243 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1244
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001245 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001246 log_parse_environment();
1247 log_open();
1248
Lennart Poettering4c126262011-08-01 20:52:18 +02001249 umask(0022);
1250
Lennart Poettering5008d582010-09-28 02:34:02 +02001251 label_init();
1252
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001253 items = hashmap_new(string_hash_func, string_compare_func);
1254 globs = hashmap_new(string_hash_func, string_compare_func);
1255
1256 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001257 log_error("Out of memory");
1258 r = EXIT_FAILURE;
1259 goto finish;
1260 }
1261
Lennart Poettering5008d582010-09-28 02:34:02 +02001262 r = EXIT_SUCCESS;
1263
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001264 if (optind < argc) {
1265 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001266
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001267 for (j = optind; j < argc; j++)
1268 if (read_config_file(argv[j], false) < 0)
1269 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001270
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001271 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001272 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001273
Kay Sievers44143302011-04-28 23:51:24 +02001274 r = conf_files_list(&files, ".conf",
Kay Sievers44143302011-04-28 23:51:24 +02001275 "/etc/tmpfiles.d",
Lennart Poetteringfc1a2e02012-03-14 14:25:05 +01001276 "/run/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +02001277 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +02001278 "/usr/lib/tmpfiles.d",
1279 NULL);
1280 if (r < 0) {
1281 r = EXIT_FAILURE;
1282 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1283 goto finish;
1284 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001285
Kay Sievers772f8372011-04-25 21:38:21 +02001286 STRV_FOREACH(f, files) {
1287 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001288 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001289 }
1290
Kay Sievers772f8372011-04-25 21:38:21 +02001291 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001292 }
1293
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001294 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001295 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001296
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001297 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001298 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001299
Lennart Poettering5008d582010-09-28 02:34:02 +02001300finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001301 while ((i = hashmap_steal_first(items)))
1302 item_free(i);
1303
Lennart Poettering17b90522011-02-14 21:55:06 +01001304 while ((i = hashmap_steal_first(globs)))
1305 item_free(i);
1306
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001307 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001308 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001309
Lennart Poettering17b90522011-02-14 21:55:06 +01001310 set_free_free(unix_sockets);
1311
Lennart Poettering29003cf2010-10-19 19:36:45 +02001312 label_finish();
1313
Lennart Poettering5008d582010-09-28 02:34:02 +02001314 return r;
1315}