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