1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/prctl.h>
24 #include <sys/socket.h>
28 #include "sd-daemon.h"
30 #include "alloc-util.h"
35 #include "process-util.h"
36 #include "signal-util.h"
37 #include "socket-util.h"
38 #include "string-util.h"
41 static char** arg_listen
= NULL
;
42 static bool arg_accept
= false;
43 static int arg_socket_type
= SOCK_STREAM
;
44 static char** arg_args
= NULL
;
45 static char** arg_setenv
= NULL
;
46 static char **arg_fdnames
= NULL
;
47 static bool arg_inetd
= false;
49 static int add_epoll(int epoll_fd
, int fd
) {
50 struct epoll_event ev
= {
55 assert(epoll_fd
>= 0);
59 r
= epoll_ctl(epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
);
61 return log_error_errno(errno
, "Failed to add event on epoll fd:%d for fd:%d: %m", epoll_fd
, fd
);
66 static int open_sockets(int *epoll_fd
, bool accept
) {
71 n
= sd_listen_fds(true);
73 return log_error_errno(n
, "Failed to read listening file descriptors from environment: %m");
75 log_info("Received %i descriptors via the environment.", n
);
77 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
78 r
= fd_cloexec(fd
, arg_accept
);
86 /* Close logging and all other descriptors */
90 for (fd
= 0; fd
< SD_LISTEN_FDS_START
+ n
; fd
++)
94 close_all_fds(except
, 3 + n
);
97 /** Note: we leak some fd's on error here. I doesn't matter
98 * much, since the program will exit immediately anyway, but
99 * would be a pain to fix.
102 STRV_FOREACH(address
, arg_listen
) {
103 fd
= make_socket_fd(LOG_DEBUG
, *address
, arg_socket_type
, (arg_accept
*SOCK_CLOEXEC
));
106 return log_error_errno(fd
, "Failed to open '%s': %m", *address
);
109 assert(fd
== SD_LISTEN_FDS_START
+ count
);
116 *epoll_fd
= epoll_create1(EPOLL_CLOEXEC
);
118 return log_error_errno(errno
, "Failed to create epoll object: %m");
120 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ count
; fd
++) {
121 _cleanup_free_
char *name
= NULL
;
123 getsockname_pretty(fd
, &name
);
124 log_info("Listening on %s as %i.", strna(name
), fd
);
126 r
= add_epoll(*epoll_fd
, fd
);
134 static int exec_process(const char* name
, char **argv
, char **env
, int start_fd
, int n_fds
) {
136 _cleanup_strv_free_
char **envp
= NULL
;
137 _cleanup_free_
char *joined
= NULL
;
138 unsigned n_env
= 0, length
;
143 if (arg_inetd
&& n_fds
!= 1) {
144 log_error("--inetd only supported for single file descriptors.");
148 length
= strv_length(arg_setenv
);
150 /* PATH, TERM, HOME, USER, LISTEN_FDS, LISTEN_PID, LISTEN_FDNAMES, NULL */
151 envp
= new0(char *, length
+ 8);
155 STRV_FOREACH(s
, arg_setenv
) {
157 if (strchr(*s
, '=')) {
166 _cleanup_free_
char *p
;
169 p
= strappend(*s
, "=");
173 n
= strv_find_prefix(env
, p
);
177 envp
[n_env
] = strdup(n
);
185 FOREACH_STRING(tocopy
, "TERM=", "PATH=", "USER=", "HOME=") {
188 n
= strv_find_prefix(env
, tocopy
);
192 envp
[n_env
] = strdup(n
);
202 r
= rearrange_stdio(start_fd
, start_fd
, STDERR_FILENO
); /* invalidates start_fd on success + error */
204 return log_error_errno(errno
, "Failed to move fd to stdin+stdout: %m");
207 if (start_fd
!= SD_LISTEN_FDS_START
) {
210 r
= dup2(start_fd
, SD_LISTEN_FDS_START
);
212 return log_error_errno(errno
, "Failed to dup connection: %m");
214 safe_close(start_fd
);
215 start_fd
= SD_LISTEN_FDS_START
;
218 if (asprintf((char**)(envp
+ n_env
++), "LISTEN_FDS=%i", n_fds
) < 0)
221 if (asprintf((char**)(envp
+ n_env
++), "LISTEN_PID=" PID_FMT
, getpid_cached()) < 0)
225 _cleanup_free_
char *names
= NULL
;
230 len
= strv_length(arg_fdnames
);
232 for (i
= 1; i
< n_fds
; i
++) {
233 r
= strv_extend(&arg_fdnames
, arg_fdnames
[0]);
235 return log_error_errno(r
, "Failed to extend strv: %m");
237 else if (len
!= (unsigned) n_fds
)
238 log_warning("The number of fd names is different than number of fds: %zu vs %d",
241 names
= strv_join(arg_fdnames
, ":");
245 e
= strappend("LISTEN_FDNAMES=", names
);
253 joined
= strv_join(argv
, " ");
257 log_info("Execing %s (%s)", name
, joined
);
258 execvpe(name
, argv
, envp
);
260 return log_error_errno(errno
, "Failed to execp %s (%s): %m", name
, joined
);
263 static int fork_and_exec_process(const char* child
, char** argv
, char **env
, int fd
) {
264 _cleanup_free_
char *joined
= NULL
;
268 joined
= strv_join(argv
, " ");
272 r
= safe_fork("(activate)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &child_pid
);
277 exec_process(child
, argv
, env
, fd
, 1);
281 log_info("Spawned %s (%s) as PID " PID_FMT
".", child
, joined
, child_pid
);
285 static int do_accept(const char* name
, char **argv
, char **envp
, int fd
) {
286 _cleanup_free_
char *local
= NULL
, *peer
= NULL
;
287 _cleanup_close_
int fd_accepted
= -1;
289 fd_accepted
= accept4(fd
, NULL
, NULL
, 0);
291 return log_error_errno(errno
, "Failed to accept connection on fd:%d: %m", fd
);
293 getsockname_pretty(fd_accepted
, &local
);
294 getpeername_pretty(fd_accepted
, true, &peer
);
295 log_info("Connection from %s to %s", strna(peer
), strna(local
));
297 return fork_and_exec_process(name
, argv
, envp
, fd_accepted
);
300 /* SIGCHLD handler. */
301 static void sigchld_hdl(int sig
) {
309 r
= waitid(P_ALL
, 0, &si
, WEXITED
|WNOHANG
);
312 log_error_errno(errno
, "Failed to reap children: %m");
318 log_info("Child %d died with code %d", si
.si_pid
, si
.si_status
);
322 static int install_chld_handler(void) {
323 static const struct sigaction act
= {
324 .sa_flags
= SA_NOCLDSTOP
|SA_RESTART
,
325 .sa_handler
= sigchld_hdl
,
330 r
= sigaction(SIGCHLD
, &act
, 0);
332 return log_error_errno(errno
, "Failed to install SIGCHLD handler: %m");
337 static void help(void) {
338 printf("%s [OPTIONS...]\n\n"
339 "Listen on sockets and launch child on connection.\n\n"
341 " -h --help Show this help and exit\n"
342 " --version Print version string and exit\n"
343 " -l --listen=ADDR Listen for raw connections at ADDR\n"
344 " -d --datagram Listen on datagram instead of stream socket\n"
345 " --seqpacket Listen on SOCK_SEQPACKET instead of stream socket\n"
346 " -a --accept Spawn separate child for each connection\n"
347 " -E --setenv=NAME[=VALUE] Pass an environment variable to children\n"
348 " --fdname=NAME[:NAME...] Specify names for file descriptors\n"
349 " --inetd Enable inetd file descriptor passing protocol\n"
351 "Note: file descriptors from sd_listen_fds() will be passed through.\n"
352 , program_invocation_short_name
);
355 static int parse_argv(int argc
, char *argv
[]) {
363 static const struct option options
[] = {
364 { "help", no_argument
, NULL
, 'h' },
365 { "version", no_argument
, NULL
, ARG_VERSION
},
366 { "datagram", no_argument
, NULL
, 'd' },
367 { "seqpacket", no_argument
, NULL
, ARG_SEQPACKET
},
368 { "listen", required_argument
, NULL
, 'l' },
369 { "accept", no_argument
, NULL
, 'a' },
370 { "setenv", required_argument
, NULL
, 'E' },
371 { "environment", required_argument
, NULL
, 'E' }, /* legacy alias */
372 { "fdname", required_argument
, NULL
, ARG_FDNAME
},
373 { "inetd", no_argument
, NULL
, ARG_INETD
},
382 while ((c
= getopt_long(argc
, argv
, "+hl:aE:d", options
, NULL
)) >= 0)
392 r
= strv_extend(&arg_listen
, optarg
);
399 if (arg_socket_type
== SOCK_SEQPACKET
) {
400 log_error("--datagram may not be combined with --seqpacket.");
404 arg_socket_type
= SOCK_DGRAM
;
408 if (arg_socket_type
== SOCK_DGRAM
) {
409 log_error("--seqpacket may not be combined with --datagram.");
413 arg_socket_type
= SOCK_SEQPACKET
;
421 r
= strv_extend(&arg_setenv
, optarg
);
428 _cleanup_strv_free_
char **names
;
431 names
= strv_split(optarg
, ":");
435 STRV_FOREACH(s
, names
)
436 if (!fdname_is_valid(*s
)) {
437 _cleanup_free_
char *esc
;
440 log_warning("File descriptor name \"%s\" is not valid.", esc
);
443 /* Empty optargs means one empty name */
444 r
= strv_extend_strv(&arg_fdnames
,
445 strv_isempty(names
) ? STRV_MAKE("") : names
,
448 return log_error_errno(r
, "strv_extend_strv: %m");
460 assert_not_reached("Unhandled option");
463 if (optind
== argc
) {
464 log_error("%s: command to execute is missing.",
465 program_invocation_short_name
);
469 if (arg_socket_type
== SOCK_DGRAM
&& arg_accept
) {
470 log_error("Datagram sockets do not accept connections. "
471 "The --datagram and --accept options may not be combined.");
475 arg_args
= argv
+ optind
;
477 return 1 /* work to do */;
480 int main(int argc
, char **argv
, char **envp
) {
484 log_parse_environment();
487 r
= parse_argv(argc
, argv
);
489 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
491 r
= install_chld_handler();
495 n
= open_sockets(&epoll_fd
, arg_accept
);
499 log_error("No sockets to listen on specified or passed in.");
504 struct epoll_event event
;
506 r
= epoll_wait(epoll_fd
, &event
, 1, -1);
511 log_error_errno(errno
, "epoll_wait() failed: %m");
515 log_info("Communication attempt on fd %i.", event
.data
.fd
);
517 r
= do_accept(argv
[optind
], argv
+ optind
, envp
, event
.data
.fd
);
524 exec_process(argv
[optind
], argv
+ optind
, envp
, SD_LISTEN_FDS_START
, n
);