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