From 7fa1074831202e1477e0bb5e03b7570592046e99 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 23 Jan 2018 14:14:19 +0100 Subject: [PATCH] tmpfiles: create parent directories if they are missing for more line types Currently, we create leading directories implicitly for all lines that create directory or directory-like nodes. With this, we also do the same for a number of other lines: f/F, C, p, L, c/b (that is regular files, pipes, symlinks, device nodes as well as file trees we copy). The leading directories are created with te default access mode of 0755. If something else is desired, users should simply declare appropriate "d" lines. Fixes: #7853 --- man/tmpfiles.d.xml | 7 +++++++ src/tmpfiles/tmpfiles.c | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/man/tmpfiles.d.xml b/man/tmpfiles.d.xml index 861c6eb1eb..30aa886388 100644 --- a/man/tmpfiles.d.xml +++ b/man/tmpfiles.d.xml @@ -484,6 +484,13 @@ r! /tmp/.X[0-9]*-lock The second line in contrast to the first one would break a running system, and will only be executed with . + + Note that for all line types that result in creation of any kind of file node + (i.e. f/F, + d/D/v/q/Q, + p, L, c/b and C) + leading directories are implicitly created if needed, owned by root with an access mode of 0755. In order to + create them with different modes or ownership make sure to add appropriate d lines. diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 5b56e7dcdd..4d8c36870c 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -1343,14 +1343,24 @@ static int create_item(Item *i) { case CREATE_FILE: case TRUNCATE_FILE: + RUN_WITH_UMASK(0000) + (void) mkdir_parents_label(i->path, 0755); + r = write_one_file(i, i->path); if (r < 0) return r; break; case COPY_FILES: { + + RUN_WITH_UMASK(0000) + (void) mkdir_parents_label(i->path, 0755); + log_debug("Copying tree \"%s\" to \"%s\".", i->argument, i->path); - r = copy_tree(i->argument, i->path, i->uid_set ? i->uid : UID_INVALID, i->gid_set ? i->gid : GID_INVALID, COPY_REFLINK); + r = copy_tree(i->argument, i->path, + i->uid_set ? i->uid : UID_INVALID, + i->gid_set ? i->gid : GID_INVALID, + COPY_REFLINK); if (r == -EROFS && stat(i->path, &st) == 0) r = -EEXIST; @@ -1392,7 +1402,7 @@ static int create_item(Item *i) { case CREATE_SUBVOLUME_INHERIT_QUOTA: case CREATE_SUBVOLUME_NEW_QUOTA: RUN_WITH_UMASK(0000) - mkdir_parents_label(i->path, 0755); + (void) mkdir_parents_label(i->path, 0755); if (IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA)) { @@ -1474,6 +1484,8 @@ static int create_item(Item *i) { case CREATE_FIFO: RUN_WITH_UMASK(0000) { + (void) mkdir_parents_label(i->path, 0755); + mac_selinux_create_file_prepare(i->path, S_IFIFO); r = mkfifo(i->path, i->mode); mac_selinux_create_file_clear(); @@ -1516,6 +1528,9 @@ static int create_item(Item *i) { } case CREATE_SYMLINK: { + RUN_WITH_UMASK(0000) + (void) mkdir_parents_label(i->path, 0755); + mac_selinux_create_file_prepare(i->path, S_IFLNK); r = symlink(i->argument, i->path); mac_selinux_create_file_clear(); @@ -1574,6 +1589,9 @@ static int create_item(Item *i) { return 0; } + RUN_WITH_UMASK(0000) + (void) mkdir_parents_label(i->path, 0755); + file_type = i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR; RUN_WITH_UMASK(0000) {