]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/socket.c
Merge pull request #3201 from ssahani/net-word
[thirdparty/systemd.git] / src / core / socket.c
CommitLineData
a7334b09
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
a7334b09 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
e8da24a6 20#include <arpa/inet.h>
83c60c9f
LP
21#include <errno.h>
22#include <fcntl.h>
916abb21 23#include <mqueue.h>
e8da24a6
LP
24#include <netinet/tcp.h>
25#include <signal.h>
26#include <sys/epoll.h>
27#include <sys/stat.h>
28#include <unistd.h>
62bc4efc 29#include <linux/sctp.h>
83c60c9f 30
718db961 31#include "sd-event.h"
b5efdb8a 32#include "alloc-util.h"
e8da24a6
LP
33#include "bus-error.h"
34#include "bus-util.h"
35#include "copy.h"
36#include "dbus-socket.h"
37#include "def.h"
38#include "exit-status.h"
3ffd4af2 39#include "fd-util.h"
e8da24a6
LP
40#include "formats-util.h"
41#include "label.h"
83c60c9f 42#include "log.h"
e8da24a6 43#include "missing.h"
49e942b2 44#include "mkdir.h"
6bedfcbb 45#include "parse-util.h"
9eb977db 46#include "path-util.h"
7b3e062c 47#include "process-util.h"
d7b8eec7 48#include "selinux-util.h"
24882e06 49#include "signal-util.h"
e8da24a6 50#include "smack-util.h"
24882e06 51#include "socket.h"
e8da24a6 52#include "special.h"
8b43440b 53#include "string-table.h"
07630cea 54#include "string-util.h"
e8da24a6
LP
55#include "strv.h"
56#include "unit-name.h"
57#include "unit-printf.h"
58#include "unit.h"
b1d4f8e1 59#include "user-util.h"
83c60c9f 60
acbb0225 61static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
87f0e418
LP
62 [SOCKET_DEAD] = UNIT_INACTIVE,
63 [SOCKET_START_PRE] = UNIT_ACTIVATING,
3900e5fd 64 [SOCKET_START_CHOWN] = UNIT_ACTIVATING,
87f0e418
LP
65 [SOCKET_START_POST] = UNIT_ACTIVATING,
66 [SOCKET_LISTENING] = UNIT_ACTIVE,
67 [SOCKET_RUNNING] = UNIT_ACTIVE,
68 [SOCKET_STOP_PRE] = UNIT_DEACTIVATING,
69 [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING,
70 [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING,
71 [SOCKET_STOP_POST] = UNIT_DEACTIVATING,
80876c20
LP
72 [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING,
73 [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING,
fdf20a31 74 [SOCKET_FAILED] = UNIT_FAILED
83c60c9f 75};
5cb5a6ff 76
718db961
LP
77static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
78static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
79
a16e1123
LP
80static void socket_init(Unit *u) {
81 Socket *s = SOCKET(u);
82
83 assert(u);
ac155bb8 84 assert(u->load_state == UNIT_STUB);
a16e1123 85
a16e1123 86 s->backlog = SOMAXCONN;
1f19a534 87 s->timeout_usec = u->manager->default_timeout_start_usec;
a16e1123 88 s->directory_mode = 0755;
9131f660 89 s->socket_mode = 0666;
a16e1123 90
6cf6bbc2
LP
91 s->max_connections = 64;
92
4fd5948e 93 s->priority = -1;
4fd5948e
LP
94 s->ip_tos = -1;
95 s->ip_ttl = -1;
4fd5948e 96 s->mark = -1;
4fd5948e 97
ac155bb8
MS
98 s->exec_context.std_output = u->manager->default_std_output;
99 s->exec_context.std_error = u->manager->default_std_error;
085afe36 100
a16e1123 101 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
8b26cdbd 102
1f15ce28
LP
103 s->trigger_limit.interval = USEC_INFINITY;
104 s->trigger_limit.burst = (unsigned) -1;
a16e1123 105}
acbb0225 106
5e94833f
LP
107static void socket_unwatch_control_pid(Socket *s) {
108 assert(s);
109
110 if (s->control_pid <= 0)
111 return;
112
113 unit_unwatch_pid(UNIT(s), s->control_pid);
114 s->control_pid = 0;
115}
116
15087cdb 117static void socket_cleanup_fd_list(SocketPort *p) {
3ffd4af2 118 assert(p);
15087cdb 119
3ffd4af2 120 close_many(p->auxiliary_fds, p->n_auxiliary_fds);
15087cdb
PS
121 p->auxiliary_fds = mfree(p->auxiliary_fds);
122 p->n_auxiliary_fds = 0;
123}
124
74051b9b 125void socket_free_ports(Socket *s) {
034c6ed7
LP
126 SocketPort *p;
127
128 assert(s);
129
130 while ((p = s->ports)) {
71fda00f 131 LIST_REMOVE(port, s->ports, p);
034c6ed7 132
718db961
LP
133 sd_event_source_unref(p->event_source);
134
15087cdb 135 socket_cleanup_fd_list(p);
03e334a1 136 safe_close(p->fd);
034c6ed7
LP
137 free(p->path);
138 free(p);
139 }
74051b9b
LP
140}
141
142static void socket_done(Unit *u) {
143 Socket *s = SOCKET(u);
144
145 assert(s);
146
147 socket_free_ports(s);
034c6ed7 148
613b411c 149 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
e537352b 150 exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
034c6ed7
LP
151 s->control_command = NULL;
152
5e94833f 153 socket_unwatch_control_pid(s);
034c6ed7 154
57020a3a 155 unit_ref_unset(&s->service);
034c6ed7 156
a1e58e8e
LP
157 s->tcp_congestion = mfree(s->tcp_congestion);
158 s->bind_to_device = mfree(s->bind_to_device);
acbb0225 159
0a78712e
DM
160 s->smack = mfree(s->smack);
161 s->smack_ip_in = mfree(s->smack_ip_in);
162 s->smack_ip_out = mfree(s->smack_ip_out);
0eb59ccf 163
811ba7a0
LP
164 strv_free(s->symlinks);
165
0a78712e
DM
166 s->user = mfree(s->user);
167 s->group = mfree(s->group);
3900e5fd 168
a97b23d6
DM
169 s->fdname = mfree(s->fdname);
170
718db961
LP
171 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
172}
173
36c16a7c 174static int socket_arm_timer(Socket *s, usec_t usec) {
718db961
LP
175 int r;
176
177 assert(s);
178
718db961 179 if (s->timer_event_source) {
36c16a7c 180 r = sd_event_source_set_time(s->timer_event_source, usec);
718db961
LP
181 if (r < 0)
182 return r;
183
184 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
185 }
186
36c16a7c
LP
187 if (usec == USEC_INFINITY)
188 return 0;
189
7dfbe2e3 190 r = sd_event_add_time(
6a0f1f6d
LP
191 UNIT(s)->manager->event,
192 &s->timer_event_source,
193 CLOCK_MONOTONIC,
36c16a7c 194 usec, 0,
6a0f1f6d 195 socket_dispatch_timer, s);
7dfbe2e3
TG
196 if (r < 0)
197 return r;
198
199 (void) sd_event_source_set_description(s->timer_event_source, "socket-timer");
200
201 return 0;
5cb5a6ff
LP
202}
203
8b835fcc
ZJS
204int socket_instantiate_service(Socket *s) {
205 _cleanup_free_ char *prefix = NULL, *name = NULL;
b15bdda8
LP
206 int r;
207 Unit *u;
208
209 assert(s);
210
211 /* This fills in s->service if it isn't filled in yet. For
212 * Accept=yes sockets we create the next connection service
213 * here. For Accept=no this is mostly a NOP since the service
214 * is figured out at load time anyway. */
215
7410616c 216 if (UNIT_DEREF(s->service))
b15bdda8
LP
217 return 0;
218
7410616c
LP
219 if (!s->accept)
220 return 0;
221
222 r = unit_name_to_prefix(UNIT(s)->id, &prefix);
223 if (r < 0)
224 return r;
b15bdda8 225
315db1a8 226 if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0)
b15bdda8
LP
227 return -ENOMEM;
228
1124fe6f 229 r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u);
b15bdda8
LP
230 if (r < 0)
231 return r;
232
57020a3a
LP
233 unit_ref_set(&s->service, u);
234
235 return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false);
b15bdda8
LP
236}
237
4f2d528d
LP
238static bool have_non_accept_socket(Socket *s) {
239 SocketPort *p;
240
241 assert(s);
242
243 if (!s->accept)
244 return true;
245
dd5ad9d4
LP
246 LIST_FOREACH(port, p, s->ports) {
247
248 if (p->type != SOCKET_SOCKET)
249 return true;
250
4f2d528d
LP
251 if (!socket_address_can_accept(&p->address))
252 return true;
dd5ad9d4 253 }
4f2d528d
LP
254
255 return false;
256}
257
a57f7e2c 258static int socket_add_mount_links(Socket *s) {
6e2ef85b 259 SocketPort *p;
6e2ef85b
LP
260 int r;
261
262 assert(s);
6e2ef85b 263
a57f7e2c
LP
264 LIST_FOREACH(port, p, s->ports) {
265 const char *path = NULL;
6e2ef85b 266
a57f7e2c
LP
267 if (p->type == SOCKET_SOCKET)
268 path = socket_address_get_path(&p->address);
60252446 269 else if (IN_SET(p->type, SOCKET_FIFO, SOCKET_SPECIAL, SOCKET_USB_FUNCTION))
a57f7e2c 270 path = p->path;
6e2ef85b 271
a57f7e2c
LP
272 if (!path)
273 continue;
6e2ef85b 274
a57f7e2c 275 r = unit_require_mounts_for(UNIT(s), path);
b87705cd 276 if (r < 0)
6e2ef85b 277 return r;
b87705cd 278 }
6e2ef85b
LP
279
280 return 0;
281}
282
283static int socket_add_device_link(Socket *s) {
284 char *t;
6e2ef85b
LP
285
286 assert(s);
287
7d0c710d 288 if (!s->bind_to_device || streq(s->bind_to_device, "lo"))
6e2ef85b
LP
289 return 0;
290
63c372cb 291 t = strjoina("/sys/subsystem/net/devices/", s->bind_to_device);
9d06297e 292 return unit_add_node_link(UNIT(s), t, false, UNIT_BINDS_TO);
6e2ef85b
LP
293}
294
a40eb732
LP
295static int socket_add_default_dependencies(Socket *s) {
296 int r;
297 assert(s);
298
4c9ea260
LP
299 if (!UNIT(s)->default_dependencies)
300 return 0;
301
e3d84721
LP
302 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, NULL, true);
303 if (r < 0)
304 return r;
2a77d31d 305
463d0d15 306 if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) {
e3d84721
LP
307 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
308 if (r < 0)
a40eb732 309 return r;
2a77d31d 310 }
a40eb732 311
ead8e478 312 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
a40eb732
LP
313}
314
44a6b1b6 315_pure_ static bool socket_has_exec(Socket *s) {
4cfc6dbe
LP
316 unsigned i;
317 assert(s);
318
319 for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++)
320 if (s->exec_command[i])
321 return true;
322
323 return false;
324}
325
e821075a
LP
326static int socket_add_extras(Socket *s) {
327 Unit *u = UNIT(s);
e537352b 328 int r;
44d8db9e 329
e821075a 330 assert(s);
57020a3a 331
1f15ce28
LP
332 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
333 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
334 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
335 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
336 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
337 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
338 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
339 * service starts are typical. */
340
341 if (s->trigger_limit.interval == USEC_INFINITY)
342 s->trigger_limit.interval = 2 * USEC_PER_SEC;
343
344 if (s->trigger_limit.burst == (unsigned) -1) {
345 if (s->accept)
346 s->trigger_limit.burst = 200;
347 else
348 s->trigger_limit.burst = 20;
349 }
350
e821075a 351 if (have_non_accept_socket(s)) {
23a177ef 352
e821075a
LP
353 if (!UNIT_DEREF(s->service)) {
354 Unit *x;
57020a3a 355
e821075a 356 r = unit_load_related_unit(u, ".service", &x);
57020a3a 357 if (r < 0)
4f2d528d 358 return r;
e821075a
LP
359
360 unit_ref_set(&s->service, x);
4f2d528d 361 }
44d8db9e 362
e821075a
LP
363 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true);
364 if (r < 0)
6e2ef85b 365 return r;
e821075a 366 }
6e2ef85b 367
e821075a
LP
368 r = socket_add_mount_links(s);
369 if (r < 0)
370 return r;
6e2ef85b 371
e821075a
LP
372 r = socket_add_device_link(s);
373 if (r < 0)
374 return r;
375
598459ce 376 r = unit_patch_contexts(u);
e821075a
LP
377 if (r < 0)
378 return r;
379
380 if (socket_has_exec(s)) {
381 r = unit_add_exec_dependencies(u, &s->exec_context);
382 if (r < 0)
383 return r;
23a177ef 384
d79200e2 385 r = unit_set_default_slice(u);
a016b922
LP
386 if (r < 0)
387 return r;
e821075a 388 }
a016b922 389
4c9ea260
LP
390 r = socket_add_default_dependencies(s);
391 if (r < 0)
392 return r;
e821075a
LP
393
394 return 0;
395}
396
811ba7a0
LP
397static const char *socket_find_symlink_target(Socket *s) {
398 const char *found = NULL;
399 SocketPort *p;
400
401 LIST_FOREACH(port, p, s->ports) {
402 const char *f = NULL;
403
404 switch (p->type) {
405
406 case SOCKET_FIFO:
407 f = p->path;
408 break;
409
410 case SOCKET_SOCKET:
411 if (p->address.sockaddr.un.sun_path[0] != 0)
412 f = p->address.sockaddr.un.sun_path;
413 break;
414
415 default:
416 break;
417 }
418
419 if (f) {
420 if (found)
421 return NULL;
422
423 found = f;
424 }
425 }
426
427 return found;
428}
429
e821075a
LP
430static int socket_verify(Socket *s) {
431 assert(s);
432
433 if (UNIT(s)->load_state != UNIT_LOADED)
434 return 0;
435
436 if (!s->ports) {
f2341e0a 437 log_unit_error(UNIT(s), "Unit lacks Listen setting. Refusing.");
e821075a
LP
438 return -EINVAL;
439 }
440
441 if (s->accept && have_non_accept_socket(s)) {
f2341e0a 442 log_unit_error(UNIT(s), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
e821075a
LP
443 return -EINVAL;
444 }
445
446 if (s->accept && s->max_connections <= 0) {
f2341e0a 447 log_unit_error(UNIT(s), "MaxConnection= setting too small. Refusing.");
e821075a
LP
448 return -EINVAL;
449 }
e06c73cc 450
e821075a 451 if (s->accept && UNIT_DEREF(s->service)) {
f2341e0a 452 log_unit_error(UNIT(s), "Explicit service configuration for accepting socket units not supported. Refusing.");
e821075a
LP
453 return -EINVAL;
454 }
455
456 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
f2341e0a 457 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
e821075a
LP
458 return -EINVAL;
459 }
460
811ba7a0 461 if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) {
f2341e0a 462 log_unit_error(UNIT(s), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
811ba7a0
LP
463 return -EINVAL;
464 }
465
e821075a
LP
466 return 0;
467}
468
469static int socket_load(Unit *u) {
470 Socket *s = SOCKET(u);
471 int r;
472
473 assert(u);
474 assert(u->load_state == UNIT_STUB);
475
476 r = unit_load_fragment_and_dropin(u);
477 if (r < 0)
478 return r;
479
480 if (u->load_state == UNIT_LOADED) {
481 /* This is a new unit? Then let's add in some extras */
482 r = socket_add_extras(s);
e06c73cc
LP
483 if (r < 0)
484 return r;
23a177ef
LP
485 }
486
4f2d528d 487 return socket_verify(s);
44d8db9e
LP
488}
489
44a6b1b6 490_const_ static const char* listen_lookup(int family, int type) {
7a22745a
LP
491
492 if (family == AF_NETLINK)
493 return "ListenNetlink";
542563ba
LP
494
495 if (type == SOCK_STREAM)
496 return "ListenStream";
497 else if (type == SOCK_DGRAM)
498 return "ListenDatagram";
499 else if (type == SOCK_SEQPACKET)
500 return "ListenSequentialPacket";
501
034c6ed7 502 assert_not_reached("Unknown socket type");
542563ba
LP
503 return NULL;
504}
505
87f0e418 506static void socket_dump(Unit *u, FILE *f, const char *prefix) {
209e9dcd 507 char time_string[FORMAT_TIMESPAN_MAX];
5cb5a6ff 508 SocketExecCommand c;
87f0e418 509 Socket *s = SOCKET(u);
542563ba 510 SocketPort *p;
82ba9f08 511 const char *prefix2;
5cb5a6ff
LP
512
513 assert(s);
fa068367 514 assert(f);
5cb5a6ff 515
4c940960 516 prefix = strempty(prefix);
63c372cb 517 prefix2 = strjoina(prefix, "\t");
c43d20a0 518
5cb5a6ff
LP
519 fprintf(f,
520 "%sSocket State: %s\n"
81a5c6d0 521 "%sResult: %s\n"
542563ba 522 "%sBindIPv6Only: %s\n"
b5a0699f
LP
523 "%sBacklog: %u\n"
524 "%sSocketMode: %04o\n"
4fd5948e
LP
525 "%sDirectoryMode: %04o\n"
526 "%sKeepAlive: %s\n"
4427c3f4 527 "%sNoDelay: %s\n"
cebf8b20 528 "%sFreeBind: %s\n"
6b6d2dee 529 "%sTransparent: %s\n"
ec6370a2 530 "%sBroadcast: %s\n"
ede3deb4 531 "%sPassCredentials: %s\n"
54ecda32 532 "%sPassSecurity: %s\n"
bd1fe7c7 533 "%sTCPCongestion: %s\n"
16115b0a 534 "%sRemoveOnStop: %s\n"
55301ec0 535 "%sWritable: %s\n"
8dd4c05b 536 "%sFDName: %s\n"
16115b0a 537 "%sSELinuxContextFromNet: %s\n",
a16e1123 538 prefix, socket_state_to_string(s->state),
81a5c6d0 539 prefix, socket_result_to_string(s->result),
c0120d99 540 prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only),
b5a0699f
LP
541 prefix, s->backlog,
542 prefix, s->socket_mode,
4fd5948e
LP
543 prefix, s->directory_mode,
544 prefix, yes_no(s->keep_alive),
4427c3f4 545 prefix, yes_no(s->no_delay),
cebf8b20 546 prefix, yes_no(s->free_bind),
6b6d2dee 547 prefix, yes_no(s->transparent),
ec6370a2 548 prefix, yes_no(s->broadcast),
d68af586 549 prefix, yes_no(s->pass_cred),
54ecda32 550 prefix, yes_no(s->pass_sec),
bd1fe7c7 551 prefix, strna(s->tcp_congestion),
16115b0a 552 prefix, yes_no(s->remove_on_stop),
55301ec0 553 prefix, yes_no(s->writable),
8dd4c05b 554 prefix, socket_fdname(s),
16115b0a 555 prefix, yes_no(s->selinux_context_from_net));
542563ba 556
70123e68
LP
557 if (s->control_pid > 0)
558 fprintf(f,
de0671ee
ZJS
559 "%sControl PID: "PID_FMT"\n",
560 prefix, s->control_pid);
70123e68 561
acbb0225
LP
562 if (s->bind_to_device)
563 fprintf(f,
564 "%sBindToDevice: %s\n",
565 prefix, s->bind_to_device);
566
4f2d528d
LP
567 if (s->accept)
568 fprintf(f,
6cf6bbc2
LP
569 "%sAccepted: %u\n"
570 "%sNConnections: %u\n"
571 "%sMaxConnections: %u\n",
572 prefix, s->n_accepted,
573 prefix, s->n_connections,
574 prefix, s->max_connections);
4f2d528d 575
4fd5948e
LP
576 if (s->priority >= 0)
577 fprintf(f,
578 "%sPriority: %i\n",
579 prefix, s->priority);
580
581 if (s->receive_buffer > 0)
582 fprintf(f,
583 "%sReceiveBuffer: %zu\n",
584 prefix, s->receive_buffer);
585
586 if (s->send_buffer > 0)
587 fprintf(f,
588 "%sSendBuffer: %zu\n",
589 prefix, s->send_buffer);
590
591 if (s->ip_tos >= 0)
592 fprintf(f,
593 "%sIPTOS: %i\n",
594 prefix, s->ip_tos);
595
596 if (s->ip_ttl >= 0)
597 fprintf(f,
598 "%sIPTTL: %i\n",
599 prefix, s->ip_ttl);
600
601 if (s->pipe_size > 0)
602 fprintf(f,
603 "%sPipeSize: %zu\n",
604 prefix, s->pipe_size);
605
606 if (s->mark >= 0)
607 fprintf(f,
608 "%sMark: %i\n",
609 prefix, s->mark);
610
916abb21
LP
611 if (s->mq_maxmsg > 0)
612 fprintf(f,
613 "%sMessageQueueMaxMessages: %li\n",
614 prefix, s->mq_maxmsg);
615
616 if (s->mq_msgsize > 0)
617 fprintf(f,
618 "%sMessageQueueMessageSize: %li\n",
619 prefix, s->mq_msgsize);
620
718db961 621 if (s->reuse_port)
f7db7a69
SL
622 fprintf(f,
623 "%sReusePort: %s\n",
718db961 624 prefix, yes_no(s->reuse_port));
f7db7a69 625
0eb59ccf
AK
626 if (s->smack)
627 fprintf(f,
628 "%sSmackLabel: %s\n",
629 prefix, s->smack);
630
631 if (s->smack_ip_in)
632 fprintf(f,
633 "%sSmackLabelIPIn: %s\n",
634 prefix, s->smack_ip_in);
635
636 if (s->smack_ip_out)
637 fprintf(f,
638 "%sSmackLabelIPOut: %s\n",
639 prefix, s->smack_ip_out);
640
3900e5fd
LP
641 if (!isempty(s->user) || !isempty(s->group))
642 fprintf(f,
d2a50e3b
LP
643 "%sSocketUser: %s\n"
644 "%sSocketGroup: %s\n",
3900e5fd
LP
645 prefix, strna(s->user),
646 prefix, strna(s->group));
647
3cd761e4 648 if (s->keep_alive_time > 0)
209e9dcd 649 fprintf(f,
3cd761e4
LP
650 "%sKeepAliveTimeSec: %s\n",
651 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_time, USEC_PER_SEC));
209e9dcd 652
3cd761e4 653 if (s->keep_alive_interval)
209e9dcd 654 fprintf(f,
3cd761e4
LP
655 "%sKeepAliveIntervalSec: %s\n",
656 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_interval, USEC_PER_SEC));
209e9dcd 657
3cd761e4 658 if (s->keep_alive_cnt)
209e9dcd
SS
659 fprintf(f,
660 "%sKeepAliveProbes: %u\n",
661 prefix, s->keep_alive_cnt);
662
3cd761e4 663 if (s->defer_accept)
cc567c9b 664 fprintf(f,
3cd761e4
LP
665 "%sDeferAcceptSec: %s\n",
666 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->defer_accept, USEC_PER_SEC));
cc567c9b 667
034c6ed7 668 LIST_FOREACH(port, p, s->ports) {
5cb5a6ff 669
542563ba
LP
670 if (p->type == SOCKET_SOCKET) {
671 const char *t;
672 int r;
e364ad06 673 char *k = NULL;
542563ba 674
e53fc357
LP
675 r = socket_address_print(&p->address, &k);
676 if (r < 0)
542563ba
LP
677 t = strerror(-r);
678 else
679 t = k;
680
7a22745a 681 fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
542563ba 682 free(k);
b0a3f2bc
LP
683 } else if (p->type == SOCKET_SPECIAL)
684 fprintf(f, "%sListenSpecial: %s\n", prefix, p->path);
60252446
PS
685 else if (p->type == SOCKET_USB_FUNCTION)
686 fprintf(f, "%sListenUSBFunction: %s\n", prefix, p->path);
916abb21
LP
687 else if (p->type == SOCKET_MQUEUE)
688 fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path);
b0a3f2bc 689 else
542563ba
LP
690 fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
691 }
5cb5a6ff
LP
692
693 exec_context_dump(&s->exec_context, f, prefix);
4819ff03 694 kill_context_dump(&s->kill_context, f, prefix);
5cb5a6ff 695
e537352b 696 for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) {
c43d20a0
LP
697 if (!s->exec_command[c])
698 continue;
5cb5a6ff 699
40d50879 700 fprintf(f, "%s-> %s:\n",
a16e1123 701 prefix, socket_exec_command_to_string(c));
c43d20a0
LP
702
703 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff
LP
704 }
705}
706
4f2d528d
LP
707static int instance_from_socket(int fd, unsigned nr, char **instance) {
708 socklen_t l;
709 char *r;
9d16d0c7 710 union sockaddr_union local, remote;
4f2d528d
LP
711
712 assert(fd >= 0);
713 assert(instance);
714
715 l = sizeof(local);
716 if (getsockname(fd, &local.sa, &l) < 0)
717 return -errno;
718
719 l = sizeof(remote);
720 if (getpeername(fd, &remote.sa, &l) < 0)
721 return -errno;
722
723 switch (local.sa.sa_family) {
724
725 case AF_INET: {
726 uint32_t
727 a = ntohl(local.in.sin_addr.s_addr),
728 b = ntohl(remote.in.sin_addr.s_addr);
729
730 if (asprintf(&r,
77b088c2
LP
731 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
732 nr,
4f2d528d
LP
733 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
734 ntohs(local.in.sin_port),
735 b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
736 ntohs(remote.in.sin_port)) < 0)
737 return -ENOMEM;
738
739 break;
740 }
741
742 case AF_INET6: {
c65a0b14 743 static const unsigned char ipv4_prefix[] = {
2b061f5a
LP
744 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
745 };
746
747 if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 &&
748 memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
749 const uint8_t
750 *a = local.in6.sin6_addr.s6_addr+12,
751 *b = remote.in6.sin6_addr.s6_addr+12;
752
753 if (asprintf(&r,
77b088c2
LP
754 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
755 nr,
2b061f5a
LP
756 a[0], a[1], a[2], a[3],
757 ntohs(local.in6.sin6_port),
758 b[0], b[1], b[2], b[3],
759 ntohs(remote.in6.sin6_port)) < 0)
760 return -ENOMEM;
761 } else {
762 char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN];
763
764 if (asprintf(&r,
77b088c2
LP
765 "%u-%s:%u-%s:%u",
766 nr,
2b061f5a
LP
767 inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)),
768 ntohs(local.in6.sin6_port),
769 inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)),
770 ntohs(remote.in6.sin6_port)) < 0)
771 return -ENOMEM;
772 }
4f2d528d
LP
773
774 break;
775 }
776
777 case AF_UNIX: {
778 struct ucred ucred;
eff05270 779 int k;
4f2d528d 780
eff05270 781 k = getpeercred(fd, &ucred);
d38f6e34
ZJS
782 if (k >= 0) {
783 if (asprintf(&r,
784 "%u-"PID_FMT"-"UID_FMT,
785 nr, ucred.pid, ucred.uid) < 0)
786 return -ENOMEM;
787 } else if (k == -ENODATA) {
788 /* This handles the case where somebody is
789 * connecting from another pid/uid namespace
790 * (e.g. from outside of our container). */
791 if (asprintf(&r,
792 "%u-unknown",
793 nr) < 0)
794 return -ENOMEM;
795 } else
eff05270 796 return k;
2f20a8eb 797
2f20a8eb 798 break;
4f2d528d
LP
799 }
800
801 default:
802 assert_not_reached("Unhandled socket type.");
803 }
804
805 *instance = r;
806 return 0;
807}
808
034c6ed7 809static void socket_close_fds(Socket *s) {
83c60c9f 810 SocketPort *p;
811ba7a0 811 char **i;
83c60c9f
LP
812
813 assert(s);
814
034c6ed7 815 LIST_FOREACH(port, p, s->ports) {
18e854f8 816 bool was_open;
718db961 817
18e854f8 818 was_open = p->fd >= 0;
83c60c9f 819
18e854f8 820 p->event_source = sd_event_source_unref(p->event_source);
03e334a1 821 p->fd = safe_close(p->fd);
15087cdb 822 socket_cleanup_fd_list(p);
a16e1123 823
18e854f8
LP
824 /* One little note: we should normally not delete any sockets in the file system here! After all some
825 * other process we spawned might still have a reference of this fd and wants to continue to use
826 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
827 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
828 * anyway, but only then. */
bd1fe7c7 829
18e854f8
LP
830 if (!was_open || !s->remove_on_stop)
831 continue;
832
833 switch (p->type) {
bd1fe7c7 834
18e854f8
LP
835 case SOCKET_FIFO:
836 (void) unlink(p->path);
837 break;
bd1fe7c7 838
18e854f8
LP
839 case SOCKET_MQUEUE:
840 (void) mq_unlink(p->path);
841 break;
bd1fe7c7 842
18e854f8
LP
843 case SOCKET_SOCKET:
844 (void) socket_address_unlink(&p->address);
845 break;
bd1fe7c7 846
18e854f8
LP
847 default:
848 break;
bd1fe7c7 849 }
83c60c9f 850 }
811ba7a0
LP
851
852 if (s->remove_on_stop)
853 STRV_FOREACH(i, s->symlinks)
18e854f8 854 (void) unlink(*i);
83c60c9f
LP
855}
856
4fd5948e 857static void socket_apply_socket_options(Socket *s, int fd) {
d53e386d
LP
858 int r;
859
4fd5948e
LP
860 assert(s);
861 assert(fd >= 0);
862
863 if (s->keep_alive) {
864 int b = s->keep_alive;
865 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) < 0)
f2341e0a 866 log_unit_warning_errno(UNIT(s), errno, "SO_KEEPALIVE failed: %m");
4fd5948e
LP
867 }
868
209e9dcd
SS
869 if (s->keep_alive_time) {
870 int value = s->keep_alive_time / USEC_PER_SEC;
871 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &value, sizeof(value)) < 0)
f2341e0a 872 log_unit_warning_errno(UNIT(s), errno, "TCP_KEEPIDLE failed: %m");
209e9dcd
SS
873 }
874
875 if (s->keep_alive_interval) {
920b52e4 876 int value = s->keep_alive_interval / USEC_PER_SEC;
209e9dcd 877 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &value, sizeof(value)) < 0)
f2341e0a 878 log_unit_warning_errno(UNIT(s), errno, "TCP_KEEPINTVL failed: %m");
209e9dcd
SS
879 }
880
881 if (s->keep_alive_cnt) {
882 int value = s->keep_alive_cnt;
172cfe87 883 if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &value, sizeof(value)) < 0)
f2341e0a 884 log_unit_warning_errno(UNIT(s), errno, "TCP_KEEPCNT failed: %m");
209e9dcd
SS
885 }
886
cc567c9b
SS
887 if (s->defer_accept) {
888 int value = s->defer_accept / USEC_PER_SEC;
889 if (setsockopt(fd, SOL_TCP, TCP_DEFER_ACCEPT, &value, sizeof(value)) < 0)
f2341e0a 890 log_unit_warning_errno(UNIT(s), errno, "TCP_DEFER_ACCEPT failed: %m");
cc567c9b
SS
891 }
892
4427c3f4
SS
893 if (s->no_delay) {
894 int b = s->no_delay;
62bc4efc
SS
895
896 if (s->socket_protocol == IPPROTO_SCTP) {
897 if (setsockopt(fd, SOL_SCTP, SCTP_NODELAY, &b, sizeof(b)) < 0)
898 log_unit_warning_errno(UNIT(s), errno, "SCTP_NODELAY failed: %m");
899 } else {
900 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &b, sizeof(b)) < 0)
901 log_unit_warning_errno(UNIT(s), errno, "TCP_NODELAY failed: %m");
902 }
4427c3f4
SS
903 }
904
ec6370a2
LP
905 if (s->broadcast) {
906 int one = 1;
907 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0)
f2341e0a 908 log_unit_warning_errno(UNIT(s), errno, "SO_BROADCAST failed: %m");
ec6370a2
LP
909 }
910
d68af586
MS
911 if (s->pass_cred) {
912 int one = 1;
913 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
f2341e0a 914 log_unit_warning_errno(UNIT(s), errno, "SO_PASSCRED failed: %m");
d68af586
MS
915 }
916
54ecda32
LP
917 if (s->pass_sec) {
918 int one = 1;
919 if (setsockopt(fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one)) < 0)
f2341e0a 920 log_unit_warning_errno(UNIT(s), errno, "SO_PASSSEC failed: %m");
54ecda32
LP
921 }
922
4fd5948e
LP
923 if (s->priority >= 0)
924 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0)
f2341e0a 925 log_unit_warning_errno(UNIT(s), errno, "SO_PRIORITY failed: %m");
4fd5948e
LP
926
927 if (s->receive_buffer > 0) {
928 int value = (int) s->receive_buffer;
7d9eaa84
LP
929
930 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
931
b8cef44e 932 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0)
7d9eaa84 933 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0)
f2341e0a 934 log_unit_warning_errno(UNIT(s), errno, "SO_RCVBUF failed: %m");
4fd5948e
LP
935 }
936
937 if (s->send_buffer > 0) {
938 int value = (int) s->send_buffer;
b8cef44e 939 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0)
7d9eaa84 940 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0)
f2341e0a 941 log_unit_warning_errno(UNIT(s), errno, "SO_SNDBUF failed: %m");
4fd5948e
LP
942 }
943
944 if (s->mark >= 0)
945 if (setsockopt(fd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)) < 0)
f2341e0a 946 log_unit_warning_errno(UNIT(s), errno, "SO_MARK failed: %m");
4fd5948e
LP
947
948 if (s->ip_tos >= 0)
949 if (setsockopt(fd, IPPROTO_IP, IP_TOS, &s->ip_tos, sizeof(s->ip_tos)) < 0)
f2341e0a 950 log_unit_warning_errno(UNIT(s), errno, "IP_TOS failed: %m");
4fd5948e 951
46925ac5 952 if (s->ip_ttl >= 0) {
d53e386d 953 int x;
46925ac5
LP
954
955 r = setsockopt(fd, IPPROTO_IP, IP_TTL, &s->ip_ttl, sizeof(s->ip_ttl));
5bfcc1c6
FF
956
957 if (socket_ipv6_is_supported())
958 x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl));
959 else {
960 x = -1;
961 errno = EAFNOSUPPORT;
962 }
46925ac5
LP
963
964 if (r < 0 && x < 0)
f2341e0a 965 log_unit_warning_errno(UNIT(s), errno, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
46925ac5 966 }
cebf8b20
TT
967
968 if (s->tcp_congestion)
969 if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0)
f2341e0a 970 log_unit_warning_errno(UNIT(s), errno, "TCP_CONGESTION failed: %m");
0eb59ccf 971
d53e386d 972 if (s->smack_ip_in) {
5ab58c20 973 r = mac_smack_apply_fd(fd, SMACK_ATTR_IPIN, s->smack_ip_in);
d53e386d 974 if (r < 0)
f2341e0a 975 log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_in_fd: %m");
d53e386d 976 }
9a4e038c 977
d53e386d 978 if (s->smack_ip_out) {
5ab58c20 979 r = mac_smack_apply_fd(fd, SMACK_ATTR_IPOUT, s->smack_ip_out);
d53e386d 980 if (r < 0)
f2341e0a 981 log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_out_fd: %m");
d53e386d 982 }
4fd5948e
LP
983}
984
b15bdda8 985static void socket_apply_fifo_options(Socket *s, int fd) {
d53e386d
LP
986 int r;
987
4fd5948e
LP
988 assert(s);
989 assert(fd >= 0);
990
991 if (s->pipe_size > 0)
992 if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0)
ed460b07 993 log_unit_warning_errno(UNIT(s), errno, "Setting pipe size failed, ignoring: %m");
0eb59ccf 994
d53e386d 995 if (s->smack) {
5ab58c20 996 r = mac_smack_apply_fd(fd, SMACK_ATTR_ACCESS, s->smack);
d53e386d 997 if (r < 0)
ed460b07 998 log_unit_error_errno(UNIT(s), r, "SMACK relabelling failed, ignoring: %m");
d53e386d 999 }
4fd5948e
LP
1000}
1001
b15bdda8
LP
1002static int fifo_address_create(
1003 const char *path,
1004 mode_t directory_mode,
e8da24a6 1005 mode_t socket_mode) {
b15bdda8 1006
e8da24a6 1007 _cleanup_close_ int fd = -1;
b15bdda8 1008 mode_t old_mask;
e8da24a6
LP
1009 struct stat st;
1010 int r;
b15bdda8
LP
1011
1012 assert(path);
b15bdda8 1013
d2e54fae 1014 mkdir_parents_label(path, directory_mode);
b15bdda8 1015
ecabcf8b 1016 r = mac_selinux_create_file_prepare(path, S_IFIFO);
e9a5ef7c 1017 if (r < 0)
e8da24a6 1018 return r;
b15bdda8
LP
1019
1020 /* Enforce the right access mode for the fifo */
1021 old_mask = umask(~ socket_mode);
1022
1023 /* Include the original umask in our mask */
e8da24a6 1024 (void) umask(~socket_mode | old_mask);
b15bdda8
LP
1025
1026 r = mkfifo(path, socket_mode);
e8da24a6 1027 (void) umask(old_mask);
b15bdda8 1028
94bc2731 1029 if (r < 0 && errno != EEXIST) {
b15bdda8
LP
1030 r = -errno;
1031 goto fail;
1032 }
1033
e8da24a6 1034 fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
3cc2aff1 1035 if (fd < 0) {
b15bdda8
LP
1036 r = -errno;
1037 goto fail;
1038 }
1039
ecabcf8b 1040 mac_selinux_create_file_clear();
7a58bfa4 1041
b15bdda8
LP
1042 if (fstat(fd, &st) < 0) {
1043 r = -errno;
1044 goto fail;
1045 }
1046
1047 if (!S_ISFIFO(st.st_mode) ||
de0200fc 1048 (st.st_mode & 0777) != (socket_mode & ~old_mask) ||
e4f44e73
DR
1049 st.st_uid != getuid() ||
1050 st.st_gid != getgid()) {
b15bdda8
LP
1051 r = -EEXIST;
1052 goto fail;
1053 }
1054
e8da24a6
LP
1055 r = fd;
1056 fd = -1;
1057
1058 return r;
b15bdda8
LP
1059
1060fail:
ecabcf8b 1061 mac_selinux_create_file_clear();
b15bdda8
LP
1062 return r;
1063}
1064
55301ec0 1065static int special_address_create(const char *path, bool writable) {
e8da24a6 1066 _cleanup_close_ int fd = -1;
b0a3f2bc 1067 struct stat st;
e8da24a6 1068 int r;
b0a3f2bc
LP
1069
1070 assert(path);
b0a3f2bc 1071
55301ec0 1072 fd = open(path, (writable ? O_RDWR : O_RDONLY)|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
e8da24a6
LP
1073 if (fd < 0)
1074 return -errno;
b0a3f2bc 1075
e8da24a6
LP
1076 if (fstat(fd, &st) < 0)
1077 return -errno;
b0a3f2bc
LP
1078
1079 /* Check whether this is a /proc, /sys or /dev file or char device */
e8da24a6
LP
1080 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode))
1081 return -EEXIST;
b0a3f2bc 1082
e8da24a6
LP
1083 r = fd;
1084 fd = -1;
b0a3f2bc
LP
1085
1086 return r;
1087}
1088
36078102 1089static int usbffs_address_create(const char *path) {
60252446
PS
1090 _cleanup_close_ int fd = -1;
1091 struct stat st;
e8da24a6 1092 int r;
60252446
PS
1093
1094 assert(path);
60252446
PS
1095
1096 fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
1097 if (fd < 0)
1098 return -errno;
1099
1100 if (fstat(fd, &st) < 0)
1101 return -errno;
1102
1103 /* Check whether this is a regular file (ffs endpoint)*/
1104 if (!S_ISREG(st.st_mode))
1105 return -EEXIST;
1106
e8da24a6 1107 r = fd;
60252446
PS
1108 fd = -1;
1109
e8da24a6 1110 return r;
60252446
PS
1111}
1112
916abb21
LP
1113static int mq_address_create(
1114 const char *path,
1115 mode_t mq_mode,
1116 long maxmsg,
e8da24a6 1117 long msgsize) {
916abb21 1118
e8da24a6 1119 _cleanup_close_ int fd = -1;
916abb21
LP
1120 struct stat st;
1121 mode_t old_mask;
1122 struct mq_attr _attr, *attr = NULL;
e8da24a6 1123 int r;
916abb21
LP
1124
1125 assert(path);
916abb21
LP
1126
1127 if (maxmsg > 0 && msgsize > 0) {
e8da24a6
LP
1128 _attr = (struct mq_attr) {
1129 .mq_flags = O_NONBLOCK,
1130 .mq_maxmsg = maxmsg,
1131 .mq_msgsize = msgsize,
1132 };
916abb21
LP
1133 attr = &_attr;
1134 }
1135
1136 /* Enforce the right access mode for the mq */
1137 old_mask = umask(~ mq_mode);
1138
1139 /* Include the original umask in our mask */
e8da24a6 1140 (void) umask(~mq_mode | old_mask);
916abb21 1141 fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr);
e8da24a6 1142 (void) umask(old_mask);
916abb21 1143
e8da24a6
LP
1144 if (fd < 0)
1145 return -errno;
916abb21 1146
e8da24a6
LP
1147 if (fstat(fd, &st) < 0)
1148 return -errno;
916abb21
LP
1149
1150 if ((st.st_mode & 0777) != (mq_mode & ~old_mask) ||
1151 st.st_uid != getuid() ||
e8da24a6
LP
1152 st.st_gid != getgid())
1153 return -EEXIST;
916abb21 1154
e8da24a6
LP
1155 r = fd;
1156 fd = -1;
916abb21 1157
916abb21
LP
1158 return r;
1159}
1160
811ba7a0
LP
1161static int socket_symlink(Socket *s) {
1162 const char *p;
1163 char **i;
1164
1165 assert(s);
1166
1167 p = socket_find_symlink_target(s);
1168 if (!p)
1169 return 0;
1170
1171 STRV_FOREACH(i, s->symlinks)
43b133b4 1172 symlink_label(p, *i);
811ba7a0
LP
1173
1174 return 0;
1175}
1176
36078102 1177static int usbffs_write_descs(int fd, Service *s) {
6b7e5923
PS
1178 int r;
1179
1180 if (!s->usb_function_descriptors || !s->usb_function_strings)
1181 return -EINVAL;
1182
1183 r = copy_file_fd(s->usb_function_descriptors, fd, false);
1184 if (r < 0)
e8da24a6 1185 return r;
6b7e5923 1186
e8da24a6 1187 return copy_file_fd(s->usb_function_strings, fd, false);
6b7e5923
PS
1188}
1189
36078102 1190static int usbffs_select_ep(const struct dirent *d) {
60252446
PS
1191 return d->d_name[0] != '.' && !streq(d->d_name, "ep0");
1192}
1193
36078102 1194static int usbffs_dispatch_eps(SocketPort *p) {
60252446 1195 _cleanup_free_ struct dirent **ent = NULL;
60252446 1196 _cleanup_free_ char *path = NULL;
36078102 1197 int r, i, n, k;
60252446 1198
5f311f8c
LP
1199 path = dirname_malloc(p->path);
1200 if (!path)
1201 return -ENOMEM;
60252446 1202
36078102 1203 r = scandir(path, &ent, usbffs_select_ep, alphasort);
60252446
PS
1204 if (r < 0)
1205 return -errno;
1206
1207 n = r;
1208 p->auxiliary_fds = new(int, n);
1209 if (!p->auxiliary_fds)
1210 return -ENOMEM;
1211
1212 p->n_auxiliary_fds = n;
1213
1214 k = 0;
1215 for (i = 0; i < n; ++i) {
1216 _cleanup_free_ char *ep = NULL;
1217
1218 ep = path_make_absolute(ent[i]->d_name, path);
1219 if (!ep)
1220 return -ENOMEM;
1221
1222 path_kill_slashes(ep);
1223
36078102 1224 r = usbffs_address_create(ep);
60252446
PS
1225 if (r < 0)
1226 goto fail;
1227
e8da24a6
LP
1228 p->auxiliary_fds[k] = r;
1229
60252446
PS
1230 ++k;
1231 free(ent[i]);
1232 }
1233
1234 return r;
1235
1236fail:
e8da24a6 1237 close_many(p->auxiliary_fds, k);
60252446
PS
1238 p->auxiliary_fds = mfree(p->auxiliary_fds);
1239 p->n_auxiliary_fds = 0;
1240
1241 return r;
1242}
1243
034c6ed7 1244static int socket_open_fds(Socket *s) {
710a6b50
LP
1245 _cleanup_(mac_selinux_freep) char *label = NULL;
1246 bool know_label = false;
83c60c9f
LP
1247 SocketPort *p;
1248 int r;
1249
1250 assert(s);
1251
034c6ed7 1252 LIST_FOREACH(port, p, s->ports) {
83c60c9f 1253
034c6ed7
LP
1254 if (p->fd >= 0)
1255 continue;
83c60c9f 1256
00411a13
LP
1257 switch (p->type) {
1258
1259 case SOCKET_SOCKET:
049f8642 1260
7f416dae
LP
1261 if (!know_label) {
1262 /* Figure out label, if we don't it know
1263 * yet. We do it once, for the first
1264 * socket where we need this and
1265 * remember it for the rest. */
1266
1267 if (s->selinux_context_from_net) {
1268 /* Get it from the network label */
1269
1270 r = mac_selinux_get_our_label(&label);
75514a70 1271 if (r < 0 && r != -EOPNOTSUPP)
7f416dae 1272 goto rollback;
049f8642 1273
7f416dae
LP
1274 } else {
1275 /* Get it from the executable we are about to start */
1276
1277 r = socket_instantiate_service(s);
1278 if (r < 0)
1279 goto rollback;
1280
1281 if (UNIT_ISSET(s->service) &&
1282 SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]) {
1283 r = mac_selinux_get_create_label_from_exe(SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]->path, &label);
75514a70 1284 if (r < 0 && r != -EPERM && r != -EOPNOTSUPP)
7f416dae
LP
1285 goto rollback;
1286 }
189583d7 1287 }
049f8642
LP
1288
1289 know_label = true;
1290 }
1291
74bb646e
SS
1292 /* Apply the socket protocol */
1293 switch(p->address.type) {
d2a50e3b 1294
74bb646e
SS
1295 case SOCK_STREAM:
1296 case SOCK_SEQPACKET:
1297 if (p->socket->socket_protocol == IPPROTO_SCTP)
1298 p->address.protocol = p->socket->socket_protocol;
1299 break;
d2a50e3b 1300
74bb646e
SS
1301 case SOCK_DGRAM:
1302 if (p->socket->socket_protocol == IPPROTO_UDPLITE)
1303 p->address.protocol = p->socket->socket_protocol;
1304 break;
1305 }
1306
175a3d25
LP
1307 r = socket_address_listen(
1308 &p->address,
1309 SOCK_CLOEXEC|SOCK_NONBLOCK,
1310 s->backlog,
1311 s->bind_ipv6_only,
1312 s->bind_to_device,
54255c64 1313 s->reuse_port,
175a3d25
LP
1314 s->free_bind,
1315 s->transparent,
1316 s->directory_mode,
1317 s->socket_mode,
1318 label);
1319 if (r < 0)
83c60c9f
LP
1320 goto rollback;
1321
175a3d25 1322 p->fd = r;
4fd5948e 1323 socket_apply_socket_options(s, p->fd);
811ba7a0 1324 socket_symlink(s);
00411a13 1325 break;
4fd5948e 1326
00411a13 1327 case SOCKET_SPECIAL:
b0a3f2bc 1328
55301ec0 1329 p->fd = special_address_create(p->path, s->writable);
e8da24a6
LP
1330 if (p->fd < 0) {
1331 r = p->fd;
b0a3f2bc 1332 goto rollback;
e8da24a6 1333 }
00411a13 1334 break;
b0a3f2bc 1335
00411a13 1336 case SOCKET_FIFO:
83c60c9f 1337
e8da24a6 1338 p->fd = fifo_address_create(
175a3d25
LP
1339 p->path,
1340 s->directory_mode,
e8da24a6
LP
1341 s->socket_mode);
1342 if (p->fd < 0) {
1343 r = p->fd;
83c60c9f 1344 goto rollback;
e8da24a6 1345 }
83c60c9f 1346
b15bdda8 1347 socket_apply_fifo_options(s, p->fd);
811ba7a0 1348 socket_symlink(s);
00411a13 1349 break;
811ba7a0 1350
00411a13 1351 case SOCKET_MQUEUE:
83c60c9f 1352
e8da24a6 1353 p->fd = mq_address_create(
175a3d25
LP
1354 p->path,
1355 s->socket_mode,
1356 s->mq_maxmsg,
e8da24a6
LP
1357 s->mq_msgsize);
1358 if (p->fd < 0) {
1359 r = p->fd;
916abb21 1360 goto rollback;
e8da24a6 1361 }
00411a13 1362 break;
e8da24a6 1363
d2a50e3b 1364 case SOCKET_USB_FUNCTION: {
27a6ea91 1365 _cleanup_free_ char *ep = NULL;
60252446 1366
27a6ea91
GB
1367 ep = path_make_absolute("ep0", p->path);
1368
1369 p->fd = usbffs_address_create(ep);
e8da24a6
LP
1370 if (p->fd < 0) {
1371 r = p->fd;
60252446 1372 goto rollback;
e8da24a6 1373 }
60252446 1374
36078102 1375 r = usbffs_write_descs(p->fd, SERVICE(UNIT_DEREF(s->service)));
6b7e5923
PS
1376 if (r < 0)
1377 goto rollback;
1378
36078102 1379 r = usbffs_dispatch_eps(p);
60252446
PS
1380 if (r < 0)
1381 goto rollback;
00411a13
LP
1382
1383 break;
27a6ea91 1384 }
00411a13 1385 default:
b15bdda8 1386 assert_not_reached("Unknown port type");
00411a13 1387 }
034c6ed7
LP
1388 }
1389
1390 return 0;
1391
1392rollback:
1393 socket_close_fds(s);
1394 return r;
1395}
1396
1397static void socket_unwatch_fds(Socket *s) {
1398 SocketPort *p;
718db961 1399 int r;
9152c765 1400
034c6ed7
LP
1401 assert(s);
1402
1403 LIST_FOREACH(port, p, s->ports) {
1404 if (p->fd < 0)
1405 continue;
1406
a4152e3f
LP
1407 if (!p->event_source)
1408 continue;
1409
1410 r = sd_event_source_set_enabled(p->event_source, SD_EVENT_OFF);
1411 if (r < 0)
f2341e0a 1412 log_unit_debug_errno(UNIT(s), r, "Failed to disable event source: %m");
83c60c9f 1413 }
034c6ed7
LP
1414}
1415
1416static int socket_watch_fds(Socket *s) {
1417 SocketPort *p;
1418 int r;
1419
1420 assert(s);
83c60c9f 1421
034c6ed7
LP
1422 LIST_FOREACH(port, p, s->ports) {
1423 if (p->fd < 0)
1424 continue;
1425
cbf60d0a 1426 if (p->event_source) {
718db961 1427 r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON);
cbf60d0a
LP
1428 if (r < 0)
1429 goto fail;
1430 } else {
151b9b96 1431 r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p);
cbf60d0a
LP
1432 if (r < 0)
1433 goto fail;
4f2d528d 1434
cbf60d0a 1435 (void) sd_event_source_set_description(p->event_source, "socket-port-io");
718db961 1436 }
034c6ed7 1437 }
83c60c9f 1438
542563ba 1439 return 0;
83c60c9f 1440
034c6ed7 1441fail:
cbf60d0a 1442 log_unit_warning_errno(UNIT(s), r, "Failed to watch listening fds: %m");
034c6ed7
LP
1443 socket_unwatch_fds(s);
1444 return r;
1445}
1446
1447static void socket_set_state(Socket *s, SocketState state) {
1448 SocketState old_state;
1449 assert(s);
1450
1451 old_state = s->state;
1452 s->state = state;
1453
bd1fe7c7
LP
1454 if (!IN_SET(state,
1455 SOCKET_START_PRE,
3900e5fd 1456 SOCKET_START_CHOWN,
bd1fe7c7
LP
1457 SOCKET_START_POST,
1458 SOCKET_STOP_PRE,
1459 SOCKET_STOP_PRE_SIGTERM,
1460 SOCKET_STOP_PRE_SIGKILL,
1461 SOCKET_STOP_POST,
1462 SOCKET_FINAL_SIGTERM,
1463 SOCKET_FINAL_SIGKILL)) {
718db961
LP
1464
1465 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
5e94833f 1466 socket_unwatch_control_pid(s);
034c6ed7 1467 s->control_command = NULL;
a16e1123 1468 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
e537352b 1469 }
034c6ed7 1470
a16e1123
LP
1471 if (state != SOCKET_LISTENING)
1472 socket_unwatch_fds(s);
1473
bd1fe7c7 1474 if (!IN_SET(state,
3900e5fd 1475 SOCKET_START_CHOWN,
bd1fe7c7
LP
1476 SOCKET_START_POST,
1477 SOCKET_LISTENING,
1478 SOCKET_RUNNING,
1479 SOCKET_STOP_PRE,
1480 SOCKET_STOP_PRE_SIGTERM,
1481 SOCKET_STOP_PRE_SIGKILL))
034c6ed7
LP
1482 socket_close_fds(s);
1483
e537352b 1484 if (state != old_state)
f2341e0a 1485 log_unit_debug(UNIT(s), "Changed %s -> %s", socket_state_to_string(old_state), socket_state_to_string(state));
acbb0225 1486
e2f3b44c 1487 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
034c6ed7
LP
1488}
1489
be847e82 1490static int socket_coldplug(Unit *u) {
a16e1123
LP
1491 Socket *s = SOCKET(u);
1492 int r;
1493
1494 assert(s);
1495 assert(s->state == SOCKET_DEAD);
1496
e821075a
LP
1497 if (s->deserialized_state == s->state)
1498 return 0;
a16e1123 1499
c386f588
LP
1500 if (s->control_pid > 0 &&
1501 pid_is_unwaited(s->control_pid) &&
1502 IN_SET(s->deserialized_state,
3900e5fd
LP
1503 SOCKET_START_PRE,
1504 SOCKET_START_CHOWN,
1505 SOCKET_START_POST,
1506 SOCKET_STOP_PRE,
1507 SOCKET_STOP_PRE_SIGTERM,
1508 SOCKET_STOP_PRE_SIGKILL,
1509 SOCKET_STOP_POST,
1510 SOCKET_FINAL_SIGTERM,
1511 SOCKET_FINAL_SIGKILL)) {
a16e1123 1512
e821075a
LP
1513 r = unit_watch_pid(UNIT(s), s->control_pid);
1514 if (r < 0)
1515 return r;
a16e1123 1516
36c16a7c 1517 r = socket_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
e821075a
LP
1518 if (r < 0)
1519 return r;
1520 }
a16e1123 1521
3900e5fd
LP
1522 if (IN_SET(s->deserialized_state,
1523 SOCKET_START_CHOWN,
1524 SOCKET_START_POST,
1525 SOCKET_LISTENING,
1526 SOCKET_RUNNING,
1527 SOCKET_STOP_PRE,
1528 SOCKET_STOP_PRE_SIGTERM,
1529 SOCKET_STOP_PRE_SIGKILL)) {
36c16a7c 1530
e821075a
LP
1531 r = socket_open_fds(s);
1532 if (r < 0)
1533 return r;
1534 }
a16e1123 1535
e821075a
LP
1536 if (s->deserialized_state == SOCKET_LISTENING) {
1537 r = socket_watch_fds(s);
1538 if (r < 0)
1539 return r;
a16e1123
LP
1540 }
1541
e821075a 1542 socket_set_state(s, s->deserialized_state);
a16e1123
LP
1543 return 0;
1544}
1545
e537352b 1546static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
3900e5fd 1547 _cleanup_free_ char **argv = NULL;
034c6ed7
LP
1548 pid_t pid;
1549 int r;
9fa95f85
DM
1550 ExecParameters exec_params = {
1551 .apply_permissions = true,
1552 .apply_chroot = true,
1553 .apply_tty_stdin = true,
a34ceba6
LP
1554 .stdin_fd = -1,
1555 .stdout_fd = -1,
1556 .stderr_fd = -1,
9fa95f85 1557 };
034c6ed7
LP
1558
1559 assert(s);
1560 assert(c);
1561 assert(_pid);
1562
5ad096b3
LP
1563 (void) unit_realize_cgroup(UNIT(s));
1564 if (s->reset_cpu_usage) {
1565 (void) unit_reset_cpu_usage(UNIT(s));
1566 s->reset_cpu_usage = false;
1567 }
4ad49000 1568
613b411c
LP
1569 r = unit_setup_exec_runtime(UNIT(s));
1570 if (r < 0)
36c16a7c 1571 return r;
613b411c 1572
36c16a7c 1573 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
36697dc0 1574 if (r < 0)
36c16a7c 1575 return r;
034c6ed7 1576
19f6d710
LP
1577 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1578 if (r < 0)
36c16a7c 1579 return r;
9e2f7c11 1580
9fa95f85
DM
1581 exec_params.argv = argv;
1582 exec_params.environment = UNIT(s)->manager->environment;
1583 exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
1584 exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
1585 exec_params.cgroup_path = UNIT(s)->cgroup_path;
a931ad47 1586 exec_params.cgroup_delegate = s->cgroup_context.delegate;
9fa95f85 1587 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
9fa95f85 1588
f2341e0a
LP
1589 r = exec_spawn(UNIT(s),
1590 c,
9e2f7c11 1591 &s->exec_context,
9fa95f85 1592 &exec_params,
613b411c 1593 s->exec_runtime,
9e2f7c11 1594 &pid);
cee288ad 1595 if (r < 0)
36c16a7c 1596 return r;
9e2f7c11 1597
3900e5fd 1598 r = unit_watch_pid(UNIT(s), pid);
9e2f7c11 1599 if (r < 0)
3900e5fd 1600 /* FIXME: we need to do something here */
36c16a7c 1601 return r;
034c6ed7 1602
3900e5fd
LP
1603 *_pid = pid;
1604 return 0;
3900e5fd
LP
1605}
1606
1607static int socket_chown(Socket *s, pid_t *_pid) {
1608 pid_t pid;
1609 int r;
1610
36c16a7c 1611 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
3900e5fd
LP
1612 if (r < 0)
1613 goto fail;
1614
1615 /* We have to resolve the user names out-of-process, hence
1616 * let's fork here. It's messy, but well, what can we do? */
1617
1618 pid = fork();
1619 if (pid < 0)
1620 return -errno;
1621
1622 if (pid == 0) {
1623 SocketPort *p;
fed1e721
LP
1624 uid_t uid = UID_INVALID;
1625 gid_t gid = GID_INVALID;
3900e5fd
LP
1626 int ret;
1627
ce30c8dc
LP
1628 (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
1629 (void) ignore_signals(SIGPIPE, -1);
3900e5fd
LP
1630 log_forget_fds();
1631
1632 if (!isempty(s->user)) {
1633 const char *user = s->user;
1634
1635 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
1636 if (r < 0) {
1637 ret = EXIT_USER;
1638 goto fail_child;
1639 }
1640 }
1641
1642 if (!isempty(s->group)) {
1643 const char *group = s->group;
1644
1645 r = get_group_creds(&group, &gid);
1646 if (r < 0) {
1647 ret = EXIT_GROUP;
1648 goto fail_child;
1649 }
1650 }
1651
1652 LIST_FOREACH(port, p, s->ports) {
e5a1c18d 1653 const char *path = NULL;
3900e5fd
LP
1654
1655 if (p->type == SOCKET_SOCKET)
1656 path = socket_address_get_path(&p->address);
1657 else if (p->type == SOCKET_FIFO)
1658 path = p->path;
1659
1660 if (!path)
1661 continue;
1662
1663 if (chown(path, uid, gid) < 0) {
1664 r = -errno;
1665 ret = EXIT_CHOWN;
1666 goto fail_child;
1667 }
1668 }
1669
1670 _exit(0);
1671
1672 fail_child:
1673 log_open();
da927ba9 1674 log_error_errno(r, "Failed to chown socket at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
3900e5fd
LP
1675
1676 _exit(ret);
1677 }
1678
718db961
LP
1679 r = unit_watch_pid(UNIT(s), pid);
1680 if (r < 0)
034c6ed7 1681 goto fail;
83c60c9f 1682
034c6ed7 1683 *_pid = pid;
034c6ed7
LP
1684 return 0;
1685
1686fail:
718db961 1687 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
83c60c9f 1688 return r;
542563ba
LP
1689}
1690
cfc4eb4c 1691static void socket_enter_dead(Socket *s, SocketResult f) {
034c6ed7
LP
1692 assert(s);
1693
cfc4eb4c
LP
1694 if (f != SOCKET_SUCCESS)
1695 s->result = f;
034c6ed7 1696
613b411c
LP
1697 exec_runtime_destroy(s->exec_runtime);
1698 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1699
e66cf1a3
LP
1700 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1701
cfc4eb4c 1702 socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD);
034c6ed7
LP
1703}
1704
cfc4eb4c 1705static void socket_enter_signal(Socket *s, SocketState state, SocketResult f);
80876c20 1706
cfc4eb4c 1707static void socket_enter_stop_post(Socket *s, SocketResult f) {
034c6ed7
LP
1708 int r;
1709 assert(s);
1710
cfc4eb4c
LP
1711 if (f != SOCKET_SUCCESS)
1712 s->result = f;
034c6ed7 1713
5e94833f 1714 socket_unwatch_control_pid(s);
a16e1123 1715 s->control_command_id = SOCKET_EXEC_STOP_POST;
3900e5fd 1716 s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST];
a16e1123 1717
3900e5fd
LP
1718 if (s->control_command) {
1719 r = socket_spawn(s, s->control_command, &s->control_pid);
1720 if (r < 0)
034c6ed7
LP
1721 goto fail;
1722
80876c20
LP
1723 socket_set_state(s, SOCKET_STOP_POST);
1724 } else
cfc4eb4c 1725 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS);
034c6ed7
LP
1726
1727 return;
1728
1729fail:
f2341e0a 1730 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m");
cfc4eb4c 1731 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
1732}
1733
cfc4eb4c 1734static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
034c6ed7
LP
1735 int r;
1736
1737 assert(s);
1738
cfc4eb4c
LP
1739 if (f != SOCKET_SUCCESS)
1740 s->result = f;
034c6ed7 1741
cd2086fe
LP
1742 r = unit_kill_context(
1743 UNIT(s),
1744 &s->kill_context,
db2cb23b
UTL
1745 (state != SOCKET_STOP_PRE_SIGTERM && state != SOCKET_FINAL_SIGTERM) ?
1746 KILL_KILL : KILL_TERMINATE,
cd2086fe
LP
1747 -1,
1748 s->control_pid,
1749 false);
1750 if (r < 0)
1751 goto fail;
034c6ed7 1752
cd2086fe 1753 if (r > 0) {
36c16a7c 1754 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
36697dc0 1755 if (r < 0)
80876c20 1756 goto fail;
d6ea93e3 1757
80876c20 1758 socket_set_state(s, state);
ac84d1fb
LP
1759 } else if (state == SOCKET_STOP_PRE_SIGTERM)
1760 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS);
1761 else if (state == SOCKET_STOP_PRE_SIGKILL)
cfc4eb4c 1762 socket_enter_stop_post(s, SOCKET_SUCCESS);
ac84d1fb
LP
1763 else if (state == SOCKET_FINAL_SIGTERM)
1764 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS);
80876c20 1765 else
cfc4eb4c 1766 socket_enter_dead(s, SOCKET_SUCCESS);
034c6ed7
LP
1767
1768 return;
1769
1770fail:
f2341e0a 1771 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
034c6ed7
LP
1772
1773 if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
cfc4eb4c 1774 socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
034c6ed7 1775 else
cfc4eb4c 1776 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
1777}
1778
cfc4eb4c 1779static void socket_enter_stop_pre(Socket *s, SocketResult f) {
034c6ed7
LP
1780 int r;
1781 assert(s);
1782
cfc4eb4c
LP
1783 if (f != SOCKET_SUCCESS)
1784 s->result = f;
034c6ed7 1785
5e94833f 1786 socket_unwatch_control_pid(s);
a16e1123 1787 s->control_command_id = SOCKET_EXEC_STOP_PRE;
3900e5fd 1788 s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE];
a16e1123 1789
3900e5fd
LP
1790 if (s->control_command) {
1791 r = socket_spawn(s, s->control_command, &s->control_pid);
1792 if (r < 0)
034c6ed7
LP
1793 goto fail;
1794
80876c20
LP
1795 socket_set_state(s, SOCKET_STOP_PRE);
1796 } else
cfc4eb4c 1797 socket_enter_stop_post(s, SOCKET_SUCCESS);
034c6ed7
LP
1798
1799 return;
1800
1801fail:
f2341e0a 1802 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-pre' task: %m");
cfc4eb4c 1803 socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
1804}
1805
e9af15c3
LP
1806static void socket_enter_listening(Socket *s) {
1807 int r;
1808 assert(s);
1809
cfc4eb4c
LP
1810 r = socket_watch_fds(s);
1811 if (r < 0) {
f2341e0a 1812 log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m");
e9af15c3
LP
1813 goto fail;
1814 }
1815
1816 socket_set_state(s, SOCKET_LISTENING);
1817 return;
1818
1819fail:
cfc4eb4c 1820 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
e9af15c3
LP
1821}
1822
034c6ed7
LP
1823static void socket_enter_start_post(Socket *s) {
1824 int r;
1825 assert(s);
1826
3900e5fd
LP
1827 socket_unwatch_control_pid(s);
1828 s->control_command_id = SOCKET_EXEC_START_POST;
1829 s->control_command = s->exec_command[SOCKET_EXEC_START_POST];
1830
1831 if (s->control_command) {
1832 r = socket_spawn(s, s->control_command, &s->control_pid);
1833 if (r < 0) {
f2341e0a 1834 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m");
3900e5fd
LP
1835 goto fail;
1836 }
1837
1838 socket_set_state(s, SOCKET_START_POST);
1839 } else
1840 socket_enter_listening(s);
1841
1842 return;
1843
1844fail:
1845 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
1846}
1847
1848static void socket_enter_start_chown(Socket *s) {
1849 int r;
1850
1851 assert(s);
1852
cfc4eb4c
LP
1853 r = socket_open_fds(s);
1854 if (r < 0) {
f2341e0a 1855 log_unit_warning_errno(UNIT(s), r, "Failed to listen on sockets: %m");
034c6ed7
LP
1856 goto fail;
1857 }
1858
3900e5fd 1859 if (!isempty(s->user) || !isempty(s->group)) {
5e94833f 1860
3900e5fd
LP
1861 socket_unwatch_control_pid(s);
1862 s->control_command_id = SOCKET_EXEC_START_CHOWN;
1863 s->control_command = NULL;
a16e1123 1864
3900e5fd 1865 r = socket_chown(s, &s->control_pid);
cfc4eb4c 1866 if (r < 0) {
f2341e0a 1867 log_unit_warning_errno(UNIT(s), r, "Failed to fork 'start-chown' task: %m");
034c6ed7
LP
1868 goto fail;
1869 }
1870
3900e5fd 1871 socket_set_state(s, SOCKET_START_CHOWN);
80876c20 1872 } else
3900e5fd 1873 socket_enter_start_post(s);
034c6ed7
LP
1874
1875 return;
1876
1877fail:
cfc4eb4c 1878 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
1879}
1880
1881static void socket_enter_start_pre(Socket *s) {
1882 int r;
1883 assert(s);
1884
5e94833f 1885 socket_unwatch_control_pid(s);
a16e1123 1886 s->control_command_id = SOCKET_EXEC_START_PRE;
3900e5fd 1887 s->control_command = s->exec_command[SOCKET_EXEC_START_PRE];
a16e1123 1888
3900e5fd 1889 if (s->control_command) {
e821075a 1890 r = socket_spawn(s, s->control_command, &s->control_pid);
3900e5fd 1891 if (r < 0) {
f2341e0a 1892 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m");
034c6ed7 1893 goto fail;
3900e5fd 1894 }
034c6ed7 1895
80876c20
LP
1896 socket_set_state(s, SOCKET_START_PRE);
1897 } else
3900e5fd 1898 socket_enter_start_chown(s);
034c6ed7
LP
1899
1900 return;
1901
1902fail:
cfc4eb4c 1903 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
1904}
1905
4f2d528d 1906static void socket_enter_running(Socket *s, int cfd) {
4afd3348 1907 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
034c6ed7
LP
1908 int r;
1909
8b26cdbd
LP
1910 /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or
1911 * close it. */
1912
034c6ed7
LP
1913 assert(s);
1914
ba3e67a7
LP
1915 /* We don't take connections anymore if we are supposed to
1916 * shut down anyway */
31afa0a4 1917 if (unit_stop_pending(UNIT(s))) {
e821075a 1918
f2341e0a 1919 log_unit_debug(UNIT(s), "Suppressing connection request since unit stop is scheduled.");
5d909e3e 1920
7c610628 1921 if (cfd >= 0)
8b26cdbd 1922 cfd = safe_close(cfd);
7c610628
LP
1923 else {
1924 /* Flush all sockets by closing and reopening them */
1925 socket_close_fds(s);
1926
16ac4014
LP
1927 r = socket_open_fds(s);
1928 if (r < 0) {
f2341e0a 1929 log_unit_warning_errno(UNIT(s), r, "Failed to listen on sockets: %m");
16ac4014
LP
1930 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
1931 return;
1932 }
1933
1a710b43
MS
1934 r = socket_watch_fds(s);
1935 if (r < 0) {
f2341e0a 1936 log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m");
cfc4eb4c 1937 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
7c610628
LP
1938 }
1939 }
1940
ba3e67a7
LP
1941 return;
1942 }
1943
8b26cdbd
LP
1944 if (!ratelimit_test(&s->trigger_limit)) {
1945 safe_close(cfd);
1946 log_unit_warning(UNIT(s), "Trigger limit hit, refusing further activation.");
1947 socket_enter_stop_pre(s, SOCKET_FAILURE_TRIGGER_LIMIT_HIT);
1948 return;
1949 }
1950
4f2d528d 1951 if (cfd < 0) {
57020a3a 1952 Iterator i;
e821075a 1953 Unit *other;
f976f3f6 1954 bool pending = false;
f976f3f6
LP
1955
1956 /* If there's already a start pending don't bother to
1957 * do anything */
e821075a
LP
1958 SET_FOREACH(other, UNIT(s)->dependencies[UNIT_TRIGGERS], i)
1959 if (unit_active_or_pending(other)) {
57020a3a
LP
1960 pending = true;
1961 break;
1962 }
f976f3f6 1963
1a710b43 1964 if (!pending) {
640ace4a 1965 if (!UNIT_ISSET(s->service)) {
f2341e0a 1966 log_unit_error(UNIT(s), "Service to activate vanished, refusing activation.");
640ace4a
LP
1967 r = -ENOENT;
1968 goto fail;
1969 }
1970
4bd29fe5 1971 r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, &error, NULL);
1a710b43 1972 if (r < 0)
f976f3f6 1973 goto fail;
1a710b43 1974 }
4f2d528d
LP
1975
1976 socket_set_state(s, SOCKET_RUNNING);
1977 } else {
e55001eb 1978 _cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL;
b15bdda8 1979 Service *service;
4f2d528d 1980
6cf6bbc2 1981 if (s->n_connections >= s->max_connections) {
8b26cdbd 1982 log_unit_warning(UNIT(s), "Too many incoming connections (%u), refusing connection attempt.", s->n_connections);
03e334a1 1983 safe_close(cfd);
6cf6bbc2
LP
1984 return;
1985 }
1986
1a710b43
MS
1987 r = socket_instantiate_service(s);
1988 if (r < 0)
b15bdda8
LP
1989 goto fail;
1990
1a710b43
MS
1991 r = instance_from_socket(cfd, s->n_accepted, &instance);
1992 if (r < 0) {
1993 if (r != -ENOTCONN)
1994 goto fail;
1995
1996 /* ENOTCONN is legitimate if TCP RST was received.
1997 * This connection is over, but the socket unit lives on. */
8b26cdbd 1998 log_unit_debug(UNIT(s), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
03e334a1 1999 safe_close(cfd);
1a710b43
MS
2000 return;
2001 }
4f2d528d 2002
7410616c
LP
2003 r = unit_name_to_prefix(UNIT(s)->id, &prefix);
2004 if (r < 0)
4f2d528d 2005 goto fail;
4f2d528d 2006
7410616c
LP
2007 r = unit_name_build(prefix, instance, ".service", &name);
2008 if (r < 0)
b6dbbe1c 2009 goto fail;
4f2d528d 2010
1a710b43 2011 r = unit_add_name(UNIT_DEREF(s->service), name);
e55001eb 2012 if (r < 0)
4f2d528d 2013 goto fail;
b15bdda8 2014
57020a3a
LP
2015 service = SERVICE(UNIT_DEREF(s->service));
2016 unit_ref_unset(&s->service);
6c073082 2017
e4f67317 2018 s->n_accepted++;
b15bdda8 2019 unit_choose_id(UNIT(service), name);
b15bdda8 2020
16115b0a 2021 r = service_set_socket_fd(service, cfd, s, s->selinux_context_from_net);
1a710b43 2022 if (r < 0)
4f2d528d
LP
2023 goto fail;
2024
8b26cdbd 2025 cfd = -1; /* We passed ownership of the fd to the service now. Forget it here. */
313cefa1 2026 s->n_connections++;
6cf6bbc2 2027
4bd29fe5 2028 r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, &error, NULL);
3e7a1f50
LP
2029 if (r < 0) {
2030 /* We failed to activate the new service, but it still exists. Let's make sure the service
2031 * closes and forgets the connection fd again, immediately. */
2032 service_close_socket_fd(service);
4f2d528d 2033 goto fail;
3e7a1f50 2034 }
c4e2ceae
LP
2035
2036 /* Notify clients about changed counters */
2037 unit_add_to_dbus_queue(UNIT(s));
4f2d528d 2038 }
034c6ed7 2039
034c6ed7
LP
2040 return;
2041
2042fail:
f2341e0a
LP
2043 log_unit_warning(UNIT(s), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2044 cfd >= 0 ? "template" : "non-template",
718db961 2045 bus_error_message(&error, r));
e821075a 2046
60089004 2047 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
03e334a1 2048 safe_close(cfd);
034c6ed7
LP
2049}
2050
cfc4eb4c 2051static void socket_run_next(Socket *s) {
034c6ed7
LP
2052 int r;
2053
2054 assert(s);
2055 assert(s->control_command);
2056 assert(s->control_command->command_next);
2057
5e94833f
LP
2058 socket_unwatch_control_pid(s);
2059
034c6ed7
LP
2060 s->control_command = s->control_command->command_next;
2061
e821075a
LP
2062 r = socket_spawn(s, s->control_command, &s->control_pid);
2063 if (r < 0)
034c6ed7
LP
2064 goto fail;
2065
2066 return;
2067
2068fail:
f2341e0a 2069 log_unit_warning_errno(UNIT(s), r, "Failed to run next task: %m");
80876c20
LP
2070
2071 if (s->state == SOCKET_START_POST)
cfc4eb4c 2072 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
034c6ed7 2073 else if (s->state == SOCKET_STOP_POST)
cfc4eb4c 2074 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
034c6ed7 2075 else
cfc4eb4c 2076 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
2077}
2078
87f0e418
LP
2079static int socket_start(Unit *u) {
2080 Socket *s = SOCKET(u);
07299350 2081 int r;
83c60c9f
LP
2082
2083 assert(s);
2084
034c6ed7
LP
2085 /* We cannot fulfill this request right now, try again later
2086 * please! */
3900e5fd
LP
2087 if (IN_SET(s->state,
2088 SOCKET_STOP_PRE,
2089 SOCKET_STOP_PRE_SIGKILL,
2090 SOCKET_STOP_PRE_SIGTERM,
2091 SOCKET_STOP_POST,
2092 SOCKET_FINAL_SIGTERM,
2093 SOCKET_FINAL_SIGKILL))
034c6ed7
LP
2094 return -EAGAIN;
2095
a4152e3f 2096 /* Already on it! */
3900e5fd
LP
2097 if (IN_SET(s->state,
2098 SOCKET_START_PRE,
2099 SOCKET_START_CHOWN,
2100 SOCKET_START_POST))
034c6ed7 2101 return 0;
83c60c9f 2102
034c6ed7 2103 /* Cannot run this without the service being around */
9444b1f2 2104 if (UNIT_ISSET(s->service)) {
57020a3a
LP
2105 Service *service;
2106
2107 service = SERVICE(UNIT_DEREF(s->service));
2108
1124fe6f 2109 if (UNIT(service)->load_state != UNIT_LOADED) {
f2341e0a 2110 log_unit_error(u, "Socket service %s not loaded, refusing.", UNIT(service)->id);
4f2d528d 2111 return -ENOENT;
4ac9236f 2112 }
4f2d528d 2113
35b8ca3a 2114 /* If the service is already active we cannot start the
4f2d528d 2115 * socket */
57020a3a
LP
2116 if (service->state != SERVICE_DEAD &&
2117 service->state != SERVICE_FAILED &&
2118 service->state != SERVICE_AUTO_RESTART) {
f2341e0a 2119 log_unit_error(u, "Socket service %s already active, refusing.", UNIT(service)->id);
4f2d528d 2120 return -EBUSY;
4ac9236f 2121 }
4f2d528d 2122 }
e537352b 2123
fdf20a31 2124 assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED);
83c60c9f 2125
07299350
LP
2126 r = unit_start_limit_test(u);
2127 if (r < 0) {
2128 socket_enter_dead(s, SOCKET_FAILURE_START_LIMIT_HIT);
2129 return r;
2130 }
2131
cfc4eb4c 2132 s->result = SOCKET_SUCCESS;
5ad096b3
LP
2133 s->reset_cpu_usage = true;
2134
034c6ed7 2135 socket_enter_start_pre(s);
e821075a 2136
82a2b6bb 2137 return 1;
034c6ed7 2138}
83c60c9f 2139
87f0e418
LP
2140static int socket_stop(Unit *u) {
2141 Socket *s = SOCKET(u);
034c6ed7
LP
2142
2143 assert(s);
2144
e537352b 2145 /* Already on it */
3900e5fd
LP
2146 if (IN_SET(s->state,
2147 SOCKET_STOP_PRE,
2148 SOCKET_STOP_PRE_SIGTERM,
2149 SOCKET_STOP_PRE_SIGKILL,
2150 SOCKET_STOP_POST,
2151 SOCKET_FINAL_SIGTERM,
2152 SOCKET_FINAL_SIGKILL))
e537352b
LP
2153 return 0;
2154
3f6c78dc
LP
2155 /* If there's already something running we go directly into
2156 * kill mode. */
3900e5fd
LP
2157 if (IN_SET(s->state,
2158 SOCKET_START_PRE,
2159 SOCKET_START_CHOWN,
2160 SOCKET_START_POST)) {
cfc4eb4c 2161 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS);
3f6c78dc
LP
2162 return -EAGAIN;
2163 }
2164
034c6ed7 2165 assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
83c60c9f 2166
cfc4eb4c 2167 socket_enter_stop_pre(s, SOCKET_SUCCESS);
82a2b6bb 2168 return 1;
542563ba
LP
2169}
2170
a16e1123
LP
2171static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
2172 Socket *s = SOCKET(u);
2173 SocketPort *p;
2174 int r;
2175
2176 assert(u);
2177 assert(f);
2178 assert(fds);
2179
2180 unit_serialize_item(u, f, "state", socket_state_to_string(s->state));
cfc4eb4c 2181 unit_serialize_item(u, f, "result", socket_result_to_string(s->result));
a16e1123
LP
2182 unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted);
2183
2184 if (s->control_pid > 0)
de0671ee 2185 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
a16e1123
LP
2186
2187 if (s->control_command_id >= 0)
2188 unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id));
2189
2190 LIST_FOREACH(port, p, s->ports) {
2191 int copy;
2192
2193 if (p->fd < 0)
2194 continue;
2195
613b411c
LP
2196 copy = fdset_put_dup(fds, p->fd);
2197 if (copy < 0)
a16e1123
LP
2198 return copy;
2199
2200 if (p->type == SOCKET_SOCKET) {
613b411c 2201 _cleanup_free_ char *t = NULL;
a16e1123 2202
ee092817
LP
2203 r = socket_address_print(&p->address, &t);
2204 if (r < 0)
a16e1123
LP
2205 return r;
2206
7a22745a
LP
2207 if (socket_address_family(&p->address) == AF_NETLINK)
2208 unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t);
2209 else
2210 unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
613b411c 2211
b0a3f2bc
LP
2212 } else if (p->type == SOCKET_SPECIAL)
2213 unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path);
ee092817
LP
2214 else if (p->type == SOCKET_MQUEUE)
2215 unit_serialize_item_format(u, f, "mqueue", "%i %s", copy, p->path);
60252446
PS
2216 else if (p->type == SOCKET_USB_FUNCTION)
2217 unit_serialize_item_format(u, f, "ffs", "%i %s", copy, p->path);
b0a3f2bc 2218 else {
a16e1123
LP
2219 assert(p->type == SOCKET_FIFO);
2220 unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path);
2221 }
2222 }
2223
2224 return 0;
2225}
2226
2227static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2228 Socket *s = SOCKET(u);
a16e1123
LP
2229
2230 assert(u);
2231 assert(key);
2232 assert(value);
a16e1123
LP
2233
2234 if (streq(key, "state")) {
2235 SocketState state;
2236
ee092817
LP
2237 state = socket_state_from_string(value);
2238 if (state < 0)
f2341e0a 2239 log_unit_debug(u, "Failed to parse state value: %s", value);
a16e1123
LP
2240 else
2241 s->deserialized_state = state;
cfc4eb4c
LP
2242 } else if (streq(key, "result")) {
2243 SocketResult f;
a16e1123 2244
cfc4eb4c
LP
2245 f = socket_result_from_string(value);
2246 if (f < 0)
f2341e0a 2247 log_unit_debug(u, "Failed to parse result value: %s", value);
cfc4eb4c
LP
2248 else if (f != SOCKET_SUCCESS)
2249 s->result = f;
a16e1123
LP
2250
2251 } else if (streq(key, "n-accepted")) {
2252 unsigned k;
2253
e364ad06 2254 if (safe_atou(value, &k) < 0)
f2341e0a 2255 log_unit_debug(u, "Failed to parse n-accepted value: %s", value);
a16e1123
LP
2256 else
2257 s->n_accepted += k;
2258 } else if (streq(key, "control-pid")) {
5925dd3c 2259 pid_t pid;
a16e1123 2260
e364ad06 2261 if (parse_pid(value, &pid) < 0)
f2341e0a 2262 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
a16e1123 2263 else
5925dd3c 2264 s->control_pid = pid;
a16e1123
LP
2265 } else if (streq(key, "control-command")) {
2266 SocketExecCommand id;
2267
66870f90
ZJS
2268 id = socket_exec_command_from_string(value);
2269 if (id < 0)
f2341e0a 2270 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
a16e1123
LP
2271 else {
2272 s->control_command_id = id;
2273 s->control_command = s->exec_command[id];
2274 }
2275 } else if (streq(key, "fifo")) {
2276 int fd, skip = 0;
2277 SocketPort *p;
2278
2279 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2280 log_unit_debug(u, "Failed to parse fifo value: %s", value);
a16e1123
LP
2281 else {
2282
2283 LIST_FOREACH(port, p, s->ports)
b0a3f2bc 2284 if (p->type == SOCKET_FIFO &&
c78e47a6 2285 path_equal_or_files_same(p->path, value+skip))
b0a3f2bc
LP
2286 break;
2287
2288 if (p) {
03e334a1 2289 safe_close(p->fd);
b0a3f2bc
LP
2290 p->fd = fdset_remove(fds, fd);
2291 }
2292 }
2293
2294 } else if (streq(key, "special")) {
2295 int fd, skip = 0;
2296 SocketPort *p;
2297
2298 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2299 log_unit_debug(u, "Failed to parse special value: %s", value);
b0a3f2bc
LP
2300 else {
2301
2302 LIST_FOREACH(port, p, s->ports)
2303 if (p->type == SOCKET_SPECIAL &&
c78e47a6 2304 path_equal_or_files_same(p->path, value+skip))
a16e1123
LP
2305 break;
2306
2307 if (p) {
03e334a1 2308 safe_close(p->fd);
a16e1123
LP
2309 p->fd = fdset_remove(fds, fd);
2310 }
2311 }
2312
ee092817
LP
2313 } else if (streq(key, "mqueue")) {
2314 int fd, skip = 0;
2315 SocketPort *p;
2316
2317 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2318 log_unit_debug(u, "Failed to parse mqueue value: %s", value);
ee092817
LP
2319 else {
2320
2321 LIST_FOREACH(port, p, s->ports)
2322 if (p->type == SOCKET_MQUEUE &&
c78e47a6 2323 streq(p->path, value+skip))
ee092817
LP
2324 break;
2325
2326 if (p) {
03e334a1 2327 safe_close(p->fd);
ee092817
LP
2328 p->fd = fdset_remove(fds, fd);
2329 }
2330 }
2331
a16e1123 2332 } else if (streq(key, "socket")) {
27ca8d7a 2333 int fd, type, skip = 0;
a16e1123
LP
2334 SocketPort *p;
2335
27ca8d7a 2336 if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
f2341e0a 2337 log_unit_debug(u, "Failed to parse socket value: %s", value);
a16e1123
LP
2338 else {
2339
2340 LIST_FOREACH(port, p, s->ports)
27ca8d7a 2341 if (socket_address_is(&p->address, value+skip, type))
a16e1123
LP
2342 break;
2343
2344 if (p) {
03e334a1 2345 safe_close(p->fd);
a16e1123
LP
2346 p->fd = fdset_remove(fds, fd);
2347 }
2348 }
2349
7a22745a
LP
2350 } else if (streq(key, "netlink")) {
2351 int fd, skip = 0;
2352 SocketPort *p;
2353
2354 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2355 log_unit_debug(u, "Failed to parse socket value: %s", value);
7a22745a
LP
2356 else {
2357
2358 LIST_FOREACH(port, p, s->ports)
2359 if (socket_address_is_netlink(&p->address, value+skip))
2360 break;
2361
2362 if (p) {
03e334a1 2363 safe_close(p->fd);
7a22745a
LP
2364 p->fd = fdset_remove(fds, fd);
2365 }
2366 }
60252446
PS
2367
2368 } else if (streq(key, "ffs")) {
2369 int fd, skip = 0;
2370 SocketPort *p;
2371
2372 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
2373 log_unit_debug(u, "Failed to parse ffs value: %s", value);
2374 else {
2375
2376 LIST_FOREACH(port, p, s->ports)
2377 if (p->type == SOCKET_USB_FUNCTION &&
2378 path_equal_or_files_same(p->path, value+skip))
2379 break;
2380
2381 if (p) {
2382 safe_close(p->fd);
2383 p->fd = fdset_remove(fds, fd);
2384 }
2385 }
2386
a16e1123 2387 } else
f2341e0a 2388 log_unit_debug(UNIT(s), "Unknown serialization key: %s", key);
a16e1123
LP
2389
2390 return 0;
2391}
2392
9ff1a6f1 2393static void socket_distribute_fds(Unit *u, FDSet *fds) {
01e10de3
LP
2394 Socket *s = SOCKET(u);
2395 SocketPort *p;
2396
2397 assert(u);
2398
2399 LIST_FOREACH(port, p, s->ports) {
2400 Iterator i;
2401 int fd;
2402
2403 if (p->type != SOCKET_SOCKET)
2404 continue;
2405
2406 if (p->fd >= 0)
2407 continue;
2408
2409 FDSET_FOREACH(fd, fds, i) {
2410 if (socket_address_matches_fd(&p->address, fd)) {
2411 p->fd = fdset_remove(fds, fd);
2412 s->deserialized_state = SOCKET_LISTENING;
2413 break;
2414 }
2415 }
2416 }
01e10de3
LP
2417}
2418
44a6b1b6 2419_pure_ static UnitActiveState socket_active_state(Unit *u) {
87f0e418 2420 assert(u);
5cb5a6ff 2421
acbb0225 2422 return state_translation_table[SOCKET(u)->state];
5cb5a6ff
LP
2423}
2424
44a6b1b6 2425_pure_ static const char *socket_sub_state_to_string(Unit *u) {
10a94420
LP
2426 assert(u);
2427
a16e1123 2428 return socket_state_to_string(SOCKET(u)->state);
10a94420
LP
2429}
2430
67419600
OS
2431const char* socket_port_type_to_string(SocketPort *p) {
2432
2433 assert(p);
2434
2435 switch (p->type) {
718db961
LP
2436
2437 case SOCKET_SOCKET:
2438
2439 switch (p->address.type) {
2440
2441 case SOCK_STREAM:
2442 return "Stream";
2443
2444 case SOCK_DGRAM:
2445 return "Datagram";
2446
2447 case SOCK_SEQPACKET:
2448 return "SequentialPacket";
2449
2450 case SOCK_RAW:
2451 if (socket_address_family(&p->address) == AF_NETLINK)
2452 return "Netlink";
2453
2454 default:
2455 return NULL;
2456 }
2457
2458 case SOCKET_SPECIAL:
2459 return "Special";
2460
2461 case SOCKET_MQUEUE:
2462 return "MessageQueue";
2463
2464 case SOCKET_FIFO:
2465 return "FIFO";
2466
60252446
PS
2467 case SOCKET_USB_FUNCTION:
2468 return "USBFunction";
2469
718db961
LP
2470 default:
2471 return NULL;
67419600
OS
2472 }
2473}
2474
44a6b1b6 2475_pure_ static bool socket_check_gc(Unit *u) {
6cf6bbc2
LP
2476 Socket *s = SOCKET(u);
2477
2478 assert(u);
2479
2480 return s->n_connections > 0;
2481}
2482
718db961
LP
2483static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2484 SocketPort *p = userdata;
4f2d528d 2485 int cfd = -1;
9152c765 2486
718db961 2487 assert(p);
8d567588 2488 assert(fd >= 0);
9152c765 2489
718db961
LP
2490 if (p->socket->state != SOCKET_LISTENING)
2491 return 0;
871d7de4 2492
f2341e0a 2493 log_unit_debug(UNIT(p->socket), "Incoming traffic");
9152c765 2494
718db961 2495 if (revents != EPOLLIN) {
641e01dc 2496
718db961 2497 if (revents & EPOLLHUP)
f2341e0a 2498 log_unit_error(UNIT(p->socket), "Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.");
641e01dc 2499 else
f2341e0a 2500 log_unit_error(UNIT(p->socket), "Got unexpected poll event (0x%x) on socket.", revents);
8d567588 2501 goto fail;
4f2d528d
LP
2502 }
2503
718db961
LP
2504 if (p->socket->accept &&
2505 p->type == SOCKET_SOCKET &&
2506 socket_address_can_accept(&p->address)) {
2507
4f2d528d
LP
2508 for (;;) {
2509
b14eda96
LP
2510 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK);
2511 if (cfd < 0) {
4f2d528d
LP
2512
2513 if (errno == EINTR)
2514 continue;
2515
f2341e0a 2516 log_unit_error_errno(UNIT(p->socket), errno, "Failed to accept socket: %m");
8d567588 2517 goto fail;
4f2d528d
LP
2518 }
2519
2520 break;
2521 }
4fd5948e 2522
718db961 2523 socket_apply_socket_options(p->socket, cfd);
4f2d528d 2524 }
9152c765 2525
718db961
LP
2526 socket_enter_running(p->socket, cfd);
2527 return 0;
8d567588
LP
2528
2529fail:
718db961
LP
2530 socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES);
2531 return 0;
9152c765
LP
2532}
2533
87f0e418
LP
2534static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2535 Socket *s = SOCKET(u);
cfc4eb4c 2536 SocketResult f;
5cb5a6ff
LP
2537
2538 assert(s);
034c6ed7 2539 assert(pid >= 0);
5cb5a6ff 2540
8c47c732
LP
2541 if (pid != s->control_pid)
2542 return;
542563ba 2543
034c6ed7
LP
2544 s->control_pid = 0;
2545
96342de6 2546 if (is_clean_exit(code, status, NULL))
cfc4eb4c
LP
2547 f = SOCKET_SUCCESS;
2548 else if (code == CLD_EXITED)
2549 f = SOCKET_FAILURE_EXIT_CODE;
2550 else if (code == CLD_KILLED)
2551 f = SOCKET_FAILURE_SIGNAL;
2552 else if (code == CLD_DUMPED)
2553 f = SOCKET_FAILURE_CORE_DUMP;
2554 else
a4152e3f 2555 assert_not_reached("Unknown sigchld code");
8c47c732 2556
b708e7ce 2557 if (s->control_command) {
6ea832a2 2558 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
a16e1123 2559
b708e7ce 2560 if (s->control_command->ignore)
cfc4eb4c 2561 f = SOCKET_SUCCESS;
b708e7ce
LP
2562 }
2563
f2341e0a
LP
2564 log_unit_full(u, f == SOCKET_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
2565 "Control process exited, code=%s status=%i",
2566 sigchld_code_to_string(code), status);
034c6ed7 2567
cfc4eb4c
LP
2568 if (f != SOCKET_SUCCESS)
2569 s->result = f;
2570
2571 if (s->control_command &&
2572 s->control_command->command_next &&
2573 f == SOCKET_SUCCESS) {
2574
f2341e0a 2575 log_unit_debug(u, "Running next command for state %s", socket_state_to_string(s->state));
cfc4eb4c 2576 socket_run_next(s);
acbb0225 2577 } else {
a16e1123
LP
2578 s->control_command = NULL;
2579 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
2580
034c6ed7
LP
2581 /* No further commands for this step, so let's figure
2582 * out what to do next */
5cb5a6ff 2583
f2341e0a 2584 log_unit_debug(u, "Got final SIGCHLD for state %s", socket_state_to_string(s->state));
acbb0225 2585
034c6ed7
LP
2586 switch (s->state) {
2587
2588 case SOCKET_START_PRE:
cfc4eb4c 2589 if (f == SOCKET_SUCCESS)
3900e5fd 2590 socket_enter_start_chown(s);
034c6ed7 2591 else
cfc4eb4c 2592 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f);
034c6ed7
LP
2593 break;
2594
3900e5fd
LP
2595 case SOCKET_START_CHOWN:
2596 if (f == SOCKET_SUCCESS)
2597 socket_enter_start_post(s);
2598 else
2599 socket_enter_stop_pre(s, f);
2600 break;
2601
034c6ed7 2602 case SOCKET_START_POST:
cfc4eb4c 2603 if (f == SOCKET_SUCCESS)
e9af15c3 2604 socket_enter_listening(s);
034c6ed7 2605 else
cfc4eb4c 2606 socket_enter_stop_pre(s, f);
034c6ed7
LP
2607 break;
2608
2609 case SOCKET_STOP_PRE:
2610 case SOCKET_STOP_PRE_SIGTERM:
2611 case SOCKET_STOP_PRE_SIGKILL:
cfc4eb4c 2612 socket_enter_stop_post(s, f);
034c6ed7
LP
2613 break;
2614
2615 case SOCKET_STOP_POST:
80876c20
LP
2616 case SOCKET_FINAL_SIGTERM:
2617 case SOCKET_FINAL_SIGKILL:
cfc4eb4c 2618 socket_enter_dead(s, f);
034c6ed7
LP
2619 break;
2620
2621 default:
2622 assert_not_reached("Uh, control process died at wrong time.");
2623 }
2624 }
c4e2ceae
LP
2625
2626 /* Notify clients about changed exit status */
2627 unit_add_to_dbus_queue(u);
034c6ed7 2628}
5cb5a6ff 2629
718db961
LP
2630static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2631 Socket *s = SOCKET(userdata);
5cb5a6ff 2632
034c6ed7 2633 assert(s);
718db961 2634 assert(s->timer_event_source == source);
034c6ed7
LP
2635
2636 switch (s->state) {
2637
2638 case SOCKET_START_PRE:
f2341e0a 2639 log_unit_warning(UNIT(s), "Starting timed out. Terminating.");
cfc4eb4c 2640 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
da19d5c1 2641 break;
80876c20 2642
3900e5fd 2643 case SOCKET_START_CHOWN:
034c6ed7 2644 case SOCKET_START_POST:
f2341e0a 2645 log_unit_warning(UNIT(s), "Starting timed out. Stopping.");
cfc4eb4c 2646 socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
2647 break;
2648
2649 case SOCKET_STOP_PRE:
f2341e0a 2650 log_unit_warning(UNIT(s), "Stopping timed out. Terminating.");
cfc4eb4c 2651 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
2652 break;
2653
2654 case SOCKET_STOP_PRE_SIGTERM:
4819ff03 2655 if (s->kill_context.send_sigkill) {
f2341e0a 2656 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
cfc4eb4c 2657 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT);
ba035df2 2658 } else {
f2341e0a 2659 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL. Ignoring.");
cfc4eb4c 2660 socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
ba035df2 2661 }
034c6ed7
LP
2662 break;
2663
2664 case SOCKET_STOP_PRE_SIGKILL:
f2341e0a 2665 log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring.");
cfc4eb4c 2666 socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
2667 break;
2668
2669 case SOCKET_STOP_POST:
f2341e0a 2670 log_unit_warning(UNIT(s), "Stopping timed out (2). Terminating.");
cfc4eb4c 2671 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
2672 break;
2673
80876c20 2674 case SOCKET_FINAL_SIGTERM:
4819ff03 2675 if (s->kill_context.send_sigkill) {
f2341e0a 2676 log_unit_warning(UNIT(s), "Stopping timed out (2). Killing.");
cfc4eb4c 2677 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT);
ba035df2 2678 } else {
f2341e0a 2679 log_unit_warning(UNIT(s), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
cfc4eb4c 2680 socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
ba035df2 2681 }
034c6ed7
LP
2682 break;
2683
80876c20 2684 case SOCKET_FINAL_SIGKILL:
f2341e0a 2685 log_unit_warning(UNIT(s), "Still around after SIGKILL (2). Entering failed mode.");
cfc4eb4c 2686 socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
2687 break;
2688
2689 default:
2690 assert_not_reached("Timeout at wrong time.");
2691 }
718db961
LP
2692
2693 return 0;
5cb5a6ff
LP
2694}
2695
79c7626d
LP
2696int socket_collect_fds(Socket *s, int **fds) {
2697 int *rfds, k = 0, n = 0;
44d8db9e
LP
2698 SocketPort *p;
2699
2700 assert(s);
2701 assert(fds);
44d8db9e
LP
2702
2703 /* Called from the service code for requesting our fds */
2704
15087cdb 2705 LIST_FOREACH(port, p, s->ports) {
44d8db9e 2706 if (p->fd >= 0)
79c7626d
LP
2707 n++;
2708 n += p->n_auxiliary_fds;
15087cdb 2709 }
44d8db9e 2710
79c7626d 2711 if (n <= 0) {
de3756ab 2712 *fds = NULL;
de3756ab
LP
2713 return 0;
2714 }
2715
79c7626d 2716 rfds = new(int, n);
e5403f09 2717 if (!rfds)
44d8db9e
LP
2718 return -ENOMEM;
2719
15087cdb 2720 LIST_FOREACH(port, p, s->ports) {
79c7626d
LP
2721 int i;
2722
44d8db9e
LP
2723 if (p->fd >= 0)
2724 rfds[k++] = p->fd;
15087cdb
PS
2725 for (i = 0; i < p->n_auxiliary_fds; ++i)
2726 rfds[k++] = p->auxiliary_fds[i];
2727 }
44d8db9e 2728
79c7626d 2729 assert(k == n);
44d8db9e
LP
2730
2731 *fds = rfds;
79c7626d 2732 return n;
44d8db9e
LP
2733}
2734
e821075a
LP
2735static void socket_reset_failed(Unit *u) {
2736 Socket *s = SOCKET(u);
2737
2738 assert(s);
2739
2740 if (s->state == SOCKET_FAILED)
2741 socket_set_state(s, SOCKET_DEAD);
2742
2743 s->result = SOCKET_SUCCESS;
2744}
2745
6cf6bbc2
LP
2746void socket_connection_unref(Socket *s) {
2747 assert(s);
2748
2749 /* The service is dead. Yay!
2750 *
35b8ca3a 2751 * This is strictly for one-instance-per-connection
6cf6bbc2
LP
2752 * services. */
2753
2754 assert(s->n_connections > 0);
2755 s->n_connections--;
2756
f2341e0a 2757 log_unit_debug(UNIT(s), "One connection closed, %u left.", s->n_connections);
5632e374
LP
2758}
2759
d137a488
UTL
2760static void socket_trigger_notify(Unit *u, Unit *other) {
2761 Socket *s = SOCKET(u);
d137a488
UTL
2762
2763 assert(u);
2764 assert(other);
2765
d14e3a0d
LP
2766 /* Filter out invocations with bogus state */
2767 if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
2768 return;
2769
2770 /* Don't propagate state changes from the service if we are already down */
2771 if (!IN_SET(s->state, SOCKET_RUNNING, SOCKET_LISTENING))
d137a488
UTL
2772 return;
2773
d14e3a0d
LP
2774 /* We don't care for the service state if we are in Accept=yes mode */
2775 if (s->accept)
2776 return;
2777
2778 /* Propagate start limit hit state */
6bf0f408
LP
2779 if (other->start_limit_hit) {
2780 socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT);
d137a488 2781 return;
6bf0f408 2782 }
d137a488 2783
d14e3a0d
LP
2784 /* Don't propagate anything if there's still a job queued */
2785 if (other->job)
6bf0f408 2786 return;
d137a488 2787
6bf0f408
LP
2788 if (IN_SET(SERVICE(other)->state,
2789 SERVICE_DEAD, SERVICE_FAILED,
2790 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
2791 SERVICE_AUTO_RESTART))
2792 socket_enter_listening(s);
d137a488 2793
6bf0f408 2794 if (SERVICE(other)->state == SERVICE_RUNNING)
d137a488
UTL
2795 socket_set_state(s, SOCKET_RUNNING);
2796}
2797
718db961 2798static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
814cc562 2799 return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error);
8a0867d6
LP
2800}
2801
7a7821c8 2802static int socket_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 2803 Socket *s = SOCKET(u);
7a7821c8 2804 usec_t t;
68db7a3b
ZJS
2805 int r;
2806
2807 if (!s->timer_event_source)
2808 return 0;
2809
7a7821c8 2810 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
2811 if (r < 0)
2812 return r;
7a7821c8
LP
2813 if (t == USEC_INFINITY)
2814 return 0;
68db7a3b 2815
7a7821c8 2816 *timeout = t;
68db7a3b
ZJS
2817 return 1;
2818}
2819
8dd4c05b
LP
2820char *socket_fdname(Socket *s) {
2821 assert(s);
2822
2823 /* Returns the name to use for $LISTEN_NAMES. If the user
2824 * didn't specify anything specifically, use the socket unit's
2825 * name as fallback. */
2826
2827 if (s->fdname)
2828 return s->fdname;
2829
2830 return UNIT(s)->id;
2831}
2832
291d565a
LP
2833static int socket_control_pid(Unit *u) {
2834 Socket *s = SOCKET(u);
2835
2836 assert(s);
2837
2838 return s->control_pid;
2839}
2840
a16e1123
LP
2841static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
2842 [SOCKET_EXEC_START_PRE] = "StartPre",
3900e5fd 2843 [SOCKET_EXEC_START_CHOWN] = "StartChown",
a16e1123
LP
2844 [SOCKET_EXEC_START_POST] = "StartPost",
2845 [SOCKET_EXEC_STOP_PRE] = "StopPre",
2846 [SOCKET_EXEC_STOP_POST] = "StopPost"
2847};
2848
2849DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand);
2850
cfc4eb4c
LP
2851static const char* const socket_result_table[_SOCKET_RESULT_MAX] = {
2852 [SOCKET_SUCCESS] = "success",
2853 [SOCKET_FAILURE_RESOURCES] = "resources",
2854 [SOCKET_FAILURE_TIMEOUT] = "timeout",
2855 [SOCKET_FAILURE_EXIT_CODE] = "exit-code",
2856 [SOCKET_FAILURE_SIGNAL] = "signal",
c2f34808 2857 [SOCKET_FAILURE_CORE_DUMP] = "core-dump",
07299350 2858 [SOCKET_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
8b26cdbd 2859 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT] = "trigger-limit-hit",
6bf0f408 2860 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit"
cfc4eb4c
LP
2861};
2862
2863DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult);
2864
87f0e418 2865const UnitVTable socket_vtable = {
7d17cfbc 2866 .object_size = sizeof(Socket),
718db961
LP
2867 .exec_context_offset = offsetof(Socket, exec_context),
2868 .cgroup_context_offset = offsetof(Socket, cgroup_context),
2869 .kill_context_offset = offsetof(Socket, kill_context),
613b411c 2870 .exec_runtime_offset = offsetof(Socket, exec_runtime),
3ef63c31 2871
f975e971
LP
2872 .sections =
2873 "Unit\0"
2874 "Socket\0"
2875 "Install\0",
4ad49000 2876 .private_section = "Socket",
71645aca 2877
034c6ed7
LP
2878 .init = socket_init,
2879 .done = socket_done,
a16e1123
LP
2880 .load = socket_load,
2881
2882 .coldplug = socket_coldplug,
034c6ed7 2883
5cb5a6ff
LP
2884 .dump = socket_dump,
2885
542563ba
LP
2886 .start = socket_start,
2887 .stop = socket_stop,
5cb5a6ff 2888
718db961
LP
2889 .kill = socket_kill,
2890
68db7a3b
ZJS
2891 .get_timeout = socket_get_timeout,
2892
a16e1123
LP
2893 .serialize = socket_serialize,
2894 .deserialize_item = socket_deserialize_item,
01e10de3 2895 .distribute_fds = socket_distribute_fds,
a16e1123 2896
5cb5a6ff 2897 .active_state = socket_active_state,
10a94420 2898 .sub_state_to_string = socket_sub_state_to_string,
5cb5a6ff 2899
6cf6bbc2
LP
2900 .check_gc = socket_check_gc,
2901
034c6ed7 2902 .sigchld_event = socket_sigchld_event,
4139c1b2 2903
d137a488
UTL
2904 .trigger_notify = socket_trigger_notify,
2905
fdf20a31 2906 .reset_failed = socket_reset_failed,
5632e374 2907
291d565a
LP
2908 .control_pid = socket_control_pid,
2909
718db961 2910 .bus_vtable = bus_socket_vtable,
74c964d3
LP
2911 .bus_set_property = bus_socket_set_property,
2912 .bus_commit_properties = bus_socket_commit_properties,
c6918296
MS
2913
2914 .status_message_formats = {
2915 /*.starting_stopping = {
2916 [0] = "Starting socket %s...",
2917 [1] = "Stopping socket %s...",
2918 },*/
2919 .finished_start_job = {
2920 [JOB_DONE] = "Listening on %s.",
2921 [JOB_FAILED] = "Failed to listen on %s.",
c6918296
MS
2922 [JOB_TIMEOUT] = "Timed out starting %s.",
2923 },
2924 .finished_stop_job = {
2925 [JOB_DONE] = "Closed %s.",
2926 [JOB_FAILED] = "Failed stopping %s.",
2927 [JOB_TIMEOUT] = "Timed out stopping %s.",
2928 },
2929 },
5cb5a6ff 2930};