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