[systemd-commits] 2 commits - man/tmpfiles.d.xml src/shared src/tmpfiles tmpfiles.d/etc.conf

Lennart Poettering lennart at kemper.freedesktop.org
Mon Jun 16 05:00:14 PDT 2014


 man/tmpfiles.d.xml      |   11 ++++++
 src/shared/util.c       |   80 ++++++++++++++++++++++++++++++++----------------
 src/shared/util.h       |    3 +
 src/tmpfiles/tmpfiles.c |   56 ++++++++++++++++++++++-----------
 tmpfiles.d/etc.conf     |    2 -
 5 files changed, 106 insertions(+), 46 deletions(-)

New commits:
commit 2e78fa79bbaebb358d2657c397180d2d08d69b12
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Jun 16 13:21:07 2014 +0200

    tmpfiles: add new "L+" command as stronger version of "L", that removes the destination before creating a symlink
    
    Also, make use of this for mtab as long as mount insists on creating it
    even if we invoke it with "-n".

diff --git a/man/tmpfiles.d.xml b/man/tmpfiles.d.xml
index 0081a67..2d8af98 100644
--- a/man/tmpfiles.d.xml
+++ b/man/tmpfiles.d.xml
@@ -174,7 +174,16 @@ L    /tmp/foobar -    -    -    -   /dev/null</programlisting>
 
                                 <varlistentry>
                                         <term><varname>L</varname></term>
-                                        <listitem><para>Create a symlink if it does not exist yet.</para></listitem>
+                                        <term><varname>L+</varname></term>
+                                        <listitem><para>Create a
+                                        symlink if it does not exist
+                                        yet. If suffixed with
+                                        <varname>+</varname> and a
+                                        file already exists where the
+                                        symlink is to be created it
+                                        will be removed and be
+                                        replaced by the
+                                        symlink.</para></listitem>
                                 </varlistentry>
 
                                 <varlistentry>
diff --git a/src/shared/util.c b/src/shared/util.c
index d93a968..d840ded 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -4013,7 +4013,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
         assert(_f);
         assert(_temp_path);
 
-        t = strappend(path, ".XXXXXX");
+        t = tempfn_xxxxxx(path);
         if (!t)
                 return -ENOMEM;
 
