Unverified Commit 578a00ff by Christian Brauner Committed by Stéphane Graber

lxccontainer: non-functional changes

parent cf605ce7
...@@ -781,31 +781,32 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -781,31 +781,32 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
}; };
char **init_cmd = NULL; char **init_cmd = NULL;
/* container exists */ /* container does exist */
if (!c) if (!c)
return false; return false;
/* If anything fails before we set error_num, we want an error in there */ /* If anything fails before we set error_num, we want an error in there.
*/
c->error_num = 1; c->error_num = 1;
/* container has not been setup */ /* Container has not been setup. */
if (!c->lxc_conf) if (!c->lxc_conf)
return false; return false;
ret = ongoing_create(c); ret = ongoing_create(c);
if (ret < 0) { if (ret < 0) {
ERROR("Error checking for incomplete creation"); ERROR("Failed checking for incomplete container creation");
return false; return false;
} else if (ret == 1) { } else if (ret == 1) {
ERROR("Error: creation of %s is ongoing", c->name); ERROR("Ongoing container creation detected");
return false; return false;
} else if (ret == 2) { } else if (ret == 2) {
ERROR("Error: %s creation was not completed", c->name); ERROR("Failed to create container");
do_lxcapi_destroy(c); do_lxcapi_destroy(c);
return false; return false;
} }
/* is this app meant to be run through lxcinit, as in lxc-execute? */ /* Is this app meant to be run through lxcinit, as in lxc-execute? */
if (useinit && !argv) if (useinit && !argv)
return false; return false;
...@@ -825,24 +826,27 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -825,24 +826,27 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
ret = lxc_execute(c->name, argv, 1, handler, c->config_path, ret = lxc_execute(c->name, argv, 1, handler, c->config_path,
daemonize); daemonize);
c->error_num = ret; c->error_num = ret;
return ret == 0 ? true : false;
if (ret != 0)
return false;
return true;
} }
/* if no argv was passed in, use lxc.init_cmd if provided in /* If no argv was passed in, use lxc.init_cmd if provided in the
* configuration */ * configuration
*/
if (!argv) if (!argv)
argv = init_cmd = split_init_cmd(conf->init_cmd); argv = init_cmd = split_init_cmd(conf->init_cmd);
/* ... and otherwise use default_args */ /* ... otherwise use default_args. */
if (!argv) if (!argv)
argv = default_args; argv = default_args;
/* /* I'm not sure what locks we want here.Any? Is liblxc's locking enough
* say, I'm not sure - what locks do we want here? Any? * here to protect the on disk container? We don't want to exclude
* Is liblxc's locking enough here to protect the on disk * things like lxc_info while the container is running.
* container? We don't want to exclude things like lxc_info */
* while container is running...
*/
if (daemonize) { if (daemonize) {
bool started; bool started;
char title[2048]; char title[2048];
...@@ -855,12 +859,16 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -855,12 +859,16 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
return false; return false;
} }
/* first parent */
if (pid != 0) { if (pid != 0) {
/* Set to NULL because we don't want father unlink /* Set to NULL because we don't want father unlink
* the PID file, child will do the free and unlink. * the PID file, child will do the free and unlink.
*/ */
c->pidfile = NULL; c->pidfile = NULL;
/* Wait for container to tell us whether it started
* successfully.
*/
started = wait_on_daemonized_start(handler, pid); started = wait_on_daemonized_start(handler, pid);
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
...@@ -868,42 +876,61 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -868,42 +876,61 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
return started; return started;
} }
/* first child */
/* We don't really care if this doesn't print all the /* We don't really care if this doesn't print all the
* characters; all that it means is that the proctitle will be * characters. All that it means is that the proctitle will be
* ugly. Similarly, we also don't care if setproctitle() * ugly. Similarly, we also don't care if setproctitle() fails.
* fails. */ * */
snprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name); snprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name);
INFO("Attempting to set proc title to %s", title); INFO("Attempting to set proc title to %s", title);
setproctitle(title); setproctitle(title);
/* second fork to be reparented by init */ /* We fork() a second time to be reparented to init. Like
* POSIX's daemon() function we change to "/" and redirect
* std{in,out,err} to /dev/null.
*/
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
SYSERROR("Error doing dual-fork"); SYSERROR("Failed to fork first child process");
exit(1); exit(EXIT_FAILURE);
} }
/* second parent */
if (pid != 0) { if (pid != 0) {
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
lxc_free_handler(handler); lxc_free_handler(handler);
exit(0); exit(EXIT_SUCCESS);
} }
/* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */ /* second child */
if (chdir("/")) {
SYSERROR("Error chdir()ing to /."); /* change to / directory */
exit(1); ret = chdir("/");
if (ret < 0) {
SYSERROR("Failed to change to \"/\" directory");
exit(EXIT_FAILURE);
} }
lxc_check_inherited(conf, true,
(int[]){handler->conf->maincmd_fd, ret = lxc_check_inherited(conf, true,
handler->state_socket_pair[0], (int[]){handler->conf->maincmd_fd,
handler->state_socket_pair[1]}, handler->state_socket_pair[0],
handler->state_socket_pair[1]},
3); 3);
if (null_stdfds() < 0) { if (ret < 0)
ERROR("failed to close fds"); exit(EXIT_FAILURE);
exit(1);
/* redirect std{in,out,err} to /dev/null */
ret = null_stdfds();
if (ret < 0) {
ERROR("Failed to redirect std{in,out,err} to /dev/null");
exit(EXIT_FAILURE);
} }
setsid();
/* become session leader */
ret = setsid();
if (ret < 0)
TRACE("Process %d is already process group leader", getpid());
} else { } else {
if (!am_single_threaded()) { if (!am_single_threaded()) {
ERROR("Cannot start non-daemonized container when threaded"); ERROR("Cannot start non-daemonized container when threaded");
...@@ -924,7 +951,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -924,7 +951,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
lxc_free_handler(handler); lxc_free_handler(handler);
if (daemonize) if (daemonize)
exit(1); exit(EXIT_FAILURE);
return false; return false;
} }
...@@ -935,7 +962,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -935,7 +962,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
lxc_free_handler(handler); lxc_free_handler(handler);
if (daemonize) if (daemonize)
exit(1); exit(EXIT_FAILURE);
return false; return false;
} }
...@@ -947,17 +974,20 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -947,17 +974,20 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
/* Unshare the mount namespace if requested */ /* Unshare the mount namespace if requested */
if (conf->monitor_unshare) { if (conf->monitor_unshare) {
if (unshare(CLONE_NEWNS)) { ret = unshare(CLONE_NEWNS);
if (ret < 0) {
SYSERROR("failed to unshare mount namespace"); SYSERROR("failed to unshare mount namespace");
lxc_free_handler(handler); lxc_free_handler(handler);
ret = 1; ret = 1;
goto out; goto on_error;
} }
if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
ret = mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
if (ret < 0) {
SYSERROR("Failed to make / rslave at startup"); SYSERROR("Failed to make / rslave at startup");
lxc_free_handler(handler); lxc_free_handler(handler);
ret = 1; ret = 1;
goto out; goto on_error;
} }
} }
...@@ -967,31 +997,31 @@ reboot: ...@@ -967,31 +997,31 @@ reboot:
handler = lxc_init_handler(c->name, conf, c->config_path, daemonize); handler = lxc_init_handler(c->name, conf, c->config_path, daemonize);
if (!handler) { if (!handler) {
ret = 1; ret = 1;
goto out; goto on_error;
} }
} }
if (lxc_check_inherited(conf, daemonize, ret = lxc_check_inherited(conf, daemonize,
(int[]){handler->conf->maincmd_fd, (int[]){handler->conf->maincmd_fd,
handler->state_socket_pair[0], handler->state_socket_pair[0],
handler->state_socket_pair[1]}, handler->state_socket_pair[1]},
3)) { 3);
ERROR("Inherited fds found"); if (ret < 0) {
lxc_free_handler(handler); lxc_free_handler(handler);
ret = 1; ret = 1;
goto out; goto on_error;
} }
ret = lxc_start(c->name, argv, handler, c->config_path, daemonize); ret = lxc_start(c->name, argv, handler, c->config_path, daemonize);
c->error_num = ret; c->error_num = ret;
if (conf->reboot == 1) { if (conf->reboot == 1) {
INFO("container requested reboot"); INFO("Container requested reboot");
conf->reboot = 2; conf->reboot = 2;
goto reboot; goto reboot;
} }
out: on_error:
if (c->pidfile) { if (c->pidfile) {
unlink(c->pidfile); unlink(c->pidfile);
free(c->pidfile); free(c->pidfile);
...@@ -999,9 +1029,15 @@ out: ...@@ -999,9 +1029,15 @@ out:
} }
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
if (daemonize) if (daemonize && ret != 0)
exit(ret == 0 ? true : false); exit(EXIT_FAILURE);
return (ret == 0 ? true : false); else if (daemonize)
exit(EXIT_SUCCESS);
if (ret != 0)
return false;
return true;
} }
static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[]) static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
......
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