X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Ftmpfiles%2Ftmpfiles.c;h=eae993e6b690f630e1d201672b7525ee0ab153bb;hp=84f4986503b206efc3054386e839a1014d713066;hb=e2f2fb786059fbed410938f16e5cc8b851366b14;hpb=7f602784de4fd378120e8ebfe6d830862b9cae03 diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 84f498650..eae993e6b 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -135,7 +135,7 @@ static struct Item* find_glob(Hashmap *h, const char *match) { } static void load_unix_sockets(void) { - FILE _cleanup_fclose_ *f = NULL; + _cleanup_fclose_ FILE *f = NULL; char line[LINE_MAX]; if (unix_sockets) @@ -186,13 +186,9 @@ static void load_unix_sockets(void) { path_kill_slashes(s); - k = set_put(unix_sockets, s); - if (k < 0) { - free(s); - - if (k != -EEXIST) - goto fail; - } + k = set_consume(unix_sockets, s); + if (k < 0 && k != -EEXIST) + goto fail; } return; @@ -269,7 +265,7 @@ static int dir_cleanup( while ((dent = readdir(d))) { struct stat s; usec_t age; - char _cleanup_free_ *sub_path = NULL; + _cleanup_free_ char *sub_path = NULL; if (streq(dent->d_name, ".") || streq(dent->d_name, "..")) @@ -321,7 +317,7 @@ static int dir_cleanup( if (maxdepth <= 0) log_warning("Reached max depth on %s.", sub_path); else { - DIR _cleanup_closedir_ *sub_dir; + _cleanup_closedir_ DIR *sub_dir; int q; sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW|O_NOATIME); @@ -454,18 +450,17 @@ static int item_set_perms(Item *i, const char *path) { static int write_one_file(Item *i, const char *path) { int r, e, fd, flags; struct stat st; - mode_t u; flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND : i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0; - u = umask(0); - label_context_set(path, S_IFREG); - fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode); - e = errno; - label_context_clear(); - umask(u); - errno = e; + RUN_WITH_UMASK(0) { + label_context_set(path, S_IFREG); + fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode); + e = errno; + label_context_clear(); + errno = e; + } if (fd < 0) { if (i->type == WRITE_FILE && errno == ENOENT) @@ -516,7 +511,7 @@ static int write_one_file(Item *i, const char *path) { } static int recursive_relabel_children(Item *i, const char *path) { - DIR _cleanup_closedir_ *d; + _cleanup_closedir_ DIR *d; int ret = 0; /* This returns the first error we run into, but nevertheless @@ -531,7 +526,7 @@ static int recursive_relabel_children(Item *i, const char *path) { union dirent_storage buf; bool is_dir; int r; - char _cleanup_free_ *entry_path = NULL; + _cleanup_free_ char *entry_path = NULL; r = readdir_r(d, &buf.de, &de); if (r != 0) { @@ -602,14 +597,12 @@ static int recursive_relabel(Item *i, const char *path) { static int glob_item(Item *i, int (*action)(Item *, const char *)) { int r = 0, k; - glob_t g; + _cleanup_globfree_ glob_t g = {}; char **fn; - zero(g); - errno = 0; - if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) { - + k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g); + if (k != 0) if (k != GLOB_NOMATCH) { if (errno > 0) errno = EIO; @@ -617,19 +610,18 @@ static int glob_item(Item *i, int (*action)(Item *, const char *)) { log_error("glob(%s) failed: %m", i->path); return -errno; } - } - STRV_FOREACH(fn, g.gl_pathv) - if ((k = action(i, *fn)) < 0) + STRV_FOREACH(fn, g.gl_pathv) { + k = action(i, *fn); + if (k < 0) r = k; + } - globfree(&g); return r; } static int create_item(Item *i) { int r, e; - mode_t u; struct stat st; assert(i); @@ -658,10 +650,10 @@ static int create_item(Item *i) { case TRUNCATE_DIRECTORY: case CREATE_DIRECTORY: - u = umask(0); - mkdir_parents_label(i->path, 0755); - r = mkdir(i->path, i->mode); - umask(u); + RUN_WITH_UMASK(0000) { + mkdir_parents_label(i->path, 0755); + r = mkdir(i->path, i->mode); + } if (r < 0 && errno != EEXIST) { log_error("Failed to create directory %s: %m", i->path); @@ -686,9 +678,9 @@ static int create_item(Item *i) { case CREATE_FIFO: - u = umask(0); - r = mkfifo(i->path, i->mode); - umask(u); + RUN_WITH_UMASK(0000) { + r = mkfifo(i->path, i->mode); + } if (r < 0 && errno != EEXIST) { log_error("Failed to create fifo %s: %m", i->path); @@ -747,7 +739,7 @@ static int create_item(Item *i) { if (have_effective_cap(CAP_MKNOD) == 0) { /* In a container we lack CAP_MKNOD. We - shouldnt attempt to create the device node in + shouldn't attempt to create the device node in that case to avoid noise, and we don't support virtualized devices in containers anyway. */ @@ -757,13 +749,13 @@ static int create_item(Item *i) { file_type = (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); - u = umask(0); - label_context_set(i->path, file_type); - r = mknod(i->path, i->mode | file_type, i->major_minor); - e = errno; - label_context_clear(); - umask(u); - errno = e; + RUN_WITH_UMASK(0000) { + label_context_set(i->path, file_type); + r = mknod(i->path, i->mode | file_type, i->major_minor); + e = errno; + label_context_clear(); + errno = e; + } if (r < 0 && errno != EEXIST) { log_error("Failed to create device node %s: %m", i->path); @@ -791,7 +783,7 @@ static int create_item(Item *i) { r = glob_item(i, item_set_perms); if (r < 0) - return 0; + return r; break; case RECURSIVE_RELABEL_PATH: @@ -883,7 +875,7 @@ static int remove_item(Item *i) { } static int clean_item_instance(Item *i, const char* instance) { - DIR _cleanup_closedir_ *d = NULL; + _cleanup_closedir_ DIR *d = NULL; struct stat s, ps; bool mountpoint; int r; @@ -979,6 +971,12 @@ static void item_free(Item *i) { free(i); } +static inline void item_freep(Item **i) { + if (*i) + item_free(*i); +} +#define _cleanup_item_free_ _cleanup_(item_freep) + static bool item_equal(Item *a, Item *b) { assert(a); assert(b); @@ -1021,9 +1019,9 @@ static bool item_equal(Item *a, Item *b) { } static int parse_line(const char *fname, unsigned line, const char *buffer) { - Item _cleanup_free_ *i = NULL; + _cleanup_item_free_ Item *i = NULL; Item *existing; - char _cleanup_free_ + _cleanup_free_ char *mode = NULL, *user = NULL, *group = NULL, *age = NULL; char type; Hashmap *h;