]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
83c60c9f LP |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
26 | #include <fcntl.h> | |
f94ea366 | 27 | #include <sys/epoll.h> |
034c6ed7 | 28 | #include <signal.h> |
4f2d528d | 29 | #include <arpa/inet.h> |
718db961 | 30 | #include <netinet/tcp.h> |
916abb21 | 31 | #include <mqueue.h> |
b00ad20f | 32 | #ifdef HAVE_XATTR |
0eb59ccf | 33 | #include <attr/xattr.h> |
cf37cd2f | 34 | #endif |
83c60c9f | 35 | |
718db961 | 36 | #include "sd-event.h" |
83c60c9f | 37 | #include "log.h" |
23a177ef LP |
38 | #include "load-dropin.h" |
39 | #include "load-fragment.h" | |
9e2f7c11 | 40 | #include "strv.h" |
49e942b2 | 41 | #include "mkdir.h" |
9eb977db | 42 | #include "path-util.h" |
4f2d528d | 43 | #include "unit-name.h" |
41f9172f | 44 | #include "unit-printf.h" |
4fd5948e | 45 | #include "missing.h" |
a40eb732 | 46 | #include "special.h" |
e51bc1a2 | 47 | #include "label.h" |
9a57c629 | 48 | #include "exit-status.h" |
f6a6225e | 49 | #include "def.h" |
5a045dad | 50 | #include "smack-util.h" |
718db961 LP |
51 | #include "bus-util.h" |
52 | #include "bus-error.h" | |
e821075a LP |
53 | #include "dbus-socket.h" |
54 | #include "unit.h" | |
55 | #include "socket.h" | |
83c60c9f | 56 | |
acbb0225 | 57 | static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { |
87f0e418 LP |
58 | [SOCKET_DEAD] = UNIT_INACTIVE, |
59 | [SOCKET_START_PRE] = UNIT_ACTIVATING, | |
60 | [SOCKET_START_POST] = UNIT_ACTIVATING, | |
61 | [SOCKET_LISTENING] = UNIT_ACTIVE, | |
62 | [SOCKET_RUNNING] = UNIT_ACTIVE, | |
63 | [SOCKET_STOP_PRE] = UNIT_DEACTIVATING, | |
64 | [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING, | |
65 | [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING, | |
66 | [SOCKET_STOP_POST] = UNIT_DEACTIVATING, | |
80876c20 LP |
67 | [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING, |
68 | [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 69 | [SOCKET_FAILED] = UNIT_FAILED |
83c60c9f | 70 | }; |
5cb5a6ff | 71 | |
718db961 LP |
72 | static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
73 | static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
74 | ||
a16e1123 LP |
75 | static void socket_init(Unit *u) { |
76 | Socket *s = SOCKET(u); | |
77 | ||
78 | assert(u); | |
ac155bb8 | 79 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 80 | |
a16e1123 | 81 | s->backlog = SOMAXCONN; |
1f19a534 | 82 | s->timeout_usec = u->manager->default_timeout_start_usec; |
a16e1123 | 83 | s->directory_mode = 0755; |
9131f660 | 84 | s->socket_mode = 0666; |
a16e1123 | 85 | |
6cf6bbc2 LP |
86 | s->max_connections = 64; |
87 | ||
4fd5948e | 88 | s->priority = -1; |
4fd5948e LP |
89 | s->ip_tos = -1; |
90 | s->ip_ttl = -1; | |
4fd5948e | 91 | s->mark = -1; |
4fd5948e | 92 | |
ac155bb8 MS |
93 | s->exec_context.std_output = u->manager->default_std_output; |
94 | s->exec_context.std_error = u->manager->default_std_error; | |
085afe36 | 95 | |
a16e1123 LP |
96 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; |
97 | } | |
acbb0225 | 98 | |
5e94833f LP |
99 | static void socket_unwatch_control_pid(Socket *s) { |
100 | assert(s); | |
101 | ||
102 | if (s->control_pid <= 0) | |
103 | return; | |
104 | ||
105 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
106 | s->control_pid = 0; | |
107 | } | |
108 | ||
74051b9b | 109 | void socket_free_ports(Socket *s) { |
034c6ed7 LP |
110 | SocketPort *p; |
111 | ||
112 | assert(s); | |
113 | ||
114 | while ((p = s->ports)) { | |
71fda00f | 115 | LIST_REMOVE(port, s->ports, p); |
034c6ed7 | 116 | |
718db961 LP |
117 | sd_event_source_unref(p->event_source); |
118 | ||
03e334a1 | 119 | safe_close(p->fd); |
034c6ed7 LP |
120 | free(p->path); |
121 | free(p); | |
122 | } | |
74051b9b LP |
123 | } |
124 | ||
125 | static void socket_done(Unit *u) { | |
126 | Socket *s = SOCKET(u); | |
127 | ||
128 | assert(s); | |
129 | ||
130 | socket_free_ports(s); | |
034c6ed7 | 131 | |
613b411c | 132 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); |
e537352b | 133 | exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); |
034c6ed7 LP |
134 | s->control_command = NULL; |
135 | ||
5e94833f | 136 | socket_unwatch_control_pid(s); |
034c6ed7 | 137 | |
57020a3a | 138 | unit_ref_unset(&s->service); |
034c6ed7 | 139 | |
cebf8b20 TT |
140 | free(s->tcp_congestion); |
141 | s->tcp_congestion = NULL; | |
142 | ||
acbb0225 | 143 | free(s->bind_to_device); |
e537352b | 144 | s->bind_to_device = NULL; |
acbb0225 | 145 | |
0eb59ccf AK |
146 | free(s->smack); |
147 | free(s->smack_ip_in); | |
148 | free(s->smack_ip_out); | |
149 | ||
718db961 LP |
150 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
151 | } | |
152 | ||
153 | static int socket_arm_timer(Socket *s) { | |
154 | int r; | |
155 | ||
156 | assert(s); | |
157 | ||
158 | if (s->timeout_usec <= 0) { | |
159 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
160 | return 0; | |
161 | } | |
162 | ||
163 | if (s->timer_event_source) { | |
164 | r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_usec); | |
165 | if (r < 0) | |
166 | return r; | |
167 | ||
168 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
169 | } | |
170 | ||
151b9b96 | 171 | return sd_event_add_monotonic(UNIT(s)->manager->event, &s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_usec, 0, socket_dispatch_timer, s); |
5cb5a6ff LP |
172 | } |
173 | ||
b15bdda8 | 174 | static int socket_instantiate_service(Socket *s) { |
6c833cdf MS |
175 | _cleanup_free_ char *prefix = NULL; |
176 | _cleanup_free_ char *name = NULL; | |
b15bdda8 LP |
177 | int r; |
178 | Unit *u; | |
179 | ||
180 | assert(s); | |
181 | ||
182 | /* This fills in s->service if it isn't filled in yet. For | |
183 | * Accept=yes sockets we create the next connection service | |
184 | * here. For Accept=no this is mostly a NOP since the service | |
185 | * is figured out at load time anyway. */ | |
186 | ||
57020a3a | 187 | if (UNIT_DEREF(s->service)) |
b15bdda8 LP |
188 | return 0; |
189 | ||
190 | assert(s->accept); | |
191 | ||
315db1a8 ZJS |
192 | prefix = unit_name_to_prefix(UNIT(s)->id); |
193 | if (!prefix) | |
b15bdda8 LP |
194 | return -ENOMEM; |
195 | ||
315db1a8 | 196 | if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0) |
b15bdda8 LP |
197 | return -ENOMEM; |
198 | ||
1124fe6f | 199 | r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u); |
b15bdda8 LP |
200 | if (r < 0) |
201 | return r; | |
202 | ||
7b4bf06b | 203 | #ifdef HAVE_SYSV_COMPAT |
1b64d026 | 204 | if (SERVICE(u)->is_sysv) { |
7b4bf06b LP |
205 | log_error("Using SysV services for socket activation is not supported. Refusing."); |
206 | return -ENOENT; | |
207 | } | |
208 | #endif | |
209 | ||
ac155bb8 | 210 | u->no_gc = true; |
57020a3a LP |
211 | unit_ref_set(&s->service, u); |
212 | ||
213 | return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false); | |
b15bdda8 LP |
214 | } |
215 | ||
4f2d528d LP |
216 | static bool have_non_accept_socket(Socket *s) { |
217 | SocketPort *p; | |
218 | ||
219 | assert(s); | |
220 | ||
221 | if (!s->accept) | |
222 | return true; | |
223 | ||
dd5ad9d4 LP |
224 | LIST_FOREACH(port, p, s->ports) { |
225 | ||
226 | if (p->type != SOCKET_SOCKET) | |
227 | return true; | |
228 | ||
4f2d528d LP |
229 | if (!socket_address_can_accept(&p->address)) |
230 | return true; | |
dd5ad9d4 | 231 | } |
4f2d528d LP |
232 | |
233 | return false; | |
234 | } | |
235 | ||
a57f7e2c | 236 | static int socket_add_mount_links(Socket *s) { |
6e2ef85b | 237 | SocketPort *p; |
6e2ef85b LP |
238 | int r; |
239 | ||
240 | assert(s); | |
6e2ef85b | 241 | |
a57f7e2c LP |
242 | LIST_FOREACH(port, p, s->ports) { |
243 | const char *path = NULL; | |
6e2ef85b | 244 | |
a57f7e2c LP |
245 | if (p->type == SOCKET_SOCKET) |
246 | path = socket_address_get_path(&p->address); | |
247 | else if (p->type == SOCKET_FIFO || p->type == SOCKET_SPECIAL) | |
248 | path = p->path; | |
6e2ef85b | 249 | |
a57f7e2c LP |
250 | if (!path) |
251 | continue; | |
6e2ef85b | 252 | |
a57f7e2c | 253 | r = unit_require_mounts_for(UNIT(s), path); |
b87705cd | 254 | if (r < 0) |
6e2ef85b | 255 | return r; |
b87705cd | 256 | } |
6e2ef85b LP |
257 | |
258 | return 0; | |
259 | } | |
260 | ||
261 | static int socket_add_device_link(Socket *s) { | |
262 | char *t; | |
6e2ef85b LP |
263 | |
264 | assert(s); | |
265 | ||
7d0c710d | 266 | if (!s->bind_to_device || streq(s->bind_to_device, "lo")) |
6e2ef85b LP |
267 | return 0; |
268 | ||
e821075a LP |
269 | t = strappenda("/sys/subsystem/net/devices/", s->bind_to_device); |
270 | return unit_add_node_link(UNIT(s), t, false); | |
6e2ef85b LP |
271 | } |
272 | ||
a40eb732 LP |
273 | static int socket_add_default_dependencies(Socket *s) { |
274 | int r; | |
275 | assert(s); | |
276 | ||
e3d84721 LP |
277 | r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, NULL, true); |
278 | if (r < 0) | |
279 | return r; | |
2a77d31d | 280 | |
e3d84721 LP |
281 | if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) { |
282 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true); | |
283 | if (r < 0) | |
a40eb732 | 284 | return r; |
2a77d31d | 285 | } |
a40eb732 | 286 | |
ead8e478 | 287 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); |
a40eb732 LP |
288 | } |
289 | ||
44a6b1b6 | 290 | _pure_ static bool socket_has_exec(Socket *s) { |
4cfc6dbe LP |
291 | unsigned i; |
292 | assert(s); | |
293 | ||
294 | for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++) | |
295 | if (s->exec_command[i]) | |
296 | return true; | |
297 | ||
298 | return false; | |
299 | } | |
300 | ||
e821075a LP |
301 | static int socket_add_extras(Socket *s) { |
302 | Unit *u = UNIT(s); | |
e537352b | 303 | int r; |
44d8db9e | 304 | |
e821075a | 305 | assert(s); |
57020a3a | 306 | |
e821075a | 307 | if (have_non_accept_socket(s)) { |
23a177ef | 308 | |
e821075a LP |
309 | if (!UNIT_DEREF(s->service)) { |
310 | Unit *x; | |
57020a3a | 311 | |
e821075a | 312 | r = unit_load_related_unit(u, ".service", &x); |
57020a3a | 313 | if (r < 0) |
4f2d528d | 314 | return r; |
e821075a LP |
315 | |
316 | unit_ref_set(&s->service, x); | |
4f2d528d | 317 | } |
44d8db9e | 318 | |
e821075a LP |
319 | r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true); |
320 | if (r < 0) | |
6e2ef85b | 321 | return r; |
e821075a | 322 | } |
6e2ef85b | 323 | |
e821075a LP |
324 | r = socket_add_mount_links(s); |
325 | if (r < 0) | |
326 | return r; | |
6e2ef85b | 327 | |
e821075a LP |
328 | r = socket_add_device_link(s); |
329 | if (r < 0) | |
330 | return r; | |
331 | ||
598459ce | 332 | r = unit_patch_contexts(u); |
e821075a LP |
333 | if (r < 0) |
334 | return r; | |
335 | ||
336 | if (socket_has_exec(s)) { | |
337 | r = unit_add_exec_dependencies(u, &s->exec_context); | |
338 | if (r < 0) | |
339 | return r; | |
23a177ef | 340 | |
598459ce | 341 | r = unit_add_default_slice(u, &s->cgroup_context); |
a016b922 LP |
342 | if (r < 0) |
343 | return r; | |
e821075a | 344 | } |
a016b922 | 345 | |
e821075a LP |
346 | if (u->default_dependencies) { |
347 | r = socket_add_default_dependencies(s); | |
348 | if (r < 0) | |
349 | return r; | |
350 | } | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | static int socket_verify(Socket *s) { | |
356 | assert(s); | |
357 | ||
358 | if (UNIT(s)->load_state != UNIT_LOADED) | |
359 | return 0; | |
360 | ||
361 | if (!s->ports) { | |
362 | log_error_unit(UNIT(s)->id, "%s lacks Listen setting. Refusing.", UNIT(s)->id); | |
363 | return -EINVAL; | |
364 | } | |
365 | ||
366 | if (s->accept && have_non_accept_socket(s)) { | |
367 | log_error_unit(UNIT(s)->id, "%s configured for accepting sockets, but sockets are non-accepting. Refusing.", | |
368 | UNIT(s)->id); | |
369 | return -EINVAL; | |
370 | } | |
371 | ||
372 | if (s->accept && s->max_connections <= 0) { | |
373 | log_error_unit(UNIT(s)->id, "%s's MaxConnection setting too small. Refusing.", UNIT(s)->id); | |
374 | return -EINVAL; | |
375 | } | |
e06c73cc | 376 | |
e821075a LP |
377 | if (s->accept && UNIT_DEREF(s->service)) { |
378 | log_error_unit(UNIT(s)->id, "Explicit service configuration for accepting sockets not supported on %s. Refusing.", UNIT(s)->id); | |
379 | return -EINVAL; | |
380 | } | |
381 | ||
382 | if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) { | |
383 | log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id); | |
384 | return -EINVAL; | |
385 | } | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | static int socket_load(Unit *u) { | |
391 | Socket *s = SOCKET(u); | |
392 | int r; | |
393 | ||
394 | assert(u); | |
395 | assert(u->load_state == UNIT_STUB); | |
396 | ||
397 | r = unit_load_fragment_and_dropin(u); | |
398 | if (r < 0) | |
399 | return r; | |
400 | ||
401 | if (u->load_state == UNIT_LOADED) { | |
402 | /* This is a new unit? Then let's add in some extras */ | |
403 | r = socket_add_extras(s); | |
e06c73cc LP |
404 | if (r < 0) |
405 | return r; | |
23a177ef LP |
406 | } |
407 | ||
4f2d528d | 408 | return socket_verify(s); |
44d8db9e LP |
409 | } |
410 | ||
44a6b1b6 | 411 | _const_ static const char* listen_lookup(int family, int type) { |
7a22745a LP |
412 | |
413 | if (family == AF_NETLINK) | |
414 | return "ListenNetlink"; | |
542563ba LP |
415 | |
416 | if (type == SOCK_STREAM) | |
417 | return "ListenStream"; | |
418 | else if (type == SOCK_DGRAM) | |
419 | return "ListenDatagram"; | |
420 | else if (type == SOCK_SEQPACKET) | |
421 | return "ListenSequentialPacket"; | |
422 | ||
034c6ed7 | 423 | assert_not_reached("Unknown socket type"); |
542563ba LP |
424 | return NULL; |
425 | } | |
426 | ||
87f0e418 | 427 | static void socket_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 428 | SocketExecCommand c; |
87f0e418 | 429 | Socket *s = SOCKET(u); |
542563ba | 430 | SocketPort *p; |
82ba9f08 | 431 | const char *prefix2; |
5cb5a6ff LP |
432 | |
433 | assert(s); | |
fa068367 | 434 | assert(f); |
5cb5a6ff | 435 | |
e821075a | 436 | prefix2 = strappenda(prefix, "\t"); |
c43d20a0 | 437 | |
5cb5a6ff LP |
438 | fprintf(f, |
439 | "%sSocket State: %s\n" | |
81a5c6d0 | 440 | "%sResult: %s\n" |
542563ba | 441 | "%sBindIPv6Only: %s\n" |
b5a0699f LP |
442 | "%sBacklog: %u\n" |
443 | "%sSocketMode: %04o\n" | |
4fd5948e LP |
444 | "%sDirectoryMode: %04o\n" |
445 | "%sKeepAlive: %s\n" | |
cebf8b20 | 446 | "%sFreeBind: %s\n" |
6b6d2dee | 447 | "%sTransparent: %s\n" |
ec6370a2 | 448 | "%sBroadcast: %s\n" |
ede3deb4 | 449 | "%sPassCredentials: %s\n" |
54ecda32 | 450 | "%sPassSecurity: %s\n" |
cebf8b20 | 451 | "%sTCPCongestion: %s\n", |
a16e1123 | 452 | prefix, socket_state_to_string(s->state), |
81a5c6d0 | 453 | prefix, socket_result_to_string(s->result), |
c0120d99 | 454 | prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only), |
b5a0699f LP |
455 | prefix, s->backlog, |
456 | prefix, s->socket_mode, | |
4fd5948e LP |
457 | prefix, s->directory_mode, |
458 | prefix, yes_no(s->keep_alive), | |
cebf8b20 | 459 | prefix, yes_no(s->free_bind), |
6b6d2dee | 460 | prefix, yes_no(s->transparent), |
ec6370a2 | 461 | prefix, yes_no(s->broadcast), |
d68af586 | 462 | prefix, yes_no(s->pass_cred), |
54ecda32 | 463 | prefix, yes_no(s->pass_sec), |
83a95334 | 464 | prefix, strna(s->tcp_congestion)); |
542563ba | 465 | |
70123e68 LP |
466 | if (s->control_pid > 0) |
467 | fprintf(f, | |
bb00e604 LP |
468 | "%sControl PID: %lu\n", |
469 | prefix, (unsigned long) s->control_pid); | |
70123e68 | 470 | |
acbb0225 LP |
471 | if (s->bind_to_device) |
472 | fprintf(f, | |
473 | "%sBindToDevice: %s\n", | |
474 | prefix, s->bind_to_device); | |
475 | ||
4f2d528d LP |
476 | if (s->accept) |
477 | fprintf(f, | |
6cf6bbc2 LP |
478 | "%sAccepted: %u\n" |
479 | "%sNConnections: %u\n" | |
480 | "%sMaxConnections: %u\n", | |
481 | prefix, s->n_accepted, | |
482 | prefix, s->n_connections, | |
483 | prefix, s->max_connections); | |
4f2d528d | 484 | |
4fd5948e LP |
485 | if (s->priority >= 0) |
486 | fprintf(f, | |
487 | "%sPriority: %i\n", | |
488 | prefix, s->priority); | |
489 | ||
490 | if (s->receive_buffer > 0) | |
491 | fprintf(f, | |
492 | "%sReceiveBuffer: %zu\n", | |
493 | prefix, s->receive_buffer); | |
494 | ||
495 | if (s->send_buffer > 0) | |
496 | fprintf(f, | |
497 | "%sSendBuffer: %zu\n", | |
498 | prefix, s->send_buffer); | |
499 | ||
500 | if (s->ip_tos >= 0) | |
501 | fprintf(f, | |
502 | "%sIPTOS: %i\n", | |
503 | prefix, s->ip_tos); | |
504 | ||
505 | if (s->ip_ttl >= 0) | |
506 | fprintf(f, | |
507 | "%sIPTTL: %i\n", | |
508 | prefix, s->ip_ttl); | |
509 | ||
510 | if (s->pipe_size > 0) | |
511 | fprintf(f, | |
512 | "%sPipeSize: %zu\n", | |
513 | prefix, s->pipe_size); | |
514 | ||
515 | if (s->mark >= 0) | |
516 | fprintf(f, | |
517 | "%sMark: %i\n", | |
518 | prefix, s->mark); | |
519 | ||
916abb21 LP |
520 | if (s->mq_maxmsg > 0) |
521 | fprintf(f, | |
522 | "%sMessageQueueMaxMessages: %li\n", | |
523 | prefix, s->mq_maxmsg); | |
524 | ||
525 | if (s->mq_msgsize > 0) | |
526 | fprintf(f, | |
527 | "%sMessageQueueMessageSize: %li\n", | |
528 | prefix, s->mq_msgsize); | |
529 | ||
718db961 | 530 | if (s->reuse_port) |
f7db7a69 SL |
531 | fprintf(f, |
532 | "%sReusePort: %s\n", | |
718db961 | 533 | prefix, yes_no(s->reuse_port)); |
f7db7a69 | 534 | |
0eb59ccf AK |
535 | if (s->smack) |
536 | fprintf(f, | |
537 | "%sSmackLabel: %s\n", | |
538 | prefix, s->smack); | |
539 | ||
540 | if (s->smack_ip_in) | |
541 | fprintf(f, | |
542 | "%sSmackLabelIPIn: %s\n", | |
543 | prefix, s->smack_ip_in); | |
544 | ||
545 | if (s->smack_ip_out) | |
546 | fprintf(f, | |
547 | "%sSmackLabelIPOut: %s\n", | |
548 | prefix, s->smack_ip_out); | |
549 | ||
034c6ed7 | 550 | LIST_FOREACH(port, p, s->ports) { |
5cb5a6ff | 551 | |
542563ba LP |
552 | if (p->type == SOCKET_SOCKET) { |
553 | const char *t; | |
554 | int r; | |
e364ad06 | 555 | char *k = NULL; |
542563ba LP |
556 | |
557 | if ((r = socket_address_print(&p->address, &k)) < 0) | |
558 | t = strerror(-r); | |
559 | else | |
560 | t = k; | |
561 | ||
7a22745a | 562 | fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t); |
542563ba | 563 | free(k); |
b0a3f2bc LP |
564 | } else if (p->type == SOCKET_SPECIAL) |
565 | fprintf(f, "%sListenSpecial: %s\n", prefix, p->path); | |
916abb21 LP |
566 | else if (p->type == SOCKET_MQUEUE) |
567 | fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path); | |
b0a3f2bc | 568 | else |
542563ba LP |
569 | fprintf(f, "%sListenFIFO: %s\n", prefix, p->path); |
570 | } | |
5cb5a6ff LP |
571 | |
572 | exec_context_dump(&s->exec_context, f, prefix); | |
4819ff03 | 573 | kill_context_dump(&s->kill_context, f, prefix); |
5cb5a6ff | 574 | |
e537352b | 575 | for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) { |
c43d20a0 LP |
576 | if (!s->exec_command[c]) |
577 | continue; | |
5cb5a6ff | 578 | |
40d50879 | 579 | fprintf(f, "%s-> %s:\n", |
a16e1123 | 580 | prefix, socket_exec_command_to_string(c)); |
c43d20a0 LP |
581 | |
582 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff LP |
583 | } |
584 | } | |
585 | ||
4f2d528d LP |
586 | static int instance_from_socket(int fd, unsigned nr, char **instance) { |
587 | socklen_t l; | |
588 | char *r; | |
9d16d0c7 | 589 | union sockaddr_union local, remote; |
4f2d528d LP |
590 | |
591 | assert(fd >= 0); | |
592 | assert(instance); | |
593 | ||
594 | l = sizeof(local); | |
595 | if (getsockname(fd, &local.sa, &l) < 0) | |
596 | return -errno; | |
597 | ||
598 | l = sizeof(remote); | |
599 | if (getpeername(fd, &remote.sa, &l) < 0) | |
600 | return -errno; | |
601 | ||
602 | switch (local.sa.sa_family) { | |
603 | ||
604 | case AF_INET: { | |
605 | uint32_t | |
606 | a = ntohl(local.in.sin_addr.s_addr), | |
607 | b = ntohl(remote.in.sin_addr.s_addr); | |
608 | ||
609 | if (asprintf(&r, | |
77b088c2 LP |
610 | "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
611 | nr, | |
4f2d528d LP |
612 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, |
613 | ntohs(local.in.sin_port), | |
614 | b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF, | |
615 | ntohs(remote.in.sin_port)) < 0) | |
616 | return -ENOMEM; | |
617 | ||
618 | break; | |
619 | } | |
620 | ||
621 | case AF_INET6: { | |
c65a0b14 | 622 | static const unsigned char ipv4_prefix[] = { |
2b061f5a LP |
623 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF |
624 | }; | |
625 | ||
626 | if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 && | |
627 | memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |
628 | const uint8_t | |
629 | *a = local.in6.sin6_addr.s6_addr+12, | |
630 | *b = remote.in6.sin6_addr.s6_addr+12; | |
631 | ||
632 | if (asprintf(&r, | |
77b088c2 LP |
633 | "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
634 | nr, | |
2b061f5a LP |
635 | a[0], a[1], a[2], a[3], |
636 | ntohs(local.in6.sin6_port), | |
637 | b[0], b[1], b[2], b[3], | |
638 | ntohs(remote.in6.sin6_port)) < 0) | |
639 | return -ENOMEM; | |
640 | } else { | |
641 | char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN]; | |
642 | ||
643 | if (asprintf(&r, | |
77b088c2 LP |
644 | "%u-%s:%u-%s:%u", |
645 | nr, | |
2b061f5a LP |
646 | inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)), |
647 | ntohs(local.in6.sin6_port), | |
648 | inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)), | |
649 | ntohs(remote.in6.sin6_port)) < 0) | |
650 | return -ENOMEM; | |
651 | } | |
4f2d528d LP |
652 | |
653 | break; | |
654 | } | |
655 | ||
656 | case AF_UNIX: { | |
657 | struct ucred ucred; | |
eff05270 | 658 | int k; |
4f2d528d | 659 | |
eff05270 LP |
660 | k = getpeercred(fd, &ucred); |
661 | if (k < 0) | |
662 | return k; | |
4f2d528d LP |
663 | |
664 | if (asprintf(&r, | |
bb00e604 | 665 | "%u-%lu-%lu", |
4f2d528d | 666 | nr, |
bb00e604 LP |
667 | (unsigned long) ucred.pid, |
668 | (unsigned long) ucred.uid) < 0) | |
4f2d528d LP |
669 | return -ENOMEM; |
670 | ||
671 | break; | |
672 | } | |
673 | ||
674 | default: | |
675 | assert_not_reached("Unhandled socket type."); | |
676 | } | |
677 | ||
678 | *instance = r; | |
679 | return 0; | |
680 | } | |
681 | ||
034c6ed7 | 682 | static void socket_close_fds(Socket *s) { |
83c60c9f LP |
683 | SocketPort *p; |
684 | ||
685 | assert(s); | |
686 | ||
034c6ed7 | 687 | LIST_FOREACH(port, p, s->ports) { |
718db961 LP |
688 | |
689 | p->event_source = sd_event_source_unref(p->event_source); | |
690 | ||
83c60c9f LP |
691 | if (p->fd < 0) |
692 | continue; | |
693 | ||
03e334a1 | 694 | p->fd = safe_close(p->fd); |
a16e1123 LP |
695 | |
696 | /* One little note: we should never delete any sockets | |
697 | * in the file system here! After all some other | |
698 | * process we spawned might still have a reference of | |
699 | * this fd and wants to continue to use it. Therefore | |
700 | * we delete sockets in the file system before we | |
701 | * create a new one, not after we stopped using | |
702 | * one! */ | |
83c60c9f LP |
703 | } |
704 | } | |
705 | ||
4fd5948e LP |
706 | static void socket_apply_socket_options(Socket *s, int fd) { |
707 | assert(s); | |
708 | assert(fd >= 0); | |
709 | ||
710 | if (s->keep_alive) { | |
711 | int b = s->keep_alive; | |
712 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) < 0) | |
66870f90 | 713 | log_warning_unit(UNIT(s)->id, "SO_KEEPALIVE failed: %m"); |
4fd5948e LP |
714 | } |
715 | ||
ec6370a2 LP |
716 | if (s->broadcast) { |
717 | int one = 1; | |
718 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0) | |
66870f90 | 719 | log_warning_unit(UNIT(s)->id, "SO_BROADCAST failed: %m"); |
ec6370a2 LP |
720 | } |
721 | ||
d68af586 MS |
722 | if (s->pass_cred) { |
723 | int one = 1; | |
724 | if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) | |
66870f90 | 725 | log_warning_unit(UNIT(s)->id, "SO_PASSCRED failed: %m"); |
d68af586 MS |
726 | } |
727 | ||
54ecda32 LP |
728 | if (s->pass_sec) { |
729 | int one = 1; | |
730 | if (setsockopt(fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one)) < 0) | |
66870f90 | 731 | log_warning_unit(UNIT(s)->id, "SO_PASSSEC failed: %m"); |
54ecda32 LP |
732 | } |
733 | ||
4fd5948e LP |
734 | if (s->priority >= 0) |
735 | if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0) | |
66870f90 | 736 | log_warning_unit(UNIT(s)->id, "SO_PRIORITY failed: %m"); |
4fd5948e LP |
737 | |
738 | if (s->receive_buffer > 0) { | |
739 | int value = (int) s->receive_buffer; | |
7d9eaa84 LP |
740 | |
741 | /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */ | |
742 | ||
b8cef44e | 743 | if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0) |
7d9eaa84 | 744 | if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0) |
66870f90 | 745 | log_warning_unit(UNIT(s)->id, "SO_RCVBUF failed: %m"); |
4fd5948e LP |
746 | } |
747 | ||
748 | if (s->send_buffer > 0) { | |
749 | int value = (int) s->send_buffer; | |
b8cef44e | 750 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0) |
7d9eaa84 | 751 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0) |
66870f90 | 752 | log_warning_unit(UNIT(s)->id, "SO_SNDBUF failed: %m"); |
4fd5948e LP |
753 | } |
754 | ||
755 | if (s->mark >= 0) | |
756 | if (setsockopt(fd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)) < 0) | |
66870f90 | 757 | log_warning_unit(UNIT(s)->id, "SO_MARK failed: %m"); |
4fd5948e LP |
758 | |
759 | if (s->ip_tos >= 0) | |
760 | if (setsockopt(fd, IPPROTO_IP, IP_TOS, &s->ip_tos, sizeof(s->ip_tos)) < 0) | |
66870f90 | 761 | log_warning_unit(UNIT(s)->id, "IP_TOS failed: %m"); |
4fd5948e | 762 | |
46925ac5 LP |
763 | if (s->ip_ttl >= 0) { |
764 | int r, x; | |
765 | ||
766 | r = setsockopt(fd, IPPROTO_IP, IP_TTL, &s->ip_ttl, sizeof(s->ip_ttl)); | |
5bfcc1c6 FF |
767 | |
768 | if (socket_ipv6_is_supported()) | |
769 | x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl)); | |
770 | else { | |
771 | x = -1; | |
772 | errno = EAFNOSUPPORT; | |
773 | } | |
46925ac5 LP |
774 | |
775 | if (r < 0 && x < 0) | |
66870f90 ZJS |
776 | log_warning_unit(UNIT(s)->id, |
777 | "IP_TTL/IPV6_UNICAST_HOPS failed: %m"); | |
46925ac5 | 778 | } |
cebf8b20 TT |
779 | |
780 | if (s->tcp_congestion) | |
781 | if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0) | |
66870f90 | 782 | log_warning_unit(UNIT(s)->id, "TCP_CONGESTION failed: %m"); |
0eb59ccf | 783 | |
718db961 LP |
784 | if (s->reuse_port) { |
785 | int b = s->reuse_port; | |
f0511bd7 | 786 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &b, sizeof(b)) < 0) |
f7db7a69 SL |
787 | log_warning_unit(UNIT(s)->id, "SO_REUSEPORT failed: %m"); |
788 | } | |
789 | ||
9a4e038c KS |
790 | if (s->smack_ip_in) |
791 | if (smack_label_ip_in_fd(fd, s->smack_ip_in) < 0) | |
792 | log_error_unit(UNIT(s)->id, "smack_label_ip_in_fd: %m"); | |
793 | ||
794 | if (s->smack_ip_out) | |
795 | if (smack_label_ip_out_fd(fd, s->smack_ip_out) < 0) | |
796 | log_error_unit(UNIT(s)->id, "smack_label_ip_out_fd: %m"); | |
4fd5948e LP |
797 | } |
798 | ||
b15bdda8 | 799 | static void socket_apply_fifo_options(Socket *s, int fd) { |
4fd5948e LP |
800 | assert(s); |
801 | assert(fd >= 0); | |
802 | ||
803 | if (s->pipe_size > 0) | |
804 | if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0) | |
66870f90 ZJS |
805 | log_warning_unit(UNIT(s)->id, |
806 | "F_SETPIPE_SZ: %m"); | |
0eb59ccf | 807 | |
9a4e038c KS |
808 | if (s->smack) |
809 | if (smack_label_fd(fd, s->smack) < 0) | |
810 | log_error_unit(UNIT(s)->id, "smack_label_fd: %m"); | |
4fd5948e LP |
811 | } |
812 | ||
b15bdda8 LP |
813 | static int fifo_address_create( |
814 | const char *path, | |
815 | mode_t directory_mode, | |
816 | mode_t socket_mode, | |
b15bdda8 LP |
817 | int *_fd) { |
818 | ||
7a58bfa4 | 819 | int fd = -1, r = 0; |
b15bdda8 LP |
820 | struct stat st; |
821 | mode_t old_mask; | |
822 | ||
823 | assert(path); | |
824 | assert(_fd); | |
825 | ||
d2e54fae | 826 | mkdir_parents_label(path, directory_mode); |
b15bdda8 | 827 | |
e9a5ef7c KS |
828 | r = label_context_set(path, S_IFIFO); |
829 | if (r < 0) | |
56cf987f | 830 | goto fail; |
b15bdda8 LP |
831 | |
832 | /* Enforce the right access mode for the fifo */ | |
833 | old_mask = umask(~ socket_mode); | |
834 | ||
835 | /* Include the original umask in our mask */ | |
836 | umask(~socket_mode | old_mask); | |
837 | ||
838 | r = mkfifo(path, socket_mode); | |
839 | umask(old_mask); | |
840 | ||
94bc2731 | 841 | if (r < 0 && errno != EEXIST) { |
b15bdda8 LP |
842 | r = -errno; |
843 | goto fail; | |
844 | } | |
845 | ||
e4f44e73 | 846 | if ((fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) { |
b15bdda8 LP |
847 | r = -errno; |
848 | goto fail; | |
849 | } | |
850 | ||
e9a5ef7c | 851 | label_context_clear(); |
7a58bfa4 | 852 | |
b15bdda8 LP |
853 | if (fstat(fd, &st) < 0) { |
854 | r = -errno; | |
855 | goto fail; | |
856 | } | |
857 | ||
858 | if (!S_ISFIFO(st.st_mode) || | |
de0200fc | 859 | (st.st_mode & 0777) != (socket_mode & ~old_mask) || |
e4f44e73 DR |
860 | st.st_uid != getuid() || |
861 | st.st_gid != getgid()) { | |
b15bdda8 LP |
862 | |
863 | r = -EEXIST; | |
864 | goto fail; | |
865 | } | |
866 | ||
867 | *_fd = fd; | |
868 | return 0; | |
869 | ||
870 | fail: | |
e9a5ef7c | 871 | label_context_clear(); |
03e334a1 | 872 | safe_close(fd); |
b15bdda8 LP |
873 | |
874 | return r; | |
875 | } | |
876 | ||
b0a3f2bc LP |
877 | static int special_address_create( |
878 | const char *path, | |
879 | int *_fd) { | |
880 | ||
881 | int fd = -1, r = 0; | |
882 | struct stat st; | |
883 | ||
884 | assert(path); | |
885 | assert(_fd); | |
886 | ||
887 | if ((fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) { | |
888 | r = -errno; | |
889 | goto fail; | |
890 | } | |
891 | ||
892 | if (fstat(fd, &st) < 0) { | |
893 | r = -errno; | |
894 | goto fail; | |
895 | } | |
896 | ||
897 | /* Check whether this is a /proc, /sys or /dev file or char device */ | |
898 | if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) { | |
899 | r = -EEXIST; | |
900 | goto fail; | |
901 | } | |
902 | ||
903 | *_fd = fd; | |
904 | return 0; | |
905 | ||
906 | fail: | |
03e334a1 | 907 | safe_close(fd); |
b0a3f2bc LP |
908 | |
909 | return r; | |
910 | } | |
911 | ||
916abb21 LP |
912 | static int mq_address_create( |
913 | const char *path, | |
914 | mode_t mq_mode, | |
915 | long maxmsg, | |
916 | long msgsize, | |
917 | int *_fd) { | |
918 | ||
919 | int fd = -1, r = 0; | |
920 | struct stat st; | |
921 | mode_t old_mask; | |
922 | struct mq_attr _attr, *attr = NULL; | |
923 | ||
924 | assert(path); | |
925 | assert(_fd); | |
926 | ||
927 | if (maxmsg > 0 && msgsize > 0) { | |
928 | zero(_attr); | |
929 | _attr.mq_flags = O_NONBLOCK; | |
930 | _attr.mq_maxmsg = maxmsg; | |
931 | _attr.mq_msgsize = msgsize; | |
932 | attr = &_attr; | |
933 | } | |
934 | ||
935 | /* Enforce the right access mode for the mq */ | |
936 | old_mask = umask(~ mq_mode); | |
937 | ||
938 | /* Include the original umask in our mask */ | |
939 | umask(~mq_mode | old_mask); | |
940 | ||
941 | fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr); | |
942 | umask(old_mask); | |
943 | ||
8ea913b2 | 944 | if (fd < 0) { |
916abb21 LP |
945 | r = -errno; |
946 | goto fail; | |
947 | } | |
948 | ||
949 | if (fstat(fd, &st) < 0) { | |
950 | r = -errno; | |
951 | goto fail; | |
952 | } | |
953 | ||
954 | if ((st.st_mode & 0777) != (mq_mode & ~old_mask) || | |
955 | st.st_uid != getuid() || | |
956 | st.st_gid != getgid()) { | |
957 | ||
958 | r = -EEXIST; | |
959 | goto fail; | |
960 | } | |
961 | ||
962 | *_fd = fd; | |
963 | return 0; | |
964 | ||
965 | fail: | |
03e334a1 | 966 | safe_close(fd); |
916abb21 LP |
967 | return r; |
968 | } | |
969 | ||
034c6ed7 | 970 | static int socket_open_fds(Socket *s) { |
83c60c9f LP |
971 | SocketPort *p; |
972 | int r; | |
56cf987f | 973 | char *label = NULL; |
049f8642 | 974 | bool know_label = false; |
83c60c9f LP |
975 | |
976 | assert(s); | |
977 | ||
034c6ed7 | 978 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f | 979 | |
034c6ed7 LP |
980 | if (p->fd >= 0) |
981 | continue; | |
83c60c9f LP |
982 | |
983 | if (p->type == SOCKET_SOCKET) { | |
984 | ||
049f8642 LP |
985 | if (!know_label) { |
986 | ||
987 | if ((r = socket_instantiate_service(s)) < 0) | |
988 | return r; | |
989 | ||
9444b1f2 | 990 | if (UNIT_ISSET(s->service) && |
57020a3a LP |
991 | SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]) { |
992 | r = label_get_create_label_from_exe(SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]->path, &label); | |
189583d7 LP |
993 | |
994 | if (r < 0) { | |
a7444eda LP |
995 | if (r != -EPERM) |
996 | return r; | |
997 | } | |
189583d7 | 998 | } |
049f8642 LP |
999 | |
1000 | know_label = true; | |
1001 | } | |
1002 | ||
175a3d25 LP |
1003 | r = socket_address_listen( |
1004 | &p->address, | |
1005 | SOCK_CLOEXEC|SOCK_NONBLOCK, | |
1006 | s->backlog, | |
1007 | s->bind_ipv6_only, | |
1008 | s->bind_to_device, | |
1009 | s->free_bind, | |
1010 | s->transparent, | |
1011 | s->directory_mode, | |
1012 | s->socket_mode, | |
1013 | label); | |
1014 | if (r < 0) | |
83c60c9f LP |
1015 | goto rollback; |
1016 | ||
175a3d25 | 1017 | p->fd = r; |
4fd5948e LP |
1018 | socket_apply_socket_options(s, p->fd); |
1019 | ||
b0a3f2bc LP |
1020 | } else if (p->type == SOCKET_SPECIAL) { |
1021 | ||
175a3d25 LP |
1022 | r = special_address_create( |
1023 | p->path, | |
1024 | &p->fd); | |
1025 | if (r < 0) | |
b0a3f2bc LP |
1026 | goto rollback; |
1027 | ||
b15bdda8 | 1028 | } else if (p->type == SOCKET_FIFO) { |
83c60c9f | 1029 | |
175a3d25 LP |
1030 | r = fifo_address_create( |
1031 | p->path, | |
1032 | s->directory_mode, | |
1033 | s->socket_mode, | |
1034 | &p->fd); | |
1035 | if (r < 0) | |
83c60c9f | 1036 | goto rollback; |
83c60c9f | 1037 | |
b15bdda8 | 1038 | socket_apply_fifo_options(s, p->fd); |
916abb21 | 1039 | } else if (p->type == SOCKET_MQUEUE) { |
83c60c9f | 1040 | |
175a3d25 LP |
1041 | r = mq_address_create( |
1042 | p->path, | |
1043 | s->socket_mode, | |
1044 | s->mq_maxmsg, | |
1045 | s->mq_msgsize, | |
1046 | &p->fd); | |
1047 | if (r < 0) | |
916abb21 | 1048 | goto rollback; |
b15bdda8 LP |
1049 | } else |
1050 | assert_not_reached("Unknown port type"); | |
034c6ed7 LP |
1051 | } |
1052 | ||
56cf987f | 1053 | label_free(label); |
034c6ed7 LP |
1054 | return 0; |
1055 | ||
1056 | rollback: | |
1057 | socket_close_fds(s); | |
56cf987f | 1058 | label_free(label); |
034c6ed7 LP |
1059 | return r; |
1060 | } | |
1061 | ||
1062 | static void socket_unwatch_fds(Socket *s) { | |
1063 | SocketPort *p; | |
718db961 | 1064 | int r; |
9152c765 | 1065 | |
034c6ed7 LP |
1066 | assert(s); |
1067 | ||
1068 | LIST_FOREACH(port, p, s->ports) { | |
1069 | if (p->fd < 0) | |
1070 | continue; | |
1071 | ||
718db961 LP |
1072 | if (p->event_source) { |
1073 | r = sd_event_source_set_enabled(p->event_source, SD_EVENT_OFF); | |
1074 | if (r < 0) | |
1075 | log_debug_unit(UNIT(s)->id, "Failed to disable event source."); | |
1076 | } | |
83c60c9f | 1077 | } |
034c6ed7 LP |
1078 | } |
1079 | ||
1080 | static int socket_watch_fds(Socket *s) { | |
1081 | SocketPort *p; | |
1082 | int r; | |
1083 | ||
1084 | assert(s); | |
83c60c9f | 1085 | |
034c6ed7 LP |
1086 | LIST_FOREACH(port, p, s->ports) { |
1087 | if (p->fd < 0) | |
1088 | continue; | |
1089 | ||
718db961 LP |
1090 | if (p->event_source) |
1091 | r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON); | |
1092 | else | |
151b9b96 | 1093 | r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p); |
4f2d528d | 1094 | |
718db961 LP |
1095 | if (r < 0) { |
1096 | log_warning_unit(UNIT(s)->id, "Failed to watch listening fds: %s", strerror(-r)); | |
034c6ed7 | 1097 | goto fail; |
718db961 | 1098 | } |
034c6ed7 | 1099 | } |
83c60c9f | 1100 | |
542563ba | 1101 | return 0; |
83c60c9f | 1102 | |
034c6ed7 LP |
1103 | fail: |
1104 | socket_unwatch_fds(s); | |
1105 | return r; | |
1106 | } | |
1107 | ||
1108 | static void socket_set_state(Socket *s, SocketState state) { | |
1109 | SocketState old_state; | |
1110 | assert(s); | |
1111 | ||
1112 | old_state = s->state; | |
1113 | s->state = state; | |
1114 | ||
1115 | if (state != SOCKET_START_PRE && | |
1116 | state != SOCKET_START_POST && | |
1117 | state != SOCKET_STOP_PRE && | |
1118 | state != SOCKET_STOP_PRE_SIGTERM && | |
1119 | state != SOCKET_STOP_PRE_SIGKILL && | |
1120 | state != SOCKET_STOP_POST && | |
80876c20 LP |
1121 | state != SOCKET_FINAL_SIGTERM && |
1122 | state != SOCKET_FINAL_SIGKILL) { | |
718db961 LP |
1123 | |
1124 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
5e94833f | 1125 | socket_unwatch_control_pid(s); |
034c6ed7 | 1126 | s->control_command = NULL; |
a16e1123 | 1127 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; |
e537352b | 1128 | } |
034c6ed7 | 1129 | |
a16e1123 LP |
1130 | if (state != SOCKET_LISTENING) |
1131 | socket_unwatch_fds(s); | |
1132 | ||
034c6ed7 LP |
1133 | if (state != SOCKET_START_POST && |
1134 | state != SOCKET_LISTENING && | |
1135 | state != SOCKET_RUNNING && | |
1136 | state != SOCKET_STOP_PRE && | |
1137 | state != SOCKET_STOP_PRE_SIGTERM && | |
1138 | state != SOCKET_STOP_PRE_SIGKILL) | |
1139 | socket_close_fds(s); | |
1140 | ||
e537352b | 1141 | if (state != old_state) |
e821075a LP |
1142 | log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", |
1143 | UNIT(s)->id, socket_state_to_string(old_state), socket_state_to_string(state)); | |
acbb0225 | 1144 | |
e2f3b44c | 1145 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true); |
034c6ed7 LP |
1146 | } |
1147 | ||
a16e1123 LP |
1148 | static int socket_coldplug(Unit *u) { |
1149 | Socket *s = SOCKET(u); | |
1150 | int r; | |
1151 | ||
1152 | assert(s); | |
1153 | assert(s->state == SOCKET_DEAD); | |
1154 | ||
e821075a LP |
1155 | if (s->deserialized_state == s->state) |
1156 | return 0; | |
a16e1123 | 1157 | |
e821075a LP |
1158 | if (s->deserialized_state == SOCKET_START_PRE || |
1159 | s->deserialized_state == SOCKET_START_POST || | |
1160 | s->deserialized_state == SOCKET_STOP_PRE || | |
1161 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
1162 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL || | |
1163 | s->deserialized_state == SOCKET_STOP_POST || | |
1164 | s->deserialized_state == SOCKET_FINAL_SIGTERM || | |
1165 | s->deserialized_state == SOCKET_FINAL_SIGKILL) { | |
a16e1123 | 1166 | |
e821075a LP |
1167 | if (s->control_pid <= 0) |
1168 | return -EBADMSG; | |
a16e1123 | 1169 | |
e821075a LP |
1170 | r = unit_watch_pid(UNIT(s), s->control_pid); |
1171 | if (r < 0) | |
1172 | return r; | |
a16e1123 | 1173 | |
e821075a LP |
1174 | r = socket_arm_timer(s); |
1175 | if (r < 0) | |
1176 | return r; | |
1177 | } | |
a16e1123 | 1178 | |
e821075a LP |
1179 | if (s->deserialized_state == SOCKET_START_POST || |
1180 | s->deserialized_state == SOCKET_LISTENING || | |
1181 | s->deserialized_state == SOCKET_RUNNING || | |
1182 | s->deserialized_state == SOCKET_STOP_PRE || | |
1183 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
1184 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) { | |
1185 | r = socket_open_fds(s); | |
1186 | if (r < 0) | |
1187 | return r; | |
1188 | } | |
a16e1123 | 1189 | |
e821075a LP |
1190 | if (s->deserialized_state == SOCKET_LISTENING) { |
1191 | r = socket_watch_fds(s); | |
1192 | if (r < 0) | |
1193 | return r; | |
a16e1123 LP |
1194 | } |
1195 | ||
e821075a | 1196 | socket_set_state(s, s->deserialized_state); |
a16e1123 LP |
1197 | return 0; |
1198 | } | |
1199 | ||
e537352b | 1200 | static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { |
034c6ed7 LP |
1201 | pid_t pid; |
1202 | int r; | |
9e2f7c11 | 1203 | char **argv; |
034c6ed7 LP |
1204 | |
1205 | assert(s); | |
1206 | assert(c); | |
1207 | assert(_pid); | |
1208 | ||
4ad49000 LP |
1209 | unit_realize_cgroup(UNIT(s)); |
1210 | ||
613b411c LP |
1211 | r = unit_setup_exec_runtime(UNIT(s)); |
1212 | if (r < 0) | |
1213 | goto fail; | |
1214 | ||
718db961 | 1215 | r = socket_arm_timer(s); |
36697dc0 | 1216 | if (r < 0) |
e537352b | 1217 | goto fail; |
034c6ed7 | 1218 | |
19f6d710 LP |
1219 | r = unit_full_printf_strv(UNIT(s), c->argv, &argv); |
1220 | if (r < 0) | |
9e2f7c11 | 1221 | goto fail; |
9e2f7c11 LP |
1222 | |
1223 | r = exec_spawn(c, | |
1224 | argv, | |
1225 | &s->exec_context, | |
1226 | NULL, 0, | |
1124fe6f | 1227 | UNIT(s)->manager->environment, |
9e2f7c11 LP |
1228 | true, |
1229 | true, | |
1e3ad081 | 1230 | true, |
1124fe6f | 1231 | UNIT(s)->manager->confirm_spawn, |
13b84ec7 | 1232 | UNIT(s)->manager->cgroup_supported, |
4ad49000 | 1233 | UNIT(s)->cgroup_path, |
e66cf1a3 | 1234 | manager_get_runtime_prefix(UNIT(s)->manager), |
62bca2c6 | 1235 | UNIT(s)->id, |
09812eb7 | 1236 | 0, |
f2b68789 | 1237 | NULL, |
613b411c | 1238 | s->exec_runtime, |
9e2f7c11 LP |
1239 | &pid); |
1240 | ||
1241 | strv_free(argv); | |
1242 | if (r < 0) | |
034c6ed7 LP |
1243 | goto fail; |
1244 | ||
718db961 LP |
1245 | r = unit_watch_pid(UNIT(s), pid); |
1246 | if (r < 0) | |
034c6ed7 LP |
1247 | /* FIXME: we need to do something here */ |
1248 | goto fail; | |
83c60c9f | 1249 | |
034c6ed7 LP |
1250 | *_pid = pid; |
1251 | ||
1252 | return 0; | |
1253 | ||
1254 | fail: | |
718db961 | 1255 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
83c60c9f LP |
1256 | |
1257 | return r; | |
542563ba LP |
1258 | } |
1259 | ||
cfc4eb4c | 1260 | static void socket_enter_dead(Socket *s, SocketResult f) { |
034c6ed7 LP |
1261 | assert(s); |
1262 | ||
cfc4eb4c LP |
1263 | if (f != SOCKET_SUCCESS) |
1264 | s->result = f; | |
034c6ed7 | 1265 | |
613b411c LP |
1266 | exec_runtime_destroy(s->exec_runtime); |
1267 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
1268 | ||
e66cf1a3 LP |
1269 | exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager)); |
1270 | ||
cfc4eb4c | 1271 | socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD); |
034c6ed7 LP |
1272 | } |
1273 | ||
cfc4eb4c | 1274 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f); |
80876c20 | 1275 | |
cfc4eb4c | 1276 | static void socket_enter_stop_post(Socket *s, SocketResult f) { |
034c6ed7 LP |
1277 | int r; |
1278 | assert(s); | |
1279 | ||
cfc4eb4c LP |
1280 | if (f != SOCKET_SUCCESS) |
1281 | s->result = f; | |
034c6ed7 | 1282 | |
5e94833f LP |
1283 | socket_unwatch_control_pid(s); |
1284 | ||
a16e1123 LP |
1285 | s->control_command_id = SOCKET_EXEC_STOP_POST; |
1286 | ||
80876c20 | 1287 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) { |
e537352b | 1288 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1289 | goto fail; |
1290 | ||
80876c20 LP |
1291 | socket_set_state(s, SOCKET_STOP_POST); |
1292 | } else | |
cfc4eb4c | 1293 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS); |
034c6ed7 LP |
1294 | |
1295 | return; | |
1296 | ||
1297 | fail: | |
66870f90 ZJS |
1298 | log_warning_unit(UNIT(s)->id, |
1299 | "%s failed to run 'stop-post' task: %s", | |
1300 | UNIT(s)->id, strerror(-r)); | |
cfc4eb4c | 1301 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1302 | } |
1303 | ||
cfc4eb4c | 1304 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) { |
034c6ed7 LP |
1305 | int r; |
1306 | ||
1307 | assert(s); | |
1308 | ||
cfc4eb4c LP |
1309 | if (f != SOCKET_SUCCESS) |
1310 | s->result = f; | |
034c6ed7 | 1311 | |
cd2086fe LP |
1312 | r = unit_kill_context( |
1313 | UNIT(s), | |
1314 | &s->kill_context, | |
1315 | state != SOCKET_STOP_PRE_SIGTERM && state != SOCKET_FINAL_SIGTERM, | |
1316 | -1, | |
1317 | s->control_pid, | |
1318 | false); | |
1319 | if (r < 0) | |
1320 | goto fail; | |
034c6ed7 | 1321 | |
cd2086fe | 1322 | if (r > 0) { |
718db961 | 1323 | r = socket_arm_timer(s); |
36697dc0 | 1324 | if (r < 0) |
80876c20 | 1325 | goto fail; |
d6ea93e3 | 1326 | |
80876c20 | 1327 | socket_set_state(s, state); |
ac84d1fb LP |
1328 | } else if (state == SOCKET_STOP_PRE_SIGTERM) |
1329 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS); | |
1330 | else if (state == SOCKET_STOP_PRE_SIGKILL) | |
cfc4eb4c | 1331 | socket_enter_stop_post(s, SOCKET_SUCCESS); |
ac84d1fb LP |
1332 | else if (state == SOCKET_FINAL_SIGTERM) |
1333 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS); | |
80876c20 | 1334 | else |
cfc4eb4c | 1335 | socket_enter_dead(s, SOCKET_SUCCESS); |
034c6ed7 LP |
1336 | |
1337 | return; | |
1338 | ||
1339 | fail: | |
e821075a | 1340 | log_warning_unit(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r)); |
034c6ed7 LP |
1341 | |
1342 | if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
cfc4eb4c | 1343 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 1344 | else |
cfc4eb4c | 1345 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1346 | } |
1347 | ||
cfc4eb4c | 1348 | static void socket_enter_stop_pre(Socket *s, SocketResult f) { |
034c6ed7 LP |
1349 | int r; |
1350 | assert(s); | |
1351 | ||
cfc4eb4c LP |
1352 | if (f != SOCKET_SUCCESS) |
1353 | s->result = f; | |
034c6ed7 | 1354 | |
5e94833f LP |
1355 | socket_unwatch_control_pid(s); |
1356 | ||
a16e1123 LP |
1357 | s->control_command_id = SOCKET_EXEC_STOP_PRE; |
1358 | ||
80876c20 | 1359 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) { |
e537352b | 1360 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1361 | goto fail; |
1362 | ||
80876c20 LP |
1363 | socket_set_state(s, SOCKET_STOP_PRE); |
1364 | } else | |
cfc4eb4c | 1365 | socket_enter_stop_post(s, SOCKET_SUCCESS); |
034c6ed7 LP |
1366 | |
1367 | return; | |
1368 | ||
1369 | fail: | |
e821075a | 1370 | log_warning_unit(UNIT(s)->id, "%s failed to run 'stop-pre' task: %s", UNIT(s)->id, strerror(-r)); |
cfc4eb4c | 1371 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1372 | } |
1373 | ||
e9af15c3 LP |
1374 | static void socket_enter_listening(Socket *s) { |
1375 | int r; | |
1376 | assert(s); | |
1377 | ||
cfc4eb4c LP |
1378 | r = socket_watch_fds(s); |
1379 | if (r < 0) { | |
e821075a | 1380 | log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r)); |
e9af15c3 LP |
1381 | goto fail; |
1382 | } | |
1383 | ||
1384 | socket_set_state(s, SOCKET_LISTENING); | |
1385 | return; | |
1386 | ||
1387 | fail: | |
cfc4eb4c | 1388 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
e9af15c3 LP |
1389 | } |
1390 | ||
034c6ed7 LP |
1391 | static void socket_enter_start_post(Socket *s) { |
1392 | int r; | |
1393 | assert(s); | |
1394 | ||
cfc4eb4c LP |
1395 | r = socket_open_fds(s); |
1396 | if (r < 0) { | |
e821075a | 1397 | log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r)); |
034c6ed7 LP |
1398 | goto fail; |
1399 | } | |
1400 | ||
5e94833f LP |
1401 | socket_unwatch_control_pid(s); |
1402 | ||
a16e1123 LP |
1403 | s->control_command_id = SOCKET_EXEC_START_POST; |
1404 | ||
80876c20 | 1405 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { |
cfc4eb4c LP |
1406 | r = socket_spawn(s, s->control_command, &s->control_pid); |
1407 | if (r < 0) { | |
e821075a | 1408 | log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r)); |
034c6ed7 LP |
1409 | goto fail; |
1410 | } | |
1411 | ||
80876c20 LP |
1412 | socket_set_state(s, SOCKET_START_POST); |
1413 | } else | |
e9af15c3 | 1414 | socket_enter_listening(s); |
034c6ed7 LP |
1415 | |
1416 | return; | |
1417 | ||
1418 | fail: | |
cfc4eb4c | 1419 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1420 | } |
1421 | ||
1422 | static void socket_enter_start_pre(Socket *s) { | |
1423 | int r; | |
1424 | assert(s); | |
1425 | ||
5e94833f LP |
1426 | socket_unwatch_control_pid(s); |
1427 | ||
a16e1123 LP |
1428 | s->control_command_id = SOCKET_EXEC_START_PRE; |
1429 | ||
80876c20 | 1430 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { |
e821075a LP |
1431 | r = socket_spawn(s, s->control_command, &s->control_pid); |
1432 | if (r < 0) | |
034c6ed7 LP |
1433 | goto fail; |
1434 | ||
80876c20 LP |
1435 | socket_set_state(s, SOCKET_START_PRE); |
1436 | } else | |
034c6ed7 LP |
1437 | socket_enter_start_post(s); |
1438 | ||
1439 | return; | |
1440 | ||
1441 | fail: | |
e821075a | 1442 | log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r)); |
cfc4eb4c | 1443 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1444 | } |
1445 | ||
4f2d528d | 1446 | static void socket_enter_running(Socket *s, int cfd) { |
718db961 | 1447 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 LP |
1448 | int r; |
1449 | ||
1450 | assert(s); | |
1451 | ||
ba3e67a7 LP |
1452 | /* We don't take connections anymore if we are supposed to |
1453 | * shut down anyway */ | |
31afa0a4 | 1454 | if (unit_stop_pending(UNIT(s))) { |
e821075a LP |
1455 | |
1456 | log_debug_unit(UNIT(s)->id, "Suppressing connection request on %s since unit stop is scheduled.", UNIT(s)->id); | |
5d909e3e | 1457 | |
7c610628 | 1458 | if (cfd >= 0) |
03e334a1 | 1459 | safe_close(cfd); |
7c610628 LP |
1460 | else { |
1461 | /* Flush all sockets by closing and reopening them */ | |
1462 | socket_close_fds(s); | |
1463 | ||
16ac4014 LP |
1464 | r = socket_open_fds(s); |
1465 | if (r < 0) { | |
1466 | log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r)); | |
1467 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
1468 | return; | |
1469 | } | |
1470 | ||
1a710b43 MS |
1471 | r = socket_watch_fds(s); |
1472 | if (r < 0) { | |
e821075a | 1473 | log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r)); |
cfc4eb4c | 1474 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
7c610628 LP |
1475 | } |
1476 | } | |
1477 | ||
ba3e67a7 LP |
1478 | return; |
1479 | } | |
1480 | ||
4f2d528d | 1481 | if (cfd < 0) { |
57020a3a | 1482 | Iterator i; |
e821075a | 1483 | Unit *other; |
f976f3f6 | 1484 | bool pending = false; |
f976f3f6 LP |
1485 | |
1486 | /* If there's already a start pending don't bother to | |
1487 | * do anything */ | |
e821075a LP |
1488 | SET_FOREACH(other, UNIT(s)->dependencies[UNIT_TRIGGERS], i) |
1489 | if (unit_active_or_pending(other)) { | |
57020a3a LP |
1490 | pending = true; |
1491 | break; | |
1492 | } | |
f976f3f6 | 1493 | |
1a710b43 MS |
1494 | if (!pending) { |
1495 | r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, true, &error, NULL); | |
1496 | if (r < 0) | |
f976f3f6 | 1497 | goto fail; |
1a710b43 | 1498 | } |
4f2d528d LP |
1499 | |
1500 | socket_set_state(s, SOCKET_RUNNING); | |
1501 | } else { | |
e55001eb | 1502 | _cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL; |
b15bdda8 | 1503 | Service *service; |
4f2d528d | 1504 | |
6cf6bbc2 | 1505 | if (s->n_connections >= s->max_connections) { |
e821075a | 1506 | log_warning_unit(UNIT(s)->id, "%s: Too many incoming connections (%u)", UNIT(s)->id, s->n_connections); |
03e334a1 | 1507 | safe_close(cfd); |
6cf6bbc2 LP |
1508 | return; |
1509 | } | |
1510 | ||
1a710b43 MS |
1511 | r = socket_instantiate_service(s); |
1512 | if (r < 0) | |
b15bdda8 LP |
1513 | goto fail; |
1514 | ||
1a710b43 MS |
1515 | r = instance_from_socket(cfd, s->n_accepted, &instance); |
1516 | if (r < 0) { | |
1517 | if (r != -ENOTCONN) | |
1518 | goto fail; | |
1519 | ||
1520 | /* ENOTCONN is legitimate if TCP RST was received. | |
1521 | * This connection is over, but the socket unit lives on. */ | |
03e334a1 | 1522 | safe_close(cfd); |
1a710b43 MS |
1523 | return; |
1524 | } | |
4f2d528d | 1525 | |
1a710b43 MS |
1526 | prefix = unit_name_to_prefix(UNIT(s)->id); |
1527 | if (!prefix) { | |
4f2d528d LP |
1528 | r = -ENOMEM; |
1529 | goto fail; | |
1530 | } | |
1531 | ||
1532 | name = unit_name_build(prefix, instance, ".service"); | |
b6dbbe1c | 1533 | if (!name) { |
4f2d528d | 1534 | r = -ENOMEM; |
b6dbbe1c LP |
1535 | goto fail; |
1536 | } | |
4f2d528d | 1537 | |
1a710b43 | 1538 | r = unit_add_name(UNIT_DEREF(s->service), name); |
e55001eb | 1539 | if (r < 0) |
4f2d528d | 1540 | goto fail; |
b15bdda8 | 1541 | |
57020a3a LP |
1542 | service = SERVICE(UNIT_DEREF(s->service)); |
1543 | unit_ref_unset(&s->service); | |
b15bdda8 | 1544 | s->n_accepted ++; |
4f2d528d | 1545 | |
1124fe6f | 1546 | UNIT(service)->no_gc = false; |
6c073082 | 1547 | |
b15bdda8 | 1548 | unit_choose_id(UNIT(service), name); |
b15bdda8 | 1549 | |
1a710b43 MS |
1550 | r = service_set_socket_fd(service, cfd, s); |
1551 | if (r < 0) | |
4f2d528d LP |
1552 | goto fail; |
1553 | ||
1554 | cfd = -1; | |
6cf6bbc2 LP |
1555 | s->n_connections ++; |
1556 | ||
1a710b43 MS |
1557 | r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL); |
1558 | if (r < 0) | |
4f2d528d | 1559 | goto fail; |
c4e2ceae LP |
1560 | |
1561 | /* Notify clients about changed counters */ | |
1562 | unit_add_to_dbus_queue(UNIT(s)); | |
4f2d528d | 1563 | } |
034c6ed7 | 1564 | |
034c6ed7 LP |
1565 | return; |
1566 | ||
1567 | fail: | |
e821075a LP |
1568 | log_warning_unit(UNIT(s)->id, "%s failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s", |
1569 | UNIT(s)->id, cfd >= 0 ? "template" : "non-template", | |
718db961 | 1570 | bus_error_message(&error, r)); |
e821075a | 1571 | |
60089004 | 1572 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
03e334a1 | 1573 | safe_close(cfd); |
034c6ed7 LP |
1574 | } |
1575 | ||
cfc4eb4c | 1576 | static void socket_run_next(Socket *s) { |
034c6ed7 LP |
1577 | int r; |
1578 | ||
1579 | assert(s); | |
1580 | assert(s->control_command); | |
1581 | assert(s->control_command->command_next); | |
1582 | ||
5e94833f LP |
1583 | socket_unwatch_control_pid(s); |
1584 | ||
034c6ed7 LP |
1585 | s->control_command = s->control_command->command_next; |
1586 | ||
e821075a LP |
1587 | r = socket_spawn(s, s->control_command, &s->control_pid); |
1588 | if (r < 0) | |
034c6ed7 LP |
1589 | goto fail; |
1590 | ||
1591 | return; | |
1592 | ||
1593 | fail: | |
e821075a | 1594 | log_warning_unit(UNIT(s)->id, "%s failed to run next task: %s", UNIT(s)->id, strerror(-r)); |
80876c20 LP |
1595 | |
1596 | if (s->state == SOCKET_START_POST) | |
cfc4eb4c | 1597 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 1598 | else if (s->state == SOCKET_STOP_POST) |
cfc4eb4c | 1599 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 1600 | else |
cfc4eb4c | 1601 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1602 | } |
1603 | ||
87f0e418 LP |
1604 | static int socket_start(Unit *u) { |
1605 | Socket *s = SOCKET(u); | |
83c60c9f LP |
1606 | |
1607 | assert(s); | |
1608 | ||
034c6ed7 LP |
1609 | /* We cannot fulfill this request right now, try again later |
1610 | * please! */ | |
1611 | if (s->state == SOCKET_STOP_PRE || | |
1612 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1613 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1614 | s->state == SOCKET_STOP_POST || | |
80876c20 LP |
1615 | s->state == SOCKET_FINAL_SIGTERM || |
1616 | s->state == SOCKET_FINAL_SIGKILL) | |
034c6ed7 LP |
1617 | return -EAGAIN; |
1618 | ||
83c60c9f LP |
1619 | if (s->state == SOCKET_START_PRE || |
1620 | s->state == SOCKET_START_POST) | |
034c6ed7 | 1621 | return 0; |
83c60c9f | 1622 | |
034c6ed7 | 1623 | /* Cannot run this without the service being around */ |
9444b1f2 | 1624 | if (UNIT_ISSET(s->service)) { |
57020a3a LP |
1625 | Service *service; |
1626 | ||
1627 | service = SERVICE(UNIT_DEREF(s->service)); | |
1628 | ||
1124fe6f | 1629 | if (UNIT(service)->load_state != UNIT_LOADED) { |
e821075a | 1630 | log_error_unit(u->id, "Socket service %s not loaded, refusing.", UNIT(service)->id); |
4f2d528d | 1631 | return -ENOENT; |
4ac9236f | 1632 | } |
4f2d528d | 1633 | |
35b8ca3a | 1634 | /* If the service is already active we cannot start the |
4f2d528d | 1635 | * socket */ |
57020a3a LP |
1636 | if (service->state != SERVICE_DEAD && |
1637 | service->state != SERVICE_FAILED && | |
1638 | service->state != SERVICE_AUTO_RESTART) { | |
e821075a | 1639 | log_error_unit(u->id, "Socket service %s already active, refusing.", UNIT(service)->id); |
4f2d528d | 1640 | return -EBUSY; |
4ac9236f | 1641 | } |
7b4bf06b LP |
1642 | |
1643 | #ifdef HAVE_SYSV_COMPAT | |
1b64d026 | 1644 | if (service->is_sysv) { |
d7607eac | 1645 | log_error_unit(u->id, |
66870f90 | 1646 | "Using SysV services for socket activation is not supported. Refusing."); |
7b4bf06b LP |
1647 | return -ENOENT; |
1648 | } | |
1649 | #endif | |
4f2d528d | 1650 | } |
e537352b | 1651 | |
fdf20a31 | 1652 | assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED); |
83c60c9f | 1653 | |
cfc4eb4c | 1654 | s->result = SOCKET_SUCCESS; |
034c6ed7 | 1655 | socket_enter_start_pre(s); |
e821075a | 1656 | |
034c6ed7 LP |
1657 | return 0; |
1658 | } | |
83c60c9f | 1659 | |
87f0e418 LP |
1660 | static int socket_stop(Unit *u) { |
1661 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
1662 | |
1663 | assert(s); | |
1664 | ||
e537352b LP |
1665 | /* Already on it */ |
1666 | if (s->state == SOCKET_STOP_PRE || | |
1667 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1668 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1669 | s->state == SOCKET_STOP_POST || | |
80876c20 | 1670 | s->state == SOCKET_FINAL_SIGTERM || |
3f6c78dc | 1671 | s->state == SOCKET_FINAL_SIGKILL) |
e537352b LP |
1672 | return 0; |
1673 | ||
3f6c78dc LP |
1674 | /* If there's already something running we go directly into |
1675 | * kill mode. */ | |
1676 | if (s->state == SOCKET_START_PRE || | |
1677 | s->state == SOCKET_START_POST) { | |
cfc4eb4c | 1678 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS); |
3f6c78dc LP |
1679 | return -EAGAIN; |
1680 | } | |
1681 | ||
034c6ed7 | 1682 | assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING); |
83c60c9f | 1683 | |
cfc4eb4c | 1684 | socket_enter_stop_pre(s, SOCKET_SUCCESS); |
542563ba LP |
1685 | return 0; |
1686 | } | |
1687 | ||
a16e1123 LP |
1688 | static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { |
1689 | Socket *s = SOCKET(u); | |
1690 | SocketPort *p; | |
1691 | int r; | |
1692 | ||
1693 | assert(u); | |
1694 | assert(f); | |
1695 | assert(fds); | |
1696 | ||
1697 | unit_serialize_item(u, f, "state", socket_state_to_string(s->state)); | |
cfc4eb4c | 1698 | unit_serialize_item(u, f, "result", socket_result_to_string(s->result)); |
a16e1123 LP |
1699 | unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted); |
1700 | ||
1701 | if (s->control_pid > 0) | |
5925dd3c | 1702 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); |
a16e1123 LP |
1703 | |
1704 | if (s->control_command_id >= 0) | |
1705 | unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id)); | |
1706 | ||
1707 | LIST_FOREACH(port, p, s->ports) { | |
1708 | int copy; | |
1709 | ||
1710 | if (p->fd < 0) | |
1711 | continue; | |
1712 | ||
613b411c LP |
1713 | copy = fdset_put_dup(fds, p->fd); |
1714 | if (copy < 0) | |
a16e1123 LP |
1715 | return copy; |
1716 | ||
1717 | if (p->type == SOCKET_SOCKET) { | |
613b411c | 1718 | _cleanup_free_ char *t = NULL; |
a16e1123 | 1719 | |
ee092817 LP |
1720 | r = socket_address_print(&p->address, &t); |
1721 | if (r < 0) | |
a16e1123 LP |
1722 | return r; |
1723 | ||
7a22745a LP |
1724 | if (socket_address_family(&p->address) == AF_NETLINK) |
1725 | unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t); | |
1726 | else | |
1727 | unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t); | |
613b411c | 1728 | |
b0a3f2bc LP |
1729 | } else if (p->type == SOCKET_SPECIAL) |
1730 | unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path); | |
ee092817 LP |
1731 | else if (p->type == SOCKET_MQUEUE) |
1732 | unit_serialize_item_format(u, f, "mqueue", "%i %s", copy, p->path); | |
b0a3f2bc | 1733 | else { |
a16e1123 LP |
1734 | assert(p->type == SOCKET_FIFO); |
1735 | unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path); | |
1736 | } | |
1737 | } | |
1738 | ||
1739 | return 0; | |
1740 | } | |
1741 | ||
1742 | static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1743 | Socket *s = SOCKET(u); | |
a16e1123 LP |
1744 | |
1745 | assert(u); | |
1746 | assert(key); | |
1747 | assert(value); | |
a16e1123 LP |
1748 | |
1749 | if (streq(key, "state")) { | |
1750 | SocketState state; | |
1751 | ||
ee092817 LP |
1752 | state = socket_state_from_string(value); |
1753 | if (state < 0) | |
e821075a | 1754 | log_debug_unit(u->id, "Failed to parse state value %s", value); |
a16e1123 LP |
1755 | else |
1756 | s->deserialized_state = state; | |
cfc4eb4c LP |
1757 | } else if (streq(key, "result")) { |
1758 | SocketResult f; | |
a16e1123 | 1759 | |
cfc4eb4c LP |
1760 | f = socket_result_from_string(value); |
1761 | if (f < 0) | |
e821075a | 1762 | log_debug_unit(u->id, "Failed to parse result value %s", value); |
cfc4eb4c LP |
1763 | else if (f != SOCKET_SUCCESS) |
1764 | s->result = f; | |
a16e1123 LP |
1765 | |
1766 | } else if (streq(key, "n-accepted")) { | |
1767 | unsigned k; | |
1768 | ||
e364ad06 | 1769 | if (safe_atou(value, &k) < 0) |
e821075a | 1770 | log_debug_unit(u->id, "Failed to parse n-accepted value %s", value); |
a16e1123 LP |
1771 | else |
1772 | s->n_accepted += k; | |
1773 | } else if (streq(key, "control-pid")) { | |
5925dd3c | 1774 | pid_t pid; |
a16e1123 | 1775 | |
e364ad06 | 1776 | if (parse_pid(value, &pid) < 0) |
e821075a | 1777 | log_debug_unit(u->id, "Failed to parse control-pid value %s", value); |
a16e1123 | 1778 | else |
5925dd3c | 1779 | s->control_pid = pid; |
a16e1123 LP |
1780 | } else if (streq(key, "control-command")) { |
1781 | SocketExecCommand id; | |
1782 | ||
66870f90 ZJS |
1783 | id = socket_exec_command_from_string(value); |
1784 | if (id < 0) | |
e821075a | 1785 | log_debug_unit(u->id, "Failed to parse exec-command value %s", value); |
a16e1123 LP |
1786 | else { |
1787 | s->control_command_id = id; | |
1788 | s->control_command = s->exec_command[id]; | |
1789 | } | |
1790 | } else if (streq(key, "fifo")) { | |
1791 | int fd, skip = 0; | |
1792 | SocketPort *p; | |
1793 | ||
1794 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1795 | log_debug_unit(u->id, "Failed to parse fifo value %s", value); |
a16e1123 LP |
1796 | else { |
1797 | ||
1798 | LIST_FOREACH(port, p, s->ports) | |
b0a3f2bc LP |
1799 | if (p->type == SOCKET_FIFO && |
1800 | streq_ptr(p->path, value+skip)) | |
1801 | break; | |
1802 | ||
1803 | if (p) { | |
03e334a1 | 1804 | safe_close(p->fd); |
b0a3f2bc LP |
1805 | p->fd = fdset_remove(fds, fd); |
1806 | } | |
1807 | } | |
1808 | ||
1809 | } else if (streq(key, "special")) { | |
1810 | int fd, skip = 0; | |
1811 | SocketPort *p; | |
1812 | ||
1813 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1814 | log_debug_unit(u->id, "Failed to parse special value %s", value); |
b0a3f2bc LP |
1815 | else { |
1816 | ||
1817 | LIST_FOREACH(port, p, s->ports) | |
1818 | if (p->type == SOCKET_SPECIAL && | |
1819 | streq_ptr(p->path, value+skip)) | |
a16e1123 LP |
1820 | break; |
1821 | ||
1822 | if (p) { | |
03e334a1 | 1823 | safe_close(p->fd); |
a16e1123 LP |
1824 | p->fd = fdset_remove(fds, fd); |
1825 | } | |
1826 | } | |
1827 | ||
ee092817 LP |
1828 | } else if (streq(key, "mqueue")) { |
1829 | int fd, skip = 0; | |
1830 | SocketPort *p; | |
1831 | ||
1832 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1833 | log_debug_unit(u->id, "Failed to parse mqueue value %s", value); |
ee092817 LP |
1834 | else { |
1835 | ||
1836 | LIST_FOREACH(port, p, s->ports) | |
1837 | if (p->type == SOCKET_MQUEUE && | |
1838 | streq_ptr(p->path, value+skip)) | |
1839 | break; | |
1840 | ||
1841 | if (p) { | |
03e334a1 | 1842 | safe_close(p->fd); |
ee092817 LP |
1843 | p->fd = fdset_remove(fds, fd); |
1844 | } | |
1845 | } | |
1846 | ||
a16e1123 | 1847 | } else if (streq(key, "socket")) { |
27ca8d7a | 1848 | int fd, type, skip = 0; |
a16e1123 LP |
1849 | SocketPort *p; |
1850 | ||
27ca8d7a | 1851 | if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd)) |
e821075a | 1852 | log_debug_unit(u->id, "Failed to parse socket value %s", value); |
a16e1123 LP |
1853 | else { |
1854 | ||
1855 | LIST_FOREACH(port, p, s->ports) | |
27ca8d7a | 1856 | if (socket_address_is(&p->address, value+skip, type)) |
a16e1123 LP |
1857 | break; |
1858 | ||
1859 | if (p) { | |
03e334a1 | 1860 | safe_close(p->fd); |
a16e1123 LP |
1861 | p->fd = fdset_remove(fds, fd); |
1862 | } | |
1863 | } | |
1864 | ||
7a22745a LP |
1865 | } else if (streq(key, "netlink")) { |
1866 | int fd, skip = 0; | |
1867 | SocketPort *p; | |
1868 | ||
1869 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1870 | log_debug_unit(u->id, "Failed to parse socket value %s", value); |
7a22745a LP |
1871 | else { |
1872 | ||
1873 | LIST_FOREACH(port, p, s->ports) | |
1874 | if (socket_address_is_netlink(&p->address, value+skip)) | |
1875 | break; | |
1876 | ||
1877 | if (p) { | |
03e334a1 | 1878 | safe_close(p->fd); |
7a22745a LP |
1879 | p->fd = fdset_remove(fds, fd); |
1880 | } | |
1881 | } | |
a16e1123 | 1882 | } else |
e821075a | 1883 | log_debug_unit(UNIT(s)->id, "Unknown serialization key '%s'", key); |
a16e1123 LP |
1884 | |
1885 | return 0; | |
1886 | } | |
1887 | ||
01e10de3 LP |
1888 | static int socket_distribute_fds(Unit *u, FDSet *fds) { |
1889 | Socket *s = SOCKET(u); | |
1890 | SocketPort *p; | |
1891 | ||
1892 | assert(u); | |
1893 | ||
1894 | LIST_FOREACH(port, p, s->ports) { | |
1895 | Iterator i; | |
1896 | int fd; | |
1897 | ||
1898 | if (p->type != SOCKET_SOCKET) | |
1899 | continue; | |
1900 | ||
1901 | if (p->fd >= 0) | |
1902 | continue; | |
1903 | ||
1904 | FDSET_FOREACH(fd, fds, i) { | |
1905 | if (socket_address_matches_fd(&p->address, fd)) { | |
1906 | p->fd = fdset_remove(fds, fd); | |
1907 | s->deserialized_state = SOCKET_LISTENING; | |
1908 | break; | |
1909 | } | |
1910 | } | |
1911 | } | |
1912 | ||
1913 | return 0; | |
1914 | } | |
1915 | ||
44a6b1b6 | 1916 | _pure_ static UnitActiveState socket_active_state(Unit *u) { |
87f0e418 | 1917 | assert(u); |
5cb5a6ff | 1918 | |
acbb0225 | 1919 | return state_translation_table[SOCKET(u)->state]; |
5cb5a6ff LP |
1920 | } |
1921 | ||
44a6b1b6 | 1922 | _pure_ static const char *socket_sub_state_to_string(Unit *u) { |
10a94420 LP |
1923 | assert(u); |
1924 | ||
a16e1123 | 1925 | return socket_state_to_string(SOCKET(u)->state); |
10a94420 LP |
1926 | } |
1927 | ||
67419600 OS |
1928 | const char* socket_port_type_to_string(SocketPort *p) { |
1929 | ||
1930 | assert(p); | |
1931 | ||
1932 | switch (p->type) { | |
718db961 LP |
1933 | |
1934 | case SOCKET_SOCKET: | |
1935 | ||
1936 | switch (p->address.type) { | |
1937 | ||
1938 | case SOCK_STREAM: | |
1939 | return "Stream"; | |
1940 | ||
1941 | case SOCK_DGRAM: | |
1942 | return "Datagram"; | |
1943 | ||
1944 | case SOCK_SEQPACKET: | |
1945 | return "SequentialPacket"; | |
1946 | ||
1947 | case SOCK_RAW: | |
1948 | if (socket_address_family(&p->address) == AF_NETLINK) | |
1949 | return "Netlink"; | |
1950 | ||
1951 | default: | |
1952 | return NULL; | |
1953 | } | |
1954 | ||
1955 | case SOCKET_SPECIAL: | |
1956 | return "Special"; | |
1957 | ||
1958 | case SOCKET_MQUEUE: | |
1959 | return "MessageQueue"; | |
1960 | ||
1961 | case SOCKET_FIFO: | |
1962 | return "FIFO"; | |
1963 | ||
1964 | default: | |
1965 | return NULL; | |
67419600 OS |
1966 | } |
1967 | } | |
1968 | ||
44a6b1b6 | 1969 | _pure_ static bool socket_check_gc(Unit *u) { |
6cf6bbc2 LP |
1970 | Socket *s = SOCKET(u); |
1971 | ||
1972 | assert(u); | |
1973 | ||
1974 | return s->n_connections > 0; | |
1975 | } | |
1976 | ||
718db961 LP |
1977 | static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
1978 | SocketPort *p = userdata; | |
4f2d528d | 1979 | int cfd = -1; |
9152c765 | 1980 | |
718db961 | 1981 | assert(p); |
8d567588 | 1982 | assert(fd >= 0); |
9152c765 | 1983 | |
718db961 LP |
1984 | if (p->socket->state != SOCKET_LISTENING) |
1985 | return 0; | |
871d7de4 | 1986 | |
718db961 | 1987 | log_debug_unit(UNIT(p->socket)->id, "Incoming traffic on %s", UNIT(p->socket)->id); |
9152c765 | 1988 | |
718db961 | 1989 | if (revents != EPOLLIN) { |
641e01dc | 1990 | |
718db961 | 1991 | if (revents & EPOLLHUP) |
e821075a | 1992 | log_error_unit(UNIT(p->socket)->id, "%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", |
718db961 | 1993 | UNIT(p->socket)->id); |
641e01dc | 1994 | else |
e821075a | 1995 | log_error_unit(UNIT(p->socket)->id, "%s: Got unexpected poll event (0x%x) on socket.", |
718db961 | 1996 | UNIT(p->socket)->id, revents); |
641e01dc | 1997 | |
8d567588 | 1998 | goto fail; |
4f2d528d LP |
1999 | } |
2000 | ||
718db961 LP |
2001 | if (p->socket->accept && |
2002 | p->type == SOCKET_SOCKET && | |
2003 | socket_address_can_accept(&p->address)) { | |
2004 | ||
4f2d528d LP |
2005 | for (;;) { |
2006 | ||
b14eda96 LP |
2007 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK); |
2008 | if (cfd < 0) { | |
4f2d528d LP |
2009 | |
2010 | if (errno == EINTR) | |
2011 | continue; | |
2012 | ||
718db961 | 2013 | log_error_unit(UNIT(p->socket)->id, |
66870f90 | 2014 | "Failed to accept socket: %m"); |
8d567588 | 2015 | goto fail; |
4f2d528d LP |
2016 | } |
2017 | ||
2018 | break; | |
2019 | } | |
4fd5948e | 2020 | |
718db961 | 2021 | socket_apply_socket_options(p->socket, cfd); |
4f2d528d | 2022 | } |
9152c765 | 2023 | |
718db961 LP |
2024 | socket_enter_running(p->socket, cfd); |
2025 | return 0; | |
8d567588 LP |
2026 | |
2027 | fail: | |
718db961 LP |
2028 | socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES); |
2029 | return 0; | |
9152c765 LP |
2030 | } |
2031 | ||
87f0e418 LP |
2032 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2033 | Socket *s = SOCKET(u); | |
cfc4eb4c | 2034 | SocketResult f; |
5cb5a6ff LP |
2035 | |
2036 | assert(s); | |
034c6ed7 | 2037 | assert(pid >= 0); |
5cb5a6ff | 2038 | |
8c47c732 LP |
2039 | if (pid != s->control_pid) |
2040 | return; | |
542563ba | 2041 | |
034c6ed7 LP |
2042 | s->control_pid = 0; |
2043 | ||
96342de6 | 2044 | if (is_clean_exit(code, status, NULL)) |
cfc4eb4c LP |
2045 | f = SOCKET_SUCCESS; |
2046 | else if (code == CLD_EXITED) | |
2047 | f = SOCKET_FAILURE_EXIT_CODE; | |
2048 | else if (code == CLD_KILLED) | |
2049 | f = SOCKET_FAILURE_SIGNAL; | |
2050 | else if (code == CLD_DUMPED) | |
2051 | f = SOCKET_FAILURE_CORE_DUMP; | |
2052 | else | |
2053 | assert_not_reached("Unknown code"); | |
8c47c732 | 2054 | |
b708e7ce | 2055 | if (s->control_command) { |
6ea832a2 | 2056 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 2057 | |
b708e7ce | 2058 | if (s->control_command->ignore) |
cfc4eb4c | 2059 | f = SOCKET_SUCCESS; |
b708e7ce LP |
2060 | } |
2061 | ||
66870f90 ZJS |
2062 | log_full_unit(f == SOCKET_SUCCESS ? LOG_DEBUG : LOG_NOTICE, |
2063 | u->id, | |
2064 | "%s control process exited, code=%s status=%i", | |
2065 | u->id, sigchld_code_to_string(code), status); | |
034c6ed7 | 2066 | |
cfc4eb4c LP |
2067 | if (f != SOCKET_SUCCESS) |
2068 | s->result = f; | |
2069 | ||
2070 | if (s->control_command && | |
2071 | s->control_command->command_next && | |
2072 | f == SOCKET_SUCCESS) { | |
2073 | ||
66870f90 ZJS |
2074 | log_debug_unit(u->id, |
2075 | "%s running next command for state %s", | |
2076 | u->id, socket_state_to_string(s->state)); | |
cfc4eb4c | 2077 | socket_run_next(s); |
acbb0225 | 2078 | } else { |
a16e1123 LP |
2079 | s->control_command = NULL; |
2080 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
2081 | ||
034c6ed7 LP |
2082 | /* No further commands for this step, so let's figure |
2083 | * out what to do next */ | |
5cb5a6ff | 2084 | |
66870f90 ZJS |
2085 | log_debug_unit(u->id, |
2086 | "%s got final SIGCHLD for state %s", | |
2087 | u->id, socket_state_to_string(s->state)); | |
acbb0225 | 2088 | |
034c6ed7 LP |
2089 | switch (s->state) { |
2090 | ||
2091 | case SOCKET_START_PRE: | |
cfc4eb4c | 2092 | if (f == SOCKET_SUCCESS) |
acbb0225 | 2093 | socket_enter_start_post(s); |
034c6ed7 | 2094 | else |
cfc4eb4c | 2095 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f); |
034c6ed7 LP |
2096 | break; |
2097 | ||
2098 | case SOCKET_START_POST: | |
cfc4eb4c | 2099 | if (f == SOCKET_SUCCESS) |
e9af15c3 | 2100 | socket_enter_listening(s); |
034c6ed7 | 2101 | else |
cfc4eb4c | 2102 | socket_enter_stop_pre(s, f); |
034c6ed7 LP |
2103 | break; |
2104 | ||
2105 | case SOCKET_STOP_PRE: | |
2106 | case SOCKET_STOP_PRE_SIGTERM: | |
2107 | case SOCKET_STOP_PRE_SIGKILL: | |
cfc4eb4c | 2108 | socket_enter_stop_post(s, f); |
034c6ed7 LP |
2109 | break; |
2110 | ||
2111 | case SOCKET_STOP_POST: | |
80876c20 LP |
2112 | case SOCKET_FINAL_SIGTERM: |
2113 | case SOCKET_FINAL_SIGKILL: | |
cfc4eb4c | 2114 | socket_enter_dead(s, f); |
034c6ed7 LP |
2115 | break; |
2116 | ||
2117 | default: | |
2118 | assert_not_reached("Uh, control process died at wrong time."); | |
2119 | } | |
2120 | } | |
c4e2ceae LP |
2121 | |
2122 | /* Notify clients about changed exit status */ | |
2123 | unit_add_to_dbus_queue(u); | |
034c6ed7 | 2124 | } |
5cb5a6ff | 2125 | |
718db961 LP |
2126 | static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
2127 | Socket *s = SOCKET(userdata); | |
5cb5a6ff | 2128 | |
034c6ed7 | 2129 | assert(s); |
718db961 | 2130 | assert(s->timer_event_source == source); |
034c6ed7 LP |
2131 | |
2132 | switch (s->state) { | |
2133 | ||
2134 | case SOCKET_START_PRE: | |
718db961 LP |
2135 | log_warning_unit(UNIT(s)->id, |
2136 | "%s starting timed out. Terminating.", UNIT(s)->id); | |
cfc4eb4c | 2137 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
da19d5c1 | 2138 | break; |
80876c20 | 2139 | |
034c6ed7 | 2140 | case SOCKET_START_POST: |
718db961 LP |
2141 | log_warning_unit(UNIT(s)->id, |
2142 | "%s starting timed out. Stopping.", UNIT(s)->id); | |
cfc4eb4c | 2143 | socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2144 | break; |
2145 | ||
2146 | case SOCKET_STOP_PRE: | |
718db961 LP |
2147 | log_warning_unit(UNIT(s)->id, |
2148 | "%s stopping timed out. Terminating.", UNIT(s)->id); | |
cfc4eb4c | 2149 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2150 | break; |
2151 | ||
2152 | case SOCKET_STOP_PRE_SIGTERM: | |
4819ff03 | 2153 | if (s->kill_context.send_sigkill) { |
718db961 LP |
2154 | log_warning_unit(UNIT(s)->id, |
2155 | "%s stopping timed out. Killing.", UNIT(s)->id); | |
cfc4eb4c | 2156 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2157 | } else { |
718db961 | 2158 | log_warning_unit(UNIT(s)->id, |
66870f90 | 2159 | "%s stopping timed out. Skipping SIGKILL. Ignoring.", |
718db961 | 2160 | UNIT(s)->id); |
cfc4eb4c | 2161 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2162 | } |
034c6ed7 LP |
2163 | break; |
2164 | ||
2165 | case SOCKET_STOP_PRE_SIGKILL: | |
718db961 LP |
2166 | log_warning_unit(UNIT(s)->id, |
2167 | "%s still around after SIGKILL. Ignoring.", UNIT(s)->id); | |
cfc4eb4c | 2168 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2169 | break; |
2170 | ||
2171 | case SOCKET_STOP_POST: | |
718db961 LP |
2172 | log_warning_unit(UNIT(s)->id, |
2173 | "%s stopping timed out (2). Terminating.", UNIT(s)->id); | |
cfc4eb4c | 2174 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2175 | break; |
2176 | ||
80876c20 | 2177 | case SOCKET_FINAL_SIGTERM: |
4819ff03 | 2178 | if (s->kill_context.send_sigkill) { |
718db961 LP |
2179 | log_warning_unit(UNIT(s)->id, |
2180 | "%s stopping timed out (2). Killing.", UNIT(s)->id); | |
cfc4eb4c | 2181 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2182 | } else { |
718db961 | 2183 | log_warning_unit(UNIT(s)->id, |
66870f90 | 2184 | "%s stopping timed out (2). Skipping SIGKILL. Ignoring.", |
718db961 | 2185 | UNIT(s)->id); |
cfc4eb4c | 2186 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2187 | } |
034c6ed7 LP |
2188 | break; |
2189 | ||
80876c20 | 2190 | case SOCKET_FINAL_SIGKILL: |
718db961 | 2191 | log_warning_unit(UNIT(s)->id, |
66870f90 | 2192 | "%s still around after SIGKILL (2). Entering failed mode.", |
718db961 | 2193 | UNIT(s)->id); |
cfc4eb4c | 2194 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2195 | break; |
2196 | ||
2197 | default: | |
2198 | assert_not_reached("Timeout at wrong time."); | |
2199 | } | |
718db961 LP |
2200 | |
2201 | return 0; | |
5cb5a6ff LP |
2202 | } |
2203 | ||
44d8db9e LP |
2204 | int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { |
2205 | int *rfds; | |
2206 | unsigned rn_fds, k; | |
2207 | SocketPort *p; | |
2208 | ||
2209 | assert(s); | |
2210 | assert(fds); | |
2211 | assert(n_fds); | |
2212 | ||
2213 | /* Called from the service code for requesting our fds */ | |
2214 | ||
2215 | rn_fds = 0; | |
2216 | LIST_FOREACH(port, p, s->ports) | |
2217 | if (p->fd >= 0) | |
2218 | rn_fds++; | |
2219 | ||
de3756ab LP |
2220 | if (rn_fds <= 0) { |
2221 | *fds = NULL; | |
2222 | *n_fds = 0; | |
2223 | return 0; | |
2224 | } | |
2225 | ||
e364ad06 | 2226 | if (!(rfds = new(int, rn_fds))) |
44d8db9e LP |
2227 | return -ENOMEM; |
2228 | ||
2229 | k = 0; | |
2230 | LIST_FOREACH(port, p, s->ports) | |
2231 | if (p->fd >= 0) | |
2232 | rfds[k++] = p->fd; | |
2233 | ||
2234 | assert(k == rn_fds); | |
2235 | ||
2236 | *fds = rfds; | |
2237 | *n_fds = rn_fds; | |
2238 | ||
2239 | return 0; | |
2240 | } | |
2241 | ||
e821075a LP |
2242 | static void socket_reset_failed(Unit *u) { |
2243 | Socket *s = SOCKET(u); | |
2244 | ||
2245 | assert(s); | |
2246 | ||
2247 | if (s->state == SOCKET_FAILED) | |
2248 | socket_set_state(s, SOCKET_DEAD); | |
2249 | ||
2250 | s->result = SOCKET_SUCCESS; | |
2251 | } | |
2252 | ||
d137a488 | 2253 | static void socket_notify_service_dead(Socket *s, bool failed_permanent) { |
ceee3d82 LP |
2254 | assert(s); |
2255 | ||
6cf6bbc2 LP |
2256 | /* The service is dead. Dang! |
2257 | * | |
2258 | * This is strictly for one-instance-for-all-connections | |
2259 | * services. */ | |
ceee3d82 LP |
2260 | |
2261 | if (s->state == SOCKET_RUNNING) { | |
e821075a | 2262 | log_debug_unit(UNIT(s)->id, "%s got notified about service death (failed permanently: %s)", UNIT(s)->id, yes_no(failed_permanent)); |
6bda96a0 LP |
2263 | if (failed_permanent) |
2264 | socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_FAILED_PERMANENT); | |
c2f34808 MS |
2265 | else |
2266 | socket_enter_listening(s); | |
ceee3d82 LP |
2267 | } |
2268 | } | |
2269 | ||
6cf6bbc2 LP |
2270 | void socket_connection_unref(Socket *s) { |
2271 | assert(s); | |
2272 | ||
2273 | /* The service is dead. Yay! | |
2274 | * | |
35b8ca3a | 2275 | * This is strictly for one-instance-per-connection |
6cf6bbc2 LP |
2276 | * services. */ |
2277 | ||
2278 | assert(s->n_connections > 0); | |
2279 | s->n_connections--; | |
2280 | ||
e821075a | 2281 | log_debug_unit(UNIT(s)->id, "%s: One connection closed, %u left.", UNIT(s)->id, s->n_connections); |
5632e374 LP |
2282 | } |
2283 | ||
d137a488 UTL |
2284 | static void socket_trigger_notify(Unit *u, Unit *other) { |
2285 | Socket *s = SOCKET(u); | |
e821075a | 2286 | Service *se; |
d137a488 UTL |
2287 | |
2288 | assert(u); | |
2289 | assert(other); | |
2290 | ||
2291 | /* Don't propagate state changes from the service if we are | |
2292 | already down or accepting connections */ | |
2293 | if ((s->state != SOCKET_RUNNING && | |
2294 | s->state != SOCKET_LISTENING) || | |
2295 | s->accept) | |
2296 | return; | |
2297 | ||
2298 | if (other->load_state != UNIT_LOADED || | |
2299 | other->type != UNIT_SERVICE) | |
2300 | return; | |
2301 | ||
e821075a LP |
2302 | se = SERVICE(other); |
2303 | ||
d137a488 UTL |
2304 | if (se->state == SERVICE_FAILED) |
2305 | socket_notify_service_dead(s, se->result == SERVICE_FAILURE_START_LIMIT); | |
2306 | ||
2307 | if (se->state == SERVICE_DEAD || | |
2308 | se->state == SERVICE_STOP || | |
2309 | se->state == SERVICE_STOP_SIGTERM || | |
2310 | se->state == SERVICE_STOP_SIGKILL || | |
2311 | se->state == SERVICE_STOP_POST || | |
2312 | se->state == SERVICE_FINAL_SIGTERM || | |
2313 | se->state == SERVICE_FINAL_SIGKILL || | |
2314 | se->state == SERVICE_AUTO_RESTART) | |
2315 | socket_notify_service_dead(s, false); | |
2316 | ||
2317 | if (se->state == SERVICE_RUNNING) | |
2318 | socket_set_state(s, SOCKET_RUNNING); | |
2319 | } | |
2320 | ||
718db961 | 2321 | static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
814cc562 | 2322 | return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error); |
8a0867d6 LP |
2323 | } |
2324 | ||
68db7a3b ZJS |
2325 | static int socket_get_timeout(Unit *u, uint64_t *timeout) { |
2326 | Socket *s = SOCKET(u); | |
2327 | int r; | |
2328 | ||
2329 | if (!s->timer_event_source) | |
2330 | return 0; | |
2331 | ||
2332 | r = sd_event_source_get_time(s->timer_event_source, timeout); | |
2333 | if (r < 0) | |
2334 | return r; | |
2335 | ||
2336 | return 1; | |
2337 | } | |
2338 | ||
a16e1123 LP |
2339 | static const char* const socket_state_table[_SOCKET_STATE_MAX] = { |
2340 | [SOCKET_DEAD] = "dead", | |
2341 | [SOCKET_START_PRE] = "start-pre", | |
2342 | [SOCKET_START_POST] = "start-post", | |
2343 | [SOCKET_LISTENING] = "listening", | |
2344 | [SOCKET_RUNNING] = "running", | |
2345 | [SOCKET_STOP_PRE] = "stop-pre", | |
2346 | [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm", | |
2347 | [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill", | |
2348 | [SOCKET_STOP_POST] = "stop-post", | |
2349 | [SOCKET_FINAL_SIGTERM] = "final-sigterm", | |
2350 | [SOCKET_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 2351 | [SOCKET_FAILED] = "failed" |
a16e1123 LP |
2352 | }; |
2353 | ||
2354 | DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState); | |
2355 | ||
2356 | static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { | |
2357 | [SOCKET_EXEC_START_PRE] = "StartPre", | |
2358 | [SOCKET_EXEC_START_POST] = "StartPost", | |
2359 | [SOCKET_EXEC_STOP_PRE] = "StopPre", | |
2360 | [SOCKET_EXEC_STOP_POST] = "StopPost" | |
2361 | }; | |
2362 | ||
2363 | DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); | |
2364 | ||
cfc4eb4c LP |
2365 | static const char* const socket_result_table[_SOCKET_RESULT_MAX] = { |
2366 | [SOCKET_SUCCESS] = "success", | |
2367 | [SOCKET_FAILURE_RESOURCES] = "resources", | |
2368 | [SOCKET_FAILURE_TIMEOUT] = "timeout", | |
2369 | [SOCKET_FAILURE_EXIT_CODE] = "exit-code", | |
2370 | [SOCKET_FAILURE_SIGNAL] = "signal", | |
c2f34808 | 2371 | [SOCKET_FAILURE_CORE_DUMP] = "core-dump", |
6bda96a0 | 2372 | [SOCKET_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent" |
cfc4eb4c LP |
2373 | }; |
2374 | ||
2375 | DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult); | |
2376 | ||
87f0e418 | 2377 | const UnitVTable socket_vtable = { |
7d17cfbc | 2378 | .object_size = sizeof(Socket), |
718db961 LP |
2379 | .exec_context_offset = offsetof(Socket, exec_context), |
2380 | .cgroup_context_offset = offsetof(Socket, cgroup_context), | |
2381 | .kill_context_offset = offsetof(Socket, kill_context), | |
613b411c | 2382 | .exec_runtime_offset = offsetof(Socket, exec_runtime), |
3ef63c31 | 2383 | |
f975e971 LP |
2384 | .sections = |
2385 | "Unit\0" | |
2386 | "Socket\0" | |
2387 | "Install\0", | |
4ad49000 | 2388 | .private_section = "Socket", |
71645aca | 2389 | |
034c6ed7 LP |
2390 | .init = socket_init, |
2391 | .done = socket_done, | |
a16e1123 LP |
2392 | .load = socket_load, |
2393 | ||
2394 | .coldplug = socket_coldplug, | |
034c6ed7 | 2395 | |
5cb5a6ff LP |
2396 | .dump = socket_dump, |
2397 | ||
542563ba LP |
2398 | .start = socket_start, |
2399 | .stop = socket_stop, | |
5cb5a6ff | 2400 | |
718db961 LP |
2401 | .kill = socket_kill, |
2402 | ||
68db7a3b ZJS |
2403 | .get_timeout = socket_get_timeout, |
2404 | ||
a16e1123 LP |
2405 | .serialize = socket_serialize, |
2406 | .deserialize_item = socket_deserialize_item, | |
01e10de3 | 2407 | .distribute_fds = socket_distribute_fds, |
a16e1123 | 2408 | |
5cb5a6ff | 2409 | .active_state = socket_active_state, |
10a94420 | 2410 | .sub_state_to_string = socket_sub_state_to_string, |
5cb5a6ff | 2411 | |
6cf6bbc2 LP |
2412 | .check_gc = socket_check_gc, |
2413 | ||
034c6ed7 | 2414 | .sigchld_event = socket_sigchld_event, |
4139c1b2 | 2415 | |
d137a488 UTL |
2416 | .trigger_notify = socket_trigger_notify, |
2417 | ||
fdf20a31 | 2418 | .reset_failed = socket_reset_failed, |
5632e374 | 2419 | |
c4e2ceae | 2420 | .bus_interface = "org.freedesktop.systemd1.Socket", |
718db961 | 2421 | .bus_vtable = bus_socket_vtable, |
74c964d3 LP |
2422 | .bus_set_property = bus_socket_set_property, |
2423 | .bus_commit_properties = bus_socket_commit_properties, | |
c6918296 MS |
2424 | |
2425 | .status_message_formats = { | |
2426 | /*.starting_stopping = { | |
2427 | [0] = "Starting socket %s...", | |
2428 | [1] = "Stopping socket %s...", | |
2429 | },*/ | |
2430 | .finished_start_job = { | |
2431 | [JOB_DONE] = "Listening on %s.", | |
2432 | [JOB_FAILED] = "Failed to listen on %s.", | |
2433 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
2434 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
2435 | }, | |
2436 | .finished_stop_job = { | |
2437 | [JOB_DONE] = "Closed %s.", | |
2438 | [JOB_FAILED] = "Failed stopping %s.", | |
2439 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
2440 | }, | |
2441 | }, | |
5cb5a6ff | 2442 | }; |