Commit c379af4c by Tycho Andersen

the bike shed should be brilliant purple

parent eeeb5865
...@@ -46,6 +46,9 @@ ...@@ -46,6 +46,9 @@
#define OPT_USAGE 0x1000 #define OPT_USAGE 0x1000
#define OPT_VERSION OPT_USAGE - 1 #define OPT_VERSION OPT_USAGE - 1
#define QUOTE(macro) #macro
#define QUOTEVAL(macro) QUOTE(macro)
lxc_log_define(lxc_init, lxc); lxc_log_define(lxc_init, lxc);
static sig_atomic_t was_interrupted = 0; static sig_atomic_t was_interrupted = 0;
...@@ -95,40 +98,49 @@ static struct arguments my_args = { ...@@ -95,40 +98,49 @@ static struct arguments my_args = {
static void prevent_forking(void) static void prevent_forking(void)
{ {
FILE *f; FILE *f;
char name[PATH_MAX], path[PATH_MAX]; char name[MAXPATHLEN], path[MAXPATHLEN];
int ret; int ret;
f = fopen("/proc/self/cgroup", "r"); f = fopen("/proc/self/cgroup", "r");
if (!f) { if (!f) {
SYSERROR("opening /proc/self/cgroup"); SYSERROR("Failed to open \"/proc/self/cgroup\"");
return; return;
} }
while (!feof(f)) { while (!feof(f)) {
int fd; int fd, i;
if (2 != fscanf(f, "%*d:%[^:]:%s", name, path)) { if (1 != fscanf(f, "%*d:%" QUOTEVAL(MAXPATHLEN) "s", name)) {
ERROR("didn't scan the right number of things"); ERROR("Failed to parse \"/proc/self/cgroup\"");
goto out; goto out;
} }
path[0] = 0;
for (i = 0; i < sizeof(name); i++) {
if (name[i] == ':') {
name[i] = 0;
strncpy(path, name + i + 1, sizeof(path));
break;
}
}
if (strcmp(name, "pids")) if (strcmp(name, "pids"))
continue; continue;
ret = snprintf(name, sizeof(name), "/sys/fs/cgroup/pids/%s/pids.max", path); ret = snprintf(name, sizeof(name), "/sys/fs/cgroup/pids/%s/pids.max", path);
if (ret < 0 || ret >= sizeof(path)) { if (ret < 0 || (size_t)ret >= sizeof(path)) {
ERROR("failed snprintf"); ERROR("Failed to create string");
goto out; goto out;
} }
fd = open(name, O_WRONLY); fd = open(name, O_WRONLY);
if (fd < 0) { if (fd < 0) {
SYSERROR("open"); SYSERROR("Failed to open \"%s\"", name);
goto out; goto out;
} }
if (write(fd, "1", 1) != 1) if (write(fd, "1", 1) != 1)
SYSERROR("write"); SYSERROR("Failed to write to \"%s\"", name);
close(fd); close(fd);
break; break;
...@@ -145,14 +157,14 @@ static void kill_children(pid_t pid) ...@@ -145,14 +157,14 @@ static void kill_children(pid_t pid)
int ret; int ret;
ret = snprintf(path, sizeof(path), "/proc/%d/task/%d/children", pid, pid); ret = snprintf(path, sizeof(path), "/proc/%d/task/%d/children", pid, pid);
if (ret < 0 || ret >= sizeof(path)) { if (ret < 0 || (size_t)ret >= sizeof(path)) {
ERROR("failed snprintf"); ERROR("Failed to create string");
return; return;
} }
f = fopen(path, "r"); f = fopen(path, "r");
if (!f) { if (!f) {
SYSERROR("couldn't open %s", path); SYSERROR("Failed to open %s", path);
return; return;
} }
...@@ -160,7 +172,7 @@ static void kill_children(pid_t pid) ...@@ -160,7 +172,7 @@ static void kill_children(pid_t pid)
pid_t pid; pid_t pid;
if (fscanf(f, "%d ", &pid) != 1) { if (fscanf(f, "%d ", &pid) != 1) {
ERROR("couldn't scan pid"); ERROR("Failed to retrieve pid");
fclose(f); fclose(f);
return; return;
} }
...@@ -337,10 +349,12 @@ int main(int argc, char *argv[]) ...@@ -337,10 +349,12 @@ int main(int argc, char *argv[])
case SIGPWR: case SIGPWR:
case SIGTERM: case SIGTERM:
if (!shutdown) { if (!shutdown) {
pid_t mypid = getpid();
shutdown = 1; shutdown = 1;
prevent_forking(); prevent_forking();
if (getpid() != 1) { if (mypid != 1) {
kill_children(getpid()); kill_children(mypid);
} else { } else {
ret = kill(-1, SIGTERM); ret = kill(-1, SIGTERM);
if (ret < 0) if (ret < 0)
...@@ -350,10 +364,12 @@ int main(int argc, char *argv[]) ...@@ -350,10 +364,12 @@ int main(int argc, char *argv[])
alarm(1); alarm(1);
} }
break; break;
case SIGALRM: case SIGALRM: {
pid_t mypid = getpid();
prevent_forking(); prevent_forking();
if (getpid() != 1) { if (mypid != 1) {
kill_children(getpid()); kill_children(mypid);
} else { } else {
ret = kill(-1, SIGTERM); ret = kill(-1, SIGTERM);
if (ret < 0) if (ret < 0)
...@@ -361,6 +377,7 @@ int main(int argc, char *argv[]) ...@@ -361,6 +377,7 @@ int main(int argc, char *argv[])
"all children", strerror(errno)); "all children", strerror(errno));
} }
break; break;
}
default: default:
ret = kill(pid, was_interrupted); ret = kill(pid, was_interrupted);
if (ret < 0) if (ret < 0)
......
...@@ -314,7 +314,7 @@ bool lxc_setup_shared_ns(struct lxc_arguments *args, struct lxc_container *c) ...@@ -314,7 +314,7 @@ bool lxc_setup_shared_ns(struct lxc_arguments *args, struct lxc_container *c)
continue; continue;
if (!c->set_config_item(c, key, value)) { if (!c->set_config_item(c, key, value)) {
fprintf(stderr, "failed to set %s\n", key); lxc_error(args, "Failed to set \"%s = %s\"", key, value);
return false; return false;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment