Sophie

Sophie

distrib > Fedora > 17 > i386 > media > updates-src > by-pkgid > ab4b662b9827b6375ffd451bf4abd615 > files > 195

systemd-44-24.fc17.src.rpm

From 2f531f7ac86bf4d8a363ba8e02a3b1bc9ca0bd95 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Sun, 22 Apr 2012 01:01:22 +0200
Subject: [PATCH] nspawn: be more careful when initializing the hostname from
 the directory name (cherry picked from commit
 3a74cea5e4cea6d6f852a2a7118efe0e339b78c7)

---
 src/nspawn/nspawn.c |   31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 1ce3c33..0d0d987 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -101,8 +101,9 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case 'D':
                         free(arg_directory);
-                        if (!(arg_directory = strdup(optarg))) {
-                                log_error("Failed to duplicate root directory.");
+                        arg_directory = canonicalize_file_name(optarg);
+                        if (!arg_directory) {
+                                log_error("Failed to canonicalize root directory.");
                                 return -ENOMEM;
                         }
 
@@ -474,6 +475,28 @@ finish:
         return r;
 }
 
+static int setup_hostname(void) {
+        char *hn;
+        int r = 0;
+
+        hn = file_name_from_path(arg_directory);
+        if (hn) {
+                hn = strdup(hn);
+                if (!hn)
+                        return -ENOMEM;
+
+                hostname_cleanup(hn);
+
+                if (!isempty(hn))
+                        if (sethostname(hn, strlen(hn)) < 0)
+                                r = -errno;
+
+                free(hn);
+        }
+
+        return r;
+}
+
 static int drop_capabilities(void) {
         static const unsigned long retain[] = {
                 CAP_CHOWN,
@@ -872,7 +895,6 @@ int main(int argc, char *argv[]) {
         if (pid == 0) {
                 /* child */
 
-                const char *hn;
                 const char *home = NULL;
                 uid_t uid = (uid_t) -1;
                 gid_t gid = (gid_t) -1;
@@ -1000,8 +1022,7 @@ int main(int argc, char *argv[]) {
                     goto child_fail;
                 }
 
-                if ((hn = file_name_from_path(arg_directory)))
-                        sethostname(hn, strlen(hn));
+                setup_hostname();
 
                 if (argc > optind)
                         execvpe(argv[optind], argv + optind, (char**) envp);