2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek/***
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek This file is part of systemd.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Copyright 2013 Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew 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
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
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-Szmek***/
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include <getopt.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <sys/epoll.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <sys/prctl.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <sys/socket.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <sys/wait.h>
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include <unistd.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering#include "sd-daemon.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "fd-util.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "log.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "macro.h"
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering#include "signal-util.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "socket-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "strv.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic char** arg_listen = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic bool arg_accept = false;
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poetteringstatic int arg_socket_type = SOCK_STREAM;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic char** arg_args = NULL;
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmekstatic char** arg_setenv = NULL;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poetteringstatic const char *arg_fdname = NULL;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poetteringstatic bool arg_inetd = false;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int add_epoll(int epoll_fd, int fd) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering struct epoll_event ev = {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering .events = EPOLLIN
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering };
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(epoll_fd >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(fd >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering ev.data.fd = fd;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt if (r < 0)
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return log_error_errno(errno, "Failed to add event on epoll fd:%d for fd:%d: %m", epoll_fd, fd);
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int open_sockets(int *epoll_fd, bool accept) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering char **address;
29a5ca9baa58e55c4d9e1d008cdd014aa9c3c3e1Lennart Poettering int n, fd, r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int count = 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek n = sd_listen_fds(true);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt if (n < 0)
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering if (n > 0) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering log_info("Received %i descriptors via the environment.", n);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering r = fd_cloexec(fd, arg_accept);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering if (r < 0)
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering count ++;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek /* Close logging and all other descriptors */
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek if (arg_listen) {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek int except[3 + n];
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek for (fd = 0; fd < SD_LISTEN_FDS_START + n; fd++)
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek except[fd] = fd;
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek log_close();
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek close_all_fds(except, 3 + n);
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek }
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
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.
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek */
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek
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));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (fd < 0) {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek log_open();
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt return log_error_errno(fd, "Failed to open '%s': %m", *address);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
175a3d25d0e8596d4ba0759aea3f89ee228e7d6dLennart Poettering assert(fd == SD_LISTEN_FDS_START + count);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek count ++;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek if (arg_listen)
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek log_open();
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek *epoll_fd = epoll_create1(EPOLL_CLOEXEC);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt if (*epoll_fd < 0)
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return log_error_errno(errno, "Failed to create epoll object: %m");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + count; fd++) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering _cleanup_free_ char *name = NULL;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering getsockname_pretty(fd, &name);
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering log_info("Listening on %s as %i.", strna(name), fd);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
29a5ca9baa58e55c4d9e1d008cdd014aa9c3c3e1Lennart Poettering r = add_epoll(*epoll_fd, fd);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return count;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poetteringstatic int exec_process(const char* name, char **argv, char **env, int start_fd, int n_fds) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering _cleanup_strv_free_ char **envp = NULL;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering _cleanup_free_ char *joined = NULL;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering unsigned n_env = 0, length;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering const char *tocopy;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek unsigned i;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering char **s;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering int r;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (arg_inetd && n_fds != 1) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering log_error("--inetd only supported for single file descriptors.");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return -EINVAL;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek length = strv_length(arg_setenv);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering /* PATH, TERM, HOME, USER, LISTEN_FDS, LISTEN_PID, LISTEN_FDNAMES, NULL */
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering envp = new0(char *, length + 8);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering if (!envp)
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering return log_oom();
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek STRV_FOREACH(s, arg_setenv) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering if (strchr(*s, '=')) {
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering char *k;
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering k = strdup(*s);
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering if (!k)
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering return log_oom();
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering envp[n_env++] = k;
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering } else {
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering _cleanup_free_ char *p;
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering const char *n;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering p = strappend(*s, "=");
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek if (!p)
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek return log_oom();
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering n = strv_find_prefix(env, p);
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering if (!n)
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering continue;
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering envp[n_env] = strdup(n);
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering if (!envp[n_env])
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering return log_oom();
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering n_env ++;
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek }
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek }
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering FOREACH_STRING(tocopy, "TERM=", "PATH=", "USER=", "HOME=") {
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering const char *n;
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering n = strv_find_prefix(env, tocopy);
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering if (!n)
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering continue;
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering envp[n_env] = strdup(n);
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering if (!envp[n_env])
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering return log_oom();
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering
fa994f917d51997c6bc125161a473d1ed8d40229Lennart Poettering n_env ++;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (arg_inetd) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering assert(n_fds == 1);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering r = dup2(start_fd, STDIN_FILENO);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (r < 0)
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to dup connection to stdin: %m");
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering r = dup2(start_fd, STDOUT_FILENO);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (r < 0)
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to dup connection to stdout: %m");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering start_fd = safe_close(start_fd);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering } else {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (start_fd != SD_LISTEN_FDS_START) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering assert(n_fds == 1);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering r = dup2(start_fd, SD_LISTEN_FDS_START);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (r < 0)
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to dup connection: %m");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering safe_close(start_fd);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering start_fd = SD_LISTEN_FDS_START;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering }
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (asprintf((char**)(envp + n_env++), "LISTEN_FDS=%i", n_fds) < 0)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering return log_oom();
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (asprintf((char**)(envp + n_env++), "LISTEN_PID=" PID_FMT, getpid()) < 0)
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_oom();
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (arg_fdname) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering char *e;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering e = strappend("LISTEN_FDNAMES=", arg_fdname);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (!e)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering return log_oom();
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering for (i = 1; i < (unsigned) n_fds; i++) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering char *c;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering c = strjoin(e, ":", arg_fdname, NULL);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (!c) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering free(e);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_oom();
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering }
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering free(e);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering e = c;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering envp[n_env++] = e;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering joined = strv_join(argv, " ");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (!joined)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return log_oom();
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering log_info("Execing %s (%s)", name, joined);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek execvpe(name, argv, envp);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return log_error_errno(errno, "Failed to execp %s (%s): %m", name, joined);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poetteringstatic int fork_and_exec_process(const char* child, char** argv, char **env, int fd) {
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering _cleanup_free_ char *joined = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek pid_t parent_pid, child_pid;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering joined = strv_join(argv, " ");
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (!joined)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return log_oom();
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek parent_pid = getpid();
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek child_pid = fork();
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt if (child_pid < 0)
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return log_error_errno(errno, "Failed to fork: %m");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek /* In the child */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (child_pid == 0) {
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering (void) reset_all_signal_handlers();
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering (void) reset_signal_mask();
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering
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 _exit(EXIT_FAILURE);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek /* Check whether our parent died before we were able
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek * to set the death signal */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (getppid() != parent_pid)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _exit(EXIT_SUCCESS);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering exec_process(child, argv, env, fd, 1);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _exit(EXIT_FAILURE);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering log_info("Spawned %s (%s) as PID %d", child, joined, child_pid);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
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 _cleanup_close_ int fd_accepted = -1;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering fd_accepted = accept4(fd, NULL, NULL, 0);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering if (fd_accepted < 0)
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering return log_error_errno(errno, "Failed to accept connection on fd:%d: %m", fd);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering getsockname_pretty(fd_accepted, &local);
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering getpeername_pretty(fd_accepted, true, &peer);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering log_info("Connection from %s to %s", strna(peer), strna(local));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering return fork_and_exec_process(name, argv, envp, fd_accepted);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek/* SIGCHLD handler. */
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. Johnstatic void sigchld_hdl(int sig, siginfo_t *t, void *data) {
9d458c0902cdc5d993fd2f78d36fb83a8130c81bLennart Poettering PROTECT_ERRNO;
9d458c0902cdc5d993fd2f78d36fb83a8130c81bLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek log_info("Child %d died with code %d", t->si_pid, t->si_status);
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John /* Wait for a dead child. */
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering (void) waitpid(t->si_pid, NULL, 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int install_chld_handler(void) {
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering static const struct sigaction act = {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek .sa_flags = SA_SIGINFO,
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek .sa_sigaction = sigchld_hdl,
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek };
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering int r;
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sigaction(SIGCHLD, &act, 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering
08719b64e4de1a1197074fd2e2d18b7259f27232Lennart Poettering return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmekstatic void help(void) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek printf("%s [OPTIONS...]\n\n"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek "Listen on sockets and launch child on connection.\n\n"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek "Options:\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 "\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-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int parse_argv(int argc, char *argv[]) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek enum {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek ARG_VERSION = 0x100,
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering ARG_FDNAME,
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering ARG_SEQPACKET,
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering ARG_INETD,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek };
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
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 },
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani { "datagram", no_argument, NULL, 'd' },
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 },
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering {}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek };
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering int c, r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(argc >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(argv);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani while ((c = getopt_long(argc, argv, "+hl:aEd", options, NULL)) >= 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek switch(c) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case 'h':
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek help();
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case ARG_VERSION:
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering return version();
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering case 'l':
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering r = strv_extend(&arg_listen, optarg);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering return log_oom();
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani case 'd':
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering if (arg_socket_type == SOCK_SEQPACKET) {
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering log_error("--datagram may not be combined with --seqpacket.");
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering return -EINVAL;
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering }
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering arg_socket_type = SOCK_DGRAM;
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering break;
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering case ARG_SEQPACKET:
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering if (arg_socket_type == SOCK_DGRAM) {
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering log_error("--seqpacket may not be combined with --datagram.");
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering return -EINVAL;
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering }
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering arg_socket_type = SOCK_SEQPACKET;
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani break;
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case 'a':
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek arg_accept = true;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering case 'E':
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering r = strv_extend(&arg_setenv, optarg);
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek if (r < 0)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering return log_oom();
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek break;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering case ARG_FDNAME:
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering if (!fdname_is_valid(optarg)) {
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering log_error("File descriptor name %s is not valid, refusing.", optarg);
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering return -EINVAL;
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering }
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering arg_fdname = optarg;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering break;
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering case ARG_INETD:
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering arg_inetd = true;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering break;
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case '?':
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return -EINVAL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek default:
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering assert_not_reached("Unhandled option");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (optind == argc) {
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek log_error("%s: command to execute is missing.",
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek program_invocation_short_name);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return -EINVAL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
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.");
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani return -EINVAL;
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani }
7b7afdfc072b14a4fa4dc195f50becaa7cecc5e8Susant Sahani
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek arg_args = argv + optind;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 1 /* work to do */;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekint main(int argc, char **argv, char **envp) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int r, n;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int epoll_fd = -1;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek log_parse_environment();
eceb8483e5a02e8e337486b89719a3b99cfcb7ceLennart Poettering log_open();
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = parse_argv(argc, argv);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r <= 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = install_chld_handler();
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return EXIT_FAILURE;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek n = open_sockets(&epoll_fd, arg_accept);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (n < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return EXIT_FAILURE;
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering if (n == 0) {
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering log_error("No sockets to listen on specified or passed in.");
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering return EXIT_FAILURE;
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eceb8483e5a02e8e337486b89719a3b99cfcb7ceLennart Poettering for (;;) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek struct epoll_event event;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = epoll_wait(epoll_fd, &event, 1, -1);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (errno == EINTR)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek continue;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt log_error_errno(errno, "epoll_wait() failed: %m");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return EXIT_FAILURE;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering log_info("Communication attempt on fd %i.", event.data.fd);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (arg_accept) {
d31e430f14ea076665973b935003326e5ffcbfc8Lennart Poettering r = do_accept(argv[optind], argv + optind, envp, event.data.fd);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return EXIT_FAILURE;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek } else
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eef0a274e6187d1efb8fffaf66db94b8738662a0Lennart Poettering exec_process(argv[optind], argv + optind, envp, SD_LISTEN_FDS_START, n);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return EXIT_SUCCESS;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}