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