[systemd-commits] 12 commits - src/main.c src/missing.h src/nspawn.c src/shutdown.c src/socket.c src/umount.c src/util.c src/util.h
Lennart Poettering
lennart at kemper.freedesktop.org
Mon Mar 14 07:37:38 PDT 2011
src/main.c | 44 ++++++++++++++---
src/missing.h | 4 +
src/nspawn.c | 40 ++++++++++-----
src/shutdown.c | 146 ++++++++++++++++++++++++++-------------------------------
src/socket.c | 2
src/umount.c | 2
src/util.c | 80 +++++++++++++++++--------------
src/util.h | 1
8 files changed, 185 insertions(+), 134 deletions(-)
New commits:
commit f41de9596627ef68fcb4c0dae4b9bd8033701230
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 15:33:39 2011 +0100
shutdown: print a nice message when terminating a container
diff --git a/src/shutdown.c b/src/shutdown.c
index df2d850..a2f3b53 100644
--- a/src/shutdown.c
+++ b/src/shutdown.c
@@ -323,8 +323,10 @@ int main(int argc, char *argv[]) {
/* If we are in a container, just exit, this will kill our
* container for good. */
- if (in_container)
+ if (in_container) {
+ log_error("Exiting container.");
exit(0);
+ }
sync();
commit 9b634ea5fb7cb5466f1f7873204bd67d02652981
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 15:33:23 2011 +0100
nspawn: mount /selinux if needed
diff --git a/src/nspawn.c b/src/nspawn.c
index 7cefd3d..a053a4d 100644
--- a/src/nspawn.c
+++ b/src/nspawn.c
@@ -104,13 +104,16 @@ static int mount_all(const char *dest) {
} MountPoint;
static const MountPoint mount_table[] = {
- { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
- { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND }, /* Bind mount first */
- { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND|MS_RDONLY|MS_REMOUNT }, /* Then, make it r/o */
- { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY },
- { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID },
- { "/dev/pts", "/dev/pts", "bind", NULL, MS_BIND },
- { "tmpfs", "/dev/.run", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV },
+ { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
+ { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND }, /* Bind mount first */
+ { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND|MS_RDONLY|MS_REMOUNT }, /* Then, make it r/o */
+ { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY },
+ { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID },
+ { "/dev/pts", "/dev/pts", "bind", NULL, MS_BIND },
+ { "tmpfs", "/dev/.run", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV },
+#ifdef HAVE_SELINUX
+ { "selinux", "/selinux", "selinuxfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY },
+#endif
};
unsigned k;
commit 40e85d00198c4d9305bbfc794b384f8174fae300
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 05:40:15 2011 +0100
shutdown: just call exit() if we are in a container
diff --git a/src/shutdown.c b/src/shutdown.c
index 5c082cf..df2d850 100644
--- a/src/shutdown.c
+++ b/src/shutdown.c
@@ -96,125 +96,107 @@ static int killall(int sign) {
return n_processes;
}
-static int send_signal(int sign) {
- sigset_t mask, oldmask;
+static void wait_for_children(int n_processes, sigset_t *mask) {
usec_t until;
- int n_processes;
- struct timespec ts;
- assert_se(sigemptyset(&mask) == 0);
- assert_se(sigaddset(&mask, SIGCHLD) == 0);
- if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
- return -errno;
-
- if (kill(-1, SIGSTOP) < 0)
- log_warning("kill(-1, SIGSTOP) failed: %m");
-
- n_processes = killall(sign);
-
- if (kill(-1, SIGCONT) < 0)
- log_warning("kill(-1, SIGCONT) failed: %m");
-
- if (n_processes <= 0)
- goto finish;
+ assert(mask);
until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
for (;;) {
+ struct timespec ts;
int k;
- usec_t n = now(CLOCK_MONOTONIC);
+ usec_t n;
for (;;) {
pid_t pid = waitpid(-1, NULL, WNOHANG);
if (pid == 0)
break;
- else if (pid < 0 && errno == ECHILD) {
- n_processes = 0;
- goto finish;
- }
- if (--n_processes == 0)
- goto finish;
+ if (pid < 0 && errno == ECHILD)
+ return;
+
+ if (n_processes > 0)
+ if (--n_processes == 0)
+ return;
}
+ n = now(CLOCK_MONOTONIC);
if (n >= until)
- goto finish;
+ return;
timespec_store(&ts, until - n);
- if ((k = sigtimedwait(&mask, NULL, &ts)) != SIGCHLD) {
+
+ if ((k = sigtimedwait(mask, NULL, &ts)) != SIGCHLD) {
+
+ if (k < 0 && errno != EAGAIN) {
+ log_error("sigtimedwait() failed: %m");
+ return;
+ }
+
if (k >= 0)
log_warning("sigtimedwait() returned unexpected signal.");
- if (k < 0 && errno != EAGAIN)
- log_warning("sigtimedwait() failed: %m");
}
}
+}
+
+static void send_signal(int sign) {
+ sigset_t mask, oldmask;
+ int n_processes;
+
+ assert_se(sigemptyset(&mask) == 0);
+ assert_se(sigaddset(&mask, SIGCHLD) == 0);
+ assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
+
+ if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
+ log_warning("kill(-1, SIGSTOP) failed: %m");
+
+ n_processes = killall(sign);
+
+ if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
+ log_warning("kill(-1, SIGCONT) failed: %m");
+
+ if (n_processes <= 0)
+ goto finish;
+
+ wait_for_children(n_processes, &mask);
finish:
sigprocmask(SIG_SETMASK, &oldmask, NULL);
-
- return n_processes;
}
-static int rescue_send_signal(int sign) {
+static void ultimate_send_signal(int sign) {
sigset_t mask, oldmask;
- usec_t until;
- struct timespec ts;
int r;
- sigemptyset(&mask);
- sigaddset(&mask, SIGCHLD);
- if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
- return -errno;
+ assert_se(sigemptyset(&mask) == 0);
+ assert_se(sigaddset(&mask, SIGCHLD) == 0);
+ assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
- if (kill(-1, SIGSTOP) < 0)
+ if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
log_warning("kill(-1, SIGSTOP) failed: %m");
r = kill(-1, sign);
- if (r < 0)
- log_warning("kill(-1, %d) failed: %m", sign);
+ if (r < 0 && errno != ESRCH)
+ log_warning("kill(-1, %s) failed: %m", signal_to_string(sign));
- if (kill(-1, SIGCONT) < 0)
+ if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
log_warning("kill(-1, SIGCONT) failed: %m");
if (r < 0)
goto finish;
- until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
- for (;;) {
- int k;
- usec_t n = now(CLOCK_MONOTONIC);
-
- for (;;) {
- pid_t pid = waitpid(-1, NULL, WNOHANG);
- if (pid == 0)
- break;
- else if (pid < 0 && errno == ECHILD)
- goto finish;
- }
-
- if (n >= until)
- goto finish;
-
- timespec_store(&ts, until - n);
- if ((k = sigtimedwait(&mask, NULL, &ts)) != SIGCHLD) {
- if (k >= 0)
- log_warning("sigtimedwait() returned unexpected signal.");
- if (k < 0 && errno != EAGAIN)
- log_warning("sigtimedwait() failed: %m");
- }
- }
+ wait_for_children(0, &mask);
finish:
sigprocmask(SIG_SETMASK, &oldmask, NULL);
-
- return r;
}
int main(int argc, char *argv[]) {
int cmd, r;
unsigned retries;
bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
- bool killed_everbody = false;
+ bool killed_everbody = false, in_container;
log_parse_environment();
log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */
@@ -232,6 +214,8 @@ int main(int argc, char *argv[]) {
goto error;
}
+ in_container = detect_container(NULL) > 0;
+
if (streq(argv[1], "reboot"))
cmd = RB_AUTOBOOT;
else if (streq(argv[1], "poweroff"))
@@ -251,14 +235,13 @@ int main(int argc, char *argv[]) {
log_warning("Cannot lock process memory: %m");
log_info("Sending SIGTERM to remaining processes...");
- r = send_signal(SIGTERM);
- if (r < 0)
- log_warning("Failed to send SIGTERM to remaining processes: %s", strerror(r));
+ send_signal(SIGTERM);
log_info("Sending SIGKILL to remaining processes...");
- r = send_signal(SIGKILL);
- if (r < 0)
- log_warning("Failed to send SIGKILL to remaining processes: %s", strerror(r));
+ send_signal(SIGKILL);
+
+ if (in_container)
+ need_swapoff = false;
/* Unmount all mountpoints, swaps, and loopback devices */
for (retries = 0; retries < FINALIZE_ATTEMPTS; retries++) {
@@ -325,8 +308,8 @@ int main(int argc, char *argv[]) {
}
log_warning("Cannot finalize remaining file systems and devices, trying to kill remaining processes.");
- rescue_send_signal(SIGTERM);
- rescue_send_signal(SIGKILL);
+ ultimate_send_signal(SIGTERM);
+ ultimate_send_signal(SIGKILL);
killed_everbody = true;
}
@@ -338,6 +321,11 @@ int main(int argc, char *argv[]) {
execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, NULL);
+ /* If we are in a container, just exit, this will kill our
+ * container for good. */
+ if (in_container)
+ exit(0);
+
sync();
if (cmd == LINUX_REBOOT_CMD_KEXEC) {
commit c4f8bd1aef96063ccc2121fe2429a933a0b2068d
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 05:37:47 2011 +0100
umount: assume that a non-existing /dev/loop device means it is already detached
diff --git a/src/umount.c b/src/umount.c
index 4fd6b22..6fe0a26 100644
--- a/src/umount.c
+++ b/src/umount.c
@@ -355,7 +355,7 @@ static int delete_loopback(const char *device) {
int fd, r;
if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
- return -errno;
+ return errno == ENOENT ? 0 : -errno;
r = ioctl(fd, LOOP_CLR_FD, 0);
close_nointr_nofail(fd);
commit 973bcd30bfd8c5d6d28d40075ae9b2561f9d3e2a
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 05:37:14 2011 +0100
socket: use 777 as default mode for sockets
diff --git a/src/socket.c b/src/socket.c
index 77bbe43..130f51c 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -67,7 +67,7 @@ static void socket_init(Unit *u) {
s->backlog = SOMAXCONN;
s->timeout_usec = DEFAULT_TIMEOUT_USEC;
s->directory_mode = 0755;
- s->socket_mode = 0666;
+ s->socket_mode = 0777;
s->max_connections = 64;
commit 64af1b6207e4062e43f049a4f322ffa4de9dd87f
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 05:36:43 2011 +0100
nspawn: we don't want a network namespace
diff --git a/src/nspawn.c b/src/nspawn.c
index f340805..7cefd3d 100644
--- a/src/nspawn.c
+++ b/src/nspawn.c
@@ -383,7 +383,7 @@ int main(int argc, char *argv[]) {
log_info("Spawning namespace container on %s.", arg_directory);
- if ((pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWIPC|CLONE_NEWNS|CLONE_NEWPID|CLONE_NEWUTS|CLONE_NEWNET, NULL)) < 0) {
+ if ((pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWIPC|CLONE_NEWNS|CLONE_NEWPID|CLONE_NEWUTS, NULL)) < 0) {
log_error("clone() failed: %m");
goto finish;
}
commit 90df7e567f668b4d0e7761fd15fa8cebffc759a0
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 04:48:14 2011 +0100
main: log to the console in a container
diff --git a/src/main.c b/src/main.c
index 313afcc..0c805c9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1021,7 +1021,7 @@ int main(int argc, char *argv[]) {
if (getpid() == 1) {
arg_running_as = MANAGER_SYSTEM;
- log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
+ log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_CONSOLE : LOG_TARGET_SYSLOG_OR_KMSG);
/* This might actually not return, but cause a
* reexecution */
diff --git a/src/nspawn.c b/src/nspawn.c
index 297bb61..f340805 100644
--- a/src/nspawn.c
+++ b/src/nspawn.c
@@ -254,7 +254,7 @@ static int copy_devnodes(const char *dest) {
r = -errno;
}
- if (mount(from, to, "bind", MS_BIND, NULL) < 0) {
+ if (mount(from, to, "bind", MS_BIND|MS_RDONLY, NULL) < 0) {
log_error("bind mount for /dev/console failed: %m");
if (r == 0)
commit b770165a4f54fed39221bcf33e9d040c12d04fcc
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 04:08:12 2011 +0100
main: don't parse /proc/cmdline in containers
diff --git a/src/main.c b/src/main.c
index 54ebb0b..313afcc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -546,6 +546,11 @@ static int parse_proc_cmdline(void) {
int r;
size_t l;
+ /* Don't read /proc/cmdline if we are in a container, since
+ * that is only relevant for the host system */
+ if (detect_container(NULL) > 0)
+ return 0;
+
if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
return 0;
@@ -626,6 +631,9 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 1);
assert(argv);
+ if (getpid() == 1)
+ opterr = 0;
+
while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
switch (c) {
@@ -820,13 +828,30 @@ static int parse_argv(int argc, char *argv[]) {
break;
}
- /* PID 1 will get the kernel arguments as parameters, which we
- * ignore and unconditionally read from
- * /proc/cmdline. However, we need to ignore those arguments
- * here. */
- if (getpid() != 1 && optind < argc) {
- log_error("Excess arguments.");
- return -EINVAL;
+ if (optind < argc) {
+ if (getpid() != 1) {
+ /* Hmm, when we aren't run as init system
+ * let's complain about excess arguments */
+
+ log_error("Excess arguments.");
+ return -EINVAL;
+
+ } else if (detect_container(NULL) > 0) {
+ char **a;
+
+ /* All /proc/cmdline arguments the kernel
+ * didn't understand it passed to us. We're
+ * note really interested in that usually
+ * since /proc/cmdline is more interesting and
+ * complete. With one exception: if we are run
+ * in a container /proc/cmdline is not
+ * relevant for us, hence we rely on argv[]
+ * instead. */
+
+ for (a = argv + optind; a < argv + argc; a++)
+ if ((r = parse_proc_cmdline_word(*a)) < 0)
+ return r;
+ }
}
return 0;
@@ -1093,6 +1118,9 @@ int main(int argc, char *argv[]) {
* kernel that don't really make sense for us. */
unsetenv("HOME");
unsetenv("TERM");
+
+ /* All other variables are left as is, so that clients
+ * can still read them via /proc/1/environ */
}
/* Move out of the way, so that we won't block unmounts */
commit ef2df9f41541a62fb7876b98701ab072b41325e2
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 04:07:52 2011 +0100
util: add detect_container()
diff --git a/src/util.c b/src/util.c
index 38d630e..eefd66e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -3945,33 +3945,32 @@ int detect_vm(const char **id) {
return 0;
}
-/* Returns a short identifier for the various VM/container implementations */
-int detect_virtualization(const char **id) {
- int r;
- static __thread const char *cached_id = NULL;
- const char *_id;
+int detect_container(const char **id) {
FILE *f;
- if (cached_id) {
+ /* Unfortunately many of these operations require root access
+ * in one way or another */
- if (cached_id == (const char*) -1)
- return 0;
+ if (geteuid() != 0)
+ return -EPERM;
+
+ if (running_in_chroot() > 0) {
if (id)
- *id = cached_id;
+ *id = "chroot";
return 1;
}
- /* Unfortunately most of these operations require root access
- * in one way or another */
- if (geteuid() != 0)
- return -EPERM;
+ /* /proc/vz exists in container and outside of the container,
+ * /proc/bc only outside of the container. */
+ if (access("/proc/vz", F_OK) >= 0 &&
+ access("/proc/bc", F_OK) < 0) {
- if ((r = running_in_chroot()) > 0) {
- _id = "chroot";
- r = 1;
- goto finish;
+ if (id)
+ *id = "openvz";
+
+ return 1;
}
if ((f = fopen("/proc/self/cgroup", "r"))) {
@@ -3991,36 +3990,49 @@ int detect_virtualization(const char **id) {
if (!streq(p, ":ns:/")) {
fclose(f);
- r = 1;
- _id = "ns";
- goto finish;
+ if (id)
+ *id = "ns";
+
+ return 1;
}
}
fclose(f);
}
- /* /proc/vz exists in container and outside of the container,
- * /proc/bc only outside of the container. */
- if (access("/proc/vz", F_OK) >= 0 &&
- access("/proc/bc", F_OK) < 0) {
- _id = "openvz";
- r = 1;
- goto finish;
+ return 0;
+}
+
+/* Returns a short identifier for the various VM/container implementations */
+int detect_virtualization(const char **id) {
+ static __thread const char *cached_id = NULL;
+ const char *_id;
+ int r;
+
+ if (cached_id) {
+
+ if (cached_id == (const char*) -1)
+ return 0;
+
+ if (id)
+ *id = cached_id;
+
+ return 1;
}
+ if ((r = detect_container(&_id)) != 0)
+ goto finish;
+
r = detect_vm(&_id);
finish:
- if (r < 0)
- return r;
- else if (r > 0)
+ if (r > 0) {
cached_id = _id;
- else
- cached_id = (const char*) -1;
- if (id)
- *id = _id;
+ if (id)
+ *id = _id;
+ } else if (r == 0)
+ cached_id = (const char*) -1;
return r;
}
diff --git a/src/util.h b/src/util.h
index 320bcd7..192ebff 100644
--- a/src/util.h
+++ b/src/util.h
@@ -380,6 +380,7 @@ bool tty_is_vc(const char *tty);
const char *default_term_for_tty(const char *tty);
int detect_vm(const char **id);
+int detect_container(const char **id);
int detect_virtualization(const char **id);
void execute_directory(const char *directory, DIR *_d, char *argv[]);
commit da5b3bad1cadb9d9062d01a17056ec085fb725f9
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 03:28:16 2011 +0100
nspawn: reset environment and load login shell
diff --git a/src/nspawn.c b/src/nspawn.c
index 82b0ce4..297bb61 100644
--- a/src/nspawn.c
+++ b/src/nspawn.c
@@ -390,6 +390,11 @@ int main(int argc, char *argv[]) {
if (pid == 0) {
const char *hn;
+ const char *envp[] = {
+ "HOME=/root",
+ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
+ NULL
+ };
/* child */
@@ -425,9 +430,11 @@ int main(int argc, char *argv[]) {
sethostname(hn, strlen(hn));
if (argc > optind)
- execvp(argv[optind], argv + optind);
- else
- execl("/bin/bash", "/bin/bash", NULL);
+ execvpe(argv[optind], argv + optind, (char**) envp);
+ else {
+ chdir("/root");
+ execle("/bin/bash", "-bash", NULL, (char**) envp);
+ }
log_error("execv() failed: %m");
commit 124640f177c118b096ed2c531a4617d52a283395
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 03:28:00 2011 +0100
nspawn: reset umask if needed
diff --git a/src/nspawn.c b/src/nspawn.c
index bf4e6de..82b0ce4 100644
--- a/src/nspawn.c
+++ b/src/nspawn.c
@@ -176,6 +176,9 @@ static int copy_devnodes(const char *dest) {
int r = 0, k;
char *tty = NULL;
dev_t tty_devnum;
+ mode_t u;
+
+ u = umask(0000);
NULSTR_FOREACH(d, devnodes) {
char *from = NULL, *to = NULL;
@@ -265,6 +268,8 @@ static int copy_devnodes(const char *dest) {
free(tty);
+ umask(u);
+
return r;
}
commit 94d8298589f06de596fbc0a4bc9b50072a2a0536
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Mar 14 03:27:28 2011 +0100
nspawn: define MS_MOVE manually if needed
diff --git a/src/missing.h b/src/missing.h
index ff5dcb4..c0cb3ea 100644
--- a/src/missing.h
+++ b/src/missing.h
@@ -122,4 +122,8 @@ struct btrfs_ioctl_vol_args {
#define BTRFS_SUPER_MAGIC 0x9123683E
#endif
+#ifndef MS_MOVE
+#define MS_MOVE 8192
+#endif
+
#endif
diff --git a/src/nspawn.c b/src/nspawn.c
index fa70e86..bf4e6de 100644
--- a/src/nspawn.c
+++ b/src/nspawn.c
@@ -36,6 +36,7 @@
#include "log.h"
#include "util.h"
+#include "missing.h"
static char *arg_directory = NULL;
More information about the systemd-commits
mailing list