dd65c9
From 797dafce1bb9c3bb16da043f654391dc29075a1a Mon Sep 17 00:00:00 2001
dd65c9
From: Michal Sekletar <msekleta@redhat.com>
dd65c9
Date: Mon, 28 Aug 2017 17:33:24 +0200
dd65c9
Subject: [PATCH] tmpfiles: with "e" don't attempt to set permissions when file
dd65c9
 doesn't exist
dd65c9
dd65c9
tmpfiles.d option "e" when run through systemd-tmpfiles --create should
dd65c9
apply configured permissions (uid,gid) only to already existing
dd65c9
files. When file doesn't exist we bail out with error. Instead we should
dd65c9
silently ignore non-existing files.
dd65c9
dd65c9
$ useradd test
dd65c9
$ cat /etc/tmpfiles.d/foobar.conf
dd65c9
e /tmp/test - test test 1d
dd65c9
$ ls -l /tmp/test
dd65c9
ls: cannot access '/tmp/test': No such file or directory
dd65c9
dd65c9
Before:
dd65c9
$ systemd-tmpfiles --create /etc/tmpfiles.d/foobar.conf
dd65c9
Adjusting owner and mode for /tmp/test failed: No such file or directory
dd65c9
$ echo $?
dd65c9
1
dd65c9
dd65c9
After:
dd65c9
$ systemd-tmpfiles --create /etc/tmpfiles.d/foobar.conf
dd65c9
$ echo $?
dd65c9
0
dd65c9
dd65c9
(cherry picked from commit 3caf791a1702c97b99d2647c9d465af404f2913d)
dd65c9
dd65c9
Conflicts:
dd65c9
	src/tmpfiles/tmpfiles.c
dd65c9
dd65c9
Resolves: #1445732
dd65c9
---
dd65c9
 src/tmpfiles/tmpfiles.c | 16 ++++++++++++++--
dd65c9
 1 file changed, 14 insertions(+), 2 deletions(-)
dd65c9
dd65c9
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
Pablo Greco 48fc63
index df7676b572..ed35b8cf0d 100644
dd65c9
--- a/src/tmpfiles/tmpfiles.c
dd65c9
+++ b/src/tmpfiles/tmpfiles.c
dd65c9
@@ -585,8 +585,20 @@ static int path_set_perms(Item *i, const char *path) {
dd65c9
          * O_PATH. */
dd65c9
 
dd65c9
         fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH|O_NOATIME);
dd65c9
-        if (fd < 0)
dd65c9
-                return log_error_errno(errno, "Adjusting owner and mode for %s failed: %m", path);
dd65c9
+        if (fd < 0) {
dd65c9
+                int level = LOG_ERR, r = -errno;
dd65c9
+
dd65c9
+                /* Option "e" operates only on existing objects. Do not
dd65c9
+                 * print errors about non-existent files or directories */
dd65c9
+                if (i->type == EMPTY_DIRECTORY && errno == ENOENT) {
dd65c9
+                        level = LOG_DEBUG;
dd65c9
+                        r = 0;
dd65c9
+                }
dd65c9
+
dd65c9
+                log_full_errno(level, errno, "Adjusting owner and mode for %s failed: %m", path);
dd65c9
+
dd65c9
+                return r;
dd65c9
+        }
dd65c9
 
dd65c9
         if (fstatat(fd, "", &st, AT_EMPTY_PATH) < 0)
dd65c9
                 return log_error_errno(errno, "Failed to fstat() file %s: %m", path);