Michal Schmidt e43452
From 4f0399974eb67bc9420c3bd700465190e38d92d6 Mon Sep 17 00:00:00 2001
Michal Schmidt e43452
From: Michal Schmidt <mschmidt@redhat.com>
Michal Schmidt e43452
Date: Fri, 16 Dec 2011 18:27:35 +0100
Michal Schmidt f1996e
Subject: [PATCH] tmpfiles: add 'z', like 'Z' but not recursive (cherry picked
Michal Schmidt f1996e
 from commit 777b87e702197ad1f2d0f2a3aea5271d18062c5c)
Michal Schmidt e43452
Michal Schmidt e43452
---
Michal Schmidt e43452
 man/systemd-tmpfiles.xml |    4 ++--
Michal Schmidt e43452
 man/tmpfiles.d.xml       |   14 ++++++++++++--
Michal Schmidt e43452
 src/tmpfiles.c           |   34 ++++++++++++++++++++++++----------
Michal Schmidt e43452
 3 files changed, 38 insertions(+), 14 deletions(-)
Michal Schmidt e43452
Michal Schmidt e43452
diff --git a/man/systemd-tmpfiles.xml b/man/systemd-tmpfiles.xml
Michal Schmidt e43452
index 74dfd5a..bbb80b2 100644
Michal Schmidt e43452
--- a/man/systemd-tmpfiles.xml
Michal Schmidt e43452
+++ b/man/systemd-tmpfiles.xml
Michal Schmidt e43452
@@ -84,8 +84,8 @@
Michal Schmidt e43452
                                 <listitem><para>If this option is passed all
Michal Schmidt e43452
                                 files and directories marked with f,
Michal Schmidt e43452
                                 F, d, D in the configuration files are
Michal Schmidt e43452
-                                created. Files and directories marked with Z
Michal Schmidt e43452
-                                have their ownership, access mode and security
Michal Schmidt e43452
+                                created. Files and directories marked with z,
Michal Schmidt e43452
+                                Z have their ownership, access mode and security
Michal Schmidt e43452
                                 labels set.</para></listitem>
Michal Schmidt e43452
                         </varlistentry>
Michal Schmidt e43452
 
Michal Schmidt e43452
diff --git a/man/tmpfiles.d.xml b/man/tmpfiles.d.xml
Michal Schmidt e43452
index e137967..4a8e831 100644
Michal Schmidt e43452
--- a/man/tmpfiles.d.xml
Michal Schmidt e43452
+++ b/man/tmpfiles.d.xml
Michal Schmidt e43452
@@ -157,6 +157,16 @@ d    /run/user 0755 root root 10d</programlisting>
Michal Schmidt e43452
                                 </varlistentry>
Michal Schmidt e43452
 
Michal Schmidt e43452
                                 <varlistentry>
Michal Schmidt e43452
+                                        <term><varname>z</varname></term>
Michal Schmidt e43452
+                                        <listitem><para>Set ownership, access
Michal Schmidt e43452
+                                        mode and relabel security context of
Michal Schmidt e43452
+                                        a file or directory if it exists.
Michal Schmidt e43452
+                                        Lines of this type accept shell-style
Michal Schmidt e43452
+                                        globs in place of normal path names.
Michal Schmidt e43452
+                                        </para></listitem>
Michal Schmidt e43452
+                                </varlistentry>
Michal Schmidt e43452
+
Michal Schmidt e43452
+                                <varlistentry>
Michal Schmidt e43452
                                         <term><varname>Z</varname></term>
Michal Schmidt e43452
                                         <listitem><para>Recursively set
Michal Schmidt e43452
                                         ownership, access mode and relabel
Michal Schmidt e43452
@@ -175,7 +185,7 @@ d    /run/user 0755 root root 10d</programlisting>
Michal Schmidt e43452
                         <para>The file access mode to use when
Michal Schmidt e43452
                         creating this file or directory. If omitted or
Michal Schmidt e43452
                         when set to - the default is used: 0755 for
Michal Schmidt e43452
-                        directories, 0644 for files. For Z lines
Michal Schmidt e43452
+                        directories, 0644 for files. For z, Z lines
Michal Schmidt e43452
                         if omitted or when set to - the file access mode will
Michal Schmidt e43452
                         not be modified. This parameter is ignored for x, r, R
Michal Schmidt e43452
                         lines.</para>
Michal Schmidt e43452
@@ -188,7 +198,7 @@ d    /run/user 0755 root root 10d</programlisting>
Michal Schmidt e43452
                         or directory. This may either be a numeric
Michal Schmidt e43452
                         user/group ID or a user or group name. If
Michal Schmidt e43452
                         omitted or when set to - the default 0 (root)
Michal Schmidt e43452
-                        is used. For Z lines when omitted or when set to -
Michal Schmidt e43452
+                        is used. For z, Z lines when omitted or when set to -
Michal Schmidt e43452
                         the file ownership will not be modified.
Michal Schmidt e43452
                         These parameters are ignored for x, r, R lines.</para>
Michal Schmidt e43452
                 </refsect2>
