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