blob: 8cbce12dcd14e014b80995d6d6605ae309b4c100 [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
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (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
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 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"
44#include "strv.h"
45#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020046#include "set.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020047
Andreas Jaeger01000472010-09-29 10:08:24 +020048/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020049 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020050 * properly owned directories beneath /tmp, /var/tmp, /run, which are
51 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020052
Michal Schmidt66ccd032011-12-15 21:31:14 +010053typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010054 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020055 CREATE_FILE = 'f',
56 TRUNCATE_FILE = 'F',
Lennart Poettering31ed59c2012-01-18 16:39:04 +010057 WRITE_FILE = 'w',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020058 CREATE_DIRECTORY = 'd',
59 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020060 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010061 CREATE_SYMLINK = 'L',
62 CREATE_CHAR_DEVICE = 'c',
63 CREATE_BLOCK_DEVICE = 'b',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010064
65 /* These ones take globs */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020066 IGNORE_PATH = 'x',
67 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010068 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010069 RELABEL_PATH = 'z',
Michal Schmidta8d88782011-12-15 23:11:07 +010070 RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt66ccd032011-12-15 21:31:14 +010071} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020072
73typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +010074 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020075
76 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +010077 char *argument;
Lennart Poettering5008d582010-09-28 02:34:02 +020078 uid_t uid;
79 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020080 mode_t mode;
81 usec_t age;
82
Lennart Poettering468d7262012-01-17 15:04:12 +010083 dev_t major_minor;
84
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020085 bool uid_set:1;
86 bool gid_set:1;
87 bool mode_set:1;
88 bool age_set:1;
89} Item;
90
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010091static Hashmap *items = NULL, *globs = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +010092static Set *unix_sockets = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020093
94static bool arg_create = false;
95static bool arg_clean = false;
96static bool arg_remove = false;
97
Lennart Poetteringfba6e682011-02-13 14:00:54 +010098static const char *arg_prefix = NULL;
99
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200100#define MAX_DEPTH 256
101
Michal Schmidt66ccd032011-12-15 21:31:14 +0100102static bool needs_glob(ItemType t) {
Michal Schmidt777b87e2011-12-16 18:27:35 +0100103 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 +0100104}
105
106static struct Item* find_glob(Hashmap *h, const char *match) {
107 Item *j;
108 Iterator i;
109
110 HASHMAP_FOREACH(j, h, i)
111 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
112 return j;
113
114 return NULL;
115}
116
Lennart Poettering17b90522011-02-14 21:55:06 +0100117static void load_unix_sockets(void) {
118 FILE *f = NULL;
119 char line[LINE_MAX];
120
121 if (unix_sockets)
122 return;
123
124 /* We maintain a cache of the sockets we found in
125 * /proc/net/unix to speed things up a little. */
126
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100127 unix_sockets = set_new(string_hash_func, string_compare_func);
128 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100129 return;
130
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100131 f = fopen("/proc/net/unix", "re");
132 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100133 return;
134
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100135 /* Skip header */
136 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100137 goto fail;
138
139 for (;;) {
140 char *p, *s;
141 int k;
142
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100143 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100144 break;
145
146 truncate_nl(line);
147
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100148 p = strchr(line, ':');
149 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100150 continue;
151
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100152 if (strlen(p) < 37)
153 continue;
154
155 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100156 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100157 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100158 p += strspn(p, WHITESPACE);
159
160 if (*p != '/')
161 continue;
162
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100163 s = strdup(p);
164 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100165 goto fail;
166
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100167 path_kill_slashes(s);
168
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100169 k = set_put(unix_sockets, s);
170 if (k < 0) {
Lennart Poettering17b90522011-02-14 21:55:06 +0100171 free(s);
172
173 if (k != -EEXIST)
174 goto fail;
175 }
176 }
177
Thomas Jarosch10d975f2011-10-05 22:30:49 +0200178 fclose(f);
Lennart Poettering17b90522011-02-14 21:55:06 +0100179 return;
180
181fail:
182 set_free_free(unix_sockets);
183 unix_sockets = NULL;
184
185 if (f)
186 fclose(f);
187}
188
189static bool unix_socket_alive(const char *fn) {
190 assert(fn);
191
192 load_unix_sockets();
193
194 if (unix_sockets)
195 return !!set_get(unix_sockets, (char*) fn);
196
197 /* We don't know, so assume yes */
198 return true;
199}
200
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200201static int dir_cleanup(
202 const char *p,
203 DIR *d,
204 const struct stat *ds,
205 usec_t cutoff,
206 dev_t rootdev,
207 bool mountpoint,
208 int maxdepth)
209{
210 struct dirent *dent;
211 struct timespec times[2];
212 bool deleted = false;
213 char *sub_path = NULL;
214 int r = 0;
215
216 while ((dent = readdir(d))) {
217 struct stat s;
218 usec_t age;
219
220 if (streq(dent->d_name, ".") ||
221 streq(dent->d_name, ".."))
222 continue;
223
224 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
225
226 if (errno != ENOENT) {
227 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
228 r = -errno;
229 }
230
231 continue;
232 }
233
234 /* Stay on the same filesystem */
235 if (s.st_dev != rootdev)
236 continue;
237
238 /* Do not delete read-only files owned by root */
239 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
240 continue;
241
242 free(sub_path);
243 sub_path = NULL;
244
245 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
246 log_error("Out of memory");
247 r = -ENOMEM;
248 goto finish;
249 }
250
251 /* Is there an item configured for this path? */
252 if (hashmap_get(items, sub_path))
253 continue;
254
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100255 if (find_glob(globs, sub_path))
256 continue;
257
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200258 if (S_ISDIR(s.st_mode)) {
259
260 if (mountpoint &&
261 streq(dent->d_name, "lost+found") &&
262 s.st_uid == 0)
263 continue;
264
265 if (maxdepth <= 0)
266 log_warning("Reached max depth on %s.", sub_path);
267 else {
268 DIR *sub_dir;
269 int q;
270
Lennart Poetteringa2477552010-12-28 14:20:21 +0100271 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200272 if (sub_dir == NULL) {
273 if (errno != ENOENT) {
274 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
275 r = -errno;
276 }
277
278 continue;
279 }
280
281 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
282 closedir(sub_dir);
283
284 if (q < 0)
285 r = q;
286 }
287
288 /* Ignore ctime, we change it when deleting */
289 age = MAX(timespec_load(&s.st_mtim),
290 timespec_load(&s.st_atim));
291 if (age >= cutoff)
292 continue;
293
294 log_debug("rmdir '%s'\n", sub_path);
295
296 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
297 if (errno != ENOENT && errno != ENOTEMPTY) {
298 log_error("rmdir(%s): %m", sub_path);
299 r = -errno;
300 }
301 }
302
303 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100304 /* Skip files for which the sticky bit is
305 * set. These are semantics we define, and are
306 * unknown elsewhere. See XDG_RUNTIME_DIR
307 * specification for details. */
308 if (s.st_mode & S_ISVTX)
309 continue;
310
Lennart Poettering17b90522011-02-14 21:55:06 +0100311 if (mountpoint && S_ISREG(s.st_mode)) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200312 if (streq(dent->d_name, ".journal") &&
313 s.st_uid == 0)
314 continue;
315
316 if (streq(dent->d_name, "aquota.user") ||
317 streq(dent->d_name, "aquota.group"))
318 continue;
319 }
320
Lennart Poettering17b90522011-02-14 21:55:06 +0100321 /* Ignore sockets that are listed in /proc/net/unix */
322 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
323 continue;
324
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100325 /* Ignore device nodes */
326 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
327 continue;
328
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200329 age = MAX3(timespec_load(&s.st_mtim),
330 timespec_load(&s.st_atim),
331 timespec_load(&s.st_ctim));
332
333 if (age >= cutoff)
334 continue;
335
336 log_debug("unlink '%s'\n", sub_path);
337
338 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
339 if (errno != ENOENT) {
340 log_error("unlink(%s): %m", sub_path);
341 r = -errno;
342 }
343 }
344
345 deleted = true;
346 }
347 }
348
349finish:
350 if (deleted) {
351 /* Restore original directory timestamps */
352 times[0] = ds->st_atim;
353 times[1] = ds->st_mtim;
354
355 if (futimens(dirfd(d), times) < 0)
356 log_error("utimensat(%s): %m", p);
357 }
358
359 free(sub_path);
360
361 return r;
362}
363
364static int clean_item(Item *i) {
365 DIR *d;
366 struct stat s, ps;
367 bool mountpoint;
368 int r;
369 usec_t cutoff, n;
370
371 assert(i);
372
373 if (i->type != CREATE_DIRECTORY &&
374 i->type != TRUNCATE_DIRECTORY &&
375 i->type != IGNORE_PATH)
376 return 0;
377
378 if (!i->age_set || i->age <= 0)
379 return 0;
380
381 n = now(CLOCK_REALTIME);
382 if (n < i->age)
383 return 0;
384
385 cutoff = n - i->age;
386
387 d = opendir(i->path);
388 if (!d) {
389 if (errno == ENOENT)
390 return 0;
391
392 log_error("Failed to open directory %s: %m", i->path);
393 return -errno;
394 }
395
396 if (fstat(dirfd(d), &s) < 0) {
397 log_error("stat(%s) failed: %m", i->path);
398 r = -errno;
399 goto finish;
400 }
401
402 if (!S_ISDIR(s.st_mode)) {
403 log_error("%s is not a directory.", i->path);
404 r = -ENOTDIR;
405 goto finish;
406 }
407
408 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
409 log_error("stat(%s/..) failed: %m", i->path);
410 r = -errno;
411 goto finish;
412 }
413
414 mountpoint = s.st_dev != ps.st_dev ||
415 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
416
417 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
418
419finish:
420 if (d)
421 closedir(d);
422
423 return r;
424}
425
Michal Schmidt062e01b2011-12-16 18:00:11 +0100426static int item_set_perms(Item *i, const char *path) {
427 /* not using i->path directly because it may be a glob */
428 if (i->mode_set)
429 if (chmod(path, i->mode) < 0) {
430 log_error("chmod(%s) failed: %m", path);
431 return -errno;
432 }
433
434 if (i->uid_set || i->gid_set)
435 if (chown(path,
436 i->uid_set ? i->uid : (uid_t) -1,
437 i->gid_set ? i->gid : (gid_t) -1) < 0) {
438
439 log_error("chown(%s) failed: %m", path);
440 return -errno;
441 }
442
443 return label_fix(path, false);
444}
445
446static int recursive_relabel_children(Item *i, const char *path) {
Michal Schmidta8d88782011-12-15 23:11:07 +0100447 DIR *d;
448 int ret = 0;
449
450 /* This returns the first error we run into, but nevertheless
451 * tries to go on */
452
453 d = opendir(path);
454 if (!d)
455 return errno == ENOENT ? 0 : -errno;
456
457 for (;;) {
458 struct dirent buf, *de;
459 bool is_dir;
460 int r;
461 char *entry_path;
462
463 r = readdir_r(d, &buf, &de);
464 if (r != 0) {
465 if (ret == 0)
466 ret = -r;
467 break;
468 }
469
470 if (!de)
471 break;
472
473 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
474 continue;
475
476 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
477 if (ret == 0)
478 ret = -ENOMEM;
479 continue;
480 }
481
482 if (de->d_type == DT_UNKNOWN) {
483 struct stat st;
484
485 if (lstat(entry_path, &st) < 0) {
486 if (ret == 0 && errno != ENOENT)
487 ret = -errno;
488 free(entry_path);
489 continue;
490 }
491
492 is_dir = S_ISDIR(st.st_mode);
493
494 } else
495 is_dir = de->d_type == DT_DIR;
496
Michal Schmidt062e01b2011-12-16 18:00:11 +0100497 r = item_set_perms(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100498 if (r < 0) {
499 if (ret == 0 && r != -ENOENT)
500 ret = r;
501 free(entry_path);
502 continue;
503 }
504
505 if (is_dir) {
Michal Schmidt062e01b2011-12-16 18:00:11 +0100506 r = recursive_relabel_children(i, entry_path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100507 if (r < 0 && ret == 0)
508 ret = r;
509 }
510
511 free(entry_path);
512 }
513
514 closedir(d);
515
516 return ret;
517}
518
519static int recursive_relabel(Item *i, const char *path) {
520 int r;
521 struct stat st;
522
Michal Schmidt062e01b2011-12-16 18:00:11 +0100523 r = item_set_perms(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100524 if (r < 0)
525 return r;
526
527 if (lstat(path, &st) < 0)
528 return -errno;
529
530 if (S_ISDIR(st.st_mode))
Michal Schmidt062e01b2011-12-16 18:00:11 +0100531 r = recursive_relabel_children(i, path);
Michal Schmidta8d88782011-12-15 23:11:07 +0100532
533 return r;
534}
535
Michal Schmidt99e68c02011-12-15 23:45:26 +0100536static int glob_item(Item *i, int (*action)(Item *, const char *)) {
537 int r = 0, k;
538 glob_t g;
539 char **fn;
540
541 zero(g);
542
543 errno = 0;
544 if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
545
546 if (k != GLOB_NOMATCH) {
547 if (errno != 0)
548 errno = EIO;
549
550 log_error("glob(%s) failed: %m", i->path);
551 return -errno;
552 }
553 }
554
555 STRV_FOREACH(fn, g.gl_pathv)
556 if ((k = action(i, *fn)) < 0)
557 r = k;
558
559 globfree(&g);
560 return r;
561}
562
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200563static int create_item(Item *i) {
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100564 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200565 mode_t u;
566 struct stat st;
567
568 assert(i);
569
570 switch (i->type) {
571
572 case IGNORE_PATH:
573 case REMOVE_PATH:
574 case RECURSIVE_REMOVE_PATH:
575 return 0;
576
577 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100578 case TRUNCATE_FILE:
579 case WRITE_FILE: {
580 int fd, flags;
581
582 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
583 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200584
585 u = umask(0);
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100586 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200587 umask(u);
588
589 if (fd < 0) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100590 if (i->type == WRITE_FILE && errno == ENOENT)
591 break;
592
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200593 log_error("Failed to create file %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100594 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200595 }
596
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100597 if (i->argument) {
598 ssize_t n;
599 size_t l;
600 struct iovec iovec[2];
601 static const char new_line = '\n';
602
603 l = strlen(i->argument);
604
605 zero(iovec);
606 iovec[0].iov_base = i->argument;
607 iovec[0].iov_len = l;
608
609 iovec[1].iov_base = (void*) &new_line;
610 iovec[1].iov_len = 1;
611
612 n = writev(fd, iovec, 2);
613 if (n < 0 || (size_t) n != l+1) {
614 log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short");
615 close_nointr_nofail(fd);
616 return n < 0 ? n : -EIO;
617 }
618 }
619
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100620 close_nointr_nofail(fd);
621
622 if (stat(i->path, &st) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200623 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100624 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200625 }
626
627 if (!S_ISREG(st.st_mode)) {
628 log_error("%s is not a file.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100629 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200630 }
631
Michal Schmidt062e01b2011-12-16 18:00:11 +0100632 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100633 if (r < 0)
634 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200635
636 break;
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100637 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200638
639 case TRUNCATE_DIRECTORY:
640 case CREATE_DIRECTORY:
641
642 u = umask(0);
Kay Sievers33366862011-04-03 22:21:21 +0200643 mkdir_parents(i->path, 0755);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200644 r = mkdir(i->path, i->mode);
645 umask(u);
646
647 if (r < 0 && errno != EEXIST) {
648 log_error("Failed to create directory %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100649 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200650 }
651
652 if (stat(i->path, &st) < 0) {
653 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100654 return -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200655 }
656
657 if (!S_ISDIR(st.st_mode)) {
658 log_error("%s is not a directory.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100659 return -EEXIST;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200660 }
661
Michal Schmidt062e01b2011-12-16 18:00:11 +0100662 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100663 if (r < 0)
664 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200665
666 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200667
668 case CREATE_FIFO:
669
670 u = umask(0);
671 r = mkfifo(i->path, i->mode);
672 umask(u);
673
674 if (r < 0 && errno != EEXIST) {
675 log_error("Failed to create fifo %s: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100676 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200677 }
678
679 if (stat(i->path, &st) < 0) {
680 log_error("stat(%s) failed: %m", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100681 return -errno;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200682 }
683
684 if (!S_ISFIFO(st.st_mode)) {
685 log_error("%s is not a fifo.", i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100686 return -EEXIST;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200687 }
688
Michal Schmidt062e01b2011-12-16 18:00:11 +0100689 r = item_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100690 if (r < 0)
691 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200692
693 break;
Michal Schmidta8d88782011-12-15 23:11:07 +0100694
Lennart Poettering468d7262012-01-17 15:04:12 +0100695 case CREATE_SYMLINK: {
696 char *x;
697
698 r = symlink(i->argument, i->path);
699 if (r < 0 && errno != EEXIST) {
700 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
701 return -errno;
702 }
703
704 r = readlink_malloc(i->path, &x);
705 if (r < 0) {
706 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
707 return -errno;
708 }
709
710 if (!streq(i->argument, x)) {
711 free(x);
712 log_error("%s is not the right symlinks.", i->path);
713 return -EEXIST;
714 }
715
716 free(x);
717 break;
718 }
719
720 case CREATE_BLOCK_DEVICE:
721 case CREATE_CHAR_DEVICE: {
722
723 u = umask(0);
724 r = mknod(i->path, i->mode | (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR), i->major_minor);
725 umask(u);
726
727 if (r < 0 && errno != EEXIST) {
728 log_error("Failed to create device node %s: %m", i->path);
729 return -errno;
730 }
731
732 if (stat(i->path, &st) < 0) {
733 log_error("stat(%s) failed: %m", i->path);
734 return -errno;
735 }
736
737 if (i->type == CREATE_BLOCK_DEVICE ? !S_ISBLK(st.st_mode) : !S_ISCHR(st.st_mode)) {
738 log_error("%s is not a device node.", i->path);
739 return -EEXIST;
740 }
741
742 r = item_set_perms(i, i->path);
743 if (r < 0)
744 return r;
745
746 break;
747 }
748
Michal Schmidt777b87e2011-12-16 18:27:35 +0100749 case RELABEL_PATH:
750
751 r = glob_item(i, item_set_perms);
752 if (r < 0)
753 return 0;
754 break;
755
Michal Schmidta8d88782011-12-15 23:11:07 +0100756 case RECURSIVE_RELABEL_PATH:
757
758 r = glob_item(i, recursive_relabel);
759 if (r < 0)
760 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200761 }
762
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200763 log_debug("%s created successfully.", i->path);
764
Michal Schmidtf05bc3f2011-12-15 23:44:23 +0100765 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200766}
767
Michal Schmidta0896122011-12-15 21:32:50 +0100768static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200769 int r;
770
771 assert(i);
772
773 switch (i->type) {
774
775 case CREATE_FILE:
776 case TRUNCATE_FILE:
777 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200778 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100779 case CREATE_SYMLINK:
780 case CREATE_BLOCK_DEVICE:
781 case CREATE_CHAR_DEVICE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200782 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100783 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100784 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100785 case WRITE_FILE:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200786 break;
787
788 case REMOVE_PATH:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100789 if (remove(instance) < 0 && errno != ENOENT) {
790 log_error("remove(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200791 return -errno;
792 }
793
794 break;
795
796 case TRUNCATE_DIRECTORY:
797 case RECURSIVE_REMOVE_PATH:
Lennart Poettering468d7262012-01-17 15:04:12 +0100798 r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
799 if (r < 0 && r != -ENOENT) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100800 log_error("rm_rf(%s): %s", instance, strerror(-r));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200801 return r;
802 }
803
804 break;
805 }
806
807 return 0;
808}
809
Michal Schmidta0896122011-12-15 21:32:50 +0100810static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +0100811 int r = 0;
812
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100813 assert(i);
814
815 switch (i->type) {
816
817 case CREATE_FILE:
818 case TRUNCATE_FILE:
819 case CREATE_DIRECTORY:
Lennart Poetteringee17ee72011-07-12 03:56:56 +0200820 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +0100821 case CREATE_SYMLINK:
822 case CREATE_CHAR_DEVICE:
823 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100824 case IGNORE_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +0100825 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +0100826 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100827 case WRITE_FILE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100828 break;
829
830 case REMOVE_PATH:
831 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +0100832 case RECURSIVE_REMOVE_PATH:
833 r = glob_item(i, remove_item_instance);
834 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100835 }
836
Michal Schmidt99e68c02011-12-15 23:45:26 +0100837 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100838}
839
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200840static int process_item(Item *i) {
841 int r, q, p;
842
843 assert(i);
844
845 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +0100846 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200847 p = arg_clean ? clean_item(i) : 0;
848
849 if (r < 0)
850 return r;
851
852 if (q < 0)
853 return q;
854
855 return p;
856}
857
858static void item_free(Item *i) {
859 assert(i);
860
861 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +0100862 free(i->argument);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200863 free(i);
864}
865
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200866static bool item_equal(Item *a, Item *b) {
867 assert(a);
868 assert(b);
869
870 if (!streq_ptr(a->path, b->path))
871 return false;
872
873 if (a->type != b->type)
874 return false;
875
876 if (a->uid_set != b->uid_set ||
877 (a->uid_set && a->uid != b->uid))
878 return false;
879
880 if (a->gid_set != b->gid_set ||
881 (a->gid_set && a->gid != b->gid))
882 return false;
883
884 if (a->mode_set != b->mode_set ||
885 (a->mode_set && a->mode != b->mode))
886 return false;
887
888 if (a->age_set != b->age_set ||
889 (a->age_set && a->age != b->age))
890 return false;
891
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100892 if ((a->type == CREATE_FILE ||
893 a->type == TRUNCATE_FILE ||
894 a->type == WRITE_FILE ||
895 a->type == CREATE_SYMLINK) &&
Lennart Poettering1733ca52012-01-22 18:19:24 +0100896 !streq_ptr(a->argument, b->argument))
Lennart Poettering468d7262012-01-17 15:04:12 +0100897 return false;
898
899 if ((a->type == CREATE_CHAR_DEVICE ||
900 a->type == CREATE_BLOCK_DEVICE) &&
901 a->major_minor != b->major_minor)
902 return false;
903
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200904 return true;
905}
906
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100907static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200908 Item *i, *existing;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200909 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
Michal Schmidt66ccd032011-12-15 21:31:14 +0100910 char type;
Lennart Poetteringbfe95f32011-04-08 04:49:43 +0200911 Hashmap *h;
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100912 int r, n = -1;
Lennart Poettering5008d582010-09-28 02:34:02 +0200913
914 assert(fname);
915 assert(line >= 1);
916 assert(buffer);
917
Lennart Poettering468d7262012-01-17 15:04:12 +0100918 i = new0(Item, 1);
919 if (!i) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200920 log_error("Out of memory");
921 return -ENOMEM;
922 }
923
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100924 if (sscanf(buffer,
925 "%c "
926 "%ms "
927 "%ms "
928 "%ms "
929 "%ms "
Lennart Poettering468d7262012-01-17 15:04:12 +0100930 "%ms "
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100931 "%n",
Michal Schmidt66ccd032011-12-15 21:31:14 +0100932 &type,
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100933 &i->path,
934 &mode,
935 &user,
936 &group,
Lennart Poettering468d7262012-01-17 15:04:12 +0100937 &age,
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100938 &n) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200939 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200940 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200941 goto finish;
942 }
943
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100944 if (n >= 0) {
945 n += strspn(buffer+n, WHITESPACE);
946 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
947 i->argument = unquote(buffer+n, "\"");
948 if (!i->argument) {
949 log_error("Out of memory");
950 return -ENOMEM;
951 }
952 }
953 }
954
Michal Schmidt777b87e2011-12-16 18:27:35 +0100955 switch(type) {
Lennart Poettering468d7262012-01-17 15:04:12 +0100956
Michal Schmidt777b87e2011-12-16 18:27:35 +0100957 case CREATE_FILE:
958 case TRUNCATE_FILE:
959 case CREATE_DIRECTORY:
960 case TRUNCATE_DIRECTORY:
961 case CREATE_FIFO:
962 case IGNORE_PATH:
963 case REMOVE_PATH:
964 case RECURSIVE_REMOVE_PATH:
965 case RELABEL_PATH:
966 case RECURSIVE_RELABEL_PATH:
967 break;
Lennart Poettering468d7262012-01-17 15:04:12 +0100968
969 case CREATE_SYMLINK:
970 if (!i->argument) {
971 log_error("[%s:%u] Symlink file requires argument.", fname, line);
972 r = -EBADMSG;
973 goto finish;
974 }
975 break;
976
Lennart Poettering31ed59c2012-01-18 16:39:04 +0100977 case WRITE_FILE:
978 if (!i->argument) {
979 log_error("[%s:%u] Write file requires argument.", fname, line);
980 r = -EBADMSG;
981 goto finish;
982 }
983 break;
984
Lennart Poettering468d7262012-01-17 15:04:12 +0100985 case CREATE_CHAR_DEVICE:
986 case CREATE_BLOCK_DEVICE: {
987 unsigned major, minor;
988
989 if (!i->argument) {
990 log_error("[%s:%u] Device file requires argument.", fname, line);
991 r = -EBADMSG;
992 goto finish;
993 }
994
995 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
996 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
997 r = -EBADMSG;
998 goto finish;
999 }
1000
1001 i->major_minor = makedev(major, minor);
1002 break;
1003 }
1004
Michal Schmidt777b87e2011-12-16 18:27:35 +01001005 default:
Michal Schmidta8d88782011-12-15 23:11:07 +01001006 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001007 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001008 goto finish;
1009 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001010
Michal Schmidta8d88782011-12-15 23:11:07 +01001011 i->type = type;
Lennart Poettering5008d582010-09-28 02:34:02 +02001012
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001013 if (!path_is_absolute(i->path)) {
1014 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1015 r = -EBADMSG;
1016 goto finish;
1017 }
1018
1019 path_kill_slashes(i->path);
1020
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001021 if (arg_prefix && !path_startswith(i->path, arg_prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001022 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001023 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001024 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001025
1026 if (user && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001027 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001028
Lennart Poettering4b678342011-07-23 01:17:59 +02001029 r = get_user_creds(&u, &i->uid, NULL, NULL);
1030 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001031 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1032 goto finish;
1033 }
1034
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001035 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001036 }
1037
1038 if (group && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001039 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001040
Lennart Poettering4b678342011-07-23 01:17:59 +02001041 r = get_group_creds(&g, &i->gid);
1042 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001043 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1044 goto finish;
1045 }
1046
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001047 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001048 }
1049
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001050 if (mode && !streq(mode, "-")) {
1051 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02001052
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001053 if (sscanf(mode, "%o", &m) != 1) {
1054 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1055 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +02001056 goto finish;
1057 }
1058
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001059 i->mode = m;
1060 i->mode_set = true;
1061 } else
Lennart Poettering468d7262012-01-17 15:04:12 +01001062 i->mode =
1063 i->type == CREATE_DIRECTORY ||
1064 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001065
1066 if (age && !streq(age, "-")) {
1067 if (parse_usec(age, &i->age) < 0) {
1068 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1069 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001070 goto finish;
1071 }
1072
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001073 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001074 }
1075
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001076 h = needs_glob(i->type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01001077
Lennart Poettering468d7262012-01-17 15:04:12 +01001078 existing = hashmap_get(h, i->path);
1079 if (existing) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001080
1081 /* Two identical items are fine */
1082 if (!item_equal(existing, i))
1083 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1084
1085 r = 0;
1086 goto finish;
1087 }
1088
Lennart Poettering468d7262012-01-17 15:04:12 +01001089 r = hashmap_put(h, i->path, i);
1090 if (r < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001091 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001092 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001093 }
Lennart Poettering5008d582010-09-28 02:34:02 +02001094
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001095 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001096 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001097
1098finish:
Lennart Poettering5008d582010-09-28 02:34:02 +02001099 free(user);
1100 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001101 free(mode);
1102 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +02001103
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001104 if (i)
1105 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +02001106
1107 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001108}
1109
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001110static int help(void) {
1111
Lennart Poettering522d4a42011-02-13 15:08:15 +01001112 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1113 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001114 " -h --help Show this help\n"
1115 " --create Create marked files/directories\n"
1116 " --clean Clean up marked directories\n"
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001117 " --remove Remove marked files/directories\n"
Lennart Poettering522d4a42011-02-13 15:08:15 +01001118 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001119 program_invocation_short_name);
1120
1121 return 0;
1122}
1123
1124static int parse_argv(int argc, char *argv[]) {
1125
1126 enum {
1127 ARG_CREATE,
1128 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001129 ARG_REMOVE,
1130 ARG_PREFIX
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001131 };
1132
1133 static const struct option options[] = {
1134 { "help", no_argument, NULL, 'h' },
1135 { "create", no_argument, NULL, ARG_CREATE },
1136 { "clean", no_argument, NULL, ARG_CLEAN },
1137 { "remove", no_argument, NULL, ARG_REMOVE },
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001138 { "prefix", required_argument, NULL, ARG_PREFIX },
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001139 { NULL, 0, NULL, 0 }
1140 };
1141
1142 int c;
1143
1144 assert(argc >= 0);
1145 assert(argv);
1146
1147 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1148
1149 switch (c) {
1150
1151 case 'h':
1152 help();
1153 return 0;
1154
1155 case ARG_CREATE:
1156 arg_create = true;
1157 break;
1158
1159 case ARG_CLEAN:
1160 arg_clean = true;
1161 break;
1162
1163 case ARG_REMOVE:
1164 arg_remove = true;
1165 break;
1166
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001167 case ARG_PREFIX:
1168 arg_prefix = optarg;
1169 break;
1170
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001171 case '?':
1172 return -EINVAL;
1173
1174 default:
1175 log_error("Unknown option code %c", c);
1176 return -EINVAL;
1177 }
1178 }
1179
1180 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01001181 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001182 return -EINVAL;
1183 }
1184
1185 return 1;
1186}
1187
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001188static int read_config_file(const char *fn, bool ignore_enoent) {
1189 FILE *f;
1190 unsigned v = 0;
1191 int r = 0;
1192
1193 assert(fn);
1194
Lennart Poettering468d7262012-01-17 15:04:12 +01001195 f = fopen(fn, "re");
1196 if (!f) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001197
1198 if (ignore_enoent && errno == ENOENT)
1199 return 0;
1200
1201 log_error("Failed to open %s: %m", fn);
1202 return -errno;
1203 }
1204
Kay Sievers772f8372011-04-25 21:38:21 +02001205 log_debug("apply: %s\n", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001206 for (;;) {
1207 char line[LINE_MAX], *l;
1208 int k;
1209
1210 if (!(fgets(line, sizeof(line), f)))
1211 break;
1212
1213 v++;
1214
1215 l = strstrip(line);
1216 if (*l == '#' || *l == 0)
1217 continue;
1218
1219 if ((k = parse_line(fn, v, l)) < 0)
1220 if (r == 0)
1221 r = k;
1222 }
1223
1224 if (ferror(f)) {
1225 log_error("Failed to read from file %s: %m", fn);
1226 if (r == 0)
1227 r = -EIO;
1228 }
1229
1230 fclose(f);
1231
1232 return r;
1233}
1234
Lennart Poettering5008d582010-09-28 02:34:02 +02001235int main(int argc, char *argv[]) {
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001236 int r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001237 Item *i;
1238 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02001239
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01001240 r = parse_argv(argc, argv);
1241 if (r <= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001242 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1243
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01001244 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02001245 log_parse_environment();
1246 log_open();
1247
Lennart Poettering4c126262011-08-01 20:52:18 +02001248 umask(0022);
1249
Lennart Poettering5008d582010-09-28 02:34:02 +02001250 label_init();
1251
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001252 items = hashmap_new(string_hash_func, string_compare_func);
1253 globs = hashmap_new(string_hash_func, string_compare_func);
1254
1255 if (!items || !globs) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001256 log_error("Out of memory");
1257 r = EXIT_FAILURE;
1258 goto finish;
1259 }
1260
Lennart Poettering5008d582010-09-28 02:34:02 +02001261 r = EXIT_SUCCESS;
1262
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001263 if (optind < argc) {
1264 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02001265
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001266 for (j = optind; j < argc; j++)
1267 if (read_config_file(argv[j], false) < 0)
1268 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001269
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001270 } else {
Kay Sievers772f8372011-04-25 21:38:21 +02001271 char **files, **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02001272
Kay Sievers44143302011-04-28 23:51:24 +02001273 r = conf_files_list(&files, ".conf",
1274 "/run/tmpfiles.d",
1275 "/etc/tmpfiles.d",
Kay Sievers223a3552011-04-30 20:31:33 +02001276 "/usr/local/lib/tmpfiles.d",
Kay Sievers44143302011-04-28 23:51:24 +02001277 "/usr/lib/tmpfiles.d",
1278 NULL);
1279 if (r < 0) {
1280 r = EXIT_FAILURE;
1281 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1282 goto finish;
1283 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001284
Kay Sievers772f8372011-04-25 21:38:21 +02001285 STRV_FOREACH(f, files) {
1286 if (read_config_file(*f, true) < 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001287 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +02001288 }
1289
Kay Sievers772f8372011-04-25 21:38:21 +02001290 strv_free(files);
Lennart Poettering5008d582010-09-28 02:34:02 +02001291 }
1292
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001293 HASHMAP_FOREACH(i, globs, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001294 process_item(i);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001295
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001296 HASHMAP_FOREACH(i, items, iterator)
Lennart Poettering21bdae12011-07-02 01:44:49 +02001297 process_item(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001298
Lennart Poettering5008d582010-09-28 02:34:02 +02001299finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001300 while ((i = hashmap_steal_first(items)))
1301 item_free(i);
1302
Lennart Poettering17b90522011-02-14 21:55:06 +01001303 while ((i = hashmap_steal_first(globs)))
1304 item_free(i);
1305
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001306 hashmap_free(items);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001307 hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02001308
Lennart Poettering17b90522011-02-14 21:55:06 +01001309 set_free_free(unix_sockets);
1310
Lennart Poettering29003cf2010-10-19 19:36:45 +02001311 label_finish();
1312
Lennart Poettering5008d582010-09-28 02:34:02 +02001313 return r;
1314}