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