From 540f932aeb28274e8e7ea1e8f3a8e5889b88e1d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Graber?= Date: Fri, 29 Nov 2013 10:53:15 -0500 Subject: [PATCH] Allow unsetting daemonize and close_fds MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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: Stéphane Graber Acked-by: Serge Hallyn --- src/lua-lxc/core.c | 2 +- src/lxc/lxc_start.c | 4 ++-- src/lxc/lxccontainer.c | 18 ++++++++++-------- src/lxc/lxccontainer.h | 6 +++--- src/python-lxc/lxc.c | 10 ++++++++-- src/tests/attach.c | 2 +- src/tests/cgpath.c | 2 +- src/tests/concurrent.c | 2 +- src/tests/console.c | 2 +- src/tests/containertests.c | 2 +- src/tests/createtest.c | 2 +- src/tests/shutdowntest.c | 2 +- 12 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/lua-lxc/core.c b/src/lua-lxc/core.c index 9492c07fb..8904ffc86 100644 --- a/src/lua-lxc/core.c +++ b/src/lua-lxc/core.c @@ -156,7 +156,7 @@ static int container_start(lua_State *L) argv[j] = NULL; } - c->want_daemonize(c); + c->want_daemonize(c, true); lua_pushboolean(L, !!c->start(c, useinit, argv)); return 1; } diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c index e5378469b..5e4565cda 100644 --- a/src/lxc/lxc_start.c +++ b/src/lxc/lxc_start.c @@ -325,7 +325,7 @@ int main(int argc, char *argv[]) } if (my_args.daemonize) { - c->want_daemonize(c); + c->want_daemonize(c, true); } if (pid_fp != NULL) { @@ -337,7 +337,7 @@ int main(int argc, char *argv[]) } 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; diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 283fbb58f..50796ae16 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -455,21 +455,23 @@ static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file) 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) - return; + return false; if (container_mem_lock(c)) { ERROR("Error getting mem lock"); - return; + return false; } - c->daemonize = 1; + c->daemonize = state; /* 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); + 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) return false; @@ -477,7 +479,7 @@ static bool lxcapi_want_close_all_fds(struct lxc_container *c) ERROR("Error getting mem lock"); return false; } - c->lxc_conf->close_all_fds = 1; + c->lxc_conf->close_all_fds = state; container_mem_unlock(c); return true; } @@ -549,7 +551,7 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv { int ret; struct lxc_conf *conf; - int daemonize = 0; + bool daemonize = false; char *default_args[] = { "/sbin/init", '\0', diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h index 6044f4dc3..9093de23b 100644 --- a/src/lxc/lxccontainer.h +++ b/src/lxc/lxccontainer.h @@ -93,7 +93,7 @@ struct lxc_container { int error_num; /*! Whether container wishes to be daemonized */ - int daemonize; + bool daemonize; /*! Full path to configuration file */ char *config_path; @@ -209,7 +209,7 @@ struct lxc_container { * * \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 @@ -220,7 +220,7 @@ struct lxc_container { * \return \c true if container wants all file descriptors closed, * 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. diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c index b4f1da3a2..cd9294925 100644 --- a/src/python-lxc/lxc.c +++ b/src/python-lxc/lxc.c @@ -1301,11 +1301,17 @@ Container_start(Container *self, PyObject *args, PyObject *kwds) } 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) { - 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)) diff --git a/src/tests/attach.c b/src/tests/attach.c index 57a4bddb4..2f0cb0892 100644 --- a/src/tests/attach.c +++ b/src/tests/attach.c @@ -315,7 +315,7 @@ static struct lxc_container *test_ct_create(const char *lxcpath, if (lsm_enabled()) test_attach_lsm_set_config(ct); - ct->want_daemonize(ct); + ct->want_daemonize(ct, true); if (!ct->startl(ct, 0, NULL)) { TSTERR("starting container %s", name); goto out2; diff --git a/src/tests/cgpath.c b/src/tests/cgpath.c index 493b07a79..1c14468e0 100644 --- a/src/tests/cgpath.c +++ b/src/tests/cgpath.c @@ -172,7 +172,7 @@ static int test_container(const char *lxcpath, goto out2; } c->load_config(c, NULL); - c->want_daemonize(c); + c->want_daemonize(c, true); if (!c->startl(c, 0, NULL)) { TSTERR("starting container %s", name); goto out3; diff --git a/src/tests/concurrent.c b/src/tests/concurrent.c index 76fae8758..6ae266251 100644 --- a/src/tests/concurrent.c +++ b/src/tests/concurrent.c @@ -88,7 +88,7 @@ static void do_function(void *arguments) } } else if(strcmp(args->mode, "start") == 0) { if (c->is_defined(c) && !c->is_running(c)) { - c->want_daemonize(c); + c->want_daemonize(c, true); if (!c->start(c, false, NULL)) { fprintf(stderr, "Starting the container (%s) failed...\n", name); goto out; diff --git a/src/tests/console.c b/src/tests/console.c index c4cb3b238..558e27905 100644 --- a/src/tests/console.c +++ b/src/tests/console.c @@ -145,7 +145,7 @@ static int test_console(const char *lxcpath, c->load_config(c, NULL); c->set_config_item(c, "lxc.tty", TTYCNT_STR); c->save_config(c, NULL); - c->want_daemonize(c); + c->want_daemonize(c, true); if (!c->startl(c, 0, NULL)) { TSTERR("starting container %s", name); goto out3; diff --git a/src/tests/containertests.c b/src/tests/containertests.c index e463e352e..8ec840f6b 100644 --- a/src/tests/containertests.c +++ b/src/tests/containertests.c @@ -225,7 +225,7 @@ int main(int argc, char *argv[]) goto out; /* non-daemonized is tested in 'startone' */ - c->want_daemonize(c); + c->want_daemonize(c, true); if (!c->startl(c, 0, NULL, NULL)) { fprintf(stderr, "%d: %s failed to start daemonized\n", __LINE__, c->name); goto out; diff --git a/src/tests/createtest.c b/src/tests/createtest.c index c018458b4..b53c96384 100644 --- a/src/tests/createtest.c +++ b/src/tests/createtest.c @@ -61,7 +61,7 @@ int main(int argc, char *argv[]) } c->load_config(c, NULL); - c->want_daemonize(c); + c->want_daemonize(c, true); if (!c->startl(c, 0, NULL)) { fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME); goto out; diff --git a/src/tests/shutdowntest.c b/src/tests/shutdowntest.c index c7070c16c..fc4e8fd44 100644 --- a/src/tests/shutdowntest.c +++ b/src/tests/shutdowntest.c @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) } c->load_config(c, NULL); - c->want_daemonize(c); + c->want_daemonize(c, true); if (!c->startl(c, 0, NULL)) { fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME); goto out; -- 2.39.5