chiark / gitweb /
build-sys: add rules for man page aliases
[elogind.git] / src / socket.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #include <arpa/inet.h>
30 #include <mqueue.h>
31
32 #include "unit.h"
33 #include "socket.h"
34 #include "netinet/tcp.h"
35 #include "log.h"
36 #include "load-dropin.h"
37 #include "load-fragment.h"
38 #include "strv.h"
39 #include "unit-name.h"
40 #include "dbus-socket.h"
41 #include "missing.h"
42 #include "special.h"
43 #include "bus-errors.h"
44 #include "label.h"
45 #include "exit-status.h"
46 #include "def.h"
47
48 static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
49         [SOCKET_DEAD] = UNIT_INACTIVE,
50         [SOCKET_START_PRE] = UNIT_ACTIVATING,
51         [SOCKET_START_POST] = UNIT_ACTIVATING,
52         [SOCKET_LISTENING] = UNIT_ACTIVE,
53         [SOCKET_RUNNING] = UNIT_ACTIVE,
54         [SOCKET_STOP_PRE] = UNIT_DEACTIVATING,
55         [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING,
56         [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING,
57         [SOCKET_STOP_POST] = UNIT_DEACTIVATING,
58         [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING,
59         [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING,
60         [SOCKET_FAILED] = UNIT_FAILED
61 };
62
63 static void socket_init(Unit *u) {
64         Socket *s = SOCKET(u);
65
66         assert(u);
67         assert(u->meta.load_state == UNIT_STUB);
68
69         s->backlog = SOMAXCONN;
70         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
71         s->directory_mode = 0755;
72         s->socket_mode = 0666;
73
74         s->max_connections = 64;
75
76         s->priority = -1;
77         s->ip_tos = -1;
78         s->ip_ttl = -1;
79         s->mark = -1;
80
81         exec_context_init(&s->exec_context);
82         s->exec_context.std_output = u->meta.manager->default_std_output;
83         s->exec_context.std_error = u->meta.manager->default_std_error;
84
85         s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
86 }
87
88 static void socket_unwatch_control_pid(Socket *s) {
89         assert(s);
90
91         if (s->control_pid <= 0)
92                 return;
93
94         unit_unwatch_pid(UNIT(s), s->control_pid);
95         s->control_pid = 0;
96 }
97
98 static void socket_done(Unit *u) {
99         Socket *s = SOCKET(u);
100         SocketPort *p;
101         Meta *i;
102
103         assert(s);
104
105         while ((p = s->ports)) {
106                 LIST_REMOVE(SocketPort, port, s->ports, p);
107
108                 if (p->fd >= 0) {
109                         unit_unwatch_fd(UNIT(s), &p->fd_watch);
110                         close_nointr_nofail(p->fd);
111                 }
112
113                 free(p->path);
114                 free(p);
115         }
116
117         exec_context_done(&s->exec_context);
118         exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
119         s->control_command = NULL;
120
121         socket_unwatch_control_pid(s);
122
123         s->service = NULL;
124
125         free(s->tcp_congestion);
126         s->tcp_congestion = NULL;
127
128         free(s->bind_to_device);
129         s->bind_to_device = NULL;
130
131         unit_unwatch_timer(u, &s->timer_watch);
132
133         /* Make sure no service instance refers to us anymore. */
134         LIST_FOREACH(units_by_type, i, u->meta.manager->units_by_type[UNIT_SERVICE]) {
135                 Service *service = (Service *) i;
136
137                 if (service->accept_socket == s)
138                         service->accept_socket = NULL;
139
140                 set_remove(service->configured_sockets, s);
141         }
142 }
143
144 static int socket_instantiate_service(Socket *s) {
145         char *prefix, *name;
146         int r;
147         Unit *u;
148
149         assert(s);
150
151         /* This fills in s->service if it isn't filled in yet. For
152          * Accept=yes sockets we create the next connection service
153          * here. For Accept=no this is mostly a NOP since the service
154          * is figured out at load time anyway. */
155
156         if (s->service)
157                 return 0;
158
159         assert(s->accept);
160
161         if (!(prefix = unit_name_to_prefix(s->meta.id)))
162                 return -ENOMEM;
163
164         r = asprintf(&name, "%s@%u.service", prefix, s->n_accepted);
165         free(prefix);
166
167         if (r < 0)
168                 return -ENOMEM;
169
170         r = manager_load_unit(s->meta.manager, name, NULL, NULL, &u);
171         free(name);
172
173         if (r < 0)
174                 return r;
175
176 #ifdef HAVE_SYSV_COMPAT
177         if (SERVICE(u)->sysv_path) {
178                 log_error("Using SysV services for socket activation is not supported. Refusing.");
179                 return -ENOENT;
180         }
181 #endif
182
183         u->meta.no_gc = true;
184         s->service = SERVICE(u);
185         return 0;
186 }
187
188 static bool have_non_accept_socket(Socket *s) {
189         SocketPort *p;
190
191         assert(s);
192
193         if (!s->accept)
194                 return true;
195
196         LIST_FOREACH(port, p, s->ports) {
197
198                 if (p->type != SOCKET_SOCKET)
199                         return true;
200
201                 if (!socket_address_can_accept(&p->address))
202                         return true;
203         }
204
205         return false;
206 }
207
208 static int socket_verify(Socket *s) {
209         assert(s);
210
211         if (s->meta.load_state != UNIT_LOADED)
212                 return 0;
213
214         if (!s->ports) {
215                 log_error("%s lacks Listen setting. Refusing.", s->meta.id);
216                 return -EINVAL;
217         }
218
219         if (s->accept && have_non_accept_socket(s)) {
220                 log_error("%s configured for accepting sockets, but sockets are non-accepting. Refusing.", s->meta.id);
221                 return -EINVAL;
222         }
223
224         if (s->accept && s->max_connections <= 0) {
225                 log_error("%s's MaxConnection setting too small. Refusing.", s->meta.id);
226                 return -EINVAL;
227         }
228
229         if (s->accept && s->service) {
230                 log_error("Explicit service configuration for accepting sockets not supported on %s. Refusing.", s->meta.id);
231                 return -EINVAL;
232         }
233
234         if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
235                 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
236                 return -EINVAL;
237         }
238
239         return 0;
240 }
241
242 static bool socket_needs_mount(Socket *s, const char *prefix) {
243         SocketPort *p;
244
245         assert(s);
246
247         LIST_FOREACH(port, p, s->ports) {
248
249                 if (p->type == SOCKET_SOCKET) {
250                         if (socket_address_needs_mount(&p->address, prefix))
251                                 return true;
252                 } else if (p->type == SOCKET_FIFO || p->type == SOCKET_SPECIAL) {
253                         if (path_startswith(p->path, prefix))
254                                 return true;
255                 }
256         }
257
258         return false;
259 }
260
261 int socket_add_one_mount_link(Socket *s, Mount *m) {
262         int r;
263
264         assert(s);
265         assert(m);
266
267         if (s->meta.load_state != UNIT_LOADED ||
268             m->meta.load_state != UNIT_LOADED)
269                 return 0;
270
271         if (!socket_needs_mount(s, m->where))
272                 return 0;
273
274         if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
275                 return r;
276
277         return 0;
278 }
279
280 static int socket_add_mount_links(Socket *s) {
281         Meta *other;
282         int r;
283
284         assert(s);
285
286         LIST_FOREACH(units_by_type, other, s->meta.manager->units_by_type[UNIT_MOUNT])
287                 if ((r = socket_add_one_mount_link(s, (Mount*) other)) < 0)
288                         return r;
289
290         return 0;
291 }
292
293 static int socket_add_device_link(Socket *s) {
294         char *t;
295         int r;
296
297         assert(s);
298
299         if (!s->bind_to_device)
300                 return 0;
301
302         if (asprintf(&t, "/sys/subsystem/net/devices/%s", s->bind_to_device) < 0)
303                 return -ENOMEM;
304
305         r = unit_add_node_link(UNIT(s), t, false);
306         free(t);
307
308         return r;
309 }
310
311 static int socket_add_default_dependencies(Socket *s) {
312         int r;
313         assert(s);
314
315         if (s->meta.manager->running_as == MANAGER_SYSTEM) {
316                 if ((r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
317                         return r;
318
319                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
320                         return r;
321         }
322
323         return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
324 }
325
326 static int socket_load(Unit *u) {
327         Socket *s = SOCKET(u);
328         int r;
329
330         assert(u);
331         assert(u->meta.load_state == UNIT_STUB);
332
333         if ((r = unit_load_fragment_and_dropin(u)) < 0)
334                 return r;
335
336         /* This is a new unit? Then let's add in some extras */
337         if (u->meta.load_state == UNIT_LOADED) {
338
339                 if (have_non_accept_socket(s)) {
340
341                         if (!s->service)
342                                 if ((r = unit_load_related_unit(u, ".service", (Unit**) &s->service)) < 0)
343                                         return r;
344
345                         if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service), true)) < 0)
346                                 return r;
347                 }
348
349                 if ((r = socket_add_mount_links(s)) < 0)
350                         return r;
351
352                 if ((r = socket_add_device_link(s)) < 0)
353                         return r;
354
355                 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
356                         return r;
357
358                 if ((r = unit_add_default_cgroups(u)) < 0)
359                         return r;
360
361                 if (s->meta.default_dependencies)
362                         if ((r = socket_add_default_dependencies(s)) < 0)
363                                 return r;
364         }
365
366         return socket_verify(s);
367 }
368
369 static const char* listen_lookup(int family, int type) {
370
371         if (family == AF_NETLINK)
372                 return "ListenNetlink";
373
374         if (type == SOCK_STREAM)
375                 return "ListenStream";
376         else if (type == SOCK_DGRAM)
377                 return "ListenDatagram";
378         else if (type == SOCK_SEQPACKET)
379                 return "ListenSequentialPacket";
380
381         assert_not_reached("Unknown socket type");
382         return NULL;
383 }
384
385 static void socket_dump(Unit *u, FILE *f, const char *prefix) {
386
387         SocketExecCommand c;
388         Socket *s = SOCKET(u);
389         SocketPort *p;
390         const char *prefix2;
391         char *p2;
392
393         assert(s);
394         assert(f);
395
396         p2 = strappend(prefix, "\t");
397         prefix2 = p2 ? p2 : prefix;
398
399         fprintf(f,
400                 "%sSocket State: %s\n"
401                 "%sBindIPv6Only: %s\n"
402                 "%sBacklog: %u\n"
403                 "%sSocketMode: %04o\n"
404                 "%sDirectoryMode: %04o\n"
405                 "%sKeepAlive: %s\n"
406                 "%sFreeBind: %s\n"
407                 "%sTransparent: %s\n"
408                 "%sBroadcast: %s\n"
409                 "%sTCPCongestion: %s\n",
410                 prefix, socket_state_to_string(s->state),
411                 prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only),
412                 prefix, s->backlog,
413                 prefix, s->socket_mode,
414                 prefix, s->directory_mode,
415                 prefix, yes_no(s->keep_alive),
416                 prefix, yes_no(s->free_bind),
417                 prefix, yes_no(s->transparent),
418                 prefix, yes_no(s->broadcast),
419                 prefix, strna(s->tcp_congestion));
420
421         if (s->control_pid > 0)
422                 fprintf(f,
423                         "%sControl PID: %lu\n",
424                         prefix, (unsigned long) s->control_pid);
425
426         if (s->bind_to_device)
427                 fprintf(f,
428                         "%sBindToDevice: %s\n",
429                         prefix, s->bind_to_device);
430
431         if (s->accept)
432                 fprintf(f,
433                         "%sAccepted: %u\n"
434                         "%sNConnections: %u\n"
435                         "%sMaxConnections: %u\n",
436                         prefix, s->n_accepted,
437                         prefix, s->n_connections,
438                         prefix, s->max_connections);
439
440         if (s->priority >= 0)
441                 fprintf(f,
442                         "%sPriority: %i\n",
443                         prefix, s->priority);
444
445         if (s->receive_buffer > 0)
446                 fprintf(f,
447                         "%sReceiveBuffer: %zu\n",
448                         prefix, s->receive_buffer);
449
450         if (s->send_buffer > 0)
451                 fprintf(f,
452                         "%sSendBuffer: %zu\n",
453                         prefix, s->send_buffer);
454
455         if (s->ip_tos >= 0)
456                 fprintf(f,
457                         "%sIPTOS: %i\n",
458                         prefix, s->ip_tos);
459
460         if (s->ip_ttl >= 0)
461                 fprintf(f,
462                         "%sIPTTL: %i\n",
463                         prefix, s->ip_ttl);
464
465         if (s->pipe_size > 0)
466                 fprintf(f,
467                         "%sPipeSize: %zu\n",
468                         prefix, s->pipe_size);
469
470         if (s->mark >= 0)
471                 fprintf(f,
472                         "%sMark: %i\n",
473                         prefix, s->mark);
474
475         if (s->mq_maxmsg > 0)
476                 fprintf(f,
477                         "%sMessageQueueMaxMessages: %li\n",
478                         prefix, s->mq_maxmsg);
479
480         if (s->mq_msgsize > 0)
481                 fprintf(f,
482                         "%sMessageQueueMessageSize: %li\n",
483                         prefix, s->mq_msgsize);
484
485         LIST_FOREACH(port, p, s->ports) {
486
487                 if (p->type == SOCKET_SOCKET) {
488                         const char *t;
489                         int r;
490                         char *k = NULL;
491
492                         if ((r = socket_address_print(&p->address, &k)) < 0)
493                                 t = strerror(-r);
494                         else
495                                 t = k;
496
497                         fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
498                         free(k);
499                 } else if (p->type == SOCKET_SPECIAL)
500                         fprintf(f, "%sListenSpecial: %s\n", prefix, p->path);
501                 else if (p->type == SOCKET_MQUEUE)
502                         fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path);
503                 else
504                         fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
505         }
506
507         exec_context_dump(&s->exec_context, f, prefix);
508
509         for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) {
510                 if (!s->exec_command[c])
511                         continue;
512
513                 fprintf(f, "%s-> %s:\n",
514                         prefix, socket_exec_command_to_string(c));
515
516                 exec_command_dump_list(s->exec_command[c], f, prefix2);
517         }
518
519         free(p2);
520 }
521
522 static int instance_from_socket(int fd, unsigned nr, char **instance) {
523         socklen_t l;
524         char *r;
525         union {
526                 struct sockaddr sa;
527                 struct sockaddr_un un;
528                 struct sockaddr_in in;
529                 struct sockaddr_in6 in6;
530                 struct sockaddr_storage storage;
531         } local, remote;
532
533         assert(fd >= 0);
534         assert(instance);
535
536         l = sizeof(local);
537         if (getsockname(fd, &local.sa, &l) < 0)
538                 return -errno;
539
540         l = sizeof(remote);
541         if (getpeername(fd, &remote.sa, &l) < 0)
542                 return -errno;
543
544         switch (local.sa.sa_family) {
545
546         case AF_INET: {
547                 uint32_t
548                         a = ntohl(local.in.sin_addr.s_addr),
549                         b = ntohl(remote.in.sin_addr.s_addr);
550
551                 if (asprintf(&r,
552                              "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
553                              a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
554                              ntohs(local.in.sin_port),
555                              b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
556                              ntohs(remote.in.sin_port)) < 0)
557                         return -ENOMEM;
558
559                 break;
560         }
561
562         case AF_INET6: {
563                 static const char ipv4_prefix[] = {
564                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
565                 };
566
567                 if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 &&
568                     memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
569                         const uint8_t
570                                 *a = local.in6.sin6_addr.s6_addr+12,
571                                 *b = remote.in6.sin6_addr.s6_addr+12;
572
573                         if (asprintf(&r,
574                                      "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
575                                      a[0], a[1], a[2], a[3],
576                                      ntohs(local.in6.sin6_port),
577                                      b[0], b[1], b[2], b[3],
578                                      ntohs(remote.in6.sin6_port)) < 0)
579                                 return -ENOMEM;
580                 } else {
581                         char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN];
582
583                         if (asprintf(&r,
584                                      "%s:%u-%s:%u",
585                                      inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)),
586                                      ntohs(local.in6.sin6_port),
587                                      inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)),
588                                      ntohs(remote.in6.sin6_port)) < 0)
589                                 return -ENOMEM;
590                 }
591
592                 break;
593         }
594
595         case AF_UNIX: {
596                 struct ucred ucred;
597
598                 l = sizeof(ucred);
599                 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
600                         return -errno;
601
602                 if (asprintf(&r,
603                              "%u-%lu-%lu",
604                              nr,
605                              (unsigned long) ucred.pid,
606                              (unsigned long) ucred.uid) < 0)
607                         return -ENOMEM;
608
609                 break;
610         }
611
612         default:
613                 assert_not_reached("Unhandled socket type.");
614         }
615
616         *instance = r;
617         return 0;
618 }
619
620 static void socket_close_fds(Socket *s) {
621         SocketPort *p;
622
623         assert(s);
624
625         LIST_FOREACH(port, p, s->ports) {
626                 if (p->fd < 0)
627                         continue;
628
629                 unit_unwatch_fd(UNIT(s), &p->fd_watch);
630                 close_nointr_nofail(p->fd);
631
632                 /* One little note: we should never delete any sockets
633                  * in the file system here! After all some other
634                  * process we spawned might still have a reference of
635                  * this fd and wants to continue to use it. Therefore
636                  * we delete sockets in the file system before we
637                  * create a new one, not after we stopped using
638                  * one! */
639
640                 p->fd = -1;
641         }
642 }
643
644 static void socket_apply_socket_options(Socket *s, int fd) {
645         assert(s);
646         assert(fd >= 0);
647
648         if (s->keep_alive) {
649                 int b = s->keep_alive;
650                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) < 0)
651                         log_warning("SO_KEEPALIVE failed: %m");
652         }
653
654         if (s->broadcast) {
655                 int one = 1;
656                 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0)
657                         log_warning("SO_BROADCAST failed: %m");
658         }
659
660         if (s->priority >= 0)
661                 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0)
662                         log_warning("SO_PRIORITY failed: %m");
663
664         if (s->receive_buffer > 0) {
665                 int value = (int) s->receive_buffer;
666                 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0)
667                         log_warning("SO_RCVBUFFORCE failed: %m");
668         }
669
670         if (s->send_buffer > 0) {
671                 int value = (int) s->send_buffer;
672                 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0)
673                         log_warning("SO_SNDBUFFORCE failed: %m");
674         }
675
676         if (s->mark >= 0)
677                 if (setsockopt(fd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)) < 0)
678                         log_warning("SO_MARK failed: %m");
679
680         if (s->ip_tos >= 0)
681                 if (setsockopt(fd, IPPROTO_IP, IP_TOS, &s->ip_tos, sizeof(s->ip_tos)) < 0)
682                         log_warning("IP_TOS failed: %m");
683
684         if (s->ip_ttl >= 0) {
685                 int r, x;
686
687                 r = setsockopt(fd, IPPROTO_IP, IP_TTL, &s->ip_ttl, sizeof(s->ip_ttl));
688
689                 if (socket_ipv6_is_supported())
690                         x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl));
691                 else {
692                         x = -1;
693                         errno = EAFNOSUPPORT;
694                 }
695
696                 if (r < 0 && x < 0)
697                         log_warning("IP_TTL/IPV6_UNICAST_HOPS failed: %m");
698         }
699
700         if (s->tcp_congestion)
701                 if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0)
702                         log_warning("TCP_CONGESTION failed: %m");
703 }
704
705 static void socket_apply_fifo_options(Socket *s, int fd) {
706         assert(s);
707         assert(fd >= 0);
708
709         if (s->pipe_size > 0)
710                 if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0)
711                         log_warning("F_SETPIPE_SZ: %m");
712 }
713
714 static int fifo_address_create(
715                 const char *path,
716                 mode_t directory_mode,
717                 mode_t socket_mode,
718                 int *_fd) {
719
720         int fd = -1, r = 0;
721         struct stat st;
722         mode_t old_mask;
723
724         assert(path);
725         assert(_fd);
726
727         mkdir_parents(path, directory_mode);
728
729         if ((r = label_fifofile_set(path)) < 0)
730                 goto fail;
731
732         /* Enforce the right access mode for the fifo */
733         old_mask = umask(~ socket_mode);
734
735         /* Include the original umask in our mask */
736         umask(~socket_mode | old_mask);
737
738         r = mkfifo(path, socket_mode);
739         umask(old_mask);
740
741         if (r < 0 && errno != EEXIST) {
742                 r = -errno;
743                 goto fail;
744         }
745
746         if ((fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) {
747                 r = -errno;
748                 goto fail;
749         }
750
751         label_file_clear();
752
753         if (fstat(fd, &st) < 0) {
754                 r = -errno;
755                 goto fail;
756         }
757
758         if (!S_ISFIFO(st.st_mode) ||
759             (st.st_mode & 0777) != (socket_mode & ~old_mask) ||
760             st.st_uid != getuid() ||
761             st.st_gid != getgid()) {
762
763                 r = -EEXIST;
764                 goto fail;
765         }
766
767         *_fd = fd;
768         return 0;
769
770 fail:
771         label_file_clear();
772
773         if (fd >= 0)
774                 close_nointr_nofail(fd);
775
776         return r;
777 }
778
779 static int special_address_create(
780                 const char *path,
781                 int *_fd) {
782
783         int fd = -1, r = 0;
784         struct stat st;
785
786         assert(path);
787         assert(_fd);
788
789         if ((fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) {
790                 r = -errno;
791                 goto fail;
792         }
793
794         if (fstat(fd, &st) < 0) {
795                 r = -errno;
796                 goto fail;
797         }
798
799         /* Check whether this is a /proc, /sys or /dev file or char device */
800         if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
801                 r = -EEXIST;
802                 goto fail;
803         }
804
805         *_fd = fd;
806         return 0;
807
808 fail:
809         if (fd >= 0)
810                 close_nointr_nofail(fd);
811
812         return r;
813 }
814
815 static int mq_address_create(
816                 const char *path,
817                 mode_t mq_mode,
818                 long maxmsg,
819                 long msgsize,
820                 int *_fd) {
821
822         int fd = -1, r = 0;
823         struct stat st;
824         mode_t old_mask;
825         struct mq_attr _attr, *attr = NULL;
826
827         assert(path);
828         assert(_fd);
829
830         if (maxmsg > 0 && msgsize > 0) {
831                 zero(_attr);
832                 _attr.mq_flags = O_NONBLOCK;
833                 _attr.mq_maxmsg = maxmsg;
834                 _attr.mq_msgsize = msgsize;
835                 attr = &_attr;
836         }
837
838         /* Enforce the right access mode for the mq */
839         old_mask = umask(~ mq_mode);
840
841         /* Include the original umask in our mask */
842         umask(~mq_mode | old_mask);
843
844         fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr);
845         umask(old_mask);
846
847         if (fd < 0) {
848                 r = -errno;
849                 goto fail;
850         }
851
852         if (fstat(fd, &st) < 0) {
853                 r = -errno;
854                 goto fail;
855         }
856
857         if ((st.st_mode & 0777) != (mq_mode & ~old_mask) ||
858             st.st_uid != getuid() ||
859             st.st_gid != getgid()) {
860
861                 r = -EEXIST;
862                 goto fail;
863         }
864
865         *_fd = fd;
866         return 0;
867
868 fail:
869         if (fd >= 0)
870                 close_nointr_nofail(fd);
871
872         return r;
873 }
874
875 static int socket_open_fds(Socket *s) {
876         SocketPort *p;
877         int r;
878         char *label = NULL;
879         bool know_label = false;
880
881         assert(s);
882
883         LIST_FOREACH(port, p, s->ports) {
884
885                 if (p->fd >= 0)
886                         continue;
887
888                 if (p->type == SOCKET_SOCKET) {
889
890                         if (!know_label) {
891
892                                 if ((r = socket_instantiate_service(s)) < 0)
893                                         return r;
894
895                                 if (s->service && s->service->exec_command[SERVICE_EXEC_START]) {
896                                         r = label_get_create_label_from_exe(s->service->exec_command[SERVICE_EXEC_START]->path, &label);
897
898                                         if (r < 0) {
899                                                 if (r != -EPERM)
900                                                         return r;
901                                         }
902                                 }
903
904                                 know_label = true;
905                         }
906
907                         if ((r = socket_address_listen(
908                                              &p->address,
909                                              s->backlog,
910                                              s->bind_ipv6_only,
911                                              s->bind_to_device,
912                                              s->free_bind,
913                                              s->transparent,
914                                              s->directory_mode,
915                                              s->socket_mode,
916                                              label,
917                                              &p->fd)) < 0)
918                                 goto rollback;
919
920                         socket_apply_socket_options(s, p->fd);
921
922                 } else  if (p->type == SOCKET_SPECIAL) {
923
924                         if ((r = special_address_create(
925                                              p->path,
926                                              &p->fd)) < 0)
927                                 goto rollback;
928
929                 } else  if (p->type == SOCKET_FIFO) {
930
931                         if ((r = fifo_address_create(
932                                              p->path,
933                                              s->directory_mode,
934                                              s->socket_mode,
935                                              &p->fd)) < 0)
936                                 goto rollback;
937
938                         socket_apply_fifo_options(s, p->fd);
939                 } else if (p->type == SOCKET_MQUEUE) {
940
941                         if ((r = mq_address_create(
942                                              p->path,
943                                              s->socket_mode,
944                                              s->mq_maxmsg,
945                                              s->mq_msgsize,
946                                              &p->fd)) < 0)
947                                 goto rollback;
948                 } else
949                         assert_not_reached("Unknown port type");
950         }
951
952         label_free(label);
953         return 0;
954
955 rollback:
956         socket_close_fds(s);
957         label_free(label);
958         return r;
959 }
960
961 static void socket_unwatch_fds(Socket *s) {
962         SocketPort *p;
963
964         assert(s);
965
966         LIST_FOREACH(port, p, s->ports) {
967                 if (p->fd < 0)
968                         continue;
969
970                 unit_unwatch_fd(UNIT(s), &p->fd_watch);
971         }
972 }
973
974 static int socket_watch_fds(Socket *s) {
975         SocketPort *p;
976         int r;
977
978         assert(s);
979
980         LIST_FOREACH(port, p, s->ports) {
981                 if (p->fd < 0)
982                         continue;
983
984                 p->fd_watch.socket_accept =
985                         s->accept &&
986                         p->type == SOCKET_SOCKET &&
987                         socket_address_can_accept(&p->address);
988
989                 if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0)
990                         goto fail;
991         }
992
993         return 0;
994
995 fail:
996         socket_unwatch_fds(s);
997         return r;
998 }
999
1000 static void socket_set_state(Socket *s, SocketState state) {
1001         SocketState old_state;
1002         assert(s);
1003
1004         old_state = s->state;
1005         s->state = state;
1006
1007         if (state != SOCKET_START_PRE &&
1008             state != SOCKET_START_POST &&
1009             state != SOCKET_STOP_PRE &&
1010             state != SOCKET_STOP_PRE_SIGTERM &&
1011             state != SOCKET_STOP_PRE_SIGKILL &&
1012             state != SOCKET_STOP_POST &&
1013             state != SOCKET_FINAL_SIGTERM &&
1014             state != SOCKET_FINAL_SIGKILL) {
1015                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1016                 socket_unwatch_control_pid(s);
1017                 s->control_command = NULL;
1018                 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
1019         }
1020
1021         if (state != SOCKET_LISTENING)
1022                 socket_unwatch_fds(s);
1023
1024         if (state != SOCKET_START_POST &&
1025             state != SOCKET_LISTENING &&
1026             state != SOCKET_RUNNING &&
1027             state != SOCKET_STOP_PRE &&
1028             state != SOCKET_STOP_PRE_SIGTERM &&
1029             state != SOCKET_STOP_PRE_SIGKILL)
1030                 socket_close_fds(s);
1031
1032         if (state != old_state)
1033                 log_debug("%s changed %s -> %s",
1034                           s->meta.id,
1035                           socket_state_to_string(old_state),
1036                           socket_state_to_string(state));
1037
1038         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
1039 }
1040
1041 static int socket_coldplug(Unit *u) {
1042         Socket *s = SOCKET(u);
1043         int r;
1044
1045         assert(s);
1046         assert(s->state == SOCKET_DEAD);
1047
1048         if (s->deserialized_state != s->state) {
1049
1050                 if (s->deserialized_state == SOCKET_START_PRE ||
1051                     s->deserialized_state == SOCKET_START_POST ||
1052                     s->deserialized_state == SOCKET_STOP_PRE ||
1053                     s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
1054                     s->deserialized_state == SOCKET_STOP_PRE_SIGKILL ||
1055                     s->deserialized_state == SOCKET_STOP_POST ||
1056                     s->deserialized_state == SOCKET_FINAL_SIGTERM ||
1057                     s->deserialized_state == SOCKET_FINAL_SIGKILL) {
1058
1059                         if (s->control_pid <= 0)
1060                                 return -EBADMSG;
1061
1062                         if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1063                                 return r;
1064
1065                         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1066                                 return r;
1067                 }
1068
1069                 if (s->deserialized_state == SOCKET_START_POST ||
1070                     s->deserialized_state == SOCKET_LISTENING ||
1071                     s->deserialized_state == SOCKET_RUNNING ||
1072                     s->deserialized_state == SOCKET_STOP_PRE ||
1073                     s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
1074                     s->deserialized_state == SOCKET_STOP_PRE_SIGKILL)
1075                         if ((r = socket_open_fds(s)) < 0)
1076                                 return r;
1077
1078                 if (s->deserialized_state == SOCKET_LISTENING)
1079                         if ((r = socket_watch_fds(s)) < 0)
1080                                 return r;
1081
1082                 socket_set_state(s, s->deserialized_state);
1083         }
1084
1085         return 0;
1086 }
1087
1088 static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
1089         pid_t pid;
1090         int r;
1091         char **argv;
1092
1093         assert(s);
1094         assert(c);
1095         assert(_pid);
1096
1097         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1098                 goto fail;
1099
1100         if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1101                 r = -ENOMEM;
1102                 goto fail;
1103         }
1104
1105         r = exec_spawn(c,
1106                        argv,
1107                        &s->exec_context,
1108                        NULL, 0,
1109                        s->meta.manager->environment,
1110                        true,
1111                        true,
1112                        true,
1113                        s->meta.manager->confirm_spawn,
1114                        s->meta.cgroup_bondings,
1115                        s->meta.cgroup_attributes,
1116                        &pid);
1117
1118         strv_free(argv);
1119         if (r < 0)
1120                 goto fail;
1121
1122         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1123                 /* FIXME: we need to do something here */
1124                 goto fail;
1125
1126         *_pid = pid;
1127
1128         return 0;
1129
1130 fail:
1131         unit_unwatch_timer(UNIT(s), &s->timer_watch);
1132
1133         return r;
1134 }
1135
1136 static void socket_enter_dead(Socket *s, bool success) {
1137         assert(s);
1138
1139         if (!success)
1140                 s->failure = true;
1141
1142         socket_set_state(s, s->failure ? SOCKET_FAILED : SOCKET_DEAD);
1143 }
1144
1145 static void socket_enter_signal(Socket *s, SocketState state, bool success);
1146
1147 static void socket_enter_stop_post(Socket *s, bool success) {
1148         int r;
1149         assert(s);
1150
1151         if (!success)
1152                 s->failure = true;
1153
1154         socket_unwatch_control_pid(s);
1155
1156         s->control_command_id = SOCKET_EXEC_STOP_POST;
1157
1158         if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) {
1159                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1160                         goto fail;
1161
1162                 socket_set_state(s, SOCKET_STOP_POST);
1163         } else
1164                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, true);
1165
1166         return;
1167
1168 fail:
1169         log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1170         socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1171 }
1172
1173 static void socket_enter_signal(Socket *s, SocketState state, bool success) {
1174         int r;
1175         Set *pid_set = NULL;
1176         bool wait_for_exit = false;
1177
1178         assert(s);
1179
1180         if (!success)
1181                 s->failure = true;
1182
1183         if (s->exec_context.kill_mode != KILL_NONE) {
1184                 int sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1185
1186                 if (s->control_pid > 0) {
1187                         if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1188
1189                                 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1190                         else
1191                                 wait_for_exit = true;
1192                 }
1193
1194                 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1195
1196                         if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1197                                 r = -ENOMEM;
1198                                 goto fail;
1199                         }
1200
1201                         /* Exclude the control pid from being killed via the cgroup */
1202                         if (s->control_pid > 0)
1203                                 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1204                                         goto fail;
1205
1206                         if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
1207                                 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1208                                         log_warning("Failed to kill control group: %s", strerror(-r));
1209                         } else if (r > 0)
1210                                 wait_for_exit = true;
1211
1212                         set_free(pid_set);
1213                         pid_set = NULL;
1214                 }
1215         }
1216
1217         if (wait_for_exit) {
1218                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1219                         goto fail;
1220
1221                 socket_set_state(s, state);
1222         } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
1223                 socket_enter_stop_post(s, true);
1224         else
1225                 socket_enter_dead(s, true);
1226
1227         return;
1228
1229 fail:
1230         log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1231
1232         if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
1233                 socket_enter_stop_post(s, false);
1234         else
1235                 socket_enter_dead(s, false);
1236
1237         if (pid_set)
1238                 set_free(pid_set);
1239 }
1240
1241 static void socket_enter_stop_pre(Socket *s, bool success) {
1242         int r;
1243         assert(s);
1244
1245         if (!success)
1246                 s->failure = true;
1247
1248         socket_unwatch_control_pid(s);
1249
1250         s->control_command_id = SOCKET_EXEC_STOP_PRE;
1251
1252         if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) {
1253                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1254                         goto fail;
1255
1256                 socket_set_state(s, SOCKET_STOP_PRE);
1257         } else
1258                 socket_enter_stop_post(s, true);
1259
1260         return;
1261
1262 fail:
1263         log_warning("%s failed to run 'stop-pre' task: %s", s->meta.id, strerror(-r));
1264         socket_enter_stop_post(s, false);
1265 }
1266
1267 static void socket_enter_listening(Socket *s) {
1268         int r;
1269         assert(s);
1270
1271         if ((r = socket_watch_fds(s)) < 0) {
1272                 log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r));
1273                 goto fail;
1274         }
1275
1276         socket_set_state(s, SOCKET_LISTENING);
1277         return;
1278
1279 fail:
1280         socket_enter_stop_pre(s, false);
1281 }
1282
1283 static void socket_enter_start_post(Socket *s) {
1284         int r;
1285         assert(s);
1286
1287         if ((r = socket_open_fds(s)) < 0) {
1288                 log_warning("%s failed to listen on sockets: %s", s->meta.id, strerror(-r));
1289                 goto fail;
1290         }
1291
1292         socket_unwatch_control_pid(s);
1293
1294         s->control_command_id = SOCKET_EXEC_START_POST;
1295
1296         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
1297                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) {
1298                         log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1299                         goto fail;
1300                 }
1301
1302                 socket_set_state(s, SOCKET_START_POST);
1303         } else
1304                 socket_enter_listening(s);
1305
1306         return;
1307
1308 fail:
1309         socket_enter_stop_pre(s, false);
1310 }
1311
1312 static void socket_enter_start_pre(Socket *s) {
1313         int r;
1314         assert(s);
1315
1316         socket_unwatch_control_pid(s);
1317
1318         s->control_command_id = SOCKET_EXEC_START_PRE;
1319
1320         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
1321                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1322                         goto fail;
1323
1324                 socket_set_state(s, SOCKET_START_PRE);
1325         } else
1326                 socket_enter_start_post(s);
1327
1328         return;
1329
1330 fail:
1331         log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1332         socket_enter_dead(s, false);
1333 }
1334
1335 static void socket_enter_running(Socket *s, int cfd) {
1336         int r;
1337         DBusError error;
1338
1339         assert(s);
1340         dbus_error_init(&error);
1341
1342         /* We don't take connections anymore if we are supposed to
1343          * shut down anyway */
1344         if (unit_pending_inactive(UNIT(s))) {
1345                 log_debug("Suppressing connection request on %s since unit stop is scheduled.", s->meta.id);
1346
1347                 if (cfd >= 0)
1348                         close_nointr_nofail(cfd);
1349                 else  {
1350                         /* Flush all sockets by closing and reopening them */
1351                         socket_close_fds(s);
1352
1353                         if ((r = socket_watch_fds(s)) < 0) {
1354                                 log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r));
1355                                 socket_enter_stop_pre(s, false);
1356                         }
1357                 }
1358
1359                 return;
1360         }
1361
1362         if (cfd < 0) {
1363                 bool pending = false;
1364                 Meta *i;
1365
1366                 /* If there's already a start pending don't bother to
1367                  * do anything */
1368                 LIST_FOREACH(units_by_type, i, s->meta.manager->units_by_type[UNIT_SERVICE]) {
1369                         Service *service = (Service *) i;
1370
1371                         if (!set_get(service->configured_sockets, s))
1372                                 continue;
1373
1374                         if (!unit_pending_active(UNIT(service)))
1375                                 continue;
1376
1377                         pending = true;
1378                         break;
1379                 }
1380
1381                 if (!pending)
1382                         if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, &error, NULL)) < 0)
1383                                 goto fail;
1384
1385                 socket_set_state(s, SOCKET_RUNNING);
1386         } else {
1387                 char *prefix, *instance = NULL, *name;
1388                 Service *service;
1389
1390                 if (s->n_connections >= s->max_connections) {
1391                         log_warning("Too many incoming connections (%u)", s->n_connections);
1392                         close_nointr_nofail(cfd);
1393                         return;
1394                 }
1395
1396                 if ((r = socket_instantiate_service(s)) < 0)
1397                         goto fail;
1398
1399                 if ((r = instance_from_socket(cfd, s->n_accepted, &instance)) < 0)
1400                         goto fail;
1401
1402                 if (!(prefix = unit_name_to_prefix(s->meta.id))) {
1403                         free(instance);
1404                         r = -ENOMEM;
1405                         goto fail;
1406                 }
1407
1408                 name = unit_name_build(prefix, instance, ".service");
1409                 free(prefix);
1410                 free(instance);
1411
1412                 if (!name) {
1413                         r = -ENOMEM;
1414                         goto fail;
1415                 }
1416
1417                 if ((r = unit_add_name(UNIT(s->service), name)) < 0) {
1418                         free(name);
1419                         goto fail;
1420                 }
1421
1422                 service = s->service;
1423                 s->service = NULL;
1424                 s->n_accepted ++;
1425
1426                 service->meta.no_gc = false;
1427
1428                 unit_choose_id(UNIT(service), name);
1429                 free(name);
1430
1431                 if ((r = service_set_socket_fd(service, cfd, s)) < 0)
1432                         goto fail;
1433
1434                 cfd = -1;
1435                 s->n_connections ++;
1436
1437                 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL)) < 0)
1438                         goto fail;
1439
1440                 /* Notify clients about changed counters */
1441                 unit_add_to_dbus_queue(UNIT(s));
1442         }
1443
1444         return;
1445
1446 fail:
1447         log_warning("%s failed to queue socket startup job: %s", s->meta.id, bus_error(&error, r));
1448         socket_enter_stop_pre(s, false);
1449
1450         if (cfd >= 0)
1451                 close_nointr_nofail(cfd);
1452
1453         dbus_error_free(&error);
1454 }
1455
1456 static void socket_run_next(Socket *s, bool success) {
1457         int r;
1458
1459         assert(s);
1460         assert(s->control_command);
1461         assert(s->control_command->command_next);
1462
1463         if (!success)
1464                 s->failure = true;
1465
1466         socket_unwatch_control_pid(s);
1467
1468         s->control_command = s->control_command->command_next;
1469
1470         if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1471                 goto fail;
1472
1473         return;
1474
1475 fail:
1476         log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r));
1477
1478         if (s->state == SOCKET_START_POST)
1479                 socket_enter_stop_pre(s, false);
1480         else if (s->state == SOCKET_STOP_POST)
1481                 socket_enter_dead(s, false);
1482         else
1483                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1484 }
1485
1486 static int socket_start(Unit *u) {
1487         Socket *s = SOCKET(u);
1488
1489         assert(s);
1490
1491         /* We cannot fulfill this request right now, try again later
1492          * please! */
1493         if (s->state == SOCKET_STOP_PRE ||
1494             s->state == SOCKET_STOP_PRE_SIGKILL ||
1495             s->state == SOCKET_STOP_PRE_SIGTERM ||
1496             s->state == SOCKET_STOP_POST ||
1497             s->state == SOCKET_FINAL_SIGTERM ||
1498             s->state == SOCKET_FINAL_SIGKILL)
1499                 return -EAGAIN;
1500
1501         if (s->state == SOCKET_START_PRE ||
1502             s->state == SOCKET_START_POST)
1503                 return 0;
1504
1505         /* Cannot run this without the service being around */
1506         if (s->service) {
1507                 if (s->service->meta.load_state != UNIT_LOADED) {
1508                         log_error("Socket service %s not loaded, refusing.", s->service->meta.id);
1509                         return -ENOENT;
1510                 }
1511
1512                 /* If the service is already active we cannot start the
1513                  * socket */
1514                 if (s->service->state != SERVICE_DEAD &&
1515                     s->service->state != SERVICE_FAILED &&
1516                     s->service->state != SERVICE_AUTO_RESTART) {
1517                         log_error("Socket service %s already active, refusing.", s->service->meta.id);
1518                         return -EBUSY;
1519                 }
1520
1521 #ifdef HAVE_SYSV_COMPAT
1522                 if (s->service->sysv_path) {
1523                         log_error("Using SysV services for socket activation is not supported. Refusing.");
1524                         return -ENOENT;
1525                 }
1526 #endif
1527         }
1528
1529         assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED);
1530
1531         s->failure = false;
1532         socket_enter_start_pre(s);
1533         return 0;
1534 }
1535
1536 static int socket_stop(Unit *u) {
1537         Socket *s = SOCKET(u);
1538
1539         assert(s);
1540
1541         /* Already on it */
1542         if (s->state == SOCKET_STOP_PRE ||
1543             s->state == SOCKET_STOP_PRE_SIGTERM ||
1544             s->state == SOCKET_STOP_PRE_SIGKILL ||
1545             s->state == SOCKET_STOP_POST ||
1546             s->state == SOCKET_FINAL_SIGTERM ||
1547             s->state == SOCKET_FINAL_SIGKILL)
1548                 return 0;
1549
1550         /* If there's already something running we go directly into
1551          * kill mode. */
1552         if (s->state == SOCKET_START_PRE ||
1553             s->state == SOCKET_START_POST) {
1554                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, true);
1555                 return -EAGAIN;
1556         }
1557
1558         assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
1559
1560         socket_enter_stop_pre(s, true);
1561         return 0;
1562 }
1563
1564 static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
1565         Socket *s = SOCKET(u);
1566         SocketPort *p;
1567         int r;
1568
1569         assert(u);
1570         assert(f);
1571         assert(fds);
1572
1573         unit_serialize_item(u, f, "state", socket_state_to_string(s->state));
1574         unit_serialize_item(u, f, "failure", yes_no(s->failure));
1575         unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted);
1576
1577         if (s->control_pid > 0)
1578                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1579
1580         if (s->control_command_id >= 0)
1581                 unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id));
1582
1583         LIST_FOREACH(port, p, s->ports) {
1584                 int copy;
1585
1586                 if (p->fd < 0)
1587                         continue;
1588
1589                 if ((copy = fdset_put_dup(fds, p->fd)) < 0)
1590                         return copy;
1591
1592                 if (p->type == SOCKET_SOCKET) {
1593                         char *t;
1594
1595                         if ((r = socket_address_print(&p->address, &t)) < 0)
1596                                 return r;
1597
1598                         if (socket_address_family(&p->address) == AF_NETLINK)
1599                                 unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t);
1600                         else
1601                                 unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
1602                         free(t);
1603                 } else if (p->type == SOCKET_SPECIAL)
1604                         unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path);
1605                 else {
1606                         assert(p->type == SOCKET_FIFO);
1607                         unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path);
1608                 }
1609         }
1610
1611         return 0;
1612 }
1613
1614 static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1615         Socket *s = SOCKET(u);
1616
1617         assert(u);
1618         assert(key);
1619         assert(value);
1620         assert(fds);
1621
1622         if (streq(key, "state")) {
1623                 SocketState state;
1624
1625                 if ((state = socket_state_from_string(value)) < 0)
1626                         log_debug("Failed to parse state value %s", value);
1627                 else
1628                         s->deserialized_state = state;
1629         } else if (streq(key, "failure")) {
1630                 int b;
1631
1632                 if ((b = parse_boolean(value)) < 0)
1633                         log_debug("Failed to parse failure value %s", value);
1634                 else
1635                         s->failure = b || s->failure;
1636
1637         } else if (streq(key, "n-accepted")) {
1638                 unsigned k;
1639
1640                 if (safe_atou(value, &k) < 0)
1641                         log_debug("Failed to parse n-accepted value %s", value);
1642                 else
1643                         s->n_accepted += k;
1644         } else if (streq(key, "control-pid")) {
1645                 pid_t pid;
1646
1647                 if (parse_pid(value, &pid) < 0)
1648                         log_debug("Failed to parse control-pid value %s", value);
1649                 else
1650                         s->control_pid = pid;
1651         } else if (streq(key, "control-command")) {
1652                 SocketExecCommand id;
1653
1654                 if ((id = socket_exec_command_from_string(value)) < 0)
1655                         log_debug("Failed to parse exec-command value %s", value);
1656                 else {
1657                         s->control_command_id = id;
1658                         s->control_command = s->exec_command[id];
1659                 }
1660         } else if (streq(key, "fifo")) {
1661                 int fd, skip = 0;
1662                 SocketPort *p;
1663
1664                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
1665                         log_debug("Failed to parse fifo value %s", value);
1666                 else {
1667
1668                         LIST_FOREACH(port, p, s->ports)
1669                                 if (p->type == SOCKET_FIFO &&
1670                                     streq_ptr(p->path, value+skip))
1671                                         break;
1672
1673                         if (p) {
1674                                 if (p->fd >= 0)
1675                                         close_nointr_nofail(p->fd);
1676                                 p->fd = fdset_remove(fds, fd);
1677                         }
1678                 }
1679
1680         } else if (streq(key, "special")) {
1681                 int fd, skip = 0;
1682                 SocketPort *p;
1683
1684                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
1685                         log_debug("Failed to parse special value %s", value);
1686                 else {
1687
1688                         LIST_FOREACH(port, p, s->ports)
1689                                 if (p->type == SOCKET_SPECIAL &&
1690                                     streq_ptr(p->path, value+skip))
1691                                         break;
1692
1693                         if (p) {
1694                                 if (p->fd >= 0)
1695                                         close_nointr_nofail(p->fd);
1696                                 p->fd = fdset_remove(fds, fd);
1697                         }
1698                 }
1699
1700         } else if (streq(key, "socket")) {
1701                 int fd, type, skip = 0;
1702                 SocketPort *p;
1703
1704                 if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
1705                         log_debug("Failed to parse socket value %s", value);
1706                 else {
1707
1708                         LIST_FOREACH(port, p, s->ports)
1709                                 if (socket_address_is(&p->address, value+skip, type))
1710                                         break;
1711
1712                         if (p) {
1713                                 if (p->fd >= 0)
1714                                         close_nointr_nofail(p->fd);
1715                                 p->fd = fdset_remove(fds, fd);
1716                         }
1717                 }
1718
1719         } else if (streq(key, "netlink")) {
1720                 int fd, skip = 0;
1721                 SocketPort *p;
1722
1723                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
1724                         log_debug("Failed to parse socket value %s", value);
1725                 else {
1726
1727                         LIST_FOREACH(port, p, s->ports)
1728                                 if (socket_address_is_netlink(&p->address, value+skip))
1729                                         break;
1730
1731                         if (p) {
1732                                 if (p->fd >= 0)
1733                                         close_nointr_nofail(p->fd);
1734                                 p->fd = fdset_remove(fds, fd);
1735                         }
1736                 }
1737
1738         } else
1739                 log_debug("Unknown serialization key '%s'", key);
1740
1741         return 0;
1742 }
1743
1744 static UnitActiveState socket_active_state(Unit *u) {
1745         assert(u);
1746
1747         return state_translation_table[SOCKET(u)->state];
1748 }
1749
1750 static const char *socket_sub_state_to_string(Unit *u) {
1751         assert(u);
1752
1753         return socket_state_to_string(SOCKET(u)->state);
1754 }
1755
1756 static bool socket_check_gc(Unit *u) {
1757         Socket *s = SOCKET(u);
1758
1759         assert(u);
1760
1761         return s->n_connections > 0;
1762 }
1763
1764 static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
1765         Socket *s = SOCKET(u);
1766         int cfd = -1;
1767
1768         assert(s);
1769         assert(fd >= 0);
1770
1771         if (s->state != SOCKET_LISTENING)
1772                 return;
1773
1774         log_debug("Incoming traffic on %s", u->meta.id);
1775
1776         if (events != EPOLLIN) {
1777
1778                 if (events & EPOLLHUP)
1779                         log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u->meta.id);
1780                 else
1781                         log_error("%s: Got unexpected poll event (0x%x) on socket.", u->meta.id, events);
1782
1783                 goto fail;
1784         }
1785
1786         if (w->socket_accept) {
1787                 for (;;) {
1788
1789                         if ((cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK)) < 0) {
1790
1791                                 if (errno == EINTR)
1792                                         continue;
1793
1794                                 log_error("Failed to accept socket: %m");
1795                                 goto fail;
1796                         }
1797
1798                         break;
1799                 }
1800
1801                 socket_apply_socket_options(s, cfd);
1802         }
1803
1804         socket_enter_running(s, cfd);
1805         return;
1806
1807 fail:
1808         socket_enter_stop_pre(s, false);
1809 }
1810
1811 static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1812         Socket *s = SOCKET(u);
1813         bool success;
1814
1815         assert(s);
1816         assert(pid >= 0);
1817
1818         if (pid != s->control_pid)
1819                 return;
1820
1821         s->control_pid = 0;
1822
1823         success = is_clean_exit(code, status);
1824
1825         if (s->control_command) {
1826                 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
1827
1828                 if (s->control_command->ignore)
1829                         success = true;
1830         }
1831
1832         log_full(success ? LOG_DEBUG : LOG_NOTICE,
1833                  "%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1834         s->failure = s->failure || !success;
1835
1836         if (s->control_command && s->control_command->command_next && success) {
1837                 log_debug("%s running next command for state %s", u->meta.id, socket_state_to_string(s->state));
1838                 socket_run_next(s, success);
1839         } else {
1840                 s->control_command = NULL;
1841                 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
1842
1843                 /* No further commands for this step, so let's figure
1844                  * out what to do next */
1845
1846                 log_debug("%s got final SIGCHLD for state %s", u->meta.id, socket_state_to_string(s->state));
1847
1848                 switch (s->state) {
1849
1850                 case SOCKET_START_PRE:
1851                         if (success)
1852                                 socket_enter_start_post(s);
1853                         else
1854                                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1855                         break;
1856
1857                 case SOCKET_START_POST:
1858                         if (success)
1859                                 socket_enter_listening(s);
1860                         else
1861                                 socket_enter_stop_pre(s, false);
1862                         break;
1863
1864                 case SOCKET_STOP_PRE:
1865                 case SOCKET_STOP_PRE_SIGTERM:
1866                 case SOCKET_STOP_PRE_SIGKILL:
1867                         socket_enter_stop_post(s, success);
1868                         break;
1869
1870                 case SOCKET_STOP_POST:
1871                 case SOCKET_FINAL_SIGTERM:
1872                 case SOCKET_FINAL_SIGKILL:
1873                         socket_enter_dead(s, success);
1874                         break;
1875
1876                 default:
1877                         assert_not_reached("Uh, control process died at wrong time.");
1878                 }
1879         }
1880
1881         /* Notify clients about changed exit status */
1882         unit_add_to_dbus_queue(u);
1883 }
1884
1885 static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1886         Socket *s = SOCKET(u);
1887
1888         assert(s);
1889         assert(elapsed == 1);
1890         assert(w == &s->timer_watch);
1891
1892         switch (s->state) {
1893
1894         case SOCKET_START_PRE:
1895                 log_warning("%s starting timed out. Terminating.", u->meta.id);
1896                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1897                 break;
1898
1899         case SOCKET_START_POST:
1900                 log_warning("%s starting timed out. Stopping.", u->meta.id);
1901                 socket_enter_stop_pre(s, false);
1902                 break;
1903
1904         case SOCKET_STOP_PRE:
1905                 log_warning("%s stopping timed out. Terminating.", u->meta.id);
1906                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false);
1907                 break;
1908
1909         case SOCKET_STOP_PRE_SIGTERM:
1910                 if (s->exec_context.send_sigkill) {
1911                         log_warning("%s stopping timed out. Killing.", u->meta.id);
1912                         socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false);
1913                 } else {
1914                         log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1915                         socket_enter_stop_post(s, false);
1916                 }
1917                 break;
1918
1919         case SOCKET_STOP_PRE_SIGKILL:
1920                 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
1921                 socket_enter_stop_post(s, false);
1922                 break;
1923
1924         case SOCKET_STOP_POST:
1925                 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
1926                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1927                 break;
1928
1929         case SOCKET_FINAL_SIGTERM:
1930                 if (s->exec_context.send_sigkill) {
1931                         log_warning("%s stopping timed out (2). Killing.", u->meta.id);
1932                         socket_enter_signal(s, SOCKET_FINAL_SIGKILL, false);
1933                 } else {
1934                         log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u->meta.id);
1935                         socket_enter_dead(s, false);
1936                 }
1937                 break;
1938
1939         case SOCKET_FINAL_SIGKILL:
1940                 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
1941                 socket_enter_dead(s, false);
1942                 break;
1943
1944         default:
1945                 assert_not_reached("Timeout at wrong time.");
1946         }
1947 }
1948
1949 int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) {
1950         int *rfds;
1951         unsigned rn_fds, k;
1952         SocketPort *p;
1953
1954         assert(s);
1955         assert(fds);
1956         assert(n_fds);
1957
1958         /* Called from the service code for requesting our fds */
1959
1960         rn_fds = 0;
1961         LIST_FOREACH(port, p, s->ports)
1962                 if (p->fd >= 0)
1963                         rn_fds++;
1964
1965         if (rn_fds <= 0) {
1966                 *fds = NULL;
1967                 *n_fds = 0;
1968                 return 0;
1969         }
1970
1971         if (!(rfds = new(int, rn_fds)))
1972                 return -ENOMEM;
1973
1974         k = 0;
1975         LIST_FOREACH(port, p, s->ports)
1976                 if (p->fd >= 0)
1977                         rfds[k++] = p->fd;
1978
1979         assert(k == rn_fds);
1980
1981         *fds = rfds;
1982         *n_fds = rn_fds;
1983
1984         return 0;
1985 }
1986
1987 void socket_notify_service_dead(Socket *s) {
1988         assert(s);
1989
1990         /* The service is dead. Dang!
1991          *
1992          * This is strictly for one-instance-for-all-connections
1993          * services. */
1994
1995         if (s->state == SOCKET_RUNNING) {
1996                 log_debug("%s got notified about service death.", s->meta.id);
1997                 socket_enter_listening(s);
1998         }
1999 }
2000
2001 void socket_connection_unref(Socket *s) {
2002         assert(s);
2003
2004         /* The service is dead. Yay!
2005          *
2006          * This is strictly for one-instance-per-connection
2007          * services. */
2008
2009         assert(s->n_connections > 0);
2010         s->n_connections--;
2011
2012         log_debug("%s: One connection closed, %u left.", s->meta.id, s->n_connections);
2013 }
2014
2015 static void socket_reset_failed(Unit *u) {
2016         Socket *s = SOCKET(u);
2017
2018         assert(s);
2019
2020         if (s->state == SOCKET_FAILED)
2021                 socket_set_state(s, SOCKET_DEAD);
2022
2023         s->failure = false;
2024 }
2025
2026 static int socket_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
2027         Socket *s = SOCKET(u);
2028         int r = 0;
2029         Set *pid_set = NULL;
2030
2031         assert(s);
2032
2033         if (who == KILL_MAIN) {
2034                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Socket units have no main processes");
2035                 return -ESRCH;
2036         }
2037
2038         if (s->control_pid <= 0 && who == KILL_CONTROL) {
2039                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
2040                 return -ESRCH;
2041         }
2042
2043         if (who == KILL_CONTROL || who == KILL_ALL)
2044                 if (s->control_pid > 0)
2045                         if (kill(s->control_pid, signo) < 0)
2046                                 r = -errno;
2047
2048         if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
2049                 int q;
2050
2051                 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
2052                         return -ENOMEM;
2053
2054                 /* Exclude the control pid from being killed via the cgroup */
2055                 if (s->control_pid > 0)
2056                         if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
2057                                 r = q;
2058                                 goto finish;
2059                         }
2060
2061                 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
2062                         if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
2063                                 r = q;
2064         }
2065
2066 finish:
2067         if (pid_set)
2068                 set_free(pid_set);
2069
2070         return r;
2071 }
2072
2073 static const char* const socket_state_table[_SOCKET_STATE_MAX] = {
2074         [SOCKET_DEAD] = "dead",
2075         [SOCKET_START_PRE] = "start-pre",
2076         [SOCKET_START_POST] = "start-post",
2077         [SOCKET_LISTENING] = "listening",
2078         [SOCKET_RUNNING] = "running",
2079         [SOCKET_STOP_PRE] = "stop-pre",
2080         [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
2081         [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
2082         [SOCKET_STOP_POST] = "stop-post",
2083         [SOCKET_FINAL_SIGTERM] = "final-sigterm",
2084         [SOCKET_FINAL_SIGKILL] = "final-sigkill",
2085         [SOCKET_FAILED] = "failed"
2086 };
2087
2088 DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState);
2089
2090 static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
2091         [SOCKET_EXEC_START_PRE] = "StartPre",
2092         [SOCKET_EXEC_START_POST] = "StartPost",
2093         [SOCKET_EXEC_STOP_PRE] = "StopPre",
2094         [SOCKET_EXEC_STOP_POST] = "StopPost"
2095 };
2096
2097 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand);
2098
2099 const UnitVTable socket_vtable = {
2100         .suffix = ".socket",
2101         .sections =
2102                 "Unit\0"
2103                 "Socket\0"
2104                 "Install\0",
2105
2106         .init = socket_init,
2107         .done = socket_done,
2108         .load = socket_load,
2109
2110         .kill = socket_kill,
2111
2112         .coldplug = socket_coldplug,
2113
2114         .dump = socket_dump,
2115
2116         .start = socket_start,
2117         .stop = socket_stop,
2118
2119         .serialize = socket_serialize,
2120         .deserialize_item = socket_deserialize_item,
2121
2122         .active_state = socket_active_state,
2123         .sub_state_to_string = socket_sub_state_to_string,
2124
2125         .check_gc = socket_check_gc,
2126
2127         .fd_event = socket_fd_event,
2128         .sigchld_event = socket_sigchld_event,
2129         .timer_event = socket_timer_event,
2130
2131         .reset_failed = socket_reset_failed,
2132
2133         .bus_interface = "org.freedesktop.systemd1.Socket",
2134         .bus_message_handler = bus_socket_message_handler,
2135         .bus_invalidating_properties =  bus_socket_invalidating_properties
2136 };