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