]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/socket.c
core: simplify unit_load() a bit
[thirdparty/systemd.git] / src / core / socket.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09 2
e8da24a6 3#include <arpa/inet.h>
83c60c9f
LP
4#include <errno.h>
5#include <fcntl.h>
916abb21 6#include <mqueue.h>
e8da24a6
LP
7#include <netinet/tcp.h>
8#include <signal.h>
9#include <sys/epoll.h>
10#include <sys/stat.h>
11#include <unistd.h>
62bc4efc 12#include <linux/sctp.h>
83c60c9f 13
b5efdb8a 14#include "alloc-util.h"
a79279c7 15#include "bpf-firewall.h"
e8da24a6
LP
16#include "bus-error.h"
17#include "bus-util.h"
18#include "copy.h"
19#include "dbus-socket.h"
6fcbec6f 20#include "dbus-unit.h"
e8da24a6
LP
21#include "def.h"
22#include "exit-status.h"
3ffd4af2 23#include "fd-util.h"
f97b34a6 24#include "format-util.h"
95f7fbbf 25#include "fs-util.h"
a79279c7 26#include "in-addr-util.h"
60d9771c 27#include "io-util.h"
da96ad5a 28#include "ip-protocol-list.h"
e8da24a6 29#include "label.h"
83c60c9f 30#include "log.h"
e8da24a6 31#include "missing.h"
49e942b2 32#include "mkdir.h"
6bedfcbb 33#include "parse-util.h"
9eb977db 34#include "path-util.h"
7b3e062c 35#include "process-util.h"
d7b8eec7 36#include "selinux-util.h"
d68c645b 37#include "serialize.h"
24882e06 38#include "signal-util.h"
e8da24a6 39#include "smack-util.h"
d68c645b 40#include "socket.h"
e8da24a6 41#include "special.h"
8b43440b 42#include "string-table.h"
07630cea 43#include "string-util.h"
e8da24a6
LP
44#include "strv.h"
45#include "unit-name.h"
e8da24a6 46#include "unit.h"
b1d4f8e1 47#include "user-util.h"
83c60c9f 48
166cf510
ZJS
49struct SocketPeer {
50 unsigned n_ref;
51
52 Socket *socket;
53 union sockaddr_union peer;
41733ae1 54 socklen_t peer_salen;
166cf510
ZJS
55};
56
acbb0225 57static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
87f0e418
LP
58 [SOCKET_DEAD] = UNIT_INACTIVE,
59 [SOCKET_START_PRE] = UNIT_ACTIVATING,
3900e5fd 60 [SOCKET_START_CHOWN] = UNIT_ACTIVATING,
87f0e418
LP
61 [SOCKET_START_POST] = UNIT_ACTIVATING,
62 [SOCKET_LISTENING] = UNIT_ACTIVE,
63 [SOCKET_RUNNING] = UNIT_ACTIVE,
64 [SOCKET_STOP_PRE] = UNIT_DEACTIVATING,
65 [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING,
66 [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING,
67 [SOCKET_STOP_POST] = UNIT_DEACTIVATING,
80876c20
LP
68 [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING,
69 [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING,
c968d76a
YW
70 [SOCKET_FAILED] = UNIT_FAILED,
71 [SOCKET_CLEANING] = UNIT_MAINTENANCE,
83c60c9f 72};
5cb5a6ff 73
718db961
LP
74static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
75static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
76
a16e1123
LP
77static void socket_init(Unit *u) {
78 Socket *s = SOCKET(u);
79
80 assert(u);
ac155bb8 81 assert(u->load_state == UNIT_STUB);
a16e1123 82
a16e1123 83 s->backlog = SOMAXCONN;
1f19a534 84 s->timeout_usec = u->manager->default_timeout_start_usec;
a16e1123 85 s->directory_mode = 0755;
9131f660 86 s->socket_mode = 0666;
a16e1123 87
6cf6bbc2
LP
88 s->max_connections = 64;
89
4fd5948e 90 s->priority = -1;
4fd5948e
LP
91 s->ip_tos = -1;
92 s->ip_ttl = -1;
4fd5948e 93 s->mark = -1;
4fd5948e 94
ac155bb8
MS
95 s->exec_context.std_output = u->manager->default_std_output;
96 s->exec_context.std_error = u->manager->default_std_error;
085afe36 97
a16e1123 98 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
8b26cdbd 99
1f15ce28
LP
100 s->trigger_limit.interval = USEC_INFINITY;
101 s->trigger_limit.burst = (unsigned) -1;
a16e1123 102}
acbb0225 103
5e94833f
LP
104static void socket_unwatch_control_pid(Socket *s) {
105 assert(s);
106
107 if (s->control_pid <= 0)
108 return;
109
110 unit_unwatch_pid(UNIT(s), s->control_pid);
111 s->control_pid = 0;
112}
113
15087cdb 114static void socket_cleanup_fd_list(SocketPort *p) {
3ffd4af2 115 assert(p);
15087cdb 116
3ffd4af2 117 close_many(p->auxiliary_fds, p->n_auxiliary_fds);
15087cdb
PS
118 p->auxiliary_fds = mfree(p->auxiliary_fds);
119 p->n_auxiliary_fds = 0;
120}
121
74051b9b 122void socket_free_ports(Socket *s) {
034c6ed7
LP
123 SocketPort *p;
124
125 assert(s);
126
127 while ((p = s->ports)) {
71fda00f 128 LIST_REMOVE(port, s->ports, p);
034c6ed7 129
718db961
LP
130 sd_event_source_unref(p->event_source);
131
15087cdb 132 socket_cleanup_fd_list(p);
03e334a1 133 safe_close(p->fd);
034c6ed7
LP
134 free(p->path);
135 free(p);
136 }
74051b9b
LP
137}
138
139static void socket_done(Unit *u) {
140 Socket *s = SOCKET(u);
9d565427 141 SocketPeer *p;
74051b9b
LP
142
143 assert(s);
144
145 socket_free_ports(s);
034c6ed7 146
9a73653c 147 while ((p = set_steal_first(s->peers_by_address)))
9d565427
SS
148 p->socket = NULL;
149
9a73653c 150 s->peers_by_address = set_free(s->peers_by_address);
9d565427 151
e8a565cb 152 s->exec_runtime = exec_runtime_unref(s->exec_runtime, false);
e537352b 153 exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
034c6ed7
LP
154 s->control_command = NULL;
155
29206d46
LP
156 dynamic_creds_unref(&s->dynamic_creds);
157
5e94833f 158 socket_unwatch_control_pid(s);
034c6ed7 159
57020a3a 160 unit_ref_unset(&s->service);
034c6ed7 161
a1e58e8e
LP
162 s->tcp_congestion = mfree(s->tcp_congestion);
163 s->bind_to_device = mfree(s->bind_to_device);
acbb0225 164
0a78712e
DM
165 s->smack = mfree(s->smack);
166 s->smack_ip_in = mfree(s->smack_ip_in);
167 s->smack_ip_out = mfree(s->smack_ip_out);
0eb59ccf 168
811ba7a0
LP
169 strv_free(s->symlinks);
170
0a78712e
DM
171 s->user = mfree(s->user);
172 s->group = mfree(s->group);
3900e5fd 173
a97b23d6
DM
174 s->fdname = mfree(s->fdname);
175
718db961
LP
176 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
177}
178
36c16a7c 179static int socket_arm_timer(Socket *s, usec_t usec) {
718db961
LP
180 int r;
181
182 assert(s);
183
718db961 184 if (s->timer_event_source) {
36c16a7c 185 r = sd_event_source_set_time(s->timer_event_source, usec);
718db961
LP
186 if (r < 0)
187 return r;
188
189 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
190 }
191
36c16a7c
LP
192 if (usec == USEC_INFINITY)
193 return 0;
194
7dfbe2e3 195 r = sd_event_add_time(
6a0f1f6d
LP
196 UNIT(s)->manager->event,
197 &s->timer_event_source,
198 CLOCK_MONOTONIC,
36c16a7c 199 usec, 0,
6a0f1f6d 200 socket_dispatch_timer, s);
7dfbe2e3
TG
201 if (r < 0)
202 return r;
203
204 (void) sd_event_source_set_description(s->timer_event_source, "socket-timer");
205
206 return 0;
5cb5a6ff
LP
207}
208
8b835fcc
ZJS
209int socket_instantiate_service(Socket *s) {
210 _cleanup_free_ char *prefix = NULL, *name = NULL;
b15bdda8
LP
211 int r;
212 Unit *u;
213
214 assert(s);
215
216 /* This fills in s->service if it isn't filled in yet. For
217 * Accept=yes sockets we create the next connection service
218 * here. For Accept=no this is mostly a NOP since the service
219 * is figured out at load time anyway. */
220
7410616c 221 if (UNIT_DEREF(s->service))
b15bdda8
LP
222 return 0;
223
7410616c
LP
224 if (!s->accept)
225 return 0;
226
227 r = unit_name_to_prefix(UNIT(s)->id, &prefix);
228 if (r < 0)
229 return r;
b15bdda8 230
315db1a8 231 if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0)
b15bdda8
LP
232 return -ENOMEM;
233
1124fe6f 234 r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u);
b15bdda8
LP
235 if (r < 0)
236 return r;
237
7f7d01ed 238 unit_ref_set(&s->service, UNIT(s), u);
57020a3a 239
eef85c4a 240 return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false, UNIT_DEPENDENCY_IMPLICIT);
b15bdda8
LP
241}
242
4f2d528d
LP
243static bool have_non_accept_socket(Socket *s) {
244 SocketPort *p;
245
246 assert(s);
247
248 if (!s->accept)
249 return true;
250
dd5ad9d4
LP
251 LIST_FOREACH(port, p, s->ports) {
252
253 if (p->type != SOCKET_SOCKET)
254 return true;
255
4f2d528d
LP
256 if (!socket_address_can_accept(&p->address))
257 return true;
dd5ad9d4 258 }
4f2d528d
LP
259
260 return false;
261}
262
eef85c4a 263static int socket_add_mount_dependencies(Socket *s) {
6e2ef85b 264 SocketPort *p;
6e2ef85b
LP
265 int r;
266
267 assert(s);
6e2ef85b 268
a57f7e2c
LP
269 LIST_FOREACH(port, p, s->ports) {
270 const char *path = NULL;
6e2ef85b 271
a57f7e2c
LP
272 if (p->type == SOCKET_SOCKET)
273 path = socket_address_get_path(&p->address);
60252446 274 else if (IN_SET(p->type, SOCKET_FIFO, SOCKET_SPECIAL, SOCKET_USB_FUNCTION))
a57f7e2c 275 path = p->path;
6e2ef85b 276
a57f7e2c
LP
277 if (!path)
278 continue;
6e2ef85b 279
eef85c4a 280 r = unit_require_mounts_for(UNIT(s), path, UNIT_DEPENDENCY_FILE);
b87705cd 281 if (r < 0)
6e2ef85b 282 return r;
b87705cd 283 }
6e2ef85b
LP
284
285 return 0;
286}
287
eef85c4a 288static int socket_add_device_dependencies(Socket *s) {
6e2ef85b 289 char *t;
6e2ef85b
LP
290
291 assert(s);
292
7d0c710d 293 if (!s->bind_to_device || streq(s->bind_to_device, "lo"))
6e2ef85b
LP
294 return 0;
295
63c372cb 296 t = strjoina("/sys/subsystem/net/devices/", s->bind_to_device);
eef85c4a 297 return unit_add_node_dependency(UNIT(s), t, false, UNIT_BINDS_TO, UNIT_DEPENDENCY_FILE);
6e2ef85b
LP
298}
299
a40eb732
LP
300static int socket_add_default_dependencies(Socket *s) {
301 int r;
302 assert(s);
303
4c9ea260
LP
304 if (!UNIT(s)->default_dependencies)
305 return 0;
306
35d8c19a 307 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
e3d84721
LP
308 if (r < 0)
309 return r;
2a77d31d 310
463d0d15 311 if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) {
5a724170 312 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
e3d84721 313 if (r < 0)
a40eb732 314 return r;
2a77d31d 315 }
a40eb732 316
5a724170 317 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
a40eb732
LP
318}
319
44a6b1b6 320_pure_ static bool socket_has_exec(Socket *s) {
4cfc6dbe
LP
321 unsigned i;
322 assert(s);
323
324 for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++)
325 if (s->exec_command[i])
326 return true;
327
328 return false;
329}
330
e821075a
LP
331static int socket_add_extras(Socket *s) {
332 Unit *u = UNIT(s);
e537352b 333 int r;
44d8db9e 334
e821075a 335 assert(s);
57020a3a 336
1f15ce28
LP
337 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
338 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
339 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
340 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
341 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
342 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
343 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
344 * service starts are typical. */
345
346 if (s->trigger_limit.interval == USEC_INFINITY)
347 s->trigger_limit.interval = 2 * USEC_PER_SEC;
348
349 if (s->trigger_limit.burst == (unsigned) -1) {
350 if (s->accept)
351 s->trigger_limit.burst = 200;
352 else
353 s->trigger_limit.burst = 20;
354 }
355
e821075a 356 if (have_non_accept_socket(s)) {
23a177ef 357
e821075a
LP
358 if (!UNIT_DEREF(s->service)) {
359 Unit *x;
57020a3a 360
e821075a 361 r = unit_load_related_unit(u, ".service", &x);
57020a3a 362 if (r < 0)
4f2d528d 363 return r;
e821075a 364
7f7d01ed 365 unit_ref_set(&s->service, u, x);
4f2d528d 366 }
44d8db9e 367
eef85c4a 368 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true, UNIT_DEPENDENCY_IMPLICIT);
e821075a 369 if (r < 0)
6e2ef85b 370 return r;
e821075a 371 }
6e2ef85b 372
eef85c4a 373 r = socket_add_mount_dependencies(s);
e821075a
LP
374 if (r < 0)
375 return r;
6e2ef85b 376
eef85c4a 377 r = socket_add_device_dependencies(s);
e821075a
LP
378 if (r < 0)
379 return r;
380
598459ce 381 r = unit_patch_contexts(u);
e821075a
LP
382 if (r < 0)
383 return r;
384
385 if (socket_has_exec(s)) {
386 r = unit_add_exec_dependencies(u, &s->exec_context);
387 if (r < 0)
388 return r;
e821075a 389 }
a016b922 390
88af31f9
LP
391 r = unit_set_default_slice(u);
392 if (r < 0)
393 return r;
394
4c9ea260
LP
395 r = socket_add_default_dependencies(s);
396 if (r < 0)
397 return r;
e821075a
LP
398
399 return 0;
400}
401
811ba7a0
LP
402static const char *socket_find_symlink_target(Socket *s) {
403 const char *found = NULL;
404 SocketPort *p;
405
406 LIST_FOREACH(port, p, s->ports) {
407 const char *f = NULL;
408
409 switch (p->type) {
410
411 case SOCKET_FIFO:
412 f = p->path;
413 break;
414
415 case SOCKET_SOCKET:
b9495e8d 416 f = socket_address_get_path(&p->address);
811ba7a0
LP
417 break;
418
419 default:
420 break;
421 }
422
423 if (f) {
424 if (found)
425 return NULL;
426
427 found = f;
428 }
429 }
430
431 return found;
432}
433
e821075a
LP
434static int socket_verify(Socket *s) {
435 assert(s);
436
437 if (UNIT(s)->load_state != UNIT_LOADED)
438 return 0;
439
440 if (!s->ports) {
da3bddc9 441 log_unit_error(UNIT(s), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
6f40aa45 442 return -ENOEXEC;
e821075a
LP
443 }
444
445 if (s->accept && have_non_accept_socket(s)) {
f2341e0a 446 log_unit_error(UNIT(s), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
6f40aa45 447 return -ENOEXEC;
e821075a
LP
448 }
449
450 if (s->accept && s->max_connections <= 0) {
f2341e0a 451 log_unit_error(UNIT(s), "MaxConnection= setting too small. Refusing.");
6f40aa45 452 return -ENOEXEC;
e821075a 453 }
e06c73cc 454
e821075a 455 if (s->accept && UNIT_DEREF(s->service)) {
f2341e0a 456 log_unit_error(UNIT(s), "Explicit service configuration for accepting socket units not supported. Refusing.");
6f40aa45 457 return -ENOEXEC;
e821075a
LP
458 }
459
460 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
f2341e0a 461 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
6f40aa45 462 return -ENOEXEC;
e821075a
LP
463 }
464
811ba7a0 465 if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) {
f2341e0a 466 log_unit_error(UNIT(s), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
6f40aa45 467 return -ENOEXEC;
811ba7a0
LP
468 }
469
e821075a
LP
470 return 0;
471}
472
7a08d314 473static void peer_address_hash_func(const SocketPeer *s, struct siphash *state) {
9d565427
SS
474 assert(s);
475
476 if (s->peer.sa.sa_family == AF_INET)
477 siphash24_compress(&s->peer.in.sin_addr, sizeof(s->peer.in.sin_addr), state);
41733ae1 478 else if (s->peer.sa.sa_family == AF_INET6)
9d565427 479 siphash24_compress(&s->peer.in6.sin6_addr, sizeof(s->peer.in6.sin6_addr), state);
359a5bcf
SH
480 else if (s->peer.sa.sa_family == AF_VSOCK)
481 siphash24_compress(&s->peer.vm.svm_cid, sizeof(s->peer.vm.svm_cid), state);
41733ae1
LP
482 else
483 assert_not_reached("Unknown address family.");
9d565427
SS
484}
485
7a08d314 486static int peer_address_compare_func(const SocketPeer *x, const SocketPeer *y) {
a0edd02e 487 int r;
9d565427 488
a0edd02e
FB
489 r = CMP(x->peer.sa.sa_family, y->peer.sa.sa_family);
490 if (r != 0)
491 return r;
9d565427
SS
492
493 switch(x->peer.sa.sa_family) {
494 case AF_INET:
495 return memcmp(&x->peer.in.sin_addr, &y->peer.in.sin_addr, sizeof(x->peer.in.sin_addr));
496 case AF_INET6:
497 return memcmp(&x->peer.in6.sin6_addr, &y->peer.in6.sin6_addr, sizeof(x->peer.in6.sin6_addr));
359a5bcf 498 case AF_VSOCK:
6dd91b36 499 return CMP(x->peer.vm.svm_cid, y->peer.vm.svm_cid);
9d565427 500 }
166cf510 501 assert_not_reached("Black sheep in the family!");
9d565427
SS
502}
503
7a08d314 504DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops, SocketPeer, peer_address_hash_func, peer_address_compare_func);
9d565427 505
e821075a
LP
506static int socket_load(Unit *u) {
507 Socket *s = SOCKET(u);
508 int r;
509
510 assert(u);
511 assert(u->load_state == UNIT_STUB);
512
9a73653c 513 r = set_ensure_allocated(&s->peers_by_address, &peer_address_hash_ops);
9d565427
SS
514 if (r < 0)
515 return r;
516
c3620770 517 r = unit_load_fragment_and_dropin(u, true);
e821075a
LP
518 if (r < 0)
519 return r;
520
521 if (u->load_state == UNIT_LOADED) {
522 /* This is a new unit? Then let's add in some extras */
523 r = socket_add_extras(s);
e06c73cc
LP
524 if (r < 0)
525 return r;
23a177ef
LP
526 }
527
4f2d528d 528 return socket_verify(s);
44d8db9e
LP
529}
530
166cf510
ZJS
531static SocketPeer *socket_peer_new(void) {
532 SocketPeer *p;
533
534 p = new0(SocketPeer, 1);
535 if (!p)
536 return NULL;
537
538 p->n_ref = 1;
539
540 return p;
541}
542
8301aa0b
YW
543static SocketPeer *socket_peer_free(SocketPeer *p) {
544 assert(p);
166cf510
ZJS
545
546 if (p->socket)
547 set_remove(p->socket->peers_by_address, p);
548
549 return mfree(p);
550}
551
8301aa0b
YW
552DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer, socket_peer, socket_peer_free);
553
3ebcd323 554int socket_acquire_peer(Socket *s, int fd, SocketPeer **p) {
166cf510
ZJS
555 _cleanup_(socket_peer_unrefp) SocketPeer *remote = NULL;
556 SocketPeer sa = {}, *i;
557 socklen_t salen = sizeof(sa.peer);
558 int r;
559
560 assert(fd >= 0);
561 assert(s);
562
563 r = getpeername(fd, &sa.peer.sa, &salen);
564 if (r < 0)
fc2d74ab 565 return log_unit_error_errno(UNIT(s), errno, "getpeername failed: %m");
166cf510 566
359a5bcf 567 if (!IN_SET(sa.peer.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) {
166cf510
ZJS
568 *p = NULL;
569 return 0;
570 }
571
572 i = set_get(s->peers_by_address, &sa);
573 if (i) {
574 *p = socket_peer_ref(i);
575 return 1;
576 }
577
578 remote = socket_peer_new();
579 if (!remote)
580 return log_oom();
581
582 remote->peer = sa.peer;
41733ae1 583 remote->peer_salen = salen;
166cf510
ZJS
584
585 r = set_put(s->peers_by_address, remote);
586 if (r < 0)
587 return r;
588
589 remote->socket = s;
590
1cc6c93a 591 *p = TAKE_PTR(remote);
166cf510
ZJS
592
593 return 1;
594}
595
44a6b1b6 596_const_ static const char* listen_lookup(int family, int type) {
7a22745a
LP
597
598 if (family == AF_NETLINK)
599 return "ListenNetlink";
542563ba
LP
600
601 if (type == SOCK_STREAM)
602 return "ListenStream";
603 else if (type == SOCK_DGRAM)
604 return "ListenDatagram";
605 else if (type == SOCK_SEQPACKET)
606 return "ListenSequentialPacket";
607
034c6ed7 608 assert_not_reached("Unknown socket type");
542563ba
LP
609 return NULL;
610}
611
87f0e418 612static void socket_dump(Unit *u, FILE *f, const char *prefix) {
209e9dcd 613 char time_string[FORMAT_TIMESPAN_MAX];
5cb5a6ff 614 SocketExecCommand c;
87f0e418 615 Socket *s = SOCKET(u);
542563ba 616 SocketPort *p;
827d9bf2 617 const char *prefix2, *str;
5cb5a6ff
LP
618
619 assert(s);
fa068367 620 assert(f);
5cb5a6ff 621
4c940960 622 prefix = strempty(prefix);
63c372cb 623 prefix2 = strjoina(prefix, "\t");
c43d20a0 624
5cb5a6ff
LP
625 fprintf(f,
626 "%sSocket State: %s\n"
81a5c6d0 627 "%sResult: %s\n"
c968d76a 628 "%sClean Result: %s\n"
542563ba 629 "%sBindIPv6Only: %s\n"
b5a0699f
LP
630 "%sBacklog: %u\n"
631 "%sSocketMode: %04o\n"
4fd5948e
LP
632 "%sDirectoryMode: %04o\n"
633 "%sKeepAlive: %s\n"
4427c3f4 634 "%sNoDelay: %s\n"
cebf8b20 635 "%sFreeBind: %s\n"
6b6d2dee 636 "%sTransparent: %s\n"
ec6370a2 637 "%sBroadcast: %s\n"
ede3deb4 638 "%sPassCredentials: %s\n"
54ecda32 639 "%sPassSecurity: %s\n"
bd1fe7c7 640 "%sTCPCongestion: %s\n"
16115b0a 641 "%sRemoveOnStop: %s\n"
55301ec0 642 "%sWritable: %s\n"
827d9bf2 643 "%sFileDescriptorName: %s\n"
16115b0a 644 "%sSELinuxContextFromNet: %s\n",
a16e1123 645 prefix, socket_state_to_string(s->state),
81a5c6d0 646 prefix, socket_result_to_string(s->result),
c968d76a 647 prefix, socket_result_to_string(s->clean_result),
c0120d99 648 prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only),
b5a0699f
LP
649 prefix, s->backlog,
650 prefix, s->socket_mode,
4fd5948e
LP
651 prefix, s->directory_mode,
652 prefix, yes_no(s->keep_alive),
4427c3f4 653 prefix, yes_no(s->no_delay),
cebf8b20 654 prefix, yes_no(s->free_bind),
6b6d2dee 655 prefix, yes_no(s->transparent),
ec6370a2 656 prefix, yes_no(s->broadcast),
d68af586 657 prefix, yes_no(s->pass_cred),
54ecda32 658 prefix, yes_no(s->pass_sec),
bd1fe7c7 659 prefix, strna(s->tcp_congestion),
16115b0a 660 prefix, yes_no(s->remove_on_stop),
55301ec0 661 prefix, yes_no(s->writable),
8dd4c05b 662 prefix, socket_fdname(s),
16115b0a 663 prefix, yes_no(s->selinux_context_from_net));
542563ba 664
70123e68
LP
665 if (s->control_pid > 0)
666 fprintf(f,
de0671ee
ZJS
667 "%sControl PID: "PID_FMT"\n",
668 prefix, s->control_pid);
70123e68 669
acbb0225
LP
670 if (s->bind_to_device)
671 fprintf(f,
672 "%sBindToDevice: %s\n",
673 prefix, s->bind_to_device);
674
4f2d528d
LP
675 if (s->accept)
676 fprintf(f,
6cf6bbc2
LP
677 "%sAccepted: %u\n"
678 "%sNConnections: %u\n"
827d9bf2
YW
679 "%sMaxConnections: %u\n"
680 "%sMaxConnectionsPerSource: %u\n",
6cf6bbc2
LP
681 prefix, s->n_accepted,
682 prefix, s->n_connections,
827d9bf2
YW
683 prefix, s->max_connections,
684 prefix, s->max_connections_per_source);
4f2d528d 685
4fd5948e
LP
686 if (s->priority >= 0)
687 fprintf(f,
688 "%sPriority: %i\n",
689 prefix, s->priority);
690
691 if (s->receive_buffer > 0)
692 fprintf(f,
693 "%sReceiveBuffer: %zu\n",
694 prefix, s->receive_buffer);
695
696 if (s->send_buffer > 0)
697 fprintf(f,
698 "%sSendBuffer: %zu\n",
699 prefix, s->send_buffer);
700
701 if (s->ip_tos >= 0)
702 fprintf(f,
703 "%sIPTOS: %i\n",
704 prefix, s->ip_tos);
705
706 if (s->ip_ttl >= 0)
707 fprintf(f,
708 "%sIPTTL: %i\n",
709 prefix, s->ip_ttl);
710
711 if (s->pipe_size > 0)
712 fprintf(f,
713 "%sPipeSize: %zu\n",
714 prefix, s->pipe_size);
715
716 if (s->mark >= 0)
717 fprintf(f,
718 "%sMark: %i\n",
719 prefix, s->mark);
720
916abb21
LP
721 if (s->mq_maxmsg > 0)
722 fprintf(f,
723 "%sMessageQueueMaxMessages: %li\n",
724 prefix, s->mq_maxmsg);
725
726 if (s->mq_msgsize > 0)
727 fprintf(f,
728 "%sMessageQueueMessageSize: %li\n",
729 prefix, s->mq_msgsize);
730
718db961 731 if (s->reuse_port)
f7db7a69
SL
732 fprintf(f,
733 "%sReusePort: %s\n",
718db961 734 prefix, yes_no(s->reuse_port));
f7db7a69 735
0eb59ccf
AK
736 if (s->smack)
737 fprintf(f,
738 "%sSmackLabel: %s\n",
739 prefix, s->smack);
740
741 if (s->smack_ip_in)
742 fprintf(f,
743 "%sSmackLabelIPIn: %s\n",
744 prefix, s->smack_ip_in);
745
746 if (s->smack_ip_out)
747 fprintf(f,
748 "%sSmackLabelIPOut: %s\n",
749 prefix, s->smack_ip_out);
750
3900e5fd
LP
751 if (!isempty(s->user) || !isempty(s->group))
752 fprintf(f,
d2a50e3b
LP
753 "%sSocketUser: %s\n"
754 "%sSocketGroup: %s\n",
3900e5fd
LP
755 prefix, strna(s->user),
756 prefix, strna(s->group));
757
3cd761e4 758 if (s->keep_alive_time > 0)
209e9dcd 759 fprintf(f,
3cd761e4
LP
760 "%sKeepAliveTimeSec: %s\n",
761 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_time, USEC_PER_SEC));
209e9dcd 762
7be9df7d 763 if (s->keep_alive_interval > 0)
209e9dcd 764 fprintf(f,
3cd761e4
LP
765 "%sKeepAliveIntervalSec: %s\n",
766 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_interval, USEC_PER_SEC));
209e9dcd 767
7be9df7d 768 if (s->keep_alive_cnt > 0)
209e9dcd
SS
769 fprintf(f,
770 "%sKeepAliveProbes: %u\n",
771 prefix, s->keep_alive_cnt);
772
7be9df7d 773 if (s->defer_accept > 0)
cc567c9b 774 fprintf(f,
3cd761e4
LP
775 "%sDeferAcceptSec: %s\n",
776 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->defer_accept, USEC_PER_SEC));
cc567c9b 777
034c6ed7 778 LIST_FOREACH(port, p, s->ports) {
5cb5a6ff 779
7be9df7d
YW
780 switch (p->type) {
781 case SOCKET_SOCKET: {
782 _cleanup_free_ char *k = NULL;
542563ba
LP
783 const char *t;
784 int r;
542563ba 785
e53fc357
LP
786 r = socket_address_print(&p->address, &k);
787 if (r < 0)
4bbccb02 788 t = strerror_safe(r);
542563ba
LP
789 else
790 t = k;
791
7a22745a 792 fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
7be9df7d
YW
793 break;
794 }
795 case SOCKET_SPECIAL:
b0a3f2bc 796 fprintf(f, "%sListenSpecial: %s\n", prefix, p->path);
7be9df7d
YW
797 break;
798 case SOCKET_USB_FUNCTION:
60252446 799 fprintf(f, "%sListenUSBFunction: %s\n", prefix, p->path);
7be9df7d
YW
800 break;
801 case SOCKET_MQUEUE:
916abb21 802 fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path);
7be9df7d
YW
803 break;
804 default:
542563ba 805 fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
7be9df7d 806 }
542563ba 807 }
5cb5a6ff 808
1745fa70
EV
809 fprintf(f,
810 "%sTriggerLimitIntervalSec: %s\n"
811 "%sTriggerLimitBurst: %u\n",
812 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->trigger_limit.interval, USEC_PER_SEC),
813 prefix, s->trigger_limit.burst);
814
da96ad5a 815 str = ip_protocol_to_name(s->socket_protocol);
827d9bf2
YW
816 if (str)
817 fprintf(f, "%sSocketProtocol: %s\n", prefix, str);
818
819 if (!strv_isempty(s->symlinks)) {
820 char **q;
821
822 fprintf(f, "%sSymlinks:", prefix);
823 STRV_FOREACH(q, s->symlinks)
824 fprintf(f, " %s", *q);
825
826 fprintf(f, "\n");
827 }
828
829 fprintf(f,
830 "%sTimeoutSec: %s\n",
831 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->timeout_usec, USEC_PER_SEC));
832
5cb5a6ff 833 exec_context_dump(&s->exec_context, f, prefix);
4819ff03 834 kill_context_dump(&s->kill_context, f, prefix);
5cb5a6ff 835
e537352b 836 for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) {
c43d20a0
LP
837 if (!s->exec_command[c])
838 continue;
5cb5a6ff 839
40d50879 840 fprintf(f, "%s-> %s:\n",
a16e1123 841 prefix, socket_exec_command_to_string(c));
c43d20a0
LP
842
843 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 844 }
18f573aa 845
bc0623df 846 cgroup_context_dump(UNIT(s), f, prefix);
5cb5a6ff
LP
847}
848
4f2d528d
LP
849static int instance_from_socket(int fd, unsigned nr, char **instance) {
850 socklen_t l;
851 char *r;
9d16d0c7 852 union sockaddr_union local, remote;
4f2d528d
LP
853
854 assert(fd >= 0);
855 assert(instance);
856
857 l = sizeof(local);
858 if (getsockname(fd, &local.sa, &l) < 0)
859 return -errno;
860
861 l = sizeof(remote);
862 if (getpeername(fd, &remote.sa, &l) < 0)
863 return -errno;
864
865 switch (local.sa.sa_family) {
866
867 case AF_INET: {
868 uint32_t
8e38570e
LP
869 a = be32toh(local.in.sin_addr.s_addr),
870 b = be32toh(remote.in.sin_addr.s_addr);
4f2d528d
LP
871
872 if (asprintf(&r,
77b088c2
LP
873 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
874 nr,
4f2d528d 875 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
8e38570e 876 be16toh(local.in.sin_port),
4f2d528d 877 b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
8e38570e 878 be16toh(remote.in.sin_port)) < 0)
4f2d528d
LP
879 return -ENOMEM;
880
881 break;
882 }
883
884 case AF_INET6: {
c65a0b14 885 static const unsigned char ipv4_prefix[] = {
2b061f5a
LP
886 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
887 };
888
889 if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 &&
890 memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
891 const uint8_t
892 *a = local.in6.sin6_addr.s6_addr+12,
893 *b = remote.in6.sin6_addr.s6_addr+12;
894
895 if (asprintf(&r,
77b088c2
LP
896 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
897 nr,
2b061f5a 898 a[0], a[1], a[2], a[3],
8e38570e 899 be16toh(local.in6.sin6_port),
2b061f5a 900 b[0], b[1], b[2], b[3],
8e38570e 901 be16toh(remote.in6.sin6_port)) < 0)
2b061f5a
LP
902 return -ENOMEM;
903 } else {
904 char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN];
905
906 if (asprintf(&r,
77b088c2
LP
907 "%u-%s:%u-%s:%u",
908 nr,
2b061f5a 909 inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)),
8e38570e 910 be16toh(local.in6.sin6_port),
2b061f5a 911 inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)),
8e38570e 912 be16toh(remote.in6.sin6_port)) < 0)
2b061f5a
LP
913 return -ENOMEM;
914 }
4f2d528d
LP
915
916 break;
917 }
918
919 case AF_UNIX: {
920 struct ucred ucred;
eff05270 921 int k;
4f2d528d 922
eff05270 923 k = getpeercred(fd, &ucred);
d38f6e34
ZJS
924 if (k >= 0) {
925 if (asprintf(&r,
926 "%u-"PID_FMT"-"UID_FMT,
927 nr, ucred.pid, ucred.uid) < 0)
928 return -ENOMEM;
929 } else if (k == -ENODATA) {
930 /* This handles the case where somebody is
931 * connecting from another pid/uid namespace
932 * (e.g. from outside of our container). */
933 if (asprintf(&r,
934 "%u-unknown",
935 nr) < 0)
936 return -ENOMEM;
937 } else
eff05270 938 return k;
2f20a8eb 939
2f20a8eb 940 break;
4f2d528d
LP
941 }
942
359a5bcf
SH
943 case AF_VSOCK:
944 if (asprintf(&r,
945 "%u-%u:%u-%u:%u",
946 nr,
947 local.vm.svm_cid, local.vm.svm_port,
948 remote.vm.svm_cid, remote.vm.svm_port) < 0)
949 return -ENOMEM;
950
951 break;
952
4f2d528d
LP
953 default:
954 assert_not_reached("Unhandled socket type.");
955 }
956
957 *instance = r;
958 return 0;
959}
960
034c6ed7 961static void socket_close_fds(Socket *s) {
83c60c9f 962 SocketPort *p;
811ba7a0 963 char **i;
83c60c9f
LP
964
965 assert(s);
966
034c6ed7 967 LIST_FOREACH(port, p, s->ports) {
18e854f8 968 bool was_open;
718db961 969
18e854f8 970 was_open = p->fd >= 0;
83c60c9f 971
18e854f8 972 p->event_source = sd_event_source_unref(p->event_source);
03e334a1 973 p->fd = safe_close(p->fd);
15087cdb 974 socket_cleanup_fd_list(p);
a16e1123 975
18e854f8
LP
976 /* One little note: we should normally not delete any sockets in the file system here! After all some
977 * other process we spawned might still have a reference of this fd and wants to continue to use
978 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
979 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
980 * anyway, but only then. */
bd1fe7c7 981
18e854f8
LP
982 if (!was_open || !s->remove_on_stop)
983 continue;
984
985 switch (p->type) {
bd1fe7c7 986
18e854f8
LP
987 case SOCKET_FIFO:
988 (void) unlink(p->path);
989 break;
bd1fe7c7 990
18e854f8
LP
991 case SOCKET_MQUEUE:
992 (void) mq_unlink(p->path);
993 break;
bd1fe7c7 994
18e854f8
LP
995 case SOCKET_SOCKET:
996 (void) socket_address_unlink(&p->address);
997 break;
bd1fe7c7 998
18e854f8
LP
999 default:
1000 break;
bd1fe7c7 1001 }
83c60c9f 1002 }
811ba7a0
LP
1003
1004 if (s->remove_on_stop)
1005 STRV_FOREACH(i, s->symlinks)
18e854f8 1006 (void) unlink(*i);
83c60c9f
LP
1007}
1008
4fd5948e 1009static void socket_apply_socket_options(Socket *s, int fd) {
d53e386d
LP
1010 int r;
1011
4fd5948e
LP
1012 assert(s);
1013 assert(fd >= 0);
1014
1015 if (s->keep_alive) {
2ff48e98
LP
1016 r = setsockopt_int(fd, SOL_SOCKET, SO_KEEPALIVE, true);
1017 if (r < 0)
1018 log_unit_warning_errno(UNIT(s), r, "SO_KEEPALIVE failed: %m");
4fd5948e
LP
1019 }
1020
7be9df7d 1021 if (s->keep_alive_time > 0) {
9e5b6496
YW
1022 r = setsockopt_int(fd, SOL_TCP, TCP_KEEPIDLE, s->keep_alive_time / USEC_PER_SEC);
1023 if (r < 0)
1024 log_unit_warning_errno(UNIT(s), r, "TCP_KEEPIDLE failed: %m");
209e9dcd
SS
1025 }
1026
7be9df7d 1027 if (s->keep_alive_interval > 0) {
9e5b6496
YW
1028 r = setsockopt_int(fd, SOL_TCP, TCP_KEEPINTVL, s->keep_alive_interval / USEC_PER_SEC);
1029 if (r < 0)
1030 log_unit_warning_errno(UNIT(s), r, "TCP_KEEPINTVL failed: %m");
209e9dcd
SS
1031 }
1032
7be9df7d 1033 if (s->keep_alive_cnt > 0) {
9e5b6496
YW
1034 r = setsockopt_int(fd, SOL_TCP, TCP_KEEPCNT, s->keep_alive_cnt);
1035 if (r < 0)
1036 log_unit_warning_errno(UNIT(s), r, "TCP_KEEPCNT failed: %m");
209e9dcd
SS
1037 }
1038
7be9df7d 1039 if (s->defer_accept > 0) {
9e5b6496
YW
1040 r = setsockopt_int(fd, SOL_TCP, TCP_DEFER_ACCEPT, s->defer_accept / USEC_PER_SEC);
1041 if (r < 0)
1042 log_unit_warning_errno(UNIT(s), r, "TCP_DEFER_ACCEPT failed: %m");
cc567c9b
SS
1043 }
1044
4427c3f4 1045 if (s->no_delay) {
62bc4efc 1046 if (s->socket_protocol == IPPROTO_SCTP) {
2ff48e98
LP
1047 r = setsockopt_int(fd, SOL_SCTP, SCTP_NODELAY, true);
1048 if (r < 0)
1049 log_unit_warning_errno(UNIT(s), r, "SCTP_NODELAY failed: %m");
62bc4efc 1050 } else {
2ff48e98
LP
1051 r = setsockopt_int(fd, SOL_TCP, TCP_NODELAY, true);
1052 if (r < 0)
1053 log_unit_warning_errno(UNIT(s), r, "TCP_NODELAY failed: %m");
62bc4efc 1054 }
4427c3f4
SS
1055 }
1056
ec6370a2 1057 if (s->broadcast) {
2ff48e98
LP
1058 r = setsockopt_int(fd, SOL_SOCKET, SO_BROADCAST, true);
1059 if (r < 0)
1060 log_unit_warning_errno(UNIT(s), r, "SO_BROADCAST failed: %m");
ec6370a2
LP
1061 }
1062
d68af586 1063 if (s->pass_cred) {
2ff48e98
LP
1064 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
1065 if (r < 0)
1066 log_unit_warning_errno(UNIT(s), r, "SO_PASSCRED failed: %m");
d68af586
MS
1067 }
1068
54ecda32 1069 if (s->pass_sec) {
2ff48e98
LP
1070 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSSEC, true);
1071 if (r < 0)
1072 log_unit_warning_errno(UNIT(s), r, "SO_PASSSEC failed: %m");
54ecda32
LP
1073 }
1074
9e5b6496
YW
1075 if (s->priority >= 0) {
1076 r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, s->priority);
1077 if (r < 0)
1078 log_unit_warning_errno(UNIT(s), r, "SO_PRIORITY failed: %m");
1079 }
4fd5948e
LP
1080
1081 if (s->receive_buffer > 0) {
7d9eaa84 1082 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
9e5b6496
YW
1083 if (setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, s->receive_buffer) < 0) {
1084 r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, s->receive_buffer);
1085 if (r < 0)
1086 log_unit_warning_errno(UNIT(s), r, "SO_RCVBUF failed: %m");
1087 }
4fd5948e
LP
1088 }
1089
1090 if (s->send_buffer > 0) {
9e5b6496
YW
1091 if (setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, s->send_buffer) < 0) {
1092 r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, s->send_buffer);
1093 if (r < 0)
1094 log_unit_warning_errno(UNIT(s), r, "SO_SNDBUF failed: %m");
1095 }
4fd5948e
LP
1096 }
1097
9e5b6496
YW
1098 if (s->mark >= 0) {
1099 r = setsockopt_int(fd, SOL_SOCKET, SO_MARK, s->mark);
1100 if (r < 0)
1101 log_unit_warning_errno(UNIT(s), r, "SO_MARK failed: %m");
1102 }
4fd5948e 1103
9e5b6496
YW
1104 if (s->ip_tos >= 0) {
1105 r = setsockopt_int(fd, IPPROTO_IP, IP_TOS, s->ip_tos);
1106 if (r < 0)
1107 log_unit_warning_errno(UNIT(s), r, "IP_TOS failed: %m");
1108 }
4fd5948e 1109
46925ac5 1110 if (s->ip_ttl >= 0) {
d53e386d 1111 int x;
46925ac5 1112
9e5b6496 1113 r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, s->ip_ttl);
5bfcc1c6
FF
1114
1115 if (socket_ipv6_is_supported())
9e5b6496
YW
1116 x = setsockopt_int(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, s->ip_ttl);
1117 else
1118 x = -EAFNOSUPPORT;
46925ac5
LP
1119
1120 if (r < 0 && x < 0)
9e5b6496 1121 log_unit_warning_errno(UNIT(s), r, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
46925ac5 1122 }
cebf8b20
TT
1123
1124 if (s->tcp_congestion)
1125 if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0)
f2341e0a 1126 log_unit_warning_errno(UNIT(s), errno, "TCP_CONGESTION failed: %m");
0eb59ccf 1127
d53e386d 1128 if (s->smack_ip_in) {
5ab58c20 1129 r = mac_smack_apply_fd(fd, SMACK_ATTR_IPIN, s->smack_ip_in);
d53e386d 1130 if (r < 0)
f2341e0a 1131 log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_in_fd: %m");
d53e386d 1132 }
9a4e038c 1133
d53e386d 1134 if (s->smack_ip_out) {
5ab58c20 1135 r = mac_smack_apply_fd(fd, SMACK_ATTR_IPOUT, s->smack_ip_out);
d53e386d 1136 if (r < 0)
f2341e0a 1137 log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_out_fd: %m");
d53e386d 1138 }
4fd5948e
LP
1139}
1140
b15bdda8 1141static void socket_apply_fifo_options(Socket *s, int fd) {
d53e386d
LP
1142 int r;
1143
4fd5948e
LP
1144 assert(s);
1145 assert(fd >= 0);
1146
1147 if (s->pipe_size > 0)
1148 if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0)
ed460b07 1149 log_unit_warning_errno(UNIT(s), errno, "Setting pipe size failed, ignoring: %m");
0eb59ccf 1150
d53e386d 1151 if (s->smack) {
5ab58c20 1152 r = mac_smack_apply_fd(fd, SMACK_ATTR_ACCESS, s->smack);
d53e386d 1153 if (r < 0)
ed460b07 1154 log_unit_error_errno(UNIT(s), r, "SMACK relabelling failed, ignoring: %m");
d53e386d 1155 }
4fd5948e
LP
1156}
1157
b15bdda8
LP
1158static int fifo_address_create(
1159 const char *path,
1160 mode_t directory_mode,
e8da24a6 1161 mode_t socket_mode) {
b15bdda8 1162
e8da24a6 1163 _cleanup_close_ int fd = -1;
b15bdda8 1164 mode_t old_mask;
e8da24a6
LP
1165 struct stat st;
1166 int r;
b15bdda8
LP
1167
1168 assert(path);
b15bdda8 1169
1af87ab7 1170 (void) mkdir_parents_label(path, directory_mode);
b15bdda8 1171
ecabcf8b 1172 r = mac_selinux_create_file_prepare(path, S_IFIFO);
e9a5ef7c 1173 if (r < 0)
e8da24a6 1174 return r;
b15bdda8
LP
1175
1176 /* Enforce the right access mode for the fifo */
7be9df7d 1177 old_mask = umask(~socket_mode);
b15bdda8
LP
1178
1179 /* Include the original umask in our mask */
e8da24a6 1180 (void) umask(~socket_mode | old_mask);
b15bdda8
LP
1181
1182 r = mkfifo(path, socket_mode);
e8da24a6 1183 (void) umask(old_mask);
b15bdda8 1184
94bc2731 1185 if (r < 0 && errno != EEXIST) {
b15bdda8
LP
1186 r = -errno;
1187 goto fail;
1188 }
1189
e8da24a6 1190 fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
3cc2aff1 1191 if (fd < 0) {
b15bdda8
LP
1192 r = -errno;
1193 goto fail;
1194 }
1195
ecabcf8b 1196 mac_selinux_create_file_clear();
7a58bfa4 1197
b15bdda8
LP
1198 if (fstat(fd, &st) < 0) {
1199 r = -errno;
1200 goto fail;
1201 }
1202
1203 if (!S_ISFIFO(st.st_mode) ||
de0200fc 1204 (st.st_mode & 0777) != (socket_mode & ~old_mask) ||
e4f44e73
DR
1205 st.st_uid != getuid() ||
1206 st.st_gid != getgid()) {
b15bdda8
LP
1207 r = -EEXIST;
1208 goto fail;
1209 }
1210
c10d6bdb 1211 return TAKE_FD(fd);
b15bdda8
LP
1212
1213fail:
ecabcf8b 1214 mac_selinux_create_file_clear();
b15bdda8
LP
1215 return r;
1216}
1217
55301ec0 1218static int special_address_create(const char *path, bool writable) {
e8da24a6 1219 _cleanup_close_ int fd = -1;
b0a3f2bc
LP
1220 struct stat st;
1221
1222 assert(path);
b0a3f2bc 1223
55301ec0 1224 fd = open(path, (writable ? O_RDWR : O_RDONLY)|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
e8da24a6
LP
1225 if (fd < 0)
1226 return -errno;
b0a3f2bc 1227
e8da24a6
LP
1228 if (fstat(fd, &st) < 0)
1229 return -errno;
b0a3f2bc
LP
1230
1231 /* Check whether this is a /proc, /sys or /dev file or char device */
e8da24a6
LP
1232 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode))
1233 return -EEXIST;
b0a3f2bc 1234
c10d6bdb 1235 return TAKE_FD(fd);
b0a3f2bc
LP
1236}
1237
36078102 1238static int usbffs_address_create(const char *path) {
60252446
PS
1239 _cleanup_close_ int fd = -1;
1240 struct stat st;
1241
1242 assert(path);
60252446
PS
1243
1244 fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
1245 if (fd < 0)
1246 return -errno;
1247
1248 if (fstat(fd, &st) < 0)
1249 return -errno;
1250
13e785f7 1251 /* Check whether this is a regular file (ffs endpoint) */
60252446
PS
1252 if (!S_ISREG(st.st_mode))
1253 return -EEXIST;
1254
c10d6bdb 1255 return TAKE_FD(fd);
60252446
PS
1256}
1257
916abb21
LP
1258static int mq_address_create(
1259 const char *path,
1260 mode_t mq_mode,
1261 long maxmsg,
e8da24a6 1262 long msgsize) {
916abb21 1263
e8da24a6 1264 _cleanup_close_ int fd = -1;
916abb21
LP
1265 struct stat st;
1266 mode_t old_mask;
1267 struct mq_attr _attr, *attr = NULL;
1268
1269 assert(path);
916abb21
LP
1270
1271 if (maxmsg > 0 && msgsize > 0) {
e8da24a6
LP
1272 _attr = (struct mq_attr) {
1273 .mq_flags = O_NONBLOCK,
1274 .mq_maxmsg = maxmsg,
1275 .mq_msgsize = msgsize,
1276 };
916abb21
LP
1277 attr = &_attr;
1278 }
1279
1280 /* Enforce the right access mode for the mq */
7be9df7d 1281 old_mask = umask(~mq_mode);
916abb21
LP
1282
1283 /* Include the original umask in our mask */
e8da24a6 1284 (void) umask(~mq_mode | old_mask);
916abb21 1285 fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr);
e8da24a6 1286 (void) umask(old_mask);
916abb21 1287
e8da24a6
LP
1288 if (fd < 0)
1289 return -errno;
916abb21 1290
e8da24a6
LP
1291 if (fstat(fd, &st) < 0)
1292 return -errno;
916abb21
LP
1293
1294 if ((st.st_mode & 0777) != (mq_mode & ~old_mask) ||
1295 st.st_uid != getuid() ||
e8da24a6
LP
1296 st.st_gid != getgid())
1297 return -EEXIST;
916abb21 1298
c10d6bdb 1299 return TAKE_FD(fd);
916abb21
LP
1300}
1301
811ba7a0
LP
1302static int socket_symlink(Socket *s) {
1303 const char *p;
1304 char **i;
95f7fbbf 1305 int r;
811ba7a0
LP
1306
1307 assert(s);
1308
1309 p = socket_find_symlink_target(s);
1310 if (!p)
1311 return 0;
1312
95f7fbbf 1313 STRV_FOREACH(i, s->symlinks) {
1af87ab7
LP
1314 (void) mkdir_parents_label(*i, s->directory_mode);
1315
6c9c51e5 1316 r = symlink_idempotent(p, *i, false);
22b20752
LP
1317
1318 if (r == -EEXIST && s->remove_on_stop) {
1319 /* If there's already something where we want to create the symlink, and the destructive
1320 * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
1321 * again. */
1322
1323 if (unlink(*i) >= 0)
6c9c51e5 1324 r = symlink_idempotent(p, *i, false);
22b20752
LP
1325 }
1326
95f7fbbf
LP
1327 if (r < 0)
1328 log_unit_warning_errno(UNIT(s), r, "Failed to create symlink %s → %s, ignoring: %m", p, *i);
1329 }
811ba7a0
LP
1330
1331 return 0;
1332}
1333
36078102 1334static int usbffs_write_descs(int fd, Service *s) {
6b7e5923
PS
1335 int r;
1336
1337 if (!s->usb_function_descriptors || !s->usb_function_strings)
1338 return -EINVAL;
1339
1c876927 1340 r = copy_file_fd(s->usb_function_descriptors, fd, 0);
6b7e5923 1341 if (r < 0)
e8da24a6 1342 return r;
6b7e5923 1343
1c876927 1344 return copy_file_fd(s->usb_function_strings, fd, 0);
6b7e5923
PS
1345}
1346
36078102 1347static int usbffs_select_ep(const struct dirent *d) {
60252446
PS
1348 return d->d_name[0] != '.' && !streq(d->d_name, "ep0");
1349}
1350
36078102 1351static int usbffs_dispatch_eps(SocketPort *p) {
60252446 1352 _cleanup_free_ struct dirent **ent = NULL;
da6053d0
LP
1353 size_t n, k, i;
1354 int r;
60252446 1355
00bb64ec 1356 r = scandir(p->path, &ent, usbffs_select_ep, alphasort);
60252446
PS
1357 if (r < 0)
1358 return -errno;
1359
da6053d0 1360 n = (size_t) r;
60252446 1361 p->auxiliary_fds = new(int, n);
0de48764
YW
1362 if (!p->auxiliary_fds) {
1363 r = -ENOMEM;
1364 goto clear;
1365 }
60252446
PS
1366
1367 p->n_auxiliary_fds = n;
1368
1369 k = 0;
1370 for (i = 0; i < n; ++i) {
1371 _cleanup_free_ char *ep = NULL;
1372
00bb64ec 1373 ep = path_make_absolute(ent[i]->d_name, p->path);
0de48764
YW
1374 if (!ep) {
1375 r = -ENOMEM;
1376 goto fail;
1377 }
60252446 1378
858d36c1 1379 path_simplify(ep, false);
60252446 1380
36078102 1381 r = usbffs_address_create(ep);
60252446
PS
1382 if (r < 0)
1383 goto fail;
1384
da6053d0 1385 p->auxiliary_fds[k++] = r;
60252446
PS
1386 }
1387
0de48764
YW
1388 r = 0;
1389 goto clear;
60252446
PS
1390
1391fail:
e8da24a6 1392 close_many(p->auxiliary_fds, k);
60252446
PS
1393 p->auxiliary_fds = mfree(p->auxiliary_fds);
1394 p->n_auxiliary_fds = 0;
1395
0de48764
YW
1396clear:
1397 for (i = 0; i < n; ++i)
1398 free(ent[i]);
1399
60252446
PS
1400 return r;
1401}
1402
d24e561d 1403static int socket_determine_selinux_label(Socket *s, char **ret) {
416be1a0 1404 Service *service;
d24e561d 1405 ExecCommand *c;
2ef044ea 1406 _cleanup_free_ char *path = NULL;
d24e561d
LP
1407 int r;
1408
1409 assert(s);
1410 assert(ret);
1411
1412 if (s->selinux_context_from_net) {
1413 /* If this is requested, get label from the network label */
1414
1415 r = mac_selinux_get_our_label(ret);
1416 if (r == -EOPNOTSUPP)
1417 goto no_label;
1418
1419 } else {
1420 /* Otherwise, get it from the executable we are about to start */
1421 r = socket_instantiate_service(s);
1422 if (r < 0)
1423 return r;
1424
1425 if (!UNIT_ISSET(s->service))
1426 goto no_label;
1427
416be1a0
FB
1428 service = SERVICE(UNIT_DEREF(s->service));
1429 c = service->exec_command[SERVICE_EXEC_START];
d24e561d
LP
1430 if (!c)
1431 goto no_label;
1432
2ef044ea
FB
1433 r = chase_symlinks(c->path, service->exec_context.root_directory, CHASE_PREFIX_ROOT, &path);
1434 if (r < 0)
1435 goto no_label;
1436
416be1a0 1437 r = mac_selinux_get_create_label_from_exe(path, ret);
4c701096 1438 if (IN_SET(r, -EPERM, -EOPNOTSUPP))
d24e561d
LP
1439 goto no_label;
1440 }
1441
1442 return r;
1443
1444no_label:
1445 *ret = NULL;
1446 return 0;
1447}
1448
a79279c7
LP
1449static int socket_address_listen_do(
1450 Socket *s,
1451 const SocketAddress *address,
1452 const char *label) {
1453
1454 assert(s);
1455 assert(address);
1456
1457 return socket_address_listen(
1458 address,
1459 SOCK_CLOEXEC|SOCK_NONBLOCK,
1460 s->backlog,
1461 s->bind_ipv6_only,
1462 s->bind_to_device,
1463 s->reuse_port,
1464 s->free_bind,
1465 s->transparent,
1466 s->directory_mode,
1467 s->socket_mode,
1468 label);
1469}
1470
65486032
YW
1471#define log_address_error_errno(u, address, error, fmt) \
1472 ({ \
1473 _cleanup_free_ char *_t = NULL; \
1474 \
1475 (void) socket_address_print(address, &_t); \
1476 log_unit_error_errno(u, error, fmt, strna(_t)); \
1477 })
ae05e1b6 1478
7619cb32
LP
1479static int fork_needed(const SocketAddress *address, const ExecContext *context) {
1480 int r;
1481
1482 assert(address);
1483 assert(context);
1484
1485 /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */
1486
1487 if (IN_SET(address->sockaddr.sa.sa_family, AF_INET, AF_INET6)) {
1488 r = bpf_firewall_supported();
1489 if (r < 0)
1490 return r;
1491 if (r != BPF_FIREWALL_UNSUPPORTED) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1492 return true;
1493 }
1494
1495 return context->private_network || context->network_namespace_path;
1496}
1497
a79279c7
LP
1498static int socket_address_listen_in_cgroup(
1499 Socket *s,
1500 const SocketAddress *address,
1501 const char *label) {
1502
1503 _cleanup_close_pair_ int pair[2] = { -1, -1 };
1504 int fd, r;
1505 pid_t pid;
1506
1507 assert(s);
1508 assert(address);
1509
7619cb32
LP
1510 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the
1511 * socket's cgroup and network namespace in which the socket is actually created. This way we ensure
1512 * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and
1513 * such. */
a79279c7 1514
7619cb32 1515 r = fork_needed(address, &s->exec_context);
a79279c7
LP
1516 if (r < 0)
1517 return r;
7619cb32
LP
1518 if (r == 0) {
1519 /* Shortcut things... */
1520 fd = socket_address_listen_do(s, address, label);
1521 if (fd < 0)
1522 return log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m");
1523
1524 return fd;
1525 }
1526
1527 r = unit_setup_exec_runtime(UNIT(s));
1528 if (r < 0)
1529 return log_unit_error_errno(UNIT(s), r, "Failed acquire runtime: %m");
1530
1531 if (s->exec_context.network_namespace_path &&
1532 s->exec_runtime &&
1533 s->exec_runtime->netns_storage_socket[0] >= 0) {
1534 r = open_netns_path(s->exec_runtime->netns_storage_socket, s->exec_context.network_namespace_path);
1535 if (r < 0)
1536 return log_unit_error_errno(UNIT(s), r, "Failed to open network namespace path %s: %m", s->exec_context.network_namespace_path);
1537 }
a79279c7
LP
1538
1539 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0)
1540 return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m");
1541
4c253ed1 1542 r = unit_fork_helper_process(UNIT(s), "(sd-listen)", &pid);
a79279c7
LP
1543 if (r < 0)
1544 return log_unit_error_errno(UNIT(s), r, "Failed to fork off listener stub process: %m");
1545 if (r == 0) {
1546 /* Child */
1547
1548 pair[0] = safe_close(pair[0]);
1549
7619cb32
LP
1550 if ((s->exec_context.private_network || s->exec_context.network_namespace_path) &&
1551 s->exec_runtime &&
1552 s->exec_runtime->netns_storage_socket[0] >= 0) {
1553
1554 if (ns_type_supported(NAMESPACE_NET)) {
1555 r = setup_netns(s->exec_runtime->netns_storage_socket);
1556 if (r < 0) {
1557 log_unit_error_errno(UNIT(s), r, "Failed to join network namespace: %m");
1558 _exit(EXIT_NETWORK);
1559 }
1560 } else if (s->exec_context.network_namespace_path) {
1561 log_unit_error(UNIT(s), "Network namespace path configured but network namespaces not supported.");
1562 _exit(EXIT_NETWORK);
1563 } else
1564 log_unit_warning(UNIT(s), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring.");
1565 }
1566
a79279c7
LP
1567 fd = socket_address_listen_do(s, address, label);
1568 if (fd < 0) {
ae05e1b6 1569 log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m");
a79279c7
LP
1570 _exit(EXIT_FAILURE);
1571 }
1572
1573 r = send_one_fd(pair[1], fd, 0);
1574 if (r < 0) {
ae05e1b6 1575 log_address_error_errno(UNIT(s), address, r, "Failed to send listening socket (%s) to parent: %m");
a79279c7
LP
1576 _exit(EXIT_FAILURE);
1577 }
1578
1579 _exit(EXIT_SUCCESS);
1580 }
1581
1582 pair[1] = safe_close(pair[1]);
1583 fd = receive_one_fd(pair[0], 0);
1584
1585 /* We synchronously wait for the helper, as it shouldn't be slow */
d2e0ac3d 1586 r = wait_for_terminate_and_check("(sd-listen)", pid, WAIT_LOG_ABNORMAL);
a79279c7
LP
1587 if (r < 0) {
1588 safe_close(fd);
1589 return r;
1590 }
1591
1592 if (fd < 0)
ae05e1b6 1593 return log_address_error_errno(UNIT(s), address, fd, "Failed to receive listening socket (%s): %m");
a79279c7
LP
1594
1595 return fd;
a79279c7
LP
1596}
1597
e5417345
YW
1598DEFINE_TRIVIAL_CLEANUP_FUNC(Socket *, socket_close_fds);
1599
1600static int socket_open_fds(Socket *_s) {
1601 _cleanup_(socket_close_fdsp) Socket *s = _s;
710a6b50
LP
1602 _cleanup_(mac_selinux_freep) char *label = NULL;
1603 bool know_label = false;
83c60c9f
LP
1604 SocketPort *p;
1605 int r;
1606
1607 assert(s);
1608
034c6ed7 1609 LIST_FOREACH(port, p, s->ports) {
83c60c9f 1610
034c6ed7
LP
1611 if (p->fd >= 0)
1612 continue;
83c60c9f 1613
00411a13
LP
1614 switch (p->type) {
1615
1616 case SOCKET_SOCKET:
049f8642 1617
7f416dae 1618 if (!know_label) {
d24e561d
LP
1619 /* Figure out label, if we don't it know yet. We do it once, for the first socket where
1620 * we need this and remember it for the rest. */
7f416dae 1621
d24e561d
LP
1622 r = socket_determine_selinux_label(s, &label);
1623 if (r < 0)
ae05e1b6 1624 return log_unit_error_errno(UNIT(s), r, "Failed to determine SELinux label: %m");
049f8642
LP
1625
1626 know_label = true;
1627 }
1628
74bb646e 1629 /* Apply the socket protocol */
d24e561d 1630 switch (p->address.type) {
d2a50e3b 1631
74bb646e
SS
1632 case SOCK_STREAM:
1633 case SOCK_SEQPACKET:
d24e561d
LP
1634 if (s->socket_protocol == IPPROTO_SCTP)
1635 p->address.protocol = s->socket_protocol;
74bb646e 1636 break;
d2a50e3b 1637
74bb646e 1638 case SOCK_DGRAM:
d24e561d
LP
1639 if (s->socket_protocol == IPPROTO_UDPLITE)
1640 p->address.protocol = s->socket_protocol;
74bb646e
SS
1641 break;
1642 }
1643
d501e52b
YW
1644 p->fd = socket_address_listen_in_cgroup(s, &p->address, label);
1645 if (p->fd < 0)
1646 return p->fd;
83c60c9f 1647
4fd5948e 1648 socket_apply_socket_options(s, p->fd);
811ba7a0 1649 socket_symlink(s);
00411a13 1650 break;
4fd5948e 1651
00411a13 1652 case SOCKET_SPECIAL:
b0a3f2bc 1653
d501e52b
YW
1654 p->fd = special_address_create(p->path, s->writable);
1655 if (p->fd < 0)
1656 return log_unit_error_errno(UNIT(s), p->fd, "Failed to open special file %s: %m", p->path);
00411a13 1657 break;
b0a3f2bc 1658
00411a13 1659 case SOCKET_FIFO:
83c60c9f 1660
d501e52b 1661 p->fd = fifo_address_create(
175a3d25
LP
1662 p->path,
1663 s->directory_mode,
e8da24a6 1664 s->socket_mode);
d501e52b
YW
1665 if (p->fd < 0)
1666 return log_unit_error_errno(UNIT(s), p->fd, "Failed to open FIFO %s: %m", p->path);
83c60c9f 1667
b15bdda8 1668 socket_apply_fifo_options(s, p->fd);
811ba7a0 1669 socket_symlink(s);
00411a13 1670 break;
811ba7a0 1671
00411a13 1672 case SOCKET_MQUEUE:
83c60c9f 1673
d501e52b 1674 p->fd = mq_address_create(
175a3d25
LP
1675 p->path,
1676 s->socket_mode,
1677 s->mq_maxmsg,
e8da24a6 1678 s->mq_msgsize);
d501e52b
YW
1679 if (p->fd < 0)
1680 return log_unit_error_errno(UNIT(s), p->fd, "Failed to open message queue %s: %m", p->path);
00411a13 1681 break;
e8da24a6 1682
d2a50e3b 1683 case SOCKET_USB_FUNCTION: {
27a6ea91 1684 _cleanup_free_ char *ep = NULL;
60252446 1685
27a6ea91
GB
1686 ep = path_make_absolute("ep0", p->path);
1687
d501e52b
YW
1688 p->fd = usbffs_address_create(ep);
1689 if (p->fd < 0)
1690 return p->fd;
60252446 1691
36078102 1692 r = usbffs_write_descs(p->fd, SERVICE(UNIT_DEREF(s->service)));
6b7e5923 1693 if (r < 0)
e5417345 1694 return r;
6b7e5923 1695
36078102 1696 r = usbffs_dispatch_eps(p);
60252446 1697 if (r < 0)
e5417345 1698 return r;
00411a13
LP
1699
1700 break;
27a6ea91 1701 }
00411a13 1702 default:
b15bdda8 1703 assert_not_reached("Unknown port type");
00411a13 1704 }
034c6ed7
LP
1705 }
1706
e5417345 1707 s = NULL;
034c6ed7 1708 return 0;
034c6ed7
LP
1709}
1710
1711static void socket_unwatch_fds(Socket *s) {
1712 SocketPort *p;
718db961 1713 int r;
9152c765 1714
034c6ed7
LP
1715 assert(s);
1716
1717 LIST_FOREACH(port, p, s->ports) {
1718 if (p->fd < 0)
1719 continue;
1720
a4152e3f
LP
1721 if (!p->event_source)
1722 continue;
1723
1724 r = sd_event_source_set_enabled(p->event_source, SD_EVENT_OFF);
1725 if (r < 0)
f2341e0a 1726 log_unit_debug_errno(UNIT(s), r, "Failed to disable event source: %m");
83c60c9f 1727 }
034c6ed7
LP
1728}
1729
1730static int socket_watch_fds(Socket *s) {
1731 SocketPort *p;
1732 int r;
1733
1734 assert(s);
83c60c9f 1735
034c6ed7
LP
1736 LIST_FOREACH(port, p, s->ports) {
1737 if (p->fd < 0)
1738 continue;
1739
cbf60d0a 1740 if (p->event_source) {
718db961 1741 r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON);
cbf60d0a
LP
1742 if (r < 0)
1743 goto fail;
1744 } else {
151b9b96 1745 r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p);
cbf60d0a
LP
1746 if (r < 0)
1747 goto fail;
4f2d528d 1748
cbf60d0a 1749 (void) sd_event_source_set_description(p->event_source, "socket-port-io");
718db961 1750 }
034c6ed7 1751 }
83c60c9f 1752
542563ba 1753 return 0;
83c60c9f 1754
034c6ed7 1755fail:
cbf60d0a 1756 log_unit_warning_errno(UNIT(s), r, "Failed to watch listening fds: %m");
034c6ed7
LP
1757 socket_unwatch_fds(s);
1758 return r;
1759}
1760
01a8b467
LP
1761enum {
1762 SOCKET_OPEN_NONE,
1763 SOCKET_OPEN_SOME,
1764 SOCKET_OPEN_ALL,
1765};
1766
1767static int socket_check_open(Socket *s) {
1768 bool have_open = false, have_closed = false;
1769 SocketPort *p;
1770
1771 assert(s);
1772
1773 LIST_FOREACH(port, p, s->ports) {
1774 if (p->fd < 0)
1775 have_closed = true;
1776 else
1777 have_open = true;
1778
1779 if (have_open && have_closed)
1780 return SOCKET_OPEN_SOME;
1781 }
1782
1783 if (have_open)
1784 return SOCKET_OPEN_ALL;
1785
1786 return SOCKET_OPEN_NONE;
1787}
1788
034c6ed7
LP
1789static void socket_set_state(Socket *s, SocketState state) {
1790 SocketState old_state;
1791 assert(s);
1792
6fcbec6f
LP
1793 if (s->state != state)
1794 bus_unit_send_pending_change_signal(UNIT(s), false);
1795
034c6ed7
LP
1796 old_state = s->state;
1797 s->state = state;
1798
bd1fe7c7
LP
1799 if (!IN_SET(state,
1800 SOCKET_START_PRE,
3900e5fd 1801 SOCKET_START_CHOWN,
bd1fe7c7
LP
1802 SOCKET_START_POST,
1803 SOCKET_STOP_PRE,
1804 SOCKET_STOP_PRE_SIGTERM,
1805 SOCKET_STOP_PRE_SIGKILL,
1806 SOCKET_STOP_POST,
1807 SOCKET_FINAL_SIGTERM,
c968d76a
YW
1808 SOCKET_FINAL_SIGKILL,
1809 SOCKET_CLEANING)) {
718db961
LP
1810
1811 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
5e94833f 1812 socket_unwatch_control_pid(s);
034c6ed7 1813 s->control_command = NULL;
a16e1123 1814 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
e537352b 1815 }
034c6ed7 1816
a16e1123
LP
1817 if (state != SOCKET_LISTENING)
1818 socket_unwatch_fds(s);
1819
bd1fe7c7 1820 if (!IN_SET(state,
3900e5fd 1821 SOCKET_START_CHOWN,
bd1fe7c7
LP
1822 SOCKET_START_POST,
1823 SOCKET_LISTENING,
1824 SOCKET_RUNNING,
1825 SOCKET_STOP_PRE,
1826 SOCKET_STOP_PRE_SIGTERM,
c968d76a
YW
1827 SOCKET_STOP_PRE_SIGKILL,
1828 SOCKET_CLEANING))
034c6ed7
LP
1829 socket_close_fds(s);
1830
e537352b 1831 if (state != old_state)
f2341e0a 1832 log_unit_debug(UNIT(s), "Changed %s -> %s", socket_state_to_string(old_state), socket_state_to_string(state));
acbb0225 1833
2ad2e41a 1834 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0);
034c6ed7
LP
1835}
1836
be847e82 1837static int socket_coldplug(Unit *u) {
a16e1123
LP
1838 Socket *s = SOCKET(u);
1839 int r;
1840
1841 assert(s);
1842 assert(s->state == SOCKET_DEAD);
1843
e821075a
LP
1844 if (s->deserialized_state == s->state)
1845 return 0;
a16e1123 1846
c386f588
LP
1847 if (s->control_pid > 0 &&
1848 pid_is_unwaited(s->control_pid) &&
1849 IN_SET(s->deserialized_state,
3900e5fd
LP
1850 SOCKET_START_PRE,
1851 SOCKET_START_CHOWN,
1852 SOCKET_START_POST,
1853 SOCKET_STOP_PRE,
1854 SOCKET_STOP_PRE_SIGTERM,
1855 SOCKET_STOP_PRE_SIGKILL,
1856 SOCKET_STOP_POST,
1857 SOCKET_FINAL_SIGTERM,
c968d76a
YW
1858 SOCKET_FINAL_SIGKILL,
1859 SOCKET_CLEANING)) {
a16e1123 1860
f75f613d 1861 r = unit_watch_pid(UNIT(s), s->control_pid, false);
e821075a
LP
1862 if (r < 0)
1863 return r;
a16e1123 1864
36c16a7c 1865 r = socket_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
e821075a
LP
1866 if (r < 0)
1867 return r;
1868 }
a16e1123 1869
3900e5fd
LP
1870 if (IN_SET(s->deserialized_state,
1871 SOCKET_START_CHOWN,
1872 SOCKET_START_POST,
1873 SOCKET_LISTENING,
01a8b467
LP
1874 SOCKET_RUNNING)) {
1875
1876 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
5238e957 1877 * for. However, this is problematic, as we won't traverse through the SOCKET_START_CHOWN state for
01a8b467
LP
1878 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1879 * and if there's a mismatch, warn loudly. */
1880
1881 r = socket_check_open(s);
1882 if (r == SOCKET_OPEN_NONE)
1883 log_unit_warning(UNIT(s),
1884 "Socket unit configuration has changed while unit has been running, "
1885 "no open socket file descriptor left. "
1886 "The socket unit is not functional until restarted.");
1887 else if (r == SOCKET_OPEN_SOME)
1888 log_unit_warning(UNIT(s),
1889 "Socket unit configuration has changed while unit has been running, "
1890 "and some socket file descriptors have not been opened yet. "
1891 "The socket unit is not fully functional until restarted.");
e821075a 1892 }
a16e1123 1893
e821075a
LP
1894 if (s->deserialized_state == SOCKET_LISTENING) {
1895 r = socket_watch_fds(s);
1896 if (r < 0)
1897 return r;
a16e1123
LP
1898 }
1899
c968d76a 1900 if (!IN_SET(s->deserialized_state, SOCKET_DEAD, SOCKET_FAILED, SOCKET_CLEANING)) {
29206d46 1901 (void) unit_setup_dynamic_creds(u);
e8a565cb
YW
1902 (void) unit_setup_exec_runtime(u);
1903 }
29206d46 1904
e821075a 1905 socket_set_state(s, s->deserialized_state);
a16e1123
LP
1906 return 0;
1907}
1908
e537352b 1909static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
3c7416b6 1910
b9c04eaf 1911 _cleanup_(exec_params_clear) ExecParameters exec_params = {
5686391b
LP
1912 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
1913 .stdin_fd = -1,
1914 .stdout_fd = -1,
1915 .stderr_fd = -1,
1916 .exec_fd = -1,
9fa95f85 1917 };
3c7416b6
LP
1918 pid_t pid;
1919 int r;
034c6ed7
LP
1920
1921 assert(s);
1922 assert(c);
1923 assert(_pid);
1924
3c7416b6 1925 r = unit_prepare_exec(UNIT(s));
29206d46
LP
1926 if (r < 0)
1927 return r;
1928
36c16a7c 1929 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
36697dc0 1930 if (r < 0)
36c16a7c 1931 return r;
034c6ed7 1932
1ad6e8b3
LP
1933 r = unit_set_exec_params(UNIT(s), &exec_params);
1934 if (r < 0)
1935 return r;
3536f49e 1936
f2341e0a
LP
1937 r = exec_spawn(UNIT(s),
1938 c,
9e2f7c11 1939 &s->exec_context,
9fa95f85 1940 &exec_params,
613b411c 1941 s->exec_runtime,
29206d46 1942 &s->dynamic_creds,
9e2f7c11 1943 &pid);
cee288ad 1944 if (r < 0)
36c16a7c 1945 return r;
9e2f7c11 1946
f75f613d 1947 r = unit_watch_pid(UNIT(s), pid, true);
9e2f7c11 1948 if (r < 0)
36c16a7c 1949 return r;
034c6ed7 1950
3900e5fd 1951 *_pid = pid;
3536f49e 1952
3900e5fd 1953 return 0;
3900e5fd
LP
1954}
1955
1956static int socket_chown(Socket *s, pid_t *_pid) {
1957 pid_t pid;
1958 int r;
1959
36c16a7c 1960 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
3900e5fd
LP
1961 if (r < 0)
1962 goto fail;
1963
1964 /* We have to resolve the user names out-of-process, hence
1965 * let's fork here. It's messy, but well, what can we do? */
1966
4c253ed1 1967 r = unit_fork_helper_process(UNIT(s), "(sd-chown)", &pid);
a79279c7
LP
1968 if (r < 0)
1969 return r;
1970 if (r == 0) {
fed1e721
LP
1971 uid_t uid = UID_INVALID;
1972 gid_t gid = GID_INVALID;
a79279c7 1973 SocketPort *p;
3900e5fd 1974
a79279c7 1975 /* Child */
3900e5fd
LP
1976
1977 if (!isempty(s->user)) {
1978 const char *user = s->user;
1979
fafff8f1 1980 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
3900e5fd 1981 if (r < 0) {
a79279c7
LP
1982 log_unit_error_errno(UNIT(s), r, "Failed to resolve user %s: %m", user);
1983 _exit(EXIT_USER);
3900e5fd
LP
1984 }
1985 }
1986
1987 if (!isempty(s->group)) {
1988 const char *group = s->group;
1989
fafff8f1 1990 r = get_group_creds(&group, &gid, 0);
3900e5fd 1991 if (r < 0) {
a79279c7
LP
1992 log_unit_error_errno(UNIT(s), r, "Failed to resolve group %s: %m", group);
1993 _exit(EXIT_GROUP);
3900e5fd
LP
1994 }
1995 }
1996
1997 LIST_FOREACH(port, p, s->ports) {
e5a1c18d 1998 const char *path = NULL;
3900e5fd
LP
1999
2000 if (p->type == SOCKET_SOCKET)
2001 path = socket_address_get_path(&p->address);
2002 else if (p->type == SOCKET_FIFO)
2003 path = p->path;
2004
2005 if (!path)
2006 continue;
2007
2008 if (chown(path, uid, gid) < 0) {
a79279c7
LP
2009 log_unit_error_errno(UNIT(s), errno, "Failed to chown(): %m");
2010 _exit(EXIT_CHOWN);
3900e5fd
LP
2011 }
2012 }
2013
a79279c7 2014 _exit(EXIT_SUCCESS);
3900e5fd
LP
2015 }
2016
f75f613d 2017 r = unit_watch_pid(UNIT(s), pid, true);
718db961 2018 if (r < 0)
034c6ed7 2019 goto fail;
83c60c9f 2020
034c6ed7 2021 *_pid = pid;
034c6ed7
LP
2022 return 0;
2023
2024fail:
718db961 2025 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
83c60c9f 2026 return r;
542563ba
LP
2027}
2028
cfc4eb4c 2029static void socket_enter_dead(Socket *s, SocketResult f) {
034c6ed7
LP
2030 assert(s);
2031
a0fef983 2032 if (s->result == SOCKET_SUCCESS)
cfc4eb4c 2033 s->result = f;
034c6ed7 2034
523ee2d4
LP
2035 if (s->result == SOCKET_SUCCESS)
2036 unit_log_success(UNIT(s));
2037 else
7c047d74 2038 unit_log_failure(UNIT(s), socket_result_to_string(s->result));
ed77d407 2039
29206d46
LP
2040 socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD);
2041
e8a565cb 2042 s->exec_runtime = exec_runtime_unref(s->exec_runtime, true);
613b411c 2043
52a12341 2044 unit_destroy_runtime_directory(UNIT(s), &s->exec_context);
e66cf1a3 2045
00d9ef85
LP
2046 unit_unref_uid_gid(UNIT(s), true);
2047
29206d46 2048 dynamic_creds_destroy(&s->dynamic_creds);
034c6ed7
LP
2049}
2050
cfc4eb4c 2051static void socket_enter_signal(Socket *s, SocketState state, SocketResult f);
80876c20 2052
cfc4eb4c 2053static void socket_enter_stop_post(Socket *s, SocketResult f) {
034c6ed7
LP
2054 int r;
2055 assert(s);
2056
a0fef983 2057 if (s->result == SOCKET_SUCCESS)
cfc4eb4c 2058 s->result = f;
034c6ed7 2059
5e94833f 2060 socket_unwatch_control_pid(s);
a16e1123 2061 s->control_command_id = SOCKET_EXEC_STOP_POST;
3900e5fd 2062 s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST];
a16e1123 2063
3900e5fd
LP
2064 if (s->control_command) {
2065 r = socket_spawn(s, s->control_command, &s->control_pid);
2066 if (r < 0)
034c6ed7
LP
2067 goto fail;
2068
80876c20
LP
2069 socket_set_state(s, SOCKET_STOP_POST);
2070 } else
cfc4eb4c 2071 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS);
034c6ed7
LP
2072
2073 return;
2074
2075fail:
f2341e0a 2076 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m");
cfc4eb4c 2077 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
2078}
2079
a232ebcc
ZJS
2080static int state_to_kill_operation(Socket *s, SocketState state) {
2081 if (state == SOCKET_STOP_PRE_SIGTERM && unit_has_job_type(UNIT(s), JOB_RESTART))
2082 return KILL_RESTART;
2083
2084 if (state == SOCKET_FINAL_SIGTERM)
2085 return KILL_TERMINATE;
2086
2087 return KILL_KILL;
2088}
2089
cfc4eb4c 2090static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
034c6ed7
LP
2091 int r;
2092
2093 assert(s);
2094
a0fef983 2095 if (s->result == SOCKET_SUCCESS)
cfc4eb4c 2096 s->result = f;
034c6ed7 2097
cd2086fe
LP
2098 r = unit_kill_context(
2099 UNIT(s),
2100 &s->kill_context,
a232ebcc 2101 state_to_kill_operation(s, state),
cd2086fe
LP
2102 -1,
2103 s->control_pid,
2104 false);
2105 if (r < 0)
2106 goto fail;
034c6ed7 2107
cd2086fe 2108 if (r > 0) {
36c16a7c 2109 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
36697dc0 2110 if (r < 0)
80876c20 2111 goto fail;
d6ea93e3 2112
80876c20 2113 socket_set_state(s, state);
ac84d1fb
LP
2114 } else if (state == SOCKET_STOP_PRE_SIGTERM)
2115 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS);
2116 else if (state == SOCKET_STOP_PRE_SIGKILL)
cfc4eb4c 2117 socket_enter_stop_post(s, SOCKET_SUCCESS);
ac84d1fb
LP
2118 else if (state == SOCKET_FINAL_SIGTERM)
2119 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS);
80876c20 2120 else
cfc4eb4c 2121 socket_enter_dead(s, SOCKET_SUCCESS);
034c6ed7
LP
2122
2123 return;
2124
2125fail:
f2341e0a 2126 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
034c6ed7 2127
3742095b 2128 if (IN_SET(state, SOCKET_STOP_PRE_SIGTERM, SOCKET_STOP_PRE_SIGKILL))
cfc4eb4c 2129 socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
034c6ed7 2130 else
cfc4eb4c 2131 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
2132}
2133
cfc4eb4c 2134static void socket_enter_stop_pre(Socket *s, SocketResult f) {
034c6ed7
LP
2135 int r;
2136 assert(s);
2137
a0fef983 2138 if (s->result == SOCKET_SUCCESS)
cfc4eb4c 2139 s->result = f;
034c6ed7 2140
5e94833f 2141 socket_unwatch_control_pid(s);
a16e1123 2142 s->control_command_id = SOCKET_EXEC_STOP_PRE;
3900e5fd 2143 s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE];
a16e1123 2144
3900e5fd
LP
2145 if (s->control_command) {
2146 r = socket_spawn(s, s->control_command, &s->control_pid);
2147 if (r < 0)
034c6ed7
LP
2148 goto fail;
2149
80876c20
LP
2150 socket_set_state(s, SOCKET_STOP_PRE);
2151 } else
cfc4eb4c 2152 socket_enter_stop_post(s, SOCKET_SUCCESS);
034c6ed7
LP
2153
2154 return;
2155
2156fail:
f2341e0a 2157 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-pre' task: %m");
cfc4eb4c 2158 socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
2159}
2160
e9af15c3
LP
2161static void socket_enter_listening(Socket *s) {
2162 int r;
2163 assert(s);
2164
cfc4eb4c
LP
2165 r = socket_watch_fds(s);
2166 if (r < 0) {
f2341e0a 2167 log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m");
e9af15c3
LP
2168 goto fail;
2169 }
2170
2171 socket_set_state(s, SOCKET_LISTENING);
2172 return;
2173
2174fail:
cfc4eb4c 2175 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
e9af15c3
LP
2176}
2177
034c6ed7
LP
2178static void socket_enter_start_post(Socket *s) {
2179 int r;
2180 assert(s);
2181
3900e5fd
LP
2182 socket_unwatch_control_pid(s);
2183 s->control_command_id = SOCKET_EXEC_START_POST;
2184 s->control_command = s->exec_command[SOCKET_EXEC_START_POST];
2185
2186 if (s->control_command) {
2187 r = socket_spawn(s, s->control_command, &s->control_pid);
2188 if (r < 0) {
f2341e0a 2189 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m");
3900e5fd
LP
2190 goto fail;
2191 }
2192
2193 socket_set_state(s, SOCKET_START_POST);
2194 } else
2195 socket_enter_listening(s);
2196
2197 return;
2198
2199fail:
2200 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
2201}
2202
2203static void socket_enter_start_chown(Socket *s) {
2204 int r;
2205
2206 assert(s);
2207
cfc4eb4c
LP
2208 r = socket_open_fds(s);
2209 if (r < 0) {
f2341e0a 2210 log_unit_warning_errno(UNIT(s), r, "Failed to listen on sockets: %m");
034c6ed7
LP
2211 goto fail;
2212 }
2213
3900e5fd 2214 if (!isempty(s->user) || !isempty(s->group)) {
5e94833f 2215
3900e5fd
LP
2216 socket_unwatch_control_pid(s);
2217 s->control_command_id = SOCKET_EXEC_START_CHOWN;
2218 s->control_command = NULL;
a16e1123 2219
3900e5fd 2220 r = socket_chown(s, &s->control_pid);
cfc4eb4c 2221 if (r < 0) {
f2341e0a 2222 log_unit_warning_errno(UNIT(s), r, "Failed to fork 'start-chown' task: %m");
034c6ed7
LP
2223 goto fail;
2224 }
2225
3900e5fd 2226 socket_set_state(s, SOCKET_START_CHOWN);
80876c20 2227 } else
3900e5fd 2228 socket_enter_start_post(s);
034c6ed7
LP
2229
2230 return;
2231
2232fail:
cfc4eb4c 2233 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
2234}
2235
2236static void socket_enter_start_pre(Socket *s) {
2237 int r;
2238 assert(s);
2239
5e94833f 2240 socket_unwatch_control_pid(s);
a4634b21
LP
2241
2242 unit_warn_leftover_processes(UNIT(s));
2243
a16e1123 2244 s->control_command_id = SOCKET_EXEC_START_PRE;
3900e5fd 2245 s->control_command = s->exec_command[SOCKET_EXEC_START_PRE];
a16e1123 2246
3900e5fd 2247 if (s->control_command) {
e821075a 2248 r = socket_spawn(s, s->control_command, &s->control_pid);
3900e5fd 2249 if (r < 0) {
f2341e0a 2250 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m");
034c6ed7 2251 goto fail;
3900e5fd 2252 }
034c6ed7 2253
80876c20
LP
2254 socket_set_state(s, SOCKET_START_PRE);
2255 } else
3900e5fd 2256 socket_enter_start_chown(s);
034c6ed7
LP
2257
2258 return;
2259
2260fail:
cfc4eb4c 2261 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
2262}
2263
60d9771c
LP
2264static void flush_ports(Socket *s) {
2265 SocketPort *p;
2266
2267 /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy
2268 * anymore */
2269
2270 LIST_FOREACH(port, p, s->ports) {
2271 if (p->fd < 0)
2272 continue;
2273
2274 (void) flush_accept(p->fd);
2275 (void) flush_fd(p->fd);
2276 }
2277}
2278
4f2d528d 2279static void socket_enter_running(Socket *s, int cfd) {
4afd3348 2280 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
034c6ed7
LP
2281 int r;
2282
8b26cdbd
LP
2283 /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or
2284 * close it. */
2285
034c6ed7
LP
2286 assert(s);
2287
60d9771c 2288 /* We don't take connections anymore if we are supposed to shut down anyway */
31afa0a4 2289 if (unit_stop_pending(UNIT(s))) {
e821075a 2290
f2341e0a 2291 log_unit_debug(UNIT(s), "Suppressing connection request since unit stop is scheduled.");
5d909e3e 2292
7c610628 2293 if (cfd >= 0)
a98f7575 2294 goto refuse;
60d9771c
LP
2295 else
2296 flush_ports(s);
7c610628 2297
ba3e67a7
LP
2298 return;
2299 }
2300
7994ac1d 2301 if (!ratelimit_below(&s->trigger_limit)) {
8b26cdbd
LP
2302 log_unit_warning(UNIT(s), "Trigger limit hit, refusing further activation.");
2303 socket_enter_stop_pre(s, SOCKET_FAILURE_TRIGGER_LIMIT_HIT);
a98f7575 2304 goto refuse;
8b26cdbd
LP
2305 }
2306
4f2d528d 2307 if (cfd < 0) {
f976f3f6 2308 bool pending = false;
eef85c4a
LP
2309 Unit *other;
2310 Iterator i;
2311 void *v;
f976f3f6
LP
2312
2313 /* If there's already a start pending don't bother to
2314 * do anything */
eef85c4a 2315 HASHMAP_FOREACH_KEY(v, other, UNIT(s)->dependencies[UNIT_TRIGGERS], i)
e821075a 2316 if (unit_active_or_pending(other)) {
57020a3a
LP
2317 pending = true;
2318 break;
2319 }
f976f3f6 2320
1a710b43 2321 if (!pending) {
640ace4a 2322 if (!UNIT_ISSET(s->service)) {
f2341e0a 2323 log_unit_error(UNIT(s), "Service to activate vanished, refusing activation.");
640ace4a
LP
2324 r = -ENOENT;
2325 goto fail;
2326 }
2327
50cbaba4 2328 r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, NULL, &error, NULL);
1a710b43 2329 if (r < 0)
f976f3f6 2330 goto fail;
1a710b43 2331 }
4f2d528d
LP
2332
2333 socket_set_state(s, SOCKET_RUNNING);
2334 } else {
e55001eb 2335 _cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL;
9d565427 2336 _cleanup_(socket_peer_unrefp) SocketPeer *p = NULL;
b15bdda8 2337 Service *service;
4f2d528d 2338
6cf6bbc2 2339 if (s->n_connections >= s->max_connections) {
ea8f50f8 2340 log_unit_warning(UNIT(s), "Too many incoming connections (%u), dropping connection.",
166cf510 2341 s->n_connections);
a98f7575 2342 goto refuse;
6cf6bbc2
LP
2343 }
2344
9d565427 2345 if (s->max_connections_per_source > 0) {
166cf510 2346 r = socket_acquire_peer(s, cfd, &p);
9d565427 2347 if (r < 0) {
a98f7575 2348 goto refuse;
166cf510 2349 } else if (r > 0 && p->n_ref > s->max_connections_per_source) {
ea8f50f8
ZJS
2350 _cleanup_free_ char *t = NULL;
2351
41733ae1 2352 (void) sockaddr_pretty(&p->peer.sa, p->peer_salen, true, false, &t);
ea8f50f8 2353
166cf510 2354 log_unit_warning(UNIT(s),
ea8f50f8
ZJS
2355 "Too many incoming connections (%u) from source %s, dropping connection.",
2356 p->n_ref, strnull(t));
a98f7575 2357 goto refuse;
9d565427
SS
2358 }
2359 }
2360
1a710b43
MS
2361 r = socket_instantiate_service(s);
2362 if (r < 0)
b15bdda8
LP
2363 goto fail;
2364
1a710b43
MS
2365 r = instance_from_socket(cfd, s->n_accepted, &instance);
2366 if (r < 0) {
2367 if (r != -ENOTCONN)
2368 goto fail;
2369
2370 /* ENOTCONN is legitimate if TCP RST was received.
2371 * This connection is over, but the socket unit lives on. */
8b26cdbd 2372 log_unit_debug(UNIT(s), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
a98f7575 2373 goto refuse;
1a710b43 2374 }
4f2d528d 2375
7410616c
LP
2376 r = unit_name_to_prefix(UNIT(s)->id, &prefix);
2377 if (r < 0)
4f2d528d 2378 goto fail;
4f2d528d 2379
7410616c
LP
2380 r = unit_name_build(prefix, instance, ".service", &name);
2381 if (r < 0)
b6dbbe1c 2382 goto fail;
4f2d528d 2383
1a710b43 2384 r = unit_add_name(UNIT_DEREF(s->service), name);
e55001eb 2385 if (r < 0)
4f2d528d 2386 goto fail;
b15bdda8 2387
57020a3a
LP
2388 service = SERVICE(UNIT_DEREF(s->service));
2389 unit_ref_unset(&s->service);
6c073082 2390
e4f67317 2391 s->n_accepted++;
b15bdda8 2392 unit_choose_id(UNIT(service), name);
b15bdda8 2393
16115b0a 2394 r = service_set_socket_fd(service, cfd, s, s->selinux_context_from_net);
1a710b43 2395 if (r < 0)
4f2d528d
LP
2396 goto fail;
2397
8b26cdbd 2398 cfd = -1; /* We passed ownership of the fd to the service now. Forget it here. */
313cefa1 2399 s->n_connections++;
6cf6bbc2 2400
1cc6c93a 2401 service->peer = TAKE_PTR(p); /* Pass ownership of the peer reference */
9d565427 2402
50cbaba4 2403 r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, NULL, &error, NULL);
3e7a1f50
LP
2404 if (r < 0) {
2405 /* We failed to activate the new service, but it still exists. Let's make sure the service
2406 * closes and forgets the connection fd again, immediately. */
2407 service_close_socket_fd(service);
4f2d528d 2408 goto fail;
3e7a1f50 2409 }
c4e2ceae
LP
2410
2411 /* Notify clients about changed counters */
2412 unit_add_to_dbus_queue(UNIT(s));
4f2d528d 2413 }
034c6ed7 2414
034c6ed7
LP
2415 return;
2416
a98f7575
MM
2417refuse:
2418 s->n_refused++;
2419 safe_close(cfd);
2420 return;
2421
034c6ed7 2422fail:
f2341e0a
LP
2423 log_unit_warning(UNIT(s), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2424 cfd >= 0 ? "template" : "non-template",
718db961 2425 bus_error_message(&error, r));
e821075a 2426
60089004 2427 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
03e334a1 2428 safe_close(cfd);
034c6ed7
LP
2429}
2430
cfc4eb4c 2431static void socket_run_next(Socket *s) {
034c6ed7
LP
2432 int r;
2433
2434 assert(s);
2435 assert(s->control_command);
2436 assert(s->control_command->command_next);
2437
5e94833f
LP
2438 socket_unwatch_control_pid(s);
2439
034c6ed7
LP
2440 s->control_command = s->control_command->command_next;
2441
e821075a
LP
2442 r = socket_spawn(s, s->control_command, &s->control_pid);
2443 if (r < 0)
034c6ed7
LP
2444 goto fail;
2445
2446 return;
2447
2448fail:
f2341e0a 2449 log_unit_warning_errno(UNIT(s), r, "Failed to run next task: %m");
80876c20
LP
2450
2451 if (s->state == SOCKET_START_POST)
cfc4eb4c 2452 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
034c6ed7 2453 else if (s->state == SOCKET_STOP_POST)
cfc4eb4c 2454 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
034c6ed7 2455 else
cfc4eb4c 2456 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
034c6ed7
LP
2457}
2458
87f0e418
LP
2459static int socket_start(Unit *u) {
2460 Socket *s = SOCKET(u);
07299350 2461 int r;
83c60c9f
LP
2462
2463 assert(s);
2464
034c6ed7
LP
2465 /* We cannot fulfill this request right now, try again later
2466 * please! */
3900e5fd
LP
2467 if (IN_SET(s->state,
2468 SOCKET_STOP_PRE,
2469 SOCKET_STOP_PRE_SIGKILL,
2470 SOCKET_STOP_PRE_SIGTERM,
2471 SOCKET_STOP_POST,
2472 SOCKET_FINAL_SIGTERM,
c968d76a
YW
2473 SOCKET_FINAL_SIGKILL,
2474 SOCKET_CLEANING))
034c6ed7
LP
2475 return -EAGAIN;
2476
a4152e3f 2477 /* Already on it! */
3900e5fd
LP
2478 if (IN_SET(s->state,
2479 SOCKET_START_PRE,
2480 SOCKET_START_CHOWN,
2481 SOCKET_START_POST))
034c6ed7 2482 return 0;
83c60c9f 2483
034c6ed7 2484 /* Cannot run this without the service being around */
9444b1f2 2485 if (UNIT_ISSET(s->service)) {
57020a3a
LP
2486 Service *service;
2487
2488 service = SERVICE(UNIT_DEREF(s->service));
2489
1124fe6f 2490 if (UNIT(service)->load_state != UNIT_LOADED) {
f2341e0a 2491 log_unit_error(u, "Socket service %s not loaded, refusing.", UNIT(service)->id);
4f2d528d 2492 return -ENOENT;
4ac9236f 2493 }
4f2d528d 2494
35b8ca3a 2495 /* If the service is already active we cannot start the
4f2d528d 2496 * socket */
ec2ce0c5 2497 if (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) {
f2341e0a 2498 log_unit_error(u, "Socket service %s already active, refusing.", UNIT(service)->id);
4f2d528d 2499 return -EBUSY;
4ac9236f 2500 }
4f2d528d 2501 }
e537352b 2502
3742095b 2503 assert(IN_SET(s->state, SOCKET_DEAD, SOCKET_FAILED));
83c60c9f 2504
97a3f4ee 2505 r = unit_test_start_limit(u);
07299350
LP
2506 if (r < 0) {
2507 socket_enter_dead(s, SOCKET_FAILURE_START_LIMIT_HIT);
2508 return r;
2509 }
2510
4b58153d
LP
2511 r = unit_acquire_invocation_id(u);
2512 if (r < 0)
2513 return r;
2514
cfc4eb4c 2515 s->result = SOCKET_SUCCESS;
6a1d4d9f 2516 exec_command_reset_status_list_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
3c7416b6
LP
2517
2518 u->reset_accounting = true;
5ad096b3 2519
034c6ed7 2520 socket_enter_start_pre(s);
82a2b6bb 2521 return 1;
034c6ed7 2522}
83c60c9f 2523
87f0e418
LP
2524static int socket_stop(Unit *u) {
2525 Socket *s = SOCKET(u);
034c6ed7
LP
2526
2527 assert(s);
2528
e537352b 2529 /* Already on it */
3900e5fd
LP
2530 if (IN_SET(s->state,
2531 SOCKET_STOP_PRE,
2532 SOCKET_STOP_PRE_SIGTERM,
2533 SOCKET_STOP_PRE_SIGKILL,
2534 SOCKET_STOP_POST,
2535 SOCKET_FINAL_SIGTERM,
2536 SOCKET_FINAL_SIGKILL))
e537352b
LP
2537 return 0;
2538
3f6c78dc
LP
2539 /* If there's already something running we go directly into
2540 * kill mode. */
3900e5fd
LP
2541 if (IN_SET(s->state,
2542 SOCKET_START_PRE,
2543 SOCKET_START_CHOWN,
2544 SOCKET_START_POST)) {
cfc4eb4c 2545 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS);
3f6c78dc
LP
2546 return -EAGAIN;
2547 }
2548
c968d76a
YW
2549 /* If we are currently cleaning, then abort it, brutally. */
2550 if (s->state == SOCKET_CLEANING) {
2551 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS);
2552 return 0;
2553 }
2554
3742095b 2555 assert(IN_SET(s->state, SOCKET_LISTENING, SOCKET_RUNNING));
83c60c9f 2556
cfc4eb4c 2557 socket_enter_stop_pre(s, SOCKET_SUCCESS);
82a2b6bb 2558 return 1;
542563ba
LP
2559}
2560
a16e1123
LP
2561static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
2562 Socket *s = SOCKET(u);
2563 SocketPort *p;
2564 int r;
2565
2566 assert(u);
2567 assert(f);
2568 assert(fds);
2569
d68c645b
LP
2570 (void) serialize_item(f, "state", socket_state_to_string(s->state));
2571 (void) serialize_item(f, "result", socket_result_to_string(s->result));
2572 (void) serialize_item_format(f, "n-accepted", "%u", s->n_accepted);
2573 (void) serialize_item_format(f, "n-refused", "%u", s->n_refused);
a16e1123
LP
2574
2575 if (s->control_pid > 0)
d68c645b 2576 (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid);
a16e1123
LP
2577
2578 if (s->control_command_id >= 0)
d68c645b 2579 (void) serialize_item(f, "control-command", socket_exec_command_to_string(s->control_command_id));
a16e1123
LP
2580
2581 LIST_FOREACH(port, p, s->ports) {
2582 int copy;
2583
2584 if (p->fd < 0)
2585 continue;
2586
613b411c
LP
2587 copy = fdset_put_dup(fds, p->fd);
2588 if (copy < 0)
fc2d74ab 2589 return log_unit_warning_errno(u, copy, "Failed to serialize socket fd: %m");
a16e1123
LP
2590
2591 if (p->type == SOCKET_SOCKET) {
613b411c 2592 _cleanup_free_ char *t = NULL;
a16e1123 2593
ee092817
LP
2594 r = socket_address_print(&p->address, &t);
2595 if (r < 0)
fc2d74ab 2596 return log_unit_error_errno(u, r, "Failed to format socket address: %m");
a16e1123 2597
7a22745a 2598 if (socket_address_family(&p->address) == AF_NETLINK)
d68c645b 2599 (void) serialize_item_format(f, "netlink", "%i %s", copy, t);
7a22745a 2600 else
d68c645b 2601 (void) serialize_item_format(f, "socket", "%i %i %s", copy, p->address.type, t);
b0a3f2bc 2602 } else if (p->type == SOCKET_SPECIAL)
d68c645b 2603 (void) serialize_item_format(f, "special", "%i %s", copy, p->path);
ee092817 2604 else if (p->type == SOCKET_MQUEUE)
d68c645b 2605 (void) serialize_item_format(f, "mqueue", "%i %s", copy, p->path);
60252446 2606 else if (p->type == SOCKET_USB_FUNCTION)
d68c645b 2607 (void) serialize_item_format(f, "ffs", "%i %s", copy, p->path);
b0a3f2bc 2608 else {
a16e1123 2609 assert(p->type == SOCKET_FIFO);
d68c645b 2610 (void) serialize_item_format(f, "fifo", "%i %s", copy, p->path);
a16e1123
LP
2611 }
2612 }
2613
2614 return 0;
2615}
2616
80a58668 2617static void socket_port_take_fd(SocketPort *p, FDSet *fds, int fd) {
d68c645b
LP
2618 assert(p);
2619
80a58668
ZJS
2620 safe_close(p->fd);
2621 p->fd = fdset_remove(fds, fd);
2622}
2623
a16e1123
LP
2624static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2625 Socket *s = SOCKET(u);
a16e1123
LP
2626
2627 assert(u);
2628 assert(key);
2629 assert(value);
a16e1123
LP
2630
2631 if (streq(key, "state")) {
2632 SocketState state;
2633
ee092817
LP
2634 state = socket_state_from_string(value);
2635 if (state < 0)
f2341e0a 2636 log_unit_debug(u, "Failed to parse state value: %s", value);
a16e1123
LP
2637 else
2638 s->deserialized_state = state;
cfc4eb4c
LP
2639 } else if (streq(key, "result")) {
2640 SocketResult f;
a16e1123 2641
cfc4eb4c
LP
2642 f = socket_result_from_string(value);
2643 if (f < 0)
f2341e0a 2644 log_unit_debug(u, "Failed to parse result value: %s", value);
cfc4eb4c
LP
2645 else if (f != SOCKET_SUCCESS)
2646 s->result = f;
a16e1123
LP
2647
2648 } else if (streq(key, "n-accepted")) {
2649 unsigned k;
2650
e364ad06 2651 if (safe_atou(value, &k) < 0)
f2341e0a 2652 log_unit_debug(u, "Failed to parse n-accepted value: %s", value);
a16e1123
LP
2653 else
2654 s->n_accepted += k;
a98f7575
MM
2655 } else if (streq(key, "n-refused")) {
2656 unsigned k;
2657
2658 if (safe_atou(value, &k) < 0)
2659 log_unit_debug(u, "Failed to parse n-refused value: %s", value);
2660 else
2661 s->n_refused += k;
a16e1123 2662 } else if (streq(key, "control-pid")) {
5925dd3c 2663 pid_t pid;
a16e1123 2664
e364ad06 2665 if (parse_pid(value, &pid) < 0)
f2341e0a 2666 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
a16e1123 2667 else
5925dd3c 2668 s->control_pid = pid;
a16e1123
LP
2669 } else if (streq(key, "control-command")) {
2670 SocketExecCommand id;
2671
66870f90
ZJS
2672 id = socket_exec_command_from_string(value);
2673 if (id < 0)
f2341e0a 2674 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
a16e1123
LP
2675 else {
2676 s->control_command_id = id;
2677 s->control_command = s->exec_command[id];
2678 }
2679 } else if (streq(key, "fifo")) {
2680 int fd, skip = 0;
2681 SocketPort *p;
2682
2683 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2684 log_unit_debug(u, "Failed to parse fifo value: %s", value);
80a58668 2685 else
a16e1123 2686 LIST_FOREACH(port, p, s->ports)
b0a3f2bc 2687 if (p->type == SOCKET_FIFO &&
e3f791a2 2688 path_equal_or_files_same(p->path, value+skip, 0)) {
80a58668 2689 socket_port_take_fd(p, fds, fd);
b0a3f2bc 2690 break;
80a58668 2691 }
b0a3f2bc
LP
2692
2693 } else if (streq(key, "special")) {
2694 int fd, skip = 0;
2695 SocketPort *p;
2696
2697 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2698 log_unit_debug(u, "Failed to parse special value: %s", value);
80a58668 2699 else
b0a3f2bc
LP
2700 LIST_FOREACH(port, p, s->ports)
2701 if (p->type == SOCKET_SPECIAL &&
e3f791a2 2702 path_equal_or_files_same(p->path, value+skip, 0)) {
80a58668 2703 socket_port_take_fd(p, fds, fd);
a16e1123 2704 break;
80a58668 2705 }
a16e1123 2706
ee092817
LP
2707 } else if (streq(key, "mqueue")) {
2708 int fd, skip = 0;
2709 SocketPort *p;
2710
2711 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2712 log_unit_debug(u, "Failed to parse mqueue value: %s", value);
80a58668 2713 else
ee092817
LP
2714 LIST_FOREACH(port, p, s->ports)
2715 if (p->type == SOCKET_MQUEUE &&
80a58668
ZJS
2716 streq(p->path, value+skip)) {
2717 socket_port_take_fd(p, fds, fd);
ee092817 2718 break;
80a58668 2719 }
ee092817 2720
a16e1123 2721 } else if (streq(key, "socket")) {
27ca8d7a 2722 int fd, type, skip = 0;
a16e1123
LP
2723 SocketPort *p;
2724
27ca8d7a 2725 if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
f2341e0a 2726 log_unit_debug(u, "Failed to parse socket value: %s", value);
80a58668 2727 else
a16e1123 2728 LIST_FOREACH(port, p, s->ports)
80a58668
ZJS
2729 if (socket_address_is(&p->address, value+skip, type)) {
2730 socket_port_take_fd(p, fds, fd);
a16e1123 2731 break;
80a58668 2732 }
a16e1123 2733
7a22745a
LP
2734 } else if (streq(key, "netlink")) {
2735 int fd, skip = 0;
2736 SocketPort *p;
2737
2738 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2739 log_unit_debug(u, "Failed to parse socket value: %s", value);
80a58668 2740 else
7a22745a 2741 LIST_FOREACH(port, p, s->ports)
80a58668
ZJS
2742 if (socket_address_is_netlink(&p->address, value+skip)) {
2743 socket_port_take_fd(p, fds, fd);
7a22745a 2744 break;
80a58668 2745 }
60252446
PS
2746
2747 } else if (streq(key, "ffs")) {
2748 int fd, skip = 0;
2749 SocketPort *p;
2750
2751 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
2752 log_unit_debug(u, "Failed to parse ffs value: %s", value);
80a58668 2753 else
60252446
PS
2754 LIST_FOREACH(port, p, s->ports)
2755 if (p->type == SOCKET_USB_FUNCTION &&
e3f791a2 2756 path_equal_or_files_same(p->path, value+skip, 0)) {
80a58668 2757 socket_port_take_fd(p, fds, fd);
60252446 2758 break;
80a58668 2759 }
60252446 2760
a16e1123 2761 } else
f2341e0a 2762 log_unit_debug(UNIT(s), "Unknown serialization key: %s", key);
a16e1123
LP
2763
2764 return 0;
2765}
2766
9ff1a6f1 2767static void socket_distribute_fds(Unit *u, FDSet *fds) {
01e10de3
LP
2768 Socket *s = SOCKET(u);
2769 SocketPort *p;
2770
2771 assert(u);
2772
2773 LIST_FOREACH(port, p, s->ports) {
2774 Iterator i;
2775 int fd;
2776
2777 if (p->type != SOCKET_SOCKET)
2778 continue;
2779
2780 if (p->fd >= 0)
2781 continue;
2782
2783 FDSET_FOREACH(fd, fds, i) {
2784 if (socket_address_matches_fd(&p->address, fd)) {
2785 p->fd = fdset_remove(fds, fd);
2786 s->deserialized_state = SOCKET_LISTENING;
2787 break;
2788 }
2789 }
2790 }
01e10de3
LP
2791}
2792
44a6b1b6 2793_pure_ static UnitActiveState socket_active_state(Unit *u) {
87f0e418 2794 assert(u);
5cb5a6ff 2795
acbb0225 2796 return state_translation_table[SOCKET(u)->state];
5cb5a6ff
LP
2797}
2798
44a6b1b6 2799_pure_ static const char *socket_sub_state_to_string(Unit *u) {
10a94420
LP
2800 assert(u);
2801
a16e1123 2802 return socket_state_to_string(SOCKET(u)->state);
10a94420
LP
2803}
2804
67419600
OS
2805const char* socket_port_type_to_string(SocketPort *p) {
2806
2807 assert(p);
2808
2809 switch (p->type) {
718db961
LP
2810
2811 case SOCKET_SOCKET:
2812
2813 switch (p->address.type) {
2814
2815 case SOCK_STREAM:
2816 return "Stream";
2817
2818 case SOCK_DGRAM:
2819 return "Datagram";
2820
2821 case SOCK_SEQPACKET:
2822 return "SequentialPacket";
2823
2824 case SOCK_RAW:
2825 if (socket_address_family(&p->address) == AF_NETLINK)
2826 return "Netlink";
2827
4831981d 2828 _fallthrough_;
718db961
LP
2829 default:
2830 return NULL;
2831 }
2832
2833 case SOCKET_SPECIAL:
2834 return "Special";
2835
2836 case SOCKET_MQUEUE:
2837 return "MessageQueue";
2838
2839 case SOCKET_FIFO:
2840 return "FIFO";
2841
60252446
PS
2842 case SOCKET_USB_FUNCTION:
2843 return "USBFunction";
2844
718db961
LP
2845 default:
2846 return NULL;
67419600
OS
2847 }
2848}
2849
038ed5a4
YW
2850SocketType socket_port_type_from_string(const char *s) {
2851 assert(s);
2852
e045e325
YW
2853 if (STR_IN_SET(s, "Stream", "Datagram", "SequentialPacket", "Netlink"))
2854 return SOCKET_SOCKET;
2855 else if (streq(s, "Special"))
2856 return SOCKET_SPECIAL;
2857 else if (streq(s, "MessageQueue"))
2858 return SOCKET_MQUEUE;
2859 else if (streq(s, "FIFO"))
2860 return SOCKET_FIFO;
2861 else if (streq(s, "USBFunction"))
2862 return SOCKET_USB_FUNCTION;
038ed5a4
YW
2863 else
2864 return _SOCKET_TYPE_INVALID;
2865}
2866
f2f725e5 2867_pure_ static bool socket_may_gc(Unit *u) {
6cf6bbc2
LP
2868 Socket *s = SOCKET(u);
2869
2870 assert(u);
2871
f2f725e5 2872 return s->n_connections == 0;
6cf6bbc2
LP
2873}
2874
a79279c7
LP
2875static int socket_accept_do(Socket *s, int fd) {
2876 int cfd;
2877
2878 assert(s);
2879 assert(fd >= 0);
2880
4ff9bc2e
LP
2881 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
2882 if (cfd < 0)
2883 /* Convert transient network errors into clean and well-defined EAGAIN */
2884 return ERRNO_IS_ACCEPT_AGAIN(errno) ? -EAGAIN : -errno;
a79279c7
LP
2885
2886 return cfd;
2887}
2888
2889static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) {
2890 _cleanup_close_pair_ int pair[2] = { -1, -1 };
2891 int cfd, r;
2892 pid_t pid;
2893
2894 assert(s);
2895 assert(p);
2896 assert(fd >= 0);
2897
5238e957 2898 /* Similar to socket_address_listen_in_cgroup(), but for accept() rather than socket(): make sure that any
a79279c7
LP
2899 * connection socket is also properly associated with the cgroup. */
2900
2901 if (!IN_SET(p->address.sockaddr.sa.sa_family, AF_INET, AF_INET6))
2902 goto shortcut;
2903
2904 r = bpf_firewall_supported();
2905 if (r < 0)
2906 return r;
2ae7ee58 2907 if (r == BPF_FIREWALL_UNSUPPORTED)
a79279c7
LP
2908 goto shortcut;
2909
2910 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0)
2911 return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m");
2912
4c253ed1 2913 r = unit_fork_helper_process(UNIT(s), "(sd-accept)", &pid);
a79279c7
LP
2914 if (r < 0)
2915 return log_unit_error_errno(UNIT(s), r, "Failed to fork off accept stub process: %m");
2916 if (r == 0) {
2917 /* Child */
2918
2919 pair[0] = safe_close(pair[0]);
2920
2921 cfd = socket_accept_do(s, fd);
4ff9bc2e
LP
2922 if (cfd == -EAGAIN) /* spurious accept() */
2923 _exit(EXIT_SUCCESS);
a79279c7
LP
2924 if (cfd < 0) {
2925 log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
2926 _exit(EXIT_FAILURE);
2927 }
2928
2929 r = send_one_fd(pair[1], cfd, 0);
2930 if (r < 0) {
2931 log_unit_error_errno(UNIT(s), r, "Failed to send connection socket to parent: %m");
2932 _exit(EXIT_FAILURE);
2933 }
2934
2935 _exit(EXIT_SUCCESS);
2936 }
2937
2938 pair[1] = safe_close(pair[1]);
2939 cfd = receive_one_fd(pair[0], 0);
2940
2941 /* We synchronously wait for the helper, as it shouldn't be slow */
d2e0ac3d 2942 r = wait_for_terminate_and_check("(sd-accept)", pid, WAIT_LOG_ABNORMAL);
a79279c7
LP
2943 if (r < 0) {
2944 safe_close(cfd);
2945 return r;
2946 }
2947
4ff9bc2e
LP
2948 /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS
2949 * this is a spurious accept(), let's convert that back to EAGAIN here. */
2950 if (cfd == -EIO)
2951 return -EAGAIN;
a79279c7
LP
2952 if (cfd < 0)
2953 return log_unit_error_errno(UNIT(s), cfd, "Failed to receive connection socket: %m");
2954
2955 return cfd;
2956
2957shortcut:
2958 cfd = socket_accept_do(s, fd);
4ff9bc2e
LP
2959 if (cfd == -EAGAIN) /* spurious accept(), skip it silently */
2960 return -EAGAIN;
a79279c7
LP
2961 if (cfd < 0)
2962 return log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
2963
2964 return cfd;
2965}
2966
718db961
LP
2967static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2968 SocketPort *p = userdata;
4f2d528d 2969 int cfd = -1;
9152c765 2970
718db961 2971 assert(p);
8d567588 2972 assert(fd >= 0);
9152c765 2973
718db961
LP
2974 if (p->socket->state != SOCKET_LISTENING)
2975 return 0;
871d7de4 2976
f2341e0a 2977 log_unit_debug(UNIT(p->socket), "Incoming traffic");
9152c765 2978
718db961 2979 if (revents != EPOLLIN) {
718db961 2980 if (revents & EPOLLHUP)
f2341e0a 2981 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 2982 else
f2341e0a 2983 log_unit_error(UNIT(p->socket), "Got unexpected poll event (0x%x) on socket.", revents);
8d567588 2984 goto fail;
4f2d528d
LP
2985 }
2986
718db961
LP
2987 if (p->socket->accept &&
2988 p->type == SOCKET_SOCKET &&
2989 socket_address_can_accept(&p->address)) {
2990
a79279c7 2991 cfd = socket_accept_in_cgroup(p->socket, p, fd);
4ff9bc2e
LP
2992 if (cfd == -EAGAIN) /* Spurious accept() */
2993 return 0;
a79279c7
LP
2994 if (cfd < 0)
2995 goto fail;
4fd5948e 2996
718db961 2997 socket_apply_socket_options(p->socket, cfd);
4f2d528d 2998 }
9152c765 2999
718db961
LP
3000 socket_enter_running(p->socket, cfd);
3001 return 0;
8d567588
LP
3002
3003fail:
718db961
LP
3004 socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES);
3005 return 0;
9152c765
LP
3006}
3007
87f0e418
LP
3008static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
3009 Socket *s = SOCKET(u);
cfc4eb4c 3010 SocketResult f;
5cb5a6ff
LP
3011
3012 assert(s);
034c6ed7 3013 assert(pid >= 0);
5cb5a6ff 3014
8c47c732
LP
3015 if (pid != s->control_pid)
3016 return;
542563ba 3017
034c6ed7
LP
3018 s->control_pid = 0;
3019
1f0958f6 3020 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
cfc4eb4c
LP
3021 f = SOCKET_SUCCESS;
3022 else if (code == CLD_EXITED)
3023 f = SOCKET_FAILURE_EXIT_CODE;
3024 else if (code == CLD_KILLED)
3025 f = SOCKET_FAILURE_SIGNAL;
3026 else if (code == CLD_DUMPED)
3027 f = SOCKET_FAILURE_CORE_DUMP;
3028 else
a4152e3f 3029 assert_not_reached("Unknown sigchld code");
8c47c732 3030
b708e7ce 3031 if (s->control_command) {
6ea832a2 3032 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
a16e1123 3033
3ed0cd26 3034 if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE)
cfc4eb4c 3035 f = SOCKET_SUCCESS;
b708e7ce
LP
3036 }
3037
91bbd9b7 3038 unit_log_process_exit(
5cc2cd1c 3039 u,
91bbd9b7
LP
3040 "Control process",
3041 socket_exec_command_to_string(s->control_command_id),
5cc2cd1c 3042 f == SOCKET_SUCCESS,
91bbd9b7 3043 code, status);
034c6ed7 3044
a0fef983 3045 if (s->result == SOCKET_SUCCESS)
cfc4eb4c
LP
3046 s->result = f;
3047
3048 if (s->control_command &&
3049 s->control_command->command_next &&
3050 f == SOCKET_SUCCESS) {
3051
f2341e0a 3052 log_unit_debug(u, "Running next command for state %s", socket_state_to_string(s->state));
cfc4eb4c 3053 socket_run_next(s);
acbb0225 3054 } else {
a16e1123
LP
3055 s->control_command = NULL;
3056 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
3057
034c6ed7
LP
3058 /* No further commands for this step, so let's figure
3059 * out what to do next */
5cb5a6ff 3060
f2341e0a 3061 log_unit_debug(u, "Got final SIGCHLD for state %s", socket_state_to_string(s->state));
acbb0225 3062
034c6ed7
LP
3063 switch (s->state) {
3064
3065 case SOCKET_START_PRE:
cfc4eb4c 3066 if (f == SOCKET_SUCCESS)
3900e5fd 3067 socket_enter_start_chown(s);
034c6ed7 3068 else
cfc4eb4c 3069 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f);
034c6ed7
LP
3070 break;
3071
3900e5fd
LP
3072 case SOCKET_START_CHOWN:
3073 if (f == SOCKET_SUCCESS)
3074 socket_enter_start_post(s);
3075 else
3076 socket_enter_stop_pre(s, f);
3077 break;
3078
034c6ed7 3079 case SOCKET_START_POST:
cfc4eb4c 3080 if (f == SOCKET_SUCCESS)
e9af15c3 3081 socket_enter_listening(s);
034c6ed7 3082 else
cfc4eb4c 3083 socket_enter_stop_pre(s, f);
034c6ed7
LP
3084 break;
3085
3086 case SOCKET_STOP_PRE:
3087 case SOCKET_STOP_PRE_SIGTERM:
3088 case SOCKET_STOP_PRE_SIGKILL:
cfc4eb4c 3089 socket_enter_stop_post(s, f);
034c6ed7
LP
3090 break;
3091
3092 case SOCKET_STOP_POST:
80876c20
LP
3093 case SOCKET_FINAL_SIGTERM:
3094 case SOCKET_FINAL_SIGKILL:
cfc4eb4c 3095 socket_enter_dead(s, f);
034c6ed7
LP
3096 break;
3097
c968d76a
YW
3098 case SOCKET_CLEANING:
3099
3100 if (s->clean_result == SOCKET_SUCCESS)
3101 s->clean_result = f;
3102
3103 socket_enter_dead(s, SOCKET_SUCCESS);
3104 break;
3105
034c6ed7
LP
3106 default:
3107 assert_not_reached("Uh, control process died at wrong time.");
3108 }
3109 }
c4e2ceae
LP
3110
3111 /* Notify clients about changed exit status */
3112 unit_add_to_dbus_queue(u);
034c6ed7 3113}
5cb5a6ff 3114
718db961
LP
3115static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
3116 Socket *s = SOCKET(userdata);
5cb5a6ff 3117
034c6ed7 3118 assert(s);
718db961 3119 assert(s->timer_event_source == source);
034c6ed7
LP
3120
3121 switch (s->state) {
3122
3123 case SOCKET_START_PRE:
f2341e0a 3124 log_unit_warning(UNIT(s), "Starting timed out. Terminating.");
cfc4eb4c 3125 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
da19d5c1 3126 break;
80876c20 3127
3900e5fd 3128 case SOCKET_START_CHOWN:
034c6ed7 3129 case SOCKET_START_POST:
f2341e0a 3130 log_unit_warning(UNIT(s), "Starting timed out. Stopping.");
cfc4eb4c 3131 socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
3132 break;
3133
3134 case SOCKET_STOP_PRE:
f2341e0a 3135 log_unit_warning(UNIT(s), "Stopping timed out. Terminating.");
cfc4eb4c 3136 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
3137 break;
3138
3139 case SOCKET_STOP_PRE_SIGTERM:
4819ff03 3140 if (s->kill_context.send_sigkill) {
f2341e0a 3141 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
cfc4eb4c 3142 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT);
ba035df2 3143 } else {
f2341e0a 3144 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL. Ignoring.");
cfc4eb4c 3145 socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
ba035df2 3146 }
034c6ed7
LP
3147 break;
3148
3149 case SOCKET_STOP_PRE_SIGKILL:
f2341e0a 3150 log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring.");
cfc4eb4c 3151 socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
3152 break;
3153
3154 case SOCKET_STOP_POST:
f2341e0a 3155 log_unit_warning(UNIT(s), "Stopping timed out (2). Terminating.");
cfc4eb4c 3156 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
3157 break;
3158
80876c20 3159 case SOCKET_FINAL_SIGTERM:
4819ff03 3160 if (s->kill_context.send_sigkill) {
f2341e0a 3161 log_unit_warning(UNIT(s), "Stopping timed out (2). Killing.");
cfc4eb4c 3162 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT);
ba035df2 3163 } else {
f2341e0a 3164 log_unit_warning(UNIT(s), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
cfc4eb4c 3165 socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
ba035df2 3166 }
034c6ed7
LP
3167 break;
3168
80876c20 3169 case SOCKET_FINAL_SIGKILL:
f2341e0a 3170 log_unit_warning(UNIT(s), "Still around after SIGKILL (2). Entering failed mode.");
cfc4eb4c 3171 socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
034c6ed7
LP
3172 break;
3173
c968d76a
YW
3174 case SOCKET_CLEANING:
3175 log_unit_warning(UNIT(s), "Cleaning timed out. killing.");
3176
3177 if (s->clean_result == SOCKET_SUCCESS)
3178 s->clean_result = SOCKET_FAILURE_TIMEOUT;
3179
3180 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, 0);
3181 break;
3182
034c6ed7
LP
3183 default:
3184 assert_not_reached("Timeout at wrong time.");
3185 }
718db961
LP
3186
3187 return 0;
5cb5a6ff
LP
3188}
3189
79c7626d 3190int socket_collect_fds(Socket *s, int **fds) {
da6053d0 3191 size_t k = 0, n = 0;
44d8db9e 3192 SocketPort *p;
da6053d0 3193 int *rfds;
44d8db9e
LP
3194
3195 assert(s);
3196 assert(fds);
44d8db9e
LP
3197
3198 /* Called from the service code for requesting our fds */
3199
15087cdb 3200 LIST_FOREACH(port, p, s->ports) {
44d8db9e 3201 if (p->fd >= 0)
79c7626d
LP
3202 n++;
3203 n += p->n_auxiliary_fds;
15087cdb 3204 }
44d8db9e 3205
79c7626d 3206 if (n <= 0) {
de3756ab 3207 *fds = NULL;
de3756ab
LP
3208 return 0;
3209 }
3210
79c7626d 3211 rfds = new(int, n);
e5403f09 3212 if (!rfds)
44d8db9e
LP
3213 return -ENOMEM;
3214
15087cdb 3215 LIST_FOREACH(port, p, s->ports) {
da6053d0 3216 size_t i;
79c7626d 3217
44d8db9e
LP
3218 if (p->fd >= 0)
3219 rfds[k++] = p->fd;
15087cdb
PS
3220 for (i = 0; i < p->n_auxiliary_fds; ++i)
3221 rfds[k++] = p->auxiliary_fds[i];
3222 }
44d8db9e 3223
79c7626d 3224 assert(k == n);
44d8db9e
LP
3225
3226 *fds = rfds;
da6053d0 3227 return (int) n;
44d8db9e
LP
3228}
3229
e821075a
LP
3230static void socket_reset_failed(Unit *u) {
3231 Socket *s = SOCKET(u);
3232
3233 assert(s);
3234
3235 if (s->state == SOCKET_FAILED)
3236 socket_set_state(s, SOCKET_DEAD);
3237
3238 s->result = SOCKET_SUCCESS;
c968d76a 3239 s->clean_result = SOCKET_SUCCESS;
e821075a
LP
3240}
3241
6cf6bbc2
LP
3242void socket_connection_unref(Socket *s) {
3243 assert(s);
3244
3245 /* The service is dead. Yay!
3246 *
35b8ca3a 3247 * This is strictly for one-instance-per-connection
6cf6bbc2
LP
3248 * services. */
3249
3250 assert(s->n_connections > 0);
3251 s->n_connections--;
3252
f2341e0a 3253 log_unit_debug(UNIT(s), "One connection closed, %u left.", s->n_connections);
5632e374
LP
3254}
3255
d137a488
UTL
3256static void socket_trigger_notify(Unit *u, Unit *other) {
3257 Socket *s = SOCKET(u);
d137a488
UTL
3258
3259 assert(u);
3260 assert(other);
3261
d14e3a0d
LP
3262 /* Filter out invocations with bogus state */
3263 if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
3264 return;
3265
3266 /* Don't propagate state changes from the service if we are already down */
3267 if (!IN_SET(s->state, SOCKET_RUNNING, SOCKET_LISTENING))
d137a488
UTL
3268 return;
3269
d14e3a0d
LP
3270 /* We don't care for the service state if we are in Accept=yes mode */
3271 if (s->accept)
3272 return;
3273
3274 /* Propagate start limit hit state */
6bf0f408
LP
3275 if (other->start_limit_hit) {
3276 socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT);
d137a488 3277 return;
6bf0f408 3278 }
d137a488 3279
d14e3a0d
LP
3280 /* Don't propagate anything if there's still a job queued */
3281 if (other->job)
6bf0f408 3282 return;
d137a488 3283
6bf0f408
LP
3284 if (IN_SET(SERVICE(other)->state,
3285 SERVICE_DEAD, SERVICE_FAILED,
3286 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
3287 SERVICE_AUTO_RESTART))
3288 socket_enter_listening(s);
d137a488 3289
6bf0f408 3290 if (SERVICE(other)->state == SERVICE_RUNNING)
d137a488
UTL
3291 socket_set_state(s, SOCKET_RUNNING);
3292}
3293
718db961 3294static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
814cc562 3295 return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error);
8a0867d6
LP
3296}
3297
7a7821c8 3298static int socket_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 3299 Socket *s = SOCKET(u);
7a7821c8 3300 usec_t t;
68db7a3b
ZJS
3301 int r;
3302
3303 if (!s->timer_event_source)
3304 return 0;
3305
7a7821c8 3306 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
3307 if (r < 0)
3308 return r;
7a7821c8
LP
3309 if (t == USEC_INFINITY)
3310 return 0;
68db7a3b 3311
7a7821c8 3312 *timeout = t;
68db7a3b
ZJS
3313 return 1;
3314}
3315
8dd4c05b
LP
3316char *socket_fdname(Socket *s) {
3317 assert(s);
3318
3319 /* Returns the name to use for $LISTEN_NAMES. If the user
3320 * didn't specify anything specifically, use the socket unit's
3321 * name as fallback. */
3322
84500122 3323 return s->fdname ?: UNIT(s)->id;
8dd4c05b
LP
3324}
3325
291d565a
LP
3326static int socket_control_pid(Unit *u) {
3327 Socket *s = SOCKET(u);
3328
3329 assert(s);
3330
3331 return s->control_pid;
3332}
3333
c968d76a
YW
3334static int socket_clean(Unit *u, ExecCleanMask mask) {
3335 _cleanup_strv_free_ char **l = NULL;
3336 Socket *s = SOCKET(u);
3337 int r;
3338
3339 assert(s);
3340 assert(mask != 0);
3341
3342 if (s->state != SOCKET_DEAD)
3343 return -EBUSY;
3344
3345 r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l);
3346 if (r < 0)
3347 return r;
3348
3349 if (strv_isempty(l))
3350 return -EUNATCH;
3351
3352 socket_unwatch_control_pid(s);
3353 s->clean_result = SOCKET_SUCCESS;
3354 s->control_command = NULL;
3355 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
3356
3357 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec));
3358 if (r < 0)
3359 goto fail;
3360
3361 r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
3362 if (r < 0)
3363 goto fail;
3364
3365 socket_set_state(s, SOCKET_CLEANING);
3366
3367 return 0;
3368
3369fail:
3370 log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m");
3371 s->clean_result = SOCKET_FAILURE_RESOURCES;
3372 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
3373 return r;
3374}
3375
3376static int socket_can_clean(Unit *u, ExecCleanMask *ret) {
3377 Socket *s = SOCKET(u);
3378
3379 assert(s);
3380
3381 return exec_context_get_clean_mask(&s->exec_context, ret);
3382}
3383
a16e1123 3384static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
836bb1cd
YW
3385 [SOCKET_EXEC_START_PRE] = "ExecStartPre",
3386 [SOCKET_EXEC_START_CHOWN] = "ExecStartChown",
3387 [SOCKET_EXEC_START_POST] = "ExecStartPost",
3388 [SOCKET_EXEC_STOP_PRE] = "ExecStopPre",
3389 [SOCKET_EXEC_STOP_POST] = "ExecStopPost"
a16e1123
LP
3390};
3391
3392DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand);
3393
cfc4eb4c
LP
3394static const char* const socket_result_table[_SOCKET_RESULT_MAX] = {
3395 [SOCKET_SUCCESS] = "success",
3396 [SOCKET_FAILURE_RESOURCES] = "resources",
3397 [SOCKET_FAILURE_TIMEOUT] = "timeout",
3398 [SOCKET_FAILURE_EXIT_CODE] = "exit-code",
3399 [SOCKET_FAILURE_SIGNAL] = "signal",
c2f34808 3400 [SOCKET_FAILURE_CORE_DUMP] = "core-dump",
07299350 3401 [SOCKET_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
8b26cdbd 3402 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT] = "trigger-limit-hit",
6bf0f408 3403 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit"
cfc4eb4c
LP
3404};
3405
3406DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult);
3407
87f0e418 3408const UnitVTable socket_vtable = {
7d17cfbc 3409 .object_size = sizeof(Socket),
718db961
LP
3410 .exec_context_offset = offsetof(Socket, exec_context),
3411 .cgroup_context_offset = offsetof(Socket, cgroup_context),
3412 .kill_context_offset = offsetof(Socket, kill_context),
613b411c 3413 .exec_runtime_offset = offsetof(Socket, exec_runtime),
29206d46 3414 .dynamic_creds_offset = offsetof(Socket, dynamic_creds),
3ef63c31 3415
f975e971
LP
3416 .sections =
3417 "Unit\0"
3418 "Socket\0"
3419 "Install\0",
4ad49000 3420 .private_section = "Socket",
71645aca 3421
9c0320e7
YW
3422 .can_transient = true,
3423
034c6ed7
LP
3424 .init = socket_init,
3425 .done = socket_done,
a16e1123
LP
3426 .load = socket_load,
3427
3428 .coldplug = socket_coldplug,
034c6ed7 3429
5cb5a6ff
LP
3430 .dump = socket_dump,
3431
542563ba
LP
3432 .start = socket_start,
3433 .stop = socket_stop,
5cb5a6ff 3434
718db961 3435 .kill = socket_kill,
c968d76a
YW
3436 .clean = socket_clean,
3437 .can_clean = socket_can_clean,
718db961 3438
68db7a3b
ZJS
3439 .get_timeout = socket_get_timeout,
3440
a16e1123
LP
3441 .serialize = socket_serialize,
3442 .deserialize_item = socket_deserialize_item,
01e10de3 3443 .distribute_fds = socket_distribute_fds,
a16e1123 3444
5cb5a6ff 3445 .active_state = socket_active_state,
10a94420 3446 .sub_state_to_string = socket_sub_state_to_string,
5cb5a6ff 3447
52a12341
YW
3448 .will_restart = unit_will_restart_default,
3449
f2f725e5 3450 .may_gc = socket_may_gc,
6cf6bbc2 3451
034c6ed7 3452 .sigchld_event = socket_sigchld_event,
4139c1b2 3453
d137a488
UTL
3454 .trigger_notify = socket_trigger_notify,
3455
fdf20a31 3456 .reset_failed = socket_reset_failed,
5632e374 3457
291d565a
LP
3458 .control_pid = socket_control_pid,
3459
718db961 3460 .bus_vtable = bus_socket_vtable,
74c964d3
LP
3461 .bus_set_property = bus_socket_set_property,
3462 .bus_commit_properties = bus_socket_commit_properties,
c6918296
MS
3463
3464 .status_message_formats = {
3465 /*.starting_stopping = {
3466 [0] = "Starting socket %s...",
3467 [1] = "Stopping socket %s...",
3468 },*/
3469 .finished_start_job = {
3470 [JOB_DONE] = "Listening on %s.",
3471 [JOB_FAILED] = "Failed to listen on %s.",
c6918296
MS
3472 [JOB_TIMEOUT] = "Timed out starting %s.",
3473 },
3474 .finished_stop_job = {
3475 [JOB_DONE] = "Closed %s.",
3476 [JOB_FAILED] = "Failed stopping %s.",
3477 [JOB_TIMEOUT] = "Timed out stopping %s.",
3478 },
3479 },
5cb5a6ff 3480};