Skip to content

Commit

Permalink
nspawn: remove temporary root directory on exit
Browse files Browse the repository at this point in the history
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: systemd#4664
  • Loading branch information
poettering committed Nov 18, 2016
1 parent 21705e1 commit 64e6041
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/nspawn/nspawn.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -4208,8 +4208,6 @@ int main(int argc, char *argv[]) {
}

} else {
char template[] = "/tmp/nspawn-root-XXXXXX";

assert(arg_image);
assert(!arg_template);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -4346,7 +4346,7 @@ int main(int argc, char *argv[]) {
"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) {
Expand All @@ -4372,6 +4372,11 @@ int main(int argc, char *argv[]) {
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;

Expand Down

0 comments on commit 64e6041

Please sign in to comment.