nspawn: remove temporary root directory on exit
authorLennart Poettering <lennart@poettering.net>
Fri, 18 Nov 2016 23:00:16 +0000 (00:00 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 22 Nov 2016 12:35:09 +0000 (13:35 +0100)
When mountint a loopback image, we need a temporary root directory we can mount
stuff to. Make sure to actually remove it when exiting, so that we don't leave
stuff around in /tmp unnecessarily.

See: #4664

src/nspawn/nspawn.c

index 8e4b94e..29755a9 100644 (file)
@@ -3789,7 +3789,6 @@ static int run(int master,
                 l = recv(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, 0);
                 if (l < 0)
                         return log_error_errno(errno, "Failed to read UID shift: %m");
-
                 if (l != sizeof arg_uid_shift) {
                         log_error("Short read while reading UID shift.");
                         return -EIO;
@@ -4023,7 +4022,7 @@ static int run(int master,
                 terminate_machine(*pid);
 
         /* Normally redundant, but better safe than sorry */
-        kill(*pid, SIGKILL);
+        (void) kill(*pid, SIGKILL);
 
         r = wait_for_container(*pid, &container_status);
         *pid = 0;
@@ -4075,7 +4074,8 @@ int main(int argc, char *argv[]) {
         pid_t pid = 0;
         union in_addr_union exposed = {};
         _cleanup_release_lock_file_ LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
-        bool interactive, veth_created = false;
+        bool interactive, veth_created = false, remove_tmprootdir = false;
+        char tmprootdir[] = "/tmp/nspawn-root-XXXXXX";
 
         log_parse_environment();
         log_open();
@@ -4208,8 +4208,6 @@ int main(int argc, char *argv[]) {
                 }
 
         } else {
-                char template[] = "/tmp/nspawn-root-XXXXXX";
-
                 assert(arg_image);
                 assert(!arg_template);
 
@@ -4251,12 +4249,14 @@ int main(int argc, char *argv[]) {
                         }
                 }
 
-                if (!mkdtemp(template)) {
+                if (!mkdtemp(tmprootdir)) {
                         r = log_error_errno(errno, "Failed to create temporary directory: %m");
                         goto finish;
                 }
 
-                arg_directory = strdup(template);
+                remove_tmprootdir = true;
+
+                arg_directory = strdup(tmprootdir);
                 if (!arg_directory) {
                         r = log_oom();
                         goto finish;
@@ -4346,7 +4346,7 @@ finish:
                                                         "STOPPING=1\nSTATUS=Terminating...");
 
         if (pid > 0)
-                kill(pid, SIGKILL);
+                (void) kill(pid, SIGKILL);
 
         /* Try to flush whatever is still queued in the pty */
         if (master >= 0) {
@@ -4372,6 +4372,11 @@ finish:
                         log_warning_errno(errno, "Can't remove image file '%s', ignoring: %m", arg_image);
         }
 
+        if (remove_tmprootdir) {
+                if (rmdir(tmprootdir) < 0)
+                        log_debug_errno(errno, "Can't remove temporary root directory '%s', ignoring: %m", tmprootdir);
+        }
+
         if (arg_machine) {
                 const char *p;