a52f674
From 730cb1d24ea7a339b06b0b513b6e99c2c7d01004 Mon Sep 17 00:00:00 2001
930e1b8
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
930e1b8
Date: Tue, 22 Oct 2013 20:39:18 -0400
930e1b8
Subject: [PATCH] systemd: use unit name in PrivateTmp directories
930e1b8
930e1b8
Unit name is used whole in the directory name, so that the unit name
930e1b8
can be easily extracted from it, e.g. "/tmp/systemd-abcd.service-DEDBIF1".
930e1b8
930e1b8
https://bugzilla.redhat.com/show_bug.cgi?id=957439
930e1b8
---
930e1b8
 Makefile.am               |  7 ++++++
930e1b8
 src/core/execute.c        |  2 +-
930e1b8
 src/core/namespace.c      | 15 ++++++++-----
930e1b8
 src/core/namespace.h      |  5 ++++-
930e1b8
 src/test/test-namespace.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++
930e1b8
 5 files changed, 77 insertions(+), 8 deletions(-)
930e1b8
 create mode 100644 src/test/test-namespace.c
930e1b8
930e1b8
diff --git a/Makefile.am b/Makefile.am
930e1b8
index 8d9c587..5e3e5d2 100644
930e1b8
--- a/Makefile.am
930e1b8
+++ b/Makefile.am
930e1b8
@@ -1130,6 +1130,7 @@ tests += \
930e1b8
 	test-unit-file \
930e1b8
 	test-utf8 \
930e1b8
 	test-util \
930e1b8
+	test-namespace \
930e1b8
 	test-date \
930e1b8
 	test-sleep \
930e1b8
 	test-replace-var \
930e1b8
@@ -1256,6 +1257,12 @@ test_util_CFLAGS = \
930e1b8
 test_util_LDADD = \
930e1b8
 	libsystemd-core.la
930e1b8
 
930e1b8
+test_namespace_SOURCES = \
930e1b8
+	src/test/test-namespace.c
930e1b8
+
930e1b8
+test_namespace_LDADD = \
930e1b8
+	libsystemd-core.la
930e1b8
+
930e1b8
 test_hashmap_SOURCES = \
930e1b8
 	src/test/test-hashmap.c
930e1b8
 
