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