Commit 540f932a by Stéphane Graber

Allow unsetting daemonize and close_fds

As mentioned in a previous commit, this does two changes: - Make want_daemonize return a bool (false on failure, true on success) - Make both want_daemonize and want_close_all_fds take a "state" argument so the user can choose to unset those flags. This commit also updates all occurences of those two functions and turns the daemonize attribute from an int to a bool. Signed-off-by: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent c1ee94cf
...@@ -156,7 +156,7 @@ static int container_start(lua_State *L) ...@@ -156,7 +156,7 @@ static int container_start(lua_State *L)
argv[j] = NULL; argv[j] = NULL;
} }
c->want_daemonize(c); c->want_daemonize(c, true);
lua_pushboolean(L, !!c->start(c, useinit, argv)); lua_pushboolean(L, !!c->start(c, useinit, argv));
return 1; return 1;
} }
......
...@@ -325,7 +325,7 @@ int main(int argc, char *argv[]) ...@@ -325,7 +325,7 @@ int main(int argc, char *argv[])
} }
if (my_args.daemonize) { if (my_args.daemonize) {
c->want_daemonize(c); c->want_daemonize(c, true);
} }
if (pid_fp != NULL) { if (pid_fp != NULL) {
...@@ -337,7 +337,7 @@ int main(int argc, char *argv[]) ...@@ -337,7 +337,7 @@ int main(int argc, char *argv[])
} }
if (my_args.close_all_fds) if (my_args.close_all_fds)
c->want_close_all_fds(c); c->want_close_all_fds(c, true);
err = c->start(c, 0, args) ? 0 : -1; err = c->start(c, 0, args) ? 0 : -1;
......
...@@ -455,21 +455,23 @@ static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file) ...@@ -455,21 +455,23 @@ static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
return ret; return ret;
} }
static void lxcapi_want_daemonize(struct lxc_container *c) static bool lxcapi_want_daemonize(struct lxc_container *c, bool state)
{ {
if (!c || !c->lxc_conf) if (!c || !c->lxc_conf)
return; return false;
if (container_mem_lock(c)) { if (container_mem_lock(c)) {
ERROR("Error getting mem lock"); ERROR("Error getting mem lock");
return; return false;
} }
c->daemonize = 1; c->daemonize = state;
/* daemonize implies close_all_fds so set it */ /* daemonize implies close_all_fds so set it */
c->lxc_conf->close_all_fds = 1; if (state == 1)
c->lxc_conf->close_all_fds = 1;
container_mem_unlock(c); container_mem_unlock(c);
return true;
} }
static bool lxcapi_want_close_all_fds(struct lxc_container *c) static bool lxcapi_want_close_all_fds(struct lxc_container *c, bool state)
{ {
if (!c || !c->lxc_conf) if (!c || !c->lxc_conf)
return false; return false;
...@@ -477,7 +479,7 @@ static bool lxcapi_want_close_all_fds(struct lxc_container *c) ...@@ -477,7 +479,7 @@ static bool lxcapi_want_close_all_fds(struct lxc_container *c)
ERROR("Error getting mem lock"); ERROR("Error getting mem lock");
return false; return false;
} }
c->lxc_conf->close_all_fds = 1; c->lxc_conf->close_all_fds = state;
container_mem_unlock(c); container_mem_unlock(c);
return true; return true;
} }
...@@ -549,7 +551,7 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv ...@@ -549,7 +551,7 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv
{ {
int ret; int ret;
struct lxc_conf *conf; struct lxc_conf *conf;
int daemonize = 0; bool daemonize = false;
char *default_args[] = { char *default_args[] = {
"/sbin/init", "/sbin/init",
'\0', '\0',
......
...@@ -93,7 +93,7 @@ struct lxc_container { ...@@ -93,7 +93,7 @@ struct lxc_container {
int error_num; int error_num;
/*! Whether container wishes to be daemonized */ /*! Whether container wishes to be daemonized */
int daemonize; bool daemonize;
/*! Full path to configuration file */ /*! Full path to configuration file */
char *config_path; char *config_path;
...@@ -209,7 +209,7 @@ struct lxc_container { ...@@ -209,7 +209,7 @@ struct lxc_container {
* *
* \return \c true if container wants to be daemonised, else \c false. * \return \c true if container wants to be daemonised, else \c false.
*/ */
void (*want_daemonize)(struct lxc_container *c); bool (*want_daemonize)(struct lxc_container *c, bool state);
/*! /*!
* \brief Determine whether container wishes all file descriptors * \brief Determine whether container wishes all file descriptors
...@@ -220,7 +220,7 @@ struct lxc_container { ...@@ -220,7 +220,7 @@ struct lxc_container {
* \return \c true if container wants all file descriptors closed, * \return \c true if container wants all file descriptors closed,
* else \c false. * else \c false.
*/ */
bool (*want_close_all_fds)(struct lxc_container *c); bool (*want_close_all_fds)(struct lxc_container *c, bool state);
/*! /*!
* \brief Return current config file name. * \brief Return current config file name.
......
...@@ -1301,11 +1301,17 @@ Container_start(Container *self, PyObject *args, PyObject *kwds) ...@@ -1301,11 +1301,17 @@ Container_start(Container *self, PyObject *args, PyObject *kwds)
} }
if (close_fds && close_fds == Py_True) { if (close_fds && close_fds == Py_True) {
self->container->want_close_all_fds(self->container); self->container->want_close_all_fds(self->container, true);
}
else {
self->container->want_close_all_fds(self->container, false);
} }
if (!daemonize || daemonize == Py_True) { if (!daemonize || daemonize == Py_True) {
self->container->want_daemonize(self->container); self->container->want_daemonize(self->container, true);
}
else {
self->container->want_daemonize(self->container, false);
} }
if (self->container->start(self->container, init_useinit, init_args)) if (self->container->start(self->container, init_useinit, init_args))
......
...@@ -315,7 +315,7 @@ static struct lxc_container *test_ct_create(const char *lxcpath, ...@@ -315,7 +315,7 @@ static struct lxc_container *test_ct_create(const char *lxcpath,
if (lsm_enabled()) if (lsm_enabled())
test_attach_lsm_set_config(ct); test_attach_lsm_set_config(ct);
ct->want_daemonize(ct); ct->want_daemonize(ct, true);
if (!ct->startl(ct, 0, NULL)) { if (!ct->startl(ct, 0, NULL)) {
TSTERR("starting container %s", name); TSTERR("starting container %s", name);
goto out2; goto out2;
......
...@@ -172,7 +172,7 @@ static int test_container(const char *lxcpath, ...@@ -172,7 +172,7 @@ static int test_container(const char *lxcpath,
goto out2; goto out2;
} }
c->load_config(c, NULL); c->load_config(c, NULL);
c->want_daemonize(c); c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) { if (!c->startl(c, 0, NULL)) {
TSTERR("starting container %s", name); TSTERR("starting container %s", name);
goto out3; goto out3;
......
...@@ -88,7 +88,7 @@ static void do_function(void *arguments) ...@@ -88,7 +88,7 @@ static void do_function(void *arguments)
} }
} else if(strcmp(args->mode, "start") == 0) { } else if(strcmp(args->mode, "start") == 0) {
if (c->is_defined(c) && !c->is_running(c)) { if (c->is_defined(c) && !c->is_running(c)) {
c->want_daemonize(c); c->want_daemonize(c, true);
if (!c->start(c, false, NULL)) { if (!c->start(c, false, NULL)) {
fprintf(stderr, "Starting the container (%s) failed...\n", name); fprintf(stderr, "Starting the container (%s) failed...\n", name);
goto out; goto out;
......
...@@ -145,7 +145,7 @@ static int test_console(const char *lxcpath, ...@@ -145,7 +145,7 @@ static int test_console(const char *lxcpath,
c->load_config(c, NULL); c->load_config(c, NULL);
c->set_config_item(c, "lxc.tty", TTYCNT_STR); c->set_config_item(c, "lxc.tty", TTYCNT_STR);
c->save_config(c, NULL); c->save_config(c, NULL);
c->want_daemonize(c); c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) { if (!c->startl(c, 0, NULL)) {
TSTERR("starting container %s", name); TSTERR("starting container %s", name);
goto out3; goto out3;
......
...@@ -225,7 +225,7 @@ int main(int argc, char *argv[]) ...@@ -225,7 +225,7 @@ int main(int argc, char *argv[])
goto out; goto out;
/* non-daemonized is tested in 'startone' */ /* non-daemonized is tested in 'startone' */
c->want_daemonize(c); c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL, NULL)) { if (!c->startl(c, 0, NULL, NULL)) {
fprintf(stderr, "%d: %s failed to start daemonized\n", __LINE__, c->name); fprintf(stderr, "%d: %s failed to start daemonized\n", __LINE__, c->name);
goto out; goto out;
......
...@@ -61,7 +61,7 @@ int main(int argc, char *argv[]) ...@@ -61,7 +61,7 @@ int main(int argc, char *argv[])
} }
c->load_config(c, NULL); c->load_config(c, NULL);
c->want_daemonize(c); c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) { if (!c->startl(c, 0, NULL)) {
fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME); fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME);
goto out; goto out;
......
...@@ -62,7 +62,7 @@ int main(int argc, char *argv[]) ...@@ -62,7 +62,7 @@ int main(int argc, char *argv[])
} }
c->load_config(c, NULL); c->load_config(c, NULL);
c->want_daemonize(c); c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) { if (!c->startl(c, 0, NULL)) {
fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME); fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME);
goto out; goto out;
......
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