2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek This file is part of systemd.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Copyright 2013 Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek (at your option) any later version.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int add_epoll(int epoll_fd, int fd) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return log_error_errno(errno, "Failed to add event on epoll fd:%d for fd:%d: %m", epoll_fd, fd);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int open_sockets(int *epoll_fd, bool accept) {
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering log_info("Received %i descriptors via the environment.", n);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek /* Close logging and all other descriptors */
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek for (fd = 0; fd < SD_LISTEN_FDS_START + n; fd++)
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek /** Note: we leak some fd's on error here. I doesn't matter
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek * much, since the program will exit immediately anyway, but
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek * would be a pain to fix.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek STRV_FOREACH(address, arg_listen) {
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering fd = make_socket_fd(LOG_DEBUG, *address, arg_socket_type, (arg_accept*SOCK_CLOEXEC));
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt return log_error_errno(fd, "Failed to open '%s': %m", *address);
175a3d25d0e8596d4ba0759aea3f89ee228e7d6dLennart Poettering assert(fd == SD_LISTEN_FDS_START + count);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek *epoll_fd = epoll_create1(EPOLL_CLOEXEC);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return log_error_errno(errno, "Failed to create epoll object: %m");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + count; fd++) {
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering log_info("Listening on %s as %i.", strna(name), fd);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poetteringstatic int exec_process(const char* name, char **argv, char **env, int start_fd, int n_fds) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering log_error("--inetd only supported for single file descriptors.");
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek length = strv_length(arg_setenv);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering /* PATH, TERM, HOME, USER, LISTEN_FDS, LISTEN_PID, LISTEN_FDNAMES, NULL */
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering const char *n;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering FOREACH_STRING(tocopy, "TERM=", "PATH=", "USER=", "HOME=") {
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering const char *n;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to dup connection to stdin: %m");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to dup connection to stdout: %m");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to dup connection: %m");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (asprintf((char**)(envp + n_env++), "LISTEN_FDS=%i", n_fds) < 0)
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (asprintf((char**)(envp + n_env++), "LISTEN_PID=" PID_FMT, getpid()) < 0)
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering e = strappend("LISTEN_FDNAMES=", arg_fdname);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering log_info("Execing %s (%s)", name, joined);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to execp %s (%s): %m", name, joined);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poetteringstatic int fork_and_exec_process(const char* child, char** argv, char **env, int fd) {
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return log_error_errno(errno, "Failed to fork: %m");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek /* In the child */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek /* Make sure the child goes away when the parent dies */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek /* Check whether our parent died before we were able
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek * to set the death signal */
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering log_info("Spawned %s (%s) as PID %d", child, joined, child_pid);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int do_accept(const char* name, char **argv, char **envp, int fd) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering _cleanup_free_ char *local = NULL, *peer = NULL;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering fd_accepted = accept4(fd, NULL, NULL, 0);
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering return log_error_errno(errno, "Failed to accept connection on fd:%d: %m", fd);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering getpeername_pretty(fd_accepted, true, &peer);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering log_info("Connection from %s to %s", strna(peer), strna(local));
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return fork_and_exec_process(name, argv, envp, fd_accepted);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek/* SIGCHLD handler. */
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. Johnstatic void sigchld_hdl(int sig, siginfo_t *t, void *data) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek log_info("Child %d died with code %d", t->si_pid, t->si_status);
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John /* Wait for a dead child. */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int install_chld_handler(void) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sigaction(SIGCHLD, &act, 0);
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek "Listen on sockets and launch child on connection.\n\n"
7e0cdf8f1ff366a1d1770ea92bcce686db8c2dfcLennart Poettering " -h --help Show this help and exit\n"
7e0cdf8f1ff366a1d1770ea92bcce686db8c2dfcLennart Poettering " --version Print version string and exit\n"
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek " -l --listen=ADDR Listen for raw connections at ADDR\n"
7e0cdf8f1ff366a1d1770ea92bcce686db8c2dfcLennart Poettering " -d --datagram Listen on datagram instead of stream socket\n"
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering " --seqpacket Listen on SOCK_SEQPACKET instead of stream socket\n"
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek " -a --accept Spawn separate child for each connection\n"
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek " -E --setenv=NAME[=VALUE] Pass an environment variable to children\n"
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering " --inetd Enable inetd file descriptor passing protocol\n"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek "Note: file descriptors from sd_listen_fds() will be passed through.\n"
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek , program_invocation_short_name);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int parse_argv(int argc, char *argv[]) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek static const struct option options[] = {
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek { "help", no_argument, NULL, 'h' },
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek { "version", no_argument, NULL, ARG_VERSION },
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering { "seqpacket", no_argument, NULL, ARG_SEQPACKET },
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek { "listen", required_argument, NULL, 'l' },
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek { "accept", no_argument, NULL, 'a' },
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek { "setenv", required_argument, NULL, 'E' },
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering { "environment", required_argument, NULL, 'E' }, /* legacy alias */
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering { "fdname", required_argument, NULL, ARG_FDNAME },
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering { "inetd", no_argument, NULL, ARG_INETD },
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani while ((c = getopt_long(argc, argv, "+hl:aEd", options, NULL)) >= 0)
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering log_error("--datagram may not be combined with --seqpacket.");
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering log_error("--seqpacket may not be combined with --datagram.");
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering log_error("File descriptor name %s is not valid, refusing.", optarg);
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek log_error("%s: command to execute is missing.",
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering if (arg_socket_type == SOCK_DGRAM && arg_accept) {
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani log_error("Datagram sockets do not accept connections. "
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani "The --datagram and --accept options may not be combined.");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekint main(int argc, char **argv, char **envp) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek n = open_sockets(&epoll_fd, arg_accept);
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering log_error("No sockets to listen on specified or passed in.");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = epoll_wait(epoll_fd, &event, 1, -1);
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt log_error_errno(errno, "epoll_wait() failed: %m");
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering log_info("Communication attempt on fd %i.", event.data.fd);
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering r = do_accept(argv[optind], argv + optind, envp, event.data.fd);