From: Christian Brauner Date: Mon, 25 Dec 2017 13:52:39 +0000 (+0100) Subject: mainloop: add mainloop macros X-Git-Tag: lxc-2.0.10~444 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c56c715ce9b0c36f56b356555c96434218f6fedb;p=thirdparty%2Flxc.git mainloop: add mainloop macros This makes it clearer why handlers return what value. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/console.c b/src/lxc/console.c index 98efa958e..796d77af7 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -120,7 +120,7 @@ int lxc_console_cb_signal_fd(int fd, uint32_t events, void *cbdata, if (siginfo.ssi_signo == SIGTERM) { DEBUG("Received SIGTERM. Detaching from the console"); - return 1; + return LXC_MAINLOOP_CLOSE; } if (siginfo.ssi_signo == SIGWINCH) @@ -202,8 +202,8 @@ void lxc_console_signal_fini(struct lxc_tty_state *ts) free(ts); } -static int lxc_console_cb_con(int fd, uint32_t events, void *data, - struct lxc_epoll_descr *descr) +int lxc_console_cb_con(int fd, uint32_t events, void *data, + struct lxc_epoll_descr *descr) { struct lxc_console *console = (struct lxc_console *)data; char buf[1024]; @@ -223,7 +223,7 @@ static int lxc_console_cb_con(int fd, uint32_t events, void *data, return 0; } close(fd); - return 1; + return LXC_MAINLOOP_CLOSE; } if (fd == console->peer) @@ -243,27 +243,36 @@ static int lxc_console_cb_con(int fd, uint32_t events, void *data, return 0; } -static void lxc_console_mainloop_add_peer(struct lxc_console *console) +static int lxc_console_mainloop_add_peer(struct lxc_console *console) { + int ret; + if (console->peer >= 0) { - if (lxc_mainloop_add_handler(console->descr, console->peer, - lxc_console_cb_con, console)) + ret = lxc_mainloop_add_handler(console->descr, console->peer, + lxc_console_cb_con, console); + if (ret < 0) { WARN("Failed to add console peer handler to mainloop"); + return -1; + } } - if (console->tty_state && console->tty_state->sigfd != -1) { - if (lxc_mainloop_add_handler(console->descr, - console->tty_state->sigfd, - lxc_console_cb_signal_fd, - console->tty_state)) { - WARN("Failed to add signal handler to mainloop"); - } + if (!console->tty_state || console->tty_state->sigfd < 0) + return 0; + + ret = lxc_mainloop_add_handler(console->descr, console->tty_state->sigfd, + lxc_console_cb_signal_fd, console->tty_state); + if (ret < 0) { + WARN("Failed to add signal handler to mainloop"); + return -1; } + + return 0; } extern int lxc_console_mainloop_add(struct lxc_epoll_descr *descr, struct lxc_conf *conf) { + int ret; struct lxc_console *console = &conf->console; if (!conf->rootfs.path) { @@ -287,7 +296,9 @@ extern int lxc_console_mainloop_add(struct lxc_epoll_descr *descr, * does attach to it in lxc_console_allocate() */ console->descr = descr; - lxc_console_mainloop_add_peer(console); + ret = lxc_console_mainloop_add_peer(console); + if (ret < 0) + return -1; return 0; } @@ -396,7 +407,9 @@ static int lxc_console_peer_proxy_alloc(struct lxc_console *console, int sockfd) console->tty_state = ts; console->peer = console->peerpty.slave; console->peerpty.busy = sockfd; - lxc_console_mainloop_add_peer(console); + ret = lxc_console_mainloop_add_peer(console); + if (ret < 0) + goto err1; DEBUG("%d %s peermaster:%d sockfd:%d", lxc_raw_getpid(), __FUNCTION__, console->peerpty.master, sockfd); return 0; @@ -655,10 +668,10 @@ int lxc_console_cb_tty_stdin(int fd, uint32_t events, void *cbdata, char c; if (fd != ts->stdinfd) - return 1; + return LXC_MAINLOOP_CLOSE; if (lxc_read_nointr(ts->stdinfd, &c, 1) <= 0) - return 1; + return LXC_MAINLOOP_CLOSE; if (ts->escape >= 1) { /* we want to exit the console with Ctrl+a q */ @@ -668,13 +681,13 @@ int lxc_console_cb_tty_stdin(int fd, uint32_t events, void *cbdata, } if (c == 'q' && ts->saw_escape) - return 1; + return LXC_MAINLOOP_CLOSE; ts->saw_escape = 0; } if (lxc_write_nointr(ts->masterfd, &c, 1) <= 0) - return 1; + return LXC_MAINLOOP_CLOSE; return 0; } @@ -687,17 +700,17 @@ int lxc_console_cb_tty_master(int fd, uint32_t events, void *cbdata, int r, w; if (fd != ts->masterfd) - return 1; + return LXC_MAINLOOP_CLOSE; r = lxc_read_nointr(fd, buf, sizeof(buf)); if (r <= 0) - return 1; + return LXC_MAINLOOP_CLOSE; w = lxc_write_nointr(ts->stdoutfd, buf, r); if (w <= 0) { - return 1; + return LXC_MAINLOOP_CLOSE; } else if (w != r) { - SYSERROR("failed to write"); + SYSERROR("Failed to write"); return 1; } diff --git a/src/lxc/console.h b/src/lxc/console.h index b5a9e0d02..290ede639 100644 --- a/src/lxc/console.h +++ b/src/lxc/console.h @@ -221,4 +221,7 @@ extern int lxc_console_cb_signal_fd(int fd, uint32_t events, void *cbdata, */ extern void lxc_console_signal_fini(struct lxc_tty_state *ts); +extern int lxc_console_cb_con(int fd, uint32_t events, void *data, + struct lxc_epoll_descr *descr); + #endif diff --git a/src/lxc/mainloop.c b/src/lxc/mainloop.c index ab9c27dc0..a7383d632 100644 --- a/src/lxc/mainloop.c +++ b/src/lxc/mainloop.c @@ -40,12 +40,11 @@ struct mainloop_handler { int lxc_mainloop(struct lxc_epoll_descr *descr, int timeout_ms) { - int i, nfds; + int i, nfds, ret; struct mainloop_handler *handler; struct epoll_event events[MAX_EVENTS]; for (;;) { - nfds = epoll_wait(descr->epfd, events, MAX_EVENTS, timeout_ms); if (nfds < 0) { if (errno == EINTR) @@ -54,13 +53,13 @@ int lxc_mainloop(struct lxc_epoll_descr *descr, int timeout_ms) } for (i = 0; i < nfds; i++) { - handler = - (struct mainloop_handler *) events[i].data.ptr; + handler = events[i].data.ptr; - /* If the handler returns a positive value, exit - the mainloop */ - if (handler->callback(handler->fd, events[i].events, - handler->data, descr) > 0) + /* If the handler returns a positive value, exit the + * mainloop. */ + ret = handler->callback(handler->fd, events[i].events, + handler->data, descr); + if (ret == LXC_MAINLOOP_CLOSE) return 0; } diff --git a/src/lxc/mainloop.h b/src/lxc/mainloop.h index 46a820c66..ea53a012c 100644 --- a/src/lxc/mainloop.h +++ b/src/lxc/mainloop.h @@ -27,6 +27,9 @@ #include #include "list.h" +#define LXC_MAINLOOP_CONTINUE 0 +#define LXC_MAINLOOP_CLOSE 1 + struct lxc_epoll_descr { int epfd; struct lxc_list handlers; diff --git a/src/lxc/start.h b/src/lxc/start.h index e35e61f7e..19e14a7b1 100644 --- a/src/lxc/start.h +++ b/src/lxc/start.h @@ -143,5 +143,5 @@ extern int __lxc_start(const char *, struct lxc_handler *, struct lxc_operations *, void *, const char *, bool); extern int resolve_clone_flags(struct lxc_handler *handler); -#endif +#endif