Michal Schmidt e43452
diff --git a/src/tmpfiles.c b/src/tmpfiles.c
Michal Schmidt e43452
index 1395082..19a7c08 100644
Michal Schmidt e43452
--- a/src/tmpfiles.c
Michal Schmidt e43452
+++ b/src/tmpfiles.c
Michal Schmidt e43452
@@ -62,6 +62,7 @@ typedef enum ItemType {
Michal Schmidt e43452
         IGNORE_PATH = 'x',
Michal Schmidt e43452
         REMOVE_PATH = 'r',
Michal Schmidt e43452
         RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt e43452
+        RELABEL_PATH = 'z',
Michal Schmidt e43452
         RECURSIVE_RELABEL_PATH = 'Z'
Michal Schmidt e43452
 } ItemType;
Michal Schmidt e43452
 
Michal Schmidt e43452
@@ -92,7 +93,7 @@ static const char *arg_prefix = NULL;
Michal Schmidt e43452
 #define MAX_DEPTH 256
Michal Schmidt e43452
 
Michal Schmidt e43452
 static bool needs_glob(ItemType t) {
Michal Schmidt e43452
-        return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RECURSIVE_RELABEL_PATH;
Michal Schmidt e43452
+        return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH || t == RELABEL_PATH || t == RECURSIVE_RELABEL_PATH;
Michal Schmidt e43452
 }
Michal Schmidt e43452
 
Michal Schmidt e43452
 static struct Item* find_glob(Hashmap *h, const char *match) {
Michal Schmidt e43452
@@ -646,6 +647,13 @@ static int create_item(Item *i) {
Michal Schmidt e43452
 
Michal Schmidt e43452
                 break;
Michal Schmidt e43452
 
Michal Schmidt e43452
+        case RELABEL_PATH:
Michal Schmidt e43452
+
Michal Schmidt e43452
+                r = glob_item(i, item_set_perms);
Michal Schmidt e43452
+                if (r < 0)
Michal Schmidt e43452
+                        return 0;
Michal Schmidt e43452
+                break;
Michal Schmidt e43452
+
Michal Schmidt e43452
         case RECURSIVE_RELABEL_PATH:
Michal Schmidt e43452
 
Michal Schmidt e43452
                 r = glob_item(i, recursive_relabel);
Michal Schmidt e43452
@@ -670,6 +678,7 @@ static int remove_item_instance(Item *i, const char *instance) {
Michal Schmidt e43452
         case CREATE_DIRECTORY:
Michal Schmidt e43452
         case CREATE_FIFO:
Michal Schmidt e43452
         case IGNORE_PATH:
Michal Schmidt e43452
+        case RELABEL_PATH:
Michal Schmidt e43452
         case RECURSIVE_RELABEL_PATH:
Michal Schmidt e43452
                 break;
Michal Schmidt e43452
 
Michal Schmidt e43452
@@ -707,6 +716,7 @@ static int remove_item(Item *i) {
Michal Schmidt e43452
         case CREATE_DIRECTORY:
Michal Schmidt e43452
         case CREATE_FIFO:
Michal Schmidt e43452
         case IGNORE_PATH:
Michal Schmidt e43452
+        case RELABEL_PATH:
Michal Schmidt e43452
         case RECURSIVE_RELABEL_PATH:
Michal Schmidt e43452
                 break;
Michal Schmidt e43452
 
Michal Schmidt e43452
@@ -808,15 +818,19 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
Michal Schmidt e43452
                 goto finish;
Michal Schmidt e43452
         }
Michal Schmidt e43452
 
Michal Schmidt e43452
-        if (type != CREATE_FILE &&
Michal Schmidt e43452
-            type != TRUNCATE_FILE &&
Michal Schmidt e43452
-            type != CREATE_DIRECTORY &&
Michal Schmidt e43452
-            type != TRUNCATE_DIRECTORY &&
Michal Schmidt e43452
-            type != CREATE_FIFO &&
Michal Schmidt e43452
-            type != IGNORE_PATH &&
Michal Schmidt e43452
-            type != REMOVE_PATH &&
Michal Schmidt e43452
-            type != RECURSIVE_REMOVE_PATH &&
Michal Schmidt e43452
-            type != RECURSIVE_RELABEL_PATH) {
Michal Schmidt e43452
+        switch(type) {
Michal Schmidt e43452
+        case CREATE_FILE:
Michal Schmidt e43452
+        case TRUNCATE_FILE:
Michal Schmidt e43452
+        case CREATE_DIRECTORY:
Michal Schmidt e43452
+        case TRUNCATE_DIRECTORY:
Michal Schmidt e43452
+        case CREATE_FIFO:
Michal Schmidt e43452
+        case IGNORE_PATH:
Michal Schmidt e43452
+        case REMOVE_PATH:
Michal Schmidt e43452
+        case RECURSIVE_REMOVE_PATH:
Michal Schmidt e43452
+        case RELABEL_PATH:
Michal Schmidt e43452
+        case RECURSIVE_RELABEL_PATH:
Michal Schmidt e43452
+                break;
Michal Schmidt e43452
+        default:
Michal Schmidt e43452
                 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
Michal Schmidt e43452
                 r = -EBADMSG;
Michal Schmidt e43452
                 goto finish;