X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Ftmpfiles.c;h=68af37aab0eb43f27edb0fb3062a0ff08cbebb3d;hp=36fc9165f81e3ed467ee36b0085366d0d510c035;hb=b925e72633bf98438f56a140520e07ec8c959e46;hpb=fba6e687234660739e5ea1f2fc9c010db893c253 diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 36fc9165f..68af37aab 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -80,6 +80,7 @@ typedef struct Item { } Item; static Hashmap *items = NULL, *globs = NULL; +static Set *unix_sockets = NULL; static bool arg_create = false; static bool arg_clean = false; @@ -104,6 +105,80 @@ static struct Item* find_glob(Hashmap *h, const char *match) { return NULL; } +static void load_unix_sockets(void) { + FILE *f = NULL; + char line[LINE_MAX]; + + if (unix_sockets) + return; + + /* We maintain a cache of the sockets we found in + * /proc/net/unix to speed things up a little. */ + + if (!(unix_sockets = set_new(string_hash_func, string_compare_func))) + return; + + if (!(f = fopen("/proc/net/unix", "re"))) + return; + + if (!(fgets(line, sizeof(line), f))) + goto fail; + + for (;;) { + char *p, *s; + int k; + + if (!(fgets(line, sizeof(line), f))) + break; + + truncate_nl(line); + + if (strlen(line) < 53) + continue; + + p = line + 53; + p += strspn(p, WHITESPACE); + p += strcspn(p, WHITESPACE); + p += strspn(p, WHITESPACE); + + if (*p != '/') + continue; + + if (!(s = strdup(p))) + goto fail; + + path_kill_slashes(s); + + if ((k = set_put(unix_sockets, s)) < 0) { + free(s); + + if (k != -EEXIST) + goto fail; + } + } + + return; + +fail: + set_free_free(unix_sockets); + unix_sockets = NULL; + + if (f) + fclose(f); +} + +static bool unix_socket_alive(const char *fn) { + assert(fn); + + load_unix_sockets(); + + if (unix_sockets) + return !!set_get(unix_sockets, (char*) fn); + + /* We don't know, so assume yes */ + return true; +} + static int dir_cleanup( const char *p, DIR *d, @@ -214,7 +289,7 @@ static int dir_cleanup( if (s.st_mode & S_ISVTX) continue; - if (mountpoint) { + if (mountpoint && S_ISREG(s.st_mode)) { if (streq(dent->d_name, ".journal") && s.st_uid == 0) continue; @@ -224,6 +299,14 @@ static int dir_cleanup( continue; } + /* Ignore sockets that are listed in /proc/net/unix */ + if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path)) + continue; + + /* Ignore device nodes */ + if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode)) + continue; + age = MAX3(timespec_load(&s.st_mtim), timespec_load(&s.st_atim), timespec_load(&s.st_ctim)); @@ -424,7 +507,7 @@ static int create_item(Item *i) { break; } - if ((r = label_fix(i->path)) < 0) + if ((r = label_fix(i->path, false)) < 0) goto finish; log_debug("%s created successfully.", i->path); @@ -692,7 +775,8 @@ static int scandir_filter(const struct dirent *d) { return 0; if (d->d_type != DT_REG && - d->d_type != DT_LNK) + d->d_type != DT_LNK && + d->d_type != DT_UNKNOWN) return 0; return endswith(d->d_name, ".conf"); @@ -700,13 +784,13 @@ static int scandir_filter(const struct dirent *d) { static int help(void) { - printf("%s [OPTIONS...] [CONFIGURATION FILE]\n\n" - "Create and clean up temporary files and directories.\n\n" + printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" + "Creates, deletes and cleans up volatile and temporary files and directories.\n\n" " -h --help Show this help\n" " --create Create marked files/directories\n" " --clean Clean up marked directories\n" " --remove Remove marked files/directories\n" - " --prefix=PATH Only apply rules that apply to paths\n", + " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n", program_invocation_short_name); return 0; @@ -769,7 +853,7 @@ static int parse_argv(int argc, char *argv[]) { } if (!arg_clean && !arg_create && !arg_remove) { - log_error("You need to specify at leat one of --clean, --create or --remove."); + log_error("You need to specify at least one of --clean, --create or --remove."); return -EINVAL; } @@ -901,9 +985,14 @@ finish: while ((i = hashmap_steal_first(items))) item_free(i); + while ((i = hashmap_steal_first(globs))) + item_free(i); + hashmap_free(items); hashmap_free(globs); + set_free_free(unix_sockets); + label_finish(); return r;