]>
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, |
09812eb7 | 1257 | 0, |
f2b68789 | 1258 | NULL, |
613b411c | 1259 | s->exec_runtime, |
9e2f7c11 LP |
1260 | &pid); |
1261 | ||
1262 | strv_free(argv); | |
1263 | if (r < 0) | |
034c6ed7 LP |
1264 | goto fail; |
1265 | ||
718db961 LP |
1266 | r = unit_watch_pid(UNIT(s), pid); |
1267 | if (r < 0) | |
034c6ed7 LP |
1268 | /* FIXME: we need to do something here */ |
1269 | goto fail; | |
83c60c9f | 1270 | |
034c6ed7 LP |
1271 | *_pid = pid; |
1272 | ||
1273 | return 0; | |
1274 | ||
1275 | fail: | |
718db961 | 1276 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
83c60c9f LP |
1277 | |
1278 | return r; | |
542563ba LP |
1279 | } |
1280 | ||
cfc4eb4c | 1281 | static void socket_enter_dead(Socket *s, SocketResult f) { |
034c6ed7 LP |
1282 | assert(s); |
1283 | ||
cfc4eb4c LP |
1284 | if (f != SOCKET_SUCCESS) |
1285 | s->result = f; | |
034c6ed7 | 1286 | |
613b411c LP |
1287 | exec_runtime_destroy(s->exec_runtime); |
1288 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
1289 | ||
cfc4eb4c | 1290 | socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD); |
034c6ed7 LP |
1291 | } |
1292 | ||
cfc4eb4c | 1293 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f); |
80876c20 | 1294 | |
cfc4eb4c | 1295 | static void socket_enter_stop_post(Socket *s, SocketResult f) { |
034c6ed7 LP |
1296 | int r; |
1297 | assert(s); | |
1298 | ||
cfc4eb4c LP |
1299 | if (f != SOCKET_SUCCESS) |
1300 | s->result = f; | |
034c6ed7 | 1301 | |
5e94833f LP |
1302 | socket_unwatch_control_pid(s); |
1303 | ||
a16e1123 LP |
1304 | s->control_command_id = SOCKET_EXEC_STOP_POST; |
1305 | ||
80876c20 | 1306 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) { |
e537352b | 1307 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1308 | goto fail; |
1309 | ||
80876c20 LP |
1310 | socket_set_state(s, SOCKET_STOP_POST); |
1311 | } else | |
cfc4eb4c | 1312 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS); |
034c6ed7 LP |
1313 | |
1314 | return; | |
1315 | ||
1316 | fail: | |
66870f90 ZJS |
1317 | log_warning_unit(UNIT(s)->id, |
1318 | "%s failed to run 'stop-post' task: %s", | |
1319 | UNIT(s)->id, strerror(-r)); | |
cfc4eb4c | 1320 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1321 | } |
1322 | ||
cfc4eb4c | 1323 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) { |
034c6ed7 LP |
1324 | int r; |
1325 | ||
1326 | assert(s); | |
1327 | ||
cfc4eb4c LP |
1328 | if (f != SOCKET_SUCCESS) |
1329 | s->result = f; | |
034c6ed7 | 1330 | |
cd2086fe LP |
1331 | r = unit_kill_context( |
1332 | UNIT(s), | |
1333 | &s->kill_context, | |
1334 | state != SOCKET_STOP_PRE_SIGTERM && state != SOCKET_FINAL_SIGTERM, | |
1335 | -1, | |
1336 | s->control_pid, | |
1337 | false); | |
1338 | if (r < 0) | |
1339 | goto fail; | |
034c6ed7 | 1340 | |
cd2086fe | 1341 | if (r > 0) { |
718db961 | 1342 | r = socket_arm_timer(s); |
36697dc0 | 1343 | if (r < 0) |
80876c20 | 1344 | goto fail; |
d6ea93e3 | 1345 | |
80876c20 LP |
1346 | socket_set_state(s, state); |
1347 | } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
cfc4eb4c | 1348 | socket_enter_stop_post(s, SOCKET_SUCCESS); |
80876c20 | 1349 | else |
cfc4eb4c | 1350 | socket_enter_dead(s, SOCKET_SUCCESS); |
034c6ed7 LP |
1351 | |
1352 | return; | |
1353 | ||
1354 | fail: | |
e821075a | 1355 | log_warning_unit(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r)); |
034c6ed7 LP |
1356 | |
1357 | if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
cfc4eb4c | 1358 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 1359 | else |
cfc4eb4c | 1360 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1361 | } |
1362 | ||
cfc4eb4c | 1363 | static void socket_enter_stop_pre(Socket *s, SocketResult f) { |
034c6ed7 LP |
1364 | int r; |
1365 | assert(s); | |
1366 | ||
cfc4eb4c LP |
1367 | if (f != SOCKET_SUCCESS) |
1368 | s->result = f; | |
034c6ed7 | 1369 | |
5e94833f LP |
1370 | socket_unwatch_control_pid(s); |
1371 | ||
a16e1123 LP |
1372 | s->control_command_id = SOCKET_EXEC_STOP_PRE; |
1373 | ||
80876c20 | 1374 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) { |
e537352b | 1375 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1376 | goto fail; |
1377 | ||
80876c20 LP |
1378 | socket_set_state(s, SOCKET_STOP_PRE); |
1379 | } else | |
cfc4eb4c | 1380 | socket_enter_stop_post(s, SOCKET_SUCCESS); |
034c6ed7 LP |
1381 | |
1382 | return; | |
1383 | ||
1384 | fail: | |
e821075a | 1385 | log_warning_unit(UNIT(s)->id, "%s failed to run 'stop-pre' task: %s", UNIT(s)->id, strerror(-r)); |
cfc4eb4c | 1386 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1387 | } |
1388 | ||
e9af15c3 LP |
1389 | static void socket_enter_listening(Socket *s) { |
1390 | int r; | |
1391 | assert(s); | |
1392 | ||
cfc4eb4c LP |
1393 | r = socket_watch_fds(s); |
1394 | if (r < 0) { | |
e821075a | 1395 | log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r)); |
e9af15c3 LP |
1396 | goto fail; |
1397 | } | |
1398 | ||
1399 | socket_set_state(s, SOCKET_LISTENING); | |
1400 | return; | |
1401 | ||
1402 | fail: | |
cfc4eb4c | 1403 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
e9af15c3 LP |
1404 | } |
1405 | ||
034c6ed7 LP |
1406 | static void socket_enter_start_post(Socket *s) { |
1407 | int r; | |
1408 | assert(s); | |
1409 | ||
cfc4eb4c LP |
1410 | r = socket_open_fds(s); |
1411 | if (r < 0) { | |
e821075a | 1412 | log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r)); |
034c6ed7 LP |
1413 | goto fail; |
1414 | } | |
1415 | ||
5e94833f LP |
1416 | socket_unwatch_control_pid(s); |
1417 | ||
a16e1123 LP |
1418 | s->control_command_id = SOCKET_EXEC_START_POST; |
1419 | ||
80876c20 | 1420 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { |
cfc4eb4c LP |
1421 | r = socket_spawn(s, s->control_command, &s->control_pid); |
1422 | if (r < 0) { | |
e821075a | 1423 | log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r)); |
034c6ed7 LP |
1424 | goto fail; |
1425 | } | |
1426 | ||
80876c20 LP |
1427 | socket_set_state(s, SOCKET_START_POST); |
1428 | } else | |
e9af15c3 | 1429 | socket_enter_listening(s); |
034c6ed7 LP |
1430 | |
1431 | return; | |
1432 | ||
1433 | fail: | |
cfc4eb4c | 1434 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1435 | } |
1436 | ||
1437 | static void socket_enter_start_pre(Socket *s) { | |
1438 | int r; | |
1439 | assert(s); | |
1440 | ||
5e94833f LP |
1441 | socket_unwatch_control_pid(s); |
1442 | ||
a16e1123 LP |
1443 | s->control_command_id = SOCKET_EXEC_START_PRE; |
1444 | ||
80876c20 | 1445 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { |
e821075a LP |
1446 | r = socket_spawn(s, s->control_command, &s->control_pid); |
1447 | if (r < 0) | |
034c6ed7 LP |
1448 | goto fail; |
1449 | ||
80876c20 LP |
1450 | socket_set_state(s, SOCKET_START_PRE); |
1451 | } else | |
034c6ed7 LP |
1452 | socket_enter_start_post(s); |
1453 | ||
1454 | return; | |
1455 | ||
1456 | fail: | |
e821075a | 1457 | log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r)); |
cfc4eb4c | 1458 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1459 | } |
1460 | ||
4f2d528d | 1461 | static void socket_enter_running(Socket *s, int cfd) { |
718db961 | 1462 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 LP |
1463 | int r; |
1464 | ||
1465 | assert(s); | |
1466 | ||
ba3e67a7 LP |
1467 | /* We don't take connections anymore if we are supposed to |
1468 | * shut down anyway */ | |
31afa0a4 | 1469 | if (unit_stop_pending(UNIT(s))) { |
e821075a LP |
1470 | |
1471 | log_debug_unit(UNIT(s)->id, "Suppressing connection request on %s since unit stop is scheduled.", UNIT(s)->id); | |
5d909e3e | 1472 | |
7c610628 LP |
1473 | if (cfd >= 0) |
1474 | close_nointr_nofail(cfd); | |
1475 | else { | |
1476 | /* Flush all sockets by closing and reopening them */ | |
1477 | socket_close_fds(s); | |
1478 | ||
16ac4014 LP |
1479 | r = socket_open_fds(s); |
1480 | if (r < 0) { | |
1481 | log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r)); | |
1482 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
1483 | return; | |
1484 | } | |
1485 | ||
1a710b43 MS |
1486 | r = socket_watch_fds(s); |
1487 | if (r < 0) { | |
e821075a | 1488 | log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r)); |
cfc4eb4c | 1489 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
7c610628 LP |
1490 | } |
1491 | } | |
1492 | ||
ba3e67a7 LP |
1493 | return; |
1494 | } | |
1495 | ||
4f2d528d | 1496 | if (cfd < 0) { |
57020a3a | 1497 | Iterator i; |
e821075a | 1498 | Unit *other; |
f976f3f6 | 1499 | bool pending = false; |
f976f3f6 LP |
1500 | |
1501 | /* If there's already a start pending don't bother to | |
1502 | * do anything */ | |
e821075a LP |
1503 | SET_FOREACH(other, UNIT(s)->dependencies[UNIT_TRIGGERS], i) |
1504 | if (unit_active_or_pending(other)) { | |
57020a3a LP |
1505 | pending = true; |
1506 | break; | |
1507 | } | |
f976f3f6 | 1508 | |
1a710b43 MS |
1509 | if (!pending) { |
1510 | r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, true, &error, NULL); | |
1511 | if (r < 0) | |
f976f3f6 | 1512 | goto fail; |
1a710b43 | 1513 | } |
4f2d528d LP |
1514 | |
1515 | socket_set_state(s, SOCKET_RUNNING); | |
1516 | } else { | |
e55001eb | 1517 | _cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL; |
b15bdda8 | 1518 | Service *service; |
4f2d528d | 1519 | |
6cf6bbc2 | 1520 | if (s->n_connections >= s->max_connections) { |
e821075a | 1521 | log_warning_unit(UNIT(s)->id, "%s: Too many incoming connections (%u)", UNIT(s)->id, s->n_connections); |
6cf6bbc2 LP |
1522 | close_nointr_nofail(cfd); |
1523 | return; | |
1524 | } | |
1525 | ||
1a710b43 MS |
1526 | r = socket_instantiate_service(s); |
1527 | if (r < 0) | |
b15bdda8 LP |
1528 | goto fail; |
1529 | ||
1a710b43 MS |
1530 | r = instance_from_socket(cfd, s->n_accepted, &instance); |
1531 | if (r < 0) { | |
1532 | if (r != -ENOTCONN) | |
1533 | goto fail; | |
1534 | ||
1535 | /* ENOTCONN is legitimate if TCP RST was received. | |
1536 | * This connection is over, but the socket unit lives on. */ | |
1537 | close_nointr_nofail(cfd); | |
1538 | return; | |
1539 | } | |
4f2d528d | 1540 | |
1a710b43 MS |
1541 | prefix = unit_name_to_prefix(UNIT(s)->id); |
1542 | if (!prefix) { | |
4f2d528d LP |
1543 | r = -ENOMEM; |
1544 | goto fail; | |
1545 | } | |
1546 | ||
1547 | name = unit_name_build(prefix, instance, ".service"); | |
b6dbbe1c | 1548 | if (!name) { |
4f2d528d | 1549 | r = -ENOMEM; |
b6dbbe1c LP |
1550 | goto fail; |
1551 | } | |
4f2d528d | 1552 | |
1a710b43 | 1553 | r = unit_add_name(UNIT_DEREF(s->service), name); |
e55001eb | 1554 | if (r < 0) |
4f2d528d | 1555 | goto fail; |
b15bdda8 | 1556 | |
57020a3a LP |
1557 | service = SERVICE(UNIT_DEREF(s->service)); |
1558 | unit_ref_unset(&s->service); | |
b15bdda8 | 1559 | s->n_accepted ++; |
4f2d528d | 1560 | |
1124fe6f | 1561 | UNIT(service)->no_gc = false; |
6c073082 | 1562 | |
b15bdda8 | 1563 | unit_choose_id(UNIT(service), name); |
b15bdda8 | 1564 | |
1a710b43 MS |
1565 | r = service_set_socket_fd(service, cfd, s); |
1566 | if (r < 0) | |
4f2d528d LP |
1567 | goto fail; |
1568 | ||
1569 | cfd = -1; | |
6cf6bbc2 LP |
1570 | s->n_connections ++; |
1571 | ||
1a710b43 MS |
1572 | r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL); |
1573 | if (r < 0) | |
4f2d528d | 1574 | goto fail; |
c4e2ceae LP |
1575 | |
1576 | /* Notify clients about changed counters */ | |
1577 | unit_add_to_dbus_queue(UNIT(s)); | |
4f2d528d | 1578 | } |
034c6ed7 | 1579 | |
034c6ed7 LP |
1580 | return; |
1581 | ||
1582 | fail: | |
e821075a LP |
1583 | 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", |
1584 | UNIT(s)->id, cfd >= 0 ? "template" : "non-template", | |
718db961 | 1585 | bus_error_message(&error, r)); |
e821075a | 1586 | |
60089004 | 1587 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
4f2d528d LP |
1588 | |
1589 | if (cfd >= 0) | |
1590 | close_nointr_nofail(cfd); | |
034c6ed7 LP |
1591 | } |
1592 | ||
cfc4eb4c | 1593 | static void socket_run_next(Socket *s) { |
034c6ed7 LP |
1594 | int r; |
1595 | ||
1596 | assert(s); | |
1597 | assert(s->control_command); | |
1598 | assert(s->control_command->command_next); | |
1599 | ||
5e94833f LP |
1600 | socket_unwatch_control_pid(s); |
1601 | ||
034c6ed7 LP |
1602 | s->control_command = s->control_command->command_next; |
1603 | ||
e821075a LP |
1604 | r = socket_spawn(s, s->control_command, &s->control_pid); |
1605 | if (r < 0) | |
034c6ed7 LP |
1606 | goto fail; |
1607 | ||
1608 | return; | |
1609 | ||
1610 | fail: | |
e821075a | 1611 | log_warning_unit(UNIT(s)->id, "%s failed to run next task: %s", UNIT(s)->id, strerror(-r)); |
80876c20 LP |
1612 | |
1613 | if (s->state == SOCKET_START_POST) | |
cfc4eb4c | 1614 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 1615 | else if (s->state == SOCKET_STOP_POST) |
cfc4eb4c | 1616 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 1617 | else |
cfc4eb4c | 1618 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
1619 | } |
1620 | ||
87f0e418 LP |
1621 | static int socket_start(Unit *u) { |
1622 | Socket *s = SOCKET(u); | |
83c60c9f LP |
1623 | |
1624 | assert(s); | |
1625 | ||
034c6ed7 LP |
1626 | /* We cannot fulfill this request right now, try again later |
1627 | * please! */ | |
1628 | if (s->state == SOCKET_STOP_PRE || | |
1629 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1630 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1631 | s->state == SOCKET_STOP_POST || | |
80876c20 LP |
1632 | s->state == SOCKET_FINAL_SIGTERM || |
1633 | s->state == SOCKET_FINAL_SIGKILL) | |
034c6ed7 LP |
1634 | return -EAGAIN; |
1635 | ||
83c60c9f LP |
1636 | if (s->state == SOCKET_START_PRE || |
1637 | s->state == SOCKET_START_POST) | |
034c6ed7 | 1638 | return 0; |
83c60c9f | 1639 | |
034c6ed7 | 1640 | /* Cannot run this without the service being around */ |
9444b1f2 | 1641 | if (UNIT_ISSET(s->service)) { |
57020a3a LP |
1642 | Service *service; |
1643 | ||
1644 | service = SERVICE(UNIT_DEREF(s->service)); | |
1645 | ||
1124fe6f | 1646 | if (UNIT(service)->load_state != UNIT_LOADED) { |
e821075a | 1647 | log_error_unit(u->id, "Socket service %s not loaded, refusing.", UNIT(service)->id); |
4f2d528d | 1648 | return -ENOENT; |
4ac9236f | 1649 | } |
4f2d528d | 1650 | |
35b8ca3a | 1651 | /* If the service is already active we cannot start the |
4f2d528d | 1652 | * socket */ |
57020a3a LP |
1653 | if (service->state != SERVICE_DEAD && |
1654 | service->state != SERVICE_FAILED && | |
1655 | service->state != SERVICE_AUTO_RESTART) { | |
e821075a | 1656 | log_error_unit(u->id, "Socket service %s already active, refusing.", UNIT(service)->id); |
4f2d528d | 1657 | return -EBUSY; |
4ac9236f | 1658 | } |
7b4bf06b LP |
1659 | |
1660 | #ifdef HAVE_SYSV_COMPAT | |
1b64d026 | 1661 | if (service->is_sysv) { |
d7607eac | 1662 | log_error_unit(u->id, |
66870f90 | 1663 | "Using SysV services for socket activation is not supported. Refusing."); |
7b4bf06b LP |
1664 | return -ENOENT; |
1665 | } | |
1666 | #endif | |
4f2d528d | 1667 | } |
e537352b | 1668 | |
fdf20a31 | 1669 | assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED); |
83c60c9f | 1670 | |
cfc4eb4c | 1671 | s->result = SOCKET_SUCCESS; |
034c6ed7 | 1672 | socket_enter_start_pre(s); |
e821075a | 1673 | |
034c6ed7 LP |
1674 | return 0; |
1675 | } | |
83c60c9f | 1676 | |
87f0e418 LP |
1677 | static int socket_stop(Unit *u) { |
1678 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
1679 | |
1680 | assert(s); | |
1681 | ||
e537352b LP |
1682 | /* Already on it */ |
1683 | if (s->state == SOCKET_STOP_PRE || | |
1684 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1685 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1686 | s->state == SOCKET_STOP_POST || | |
80876c20 | 1687 | s->state == SOCKET_FINAL_SIGTERM || |
3f6c78dc | 1688 | s->state == SOCKET_FINAL_SIGKILL) |
e537352b LP |
1689 | return 0; |
1690 | ||
3f6c78dc LP |
1691 | /* If there's already something running we go directly into |
1692 | * kill mode. */ | |
1693 | if (s->state == SOCKET_START_PRE || | |
1694 | s->state == SOCKET_START_POST) { | |
cfc4eb4c | 1695 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS); |
3f6c78dc LP |
1696 | return -EAGAIN; |
1697 | } | |
1698 | ||
034c6ed7 | 1699 | assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING); |
83c60c9f | 1700 | |
cfc4eb4c | 1701 | socket_enter_stop_pre(s, SOCKET_SUCCESS); |
542563ba LP |
1702 | return 0; |
1703 | } | |
1704 | ||
a16e1123 LP |
1705 | static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { |
1706 | Socket *s = SOCKET(u); | |
1707 | SocketPort *p; | |
1708 | int r; | |
1709 | ||
1710 | assert(u); | |
1711 | assert(f); | |
1712 | assert(fds); | |
1713 | ||
1714 | unit_serialize_item(u, f, "state", socket_state_to_string(s->state)); | |
cfc4eb4c | 1715 | unit_serialize_item(u, f, "result", socket_result_to_string(s->result)); |
a16e1123 LP |
1716 | unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted); |
1717 | ||
1718 | if (s->control_pid > 0) | |
5925dd3c | 1719 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); |
a16e1123 LP |
1720 | |
1721 | if (s->control_command_id >= 0) | |
1722 | unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id)); | |
1723 | ||
1724 | LIST_FOREACH(port, p, s->ports) { | |
1725 | int copy; | |
1726 | ||
1727 | if (p->fd < 0) | |
1728 | continue; | |
1729 | ||
613b411c LP |
1730 | copy = fdset_put_dup(fds, p->fd); |
1731 | if (copy < 0) | |
a16e1123 LP |
1732 | return copy; |
1733 | ||
1734 | if (p->type == SOCKET_SOCKET) { | |
613b411c | 1735 | _cleanup_free_ char *t = NULL; |
a16e1123 | 1736 | |
ee092817 LP |
1737 | r = socket_address_print(&p->address, &t); |
1738 | if (r < 0) | |
a16e1123 LP |
1739 | return r; |
1740 | ||
7a22745a LP |
1741 | if (socket_address_family(&p->address) == AF_NETLINK) |
1742 | unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t); | |
1743 | else | |
1744 | unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t); | |
613b411c | 1745 | |
b0a3f2bc LP |
1746 | } else if (p->type == SOCKET_SPECIAL) |
1747 | unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path); | |
ee092817 LP |
1748 | else if (p->type == SOCKET_MQUEUE) |
1749 | unit_serialize_item_format(u, f, "mqueue", "%i %s", copy, p->path); | |
b0a3f2bc | 1750 | else { |
a16e1123 LP |
1751 | assert(p->type == SOCKET_FIFO); |
1752 | unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path); | |
1753 | } | |
1754 | } | |
1755 | ||
1756 | return 0; | |
1757 | } | |
1758 | ||
1759 | static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1760 | Socket *s = SOCKET(u); | |
a16e1123 LP |
1761 | |
1762 | assert(u); | |
1763 | assert(key); | |
1764 | assert(value); | |
a16e1123 LP |
1765 | |
1766 | if (streq(key, "state")) { | |
1767 | SocketState state; | |
1768 | ||
ee092817 LP |
1769 | state = socket_state_from_string(value); |
1770 | if (state < 0) | |
e821075a | 1771 | log_debug_unit(u->id, "Failed to parse state value %s", value); |
a16e1123 LP |
1772 | else |
1773 | s->deserialized_state = state; | |
cfc4eb4c LP |
1774 | } else if (streq(key, "result")) { |
1775 | SocketResult f; | |
a16e1123 | 1776 | |
cfc4eb4c LP |
1777 | f = socket_result_from_string(value); |
1778 | if (f < 0) | |
e821075a | 1779 | log_debug_unit(u->id, "Failed to parse result value %s", value); |
cfc4eb4c LP |
1780 | else if (f != SOCKET_SUCCESS) |
1781 | s->result = f; | |
a16e1123 LP |
1782 | |
1783 | } else if (streq(key, "n-accepted")) { | |
1784 | unsigned k; | |
1785 | ||
e364ad06 | 1786 | if (safe_atou(value, &k) < 0) |
e821075a | 1787 | log_debug_unit(u->id, "Failed to parse n-accepted value %s", value); |
a16e1123 LP |
1788 | else |
1789 | s->n_accepted += k; | |
1790 | } else if (streq(key, "control-pid")) { | |
5925dd3c | 1791 | pid_t pid; |
a16e1123 | 1792 | |
e364ad06 | 1793 | if (parse_pid(value, &pid) < 0) |
e821075a | 1794 | log_debug_unit(u->id, "Failed to parse control-pid value %s", value); |
a16e1123 | 1795 | else |
5925dd3c | 1796 | s->control_pid = pid; |
a16e1123 LP |
1797 | } else if (streq(key, "control-command")) { |
1798 | SocketExecCommand id; | |
1799 | ||
66870f90 ZJS |
1800 | id = socket_exec_command_from_string(value); |
1801 | if (id < 0) | |
e821075a | 1802 | log_debug_unit(u->id, "Failed to parse exec-command value %s", value); |
a16e1123 LP |
1803 | else { |
1804 | s->control_command_id = id; | |
1805 | s->control_command = s->exec_command[id]; | |
1806 | } | |
1807 | } else if (streq(key, "fifo")) { | |
1808 | int fd, skip = 0; | |
1809 | SocketPort *p; | |
1810 | ||
1811 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1812 | log_debug_unit(u->id, "Failed to parse fifo value %s", value); |
a16e1123 LP |
1813 | else { |
1814 | ||
1815 | LIST_FOREACH(port, p, s->ports) | |
b0a3f2bc LP |
1816 | if (p->type == SOCKET_FIFO && |
1817 | streq_ptr(p->path, value+skip)) | |
1818 | break; | |
1819 | ||
1820 | if (p) { | |
1821 | if (p->fd >= 0) | |
1822 | close_nointr_nofail(p->fd); | |
1823 | p->fd = fdset_remove(fds, fd); | |
1824 | } | |
1825 | } | |
1826 | ||
1827 | } else if (streq(key, "special")) { | |
1828 | int fd, skip = 0; | |
1829 | SocketPort *p; | |
1830 | ||
1831 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1832 | log_debug_unit(u->id, "Failed to parse special value %s", value); |
b0a3f2bc LP |
1833 | else { |
1834 | ||
1835 | LIST_FOREACH(port, p, s->ports) | |
1836 | if (p->type == SOCKET_SPECIAL && | |
1837 | streq_ptr(p->path, value+skip)) | |
a16e1123 LP |
1838 | break; |
1839 | ||
1840 | if (p) { | |
1841 | if (p->fd >= 0) | |
1842 | close_nointr_nofail(p->fd); | |
1843 | p->fd = fdset_remove(fds, fd); | |
1844 | } | |
1845 | } | |
1846 | ||
ee092817 LP |
1847 | } else if (streq(key, "mqueue")) { |
1848 | int fd, skip = 0; | |
1849 | SocketPort *p; | |
1850 | ||
1851 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1852 | log_debug_unit(u->id, "Failed to parse mqueue value %s", value); |
ee092817 LP |
1853 | else { |
1854 | ||
1855 | LIST_FOREACH(port, p, s->ports) | |
1856 | if (p->type == SOCKET_MQUEUE && | |
1857 | streq_ptr(p->path, value+skip)) | |
1858 | break; | |
1859 | ||
1860 | if (p) { | |
1861 | if (p->fd >= 0) | |
1862 | close_nointr_nofail(p->fd); | |
1863 | p->fd = fdset_remove(fds, fd); | |
1864 | } | |
1865 | } | |
1866 | ||
a16e1123 | 1867 | } else if (streq(key, "socket")) { |
27ca8d7a | 1868 | int fd, type, skip = 0; |
a16e1123 LP |
1869 | SocketPort *p; |
1870 | ||
27ca8d7a | 1871 | if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd)) |
e821075a | 1872 | log_debug_unit(u->id, "Failed to parse socket value %s", value); |
a16e1123 LP |
1873 | else { |
1874 | ||
1875 | LIST_FOREACH(port, p, s->ports) | |
27ca8d7a | 1876 | if (socket_address_is(&p->address, value+skip, type)) |
a16e1123 LP |
1877 | break; |
1878 | ||
1879 | if (p) { | |
1880 | if (p->fd >= 0) | |
1881 | close_nointr_nofail(p->fd); | |
1882 | p->fd = fdset_remove(fds, fd); | |
1883 | } | |
1884 | } | |
1885 | ||
7a22745a LP |
1886 | } else if (streq(key, "netlink")) { |
1887 | int fd, skip = 0; | |
1888 | SocketPort *p; | |
1889 | ||
1890 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
e821075a | 1891 | log_debug_unit(u->id, "Failed to parse socket value %s", value); |
7a22745a LP |
1892 | else { |
1893 | ||
1894 | LIST_FOREACH(port, p, s->ports) | |
1895 | if (socket_address_is_netlink(&p->address, value+skip)) | |
1896 | break; | |
1897 | ||
1898 | if (p) { | |
1899 | if (p->fd >= 0) | |
1900 | close_nointr_nofail(p->fd); | |
1901 | p->fd = fdset_remove(fds, fd); | |
1902 | } | |
1903 | } | |
a16e1123 | 1904 | } else |
e821075a | 1905 | log_debug_unit(UNIT(s)->id, "Unknown serialization key '%s'", key); |
a16e1123 LP |
1906 | |
1907 | return 0; | |
1908 | } | |
1909 | ||
01e10de3 LP |
1910 | static int socket_distribute_fds(Unit *u, FDSet *fds) { |
1911 | Socket *s = SOCKET(u); | |
1912 | SocketPort *p; | |
1913 | ||
1914 | assert(u); | |
1915 | ||
1916 | LIST_FOREACH(port, p, s->ports) { | |
1917 | Iterator i; | |
1918 | int fd; | |
1919 | ||
1920 | if (p->type != SOCKET_SOCKET) | |
1921 | continue; | |
1922 | ||
1923 | if (p->fd >= 0) | |
1924 | continue; | |
1925 | ||
1926 | FDSET_FOREACH(fd, fds, i) { | |
1927 | if (socket_address_matches_fd(&p->address, fd)) { | |
1928 | p->fd = fdset_remove(fds, fd); | |
1929 | s->deserialized_state = SOCKET_LISTENING; | |
1930 | break; | |
1931 | } | |
1932 | } | |
1933 | } | |
1934 | ||
1935 | return 0; | |
1936 | } | |
1937 | ||
44a6b1b6 | 1938 | _pure_ static UnitActiveState socket_active_state(Unit *u) { |
87f0e418 | 1939 | assert(u); |
5cb5a6ff | 1940 | |
acbb0225 | 1941 | return state_translation_table[SOCKET(u)->state]; |
5cb5a6ff LP |
1942 | } |
1943 | ||
44a6b1b6 | 1944 | _pure_ static const char *socket_sub_state_to_string(Unit *u) { |
10a94420 LP |
1945 | assert(u); |
1946 | ||
a16e1123 | 1947 | return socket_state_to_string(SOCKET(u)->state); |
10a94420 LP |
1948 | } |
1949 | ||
67419600 OS |
1950 | const char* socket_port_type_to_string(SocketPort *p) { |
1951 | ||
1952 | assert(p); | |
1953 | ||
1954 | switch (p->type) { | |
718db961 LP |
1955 | |
1956 | case SOCKET_SOCKET: | |
1957 | ||
1958 | switch (p->address.type) { | |
1959 | ||
1960 | case SOCK_STREAM: | |
1961 | return "Stream"; | |
1962 | ||
1963 | case SOCK_DGRAM: | |
1964 | return "Datagram"; | |
1965 | ||
1966 | case SOCK_SEQPACKET: | |
1967 | return "SequentialPacket"; | |
1968 | ||
1969 | case SOCK_RAW: | |
1970 | if (socket_address_family(&p->address) == AF_NETLINK) | |
1971 | return "Netlink"; | |
1972 | ||
1973 | default: | |
1974 | return NULL; | |
1975 | } | |
1976 | ||
1977 | case SOCKET_SPECIAL: | |
1978 | return "Special"; | |
1979 | ||
1980 | case SOCKET_MQUEUE: | |
1981 | return "MessageQueue"; | |
1982 | ||
1983 | case SOCKET_FIFO: | |
1984 | return "FIFO"; | |
1985 | ||
1986 | default: | |
1987 | return NULL; | |
67419600 OS |
1988 | } |
1989 | } | |
1990 | ||
44a6b1b6 | 1991 | _pure_ static bool socket_check_gc(Unit *u) { |
6cf6bbc2 LP |
1992 | Socket *s = SOCKET(u); |
1993 | ||
1994 | assert(u); | |
1995 | ||
1996 | return s->n_connections > 0; | |
1997 | } | |
1998 | ||
718db961 LP |
1999 | static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
2000 | SocketPort *p = userdata; | |
4f2d528d | 2001 | int cfd = -1; |
9152c765 | 2002 | |
718db961 | 2003 | assert(p); |
8d567588 | 2004 | assert(fd >= 0); |
9152c765 | 2005 | |
718db961 LP |
2006 | if (p->socket->state != SOCKET_LISTENING) |
2007 | return 0; | |
871d7de4 | 2008 | |
718db961 | 2009 | log_debug_unit(UNIT(p->socket)->id, "Incoming traffic on %s", UNIT(p->socket)->id); |
9152c765 | 2010 | |
718db961 | 2011 | if (revents != EPOLLIN) { |
641e01dc | 2012 | |
718db961 | 2013 | if (revents & EPOLLHUP) |
e821075a | 2014 | 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 | 2015 | UNIT(p->socket)->id); |
641e01dc | 2016 | else |
e821075a | 2017 | log_error_unit(UNIT(p->socket)->id, "%s: Got unexpected poll event (0x%x) on socket.", |
718db961 | 2018 | UNIT(p->socket)->id, revents); |
641e01dc | 2019 | |
8d567588 | 2020 | goto fail; |
4f2d528d LP |
2021 | } |
2022 | ||
718db961 LP |
2023 | if (p->socket->accept && |
2024 | p->type == SOCKET_SOCKET && | |
2025 | socket_address_can_accept(&p->address)) { | |
2026 | ||
4f2d528d LP |
2027 | for (;;) { |
2028 | ||
b14eda96 LP |
2029 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK); |
2030 | if (cfd < 0) { | |
4f2d528d LP |
2031 | |
2032 | if (errno == EINTR) | |
2033 | continue; | |
2034 | ||
718db961 | 2035 | log_error_unit(UNIT(p->socket)->id, |
66870f90 | 2036 | "Failed to accept socket: %m"); |
8d567588 | 2037 | goto fail; |
4f2d528d LP |
2038 | } |
2039 | ||
2040 | break; | |
2041 | } | |
4fd5948e | 2042 | |
718db961 | 2043 | socket_apply_socket_options(p->socket, cfd); |
4f2d528d | 2044 | } |
9152c765 | 2045 | |
718db961 LP |
2046 | socket_enter_running(p->socket, cfd); |
2047 | return 0; | |
8d567588 LP |
2048 | |
2049 | fail: | |
718db961 LP |
2050 | socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES); |
2051 | return 0; | |
9152c765 LP |
2052 | } |
2053 | ||
87f0e418 LP |
2054 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2055 | Socket *s = SOCKET(u); | |
cfc4eb4c | 2056 | SocketResult f; |
5cb5a6ff LP |
2057 | |
2058 | assert(s); | |
034c6ed7 | 2059 | assert(pid >= 0); |
5cb5a6ff | 2060 | |
8c47c732 LP |
2061 | if (pid != s->control_pid) |
2062 | return; | |
542563ba | 2063 | |
034c6ed7 LP |
2064 | s->control_pid = 0; |
2065 | ||
96342de6 | 2066 | if (is_clean_exit(code, status, NULL)) |
cfc4eb4c LP |
2067 | f = SOCKET_SUCCESS; |
2068 | else if (code == CLD_EXITED) | |
2069 | f = SOCKET_FAILURE_EXIT_CODE; | |
2070 | else if (code == CLD_KILLED) | |
2071 | f = SOCKET_FAILURE_SIGNAL; | |
2072 | else if (code == CLD_DUMPED) | |
2073 | f = SOCKET_FAILURE_CORE_DUMP; | |
2074 | else | |
2075 | assert_not_reached("Unknown code"); | |
8c47c732 | 2076 | |
b708e7ce | 2077 | if (s->control_command) { |
6ea832a2 | 2078 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 2079 | |
b708e7ce | 2080 | if (s->control_command->ignore) |
cfc4eb4c | 2081 | f = SOCKET_SUCCESS; |
b708e7ce LP |
2082 | } |
2083 | ||
66870f90 ZJS |
2084 | log_full_unit(f == SOCKET_SUCCESS ? LOG_DEBUG : LOG_NOTICE, |
2085 | u->id, | |
2086 | "%s control process exited, code=%s status=%i", | |
2087 | u->id, sigchld_code_to_string(code), status); | |
034c6ed7 | 2088 | |
cfc4eb4c LP |
2089 | if (f != SOCKET_SUCCESS) |
2090 | s->result = f; | |
2091 | ||
2092 | if (s->control_command && | |
2093 | s->control_command->command_next && | |
2094 | f == SOCKET_SUCCESS) { | |
2095 | ||
66870f90 ZJS |
2096 | log_debug_unit(u->id, |
2097 | "%s running next command for state %s", | |
2098 | u->id, socket_state_to_string(s->state)); | |
cfc4eb4c | 2099 | socket_run_next(s); |
acbb0225 | 2100 | } else { |
a16e1123 LP |
2101 | s->control_command = NULL; |
2102 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
2103 | ||
034c6ed7 LP |
2104 | /* No further commands for this step, so let's figure |
2105 | * out what to do next */ | |
5cb5a6ff | 2106 | |
66870f90 ZJS |
2107 | log_debug_unit(u->id, |
2108 | "%s got final SIGCHLD for state %s", | |
2109 | u->id, socket_state_to_string(s->state)); | |
acbb0225 | 2110 | |
034c6ed7 LP |
2111 | switch (s->state) { |
2112 | ||
2113 | case SOCKET_START_PRE: | |
cfc4eb4c | 2114 | if (f == SOCKET_SUCCESS) |
acbb0225 | 2115 | socket_enter_start_post(s); |
034c6ed7 | 2116 | else |
cfc4eb4c | 2117 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f); |
034c6ed7 LP |
2118 | break; |
2119 | ||
2120 | case SOCKET_START_POST: | |
cfc4eb4c | 2121 | if (f == SOCKET_SUCCESS) |
e9af15c3 | 2122 | socket_enter_listening(s); |
034c6ed7 | 2123 | else |
cfc4eb4c | 2124 | socket_enter_stop_pre(s, f); |
034c6ed7 LP |
2125 | break; |
2126 | ||
2127 | case SOCKET_STOP_PRE: | |
2128 | case SOCKET_STOP_PRE_SIGTERM: | |
2129 | case SOCKET_STOP_PRE_SIGKILL: | |
cfc4eb4c | 2130 | socket_enter_stop_post(s, f); |
034c6ed7 LP |
2131 | break; |
2132 | ||
2133 | case SOCKET_STOP_POST: | |
80876c20 LP |
2134 | case SOCKET_FINAL_SIGTERM: |
2135 | case SOCKET_FINAL_SIGKILL: | |
cfc4eb4c | 2136 | socket_enter_dead(s, f); |
034c6ed7 LP |
2137 | break; |
2138 | ||
2139 | default: | |
2140 | assert_not_reached("Uh, control process died at wrong time."); | |
2141 | } | |
2142 | } | |
c4e2ceae LP |
2143 | |
2144 | /* Notify clients about changed exit status */ | |
2145 | unit_add_to_dbus_queue(u); | |
034c6ed7 | 2146 | } |
5cb5a6ff | 2147 | |
718db961 LP |
2148 | static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
2149 | Socket *s = SOCKET(userdata); | |
5cb5a6ff | 2150 | |
034c6ed7 | 2151 | assert(s); |
718db961 | 2152 | assert(s->timer_event_source == source); |
034c6ed7 LP |
2153 | |
2154 | switch (s->state) { | |
2155 | ||
2156 | case SOCKET_START_PRE: | |
718db961 LP |
2157 | log_warning_unit(UNIT(s)->id, |
2158 | "%s starting timed out. Terminating.", UNIT(s)->id); | |
cfc4eb4c | 2159 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
da19d5c1 | 2160 | break; |
80876c20 | 2161 | |
034c6ed7 | 2162 | case SOCKET_START_POST: |
718db961 LP |
2163 | log_warning_unit(UNIT(s)->id, |
2164 | "%s starting timed out. Stopping.", UNIT(s)->id); | |
cfc4eb4c | 2165 | socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2166 | break; |
2167 | ||
2168 | case SOCKET_STOP_PRE: | |
718db961 LP |
2169 | log_warning_unit(UNIT(s)->id, |
2170 | "%s stopping timed out. Terminating.", UNIT(s)->id); | |
cfc4eb4c | 2171 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2172 | break; |
2173 | ||
2174 | case SOCKET_STOP_PRE_SIGTERM: | |
4819ff03 | 2175 | if (s->kill_context.send_sigkill) { |
718db961 LP |
2176 | log_warning_unit(UNIT(s)->id, |
2177 | "%s stopping timed out. Killing.", UNIT(s)->id); | |
cfc4eb4c | 2178 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2179 | } else { |
718db961 | 2180 | log_warning_unit(UNIT(s)->id, |
66870f90 | 2181 | "%s stopping timed out. Skipping SIGKILL. Ignoring.", |
718db961 | 2182 | UNIT(s)->id); |
cfc4eb4c | 2183 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2184 | } |
034c6ed7 LP |
2185 | break; |
2186 | ||
2187 | case SOCKET_STOP_PRE_SIGKILL: | |
718db961 LP |
2188 | log_warning_unit(UNIT(s)->id, |
2189 | "%s still around after SIGKILL. Ignoring.", UNIT(s)->id); | |
cfc4eb4c | 2190 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2191 | break; |
2192 | ||
2193 | case SOCKET_STOP_POST: | |
718db961 LP |
2194 | log_warning_unit(UNIT(s)->id, |
2195 | "%s stopping timed out (2). Terminating.", UNIT(s)->id); | |
cfc4eb4c | 2196 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2197 | break; |
2198 | ||
80876c20 | 2199 | case SOCKET_FINAL_SIGTERM: |
4819ff03 | 2200 | if (s->kill_context.send_sigkill) { |
718db961 LP |
2201 | log_warning_unit(UNIT(s)->id, |
2202 | "%s stopping timed out (2). Killing.", UNIT(s)->id); | |
cfc4eb4c | 2203 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2204 | } else { |
718db961 | 2205 | log_warning_unit(UNIT(s)->id, |
66870f90 | 2206 | "%s stopping timed out (2). Skipping SIGKILL. Ignoring.", |
718db961 | 2207 | UNIT(s)->id); |
cfc4eb4c | 2208 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 2209 | } |
034c6ed7 LP |
2210 | break; |
2211 | ||
80876c20 | 2212 | case SOCKET_FINAL_SIGKILL: |
718db961 | 2213 | log_warning_unit(UNIT(s)->id, |
66870f90 | 2214 | "%s still around after SIGKILL (2). Entering failed mode.", |
718db961 | 2215 | UNIT(s)->id); |
cfc4eb4c | 2216 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
2217 | break; |
2218 | ||
2219 | default: | |
2220 | assert_not_reached("Timeout at wrong time."); | |
2221 | } | |
718db961 LP |
2222 | |
2223 | return 0; | |
5cb5a6ff LP |
2224 | } |
2225 | ||
44d8db9e LP |
2226 | int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { |
2227 | int *rfds; | |
2228 | unsigned rn_fds, k; | |
2229 | SocketPort *p; | |
2230 | ||
2231 | assert(s); | |
2232 | assert(fds); | |
2233 | assert(n_fds); | |
2234 | ||
2235 | /* Called from the service code for requesting our fds */ | |
2236 | ||
2237 | rn_fds = 0; | |
2238 | LIST_FOREACH(port, p, s->ports) | |
2239 | if (p->fd >= 0) | |
2240 | rn_fds++; | |
2241 | ||
de3756ab LP |
2242 | if (rn_fds <= 0) { |
2243 | *fds = NULL; | |
2244 | *n_fds = 0; | |
2245 | return 0; | |
2246 | } | |
2247 | ||
e364ad06 | 2248 | if (!(rfds = new(int, rn_fds))) |
44d8db9e LP |
2249 | return -ENOMEM; |
2250 | ||
2251 | k = 0; | |
2252 | LIST_FOREACH(port, p, s->ports) | |
2253 | if (p->fd >= 0) | |
2254 | rfds[k++] = p->fd; | |
2255 | ||
2256 | assert(k == rn_fds); | |
2257 | ||
2258 | *fds = rfds; | |
2259 | *n_fds = rn_fds; | |
2260 | ||
2261 | return 0; | |
2262 | } | |
2263 | ||
e821075a LP |
2264 | static void socket_reset_failed(Unit *u) { |
2265 | Socket *s = SOCKET(u); | |
2266 | ||
2267 | assert(s); | |
2268 | ||
2269 | if (s->state == SOCKET_FAILED) | |
2270 | socket_set_state(s, SOCKET_DEAD); | |
2271 | ||
2272 | s->result = SOCKET_SUCCESS; | |
2273 | } | |
2274 | ||
d137a488 | 2275 | static void socket_notify_service_dead(Socket *s, bool failed_permanent) { |
ceee3d82 LP |
2276 | assert(s); |
2277 | ||
6cf6bbc2 LP |
2278 | /* The service is dead. Dang! |
2279 | * | |
2280 | * This is strictly for one-instance-for-all-connections | |
2281 | * services. */ | |
ceee3d82 LP |
2282 | |
2283 | if (s->state == SOCKET_RUNNING) { | |
e821075a | 2284 | log_debug_unit(UNIT(s)->id, "%s got notified about service death (failed permanently: %s)", UNIT(s)->id, yes_no(failed_permanent)); |
6bda96a0 LP |
2285 | if (failed_permanent) |
2286 | socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_FAILED_PERMANENT); | |
c2f34808 MS |
2287 | else |
2288 | socket_enter_listening(s); | |
ceee3d82 LP |
2289 | } |
2290 | } | |
2291 | ||
6cf6bbc2 LP |
2292 | void socket_connection_unref(Socket *s) { |
2293 | assert(s); | |
2294 | ||
2295 | /* The service is dead. Yay! | |
2296 | * | |
35b8ca3a | 2297 | * This is strictly for one-instance-per-connection |
6cf6bbc2 LP |
2298 | * services. */ |
2299 | ||
2300 | assert(s->n_connections > 0); | |
2301 | s->n_connections--; | |
2302 | ||
e821075a | 2303 | log_debug_unit(UNIT(s)->id, "%s: One connection closed, %u left.", UNIT(s)->id, s->n_connections); |
5632e374 LP |
2304 | } |
2305 | ||
d137a488 UTL |
2306 | static void socket_trigger_notify(Unit *u, Unit *other) { |
2307 | Socket *s = SOCKET(u); | |
e821075a | 2308 | Service *se; |
d137a488 UTL |
2309 | |
2310 | assert(u); | |
2311 | assert(other); | |
2312 | ||
2313 | /* Don't propagate state changes from the service if we are | |
2314 | already down or accepting connections */ | |
2315 | if ((s->state != SOCKET_RUNNING && | |
2316 | s->state != SOCKET_LISTENING) || | |
2317 | s->accept) | |
2318 | return; | |
2319 | ||
2320 | if (other->load_state != UNIT_LOADED || | |
2321 | other->type != UNIT_SERVICE) | |
2322 | return; | |
2323 | ||
e821075a LP |
2324 | se = SERVICE(other); |
2325 | ||
d137a488 UTL |
2326 | if (se->state == SERVICE_FAILED) |
2327 | socket_notify_service_dead(s, se->result == SERVICE_FAILURE_START_LIMIT); | |
2328 | ||
2329 | if (se->state == SERVICE_DEAD || | |
2330 | se->state == SERVICE_STOP || | |
2331 | se->state == SERVICE_STOP_SIGTERM || | |
2332 | se->state == SERVICE_STOP_SIGKILL || | |
2333 | se->state == SERVICE_STOP_POST || | |
2334 | se->state == SERVICE_FINAL_SIGTERM || | |
2335 | se->state == SERVICE_FINAL_SIGKILL || | |
2336 | se->state == SERVICE_AUTO_RESTART) | |
2337 | socket_notify_service_dead(s, false); | |
2338 | ||
2339 | if (se->state == SERVICE_RUNNING) | |
2340 | socket_set_state(s, SOCKET_RUNNING); | |
2341 | } | |
2342 | ||
718db961 | 2343 | static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
814cc562 | 2344 | return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error); |
8a0867d6 LP |
2345 | } |
2346 | ||
a16e1123 LP |
2347 | static const char* const socket_state_table[_SOCKET_STATE_MAX] = { |
2348 | [SOCKET_DEAD] = "dead", | |
2349 | [SOCKET_START_PRE] = "start-pre", | |
2350 | [SOCKET_START_POST] = "start-post", | |
2351 | [SOCKET_LISTENING] = "listening", | |
2352 | [SOCKET_RUNNING] = "running", | |
2353 | [SOCKET_STOP_PRE] = "stop-pre", | |
2354 | [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm", | |
2355 | [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill", | |
2356 | [SOCKET_STOP_POST] = "stop-post", | |
2357 | [SOCKET_FINAL_SIGTERM] = "final-sigterm", | |
2358 | [SOCKET_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 2359 | [SOCKET_FAILED] = "failed" |
a16e1123 LP |
2360 | }; |
2361 | ||
2362 | DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState); | |
2363 | ||
2364 | static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { | |
2365 | [SOCKET_EXEC_START_PRE] = "StartPre", | |
2366 | [SOCKET_EXEC_START_POST] = "StartPost", | |
2367 | [SOCKET_EXEC_STOP_PRE] = "StopPre", | |
2368 | [SOCKET_EXEC_STOP_POST] = "StopPost" | |
2369 | }; | |
2370 | ||
2371 | DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); | |
2372 | ||
cfc4eb4c LP |
2373 | static const char* const socket_result_table[_SOCKET_RESULT_MAX] = { |
2374 | [SOCKET_SUCCESS] = "success", | |
2375 | [SOCKET_FAILURE_RESOURCES] = "resources", | |
2376 | [SOCKET_FAILURE_TIMEOUT] = "timeout", | |
2377 | [SOCKET_FAILURE_EXIT_CODE] = "exit-code", | |
2378 | [SOCKET_FAILURE_SIGNAL] = "signal", | |
c2f34808 | 2379 | [SOCKET_FAILURE_CORE_DUMP] = "core-dump", |
6bda96a0 | 2380 | [SOCKET_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent" |
cfc4eb4c LP |
2381 | }; |
2382 | ||
2383 | DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult); | |
2384 | ||
87f0e418 | 2385 | const UnitVTable socket_vtable = { |
7d17cfbc | 2386 | .object_size = sizeof(Socket), |
718db961 LP |
2387 | .exec_context_offset = offsetof(Socket, exec_context), |
2388 | .cgroup_context_offset = offsetof(Socket, cgroup_context), | |
2389 | .kill_context_offset = offsetof(Socket, kill_context), | |
613b411c | 2390 | .exec_runtime_offset = offsetof(Socket, exec_runtime), |
3ef63c31 | 2391 | |
f975e971 LP |
2392 | .sections = |
2393 | "Unit\0" | |
2394 | "Socket\0" | |
2395 | "Install\0", | |
4ad49000 | 2396 | .private_section = "Socket", |
71645aca | 2397 | |
034c6ed7 LP |
2398 | .init = socket_init, |
2399 | .done = socket_done, | |
a16e1123 LP |
2400 | .load = socket_load, |
2401 | ||
2402 | .coldplug = socket_coldplug, | |
034c6ed7 | 2403 | |
5cb5a6ff LP |
2404 | .dump = socket_dump, |
2405 | ||
542563ba LP |
2406 | .start = socket_start, |
2407 | .stop = socket_stop, | |
5cb5a6ff | 2408 | |
718db961 LP |
2409 | .kill = socket_kill, |
2410 | ||
a16e1123 LP |
2411 | .serialize = socket_serialize, |
2412 | .deserialize_item = socket_deserialize_item, | |
01e10de3 | 2413 | .distribute_fds = socket_distribute_fds, |
a16e1123 | 2414 | |
5cb5a6ff | 2415 | .active_state = socket_active_state, |
10a94420 | 2416 | .sub_state_to_string = socket_sub_state_to_string, |
5cb5a6ff | 2417 | |
6cf6bbc2 LP |
2418 | .check_gc = socket_check_gc, |
2419 | ||
034c6ed7 | 2420 | .sigchld_event = socket_sigchld_event, |
4139c1b2 | 2421 | |
d137a488 UTL |
2422 | .trigger_notify = socket_trigger_notify, |
2423 | ||
fdf20a31 | 2424 | .reset_failed = socket_reset_failed, |
5632e374 | 2425 | |
c4e2ceae | 2426 | .bus_interface = "org.freedesktop.systemd1.Socket", |
718db961 | 2427 | .bus_vtable = bus_socket_vtable, |
74c964d3 LP |
2428 | .bus_set_property = bus_socket_set_property, |
2429 | .bus_commit_properties = bus_socket_commit_properties, | |
c6918296 MS |
2430 | |
2431 | .status_message_formats = { | |
2432 | /*.starting_stopping = { | |
2433 | [0] = "Starting socket %s...", | |
2434 | [1] = "Stopping socket %s...", | |
2435 | },*/ | |
2436 | .finished_start_job = { | |
2437 | [JOB_DONE] = "Listening on %s.", | |
2438 | [JOB_FAILED] = "Failed to listen on %s.", | |
2439 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
2440 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
2441 | }, | |
2442 | .finished_stop_job = { | |
2443 | [JOB_DONE] = "Closed %s.", | |
2444 | [JOB_FAILED] = "Failed stopping %s.", | |
2445 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
2446 | }, | |
2447 | }, | |
5cb5a6ff | 2448 | }; |