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