930e1b8
diff --git a/src/core/execute.c b/src/core/execute.c
930e1b8
index 3f7ca52..58be72a 100644
930e1b8
--- a/src/core/execute.c
930e1b8
+++ b/src/core/execute.c
930e1b8
@@ -1089,7 +1089,7 @@ int exec_spawn(ExecCommand *command,
930e1b8
         free(line);
930e1b8
 
930e1b8
         if (context->private_tmp && !context->tmp_dir && !context->var_tmp_dir) {
930e1b8
-                r = setup_tmpdirs(&context->tmp_dir, &context->var_tmp_dir);
930e1b8
+                r = setup_tmpdirs(unit_id, &context->tmp_dir, &context->var_tmp_dir);
930e1b8
                 if (r < 0)
930e1b8
                         return r;
930e1b8
         }
930e1b8
diff --git a/src/core/namespace.c b/src/core/namespace.c
930e1b8
index 936f368..b453f8d 100644
930e1b8
--- a/src/core/namespace.c
930e1b8
+++ b/src/core/namespace.c
930e1b8
@@ -184,26 +184,29 @@ static int make_read_only(BindMount *m) {
930e1b8
         return 0;
930e1b8
 }
930e1b8
 
930e1b8
-int setup_tmpdirs(char **tmp_dir,
930e1b8
+int setup_tmpdirs(const char *unit_id,
930e1b8
+                  char **tmp_dir,
930e1b8
                   char **var_tmp_dir) {
930e1b8
         int r = 0;
930e1b8
-        char tmp_dir_template[] = "/tmp/systemd-private-XXXXXX",
930e1b8
-             var_tmp_dir_template[] = "/var/tmp/systemd-private-XXXXXX";
930e1b8
+        _cleanup_free_ char *tmp = NULL, *var = NULL;
930e1b8
 
930e1b8
         assert(tmp_dir);
930e1b8
         assert(var_tmp_dir);
930e1b8
 
930e1b8
-        r = create_tmp_dir(tmp_dir_template, tmp_dir);
930e1b8
+        tmp = strjoin("/tmp/systemd-", unit_id, "-XXXXXXX", NULL);
930e1b8
+        var = strjoin("/var/tmp/systemd-", unit_id, "-XXXXXXX", NULL);
930e1b8
+
930e1b8
+        r = create_tmp_dir(tmp, tmp_dir);
930e1b8
         if (r < 0)
930e1b8
                 return r;
930e1b8
 
930e1b8
-        r = create_tmp_dir(var_tmp_dir_template, var_tmp_dir);
930e1b8
+        r = create_tmp_dir(var, var_tmp_dir);
930e1b8
         if (r == 0)
930e1b8
                 return 0;
930e1b8
 
930e1b8
         /* failure */
930e1b8
         rmdir(*tmp_dir);
930e1b8
-        rmdir(tmp_dir_template);
930e1b8
+        rmdir(tmp);
930e1b8
         free(*tmp_dir);
930e1b8
         *tmp_dir = NULL;
930e1b8
 
930e1b8
diff --git a/src/core/namespace.h b/src/core/namespace.h
930e1b8
index ddb5794..ee7416b 100644
930e1b8
--- a/src/core/namespace.h
930e1b8
+++ b/src/core/namespace.h
930e1b8
@@ -23,7 +23,10 @@
930e1b8
 
930e1b8
 #include <stdbool.h>
930e1b8
 
930e1b8
-int setup_tmpdirs(char **tmp_dir, char **var_tmp_dir);
930e1b8
+int setup_tmpdirs(const char *unit_id,
930e1b8
+                  char **tmp_dir,
930e1b8
+                  char **var_tmp_dir);
930e1b8
+
930e1b8
 int setup_namespace(char **read_write_dirs,
930e1b8
                     char **read_only_dirs,
930e1b8
                     char **inaccessible_dirs,
930e1b8
diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c
930e1b8
new file mode 100644
930e1b8
index 0000000..1c04676
930e1b8
--- /dev/null
930e1b8
+++ b/src/test/test-namespace.c
930e1b8
@@ -0,0 +1,56 @@
930e1b8
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
930e1b8
+
930e1b8
+/***
930e1b8
+  This file is part of systemd.
930e1b8
+
930e1b8
+  Copyright 2013 Zbigniew Jędrzejewski-Szmek
930e1b8
+
930e1b8
+  systemd is free software; you can redistribute it and/or modify it
930e1b8
+  under the terms of the GNU Lesser General Public License as published by
930e1b8
+  the Free Software Foundation; either version 2.1 of the License, or
930e1b8
+  (at your option) any later version.
930e1b8
+
930e1b8
+  systemd is distributed in the hope that it will be useful, but
930e1b8
+  WITHOUT ANY WARRANTY; without even the implied warranty of
930e1b8
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
930e1b8
+  Lesser General Public License for more details.
930e1b8
+
930e1b8
+  You should have received a copy of the GNU Lesser General Public License
930e1b8
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
930e1b8
+***/
930e1b8
+
930e1b8
+#include <libgen.h>
930e1b8
+
930e1b8
+#include "namespace.h"
930e1b8
+#include "util.h"
930e1b8
+
930e1b8
+static void test_tmpdir(const char *id, const char *A, const char *B) {
930e1b8
+        _cleanup_free_ char *a, *b;
930e1b8
+
930e1b8
+        assert_se(setup_tmpdirs(id, &a, &b) == 0);
930e1b8
+        assert(startswith(a, A));
930e1b8
+        assert(startswith(b, B));
930e1b8
+        assert(access(a, F_OK) == 0);
930e1b8
+        assert(access(b, F_OK) == 0);
930e1b8
+
930e1b8
+        assert_se(rmdir(a) == 0);
930e1b8
+        assert_se(rmdir(b) == 0);
930e1b8
+
930e1b8
+        assert(endswith(a, "/tmp"));
930e1b8
+        assert(endswith(b, "/tmp"));
930e1b8
+
930e1b8
+        assert_se(rmdir(dirname(a)) == 0);
930e1b8
+        assert_se(rmdir(dirname(b)) == 0);
930e1b8
+}
930e1b8
+
930e1b8
+int main(int argc, char *argv[]) {
930e1b8
+        test_tmpdir("abcd.service",
930e1b8
+                    "/tmp/systemd-abcd.service-",
930e1b8
+                    "/var/tmp/systemd-abcd.service-");
930e1b8
+
930e1b8
+        test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device",
930e1b8
+                    "/tmp/systemd-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-",
930e1b8
+                    "/var/tmp/systemd-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
930e1b8
+
930e1b8
+        return 0;
930e1b8
+}