chiark / gitweb /
6b1da016dd4cdb13695f9a914a38ee8b6c04bcb4
[elogind.git] / socket.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <sys/epoll.h>
28 #include <signal.h>
29
30 #include "unit.h"
31 #include "socket.h"
32 #include "log.h"
33
34 static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
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,
47 };
48
49 static 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
64 static void socket_done(Unit *u) {
65         Socket *s = SOCKET(u);
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) {
84                 unit_unwatch_pid(u, s->control_pid);
85                 s->control_pid = 0;
86         }
87
88         s->service = NULL;
89
90         free(s->bind_to_device);
91
92         unit_unwatch_timer(u, &s->timer_watch);
93 }
94
95 static int socket_init(Unit *u) {
96         Socket *s = SOCKET(u);
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;
104         s->timer_watch.type = WATCH_INVALID;
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
110         if ((r = unit_load_fragment_and_dropin(u)) <= 0) {
111                 if (r == 0)
112                         r = -ENOENT;
113                 goto fail;
114         }
115
116         if (!(t = unit_name_change_suffix(unit_id(u), ".service"))) {
117                 r = -ENOMEM;
118                 goto fail;
119         }
120
121         r = manager_load_unit(u->meta.manager, t, (Unit**) &s->service);
122         free(t);
123
124         if (r < 0)
125                 goto fail;
126
127         if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0)
128                 goto fail;
129
130         return 0;
131
132 fail:
133         socket_done(u);
134         return r;
135 }
136
137 static 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
146         assert_not_reached("Unknown socket type");
147         return NULL;
148 }
149
150 static void socket_dump(Unit *u, FILE *f, const char *prefix) {
151
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;
160         Socket *s = SOCKET(u);
161         SocketPort *p;
162         const char *prefix2;
163         char *p2;
164
165         assert(s);
166         assert(f);
167
168         p2 = strappend(prefix, "\t");
169         prefix2 = p2 ? p2 : prefix;
170
171         fprintf(f,
172                 "%sSocket State: %s\n"
173                 "%sBindIPv6Only: %s\n"
174                 "%sBacklog: %u\n",
175                 prefix, state_string_table[s->state],
176                 prefix, yes_no(s->bind_ipv6_only),
177                 prefix, s->backlog);
178
179         if (s->bind_to_device)
180                 fprintf(f,
181                         "%sBindToDevice: %s\n",
182                         prefix, s->bind_to_device);
183
184         LIST_FOREACH(port, p, s->ports) {
185
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         }
201
202         exec_context_dump(&s->exec_context, f, prefix);
203
204         for (c = 0; c < _SOCKET_EXEC_MAX; c++) {
205                 if (!s->exec_command[c])
206                         continue;
207
208                 fprintf(f, "%sā†’ %s:\n",
209                         prefix, command_table[c]);
210
211                 exec_command_dump_list(s->exec_command[c], f, prefix2);
212         }
213
214         free(p2);
215 }
216
217 static void socket_close_fds(Socket *s) {
218         SocketPort *p;
219
220         assert(s);
221
222         LIST_FOREACH(port, p, s->ports) {
223                 if (p->fd < 0)
224                         continue;
225
226                 unit_unwatch_fd(UNIT(s), &p->fd_watch);
227                 assert_se(close_nointr(p->fd) >= 0);
228
229                 p->fd = -1;
230         }
231 }
232
233 static int socket_open_fds(Socket *s) {
234         SocketPort *p;
235         int r;
236
237         assert(s);
238
239         LIST_FOREACH(port, p, s->ports) {
240
241                 if (p->fd >= 0)
242                         continue;
243
244                 if (p->type == SOCKET_SOCKET) {
245
246                         if ((r = socket_address_listen(&p->address, s->backlog, s->bind_ipv6_only, s->bind_to_device, &p->fd)) < 0)
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                 }
275         }
276
277         return 0;
278
279 rollback:
280         socket_close_fds(s);
281         return r;
282 }
283
284 static void socket_unwatch_fds(Socket *s) {
285         SocketPort *p;
286
287         assert(s);
288
289         LIST_FOREACH(port, p, s->ports) {
290                 if (p->fd < 0)
291                         continue;
292
293                 unit_unwatch_fd(UNIT(s), &p->fd_watch);
294         }
295 }
296
297 static int socket_watch_fds(Socket *s) {
298         SocketPort *p;
299         int r;
300
301         assert(s);
302
303         LIST_FOREACH(port, p, s->ports) {
304                 if (p->fd < 0)
305                         continue;
306
307                 if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0)
308                         goto fail;
309         }
310
311         return 0;
312
313 fail:
314         socket_unwatch_fds(s);
315         return r;
316 }
317
318 static 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)
333                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
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)
343                 if (s->control_pid > 0) {
344                         unit_unwatch_pid(UNIT(s), s->control_pid);
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
365         log_debug("%s changed %s ā†’ %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
366
367         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
368 }
369
370 static 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) {
379                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
380                         goto fail;
381         } else
382                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
383
384         if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
385                 goto fail;
386
387         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
388                 /* FIXME: we need to do something here */
389                 goto fail;
390
391         *_pid = pid;
392
393         return 0;
394
395 fail:
396         if (timeout)
397                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
398
399         return r;
400 }
401
402 static 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
411 static void socket_enter_stop_post(Socket *s, bool success) {
412         int r;
413         assert(s);
414
415         if (!success)
416                 s->failure = true;
417
418         if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]))
419                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
420                         goto fail;
421
422         socket_set_state(s, SOCKET_STOP_POST);
423
424         if (!s->control_command)
425                 socket_enter_dead(s, true);
426
427         return;
428
429 fail:
430         log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r));
431         socket_enter_dead(s, false);
432 }
433
434 static 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                 }
451         }
452
453         socket_set_state(s, state);
454
455         if (s->control_pid <= 0)
456                 socket_enter_dead(s, true);
457
458         return;
459
460 fail:
461         log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
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
469 static void socket_enter_stop_pre(Socket *s, bool success) {
470         int r;
471         assert(s);
472
473         if (!success)
474                 s->failure = true;
475
476         if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]))
477                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
478                         goto fail;
479
480         socket_set_state(s, SOCKET_STOP_PRE);
481
482         if (!s->control_command)
483                 socket_enter_stop_post(s, true);
484
485         return;
486
487 fail:
488         log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
489         socket_enter_stop_post(s, false);
490 }
491
492 static 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
504 fail:
505         socket_enter_stop_pre(s, false);
506 }
507
508 static void socket_enter_start_post(Socket *s) {
509         int r;
510         assert(s);
511
512         if ((r = socket_open_fds(s)) < 0) {
513                 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s)), strerror(-r));
514                 goto fail;
515         }
516
517         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST]))
518                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) {
519                         log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
520                         goto fail;
521                 }
522
523         socket_set_state(s, SOCKET_START_POST);
524
525         if (!s->control_command)
526                 socket_enter_listening(s);
527
528         return;
529
530 fail:
531         socket_enter_stop_pre(s, false);
532 }
533
534 static void socket_enter_start_pre(Socket *s) {
535         int r;
536         assert(s);
537
538         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE]))
539                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
540                         goto fail;
541
542         socket_set_state(s, SOCKET_START_PRE);
543
544         if (!s->control_command)
545                 socket_enter_start_post(s);
546
547         return;
548
549 fail:
550         log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s)), strerror(-r));
551         socket_enter_dead(s, false);
552 }
553
554 static void socket_enter_running(Socket *s) {
555         int r;
556
557         assert(s);
558
559         if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, NULL)) < 0)
560                 goto fail;
561
562         socket_set_state(s, SOCKET_RUNNING);
563         return;
564
565 fail:
566         log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s)), strerror(-r));
567         socket_enter_dead(s, false);
568 }
569
570 static 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
587 fail:
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
596 static int socket_start(Unit *u) {
597         Socket *s = SOCKET(u);
598
599         assert(s);
600
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
611         if (s->state == SOCKET_START_PRE ||
612             s->state == SOCKET_START_POST)
613                 return 0;
614
615         /* Cannot run this without the service being around */
616         if (s->service->meta.load_state != UNIT_LOADED)
617                 return -ENOENT;
618
619         assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTAINANCE);
620
621         s->failure = false;
622         socket_enter_start_pre(s);
623         return 0;
624 }
625
626 static int socket_stop(Unit *u) {
627         Socket *s = SOCKET(u);
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;
636
637         assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
638
639         socket_enter_stop_pre(s, true);
640         return 0;
641 }
642
643 static UnitActiveState socket_active_state(Unit *u) {
644         assert(u);
645
646         return state_translation_table[SOCKET(u)->state];
647 }
648
649 static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
650         Socket *s = SOCKET(u);
651
652         assert(s);
653
654         log_debug("Incoming traffic on %s", unit_id(u));
655
656         if (events != EPOLLIN)
657                 socket_enter_stop_pre(s, false);
658
659         socket_enter_running(s);
660 }
661
662 static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
663         Socket *s = SOCKET(u);
664         bool success;
665
666         assert(s);
667         assert(pid >= 0);
668
669         success = code == CLD_EXITED && status == 0;
670         s->failure = s->failure || !success;
671
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
678         log_debug("%s control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
679
680         if (s->control_command->command_next &&
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]);
683                 socket_run_next(s, success);
684         } else {
685                 /* No further commands for this step, so let's figure
686                  * out what to do next */
687
688                 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
689
690                 switch (s->state) {
691
692                 case SOCKET_START_PRE:
693                         if (success)
694                                 socket_enter_start_post(s);
695                         else
696                                 socket_enter_stop_pre(s, false);
697                         break;
698
699                 case SOCKET_START_POST:
700                         if (success)
701                                 socket_enter_listening(s);
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 }
723
724 static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
725         Socket *s = SOCKET(u);
726
727         assert(s);
728         assert(elapsed == 1);
729
730         assert(w == &s->timer_watch);
731
732         switch (s->state) {
733
734         case SOCKET_START_PRE:
735         case SOCKET_START_POST:
736                 log_warning("%s operation timed out. Stopping.", unit_id(u));
737                 socket_enter_stop_pre(s, false);
738                 break;
739
740         case SOCKET_STOP_PRE:
741                 log_warning("%s stopping timed out. Terminating.", unit_id(u));
742                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false);
743                 break;
744
745         case SOCKET_STOP_PRE_SIGTERM:
746                 log_warning("%s stopping timed out. Killing.", unit_id(u));
747                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false);
748                 break;
749
750         case SOCKET_STOP_PRE_SIGKILL:
751                 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
752                 socket_enter_stop_post(s, false);
753                 break;
754
755         case SOCKET_STOP_POST:
756                 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
757                 socket_enter_signal(s, SOCKET_STOP_POST_SIGTERM, false);
758                 break;
759
760         case SOCKET_STOP_POST_SIGTERM:
761                 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
762                 socket_enter_signal(s, SOCKET_STOP_POST_SIGKILL, false);
763                 break;
764
765         case SOCKET_STOP_POST_SIGKILL:
766                 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
767                 socket_enter_dead(s, false);
768                 break;
769
770         default:
771                 assert_not_reached("Timeout at wrong time.");
772         }
773 }
774
775 int 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
807 void 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
818 const UnitVTable socket_vtable = {
819         .suffix = ".socket",
820
821         .init = socket_init,
822         .done = socket_done,
823
824         .dump = socket_dump,
825
826         .start = socket_start,
827         .stop = socket_stop,
828
829         .active_state = socket_active_state,
830
831         .fd_event = socket_fd_event,
832         .sigchld_event = socket_sigchld_event,
833         .timer_event = socket_timer_event
834 };