#define PLANCK_FEATURE_LISTEN 1 #include #include #include /* * `::listen: [arguments]` -- PID 1 holds the listening TCP socket and starts the program per * connection with the connection as its stdin and stdout (stderr stays the console): the inetd contract, e.g. * `dropbear -i`. A box whose one service runs this way keeps NO daemon resident: between connections the only * process is PID 1, and the service's binary is not even in the page cache until the first connection. * * The program is exec'd directly, no shell: the line is split on spaces (no quoting). At most MAX_LISTEN_CHILDREN * connections are served at once per line; further ones wait in the kernel's accept queue. */ #define MAX_LISTEN 2 /* listen lines; each costs one struct below in bss */ #define MAX_LISTEN_ARGS 8 /* words of the program line */ #define MAX_LISTEN_CHILDREN 8 /* concurrent connections per line: what a 32 MB box can carry of anything */ #define LISTEN_BACKLOG 8 /* accept queue while every child slot is busy */ struct listen_entry { int fd; pid_t children[MAX_LISTEN_CHILDREN]; char *argv[MAX_LISTEN_ARGS + 1]; char cmd[MAX_CMD]; }; static struct listen_entry listeners[MAX_LISTEN]; static int n_listeners; static void planck_listen_add(const char *line) { struct listen_entry *e; struct sockaddr_in sin = { .sin_family = AF_INET }; unsigned int port = 0; int one = 1, argc = 0; char *p; while (*line >= '0' && *line <= '9') port = port * 10 + (unsigned int)(*line++ - '0'); if (!port || port > 65535 || *line != ' ' || n_listeners >= MAX_LISTEN) { log_msg("listen: want ` [arguments]`"); return; } e = &listeners[n_listeners]; strncpy(e->cmd, line, sizeof(e->cmd) - 1); for (p = e->cmd; *p && argc < MAX_LISTEN_ARGS; ) { while (*p == ' ') *p++ = 0; if (!*p) break; e->argv[argc++] = p; while (*p && *p != ' ') p++; } if (!argc) return; e->fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); sin.sin_port = htons((unsigned short)port); setsockopt(e->fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); if (e->fd < 0 || bind(e->fd, (struct sockaddr *)&sin, sizeof(sin)) < 0 || listen(e->fd, LISTEN_BACKLOG) < 0) { log_msg("listen: cannot bind"); close(e->fd); return; } n_listeners++; } static void planck_listen_wake(int sig) { (void)sig; } /* A dead child frees its slot; everything else that died goes to the respawn table. */ static void planck_listen_reap(void) { pid_t died; int i, j; while ((died = waitpid(-1, NULL, WNOHANG)) > 0) { for (i = 0; i < n_listeners; i++) for (j = 0; j < MAX_LISTEN_CHILDREN; j++) if (listeners[i].children[j] == died) listeners[i].children[j] = 0; planck_handle_child_exit(died); } } /* PID 1's main loop when the inittab has listen lines; returns at once when it has none. * SIGCHLD is blocked except inside ppoll, so a child that dies between the reap and the wait is not missed. */ static void planck_listen_loop(void) { struct pollfd fds[MAX_LISTEN]; sigset_t chld, open_mask; int i, j; if (!n_listeners) return; sigemptyset(&chld); sigaddset(&chld, SIGCHLD); sigprocmask(SIG_BLOCK, &chld, &open_mask); signal(SIGCHLD, planck_listen_wake); for (;;) { planck_listen_reap(); for (i = 0; i < n_listeners; i++) { for (j = 0; j < MAX_LISTEN_CHILDREN && listeners[i].children[j]; j++) ; fds[i].fd = j < MAX_LISTEN_CHILDREN ? listeners[i].fd : -1; /* no free slot: leave them queued */ fds[i].events = POLLIN; fds[i].revents = 0; } if (ppoll(fds, (nfds_t)n_listeners, NULL, &open_mask) < 0) { if (errno == EINTR) continue; /* e.g. ENOSYS from a kernel without the syscall: retrying would spin PID 1 forever */ log_msg("listen: ppoll failed, listeners off"); sigprocmask(SIG_SETMASK, &open_mask, NULL); return; } for (i = 0; i < n_listeners; i++) { int conn; pid_t p; if (!(fds[i].revents & POLLIN)) continue; conn = accept(listeners[i].fd, NULL, NULL); if (conn < 0) continue; for (j = 0; listeners[i].children[j]; j++) ; p = vfork(); if (p == 0) { sigprocmask(SIG_SETMASK, &open_mask, NULL); /* the program must not inherit a blocked SIGCHLD */ dup2(conn, 0); dup2(conn, 1); close(conn); execv(listeners[i].argv[0], listeners[i].argv); _exit(1); } close(conn); if (p > 0) listeners[i].children[j] = p; } } }