@@ -4125,42 +4125,21 @@ int vt_disallocate(const char *name) {
 }
 
 int symlink_atomic(const char *from, const char *to) {
-        char *x;
-        _cleanup_free_ char *t;
-        const char *fn;
-        size_t k;
-        uint64_t u;
-        unsigned i;
-        int r;
+        _cleanup_free_ char *t = NULL;
 
         assert(from);
         assert(to);
 
-        t = new(char, strlen(to) + 1 + 16 + 1);
+        t = tempfn_random(to);
         if (!t)
                 return -ENOMEM;
 
-        fn = basename(to);
-        k = fn-to;
-        memcpy(t, to, k);
-        t[k] = '.';
-        x = stpcpy(t+k+1, fn);
-
-        u = random_u64();
-        for (i = 0; i < 16; i++) {
-                *(x++) = hexchar(u & 0xF);
-                u >>= 4;
-        }
-
-        *x = 0;
-
         if (symlink(from, t) < 0)
                 return -errno;
 
         if (rename(t, to) < 0) {
-                r = -errno;
-                unlink(t);
-                return r;
+                unlink_noerrno(t);
+                return -errno;
         }
 
         return 0;
@@ -6669,3 +6648,51 @@ int fflush_and_check(FILE *f) {
 
         return 0;
 }
+
+char *tempfn_xxxxxx(const char *p) {
+        const char *fn;
+        char *t;
+        size_t k;
+
+        assert(p);
+
+        t = new(char, strlen(p) + 1 + 6 + 1);
+        if (!t)
+                return NULL;
+
+        fn = basename(p);
+        k = fn - p;
+
+        strcpy(stpcpy(stpcpy(mempcpy(t, p, k), "."), fn), "XXXXXX");
+
+        return t;
+}
+
+char *tempfn_random(const char *p) {
+        const char *fn;
+        char *t, *x;
+        uint64_t u;
+        size_t k;
+        unsigned i;
+
+        assert(p);
+
+        t = new(char, strlen(p) + 1 + 16 + 1);
+        if (!t)
+                return NULL;
+
+        fn = basename(p);
+        k = fn - p;
+
+        x = stpcpy(stpcpy(mempcpy(t, p, k), "."), fn);
+
+        u = random_u64();
+        for (i = 0; i < 16; i++) {
+                *(x++) = hexchar(u & 0xF);
+                u >>= 4;
+        }
+
+        *x = 0;
+
+        return t;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 1796014..e855241 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -948,3 +948,6 @@ int umount_recursive(const char *target, int flags);
 int bind_remount_recursive(const char *prefix, bool ro);
 
 int fflush_and_check(FILE *f);
+
+char *tempfn_xxxxxx(const char *p);
+char *tempfn_random(const char *p);
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 0fd2bd2..0c1c2b1 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -103,6 +103,8 @@ typedef struct Item {
 
         bool keep_first_level:1;
 
+        bool force:1;
+
         bool done:1;
 } Item;
 
@@ -746,31 +748,38 @@ static int create_item(Item *i) {
 
                 break;
 
-        case CREATE_SYMLINK: {
-                _cleanup_free_ char *x = NULL;
+        case CREATE_SYMLINK:
 
                 label_context_set(i->path, S_IFLNK);
                 r = symlink(i->argument, i->path);
                 label_context_clear();
 
-                if (r < 0 && errno != EEXIST) {
-                        log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
-                        return -errno;
-                }
-
-                r = readlink_malloc(i->path, &x);
                 if (r < 0) {
-                        log_error("readlink(%s) failed: %s", i->path, strerror(-r));
-                        return -errno;
-                }
+                        _cleanup_free_ char *x = NULL;
 
-                if (!streq(i->argument, x)) {
-                        log_error("%s is not the right symlink.", i->path);
-                        return -EEXIST;
+                        if (errno != EEXIST) {
+                                log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
+                                return -errno;
+                        }
+
+                        r = readlink_malloc(i->path, &x);
+                        if (r < 0 || !streq(i->argument, x)) {
+
+                                if (i->force) {
+                                        label_context_set(i->path, S_IFLNK);
+                                        r = symlink_atomic(i->argument, i->path);
+                                        label_context_clear();
+
+                                        if (r < 0) {
+                                                log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
+                                                return -errno;
+                                        }
+                                } else
+                                        log_debug("%s is not a symlink or does not point to the correct path.", i->path);
+                        }
                 }
 
                 break;
-        }
 
         case CREATE_BLOCK_DEVICE:
         case CREATE_CHAR_DEVICE: {
@@ -1135,10 +1144,17 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                 return -EIO;
         }
 
-        if (strlen(action) > 2 || (strlen(action) > 1 && action[1] != '!')) {
-                log_error("[%s:%u] Unknown modifier '%s'", fname, line, action);
+        if (isempty(action)) {
+                log_error("[%s:%u] Command too short '%s'.", fname, line, action);
                 return -EINVAL;
-        } else if (strlen(action) > 1 && !arg_boot)
+        }
+
+        if (strlen(action) > 1 && !in_charset(action+1, "!+")) {
+                log_error("[%s:%u] Unknown modifiers in command '%s'", fname, line, action);
+                return -EINVAL;
+        }
+
+        if (strchr(action+1, '!') && !arg_boot)
                 return 0;
 
         type = action[0];
@@ -1147,6 +1163,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
         if (!i)
                 return log_oom();
 
+        i->force = !!strchr(action+1, '+');
+
         r = specifier_printf(path, specifier_table, NULL, &i->path);
         if (r < 0) {
                 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
diff --git a/tmpfiles.d/etc.conf b/tmpfiles.d/etc.conf
index 1294c94..e809dff 100644
--- a/tmpfiles.d/etc.conf
+++ b/tmpfiles.d/etc.conf
@@ -9,5 +9,5 @@
 
 L /etc/os-release - - - - ../usr/lib/os-release
 L /etc/localtime - - - - ../usr/share/zoneinfo/UTC
-L /etc/mtab - - - - ../proc/self/mounts
+L+ /etc/mtab - - - - ../proc/self/mounts
 L /etc/resolv.conf - - - - ../run/systemd/resolve/resolv.conf

commit 45c196a76b2d883552c90807386d9bed40da822b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Jun 16 13:20:38 2014 +0200

    tmpfiles: set up selinux label proeprly when creating fifos

diff --git a/src/shared/util.c b/src/shared/util.c
index a7aec5c..d93a968 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6659,6 +6659,7 @@ int bind_remount_recursive(const char *prefix, bool ro) {
 }
 
 int fflush_and_check(FILE *f) {
+        assert(f);
 
         errno = 0;
         fflush(f);
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index df52085..0fd2bd2 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -719,9 +719,11 @@ static int create_item(Item *i) {
 
         case CREATE_FIFO:
 
+                label_context_set(i->path, S_IFIFO);
                 RUN_WITH_UMASK(0000) {
                         r = mkfifo(i->path, i->mode);
                 }
+                label_context_clear();
 
                 if (r < 0 && errno != EEXIST) {
                         log_error("Failed to create fifo %s: %m", i->path);



More information about the systemd-commits mailing list