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