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