1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering, Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 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
11 (at your option) any later version.
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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
37 #include <sys/types.h>
38 #include <sys/param.h>
41 #include <sys/capability.h>
48 #include "path-util.h"
52 #include "conf-files.h"
53 #include "capability.h"
54 #include "specifier.h"
56 /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
57 * them in the file system. This is intended to be used to create
58 * properly owned directories beneath /tmp, /var/tmp, /run, which are
59 * volatile and hence need to be recreated on bootup. */
61 typedef enum ItemType {
62 /* These ones take file names */
66 CREATE_DIRECTORY = 'd',
67 TRUNCATE_DIRECTORY = 'D',
70 CREATE_CHAR_DEVICE = 'c',
71 CREATE_BLOCK_DEVICE = 'b',
73 /* These ones take globs */
75 IGNORE_DIRECTORY_PATH = 'X',
77 RECURSIVE_REMOVE_PATH = 'R',
79 RECURSIVE_RELABEL_PATH = 'Z'
99 bool keep_first_level:1;
102 static Hashmap *items = NULL, *globs = NULL;
103 static Set *unix_sockets = NULL;
105 static bool arg_create = false;
106 static bool arg_clean = false;
107 static bool arg_remove = false;
109 static char **include_prefixes = NULL;
110 static char **exclude_prefixes = NULL;
112 static const char conf_file_dirs[] =
115 "/usr/local/lib/tmpfiles.d\0"
116 "/usr/lib/tmpfiles.d\0"
117 #ifdef HAVE_SPLIT_USR
122 #define MAX_DEPTH 256
124 static bool needs_glob(ItemType t) {
125 return t == IGNORE_PATH || t == IGNORE_DIRECTORY_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RELABEL_PATH || t == RECURSIVE_RELABEL_PATH;
128 static struct Item* find_glob(Hashmap *h, const char *match) {
132 HASHMAP_FOREACH(j, h, i)
133 if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
139 static void load_unix_sockets(void) {
140 _cleanup_fclose_ FILE *f = NULL;
146 /* We maintain a cache of the sockets we found in
147 * /proc/net/unix to speed things up a little. */
149 unix_sockets = set_new(string_hash_func, string_compare_func);
153 f = fopen("/proc/net/unix", "re");
158 if (!fgets(line, sizeof(line), f))
165 if (!fgets(line, sizeof(line), f))
170 p = strchr(line, ':');
178 p += strspn(p, WHITESPACE);
179 p += strcspn(p, WHITESPACE); /* skip one more word */
180 p += strspn(p, WHITESPACE);
189 path_kill_slashes(s);
191 k = set_consume(unix_sockets, s);
192 if (k < 0 && k != -EEXIST)
199 set_free_free(unix_sockets);
203 static bool unix_socket_alive(const char *fn) {
209 return !!set_get(unix_sockets, (char*) fn);
211 /* We don't know, so assume yes */
215 static int dir_is_mount_point(DIR *d, const char *subdir) {
216 struct file_handle *h;
217 int mount_id_parent, mount_id;
220 h = alloca(MAX_HANDLE_SZ);
222 h->handle_bytes = MAX_HANDLE_SZ;
223 r_p = name_to_handle_at(dirfd(d), ".", h, &mount_id_parent, 0);
227 h->handle_bytes = MAX_HANDLE_SZ;
228 r = name_to_handle_at(dirfd(d), subdir, h, &mount_id, 0);
232 /* got no handle; make no assumptions, return error */
233 if (r_p < 0 && r < 0)
236 /* got both handles; if they differ, it is a mount point */
237 if (r_p >= 0 && r >= 0)
238 return mount_id_parent != mount_id;
240 /* got only one handle; assume different mount points if one
241 * of both queries was not supported by the filesystem */
242 if (r_p == -ENOSYS || r_p == -ENOTSUP || r == -ENOSYS || r == -ENOTSUP)
251 static int dir_cleanup(
255 const struct stat *ds,
260 bool keep_this_level)
263 struct timespec times[2];
264 bool deleted = false;
267 while ((dent = readdir(d))) {
270 _cleanup_free_ char *sub_path = NULL;
272 if (streq(dent->d_name, ".") ||
273 streq(dent->d_name, ".."))
276 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
278 if (errno != ENOENT) {
279 log_error("stat(%s/%s) failed: %m", p, dent->d_name);
286 /* Stay on the same filesystem */
287 if (s.st_dev != rootdev)
290 /* Try to detect bind mounts of the same filesystem instance; they
291 * do not differ in device major/minors. This type of query is not
292 * supported on all kernels or filesystem types though. */
293 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0)
296 /* Do not delete read-only files owned by root */
297 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR))
300 if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) {
305 /* Is there an item configured for this path? */
306 if (hashmap_get(items, sub_path))
309 if (find_glob(globs, sub_path))
312 if (S_ISDIR(s.st_mode)) {
315 streq(dent->d_name, "lost+found") &&
320 log_warning("Reached max depth on %s.", sub_path);
322 _cleanup_closedir_ DIR *sub_dir;
325 sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME);
326 if (sub_dir == NULL) {
327 if (errno != ENOENT) {
328 log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
335 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
341 /* Note: if you are wondering why we don't
342 * support the sticky bit for excluding
343 * directories from cleaning like we do it for
344 * other file system objects: well, the sticky
345 * bit already has a meaning for directories,
346 * so we don't want to overload that. */
351 /* Ignore ctime, we change it when deleting */
352 age = MAX(timespec_load(&s.st_mtim),
353 timespec_load(&s.st_atim));
357 if (i->type != IGNORE_DIRECTORY_PATH || !streq(dent->d_name, p)) {
358 log_debug("rmdir '%s'\n", sub_path);
360 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) {
361 if (errno != ENOENT && errno != ENOTEMPTY) {
362 log_error("rmdir(%s): %m", sub_path);
369 /* Skip files for which the sticky bit is
370 * set. These are semantics we define, and are
371 * unknown elsewhere. See XDG_RUNTIME_DIR
372 * specification for details. */
373 if (s.st_mode & S_ISVTX)
376 if (mountpoint && S_ISREG(s.st_mode)) {
377 if (streq(dent->d_name, ".journal") &&
381 if (streq(dent->d_name, "aquota.user") ||
382 streq(dent->d_name, "aquota.group"))
386 /* Ignore sockets that are listed in /proc/net/unix */
387 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path))
390 /* Ignore device nodes */
391 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
394 /* Keep files on this level around if this is
399 age = MAX3(timespec_load(&s.st_mtim),
400 timespec_load(&s.st_atim),
401 timespec_load(&s.st_ctim));
406 log_debug("unlink '%s'\n", sub_path);
408 if (unlinkat(dirfd(d), dent->d_name, 0) < 0) {
409 if (errno != ENOENT) {
410 log_error("unlink(%s): %m", sub_path);
421 /* Restore original directory timestamps */
422 times[0] = ds->st_atim;
423 times[1] = ds->st_mtim;
425 if (futimens(dirfd(d), times) < 0)
426 log_error("utimensat(%s): %m", p);
432 static int item_set_perms(Item *i, const char *path) {
433 /* not using i->path directly because it may be a glob */
435 if (chmod(path, i->mode) < 0) {
436 log_error("chmod(%s) failed: %m", path);
440 if (i->uid_set || i->gid_set)
442 i->uid_set ? i->uid : (uid_t) -1,
443 i->gid_set ? i->gid : (gid_t) -1) < 0) {
445 log_error("chown(%s) failed: %m", path);
449 return label_fix(path, false, false);
452 static int write_one_file(Item *i, const char *path) {
456 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND :
457 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
460 label_context_set(path, S_IFREG);
461 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
463 label_context_clear();
468 if (i->type == WRITE_FILE && errno == ENOENT)
471 log_error("Failed to create file %s: %m", path);
478 _cleanup_free_ char *unescaped;
480 unescaped = cunescape(i->argument);
481 if (unescaped == NULL) {
482 close_nointr_nofail(fd);
486 l = strlen(unescaped);
487 n = write(fd, unescaped, l);
489 if (n < 0 || (size_t) n < l) {
490 log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
491 close_nointr_nofail(fd);
492 return n < 0 ? n : -EIO;
496 close_nointr_nofail(fd);
498 if (stat(path, &st) < 0) {
499 log_error("stat(%s) failed: %m", path);
503 if (!S_ISREG(st.st_mode)) {
504 log_error("%s is not a file.", path);
508 r = item_set_perms(i, path);
515 static int recursive_relabel_children(Item *i, const char *path) {
516 _cleanup_closedir_ DIR *d;
519 /* This returns the first error we run into, but nevertheless
524 return errno == ENOENT ? 0 : -errno;
528 union dirent_storage buf;
531 _cleanup_free_ char *entry_path = NULL;
533 r = readdir_r(d, &buf.de, &de);
543 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
546 if (asprintf(&entry_path, "%s/%s", path, de->d_name) < 0) {
552 if (de->d_type == DT_UNKNOWN) {
555 if (lstat(entry_path, &st) < 0) {
556 if (ret == 0 && errno != ENOENT)
561 is_dir = S_ISDIR(st.st_mode);
564 is_dir = de->d_type == DT_DIR;
566 r = item_set_perms(i, entry_path);
568 if (ret == 0 && r != -ENOENT)
574 r = recursive_relabel_children(i, entry_path);
575 if (r < 0 && ret == 0)
583 static int recursive_relabel(Item *i, const char *path) {
587 r = item_set_perms(i, path);
591 if (lstat(path, &st) < 0)
594 if (S_ISDIR(st.st_mode))
595 r = recursive_relabel_children(i, path);
600 static int glob_item(Item *i, int (*action)(Item *, const char *)) {
602 _cleanup_globfree_ glob_t g = {};
606 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
608 if (k != GLOB_NOMATCH) {
612 log_error("glob(%s) failed: %m", i->path);
616 STRV_FOREACH(fn, g.gl_pathv) {
625 static int create_item(Item *i) {
634 case IGNORE_DIRECTORY_PATH:
636 case RECURSIVE_REMOVE_PATH:
641 r = write_one_file(i, i->path);
646 r = glob_item(i, write_one_file);
652 case TRUNCATE_DIRECTORY:
653 case CREATE_DIRECTORY:
655 RUN_WITH_UMASK(0000) {
656 mkdir_parents_label(i->path, 0755);
657 r = mkdir(i->path, i->mode);
660 if (r < 0 && errno != EEXIST) {
661 log_error("Failed to create directory %s: %m", i->path);
665 if (stat(i->path, &st) < 0) {
666 log_error("stat(%s) failed: %m", i->path);
670 if (!S_ISDIR(st.st_mode)) {
671 log_error("%s is not a directory.", i->path);
675 r = item_set_perms(i, i->path);
683 RUN_WITH_UMASK(0000) {
684 r = mkfifo(i->path, i->mode);
687 if (r < 0 && errno != EEXIST) {
688 log_error("Failed to create fifo %s: %m", i->path);
692 if (stat(i->path, &st) < 0) {
693 log_error("stat(%s) failed: %m", i->path);
697 if (!S_ISFIFO(st.st_mode)) {
698 log_error("%s is not a fifo.", i->path);
702 r = item_set_perms(i, i->path);
708 case CREATE_SYMLINK: {
711 label_context_set(i->path, S_IFLNK);
712 r = symlink(i->argument, i->path);
714 label_context_clear();
717 if (r < 0 && errno != EEXIST) {
718 log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
722 r = readlink_malloc(i->path, &x);
724 log_error("readlink(%s) failed: %s", i->path, strerror(-r));
728 if (!streq(i->argument, x)) {
730 log_error("%s is not the right symlinks.", i->path);
738 case CREATE_BLOCK_DEVICE:
739 case CREATE_CHAR_DEVICE: {
742 if (have_effective_cap(CAP_MKNOD) == 0) {
743 /* In a container we lack CAP_MKNOD. We
744 shouldn't attempt to create the device node in
745 that case to avoid noise, and we don't support
746 virtualized devices in containers anyway. */
748 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
752 file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
754 RUN_WITH_UMASK(0000) {
755 label_context_set(i->path, file_type);
756 r = mknod(i->path, i->mode | file_type, i->major_minor);
758 label_context_clear();
762 if (r < 0 && errno != EEXIST) {
763 log_error("Failed to create device node %s: %m", i->path);
767 if (stat(i->path, &st) < 0) {
768 log_error("stat(%s) failed: %m", i->path);
772 if ((st.st_mode & S_IFMT) != file_type) {
773 log_error("%s is not a device node.", i->path);
777 r = item_set_perms(i, i->path);
786 r = glob_item(i, item_set_perms);
791 case RECURSIVE_RELABEL_PATH:
793 r = glob_item(i, recursive_relabel);
798 log_debug("%s created successfully.", i->path);
803 static int remove_item_instance(Item *i, const char *instance) {
812 case CREATE_DIRECTORY:
815 case CREATE_BLOCK_DEVICE:
816 case CREATE_CHAR_DEVICE:
818 case IGNORE_DIRECTORY_PATH:
820 case RECURSIVE_RELABEL_PATH:
825 if (remove(instance) < 0 && errno != ENOENT) {
826 log_error("remove(%s): %m", instance);
832 case TRUNCATE_DIRECTORY:
833 case RECURSIVE_REMOVE_PATH:
834 /* FIXME: we probably should use dir_cleanup() here
835 * instead of rm_rf() so that 'x' is honoured. */
836 r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false);
837 if (r < 0 && r != -ENOENT) {
838 log_error("rm_rf(%s): %s", instance, strerror(-r));
848 static int remove_item(Item *i) {
857 case CREATE_DIRECTORY:
860 case CREATE_CHAR_DEVICE:
861 case CREATE_BLOCK_DEVICE:
863 case IGNORE_DIRECTORY_PATH:
865 case RECURSIVE_RELABEL_PATH:
870 case TRUNCATE_DIRECTORY:
871 case RECURSIVE_REMOVE_PATH:
872 r = glob_item(i, remove_item_instance);
879 static int clean_item_instance(Item *i, const char* instance) {
880 _cleanup_closedir_ DIR *d = NULL;
891 n = now(CLOCK_REALTIME);
897 d = opendir(instance);
899 if (errno == ENOENT || errno == ENOTDIR)
902 log_error("Failed to open directory %s: %m", i->path);
906 if (fstat(dirfd(d), &s) < 0) {
907 log_error("stat(%s) failed: %m", i->path);
911 if (!S_ISDIR(s.st_mode)) {
912 log_error("%s is not a directory.", i->path);
916 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) {
917 log_error("stat(%s/..) failed: %m", i->path);
921 mountpoint = s.st_dev != ps.st_dev ||
922 (s.st_dev == ps.st_dev && s.st_ino == ps.st_ino);
924 r = dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
925 MAX_DEPTH, i->keep_first_level);
929 static int clean_item(Item *i) {
935 case CREATE_DIRECTORY:
936 case TRUNCATE_DIRECTORY:
938 clean_item_instance(i, i->path);
940 case IGNORE_DIRECTORY_PATH:
941 r = glob_item(i, clean_item_instance);
950 static int process_item(Item *i) {
955 r = arg_create ? create_item(i) : 0;
956 q = arg_remove ? remove_item(i) : 0;
957 p = arg_clean ? clean_item(i) : 0;
968 static void item_free(Item *i) {
976 static inline void item_freep(Item **i) {
980 #define _cleanup_item_free_ _cleanup_(item_freep)
982 static bool item_equal(Item *a, Item *b) {
986 if (!streq_ptr(a->path, b->path))
989 if (a->type != b->type)
992 if (a->uid_set != b->uid_set ||
993 (a->uid_set && a->uid != b->uid))
996 if (a->gid_set != b->gid_set ||
997 (a->gid_set && a->gid != b->gid))
1000 if (a->mode_set != b->mode_set ||
1001 (a->mode_set && a->mode != b->mode))
1004 if (a->age_set != b->age_set ||
1005 (a->age_set && a->age != b->age))
1008 if ((a->type == CREATE_FILE ||
1009 a->type == TRUNCATE_FILE ||
1010 a->type == WRITE_FILE ||
1011 a->type == CREATE_SYMLINK) &&
1012 !streq_ptr(a->argument, b->argument))
1015 if ((a->type == CREATE_CHAR_DEVICE ||
1016 a->type == CREATE_BLOCK_DEVICE) &&
1017 a->major_minor != b->major_minor)
1023 static bool should_include_path(const char *path) {
1026 STRV_FOREACH(prefix, exclude_prefixes) {
1027 if (path_startswith(path, *prefix))
1031 STRV_FOREACH(prefix, include_prefixes) {
1032 if (path_startswith(path, *prefix))
1036 /* no matches, so we should include this path only if we
1037 * have no whitelist at all */
1038 return strv_length(include_prefixes) == 0;
1041 static int parse_line(const char *fname, unsigned line, const char *buffer) {
1043 static const Specifier specifier_table[] = {
1044 { 'm', specifier_machine_id, NULL },
1045 { 'b', specifier_boot_id, NULL },
1046 { 'H', specifier_host_name, NULL },
1047 { 'v', specifier_kernel_release, NULL },
1051 _cleanup_item_free_ Item *i = NULL;
1054 *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
1064 "%c %ms %ms %ms %ms %ms %n",
1073 log_error("[%s:%u] Syntax error.", fname, line);
1081 r = specifier_printf(path, specifier_table, NULL, &i->path);
1083 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1088 n += strspn(buffer+n, WHITESPACE);
1089 if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) {
1090 i->argument = unquote(buffer+n, "\"");
1100 case CREATE_DIRECTORY:
1101 case TRUNCATE_DIRECTORY:
1104 case IGNORE_DIRECTORY_PATH:
1106 case RECURSIVE_REMOVE_PATH:
1108 case RECURSIVE_RELABEL_PATH:
1111 case CREATE_SYMLINK:
1113 log_error("[%s:%u] Symlink file requires argument.", fname, line);
1120 log_error("[%s:%u] Write file requires argument.", fname, line);
1125 case CREATE_CHAR_DEVICE:
1126 case CREATE_BLOCK_DEVICE: {
1127 unsigned major, minor;
1130 log_error("[%s:%u] Device file requires argument.", fname, line);
1134 if (sscanf(i->argument, "%u:%u", &major, &minor) != 2) {
1135 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i->argument);
1139 i->major_minor = makedev(major, minor);
1144 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
1150 if (!path_is_absolute(i->path)) {
1151 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i->path);
1155 path_kill_slashes(i->path);
1157 if (!should_include_path(i->path))
1160 if (user && !streq(user, "-")) {
1161 const char *u = user;
1163 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
1165 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
1172 if (group && !streq(group, "-")) {
1173 const char *g = group;
1175 r = get_group_creds(&g, &i->gid);
1177 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
1184 if (mode && !streq(mode, "-")) {
1187 if (sscanf(mode, "%o", &m) != 1) {
1188 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
1196 i->type == CREATE_DIRECTORY ||
1197 i->type == TRUNCATE_DIRECTORY ? 0755 : 0644;
1199 if (age && !streq(age, "-")) {
1200 const char *a = age;
1203 i->keep_first_level = true;
1207 if (parse_sec(a, &i->age) < 0) {
1208 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
1215 h = needs_glob(i->type) ? globs : items;
1217 existing = hashmap_get(h, i->path);
1220 /* Two identical items are fine */
1221 if (!item_equal(existing, i))
1222 log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
1227 r = hashmap_put(h, i->path, i);
1229 log_error("Failed to insert item %s: %s", i->path, strerror(-r));
1233 i = NULL; /* avoid cleanup */
1238 static int help(void) {
1240 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1241 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
1242 " -h --help Show this help\n"
1243 " --create Create marked files/directories\n"
1244 " --clean Clean up marked directories\n"
1245 " --remove Remove marked files/directories\n"
1246 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n"
1247 " --exclude-prefix=PATH Ignore rules that apply to paths with the specified prefix\n",
1248 program_invocation_short_name);
1253 static int parse_argv(int argc, char *argv[]) {
1263 static const struct option options[] = {
1264 { "help", no_argument, NULL, 'h' },
1265 { "create", no_argument, NULL, ARG_CREATE },
1266 { "clean", no_argument, NULL, ARG_CLEAN },
1267 { "remove", no_argument, NULL, ARG_REMOVE },
1268 { "prefix", required_argument, NULL, ARG_PREFIX },
1269 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
1270 { NULL, 0, NULL, 0 }
1278 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
1299 if (strv_extend(&include_prefixes, optarg) < 0)
1303 case ARG_EXCLUDE_PREFIX:
1304 if (strv_extend(&exclude_prefixes, optarg) < 0)
1312 log_error("Unknown option code %c", c);
1317 if (!arg_clean && !arg_create && !arg_remove) {
1318 log_error("You need to specify at least one of --clean, --create or --remove.");
1325 static int read_config_file(const char *fn, bool ignore_enoent) {
1326 _cleanup_fclose_ FILE *f = NULL;
1327 char line[LINE_MAX];
1335 r = search_and_fopen_nulstr(fn, "re", conf_file_dirs, &f);
1337 if (ignore_enoent && r == -ENOENT)
1340 log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
1344 FOREACH_LINE(line, f, break) {
1351 if (*l == '#' || *l == 0)
1354 k = parse_line(fn, v, l);
1355 if (k < 0 && r == 0)
1359 /* we have to determine age parameter for each entry of type X */
1360 HASHMAP_FOREACH(i, globs, iterator) {
1362 Item *j, *candidate_item = NULL;
1364 if (i->type != IGNORE_DIRECTORY_PATH)
1367 HASHMAP_FOREACH(j, items, iter) {
1368 if (j->type != CREATE_DIRECTORY && j->type != TRUNCATE_DIRECTORY)
1371 if (path_equal(j->path, i->path)) {
1376 if ((!candidate_item && path_startswith(i->path, j->path)) ||
1377 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
1381 if (candidate_item) {
1382 i->age = candidate_item->age;
1388 log_error("Failed to read from file %s: %m", fn);
1396 int main(int argc, char *argv[]) {
1401 r = parse_argv(argc, argv);
1403 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1405 log_set_target(LOG_TARGET_AUTO);
1406 log_parse_environment();
1413 items = hashmap_new(string_hash_func, string_compare_func);
1414 globs = hashmap_new(string_hash_func, string_compare_func);
1416 if (!items || !globs) {
1423 if (optind < argc) {
1426 for (j = optind; j < argc; j++) {
1427 k = read_config_file(argv[j], false);
1428 if (k < 0 && r == 0)
1433 _cleanup_strv_free_ char **files = NULL;
1436 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
1438 log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
1442 STRV_FOREACH(f, files) {
1443 k = read_config_file(*f, true);
1444 if (k < 0 && r == 0)
1449 HASHMAP_FOREACH(i, globs, iterator)
1452 HASHMAP_FOREACH(i, items, iterator)
1456 while ((i = hashmap_steal_first(items)))
1459 while ((i = hashmap_steal_first(globs)))
1462 hashmap_free(items);
1463 hashmap_free(globs);
1465 strv_free(include_prefixes);
1467 set_free_free(unix_sockets);
1471 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;