chiark / gitweb /
c0139587e38b98d8e7beeafbd1a97f5e667204b3
[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_per_type, i, u->meta.manager->units_per_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_per_type, other, s->meta.manager->units_per_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 && errno != EEXIST) {
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                                         if ((r = label_get_socket_label_from_exe(s->service->exec_command[SERVICE_EXEC_START]->path, &label)) < 0) {
897                                                 if (r != -EPERM)
898                                                         return r;
899                                         }
900
901                                 know_label = true;
902                         }
903
904                         if ((r = socket_address_listen(
905                                              &p->address,
906                                              s->backlog,
907                                              s->bind_ipv6_only,
908                                              s->bind_to_device,
909                                              s->free_bind,
910                                              s->transparent,
911                                              s->directory_mode,
912                                              s->socket_mode,
913                                              label,
914                                              &p->fd)) < 0)
915                                 goto rollback;
916
917                         socket_apply_socket_options(s, p->fd);
918
919                 } else  if (p->type == SOCKET_SPECIAL) {
920
921                         if ((r = special_address_create(
922                                              p->path,
923                                              &p->fd)) < 0)
924                                 goto rollback;
925
926                 } else  if (p->type == SOCKET_FIFO) {
927
928                         if ((r = fifo_address_create(
929                                              p->path,
930                                              s->directory_mode,
931                                              s->socket_mode,
932                                              &p->fd)) < 0)
933                                 goto rollback;
934
935                         socket_apply_fifo_options(s, p->fd);
936                 } else if (p->type == SOCKET_MQUEUE) {
937
938                         if ((r = mq_address_create(
939                                              p->path,
940                                              s->socket_mode,
941                                              s->mq_maxmsg,
942                                              s->mq_msgsize,
943                                              &p->fd)) < 0)
944                                 goto rollback;
945                 } else
946                         assert_not_reached("Unknown port type");
947         }
948
949         label_free(label);
950         return 0;
951
952 rollback:
953         socket_close_fds(s);
954         label_free(label);
955         return r;
956 }
957
958 static void socket_unwatch_fds(Socket *s) {
959         SocketPort *p;
960
961         assert(s);
962
963         LIST_FOREACH(port, p, s->ports) {
964                 if (p->fd < 0)
965                         continue;
966
967                 unit_unwatch_fd(UNIT(s), &p->fd_watch);
968         }
969 }
970
971 static int socket_watch_fds(Socket *s) {
972         SocketPort *p;
973         int r;
974
975         assert(s);
976
977         LIST_FOREACH(port, p, s->ports) {
978                 if (p->fd < 0)
979                         continue;
980
981                 p->fd_watch.socket_accept =
982                         s->accept &&
983                         p->type == SOCKET_SOCKET &&
984                         socket_address_can_accept(&p->address);
985
986                 if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0)
987                         goto fail;
988         }
989
990         return 0;
991
992 fail:
993         socket_unwatch_fds(s);
994         return r;
995 }
996
997 static void socket_set_state(Socket *s, SocketState state) {
998         SocketState old_state;
999         assert(s);
1000
1001         old_state = s->state;
1002         s->state = state;
1003
1004         if (state != SOCKET_START_PRE &&
1005             state != SOCKET_START_POST &&
1006             state != SOCKET_STOP_PRE &&
1007             state != SOCKET_STOP_PRE_SIGTERM &&
1008             state != SOCKET_STOP_PRE_SIGKILL &&
1009             state != SOCKET_STOP_POST &&
1010             state != SOCKET_FINAL_SIGTERM &&
1011             state != SOCKET_FINAL_SIGKILL) {
1012                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1013                 socket_unwatch_control_pid(s);
1014                 s->control_command = NULL;
1015                 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
1016         }
1017
1018         if (state != SOCKET_LISTENING)
1019                 socket_unwatch_fds(s);
1020
1021         if (state != SOCKET_START_POST &&
1022             state != SOCKET_LISTENING &&
1023             state != SOCKET_RUNNING &&
1024             state != SOCKET_STOP_PRE &&
1025             state != SOCKET_STOP_PRE_SIGTERM &&
1026             state != SOCKET_STOP_PRE_SIGKILL)
1027                 socket_close_fds(s);
1028
1029         if (state != old_state)
1030                 log_debug("%s changed %s -> %s",
1031                           s->meta.id,
1032                           socket_state_to_string(old_state),
1033                           socket_state_to_string(state));
1034
1035         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
1036 }
1037
1038 static int socket_coldplug(Unit *u) {
1039         Socket *s = SOCKET(u);
1040         int r;
1041
1042         assert(s);
1043         assert(s->state == SOCKET_DEAD);
1044
1045         if (s->deserialized_state != s->state) {
1046
1047                 if (s->deserialized_state == SOCKET_START_PRE ||
1048                     s->deserialized_state == SOCKET_START_POST ||
1049                     s->deserialized_state == SOCKET_STOP_PRE ||
1050                     s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
1051                     s->deserialized_state == SOCKET_STOP_PRE_SIGKILL ||
1052                     s->deserialized_state == SOCKET_STOP_POST ||
1053                     s->deserialized_state == SOCKET_FINAL_SIGTERM ||
1054                     s->deserialized_state == SOCKET_FINAL_SIGKILL) {
1055
1056                         if (s->control_pid <= 0)
1057                                 return -EBADMSG;
1058
1059                         if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1060                                 return r;
1061
1062                         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1063                                 return r;
1064                 }
1065
1066                 if (s->deserialized_state == SOCKET_START_POST ||
1067                     s->deserialized_state == SOCKET_LISTENING ||
1068                     s->deserialized_state == SOCKET_RUNNING ||
1069                     s->deserialized_state == SOCKET_STOP_PRE ||
1070                     s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
1071                     s->deserialized_state == SOCKET_STOP_PRE_SIGKILL)
1072                         if ((r = socket_open_fds(s)) < 0)
1073                                 return r;
1074
1075                 if (s->deserialized_state == SOCKET_LISTENING)
1076                         if ((r = socket_watch_fds(s)) < 0)
1077                                 return r;
1078
1079                 socket_set_state(s, s->deserialized_state);
1080         }
1081
1082         return 0;
1083 }
1084
1085 static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
1086         pid_t pid;
1087         int r;
1088         char **argv;
1089
1090         assert(s);
1091         assert(c);
1092         assert(_pid);
1093
1094         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1095                 goto fail;
1096
1097         if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1098                 r = -ENOMEM;
1099                 goto fail;
1100         }
1101
1102         r = exec_spawn(c,
1103                        argv,
1104                        &s->exec_context,
1105                        NULL, 0,
1106                        s->meta.manager->environment,
1107                        true,
1108                        true,
1109                        true,
1110                        s->meta.manager->confirm_spawn,
1111                        s->meta.cgroup_bondings,
1112                        &pid);
1113
1114         strv_free(argv);
1115         if (r < 0)
1116                 goto fail;
1117
1118         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1119                 /* FIXME: we need to do something here */
1120                 goto fail;
1121
1122         *_pid = pid;
1123
1124         return 0;
1125
1126 fail:
1127         unit_unwatch_timer(UNIT(s), &s->timer_watch);
1128
1129         return r;
1130 }
1131
1132 static void socket_enter_dead(Socket *s, bool success) {
1133         assert(s);
1134
1135         if (!success)
1136                 s->failure = true;
1137
1138         socket_set_state(s, s->failure ? SOCKET_FAILED : SOCKET_DEAD);
1139 }
1140
1141 static void socket_enter_signal(Socket *s, SocketState state, bool success);
1142
1143 static void socket_enter_stop_post(Socket *s, bool success) {
1144         int r;
1145         assert(s);
1146
1147         if (!success)
1148                 s->failure = true;
1149
1150         socket_unwatch_control_pid(s);
1151
1152         s->control_command_id = SOCKET_EXEC_STOP_POST;
1153
1154         if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) {
1155                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1156                         goto fail;
1157
1158                 socket_set_state(s, SOCKET_STOP_POST);
1159         } else
1160                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, true);
1161
1162         return;
1163
1164 fail:
1165         log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1166         socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1167 }
1168
1169 static void socket_enter_signal(Socket *s, SocketState state, bool success) {
1170         int r;
1171         Set *pid_set = NULL;
1172         bool wait_for_exit = false;
1173
1174         assert(s);
1175
1176         if (!success)
1177                 s->failure = true;
1178
1179         if (s->exec_context.kill_mode != KILL_NONE) {
1180                 int sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1181
1182                 if (s->control_pid > 0) {
1183                         if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1184
1185                                 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1186                         else
1187                                 wait_for_exit = true;
1188                 }
1189
1190                 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1191
1192                         if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1193                                 r = -ENOMEM;
1194                                 goto fail;
1195                         }
1196
1197                         /* Exclude the control pid from being killed via the cgroup */
1198                         if (s->control_pid > 0)
1199                                 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1200                                         goto fail;
1201
1202                         if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
1203                                 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1204                                         log_warning("Failed to kill control group: %s", strerror(-r));
1205                         } else if (r > 0)
1206                                 wait_for_exit = true;
1207
1208                         set_free(pid_set);
1209                         pid_set = NULL;
1210                 }
1211         }
1212
1213         if (wait_for_exit) {
1214                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1215                         goto fail;
1216
1217                 socket_set_state(s, state);
1218         } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
1219                 socket_enter_stop_post(s, true);
1220         else
1221                 socket_enter_dead(s, true);
1222
1223         return;
1224
1225 fail:
1226         log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1227
1228         if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
1229                 socket_enter_stop_post(s, false);
1230         else
1231                 socket_enter_dead(s, false);
1232
1233         if (pid_set)
1234                 set_free(pid_set);
1235 }
1236
1237 static void socket_enter_stop_pre(Socket *s, bool success) {
1238         int r;
1239         assert(s);
1240
1241         if (!success)
1242                 s->failure = true;
1243
1244         socket_unwatch_control_pid(s);
1245
1246         s->control_command_id = SOCKET_EXEC_STOP_PRE;
1247
1248         if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) {
1249                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1250                         goto fail;
1251
1252                 socket_set_state(s, SOCKET_STOP_PRE);
1253         } else
1254                 socket_enter_stop_post(s, true);
1255
1256         return;
1257
1258 fail:
1259         log_warning("%s failed to run 'stop-pre' task: %s", s->meta.id, strerror(-r));
1260         socket_enter_stop_post(s, false);
1261 }
1262
1263 static void socket_enter_listening(Socket *s) {
1264         int r;
1265         assert(s);
1266
1267         if ((r = socket_watch_fds(s)) < 0) {
1268                 log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r));
1269                 goto fail;
1270         }
1271
1272         socket_set_state(s, SOCKET_LISTENING);
1273         return;
1274
1275 fail:
1276         socket_enter_stop_pre(s, false);
1277 }
1278
1279 static void socket_enter_start_post(Socket *s) {
1280         int r;
1281         assert(s);
1282
1283         if ((r = socket_open_fds(s)) < 0) {
1284                 log_warning("%s failed to listen on sockets: %s", s->meta.id, strerror(-r));
1285                 goto fail;
1286         }
1287
1288         socket_unwatch_control_pid(s);
1289
1290         s->control_command_id = SOCKET_EXEC_START_POST;
1291
1292         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
1293                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) {
1294                         log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1295                         goto fail;
1296                 }
1297
1298                 socket_set_state(s, SOCKET_START_POST);
1299         } else
1300                 socket_enter_listening(s);
1301
1302         return;
1303
1304 fail:
1305         socket_enter_stop_pre(s, false);
1306 }
1307
1308 static void socket_enter_start_pre(Socket *s) {
1309         int r;
1310         assert(s);
1311
1312         socket_unwatch_control_pid(s);
1313
1314         s->control_command_id = SOCKET_EXEC_START_PRE;
1315
1316         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
1317                 if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1318                         goto fail;
1319
1320                 socket_set_state(s, SOCKET_START_PRE);
1321         } else
1322                 socket_enter_start_post(s);
1323
1324         return;
1325
1326 fail:
1327         log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1328         socket_enter_dead(s, false);
1329 }
1330
1331 static void socket_enter_running(Socket *s, int cfd) {
1332         int r;
1333         DBusError error;
1334
1335         assert(s);
1336         dbus_error_init(&error);
1337
1338         /* We don't take connections anymore if we are supposed to
1339          * shut down anyway */
1340         if (unit_pending_inactive(UNIT(s))) {
1341                 log_debug("Suppressing connection request on %s since unit stop is scheduled.", s->meta.id);
1342
1343                 if (cfd >= 0)
1344                         close_nointr_nofail(cfd);
1345                 else  {
1346                         /* Flush all sockets by closing and reopening them */
1347                         socket_close_fds(s);
1348
1349                         if ((r = socket_watch_fds(s)) < 0) {
1350                                 log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r));
1351                                 socket_enter_stop_pre(s, false);
1352                         }
1353                 }
1354
1355                 return;
1356         }
1357
1358         if (cfd < 0) {
1359                 bool pending = false;
1360                 Meta *i;
1361
1362                 /* If there's already a start pending don't bother to
1363                  * do anything */
1364                 LIST_FOREACH(units_per_type, i, s->meta.manager->units_per_type[UNIT_SERVICE]) {
1365                         Service *service = (Service *) i;
1366
1367                         if (!set_get(service->configured_sockets, s))
1368                                 continue;
1369
1370                         if (!unit_pending_active(UNIT(service)))
1371                                 continue;
1372
1373                         pending = true;
1374                         break;
1375                 }
1376
1377                 if (!pending)
1378                         if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, &error, NULL)) < 0)
1379                                 goto fail;
1380
1381                 socket_set_state(s, SOCKET_RUNNING);
1382         } else {
1383                 char *prefix, *instance = NULL, *name;
1384                 Service *service;
1385
1386                 if (s->n_connections >= s->max_connections) {
1387                         log_warning("Too many incoming connections (%u)", s->n_connections);
1388                         close_nointr_nofail(cfd);
1389                         return;
1390                 }
1391
1392                 if ((r = socket_instantiate_service(s)) < 0)
1393                         goto fail;
1394
1395                 if ((r = instance_from_socket(cfd, s->n_accepted, &instance)) < 0)
1396                         goto fail;
1397
1398                 if (!(prefix = unit_name_to_prefix(s->meta.id))) {
1399                         free(instance);
1400                         r = -ENOMEM;
1401                         goto fail;
1402                 }
1403
1404                 name = unit_name_build(prefix, instance, ".service");
1405                 free(prefix);
1406                 free(instance);
1407
1408                 if (!name) {
1409                         r = -ENOMEM;
1410                         goto fail;
1411                 }
1412
1413                 if ((r = unit_add_name(UNIT(s->service), name)) < 0) {
1414                         free(name);
1415                         goto fail;
1416                 }
1417
1418                 service = s->service;
1419                 s->service = NULL;
1420                 s->n_accepted ++;
1421
1422                 service->meta.no_gc = false;
1423
1424                 unit_choose_id(UNIT(service), name);
1425                 free(name);
1426
1427                 if ((r = service_set_socket_fd(service, cfd, s)) < 0)
1428                         goto fail;
1429
1430                 cfd = -1;
1431                 s->n_connections ++;
1432
1433                 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL)) < 0)
1434                         goto fail;
1435
1436                 /* Notify clients about changed counters */
1437                 unit_add_to_dbus_queue(UNIT(s));
1438         }
1439
1440         return;
1441
1442 fail:
1443         log_warning("%s failed to queue socket startup job: %s", s->meta.id, bus_error(&error, r));
1444         socket_enter_stop_pre(s, false);
1445
1446         if (cfd >= 0)
1447                 close_nointr_nofail(cfd);
1448
1449         dbus_error_free(&error);
1450 }
1451
1452 static void socket_run_next(Socket *s, bool success) {
1453         int r;
1454
1455         assert(s);
1456         assert(s->control_command);
1457         assert(s->control_command->command_next);
1458
1459         if (!success)
1460                 s->failure = true;
1461
1462         socket_unwatch_control_pid(s);
1463
1464         s->control_command = s->control_command->command_next;
1465
1466         if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
1467                 goto fail;
1468
1469         return;
1470
1471 fail:
1472         log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r));
1473
1474         if (s->state == SOCKET_START_POST)
1475                 socket_enter_stop_pre(s, false);
1476         else if (s->state == SOCKET_STOP_POST)
1477                 socket_enter_dead(s, false);
1478         else
1479                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1480 }
1481
1482 static int socket_start(Unit *u) {
1483         Socket *s = SOCKET(u);
1484
1485         assert(s);
1486
1487         /* We cannot fulfill this request right now, try again later
1488          * please! */
1489         if (s->state == SOCKET_STOP_PRE ||
1490             s->state == SOCKET_STOP_PRE_SIGKILL ||
1491             s->state == SOCKET_STOP_PRE_SIGTERM ||
1492             s->state == SOCKET_STOP_POST ||
1493             s->state == SOCKET_FINAL_SIGTERM ||
1494             s->state == SOCKET_FINAL_SIGKILL)
1495                 return -EAGAIN;
1496
1497         if (s->state == SOCKET_START_PRE ||
1498             s->state == SOCKET_START_POST)
1499                 return 0;
1500
1501         /* Cannot run this without the service being around */
1502         if (s->service) {
1503                 if (s->service->meta.load_state != UNIT_LOADED) {
1504                         log_error("Socket service %s not loaded, refusing.", s->service->meta.id);
1505                         return -ENOENT;
1506                 }
1507
1508                 /* If the service is already active we cannot start the
1509                  * socket */
1510                 if (s->service->state != SERVICE_DEAD &&
1511                     s->service->state != SERVICE_FAILED &&
1512                     s->service->state != SERVICE_AUTO_RESTART) {
1513                         log_error("Socket service %s already active, refusing.", s->service->meta.id);
1514                         return -EBUSY;
1515                 }
1516
1517 #ifdef HAVE_SYSV_COMPAT
1518                 if (s->service->sysv_path) {
1519                         log_error("Using SysV services for socket activation is not supported. Refusing.");
1520                         return -ENOENT;
1521                 }
1522 #endif
1523         }
1524
1525         assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED);
1526
1527         s->failure = false;
1528         socket_enter_start_pre(s);
1529         return 0;
1530 }
1531
1532 static int socket_stop(Unit *u) {
1533         Socket *s = SOCKET(u);
1534
1535         assert(s);
1536
1537         /* Already on it */
1538         if (s->state == SOCKET_STOP_PRE ||
1539             s->state == SOCKET_STOP_PRE_SIGTERM ||
1540             s->state == SOCKET_STOP_PRE_SIGKILL ||
1541             s->state == SOCKET_STOP_POST ||
1542             s->state == SOCKET_FINAL_SIGTERM ||
1543             s->state == SOCKET_FINAL_SIGKILL)
1544                 return 0;
1545
1546         /* If there's already something running we go directly into
1547          * kill mode. */
1548         if (s->state == SOCKET_START_PRE ||
1549             s->state == SOCKET_START_POST) {
1550                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, true);
1551                 return -EAGAIN;
1552         }
1553
1554         assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
1555
1556         socket_enter_stop_pre(s, true);
1557         return 0;
1558 }
1559
1560 static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
1561         Socket *s = SOCKET(u);
1562         SocketPort *p;
1563         int r;
1564
1565         assert(u);
1566         assert(f);
1567         assert(fds);
1568
1569         unit_serialize_item(u, f, "state", socket_state_to_string(s->state));
1570         unit_serialize_item(u, f, "failure", yes_no(s->failure));
1571         unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted);
1572
1573         if (s->control_pid > 0)
1574                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1575
1576         if (s->control_command_id >= 0)
1577                 unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id));
1578
1579         LIST_FOREACH(port, p, s->ports) {
1580                 int copy;
1581
1582                 if (p->fd < 0)
1583                         continue;
1584
1585                 if ((copy = fdset_put_dup(fds, p->fd)) < 0)
1586                         return copy;
1587
1588                 if (p->type == SOCKET_SOCKET) {
1589                         char *t;
1590
1591                         if ((r = socket_address_print(&p->address, &t)) < 0)
1592                                 return r;
1593
1594                         if (socket_address_family(&p->address) == AF_NETLINK)
1595                                 unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t);
1596                         else
1597                                 unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
1598                         free(t);
1599                 } else if (p->type == SOCKET_SPECIAL)
1600                         unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path);
1601                 else {
1602                         assert(p->type == SOCKET_FIFO);
1603                         unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path);
1604                 }
1605         }
1606
1607         return 0;
1608 }
1609
1610 static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1611         Socket *s = SOCKET(u);
1612
1613         assert(u);
1614         assert(key);
1615         assert(value);
1616         assert(fds);
1617
1618         if (streq(key, "state")) {
1619                 SocketState state;
1620
1621                 if ((state = socket_state_from_string(value)) < 0)
1622                         log_debug("Failed to parse state value %s", value);
1623                 else
1624                         s->deserialized_state = state;
1625         } else if (streq(key, "failure")) {
1626                 int b;
1627
1628                 if ((b = parse_boolean(value)) < 0)
1629                         log_debug("Failed to parse failure value %s", value);
1630                 else
1631                         s->failure = b || s->failure;
1632
1633         } else if (streq(key, "n-accepted")) {
1634                 unsigned k;
1635
1636                 if (safe_atou(value, &k) < 0)
1637                         log_debug("Failed to parse n-accepted value %s", value);
1638                 else
1639                         s->n_accepted += k;
1640         } else if (streq(key, "control-pid")) {
1641                 pid_t pid;
1642
1643                 if (parse_pid(value, &pid) < 0)
1644                         log_debug("Failed to parse control-pid value %s", value);
1645                 else
1646                         s->control_pid = pid;
1647         } else if (streq(key, "control-command")) {
1648                 SocketExecCommand id;
1649
1650                 if ((id = socket_exec_command_from_string(value)) < 0)
1651                         log_debug("Failed to parse exec-command value %s", value);
1652                 else {
1653                         s->control_command_id = id;
1654                         s->control_command = s->exec_command[id];
1655                 }
1656         } else if (streq(key, "fifo")) {
1657                 int fd, skip = 0;
1658                 SocketPort *p;
1659
1660                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
1661                         log_debug("Failed to parse fifo value %s", value);
1662                 else {
1663
1664                         LIST_FOREACH(port, p, s->ports)
1665                                 if (p->type == SOCKET_FIFO &&
1666                                     streq_ptr(p->path, value+skip))
1667                                         break;
1668
1669                         if (p) {
1670                                 if (p->fd >= 0)
1671                                         close_nointr_nofail(p->fd);
1672                                 p->fd = fdset_remove(fds, fd);
1673                         }
1674                 }
1675
1676         } else if (streq(key, "special")) {
1677                 int fd, skip = 0;
1678                 SocketPort *p;
1679
1680                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
1681                         log_debug("Failed to parse special value %s", value);
1682                 else {
1683
1684                         LIST_FOREACH(port, p, s->ports)
1685                                 if (p->type == SOCKET_SPECIAL &&
1686                                     streq_ptr(p->path, value+skip))
1687                                         break;
1688
1689                         if (p) {
1690                                 if (p->fd >= 0)
1691                                         close_nointr_nofail(p->fd);
1692                                 p->fd = fdset_remove(fds, fd);
1693                         }
1694                 }
1695
1696         } else if (streq(key, "socket")) {
1697                 int fd, type, skip = 0;
1698                 SocketPort *p;
1699
1700                 if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
1701                         log_debug("Failed to parse socket value %s", value);
1702                 else {
1703
1704                         LIST_FOREACH(port, p, s->ports)
1705                                 if (socket_address_is(&p->address, value+skip, type))
1706                                         break;
1707
1708                         if (p) {
1709                                 if (p->fd >= 0)
1710                                         close_nointr_nofail(p->fd);
1711                                 p->fd = fdset_remove(fds, fd);
1712                         }
1713                 }
1714
1715         } else if (streq(key, "netlink")) {
1716                 int fd, skip = 0;
1717                 SocketPort *p;
1718
1719                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
1720                         log_debug("Failed to parse socket value %s", value);
1721                 else {
1722
1723                         LIST_FOREACH(port, p, s->ports)
1724                                 if (socket_address_is_netlink(&p->address, value+skip))
1725                                         break;
1726
1727                         if (p) {
1728                                 if (p->fd >= 0)
1729                                         close_nointr_nofail(p->fd);
1730                                 p->fd = fdset_remove(fds, fd);
1731                         }
1732                 }
1733
1734         } else
1735                 log_debug("Unknown serialization key '%s'", key);
1736
1737         return 0;
1738 }
1739
1740 static UnitActiveState socket_active_state(Unit *u) {
1741         assert(u);
1742
1743         return state_translation_table[SOCKET(u)->state];
1744 }
1745
1746 static const char *socket_sub_state_to_string(Unit *u) {
1747         assert(u);
1748
1749         return socket_state_to_string(SOCKET(u)->state);
1750 }
1751
1752 static bool socket_check_gc(Unit *u) {
1753         Socket *s = SOCKET(u);
1754
1755         assert(u);
1756
1757         return s->n_connections > 0;
1758 }
1759
1760 static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
1761         Socket *s = SOCKET(u);
1762         int cfd = -1;
1763
1764         assert(s);
1765         assert(fd >= 0);
1766
1767         if (s->state != SOCKET_LISTENING)
1768                 return;
1769
1770         log_debug("Incoming traffic on %s", u->meta.id);
1771
1772         if (events != EPOLLIN) {
1773
1774                 if (events & EPOLLHUP)
1775                         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);
1776                 else
1777                         log_error("%s: Got unexpected poll event (0x%x) on socket.", u->meta.id, events);
1778
1779                 goto fail;
1780         }
1781
1782         if (w->socket_accept) {
1783                 for (;;) {
1784
1785                         if ((cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK)) < 0) {
1786
1787                                 if (errno == EINTR)
1788                                         continue;
1789
1790                                 log_error("Failed to accept socket: %m");
1791                                 goto fail;
1792                         }
1793
1794                         break;
1795                 }
1796
1797                 socket_apply_socket_options(s, cfd);
1798         }
1799
1800         socket_enter_running(s, cfd);
1801         return;
1802
1803 fail:
1804         socket_enter_stop_pre(s, false);
1805 }
1806
1807 static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1808         Socket *s = SOCKET(u);
1809         bool success;
1810
1811         assert(s);
1812         assert(pid >= 0);
1813
1814         if (pid != s->control_pid)
1815                 return;
1816
1817         s->control_pid = 0;
1818
1819         success = is_clean_exit(code, status);
1820
1821         if (s->control_command) {
1822                 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
1823
1824                 if (s->control_command->ignore)
1825                         success = true;
1826         }
1827
1828         log_full(success ? LOG_DEBUG : LOG_NOTICE,
1829                  "%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1830         s->failure = s->failure || !success;
1831
1832         if (s->control_command && s->control_command->command_next && success) {
1833                 log_debug("%s running next command for state %s", u->meta.id, socket_state_to_string(s->state));
1834                 socket_run_next(s, success);
1835         } else {
1836                 s->control_command = NULL;
1837                 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
1838
1839                 /* No further commands for this step, so let's figure
1840                  * out what to do next */
1841
1842                 log_debug("%s got final SIGCHLD for state %s", u->meta.id, socket_state_to_string(s->state));
1843
1844                 switch (s->state) {
1845
1846                 case SOCKET_START_PRE:
1847                         if (success)
1848                                 socket_enter_start_post(s);
1849                         else
1850                                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1851                         break;
1852
1853                 case SOCKET_START_POST:
1854                         if (success)
1855                                 socket_enter_listening(s);
1856                         else
1857                                 socket_enter_stop_pre(s, false);
1858                         break;
1859
1860                 case SOCKET_STOP_PRE:
1861                 case SOCKET_STOP_PRE_SIGTERM:
1862                 case SOCKET_STOP_PRE_SIGKILL:
1863                         socket_enter_stop_post(s, success);
1864                         break;
1865
1866                 case SOCKET_STOP_POST:
1867                 case SOCKET_FINAL_SIGTERM:
1868                 case SOCKET_FINAL_SIGKILL:
1869                         socket_enter_dead(s, success);
1870                         break;
1871
1872                 default:
1873                         assert_not_reached("Uh, control process died at wrong time.");
1874                 }
1875         }
1876
1877         /* Notify clients about changed exit status */
1878         unit_add_to_dbus_queue(u);
1879 }
1880
1881 static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1882         Socket *s = SOCKET(u);
1883
1884         assert(s);
1885         assert(elapsed == 1);
1886         assert(w == &s->timer_watch);
1887
1888         switch (s->state) {
1889
1890         case SOCKET_START_PRE:
1891                 log_warning("%s starting timed out. Terminating.", u->meta.id);
1892                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1893                 break;
1894
1895         case SOCKET_START_POST:
1896                 log_warning("%s starting timed out. Stopping.", u->meta.id);
1897                 socket_enter_stop_pre(s, false);
1898                 break;
1899
1900         case SOCKET_STOP_PRE:
1901                 log_warning("%s stopping timed out. Terminating.", u->meta.id);
1902                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false);
1903                 break;
1904
1905         case SOCKET_STOP_PRE_SIGTERM:
1906                 if (s->exec_context.send_sigkill) {
1907                         log_warning("%s stopping timed out. Killing.", u->meta.id);
1908                         socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false);
1909                 } else {
1910                         log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1911                         socket_enter_stop_post(s, false);
1912                 }
1913                 break;
1914
1915         case SOCKET_STOP_PRE_SIGKILL:
1916                 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
1917                 socket_enter_stop_post(s, false);
1918                 break;
1919
1920         case SOCKET_STOP_POST:
1921                 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
1922                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
1923                 break;
1924
1925         case SOCKET_FINAL_SIGTERM:
1926                 if (s->exec_context.send_sigkill) {
1927                         log_warning("%s stopping timed out (2). Killing.", u->meta.id);
1928                         socket_enter_signal(s, SOCKET_FINAL_SIGKILL, false);
1929                 } else {
1930                         log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u->meta.id);
1931                         socket_enter_dead(s, false);
1932                 }
1933                 break;
1934
1935         case SOCKET_FINAL_SIGKILL:
1936                 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
1937                 socket_enter_dead(s, false);
1938                 break;
1939
1940         default:
1941                 assert_not_reached("Timeout at wrong time.");
1942         }
1943 }
1944
1945 int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) {
1946         int *rfds;
1947         unsigned rn_fds, k;
1948         SocketPort *p;
1949
1950         assert(s);
1951         assert(fds);
1952         assert(n_fds);
1953
1954         /* Called from the service code for requesting our fds */
1955
1956         rn_fds = 0;
1957         LIST_FOREACH(port, p, s->ports)
1958                 if (p->fd >= 0)
1959                         rn_fds++;
1960
1961         if (!(rfds = new(int, rn_fds)))
1962                 return -ENOMEM;
1963
1964         k = 0;
1965         LIST_FOREACH(port, p, s->ports)
1966                 if (p->fd >= 0)
1967                         rfds[k++] = p->fd;
1968
1969         assert(k == rn_fds);
1970
1971         *fds = rfds;
1972         *n_fds = rn_fds;
1973
1974         return 0;
1975 }
1976
1977 void socket_notify_service_dead(Socket *s) {
1978         assert(s);
1979
1980         /* The service is dead. Dang!
1981          *
1982          * This is strictly for one-instance-for-all-connections
1983          * services. */
1984
1985         if (s->state == SOCKET_RUNNING) {
1986                 log_debug("%s got notified about service death.", s->meta.id);
1987                 socket_enter_listening(s);
1988         }
1989 }
1990
1991 void socket_connection_unref(Socket *s) {
1992         assert(s);
1993
1994         /* The service is dead. Yay!
1995          *
1996          * This is strictly for one-instance-per-connection
1997          * services. */
1998
1999         assert(s->n_connections > 0);
2000         s->n_connections--;
2001
2002         log_debug("%s: One connection closed, %u left.", s->meta.id, s->n_connections);
2003 }
2004
2005 static void socket_reset_failed(Unit *u) {
2006         Socket *s = SOCKET(u);
2007
2008         assert(s);
2009
2010         if (s->state == SOCKET_FAILED)
2011                 socket_set_state(s, SOCKET_DEAD);
2012
2013         s->failure = false;
2014 }
2015
2016 static int socket_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
2017         Socket *s = SOCKET(u);
2018         int r = 0;
2019         Set *pid_set = NULL;
2020
2021         assert(s);
2022
2023         if (who == KILL_MAIN) {
2024                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Socket units have no main processes");
2025                 return -EINVAL;
2026         }
2027
2028         if (s->control_pid <= 0 && who == KILL_CONTROL) {
2029                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
2030                 return -ENOENT;
2031         }
2032
2033         if (s->control_pid > 0)
2034                 if (kill(s->control_pid, signo) < 0)
2035                         r = -errno;
2036
2037         if (mode == KILL_CONTROL_GROUP) {
2038                 int q;
2039
2040                 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
2041                         return -ENOMEM;
2042
2043                 /* Exclude the control pid from being killed via the cgroup */
2044                 if (s->control_pid > 0)
2045                         if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
2046                                 r = q;
2047                                 goto finish;
2048                         }
2049
2050                 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
2051                         if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
2052                                 r = q;
2053         }
2054
2055 finish:
2056         if (pid_set)
2057                 set_free(pid_set);
2058
2059         return r;
2060 }
2061
2062 static const char* const socket_state_table[_SOCKET_STATE_MAX] = {
2063         [SOCKET_DEAD] = "dead",
2064         [SOCKET_START_PRE] = "start-pre",
2065         [SOCKET_START_POST] = "start-post",
2066         [SOCKET_LISTENING] = "listening",
2067         [SOCKET_RUNNING] = "running",
2068         [SOCKET_STOP_PRE] = "stop-pre",
2069         [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
2070         [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
2071         [SOCKET_STOP_POST] = "stop-post",
2072         [SOCKET_FINAL_SIGTERM] = "final-sigterm",
2073         [SOCKET_FINAL_SIGKILL] = "final-sigkill",
2074         [SOCKET_FAILED] = "failed"
2075 };
2076
2077 DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState);
2078
2079 static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
2080         [SOCKET_EXEC_START_PRE] = "StartPre",
2081         [SOCKET_EXEC_START_POST] = "StartPost",
2082         [SOCKET_EXEC_STOP_PRE] = "StopPre",
2083         [SOCKET_EXEC_STOP_POST] = "StopPost"
2084 };
2085
2086 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand);
2087
2088 const UnitVTable socket_vtable = {
2089         .suffix = ".socket",
2090
2091         .init = socket_init,
2092         .done = socket_done,
2093         .load = socket_load,
2094
2095         .kill = socket_kill,
2096
2097         .coldplug = socket_coldplug,
2098
2099         .dump = socket_dump,
2100
2101         .start = socket_start,
2102         .stop = socket_stop,
2103
2104         .serialize = socket_serialize,
2105         .deserialize_item = socket_deserialize_item,
2106
2107         .active_state = socket_active_state,
2108         .sub_state_to_string = socket_sub_state_to_string,
2109
2110         .check_gc = socket_check_gc,
2111
2112         .fd_event = socket_fd_event,
2113         .sigchld_event = socket_sigchld_event,
2114         .timer_event = socket_timer_event,
2115
2116         .reset_failed = socket_reset_failed,
2117
2118         .bus_interface = "org.freedesktop.systemd1.Socket",
2119         .bus_message_handler = bus_socket_message_handler,
2120         .bus_invalidating_properties =  bus_socket_invalidating_properties
2121 };