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
This commit is contained in:
Lennart Poettering 2018-01-23 14:14:19 +01:00
parent 5579f85663
commit 7fa1074831
2 changed files with 27 additions and 2 deletions

View File

@ -484,6 +484,13 @@ r! /tmp/.X[0-9]*-lock</programlisting>
The second line in contrast to the first one would break a
running system, and will only be executed with
<option>--boot</option>.</para>
<para>Note that for all line types that result in creation of any kind of file node
(i.e. <varname>f</varname>/<varname>F</varname>,
<varname>d</varname>/<varname>D</varname>/<varname>v</varname>/<varname>q</varname>/<varname>Q</varname>,
<varname>p</varname>, <varname>L</varname>, <varname>c</varname>/<varname>b</varname> and <varname>C</varname>)
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 <varname>d</varname> lines.</para>
</refsect2>
<refsect2>

View File

@ -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) {