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