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