]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
mainloop: add mainloop macros
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 25 Dec 2017 13:52:39 +0000 (14:52 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 2 Jan 2018 00:11:20 +0000 (01:11 +0100)
This makes it clearer why handlers return what value.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/console.c
src/lxc/console.h
src/lxc/mainloop.c
src/lxc/mainloop.h
src/lxc/start.h

index 98efa958e1e6120ddba2e3da95495591122dd5e7..796d77af7c2b393a10c08123bcee1ef1bf3e1347 100644 (file)
@@ -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;
        }
 
index b5a9e0d0267658ef3ecd2742046c8dd7ee97aba0..290ede63928a6e3e037c13ff1c8367b3292d8717 100644 (file)
@@ -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
index ab9c27dc04dbbf5699e29e81b02ff201e6167e00..a7383d632f40dcc0273ba96335bdb40c3476b73f 100644 (file)
@@ -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;
                }
 
index 46a820c6698e82603157ae70245e2ac074b96d14..ea53a012c3d6b9f304c35e4ab91b170b43c66013 100644 (file)
@@ -27,6 +27,9 @@
 #include <stdint.h>
 #include "list.h"
 
+#define LXC_MAINLOOP_CONTINUE 0
+#define LXC_MAINLOOP_CLOSE 1
+
 struct lxc_epoll_descr {
        int epfd;
        struct lxc_list handlers;
index e35e61f7e66f6e36457b79df811e3d743aeaf178..19e14a7b144678f2a2118ac5a847129ccb8f32b7 100644 (file)
@@ -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