if (siginfo.ssi_signo == SIGTERM) {
DEBUG("Received SIGTERM. Detaching from the console");
- return 1;
+ return LXC_MAINLOOP_CLOSE;
}
if (siginfo.ssi_signo == SIGWINCH)
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];
return 0;
}
close(fd);
- return 1;
+ return LXC_MAINLOOP_CLOSE;
}
if (fd == console->peer)
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) {
* 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;
}
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;
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 */
}
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;
}
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;
}
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)
}
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;
}