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