]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2ca0435b | 2 | |
3f6fd1ba | 3 | #include <getopt.h> |
2ca0435b ZJS |
4 | #include <sys/epoll.h> |
5 | #include <sys/prctl.h> | |
2ca0435b | 6 | #include <sys/wait.h> |
3f6fd1ba | 7 | #include <unistd.h> |
2ca0435b | 8 | |
8dd4c05b | 9 | #include "sd-daemon.h" |
2ca0435b | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
2b2fec7d | 12 | #include "errno-util.h" |
cf98937c | 13 | #include "escape.h" |
b5efdb8a | 14 | #include "fd-util.h" |
2ca0435b | 15 | #include "log.h" |
2ca0435b | 16 | #include "macro.h" |
294bf0c3 | 17 | #include "pretty-print.h" |
df0ff127 | 18 | #include "process-util.h" |
ce30c8dc | 19 | #include "signal-util.h" |
5c3fa98d | 20 | #include "socket-netlink.h" |
3f6fd1ba | 21 | #include "socket-util.h" |
07630cea | 22 | #include "string-util.h" |
3f6fd1ba | 23 | #include "strv.h" |
37ec0fdd | 24 | #include "terminal-util.h" |
ca78ad1d | 25 | #include "util.h" |
2ca0435b | 26 | |
1ace223c | 27 | static char **arg_listen = NULL; |
2ca0435b | 28 | static bool arg_accept = false; |
d31e430f | 29 | static int arg_socket_type = SOCK_STREAM; |
1ace223c SJ |
30 | static char **arg_args = NULL; |
31 | static char **arg_setenv = NULL; | |
cf98937c | 32 | static char **arg_fdnames = NULL; |
eef0a274 | 33 | static bool arg_inetd = false; |
2ca0435b ZJS |
34 | |
35 | static int add_epoll(int epoll_fd, int fd) { | |
30374ebe | 36 | struct epoll_event ev = { |
b1c05b98 ZJS |
37 | .events = EPOLLIN, |
38 | .data.fd = fd, | |
30374ebe | 39 | }; |
2ca0435b ZJS |
40 | |
41 | assert(epoll_fd >= 0); | |
42 | assert(fd >= 0); | |
43 | ||
b1c05b98 | 44 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) |
4a62c710 | 45 | return log_error_errno(errno, "Failed to add event on epoll fd:%d for fd:%d: %m", epoll_fd, fd); |
603938e0 LP |
46 | |
47 | return 0; | |
2ca0435b ZJS |
48 | } |
49 | ||
50 | static int open_sockets(int *epoll_fd, bool accept) { | |
30374ebe | 51 | char **address; |
49aca3b1 | 52 | int n, fd, r, count = 0; |
2ca0435b ZJS |
53 | |
54 | n = sd_listen_fds(true); | |
eb56eb9b MS |
55 | if (n < 0) |
56 | return log_error_errno(n, "Failed to read listening file descriptors from environment: %m"); | |
30374ebe LP |
57 | if (n > 0) { |
58 | log_info("Received %i descriptors via the environment.", n); | |
2ca0435b | 59 | |
30374ebe LP |
60 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { |
61 | r = fd_cloexec(fd, arg_accept); | |
62 | if (r < 0) | |
63 | return r; | |
2ca0435b | 64 | |
313cefa1 | 65 | count++; |
30374ebe | 66 | } |
2ca0435b ZJS |
67 | } |
68 | ||
c0997164 ZJS |
69 | /* Close logging and all other descriptors */ |
70 | if (arg_listen) { | |
49aca3b1 LP |
71 | _cleanup_free_ int *except = NULL; |
72 | int i; | |
c0997164 | 73 | |
49aca3b1 LP |
74 | except = new(int, n); |
75 | if (!except) | |
76 | return log_oom(); | |
77 | ||
78 | for (i = 0; i < n; i++) | |
79 | except[i] = SD_LISTEN_FDS_START + i; | |
c0997164 ZJS |
80 | |
81 | log_close(); | |
49aca3b1 | 82 | r = close_all_fds(except, n); |
7acf581a ZJS |
83 | if (r < 0) |
84 | return log_error_errno(r, "Failed to close all file descriptors: %m"); | |
c0997164 ZJS |
85 | } |
86 | ||
fff40a51 ZJS |
87 | /** Note: we leak some fd's on error here. I doesn't matter |
88 | * much, since the program will exit immediately anyway, but | |
89 | * would be a pain to fix. | |
90 | */ | |
91 | ||
2ca0435b | 92 | STRV_FOREACH(address, arg_listen) { |
1ace223c | 93 | fd = make_socket_fd(LOG_DEBUG, *address, arg_socket_type, (arg_accept * SOCK_CLOEXEC)); |
2ca0435b | 94 | if (fd < 0) { |
c0997164 | 95 | log_open(); |
23bbb0de | 96 | return log_error_errno(fd, "Failed to open '%s': %m", *address); |
2ca0435b ZJS |
97 | } |
98 | ||
175a3d25 | 99 | assert(fd == SD_LISTEN_FDS_START + count); |
313cefa1 | 100 | count++; |
2ca0435b ZJS |
101 | } |
102 | ||
c0997164 ZJS |
103 | if (arg_listen) |
104 | log_open(); | |
105 | ||
2ca0435b | 106 | *epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
4a62c710 MS |
107 | if (*epoll_fd < 0) |
108 | return log_error_errno(errno, "Failed to create epoll object: %m"); | |
2ca0435b ZJS |
109 | |
110 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + count; fd++) { | |
30374ebe LP |
111 | _cleanup_free_ char *name = NULL; |
112 | ||
113 | getsockname_pretty(fd, &name); | |
2c408cb6 | 114 | log_info("Listening on %s as %i.", strna(name), fd); |
30374ebe | 115 | |
29a5ca9b | 116 | r = add_epoll(*epoll_fd, fd); |
2ca0435b ZJS |
117 | if (r < 0) |
118 | return r; | |
119 | } | |
120 | ||
121 | return count; | |
122 | } | |
123 | ||
1ace223c | 124 | static int exec_process(const char *name, char **argv, char **env, int start_fd, size_t n_fds) { |
30374ebe | 125 | _cleanup_strv_free_ char **envp = NULL; |
eef0a274 | 126 | _cleanup_free_ char *joined = NULL; |
da6053d0 | 127 | size_t n_env = 0, length; |
eef0a274 | 128 | const char *tocopy; |
eef0a274 LP |
129 | char **s; |
130 | int r; | |
131 | ||
baaa35ad ZJS |
132 | if (arg_inetd && n_fds != 1) |
133 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
134 | "--inetd only supported for single file descriptors."); | |
2ca0435b | 135 | |
892213bf | 136 | length = strv_length(arg_setenv); |
30374ebe | 137 | |
8dd4c05b LP |
138 | /* PATH, TERM, HOME, USER, LISTEN_FDS, LISTEN_PID, LISTEN_FDNAMES, NULL */ |
139 | envp = new0(char *, length + 8); | |
30374ebe LP |
140 | if (!envp) |
141 | return log_oom(); | |
5e65c93a | 142 | |
892213bf | 143 | STRV_FOREACH(s, arg_setenv) { |
eef0a274 | 144 | |
fa994f91 LP |
145 | if (strchr(*s, '=')) { |
146 | char *k; | |
147 | ||
148 | k = strdup(*s); | |
149 | if (!k) | |
150 | return log_oom(); | |
151 | ||
152 | envp[n_env++] = k; | |
153 | } else { | |
c2b2df60 | 154 | _cleanup_free_ char *p = NULL; |
fa994f91 | 155 | const char *n; |
8dd4c05b | 156 | |
b910cc72 | 157 | p = strjoin(*s, "="); |
5e65c93a ZJS |
158 | if (!p) |
159 | return log_oom(); | |
fa994f91 LP |
160 | |
161 | n = strv_find_prefix(env, p); | |
162 | if (!n) | |
163 | continue; | |
164 | ||
165 | envp[n_env] = strdup(n); | |
166 | if (!envp[n_env]) | |
167 | return log_oom(); | |
eef0a274 | 168 | |
313cefa1 | 169 | n_env++; |
5e65c93a ZJS |
170 | } |
171 | } | |
172 | ||
eef0a274 | 173 | FOREACH_STRING(tocopy, "TERM=", "PATH=", "USER=", "HOME=") { |
fa994f91 LP |
174 | const char *n; |
175 | ||
eef0a274 | 176 | n = strv_find_prefix(env, tocopy); |
fa994f91 LP |
177 | if (!n) |
178 | continue; | |
179 | ||
180 | envp[n_env] = strdup(n); | |
181 | if (!envp[n_env]) | |
182 | return log_oom(); | |
183 | ||
313cefa1 | 184 | n_env++; |
2ca0435b ZJS |
185 | } |
186 | ||
eef0a274 LP |
187 | if (arg_inetd) { |
188 | assert(n_fds == 1); | |
2ca0435b | 189 | |
2b33ab09 | 190 | r = rearrange_stdio(start_fd, start_fd, STDERR_FILENO); /* invalidates start_fd on success + error */ |
eef0a274 | 191 | if (r < 0) |
4355f1c9 | 192 | return log_error_errno(r, "Failed to move fd to stdin+stdout: %m"); |
8dd4c05b | 193 | |
eef0a274 LP |
194 | } else { |
195 | if (start_fd != SD_LISTEN_FDS_START) { | |
196 | assert(n_fds == 1); | |
197 | ||
b1c05b98 | 198 | if (dup2(start_fd, SD_LISTEN_FDS_START) < 0) |
eef0a274 LP |
199 | return log_error_errno(errno, "Failed to dup connection: %m"); |
200 | ||
201 | safe_close(start_fd); | |
eef0a274 LP |
202 | } |
203 | ||
1ace223c | 204 | if (asprintf((char **) (envp + n_env++), "LISTEN_FDS=%zu", n_fds) < 0) |
8dd4c05b LP |
205 | return log_oom(); |
206 | ||
1ace223c | 207 | if (asprintf((char **) (envp + n_env++), "LISTEN_PID=" PID_FMT, getpid_cached()) < 0) |
eef0a274 | 208 | return log_oom(); |
8dd4c05b | 209 | |
cf98937c ZJS |
210 | if (arg_fdnames) { |
211 | _cleanup_free_ char *names = NULL; | |
212 | size_t len; | |
eef0a274 | 213 | char *e; |
cf98937c ZJS |
214 | |
215 | len = strv_length(arg_fdnames); | |
4ffd4705 ZJS |
216 | if (len == 1) |
217 | for (size_t i = 1; i < n_fds; i++) { | |
cf98937c ZJS |
218 | r = strv_extend(&arg_fdnames, arg_fdnames[0]); |
219 | if (r < 0) | |
4ffd4705 | 220 | return log_oom(); |
cf98937c | 221 | } |
4ffd4705 | 222 | else if (len != n_fds) |
da6053d0 | 223 | log_warning("The number of fd names is different than number of fds: %zu vs %zu", len, n_fds); |
eef0a274 | 224 | |
cf98937c ZJS |
225 | names = strv_join(arg_fdnames, ":"); |
226 | if (!names) | |
8dd4c05b | 227 | return log_oom(); |
eef0a274 | 228 | |
b910cc72 | 229 | e = strjoin("LISTEN_FDNAMES=", names); |
cf98937c ZJS |
230 | if (!e) |
231 | return log_oom(); | |
8dd4c05b | 232 | |
eef0a274 | 233 | envp[n_env++] = e; |
8dd4c05b | 234 | } |
8dd4c05b LP |
235 | } |
236 | ||
eef0a274 LP |
237 | joined = strv_join(argv, " "); |
238 | if (!joined) | |
2ca0435b ZJS |
239 | return log_oom(); |
240 | ||
eef0a274 | 241 | log_info("Execing %s (%s)", name, joined); |
2ca0435b | 242 | execvpe(name, argv, envp); |
30374ebe | 243 | |
eef0a274 | 244 | return log_error_errno(errno, "Failed to execp %s (%s): %m", name, joined); |
2ca0435b ZJS |
245 | } |
246 | ||
1ace223c | 247 | static int fork_and_exec_process(const char *child, char **argv, char **env, int fd) { |
eef0a274 | 248 | _cleanup_free_ char *joined = NULL; |
4c253ed1 LP |
249 | pid_t child_pid; |
250 | int r; | |
2ca0435b | 251 | |
eef0a274 LP |
252 | joined = strv_join(argv, " "); |
253 | if (!joined) | |
2ca0435b ZJS |
254 | return log_oom(); |
255 | ||
1ace223c SJ |
256 | r = safe_fork("(activate)", |
257 | FORK_RESET_SIGNALS | FORK_DEATHSIG | FORK_RLIMIT_NOFILE_SAFE | FORK_LOG, | |
258 | &child_pid); | |
4c253ed1 | 259 | if (r < 0) |
b6e1fff1 | 260 | return r; |
4c253ed1 LP |
261 | if (r == 0) { |
262 | /* In the child */ | |
eef0a274 | 263 | exec_process(child, argv, env, fd, 1); |
2ca0435b ZJS |
264 | _exit(EXIT_FAILURE); |
265 | } | |
266 | ||
4c253ed1 | 267 | log_info("Spawned %s (%s) as PID " PID_FMT ".", child, joined, child_pid); |
2ca0435b ZJS |
268 | return 0; |
269 | } | |
270 | ||
1ace223c | 271 | static int do_accept(const char *name, char **argv, char **envp, int fd) { |
30374ebe | 272 | _cleanup_free_ char *local = NULL, *peer = NULL; |
eef0a274 | 273 | _cleanup_close_ int fd_accepted = -1; |
2ca0435b | 274 | |
eef0a274 | 275 | fd_accepted = accept4(fd, NULL, NULL, 0); |
4ff9bc2e LP |
276 | if (fd_accepted < 0) { |
277 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) | |
278 | return 0; | |
279 | ||
08719b64 | 280 | return log_error_errno(errno, "Failed to accept connection on fd:%d: %m", fd); |
4ff9bc2e | 281 | } |
2ca0435b | 282 | |
7ebd758c LP |
283 | (void) getsockname_pretty(fd_accepted, &local); |
284 | (void) getpeername_pretty(fd_accepted, true, &peer); | |
30374ebe | 285 | log_info("Connection from %s to %s", strna(peer), strna(local)); |
2ca0435b | 286 | |
eef0a274 | 287 | return fork_and_exec_process(name, argv, envp, fd_accepted); |
2ca0435b ZJS |
288 | } |
289 | ||
290 | /* SIGCHLD handler. */ | |
5488e52d | 291 | static void sigchld_hdl(int sig) { |
9d458c09 LP |
292 | PROTECT_ERRNO; |
293 | ||
5488e52d EV |
294 | for (;;) { |
295 | siginfo_t si; | |
296 | int r; | |
08719b64 | 297 | |
5488e52d | 298 | si.si_pid = 0; |
1ace223c | 299 | r = waitid(P_ALL, 0, &si, WEXITED | WNOHANG); |
5488e52d EV |
300 | if (r < 0) { |
301 | if (errno != ECHILD) | |
302 | log_error_errno(errno, "Failed to reap children: %m"); | |
303 | return; | |
304 | } | |
305 | if (si.si_pid == 0) | |
306 | return; | |
307 | ||
308 | log_info("Child %d died with code %d", si.si_pid, si.si_status); | |
309 | } | |
2ca0435b ZJS |
310 | } |
311 | ||
312 | static int install_chld_handler(void) { | |
08719b64 | 313 | static const struct sigaction act = { |
1ace223c | 314 | .sa_flags = SA_NOCLDSTOP | SA_RESTART, |
5488e52d | 315 | .sa_handler = sigchld_hdl, |
c0997164 | 316 | }; |
2ca0435b | 317 | |
b1c05b98 | 318 | if (sigaction(SIGCHLD, &act, 0) < 0) |
08719b64 LP |
319 | return log_error_errno(errno, "Failed to install SIGCHLD handler: %m"); |
320 | ||
321 | return 0; | |
2ca0435b ZJS |
322 | } |
323 | ||
37ec0fdd LP |
324 | static int help(void) { |
325 | _cleanup_free_ char *link = NULL; | |
326 | int r; | |
327 | ||
328 | r = terminal_urlify_man("systemd-socket-activate", "1", &link); | |
329 | if (r < 0) | |
330 | return log_oom(); | |
331 | ||
353b2baa LP |
332 | printf("%s [OPTIONS...]\n" |
333 | "\n%sListen on sockets and launch child on connection.%s\n" | |
334 | "\nOptions:\n" | |
cf98937c ZJS |
335 | " -h --help Show this help and exit\n" |
336 | " --version Print version string and exit\n" | |
337 | " -l --listen=ADDR Listen for raw connections at ADDR\n" | |
338 | " -d --datagram Listen on datagram instead of stream socket\n" | |
339 | " --seqpacket Listen on SOCK_SEQPACKET instead of stream socket\n" | |
340 | " -a --accept Spawn separate child for each connection\n" | |
341 | " -E --setenv=NAME[=VALUE] Pass an environment variable to children\n" | |
342 | " --fdname=NAME[:NAME...] Specify names for file descriptors\n" | |
343 | " --inetd Enable inetd file descriptor passing protocol\n" | |
37ec0fdd | 344 | "\nNote: file descriptors from sd_listen_fds() will be passed through.\n" |
bc556335 DDM |
345 | "\nSee the %s for details.\n", |
346 | program_invocation_short_name, | |
347 | ansi_highlight(), | |
348 | ansi_normal(), | |
349 | link); | |
37ec0fdd LP |
350 | |
351 | return 0; | |
2ca0435b ZJS |
352 | } |
353 | ||
354 | static int parse_argv(int argc, char *argv[]) { | |
355 | enum { | |
356 | ARG_VERSION = 0x100, | |
8dd4c05b | 357 | ARG_FDNAME, |
d31e430f | 358 | ARG_SEQPACKET, |
eef0a274 | 359 | ARG_INETD, |
2ca0435b ZJS |
360 | }; |
361 | ||
362 | static const struct option options[] = { | |
892213bf ZJS |
363 | { "help", no_argument, NULL, 'h' }, |
364 | { "version", no_argument, NULL, ARG_VERSION }, | |
7b7afdfc | 365 | { "datagram", no_argument, NULL, 'd' }, |
d31e430f | 366 | { "seqpacket", no_argument, NULL, ARG_SEQPACKET }, |
892213bf ZJS |
367 | { "listen", required_argument, NULL, 'l' }, |
368 | { "accept", no_argument, NULL, 'a' }, | |
369 | { "setenv", required_argument, NULL, 'E' }, | |
8dd4c05b LP |
370 | { "environment", required_argument, NULL, 'E' }, /* legacy alias */ |
371 | { "fdname", required_argument, NULL, ARG_FDNAME }, | |
eef0a274 | 372 | { "inetd", no_argument, NULL, ARG_INETD }, |
eb9da376 | 373 | {} |
2ca0435b ZJS |
374 | }; |
375 | ||
8dd4c05b | 376 | int c, r; |
2ca0435b ZJS |
377 | |
378 | assert(argc >= 0); | |
379 | assert(argv); | |
380 | ||
b722348d | 381 | while ((c = getopt_long(argc, argv, "+hl:aE:d", options, NULL)) >= 0) |
1ace223c | 382 | switch (c) { |
2ca0435b | 383 | case 'h': |
37ec0fdd | 384 | return help(); |
2ca0435b ZJS |
385 | |
386 | case ARG_VERSION: | |
3f6fd1ba | 387 | return version(); |
2ca0435b | 388 | |
8dd4c05b LP |
389 | case 'l': |
390 | r = strv_extend(&arg_listen, optarg); | |
2ca0435b | 391 | if (r < 0) |
8dd4c05b | 392 | return log_oom(); |
2ca0435b ZJS |
393 | |
394 | break; | |
2ca0435b | 395 | |
7b7afdfc | 396 | case 'd': |
baaa35ad ZJS |
397 | if (arg_socket_type == SOCK_SEQPACKET) |
398 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
399 | "--datagram may not be combined with --seqpacket."); | |
d31e430f LP |
400 | |
401 | arg_socket_type = SOCK_DGRAM; | |
402 | break; | |
403 | ||
404 | case ARG_SEQPACKET: | |
baaa35ad ZJS |
405 | if (arg_socket_type == SOCK_DGRAM) |
406 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
407 | "--seqpacket may not be combined with --datagram."); | |
d31e430f LP |
408 | |
409 | arg_socket_type = SOCK_SEQPACKET; | |
7b7afdfc SS |
410 | break; |
411 | ||
2ca0435b ZJS |
412 | case 'a': |
413 | arg_accept = true; | |
414 | break; | |
415 | ||
8dd4c05b LP |
416 | case 'E': |
417 | r = strv_extend(&arg_setenv, optarg); | |
5e65c93a | 418 | if (r < 0) |
8dd4c05b | 419 | return log_oom(); |
5e65c93a ZJS |
420 | |
421 | break; | |
8dd4c05b | 422 | |
cf98937c | 423 | case ARG_FDNAME: { |
c2b2df60 | 424 | _cleanup_strv_free_ char **names = NULL; |
cf98937c ZJS |
425 | char **s; |
426 | ||
427 | names = strv_split(optarg, ":"); | |
428 | if (!names) | |
429 | return log_oom(); | |
430 | ||
431 | STRV_FOREACH(s, names) | |
432 | if (!fdname_is_valid(*s)) { | |
c2b2df60 | 433 | _cleanup_free_ char *esc = NULL; |
163c76c9 | 434 | |
cf98937c ZJS |
435 | esc = cescape(*s); |
436 | log_warning("File descriptor name \"%s\" is not valid.", esc); | |
437 | } | |
438 | ||
439 | /* Empty optargs means one empty name */ | |
440 | r = strv_extend_strv(&arg_fdnames, | |
441 | strv_isempty(names) ? STRV_MAKE("") : names, | |
442 | false); | |
443 | if (r < 0) | |
444 | return log_error_errno(r, "strv_extend_strv: %m"); | |
8dd4c05b | 445 | break; |
cf98937c | 446 | } |
5e65c93a | 447 | |
eef0a274 LP |
448 | case ARG_INETD: |
449 | arg_inetd = true; | |
450 | break; | |
451 | ||
2ca0435b ZJS |
452 | case '?': |
453 | return -EINVAL; | |
454 | ||
455 | default: | |
eb9da376 | 456 | assert_not_reached("Unhandled option"); |
2ca0435b ZJS |
457 | } |
458 | ||
baaa35ad ZJS |
459 | if (optind == argc) |
460 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
461 | "%s: command to execute is missing.", | |
462 | program_invocation_short_name); | |
2ca0435b | 463 | |
baaa35ad ZJS |
464 | if (arg_socket_type == SOCK_DGRAM && arg_accept) |
465 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
466 | "Datagram sockets do not accept connections. " | |
467 | "The --datagram and --accept options may not be combined."); | |
7b7afdfc | 468 | |
2ca0435b ZJS |
469 | arg_args = argv + optind; |
470 | ||
471 | return 1 /* work to do */; | |
472 | } | |
473 | ||
474 | int main(int argc, char **argv, char **envp) { | |
475 | int r, n; | |
476 | int epoll_fd = -1; | |
477 | ||
1a043959 | 478 | log_show_color(true); |
2ca0435b | 479 | log_parse_environment(); |
eceb8483 | 480 | log_open(); |
2ca0435b ZJS |
481 | |
482 | r = parse_argv(argc, argv); | |
483 | if (r <= 0) | |
484 | return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
485 | ||
486 | r = install_chld_handler(); | |
487 | if (r < 0) | |
488 | return EXIT_FAILURE; | |
489 | ||
490 | n = open_sockets(&epoll_fd, arg_accept); | |
491 | if (n < 0) | |
492 | return EXIT_FAILURE; | |
2c408cb6 LP |
493 | if (n == 0) { |
494 | log_error("No sockets to listen on specified or passed in."); | |
495 | return EXIT_FAILURE; | |
496 | } | |
2ca0435b | 497 | |
eceb8483 | 498 | for (;;) { |
2ca0435b ZJS |
499 | struct epoll_event event; |
500 | ||
b1c05b98 | 501 | if (epoll_wait(epoll_fd, &event, 1, -1) < 0) { |
2ca0435b ZJS |
502 | if (errno == EINTR) |
503 | continue; | |
504 | ||
56f64d95 | 505 | log_error_errno(errno, "epoll_wait() failed: %m"); |
2ca0435b ZJS |
506 | return EXIT_FAILURE; |
507 | } | |
508 | ||
2c408cb6 | 509 | log_info("Communication attempt on fd %i.", event.data.fd); |
2ca0435b | 510 | if (arg_accept) { |
d31e430f | 511 | r = do_accept(argv[optind], argv + optind, envp, event.data.fd); |
2ca0435b ZJS |
512 | if (r < 0) |
513 | return EXIT_FAILURE; | |
514 | } else | |
515 | break; | |
516 | } | |
517 | ||
da6053d0 | 518 | exec_process(argv[optind], argv + optind, envp, SD_LISTEN_FDS_START, (size_t) n); |
2ca0435b ZJS |
519 | |
520 | return EXIT_SUCCESS; | |
521 | } |