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