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