blob: 73246bdd6773a605ea8f388a9bbf75e7c6acc8f9 [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 Poettering5008d582010-09-28 02:34:02 +020039
40#include "log.h"
41#include "util.h"
42#include "strv.h"
43#include "label.h"
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020044#include "set.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020045
Andreas Jaeger01000472010-09-29 10:08:24 +020046/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020047 * them in the file system. This is intended to be used to create
48 * properly owned directories beneath /tmp, /var/tmp, /var/run and
49 * /var/lock which are volatile and hence need to be recreated on
50 * bootup. */
51
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020052enum {
53 CREATE_FILE = 'f',
54 TRUNCATE_FILE = 'F',
55 CREATE_DIRECTORY = 'd',
56 TRUNCATE_DIRECTORY = 'D',
57 IGNORE_PATH = 'x',
58 REMOVE_PATH = 'r',
59 RECURSIVE_REMOVE_PATH = 'R'
60};
61
62typedef struct Item {
Lennart Poettering5008d582010-09-28 02:34:02 +020063 char type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020064
65 char *path;
Lennart Poettering5008d582010-09-28 02:34:02 +020066 uid_t uid;
67 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020068 mode_t mode;
69 usec_t age;
70
71 bool uid_set:1;
72 bool gid_set:1;
73 bool mode_set:1;
74 bool age_set:1;
75} Item;
76
77static Hashmap *items = NULL;
78
79static bool arg_create = false;
80static bool arg_clean = false;
81static bool arg_remove = false;
82
83#define MAX_DEPTH 256
84
85static int dir_cleanup(
86 const char *p,
87 DIR *d,
88 const struct stat *ds,
89 usec_t cutoff,
90 dev_t rootdev,
91 bool mountpoint,
92 int maxdepth)
93{
94 struct dirent *dent;
95 struct timespec times[2];
96 bool deleted = false;
97 char *sub_path = NULL;
98 int r = 0;
99
100 while ((dent = readdir(d))) {
101 struct stat s;
102 usec_t age;
103
104 if (streq(dent->d_name, ".") ||
105 streq(dent->d_name, ".."))
106 continue;
107
108 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
109
110 if (errno != ENOENT) {
111 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
112 r = -errno;
113 }
114
115 continue;
116 }
117
118 /* Stay on the same filesystem */
119 if (s.st_dev != rootdev)
120 continue;
121
122 /* Do not delete read-only files owned by root */
123 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
124 continue;
125
126 free(sub_path);
127 sub_path = NULL;
128
129 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
130 log_error("Out of memory");
131 r = -ENOMEM;
132 goto finish;
133 }
134
135 /* Is there an item configured for this path? */
136 if (hashmap_get(items, sub_path))
137 continue;
138
139 if (S_ISDIR(s.st_mode)) {
140
141 if (mountpoint &&
142 streq(dent->d_name, "lost+found") &&
143 s.st_uid == 0)
144 continue;
145
146 if (maxdepth <= 0)
147 log_warning("Reached max depth on %s.", sub_path);
148 else {
149 DIR *sub_dir;
150 int q;
151
152 sub_dir = xopendirat(dirfd(d), dent->d_name);
153 if (sub_dir == NULL) {
154 if (errno != ENOENT) {
155 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
156 r = -errno;
157 }
158
159 continue;
160 }
161
162 q = dir_cleanup(sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1);
163 closedir(sub_dir);
164
165 if (q < 0)
166 r = q;
167 }
168
169 /* Ignore ctime, we change it when deleting */
170 age = MAX(timespec_load(&s.st_mtim),
171 timespec_load(&s.st_atim));
172 if (age >= cutoff)
173 continue;
174
175 log_debug("rmdir '%s'\n", sub_path);
176
177 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
178 if (errno != ENOENT && errno != ENOTEMPTY) {
179 log_error("rmdir(%s): %m", sub_path);
180 r = -errno;
181 }
182 }
183
184 } else {
185 if (mountpoint) {
186 if (streq(dent->d_name, ".journal") &&
187 s.st_uid == 0)
188 continue;
189
190 if (streq(dent->d_name, "aquota.user") ||
191 streq(dent->d_name, "aquota.group"))
192 continue;
193 }
194
195 age = MAX3(timespec_load(&s.st_mtim),
196 timespec_load(&s.st_atim),
197 timespec_load(&s.st_ctim));
198
199 if (age >= cutoff)
200 continue;
201
202 log_debug("unlink '%s'\n", sub_path);
203
204 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
205 if (errno != ENOENT) {
206 log_error("unlink(%s): %m", sub_path);
207 r = -errno;
208 }
209 }
210
211 deleted = true;
212 }
213 }
214
215finish:
216 if (deleted) {
217 /* Restore original directory timestamps */
218 times[0] = ds->st_atim;
219 times[1] = ds->st_mtim;
220
221 if (futimens(dirfd(d), times) < 0)
222 log_error("utimensat(%s): %m", p);
223 }
224
225 free(sub_path);
226
227 return r;
228}
229
230static int clean_item(Item *i) {
231 DIR *d;
232 struct stat s, ps;
233 bool mountpoint;
234 int r;
235 usec_t cutoff, n;
236
237 assert(i);
238
239 if (i->type != CREATE_DIRECTORY &&
240 i->type != TRUNCATE_DIRECTORY &&
241 i->type != IGNORE_PATH)
242 return 0;
243
244 if (!i->age_set || i->age <= 0)
245 return 0;
246
247 n = now(CLOCK_REALTIME);
248 if (n < i->age)
249 return 0;
250
251 cutoff = n - i->age;
252
253 d = opendir(i->path);
254 if (!d) {
255 if (errno == ENOENT)
256 return 0;
257
258 log_error("Failed to open directory %s: %m", i->path);
259 return -errno;
260 }
261
262 if (fstat(dirfd(d), &s) < 0) {
263 log_error("stat(%s) failed: %m", i->path);
264 r = -errno;
265 goto finish;
266 }
267
268 if (!S_ISDIR(s.st_mode)) {
269 log_error("%s is not a directory.", i->path);
270 r = -ENOTDIR;
271 goto finish;
272 }
273
274 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
275 log_error("stat(%s/..) failed: %m", i->path);
276 r = -errno;
277 goto finish;
278 }
279
280 mountpoint = s.st_dev != ps.st_dev ||
281 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
282
283 r = dir_cleanup(i->path, d, &s, cutoff, s.st_dev, mountpoint, MAX_DEPTH);
284
285finish:
286 if (d)
287 closedir(d);
288
289 return r;
290}
291
292static int create_item(Item *i) {
293 int fd = -1, r;
294 mode_t u;
295 struct stat st;
296
297 assert(i);
298
299 switch (i->type) {
300
301 case IGNORE_PATH:
302 case REMOVE_PATH:
303 case RECURSIVE_REMOVE_PATH:
304 return 0;
305
306 case CREATE_FILE:
307 case TRUNCATE_FILE:
308
309 u = umask(0);
310 fd = open(i->path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW|
311 (i->type == TRUNCATE_FILE ? O_TRUNC : 0), i->mode);
312 umask(u);
313
314 if (fd < 0) {
315 log_error("Failed to create file %s: %m", i->path);
316 r = -errno;
317 goto finish;
318 }
319
320 if (fstat(fd, &st) < 0) {
321 log_error("stat(%s) failed: %m", i->path);
322 r = -errno;
323 goto finish;
324 }
325
326 if (!S_ISREG(st.st_mode)) {
327 log_error("%s is not a file.", i->path);
328 r = -EEXIST;
329 goto finish;
330 }
331
332 if (i->mode_set)
333 if (fchmod(fd, i->mode) < 0) {
334 log_error("chmod(%s) failed: %m", i->path);
335 r = -errno;
336 goto finish;
337 }
338
339 if (i->uid_set || i->gid_set)
340 if (fchown(fd,
341 i->uid_set ? i->uid : (uid_t) -1,
342 i->gid_set ? i->gid : (gid_t) -1) < 0) {
343 log_error("chown(%s) failed: %m", i->path);
344 r = -errno;
345 goto finish;
346 }
347
348 break;
349
350 case TRUNCATE_DIRECTORY:
351 case CREATE_DIRECTORY:
352
353 u = umask(0);
354 r = mkdir(i->path, i->mode);
355 umask(u);
356
357 if (r < 0 && errno != EEXIST) {
358 log_error("Failed to create directory %s: %m", i->path);
359 r = -errno;
360 goto finish;
361 }
362
363 if (stat(i->path, &st) < 0) {
364 log_error("stat(%s) failed: %m", i->path);
365 r = -errno;
366 goto finish;
367 }
368
369 if (!S_ISDIR(st.st_mode)) {
370 log_error("%s is not a directory.", i->path);
371 r = -EEXIST;
372 goto finish;
373 }
374
375 if (i->mode_set)
376 if (chmod(i->path, i->mode) < 0) {
377 log_error("chmod(%s) failed: %m", i->path);
378 r = -errno;
379 goto finish;
380 }
381
382 if (i->uid_set || i->gid_set)
383 if (chown(i->path,
384 i->uid_set ? i->uid : (uid_t) -1,
385 i->gid_set ? i->gid : (gid_t) -1) < 0) {
386
387 log_error("chown(%s) failed: %m", i->path);
388 r = -errno;
389 goto finish;
390 }
391
392 break;
393 }
394
395 if ((r = label_fix(i->path)) < 0)
396 goto finish;
397
398 log_debug("%s created successfully.", i->path);
399
400finish:
401 if (fd >= 0)
402 close_nointr_nofail(fd);
403
404 return r;
405}
406
407static int remove_item(Item *i) {
408 int r;
409
410 assert(i);
411
412 switch (i->type) {
413
414 case CREATE_FILE:
415 case TRUNCATE_FILE:
416 case CREATE_DIRECTORY:
417 case IGNORE_PATH:
418 break;
419
420 case REMOVE_PATH:
421 if (remove(i->path) < 0 && errno != ENOENT) {
422 log_error("remove(%s): %m", i->path);
423 return -errno;
424 }
425
426 break;
427
428 case TRUNCATE_DIRECTORY:
429 case RECURSIVE_REMOVE_PATH:
430 if ((r = rm_rf(i->path, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
431 r != -ENOENT) {
432 log_error("rm_rf(%s): %s", i->path, strerror(-r));
433 return r;
434 }
435
436 break;
437 }
438
439 return 0;
440}
441
442static int process_item(Item *i) {
443 int r, q, p;
444
445 assert(i);
446
447 r = arg_create ? create_item(i) : 0;
448 q = arg_remove ? remove_item(i) : 0;
449 p = arg_clean ? clean_item(i) : 0;
450
451 if (r < 0)
452 return r;
453
454 if (q < 0)
455 return q;
456
457 return p;
458}
459
460static void item_free(Item *i) {
461 assert(i);
462
463 free(i->path);
464 free(i);
465}
466
467static int parse_line(const char *fname, unsigned line, const char *buffer, const char *prefix) {
468 Item *i;
469 char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
470 int r, n;
Lennart Poettering5008d582010-09-28 02:34:02 +0200471
472 assert(fname);
473 assert(line >= 1);
474 assert(buffer);
475
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200476 if (!(i = new0(Item, 1))) {
477 log_error("Out of memory");
478 return -ENOMEM;
479 }
480
Lennart Poettering5008d582010-09-28 02:34:02 +0200481 if ((n = sscanf(buffer,
482 "%c "
483 "%ms "
Lennart Poettering5008d582010-09-28 02:34:02 +0200484 "%ms "
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200485 "%ms "
486 "%ms "
487 "%ms",
488 &i->type,
489 &i->path,
Lennart Poettering5008d582010-09-28 02:34:02 +0200490 &mode,
491 &user,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200492 &group,
493 &age)) < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200494 log_error("[%s:%u] Syntax error.", fname, line);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200495 r = -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +0200496 goto finish;
497 }
498
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200499 if (i->type != CREATE_FILE &&
500 i->type != CREATE_DIRECTORY &&
501 i->type != TRUNCATE_FILE &&
502 i->type != IGNORE_PATH &&
503 i->type != REMOVE_PATH &&
504 i->type != RECURSIVE_REMOVE_PATH) {
505 log_error("[%s:%u] Unknown file type '%c'.", fname, line, i->type);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200506 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200507 goto finish;
508 }
509
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200510 if (!path_is_absolute(i->path)) {
511 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
512 r = -EBADMSG;
513 goto finish;
514 }
515
516 path_kill_slashes(i->path);
517
518 if (prefix && !path_startswith(i->path, prefix)) {
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200519 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200520 goto finish;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200521 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200522
523 if (user && !streq(user, "-")) {
524 unsigned long lu;
525 struct passwd *p;
526
527 if (streq(user, "root") || streq(user, "0"))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200528 i->uid = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200529 else if (safe_atolu(user, &lu) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200530 i->uid = (uid_t) lu;
Lennart Poettering5008d582010-09-28 02:34:02 +0200531 else if ((p = getpwnam(user)))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200532 i->uid = p->pw_uid;
Lennart Poettering5008d582010-09-28 02:34:02 +0200533 else {
534 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200535 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200536 goto finish;
537 }
538
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200539 i->uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200540 }
541
542 if (group && !streq(group, "-")) {
543 unsigned long lu;
544 struct group *g;
545
546 if (streq(group, "root") || streq(group, "0"))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200547 i->gid = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200548 else if (safe_atolu(group, &lu) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200549 i->gid = (gid_t) lu;
Lennart Poettering5008d582010-09-28 02:34:02 +0200550 else if ((g = getgrnam(group)))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200551 i->gid = g->gr_gid;
Lennart Poettering5008d582010-09-28 02:34:02 +0200552 else {
553 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200554 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200555 goto finish;
556 }
557
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200558 i->gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200559 }
560
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200561 if (mode && !streq(mode, "-")) {
562 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +0200563
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200564 if (sscanf(mode, "%o", &m) != 1) {
565 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
566 r = -ENOENT;
Lennart Poettering5008d582010-09-28 02:34:02 +0200567 goto finish;
568 }
569
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200570 i->mode = m;
571 i->mode_set = true;
572 } else
573 i->mode = i->type == CREATE_DIRECTORY ? 0755 : 0644;
574
575 if (age && !streq(age, "-")) {
576 if (parse_usec(age, &i->age) < 0) {
577 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
578 r = -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +0200579 goto finish;
580 }
581
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200582 i->age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +0200583 }
584
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200585 if ((r = hashmap_put(items, i->path, i)) < 0) {
586 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200587 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200588 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200589
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200590 i = NULL;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200591 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200592
593finish:
Lennart Poettering5008d582010-09-28 02:34:02 +0200594 free(user);
595 free(group);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200596 free(mode);
597 free(age);
Lennart Poettering5008d582010-09-28 02:34:02 +0200598
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200599 if (i)
600 item_free(i);
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200601
602 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +0200603}
604
605static int scandir_filter(const struct dirent *d) {
606 assert(d);
607
608 if (ignore_file(d->d_name))
609 return 0;
610
611 if (d->d_type != DT_REG &&
612 d->d_type != DT_LNK)
613 return 0;
614
615 return endswith(d->d_name, ".conf");
616}
617
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200618static int help(void) {
619
620 printf("%s [OPTIONS...]\n\n"
621 "Create and clean up temporary directories.\n\n"
622 " -h --help Show this help\n"
623 " --create Create marked files/directories\n"
624 " --clean Clean up marked directories\n"
625 " --remove Remove marked files/directories\n",
626 program_invocation_short_name);
627
628 return 0;
629}
630
631static int parse_argv(int argc, char *argv[]) {
632
633 enum {
634 ARG_CREATE,
635 ARG_CLEAN,
636 ARG_REMOVE
637 };
638
639 static const struct option options[] = {
640 { "help", no_argument, NULL, 'h' },
641 { "create", no_argument, NULL, ARG_CREATE },
642 { "clean", no_argument, NULL, ARG_CLEAN },
643 { "remove", no_argument, NULL, ARG_REMOVE },
644 { NULL, 0, NULL, 0 }
645 };
646
647 int c;
648
649 assert(argc >= 0);
650 assert(argv);
651
652 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
653
654 switch (c) {
655
656 case 'h':
657 help();
658 return 0;
659
660 case ARG_CREATE:
661 arg_create = true;
662 break;
663
664 case ARG_CLEAN:
665 arg_clean = true;
666 break;
667
668 case ARG_REMOVE:
669 arg_remove = true;
670 break;
671
672 case '?':
673 return -EINVAL;
674
675 default:
676 log_error("Unknown option code %c", c);
677 return -EINVAL;
678 }
679 }
680
681 if (!arg_clean && !arg_create && !arg_remove) {
682 help();
683 return -EINVAL;
684 }
685
686 return 1;
687}
688
Lennart Poettering5008d582010-09-28 02:34:02 +0200689int main(int argc, char *argv[]) {
690 struct dirent **de = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200691 int r, n, j;
Lennart Poettering5008d582010-09-28 02:34:02 +0200692 const char *prefix = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200693 Item *i;
694 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +0200695
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200696 if ((r = parse_argv(argc, argv)) <= 0)
697 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
698
699 if (optind < argc)
700 prefix = argv[optind];
Lennart Poettering5008d582010-09-28 02:34:02 +0200701 else
702 prefix = "/";
703
704 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
705 log_parse_environment();
706 log_open();
707
708 label_init();
709
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200710 if (!(items = hashmap_new(string_hash_func, string_compare_func))) {
711 log_error("Out of memory");
712 r = EXIT_FAILURE;
713 goto finish;
714 }
715
Lennart Poettering7fcde282010-09-28 22:32:27 +0200716 if ((n = scandir("/etc/tmpfiles.d/", &de, scandir_filter, alphasort)) < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200717
718 if (errno == ENOENT)
719 r = EXIT_SUCCESS;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200720 else {
Lennart Poettering7fcde282010-09-28 22:32:27 +0200721 log_error("Failed to enumerate /etc/tmpfiles.d/ files: %m");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200722 r = EXIT_FAILURE;
723 }
Lennart Poettering5008d582010-09-28 02:34:02 +0200724
725 goto finish;
726 }
727
728 r = EXIT_SUCCESS;
729
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200730 for (j = 0; j < n; j++) {
Lennart Poettering5008d582010-09-28 02:34:02 +0200731 int k;
732 char *fn;
733 FILE *f;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200734 unsigned v;
Lennart Poettering5008d582010-09-28 02:34:02 +0200735
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200736 k = asprintf(&fn, "/etc/tmpfiles.d/%s", de[j]->d_name);
737 free(de[j]);
Lennart Poettering5008d582010-09-28 02:34:02 +0200738
739 if (k < 0) {
740 log_error("Failed to allocate file name.");
741 r = EXIT_FAILURE;
742 continue;
743 }
744
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200745 if (!(f = fopen(fn, "re"))) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200746
747 if (errno != ENOENT) {
748 log_error("Failed to open %s: %m", fn);
749 r = EXIT_FAILURE;
750 }
751
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200752 free(fn);
Lennart Poettering5008d582010-09-28 02:34:02 +0200753 continue;
754 }
755
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200756 v = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +0200757 for (;;) {
758 char line[LINE_MAX], *l;
759
760 if (!(fgets(line, sizeof(line), f)))
761 break;
762
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200763 v++;
Lennart Poettering5008d582010-09-28 02:34:02 +0200764
765 l = strstrip(line);
766 if (*l == '#' || *l == 0)
767 continue;
768
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200769 if (parse_line(fn, v, l, prefix) < 0)
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200770 r = EXIT_FAILURE;
Lennart Poettering5008d582010-09-28 02:34:02 +0200771 }
772
773 if (ferror(f)) {
774 r = EXIT_FAILURE;
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200775 log_error("Failed to read from file %s: %m", fn);
Lennart Poettering5008d582010-09-28 02:34:02 +0200776 }
777
Lennart Poettering4aa8b152010-09-28 22:32:05 +0200778 free(fn);
779
Lennart Poettering5008d582010-09-28 02:34:02 +0200780 fclose(f);
781 }
782
783 free(de);
784
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200785 HASHMAP_FOREACH(i, items, iterator)
786 if (process_item(i) < 0)
787 r = EXIT_FAILURE;
788
Lennart Poettering5008d582010-09-28 02:34:02 +0200789finish:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200790 while ((i = hashmap_steal_first(items)))
791 item_free(i);
792
793 hashmap_free(items);
Lennart Poettering5008d582010-09-28 02:34:02 +0200794
Lennart Poettering29003cf2010-10-19 19:36:45 +0200795 label_finish();
796
Lennart Poettering5008d582010-09-28 02:34:02 +0200797 return r;
798}