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