activate.c revision 175a3d25d0e8596d4ba0759aea3f89ee228e7d6d
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (at your option) any later version.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is distributed in the hope that it will be useful, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Lesser General Public License for more details.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackstatic bool arg_accept = false;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int add_epoll(int epoll_fd, int fd) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to add event on epoll fd:%d for fd:%d: %s",
4f10118016f9b2fd7e1d26c9ef7d91eb33fba694Lennart Poetteringstatic int print_socket(const char* desc, int fd) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = getsockname(fd, &addr.sockaddr.sa, &addr.size);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_warning("Failed to query socket on fd:%d: %m", fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_warning("socket_address_print(): %s", strerror(-r));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_warning("Connection with unknown family %d", family);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int make_socket_fd(const char* address, int flags) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Failed to parse socket: %s", strerror(-r));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fd = socket_address_listen(&a, flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT, NULL, false, false, 0755, 0644, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Failed to listen: %s", strerror(-r));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("socket_address_print(): %s", strerror(-r));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int open_sockets(int *epoll_fd, bool accept) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Failed to read listening file descriptors from environment: %s",
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /** Note: we leak some fd's on error here. I doesn't matter
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * much, since the program will exit immediately anyway, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * would be a pain to fix.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_info("Opening address %s", *address);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fd = make_socket_fd(*address, SOCK_STREAM | (arg_accept*SOCK_CLOEXEC));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to open '%s': %s", *address, strerror(-fd));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(fd == SD_LISTEN_FDS_START + count);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *epoll_fd = epoll_create1(EPOLL_CLOEXEC);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering log_error("Failed to create epoll object: %m");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + count; fd++) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackstatic int launch(char* name, char **argv, char **env, int fds) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack static const char* tocopy[] = {"TERM=", "PATH=", "USER=", "HOME="};
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* PATH, TERM, HOME, USER, LISTEN_FDS, LISTEN_PID, NULL */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_free_ char *p = strappend(*s, "=");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for (i = 0; i < ELEMENTSOF(tocopy); i++) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering envp[n_env] = strv_find_prefix(env, tocopy[i]);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((asprintf((char**)(envp + n_env++), "LISTEN_FDS=%d", fds) < 0) ||
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (asprintf((char**)(envp + n_env++), "LISTEN_PID=%d", getpid()) < 0))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to execp %s (%s): %m", name, tmp);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int launch1(const char* child, char** argv, char **env, int fd) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* In the child */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to dup connection to stdin: %m");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to dup connection to stdout: %m");
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering log_error("Failed to close dupped connection: %m");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Make sure the child goes away when the parent dies */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Check whether our parent died before we were able
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * to set the death signal */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to exec child %s: %m", child);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_info("Spawned %s (%s) as PID %d", child, tmp, child_pid);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int do_accept(const char* name, char **argv, char **envp, int fd) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fd2 = accept(fd, &addr.sockaddr.sa, &addr.size);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Failed to accept connection on fd:%d: %m", fd);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/* SIGCHLD handler. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void sigchld_hdl(int sig, siginfo_t *t, void *data) {
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering log_info("Child %d died with code %d", t->si_pid, t->si_status);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering /* Wait for a dead child. */
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek act.sa_sigaction = sigchld_hdl;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to install SIGCHLD handler: %m");
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidtstatic int help(void) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "Listen on sockets and launch child on connection.\n\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering " -l --listen=ADDR Listen for raw connections at ADDR\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering " -a --accept Spawn separate child for each connection\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering " -h --help Show this help and exit\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering " --version Print version string and exit\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "Note: file descriptors from sd_listen_fds() will be passed through.\n"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering { "listen", required_argument, NULL, 'l' },
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering { "environment", required_argument, NULL, 'E' },
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering while ((c = getopt_long(argc, argv, "+hl:saE:", options, NULL)) >= 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0 /* done */;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r = strv_extend(&arg_listen, optarg);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r = strv_extend(&arg_environ, optarg);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Usage: %s [OPTION...] PROGRAM [OPTION...]",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint main(int argc, char **argv, char **envp) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_info("Communication attempt on fd:%d", event.data.fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = do_accept(argv[optind], argv + optind, envp,