]> git.ipfire.org Git - people/ms/systemd.git/blame - socket.c
macro: drop double __ prefix to make sure we don't collide with gcc/glibc definitions
[people/ms/systemd.git] / socket.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
83c60c9f
LP
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <errno.h>
26#include <fcntl.h>
f94ea366 27#include <sys/epoll.h>
034c6ed7 28#include <signal.h>
83c60c9f 29
87f0e418 30#include "unit.h"
5cb5a6ff 31#include "socket.h"
83c60c9f
LP
32#include "log.h"
33
acbb0225 34static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
87f0e418
LP
35 [SOCKET_DEAD] = UNIT_INACTIVE,
36 [SOCKET_START_PRE] = UNIT_ACTIVATING,
37 [SOCKET_START_POST] = UNIT_ACTIVATING,
38 [SOCKET_LISTENING] = UNIT_ACTIVE,
39 [SOCKET_RUNNING] = UNIT_ACTIVE,
40 [SOCKET_STOP_PRE] = UNIT_DEACTIVATING,
41 [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING,
42 [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING,
43 [SOCKET_STOP_POST] = UNIT_DEACTIVATING,
44 [SOCKET_STOP_POST_SIGTERM] = UNIT_DEACTIVATING,
45 [SOCKET_STOP_POST_SIGKILL] = UNIT_DEACTIVATING,
46 [SOCKET_MAINTAINANCE] = UNIT_INACTIVE,
83c60c9f 47};
5cb5a6ff 48
acbb0225
LP
49static const char* const state_string_table[_SOCKET_STATE_MAX] = {
50 [SOCKET_DEAD] = "dead",
51 [SOCKET_START_PRE] = "start-pre",
52 [SOCKET_START_POST] = "start-post",
53 [SOCKET_LISTENING] = "listening",
54 [SOCKET_RUNNING] = "running",
55 [SOCKET_STOP_PRE] = "stop-pre",
56 [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
57 [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
58 [SOCKET_STOP_POST] = "stop-post",
59 [SOCKET_STOP_POST_SIGTERM] = "stop-post-sigterm",
60 [SOCKET_STOP_POST_SIGKILL] = "stop-post-sigkill",
61 [SOCKET_MAINTAINANCE] = "maintainance"
62};
63
87f0e418
LP
64static void socket_done(Unit *u) {
65 Socket *s = SOCKET(u);
034c6ed7
LP
66 SocketPort *p;
67
68 assert(s);
69
70 while ((p = s->ports)) {
71 LIST_REMOVE(SocketPort, port, s->ports, p);
72
73 if (p->fd >= 0)
74 close_nointr(p->fd);
75 free(p->path);
76 free(p);
77 }
78
79 exec_context_done(&s->exec_context);
80 exec_command_free_array(s->exec_command, _SOCKET_EXEC_MAX);
81 s->control_command = NULL;
82
83 if (s->control_pid > 0) {
87f0e418 84 unit_unwatch_pid(u, s->control_pid);
034c6ed7
LP
85 s->control_pid = 0;
86 }
87
88 s->service = NULL;
89
acbb0225
LP
90 free(s->bind_to_device);
91
92 unit_unwatch_timer(u, &s->timer_watch);
5cb5a6ff
LP
93}
94
87f0e418
LP
95static int socket_init(Unit *u) {
96 Socket *s = SOCKET(u);
44d8db9e
LP
97 char *t;
98 int r;
99
100 /* First, reset everything to the defaults, in case this is a
101 * reload */
102
103 s->state = 0;
acbb0225 104 s->timer_watch.type = WATCH_INVALID;
44d8db9e
LP
105 s->bind_ipv6_only = false;
106 s->backlog = SOMAXCONN;
107 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
108 exec_context_init(&s->exec_context);
109
d46de8a1
LP
110 if ((r = unit_load_fragment_and_dropin(u)) <= 0) {
111 if (r == 0)
112 r = -ENOENT;
44d8db9e 113 goto fail;
d46de8a1 114 }
44d8db9e 115
87f0e418 116 if (!(t = unit_name_change_suffix(unit_id(u), ".service"))) {
44d8db9e
LP
117 r = -ENOMEM;
118 goto fail;
119 }
120
87f0e418 121 r = manager_load_unit(u->meta.manager, t, (Unit**) &s->service);
44d8db9e
LP
122 free(t);
123
124 if (r < 0)
125 goto fail;
126
87f0e418 127 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0)
44d8db9e
LP
128 goto fail;
129
130 return 0;
131
132fail:
87f0e418 133 socket_done(u);
44d8db9e
LP
134 return r;
135}
136
542563ba
LP
137static const char* listen_lookup(int type) {
138
139 if (type == SOCK_STREAM)
140 return "ListenStream";
141 else if (type == SOCK_DGRAM)
142 return "ListenDatagram";
143 else if (type == SOCK_SEQPACKET)
144 return "ListenSequentialPacket";
145
034c6ed7 146 assert_not_reached("Unknown socket type");
542563ba
LP
147 return NULL;
148}
149
87f0e418 150static void socket_dump(Unit *u, FILE *f, const char *prefix) {
5cb5a6ff 151
5cb5a6ff
LP
152 static const char* const command_table[_SOCKET_EXEC_MAX] = {
153 [SOCKET_EXEC_START_PRE] = "StartPre",
154 [SOCKET_EXEC_START_POST] = "StartPost",
155 [SOCKET_EXEC_STOP_PRE] = "StopPre",
156 [SOCKET_EXEC_STOP_POST] = "StopPost"
157 };
158
159 SocketExecCommand c;
87f0e418 160 Socket *s = SOCKET(u);
542563ba 161 SocketPort *p;
c43d20a0 162 char *prefix2;
5cb5a6ff
LP
163
164 assert(s);
fa068367 165 assert(f);
5cb5a6ff 166
c43d20a0
LP
167 prefix2 = strappend(prefix, "\t");
168 if (!prefix2)
169 prefix2 = "";
170
5cb5a6ff
LP
171 fprintf(f,
172 "%sSocket State: %s\n"
542563ba
LP
173 "%sBindIPv6Only: %s\n"
174 "%sBacklog: %u\n",
acbb0225 175 prefix, state_string_table[s->state],
542563ba
LP
176 prefix, yes_no(s->bind_ipv6_only),
177 prefix, s->backlog);
178
acbb0225
LP
179 if (s->bind_to_device)
180 fprintf(f,
181 "%sBindToDevice: %s\n",
182 prefix, s->bind_to_device);
183
034c6ed7 184 LIST_FOREACH(port, p, s->ports) {
5cb5a6ff 185
542563ba
LP
186 if (p->type == SOCKET_SOCKET) {
187 const char *t;
188 int r;
189 char *k;
190
191 if ((r = socket_address_print(&p->address, &k)) < 0)
192 t = strerror(-r);
193 else
194 t = k;
195
196 fprintf(f, "%s%s: %s\n", prefix, listen_lookup(p->address.type), k);
197 free(k);
198 } else
199 fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
200 }
5cb5a6ff
LP
201
202 exec_context_dump(&s->exec_context, f, prefix);
203
204 for (c = 0; c < _SOCKET_EXEC_MAX; c++) {
c43d20a0
LP
205 if (!s->exec_command[c])
206 continue;
5cb5a6ff 207
c43d20a0
LP
208 fprintf(f, "%s→ %s:\n",
209 prefix, command_table[c]);
210
211 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 212 }
c43d20a0
LP
213
214 free(prefix2);
5cb5a6ff
LP
215}
216
034c6ed7 217static void socket_close_fds(Socket *s) {
83c60c9f
LP
218 SocketPort *p;
219
220 assert(s);
221
034c6ed7 222 LIST_FOREACH(port, p, s->ports) {
83c60c9f
LP
223 if (p->fd < 0)
224 continue;
225
acbb0225 226 unit_unwatch_fd(UNIT(s), &p->fd_watch);
9152c765
LP
227 assert_se(close_nointr(p->fd) >= 0);
228
83c60c9f
LP
229 p->fd = -1;
230 }
231}
232
034c6ed7 233static int socket_open_fds(Socket *s) {
83c60c9f
LP
234 SocketPort *p;
235 int r;
236
237 assert(s);
238
034c6ed7 239 LIST_FOREACH(port, p, s->ports) {
83c60c9f 240
034c6ed7
LP
241 if (p->fd >= 0)
242 continue;
83c60c9f
LP
243
244 if (p->type == SOCKET_SOCKET) {
245
acbb0225 246 if ((r = socket_address_listen(&p->address, s->backlog, s->bind_ipv6_only, s->bind_to_device, &p->fd)) < 0)
83c60c9f
LP
247 goto rollback;
248
249 } else {
250 struct stat st;
251 assert(p->type == SOCKET_FIFO);
252
253 if (mkfifo(p->path, 0666 & ~s->exec_context.umask) < 0 && errno != EEXIST) {
254 r = -errno;
255 goto rollback;
256 }
257
258 if ((p->fd = open(p->path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) {
259 r = -errno;
260 goto rollback;
261 }
262
263 if (fstat(p->fd, &st) < 0) {
264 r = -errno;
265 goto rollback;
266 }
267
268 /* FIXME verify user, access mode */
269
270 if (!S_ISFIFO(st.st_mode)) {
271 r = -EEXIST;
272 goto rollback;
273 }
274 }
034c6ed7
LP
275 }
276
277 return 0;
278
279rollback:
280 socket_close_fds(s);
281 return r;
282}
283
284static void socket_unwatch_fds(Socket *s) {
285 SocketPort *p;
9152c765 286
034c6ed7
LP
287 assert(s);
288
289 LIST_FOREACH(port, p, s->ports) {
290 if (p->fd < 0)
291 continue;
292
acbb0225 293 unit_unwatch_fd(UNIT(s), &p->fd_watch);
83c60c9f 294 }
034c6ed7
LP
295}
296
297static int socket_watch_fds(Socket *s) {
298 SocketPort *p;
299 int r;
300
301 assert(s);
83c60c9f 302
034c6ed7
LP
303 LIST_FOREACH(port, p, s->ports) {
304 if (p->fd < 0)
305 continue;
306
f94ea366 307 if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0)
034c6ed7
LP
308 goto fail;
309 }
83c60c9f 310
542563ba 311 return 0;
83c60c9f 312
034c6ed7
LP
313fail:
314 socket_unwatch_fds(s);
315 return r;
316}
317
318static void socket_set_state(Socket *s, SocketState state) {
319 SocketState old_state;
320 assert(s);
321
322 old_state = s->state;
323 s->state = state;
324
325 if (state != SOCKET_START_PRE &&
326 state != SOCKET_START_POST &&
327 state != SOCKET_STOP_PRE &&
328 state != SOCKET_STOP_PRE_SIGTERM &&
329 state != SOCKET_STOP_PRE_SIGKILL &&
330 state != SOCKET_STOP_POST &&
331 state != SOCKET_STOP_POST_SIGTERM &&
332 state != SOCKET_STOP_POST_SIGKILL)
acbb0225 333 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7
LP
334
335 if (state != SOCKET_START_PRE &&
336 state != SOCKET_START_POST &&
337 state != SOCKET_STOP_PRE &&
338 state != SOCKET_STOP_PRE_SIGTERM &&
339 state != SOCKET_STOP_PRE_SIGKILL &&
340 state != SOCKET_STOP_POST &&
341 state != SOCKET_STOP_POST_SIGTERM &&
342 state != SOCKET_STOP_POST_SIGKILL)
acbb0225 343 if (s->control_pid > 0) {
87f0e418 344 unit_unwatch_pid(UNIT(s), s->control_pid);
034c6ed7
LP
345 s->control_pid = 0;
346 }
347
348 if (state != SOCKET_START_PRE &&
349 state != SOCKET_START_POST &&
350 state != SOCKET_STOP_PRE &&
351 state != SOCKET_STOP_POST)
352 s->control_command = NULL;
353
354 if (state != SOCKET_START_POST &&
355 state != SOCKET_LISTENING &&
356 state != SOCKET_RUNNING &&
357 state != SOCKET_STOP_PRE &&
358 state != SOCKET_STOP_PRE_SIGTERM &&
359 state != SOCKET_STOP_PRE_SIGKILL)
360 socket_close_fds(s);
361
362 if (state != SOCKET_LISTENING)
363 socket_unwatch_fds(s);
364
d6ea93e3 365 log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
acbb0225
LP
366
367 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
034c6ed7
LP
368}
369
370static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
371 pid_t pid;
372 int r;
373
374 assert(s);
375 assert(c);
376 assert(_pid);
377
378 if (timeout) {
acbb0225 379 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
034c6ed7
LP
380 goto fail;
381 } else
acbb0225 382 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7
LP
383
384 if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
385 goto fail;
386
87f0e418 387 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
034c6ed7
LP
388 /* FIXME: we need to do something here */
389 goto fail;
83c60c9f 390
034c6ed7
LP
391 *_pid = pid;
392
393 return 0;
394
395fail:
396 if (timeout)
acbb0225 397 unit_unwatch_timer(UNIT(s), &s->timer_watch);
83c60c9f
LP
398
399 return r;
542563ba
LP
400}
401
034c6ed7
LP
402static void socket_enter_dead(Socket *s, bool success) {
403 assert(s);
404
405 if (!success)
406 s->failure = true;
407
408 socket_set_state(s, s->failure ? SOCKET_MAINTAINANCE : SOCKET_DEAD);
409}
410
411static void socket_enter_stop_post(Socket *s, bool success) {
412 int r;
413 assert(s);
414
415 if (!success)
416 s->failure = true;
417
d6ea93e3 418 if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]))
034c6ed7
LP
419 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
420 goto fail;
421
d6ea93e3
LP
422 socket_set_state(s, SOCKET_STOP_POST);
423
424 if (!s->control_command)
034c6ed7
LP
425 socket_enter_dead(s, true);
426
427 return;
428
429fail:
87f0e418 430 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
431 socket_enter_dead(s, false);
432}
433
434static void socket_enter_signal(Socket *s, SocketState state, bool success) {
435 int r;
436
437 assert(s);
438
439 if (!success)
440 s->failure = true;
441
442 if (s->control_pid > 0) {
443 int sig;
444
445 sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_POST_SIGTERM) ? SIGTERM : SIGKILL;
446
447 if (kill(s->control_pid, sig) < 0 && errno != ESRCH) {
448 r = -errno;
449 goto fail;
450 }
d6ea93e3 451 }
034c6ed7 452
d6ea93e3
LP
453 socket_set_state(s, state);
454
455 if (s->control_pid <= 0)
034c6ed7
LP
456 socket_enter_dead(s, true);
457
458 return;
459
460fail:
87f0e418 461 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
462
463 if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
464 socket_enter_stop_post(s, false);
465 else
466 socket_enter_dead(s, false);
467}
468
469static void socket_enter_stop_pre(Socket *s, bool success) {
470 int r;
471 assert(s);
472
473 if (!success)
474 s->failure = true;
475
d6ea93e3 476 if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]))
034c6ed7
LP
477 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
478 goto fail;
479
d6ea93e3
LP
480 socket_set_state(s, SOCKET_STOP_PRE);
481
482 if (!s->control_command)
034c6ed7
LP
483 socket_enter_stop_post(s, true);
484
485 return;
486
487fail:
87f0e418 488 log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
489 socket_enter_stop_post(s, false);
490}
491
e9af15c3
LP
492static void socket_enter_listening(Socket *s) {
493 int r;
494 assert(s);
495
496 if ((r = socket_watch_fds(s)) < 0) {
497 log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s)), strerror(-r));
498 goto fail;
499 }
500
501 socket_set_state(s, SOCKET_LISTENING);
502 return;
503
504fail:
505 socket_enter_stop_pre(s, false);
506}
507
034c6ed7
LP
508static void socket_enter_start_post(Socket *s) {
509 int r;
510 assert(s);
511
e9af15c3 512 if ((r = socket_open_fds(s)) < 0) {
87f0e418 513 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
514 goto fail;
515 }
516
d6ea93e3 517 if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST]))
034c6ed7 518 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) {
87f0e418 519 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
520 goto fail;
521 }
522
d6ea93e3
LP
523 socket_set_state(s, SOCKET_START_POST);
524
525 if (!s->control_command)
e9af15c3 526 socket_enter_listening(s);
034c6ed7
LP
527
528 return;
529
530fail:
531 socket_enter_stop_pre(s, false);
532}
533
534static void socket_enter_start_pre(Socket *s) {
535 int r;
536 assert(s);
537
d6ea93e3 538 if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE]))
034c6ed7
LP
539 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
540 goto fail;
541
d6ea93e3
LP
542 socket_set_state(s, SOCKET_START_PRE);
543
544 if (!s->control_command)
034c6ed7
LP
545 socket_enter_start_post(s);
546
547 return;
548
549fail:
87f0e418 550 log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
551 socket_enter_dead(s, false);
552}
553
554static void socket_enter_running(Socket *s) {
555 int r;
556
557 assert(s);
558
87f0e418 559 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, NULL)) < 0)
034c6ed7
LP
560 goto fail;
561
562 socket_set_state(s, SOCKET_RUNNING);
563 return;
564
565fail:
87f0e418 566 log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
567 socket_enter_dead(s, false);
568}
569
570static void socket_run_next(Socket *s, bool success) {
571 int r;
572
573 assert(s);
574 assert(s->control_command);
575 assert(s->control_command->command_next);
576
577 if (!success)
578 s->failure = true;
579
580 s->control_command = s->control_command->command_next;
581
582 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
583 goto fail;
584
585 return;
586
587fail:
588 if (s->state == SOCKET_STOP_PRE)
589 socket_enter_stop_post(s, false);
590 else if (s->state == SOCKET_STOP_POST)
591 socket_enter_dead(s, false);
592 else
593 socket_enter_stop_pre(s, false);
594}
595
87f0e418
LP
596static int socket_start(Unit *u) {
597 Socket *s = SOCKET(u);
83c60c9f
LP
598
599 assert(s);
600
034c6ed7
LP
601 /* We cannot fulfill this request right now, try again later
602 * please! */
603 if (s->state == SOCKET_STOP_PRE ||
604 s->state == SOCKET_STOP_PRE_SIGKILL ||
605 s->state == SOCKET_STOP_PRE_SIGTERM ||
606 s->state == SOCKET_STOP_POST ||
607 s->state == SOCKET_STOP_POST_SIGTERM ||
608 s->state == SOCKET_STOP_POST_SIGKILL)
609 return -EAGAIN;
610
83c60c9f
LP
611 if (s->state == SOCKET_START_PRE ||
612 s->state == SOCKET_START_POST)
034c6ed7 613 return 0;
83c60c9f 614
034c6ed7 615 /* Cannot run this without the service being around */
87f0e418 616 if (s->service->meta.load_state != UNIT_LOADED)
034c6ed7 617 return -ENOENT;
83c60c9f 618
034c6ed7 619 assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTAINANCE);
83c60c9f 620
034c6ed7
LP
621 s->failure = false;
622 socket_enter_start_pre(s);
623 return 0;
624}
83c60c9f 625
87f0e418
LP
626static int socket_stop(Unit *u) {
627 Socket *s = SOCKET(u);
034c6ed7
LP
628
629 assert(s);
630
631 /* We cannot fulfill this request right now, try again later
632 * please! */
633 if (s->state == SOCKET_START_PRE ||
634 s->state == SOCKET_START_POST)
635 return -EAGAIN;
83c60c9f 636
034c6ed7 637 assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
83c60c9f 638
034c6ed7 639 socket_enter_stop_pre(s, true);
542563ba
LP
640 return 0;
641}
642
87f0e418
LP
643static UnitActiveState socket_active_state(Unit *u) {
644 assert(u);
5cb5a6ff 645
acbb0225 646 return state_translation_table[SOCKET(u)->state];
5cb5a6ff
LP
647}
648
acbb0225 649static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
87f0e418 650 Socket *s = SOCKET(u);
9152c765 651
034c6ed7 652 assert(s);
9152c765 653
e9af15c3 654 log_debug("Incoming traffic on %s", unit_id(u));
9152c765 655
f94ea366 656 if (events != EPOLLIN)
034c6ed7 657 socket_enter_stop_pre(s, false);
9152c765 658
034c6ed7 659 socket_enter_running(s);
9152c765
LP
660}
661
87f0e418
LP
662static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
663 Socket *s = SOCKET(u);
034c6ed7 664 bool success;
5cb5a6ff
LP
665
666 assert(s);
034c6ed7 667 assert(pid >= 0);
5cb5a6ff 668
bd982a8b 669 success = code == CLD_EXITED && status == 0;
034c6ed7 670 s->failure = s->failure || !success;
542563ba 671
034c6ed7
LP
672 assert(s->control_pid == pid);
673 assert(s->control_command);
674
675 exec_status_fill(&s->control_command->exec_status, pid, code, status);
676 s->control_pid = 0;
677
94f04347 678 log_debug("%s control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
034c6ed7
LP
679
680 if (s->control_command->command_next &&
acbb0225
LP
681 (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST))) {
682 log_debug("%s running next command for the state %s", unit_id(u), state_string_table[s->state]);
034c6ed7 683 socket_run_next(s, success);
acbb0225 684 } else {
034c6ed7
LP
685 /* No further commands for this step, so let's figure
686 * out what to do next */
5cb5a6ff 687
bd982a8b 688 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
acbb0225 689
034c6ed7
LP
690 switch (s->state) {
691
692 case SOCKET_START_PRE:
693 if (success)
acbb0225 694 socket_enter_start_post(s);
034c6ed7
LP
695 else
696 socket_enter_stop_pre(s, false);
697 break;
698
699 case SOCKET_START_POST:
700 if (success)
e9af15c3 701 socket_enter_listening(s);
034c6ed7
LP
702 else
703 socket_enter_stop_pre(s, false);
704 break;
705
706 case SOCKET_STOP_PRE:
707 case SOCKET_STOP_PRE_SIGTERM:
708 case SOCKET_STOP_PRE_SIGKILL:
709 socket_enter_stop_post(s, success);
710 break;
711
712 case SOCKET_STOP_POST:
713 case SOCKET_STOP_POST_SIGTERM:
714 case SOCKET_STOP_POST_SIGKILL:
715 socket_enter_dead(s, success);
716 break;
717
718 default:
719 assert_not_reached("Uh, control process died at wrong time.");
720 }
721 }
722}
5cb5a6ff 723
acbb0225 724static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
87f0e418 725 Socket *s = SOCKET(u);
5cb5a6ff 726
034c6ed7
LP
727 assert(s);
728 assert(elapsed == 1);
729
acbb0225 730 assert(w == &s->timer_watch);
034c6ed7
LP
731
732 switch (s->state) {
733
734 case SOCKET_START_PRE:
735 case SOCKET_START_POST:
87f0e418 736 log_warning("%s operation timed out. Stopping.", unit_id(u));
034c6ed7
LP
737 socket_enter_stop_pre(s, false);
738 break;
739
740 case SOCKET_STOP_PRE:
87f0e418 741 log_warning("%s stopping timed out. Terminating.", unit_id(u));
034c6ed7
LP
742 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false);
743 break;
744
745 case SOCKET_STOP_PRE_SIGTERM:
87f0e418 746 log_warning("%s stopping timed out. Killing.", unit_id(u));
034c6ed7
LP
747 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false);
748 break;
749
750 case SOCKET_STOP_PRE_SIGKILL:
87f0e418 751 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
034c6ed7
LP
752 socket_enter_stop_post(s, false);
753 break;
754
755 case SOCKET_STOP_POST:
87f0e418 756 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
034c6ed7
LP
757 socket_enter_signal(s, SOCKET_STOP_POST_SIGTERM, false);
758 break;
759
760 case SOCKET_STOP_POST_SIGTERM:
87f0e418 761 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
034c6ed7
LP
762 socket_enter_signal(s, SOCKET_STOP_POST_SIGKILL, false);
763 break;
764
765 case SOCKET_STOP_POST_SIGKILL:
87f0e418 766 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
034c6ed7
LP
767 socket_enter_dead(s, false);
768 break;
769
770 default:
771 assert_not_reached("Timeout at wrong time.");
772 }
5cb5a6ff
LP
773}
774
44d8db9e
LP
775int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) {
776 int *rfds;
777 unsigned rn_fds, k;
778 SocketPort *p;
779
780 assert(s);
781 assert(fds);
782 assert(n_fds);
783
784 /* Called from the service code for requesting our fds */
785
786 rn_fds = 0;
787 LIST_FOREACH(port, p, s->ports)
788 if (p->fd >= 0)
789 rn_fds++;
790
791 if (!(rfds = new(int, rn_fds)) < 0)
792 return -ENOMEM;
793
794 k = 0;
795 LIST_FOREACH(port, p, s->ports)
796 if (p->fd >= 0)
797 rfds[k++] = p->fd;
798
799 assert(k == rn_fds);
800
801 *fds = rfds;
802 *n_fds = rn_fds;
803
804 return 0;
805}
806
ceee3d82
LP
807void socket_notify_service_dead(Socket *s) {
808 assert(s);
809
810 /* The service is dead. Dang. */
811
812 if (s->state == SOCKET_RUNNING) {
813 log_debug("%s got notified about service death.", unit_id(UNIT(s)));
814 socket_enter_listening(s);
815 }
816}
817
87f0e418 818const UnitVTable socket_vtable = {
5cb5a6ff
LP
819 .suffix = ".socket",
820
034c6ed7
LP
821 .init = socket_init,
822 .done = socket_done,
823
5cb5a6ff
LP
824 .dump = socket_dump,
825
542563ba
LP
826 .start = socket_start,
827 .stop = socket_stop,
5cb5a6ff
LP
828
829 .active_state = socket_active_state,
830
9152c765 831 .fd_event = socket_fd_event,
034c6ed7
LP
832 .sigchld_event = socket_sigchld_event,
833 .timer_event = socket_timer_event
5cb5a6ff 834};