From: Christian Brauner Date: Tue, 26 Dec 2017 12:45:12 +0000 (+0100) Subject: start: properly cleanup mainloop X-Git-Tag: lxc-2.0.10~441 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=afa93cd319edf619c4f684d33e213bb7404cc0e3;p=thirdparty%2Flxc.git start: properly cleanup mainloop Signed-off-by: Christian Brauner --- diff --git a/src/lxc/mainloop.c b/src/lxc/mainloop.c index c102295ef..9afded97c 100644 --- a/src/lxc/mainloop.c +++ b/src/lxc/mainloop.c @@ -161,5 +161,8 @@ int lxc_mainloop_close(struct lxc_epoll_descr *descr) iterator = next; } - return close(descr->epfd); + if (descr->epfd >= 0) + return close(descr->epfd); + + return 0; } diff --git a/src/lxc/start.c b/src/lxc/start.c index eef483cb7..f362fa575 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -65,6 +65,7 @@ #include "confile_utils.h" #include "console.h" #include "error.h" +#include "list.h" #include "log.h" #include "lxccontainer.h" #include "lxclock.h" @@ -459,7 +460,6 @@ int lxc_poll(const char *name, struct lxc_handler *handler) { int ret; struct lxc_epoll_descr descr, descr_console; - int sigfd = handler->sigfd; ret = lxc_mainloop_open(&descr); if (ret < 0) { @@ -473,9 +473,9 @@ int lxc_poll(const char *name, struct lxc_handler *handler) goto out_mainloop; } - ret = lxc_mainloop_add_handler(&descr, sigfd, signal_handler, handler); + ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler); if (ret < 0) { - ERROR("Failed to add signal handler for %d to mainloop", sigfd); + ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd); goto out_mainloop_console; } @@ -512,22 +512,27 @@ int lxc_poll(const char *name, struct lxc_handler *handler) TRACE("Mainloop is ready"); ret = lxc_mainloop(&descr, -1); - if (ret < 0) - return -1; - if (handler->init_died) - return lxc_mainloop(&descr_console, 0); - return ret; + close(descr.epfd); + descr.epfd = -EBADF; + if (ret < 0 || !handler->init_died) + goto out_mainloop; + + ret = lxc_mainloop(&descr_console, 0); out_mainloop: lxc_mainloop_close(&descr); + TRACE("Closed mainloop"); out_mainloop_console: lxc_mainloop_close(&descr_console); + TRACE("Closed console mainloop"); out_sigfd: - close(sigfd); + close(handler->sigfd); + TRACE("Closed signal file descriptor %d", handler->sigfd); + handler->sigfd = -EBADF; - return -1; + return ret; } void lxc_zero_handler(struct lxc_handler *handler) @@ -555,10 +560,32 @@ void lxc_zero_handler(struct lxc_handler *handler) handler->sync_sock[1] = -1; } +static void lxc_put_nsfds(struct lxc_handler *handler) +{ + int i; + + for (i = 0; i < LXC_NS_MAX; i++) { + if (handler->nsfd[i] < 0) + continue; + + close(handler->nsfd[i]); + handler->nsfd[i] = -EBADF; + } +} + void lxc_free_handler(struct lxc_handler *handler) { - if (handler->conf && handler->conf->maincmd_fd) - close(handler->conf->maincmd_fd); + if (handler->pinfd >= 0) + close(handler->pinfd); + + if (handler->sigfd >= 0) + close(handler->sigfd); + + lxc_put_nsfds(handler); + + if (handler->conf && handler->conf->reboot == 0) + if (handler->conf->maincmd_fd) + close(handler->conf->maincmd_fd); if (handler->state_socket_pair[0] >= 0) close(handler->state_socket_pair[0]); @@ -568,6 +595,7 @@ void lxc_free_handler(struct lxc_handler *handler) handler->conf = NULL; free(handler); + handler = NULL; } struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf, @@ -593,6 +621,8 @@ struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf, handler->conf = conf; handler->lxcpath = lxcpath; handler->pinfd = -1; + handler->sigfd = -EBADF; + handler->init_died = false; handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1; lxc_list_init(&handler->state_clients); @@ -765,6 +795,7 @@ void lxc_fini(const char *name, struct lxc_handler *handler) while (namespace_count--) free(namespaces[namespace_count]); + for (i = 0; i < LXC_NS_MAX; i++) { if (handler->nsfd[i] != -1) { close(handler->nsfd[i]); @@ -829,15 +860,10 @@ void lxc_fini(const char *name, struct lxc_handler *handler) free(cur); } - if (handler->data_sock[0] != -1) { - close(handler->data_sock[0]); - close(handler->data_sock[1]); - } - if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) lxc_destroy_container_on_signal(handler, name); - free(handler); + lxc_free_handler(handler); } void lxc_abort(const char *name, struct lxc_handler *handler)