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