]>
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 | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
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> |
83c60c9f | 30 | |
87f0e418 | 31 | #include "unit.h" |
5cb5a6ff | 32 | #include "socket.h" |
cebf8b20 | 33 | #include "netinet/tcp.h" |
83c60c9f | 34 | #include "log.h" |
23a177ef LP |
35 | #include "load-dropin.h" |
36 | #include "load-fragment.h" | |
9e2f7c11 | 37 | #include "strv.h" |
4f2d528d | 38 | #include "unit-name.h" |
4139c1b2 | 39 | #include "dbus-socket.h" |
4fd5948e | 40 | #include "missing.h" |
a40eb732 | 41 | #include "special.h" |
398ef8ba | 42 | #include "bus-errors.h" |
e51bc1a2 | 43 | #include "label.h" |
83c60c9f | 44 | |
acbb0225 | 45 | static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { |
87f0e418 LP |
46 | [SOCKET_DEAD] = UNIT_INACTIVE, |
47 | [SOCKET_START_PRE] = UNIT_ACTIVATING, | |
48 | [SOCKET_START_POST] = UNIT_ACTIVATING, | |
49 | [SOCKET_LISTENING] = UNIT_ACTIVE, | |
50 | [SOCKET_RUNNING] = UNIT_ACTIVE, | |
51 | [SOCKET_STOP_PRE] = UNIT_DEACTIVATING, | |
52 | [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING, | |
53 | [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING, | |
54 | [SOCKET_STOP_POST] = UNIT_DEACTIVATING, | |
80876c20 LP |
55 | [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING, |
56 | [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 57 | [SOCKET_FAILED] = UNIT_FAILED |
83c60c9f | 58 | }; |
5cb5a6ff | 59 | |
a16e1123 LP |
60 | static void socket_init(Unit *u) { |
61 | Socket *s = SOCKET(u); | |
62 | ||
63 | assert(u); | |
64 | assert(u->meta.load_state == UNIT_STUB); | |
65 | ||
a16e1123 LP |
66 | s->backlog = SOMAXCONN; |
67 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
68 | s->directory_mode = 0755; | |
69 | s->socket_mode = 0666; | |
70 | ||
6cf6bbc2 LP |
71 | s->max_connections = 64; |
72 | ||
4fd5948e | 73 | s->priority = -1; |
4fd5948e LP |
74 | s->ip_tos = -1; |
75 | s->ip_ttl = -1; | |
4fd5948e | 76 | s->mark = -1; |
4fd5948e | 77 | |
a16e1123 LP |
78 | exec_context_init(&s->exec_context); |
79 | ||
80 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
81 | } | |
acbb0225 | 82 | |
5e94833f LP |
83 | static void socket_unwatch_control_pid(Socket *s) { |
84 | assert(s); | |
85 | ||
86 | if (s->control_pid <= 0) | |
87 | return; | |
88 | ||
89 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
90 | s->control_pid = 0; | |
91 | } | |
92 | ||
87f0e418 LP |
93 | static void socket_done(Unit *u) { |
94 | Socket *s = SOCKET(u); | |
034c6ed7 | 95 | SocketPort *p; |
6cf6bbc2 | 96 | Meta *i; |
034c6ed7 LP |
97 | |
98 | assert(s); | |
99 | ||
100 | while ((p = s->ports)) { | |
101 | LIST_REMOVE(SocketPort, port, s->ports, p); | |
102 | ||
a16e1123 LP |
103 | if (p->fd >= 0) { |
104 | unit_unwatch_fd(UNIT(s), &p->fd_watch); | |
105 | close_nointr_nofail(p->fd); | |
106 | } | |
107 | ||
034c6ed7 LP |
108 | free(p->path); |
109 | free(p); | |
110 | } | |
111 | ||
112 | exec_context_done(&s->exec_context); | |
e537352b | 113 | exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); |
034c6ed7 LP |
114 | s->control_command = NULL; |
115 | ||
5e94833f | 116 | socket_unwatch_control_pid(s); |
034c6ed7 LP |
117 | |
118 | s->service = NULL; | |
119 | ||
cebf8b20 TT |
120 | free(s->tcp_congestion); |
121 | s->tcp_congestion = NULL; | |
122 | ||
acbb0225 | 123 | free(s->bind_to_device); |
e537352b | 124 | s->bind_to_device = NULL; |
acbb0225 LP |
125 | |
126 | unit_unwatch_timer(u, &s->timer_watch); | |
6cf6bbc2 LP |
127 | |
128 | /* Make sure no service instance refers to us anymore. */ | |
129 | LIST_FOREACH(units_per_type, i, u->meta.manager->units_per_type[UNIT_SERVICE]) { | |
130 | Service *service = (Service *) i; | |
131 | ||
f976f3f6 LP |
132 | if (service->accept_socket == s) |
133 | service->accept_socket = NULL; | |
134 | ||
135 | set_remove(service->configured_sockets, s); | |
6cf6bbc2 | 136 | } |
5cb5a6ff LP |
137 | } |
138 | ||
b15bdda8 LP |
139 | static int socket_instantiate_service(Socket *s) { |
140 | char *prefix, *name; | |
141 | int r; | |
142 | Unit *u; | |
143 | ||
144 | assert(s); | |
145 | ||
146 | /* This fills in s->service if it isn't filled in yet. For | |
147 | * Accept=yes sockets we create the next connection service | |
148 | * here. For Accept=no this is mostly a NOP since the service | |
149 | * is figured out at load time anyway. */ | |
150 | ||
151 | if (s->service) | |
152 | return 0; | |
153 | ||
154 | assert(s->accept); | |
155 | ||
156 | if (!(prefix = unit_name_to_prefix(s->meta.id))) | |
157 | return -ENOMEM; | |
158 | ||
159 | r = asprintf(&name, "%s@%u.service", prefix, s->n_accepted); | |
160 | free(prefix); | |
161 | ||
162 | if (r < 0) | |
163 | return -ENOMEM; | |
164 | ||
165 | r = manager_load_unit(s->meta.manager, name, NULL, NULL, &u); | |
166 | free(name); | |
167 | ||
168 | if (r < 0) | |
169 | return r; | |
170 | ||
6c073082 | 171 | u->meta.no_gc = true; |
b15bdda8 LP |
172 | s->service = SERVICE(u); |
173 | return 0; | |
174 | } | |
175 | ||
4f2d528d LP |
176 | static bool have_non_accept_socket(Socket *s) { |
177 | SocketPort *p; | |
178 | ||
179 | assert(s); | |
180 | ||
181 | if (!s->accept) | |
182 | return true; | |
183 | ||
dd5ad9d4 LP |
184 | LIST_FOREACH(port, p, s->ports) { |
185 | ||
186 | if (p->type != SOCKET_SOCKET) | |
187 | return true; | |
188 | ||
4f2d528d LP |
189 | if (!socket_address_can_accept(&p->address)) |
190 | return true; | |
dd5ad9d4 | 191 | } |
4f2d528d LP |
192 | |
193 | return false; | |
194 | } | |
195 | ||
196 | static int socket_verify(Socket *s) { | |
197 | assert(s); | |
198 | ||
4cd1fbcc | 199 | if (s->meta.load_state != UNIT_LOADED) |
4f2d528d LP |
200 | return 0; |
201 | ||
202 | if (!s->ports) { | |
4cd1fbcc | 203 | log_error("%s lacks Listen setting. Refusing.", s->meta.id); |
4f2d528d LP |
204 | return -EINVAL; |
205 | } | |
206 | ||
0009d2a6 LP |
207 | if (s->accept && have_non_accept_socket(s)) { |
208 | log_error("%s configured for accepting sockets, but sockets are non-accepting. Refusing.", s->meta.id); | |
209 | return -EINVAL; | |
210 | } | |
211 | ||
6cf6bbc2 | 212 | if (s->accept && s->max_connections <= 0) { |
4d0e5dbd LP |
213 | log_error("%s's MaxConnection setting too small. Refusing.", s->meta.id); |
214 | return -EINVAL; | |
215 | } | |
216 | ||
d9ff321a LP |
217 | if (s->accept && s->service) { |
218 | log_error("Explicit service configuration for accepting sockets not supported on %s. Refusing.", s->meta.id); | |
219 | return -EINVAL; | |
220 | } | |
221 | ||
2e22afe9 | 222 | if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) { |
4d0e5dbd | 223 | log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id); |
6cf6bbc2 LP |
224 | return -EINVAL; |
225 | } | |
226 | ||
4f2d528d LP |
227 | return 0; |
228 | } | |
229 | ||
6e2ef85b LP |
230 | static bool socket_needs_mount(Socket *s, const char *prefix) { |
231 | SocketPort *p; | |
232 | ||
233 | assert(s); | |
234 | ||
235 | LIST_FOREACH(port, p, s->ports) { | |
236 | ||
237 | if (p->type == SOCKET_SOCKET) { | |
238 | if (socket_address_needs_mount(&p->address, prefix)) | |
239 | return true; | |
240 | } else { | |
241 | assert(p->type == SOCKET_FIFO); | |
242 | if (path_startswith(p->path, prefix)) | |
243 | return true; | |
244 | } | |
245 | } | |
246 | ||
247 | return false; | |
248 | } | |
249 | ||
250 | int socket_add_one_mount_link(Socket *s, Mount *m) { | |
251 | int r; | |
252 | ||
253 | assert(s); | |
254 | assert(m); | |
255 | ||
256 | if (s->meta.load_state != UNIT_LOADED || | |
257 | m->meta.load_state != UNIT_LOADED) | |
258 | return 0; | |
259 | ||
260 | if (!socket_needs_mount(s, m->where)) | |
261 | return 0; | |
262 | ||
2c966c03 | 263 | if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0) |
6e2ef85b LP |
264 | return r; |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
269 | static int socket_add_mount_links(Socket *s) { | |
270 | Meta *other; | |
271 | int r; | |
272 | ||
273 | assert(s); | |
274 | ||
275 | LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT]) | |
276 | if ((r = socket_add_one_mount_link(s, (Mount*) other)) < 0) | |
277 | return r; | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
282 | static int socket_add_device_link(Socket *s) { | |
283 | char *t; | |
284 | int r; | |
285 | ||
286 | assert(s); | |
287 | ||
288 | if (!s->bind_to_device) | |
289 | return 0; | |
290 | ||
291 | if (asprintf(&t, "/sys/subsystem/net/devices/%s", s->bind_to_device) < 0) | |
292 | return -ENOMEM; | |
293 | ||
294 | r = unit_add_node_link(UNIT(s), t, false); | |
295 | free(t); | |
296 | ||
297 | return r; | |
298 | } | |
299 | ||
a40eb732 LP |
300 | static int socket_add_default_dependencies(Socket *s) { |
301 | int r; | |
302 | assert(s); | |
303 | ||
2a77d31d LP |
304 | if (s->meta.manager->running_as == MANAGER_SYSTEM) { |
305 | if ((r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0) | |
306 | return r; | |
307 | ||
a40eb732 LP |
308 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) |
309 | return r; | |
2a77d31d | 310 | } |
a40eb732 | 311 | |
ead8e478 | 312 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); |
a40eb732 LP |
313 | } |
314 | ||
e537352b LP |
315 | static int socket_load(Unit *u) { |
316 | Socket *s = SOCKET(u); | |
317 | int r; | |
44d8db9e | 318 | |
e537352b LP |
319 | assert(u); |
320 | assert(u->meta.load_state == UNIT_STUB); | |
44d8db9e | 321 | |
e537352b | 322 | if ((r = unit_load_fragment_and_dropin(u)) < 0) |
23a177ef | 323 | return r; |
44d8db9e | 324 | |
23a177ef | 325 | /* This is a new unit? Then let's add in some extras */ |
e537352b | 326 | if (u->meta.load_state == UNIT_LOADED) { |
44d8db9e | 327 | |
4f2d528d | 328 | if (have_non_accept_socket(s)) { |
d9ff321a LP |
329 | |
330 | if (!s->service) | |
331 | if ((r = unit_load_related_unit(u, ".service", (Unit**) &s->service)) < 0) | |
332 | return r; | |
23a177ef | 333 | |
701cc384 | 334 | if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service), true)) < 0) |
4f2d528d LP |
335 | return r; |
336 | } | |
44d8db9e | 337 | |
6e2ef85b LP |
338 | if ((r = socket_add_mount_links(s)) < 0) |
339 | return r; | |
340 | ||
341 | if ((r = socket_add_device_link(s)) < 0) | |
342 | return r; | |
343 | ||
23a177ef LP |
344 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) |
345 | return r; | |
346 | ||
347 | if ((r = unit_add_default_cgroup(u)) < 0) | |
348 | return r; | |
a40eb732 LP |
349 | |
350 | if (s->meta.default_dependencies) | |
351 | if ((r = socket_add_default_dependencies(s)) < 0) | |
352 | return r; | |
23a177ef LP |
353 | } |
354 | ||
4f2d528d | 355 | return socket_verify(s); |
44d8db9e LP |
356 | } |
357 | ||
542563ba LP |
358 | static const char* listen_lookup(int type) { |
359 | ||
360 | if (type == SOCK_STREAM) | |
361 | return "ListenStream"; | |
362 | else if (type == SOCK_DGRAM) | |
363 | return "ListenDatagram"; | |
364 | else if (type == SOCK_SEQPACKET) | |
365 | return "ListenSequentialPacket"; | |
366 | ||
034c6ed7 | 367 | assert_not_reached("Unknown socket type"); |
542563ba LP |
368 | return NULL; |
369 | } | |
370 | ||
87f0e418 | 371 | static void socket_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 372 | |
5cb5a6ff | 373 | SocketExecCommand c; |
87f0e418 | 374 | Socket *s = SOCKET(u); |
542563ba | 375 | SocketPort *p; |
82ba9f08 LP |
376 | const char *prefix2; |
377 | char *p2; | |
5cb5a6ff LP |
378 | |
379 | assert(s); | |
fa068367 | 380 | assert(f); |
5cb5a6ff | 381 | |
82ba9f08 LP |
382 | p2 = strappend(prefix, "\t"); |
383 | prefix2 = p2 ? p2 : prefix; | |
c43d20a0 | 384 | |
5cb5a6ff LP |
385 | fprintf(f, |
386 | "%sSocket State: %s\n" | |
542563ba | 387 | "%sBindIPv6Only: %s\n" |
b5a0699f LP |
388 | "%sBacklog: %u\n" |
389 | "%sSocketMode: %04o\n" | |
4fd5948e LP |
390 | "%sDirectoryMode: %04o\n" |
391 | "%sKeepAlive: %s\n" | |
cebf8b20 TT |
392 | "%sFreeBind: %s\n" |
393 | "%sTCPCongestion: %s\n", | |
a16e1123 | 394 | prefix, socket_state_to_string(s->state), |
c0120d99 | 395 | prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only), |
b5a0699f LP |
396 | prefix, s->backlog, |
397 | prefix, s->socket_mode, | |
4fd5948e LP |
398 | prefix, s->directory_mode, |
399 | prefix, yes_no(s->keep_alive), | |
cebf8b20 | 400 | prefix, yes_no(s->free_bind), |
83a95334 | 401 | prefix, strna(s->tcp_congestion)); |
542563ba | 402 | |
70123e68 LP |
403 | if (s->control_pid > 0) |
404 | fprintf(f, | |
bb00e604 LP |
405 | "%sControl PID: %lu\n", |
406 | prefix, (unsigned long) s->control_pid); | |
70123e68 | 407 | |
acbb0225 LP |
408 | if (s->bind_to_device) |
409 | fprintf(f, | |
410 | "%sBindToDevice: %s\n", | |
411 | prefix, s->bind_to_device); | |
412 | ||
4f2d528d LP |
413 | if (s->accept) |
414 | fprintf(f, | |
6cf6bbc2 LP |
415 | "%sAccepted: %u\n" |
416 | "%sNConnections: %u\n" | |
417 | "%sMaxConnections: %u\n", | |
418 | prefix, s->n_accepted, | |
419 | prefix, s->n_connections, | |
420 | prefix, s->max_connections); | |
4f2d528d | 421 | |
4fd5948e LP |
422 | if (s->priority >= 0) |
423 | fprintf(f, | |
424 | "%sPriority: %i\n", | |
425 | prefix, s->priority); | |
426 | ||
427 | if (s->receive_buffer > 0) | |
428 | fprintf(f, | |
429 | "%sReceiveBuffer: %zu\n", | |
430 | prefix, s->receive_buffer); | |
431 | ||
432 | if (s->send_buffer > 0) | |
433 | fprintf(f, | |
434 | "%sSendBuffer: %zu\n", | |
435 | prefix, s->send_buffer); | |
436 | ||
437 | if (s->ip_tos >= 0) | |
438 | fprintf(f, | |
439 | "%sIPTOS: %i\n", | |
440 | prefix, s->ip_tos); | |
441 | ||
442 | if (s->ip_ttl >= 0) | |
443 | fprintf(f, | |
444 | "%sIPTTL: %i\n", | |
445 | prefix, s->ip_ttl); | |
446 | ||
447 | if (s->pipe_size > 0) | |
448 | fprintf(f, | |
449 | "%sPipeSize: %zu\n", | |
450 | prefix, s->pipe_size); | |
451 | ||
452 | if (s->mark >= 0) | |
453 | fprintf(f, | |
454 | "%sMark: %i\n", | |
455 | prefix, s->mark); | |
456 | ||
034c6ed7 | 457 | LIST_FOREACH(port, p, s->ports) { |
5cb5a6ff | 458 | |
542563ba LP |
459 | if (p->type == SOCKET_SOCKET) { |
460 | const char *t; | |
461 | int r; | |
e364ad06 | 462 | char *k = NULL; |
542563ba LP |
463 | |
464 | if ((r = socket_address_print(&p->address, &k)) < 0) | |
465 | t = strerror(-r); | |
466 | else | |
467 | t = k; | |
468 | ||
e364ad06 | 469 | fprintf(f, "%s%s: %s\n", prefix, listen_lookup(p->address.type), t); |
542563ba LP |
470 | free(k); |
471 | } else | |
472 | fprintf(f, "%sListenFIFO: %s\n", prefix, p->path); | |
473 | } | |
5cb5a6ff LP |
474 | |
475 | exec_context_dump(&s->exec_context, f, prefix); | |
476 | ||
e537352b | 477 | for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) { |
c43d20a0 LP |
478 | if (!s->exec_command[c]) |
479 | continue; | |
5cb5a6ff | 480 | |
40d50879 | 481 | fprintf(f, "%s-> %s:\n", |
a16e1123 | 482 | prefix, socket_exec_command_to_string(c)); |
c43d20a0 LP |
483 | |
484 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 485 | } |
c43d20a0 | 486 | |
82ba9f08 | 487 | free(p2); |
5cb5a6ff LP |
488 | } |
489 | ||
4f2d528d LP |
490 | static int instance_from_socket(int fd, unsigned nr, char **instance) { |
491 | socklen_t l; | |
492 | char *r; | |
493 | union { | |
494 | struct sockaddr sa; | |
495 | struct sockaddr_un un; | |
496 | struct sockaddr_in in; | |
497 | struct sockaddr_in6 in6; | |
498 | struct sockaddr_storage storage; | |
499 | } local, remote; | |
500 | ||
501 | assert(fd >= 0); | |
502 | assert(instance); | |
503 | ||
504 | l = sizeof(local); | |
505 | if (getsockname(fd, &local.sa, &l) < 0) | |
506 | return -errno; | |
507 | ||
508 | l = sizeof(remote); | |
509 | if (getpeername(fd, &remote.sa, &l) < 0) | |
510 | return -errno; | |
511 | ||
512 | switch (local.sa.sa_family) { | |
513 | ||
514 | case AF_INET: { | |
515 | uint32_t | |
516 | a = ntohl(local.in.sin_addr.s_addr), | |
517 | b = ntohl(remote.in.sin_addr.s_addr); | |
518 | ||
519 | if (asprintf(&r, | |
b15bdda8 | 520 | "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
4f2d528d LP |
521 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, |
522 | ntohs(local.in.sin_port), | |
523 | b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF, | |
524 | ntohs(remote.in.sin_port)) < 0) | |
525 | return -ENOMEM; | |
526 | ||
527 | break; | |
528 | } | |
529 | ||
530 | case AF_INET6: { | |
2b061f5a LP |
531 | static const char ipv4_prefix[] = { |
532 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF | |
533 | }; | |
534 | ||
535 | if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 && | |
536 | memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |
537 | const uint8_t | |
538 | *a = local.in6.sin6_addr.s6_addr+12, | |
539 | *b = remote.in6.sin6_addr.s6_addr+12; | |
540 | ||
541 | if (asprintf(&r, | |
b15bdda8 | 542 | "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
2b061f5a LP |
543 | a[0], a[1], a[2], a[3], |
544 | ntohs(local.in6.sin6_port), | |
545 | b[0], b[1], b[2], b[3], | |
546 | ntohs(remote.in6.sin6_port)) < 0) | |
547 | return -ENOMEM; | |
548 | } else { | |
549 | char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN]; | |
550 | ||
551 | if (asprintf(&r, | |
b15bdda8 | 552 | "%s:%u-%s:%u", |
2b061f5a LP |
553 | inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)), |
554 | ntohs(local.in6.sin6_port), | |
555 | inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)), | |
556 | ntohs(remote.in6.sin6_port)) < 0) | |
557 | return -ENOMEM; | |
558 | } | |
4f2d528d LP |
559 | |
560 | break; | |
561 | } | |
562 | ||
563 | case AF_UNIX: { | |
564 | struct ucred ucred; | |
565 | ||
566 | l = sizeof(ucred); | |
567 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) | |
568 | return -errno; | |
569 | ||
570 | if (asprintf(&r, | |
bb00e604 | 571 | "%u-%lu-%lu", |
4f2d528d | 572 | nr, |
bb00e604 LP |
573 | (unsigned long) ucred.pid, |
574 | (unsigned long) ucred.uid) < 0) | |
4f2d528d LP |
575 | return -ENOMEM; |
576 | ||
577 | break; | |
578 | } | |
579 | ||
580 | default: | |
581 | assert_not_reached("Unhandled socket type."); | |
582 | } | |
583 | ||
584 | *instance = r; | |
585 | return 0; | |
586 | } | |
587 | ||
034c6ed7 | 588 | static void socket_close_fds(Socket *s) { |
83c60c9f LP |
589 | SocketPort *p; |
590 | ||
591 | assert(s); | |
592 | ||
034c6ed7 | 593 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f LP |
594 | if (p->fd < 0) |
595 | continue; | |
596 | ||
acbb0225 | 597 | unit_unwatch_fd(UNIT(s), &p->fd_watch); |
a16e1123 LP |
598 | close_nointr_nofail(p->fd); |
599 | ||
600 | /* One little note: we should never delete any sockets | |
601 | * in the file system here! After all some other | |
602 | * process we spawned might still have a reference of | |
603 | * this fd and wants to continue to use it. Therefore | |
604 | * we delete sockets in the file system before we | |
605 | * create a new one, not after we stopped using | |
606 | * one! */ | |
9152c765 | 607 | |
83c60c9f LP |
608 | p->fd = -1; |
609 | } | |
610 | } | |
611 | ||
4fd5948e LP |
612 | static void socket_apply_socket_options(Socket *s, int fd) { |
613 | assert(s); | |
614 | assert(fd >= 0); | |
615 | ||
616 | if (s->keep_alive) { | |
617 | int b = s->keep_alive; | |
618 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) < 0) | |
619 | log_warning("SO_KEEPALIVE failed: %m"); | |
620 | } | |
621 | ||
622 | if (s->priority >= 0) | |
623 | if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0) | |
624 | log_warning("SO_PRIORITY failed: %m"); | |
625 | ||
626 | if (s->receive_buffer > 0) { | |
627 | int value = (int) s->receive_buffer; | |
628 | if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0) | |
629 | log_warning("SO_RCVBUF failed: %m"); | |
630 | } | |
631 | ||
632 | if (s->send_buffer > 0) { | |
633 | int value = (int) s->send_buffer; | |
634 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0) | |
635 | log_warning("SO_SNDBUF failed: %m"); | |
636 | } | |
637 | ||
638 | if (s->mark >= 0) | |
639 | if (setsockopt(fd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)) < 0) | |
640 | log_warning("SO_MARK failed: %m"); | |
641 | ||
642 | if (s->ip_tos >= 0) | |
643 | if (setsockopt(fd, IPPROTO_IP, IP_TOS, &s->ip_tos, sizeof(s->ip_tos)) < 0) | |
644 | log_warning("IP_TOS failed: %m"); | |
645 | ||
46925ac5 LP |
646 | if (s->ip_ttl >= 0) { |
647 | int r, x; | |
648 | ||
649 | r = setsockopt(fd, IPPROTO_IP, IP_TTL, &s->ip_ttl, sizeof(s->ip_ttl)); | |
5bfcc1c6 FF |
650 | |
651 | if (socket_ipv6_is_supported()) | |
652 | x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl)); | |
653 | else { | |
654 | x = -1; | |
655 | errno = EAFNOSUPPORT; | |
656 | } | |
46925ac5 LP |
657 | |
658 | if (r < 0 && x < 0) | |
659 | log_warning("IP_TTL/IPV6_UNICAST_HOPS failed: %m"); | |
660 | } | |
cebf8b20 TT |
661 | |
662 | if (s->tcp_congestion) | |
663 | if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0) | |
664 | log_warning("TCP_CONGESTION failed: %m"); | |
4fd5948e LP |
665 | } |
666 | ||
b15bdda8 | 667 | static void socket_apply_fifo_options(Socket *s, int fd) { |
4fd5948e LP |
668 | assert(s); |
669 | assert(fd >= 0); | |
670 | ||
671 | if (s->pipe_size > 0) | |
672 | if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0) | |
673 | log_warning("F_SETPIPE_SZ: %m"); | |
674 | } | |
675 | ||
7a58bfa4 | 676 | |
b15bdda8 LP |
677 | static int fifo_address_create( |
678 | const char *path, | |
679 | mode_t directory_mode, | |
680 | mode_t socket_mode, | |
b15bdda8 LP |
681 | int *_fd) { |
682 | ||
7a58bfa4 | 683 | int fd = -1, r = 0; |
b15bdda8 LP |
684 | struct stat st; |
685 | mode_t old_mask; | |
686 | ||
687 | assert(path); | |
688 | assert(_fd); | |
689 | ||
690 | mkdir_parents(path, directory_mode); | |
691 | ||
c4e2ceae | 692 | if ((r = label_fifofile_set(path)) < 0) |
56cf987f | 693 | goto fail; |
b15bdda8 LP |
694 | |
695 | /* Enforce the right access mode for the fifo */ | |
696 | old_mask = umask(~ socket_mode); | |
697 | ||
698 | /* Include the original umask in our mask */ | |
699 | umask(~socket_mode | old_mask); | |
700 | ||
701 | r = mkfifo(path, socket_mode); | |
702 | umask(old_mask); | |
703 | ||
704 | if (r < 0) { | |
705 | r = -errno; | |
706 | goto fail; | |
707 | } | |
708 | ||
709 | if ((fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) { | |
710 | r = -errno; | |
711 | goto fail; | |
712 | } | |
713 | ||
56cf987f | 714 | label_file_clear(); |
7a58bfa4 | 715 | |
b15bdda8 LP |
716 | if (fstat(fd, &st) < 0) { |
717 | r = -errno; | |
718 | goto fail; | |
719 | } | |
720 | ||
721 | if (!S_ISFIFO(st.st_mode) || | |
de0200fc | 722 | (st.st_mode & 0777) != (socket_mode & ~old_mask) || |
b15bdda8 LP |
723 | st.st_uid != getuid() || |
724 | st.st_gid != getgid()) { | |
725 | ||
726 | r = -EEXIST; | |
727 | goto fail; | |
728 | } | |
729 | ||
730 | *_fd = fd; | |
731 | return 0; | |
732 | ||
733 | fail: | |
56cf987f DW |
734 | label_file_clear(); |
735 | ||
b15bdda8 LP |
736 | if (fd >= 0) |
737 | close_nointr_nofail(fd); | |
738 | ||
739 | return r; | |
740 | } | |
741 | ||
034c6ed7 | 742 | static int socket_open_fds(Socket *s) { |
83c60c9f LP |
743 | SocketPort *p; |
744 | int r; | |
56cf987f | 745 | char *label = NULL; |
049f8642 | 746 | bool know_label = false; |
83c60c9f LP |
747 | |
748 | assert(s); | |
749 | ||
034c6ed7 | 750 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f | 751 | |
034c6ed7 LP |
752 | if (p->fd >= 0) |
753 | continue; | |
83c60c9f LP |
754 | |
755 | if (p->type == SOCKET_SOCKET) { | |
756 | ||
049f8642 LP |
757 | if (!know_label) { |
758 | ||
759 | if ((r = socket_instantiate_service(s)) < 0) | |
760 | return r; | |
761 | ||
762 | if ((r = label_get_socket_label_from_exe(s->service->exec_command[SERVICE_EXEC_START]->path, &label)) < 0) | |
763 | return r; | |
764 | ||
765 | know_label = true; | |
766 | } | |
767 | ||
b5a0699f LP |
768 | if ((r = socket_address_listen( |
769 | &p->address, | |
770 | s->backlog, | |
771 | s->bind_ipv6_only, | |
772 | s->bind_to_device, | |
4fd5948e | 773 | s->free_bind, |
b5a0699f LP |
774 | s->directory_mode, |
775 | s->socket_mode, | |
56cf987f | 776 | label, |
b5a0699f | 777 | &p->fd)) < 0) |
83c60c9f LP |
778 | goto rollback; |
779 | ||
4fd5948e LP |
780 | socket_apply_socket_options(s, p->fd); |
781 | ||
b15bdda8 | 782 | } else if (p->type == SOCKET_FIFO) { |
83c60c9f | 783 | |
b15bdda8 LP |
784 | if ((r = fifo_address_create( |
785 | p->path, | |
786 | s->directory_mode, | |
787 | s->socket_mode, | |
b15bdda8 | 788 | &p->fd)) < 0) |
83c60c9f | 789 | goto rollback; |
83c60c9f | 790 | |
b15bdda8 | 791 | socket_apply_fifo_options(s, p->fd); |
83c60c9f | 792 | |
b15bdda8 LP |
793 | } else |
794 | assert_not_reached("Unknown port type"); | |
034c6ed7 LP |
795 | } |
796 | ||
56cf987f | 797 | label_free(label); |
034c6ed7 LP |
798 | return 0; |
799 | ||
800 | rollback: | |
801 | socket_close_fds(s); | |
56cf987f | 802 | label_free(label); |
034c6ed7 LP |
803 | return r; |
804 | } | |
805 | ||
806 | static void socket_unwatch_fds(Socket *s) { | |
807 | SocketPort *p; | |
9152c765 | 808 | |
034c6ed7 LP |
809 | assert(s); |
810 | ||
811 | LIST_FOREACH(port, p, s->ports) { | |
812 | if (p->fd < 0) | |
813 | continue; | |
814 | ||
acbb0225 | 815 | unit_unwatch_fd(UNIT(s), &p->fd_watch); |
83c60c9f | 816 | } |
034c6ed7 LP |
817 | } |
818 | ||
819 | static int socket_watch_fds(Socket *s) { | |
820 | SocketPort *p; | |
821 | int r; | |
822 | ||
823 | assert(s); | |
83c60c9f | 824 | |
034c6ed7 LP |
825 | LIST_FOREACH(port, p, s->ports) { |
826 | if (p->fd < 0) | |
827 | continue; | |
828 | ||
cabab516 | 829 | p->fd_watch.socket_accept = |
4f2d528d | 830 | s->accept && |
dd5ad9d4 | 831 | p->type == SOCKET_SOCKET && |
4f2d528d LP |
832 | socket_address_can_accept(&p->address); |
833 | ||
f94ea366 | 834 | if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0) |
034c6ed7 LP |
835 | goto fail; |
836 | } | |
83c60c9f | 837 | |
542563ba | 838 | return 0; |
83c60c9f | 839 | |
034c6ed7 LP |
840 | fail: |
841 | socket_unwatch_fds(s); | |
842 | return r; | |
843 | } | |
844 | ||
845 | static void socket_set_state(Socket *s, SocketState state) { | |
846 | SocketState old_state; | |
847 | assert(s); | |
848 | ||
849 | old_state = s->state; | |
850 | s->state = state; | |
851 | ||
852 | if (state != SOCKET_START_PRE && | |
853 | state != SOCKET_START_POST && | |
854 | state != SOCKET_STOP_PRE && | |
855 | state != SOCKET_STOP_PRE_SIGTERM && | |
856 | state != SOCKET_STOP_PRE_SIGKILL && | |
857 | state != SOCKET_STOP_POST && | |
80876c20 LP |
858 | state != SOCKET_FINAL_SIGTERM && |
859 | state != SOCKET_FINAL_SIGKILL) { | |
acbb0225 | 860 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
5e94833f | 861 | socket_unwatch_control_pid(s); |
034c6ed7 | 862 | s->control_command = NULL; |
a16e1123 | 863 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; |
e537352b | 864 | } |
034c6ed7 | 865 | |
a16e1123 LP |
866 | if (state != SOCKET_LISTENING) |
867 | socket_unwatch_fds(s); | |
868 | ||
034c6ed7 LP |
869 | if (state != SOCKET_START_POST && |
870 | state != SOCKET_LISTENING && | |
871 | state != SOCKET_RUNNING && | |
872 | state != SOCKET_STOP_PRE && | |
873 | state != SOCKET_STOP_PRE_SIGTERM && | |
874 | state != SOCKET_STOP_PRE_SIGKILL) | |
875 | socket_close_fds(s); | |
876 | ||
e537352b | 877 | if (state != old_state) |
40d50879 | 878 | log_debug("%s changed %s -> %s", |
a16e1123 LP |
879 | s->meta.id, |
880 | socket_state_to_string(old_state), | |
881 | socket_state_to_string(state)); | |
acbb0225 LP |
882 | |
883 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); | |
034c6ed7 LP |
884 | } |
885 | ||
a16e1123 LP |
886 | static int socket_coldplug(Unit *u) { |
887 | Socket *s = SOCKET(u); | |
888 | int r; | |
889 | ||
890 | assert(s); | |
891 | assert(s->state == SOCKET_DEAD); | |
892 | ||
893 | if (s->deserialized_state != s->state) { | |
894 | ||
895 | if (s->deserialized_state == SOCKET_START_PRE || | |
896 | s->deserialized_state == SOCKET_START_POST || | |
897 | s->deserialized_state == SOCKET_STOP_PRE || | |
898 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
899 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL || | |
900 | s->deserialized_state == SOCKET_STOP_POST || | |
901 | s->deserialized_state == SOCKET_FINAL_SIGTERM || | |
902 | s->deserialized_state == SOCKET_FINAL_SIGKILL) { | |
903 | ||
904 | if (s->control_pid <= 0) | |
905 | return -EBADMSG; | |
906 | ||
907 | if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) | |
908 | return r; | |
909 | ||
910 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
911 | return r; | |
912 | } | |
913 | ||
914 | if (s->deserialized_state == SOCKET_START_POST || | |
915 | s->deserialized_state == SOCKET_LISTENING || | |
916 | s->deserialized_state == SOCKET_RUNNING || | |
917 | s->deserialized_state == SOCKET_STOP_PRE || | |
918 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
919 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) | |
920 | if ((r = socket_open_fds(s)) < 0) | |
921 | return r; | |
922 | ||
923 | if (s->deserialized_state == SOCKET_LISTENING) | |
924 | if ((r = socket_watch_fds(s)) < 0) | |
925 | return r; | |
926 | ||
927 | socket_set_state(s, s->deserialized_state); | |
928 | } | |
929 | ||
930 | return 0; | |
931 | } | |
932 | ||
e537352b | 933 | static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { |
034c6ed7 LP |
934 | pid_t pid; |
935 | int r; | |
9e2f7c11 | 936 | char **argv; |
034c6ed7 LP |
937 | |
938 | assert(s); | |
939 | assert(c); | |
940 | assert(_pid); | |
941 | ||
e537352b LP |
942 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
943 | goto fail; | |
034c6ed7 | 944 | |
9e2f7c11 LP |
945 | if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) { |
946 | r = -ENOMEM; | |
947 | goto fail; | |
948 | } | |
949 | ||
950 | r = exec_spawn(c, | |
951 | argv, | |
952 | &s->exec_context, | |
953 | NULL, 0, | |
1137a57c | 954 | s->meta.manager->environment, |
9e2f7c11 LP |
955 | true, |
956 | true, | |
1e3ad081 | 957 | true, |
4cd1fbcc LP |
958 | s->meta.manager->confirm_spawn, |
959 | s->meta.cgroup_bondings, | |
9e2f7c11 LP |
960 | &pid); |
961 | ||
962 | strv_free(argv); | |
963 | if (r < 0) | |
034c6ed7 LP |
964 | goto fail; |
965 | ||
87f0e418 | 966 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
967 | /* FIXME: we need to do something here */ |
968 | goto fail; | |
83c60c9f | 969 | |
034c6ed7 LP |
970 | *_pid = pid; |
971 | ||
972 | return 0; | |
973 | ||
974 | fail: | |
e537352b | 975 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
83c60c9f LP |
976 | |
977 | return r; | |
542563ba LP |
978 | } |
979 | ||
034c6ed7 LP |
980 | static void socket_enter_dead(Socket *s, bool success) { |
981 | assert(s); | |
982 | ||
983 | if (!success) | |
984 | s->failure = true; | |
985 | ||
fdf20a31 | 986 | socket_set_state(s, s->failure ? SOCKET_FAILED : SOCKET_DEAD); |
034c6ed7 LP |
987 | } |
988 | ||
80876c20 LP |
989 | static void socket_enter_signal(Socket *s, SocketState state, bool success); |
990 | ||
034c6ed7 LP |
991 | static void socket_enter_stop_post(Socket *s, bool success) { |
992 | int r; | |
993 | assert(s); | |
994 | ||
995 | if (!success) | |
996 | s->failure = true; | |
997 | ||
5e94833f LP |
998 | socket_unwatch_control_pid(s); |
999 | ||
a16e1123 LP |
1000 | s->control_command_id = SOCKET_EXEC_STOP_POST; |
1001 | ||
80876c20 | 1002 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) { |
e537352b | 1003 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1004 | goto fail; |
1005 | ||
80876c20 LP |
1006 | socket_set_state(s, SOCKET_STOP_POST); |
1007 | } else | |
1008 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, true); | |
034c6ed7 LP |
1009 | |
1010 | return; | |
1011 | ||
1012 | fail: | |
6cf6bbc2 | 1013 | log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r)); |
80876c20 | 1014 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1015 | } |
1016 | ||
1017 | static void socket_enter_signal(Socket *s, SocketState state, bool success) { | |
1018 | int r; | |
ca949c9d LP |
1019 | Set *pid_set = NULL; |
1020 | bool wait_for_exit = false; | |
034c6ed7 LP |
1021 | |
1022 | assert(s); | |
1023 | ||
1024 | if (!success) | |
1025 | s->failure = true; | |
1026 | ||
2e22afe9 LP |
1027 | if (s->exec_context.kill_mode != KILL_NONE) { |
1028 | int sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL; | |
034c6ed7 | 1029 | |
ca949c9d LP |
1030 | if (s->control_pid > 0) { |
1031 | if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ? | |
1032 | -s->control_pid : | |
1033 | s->control_pid, sig) < 0 && errno != ESRCH) | |
50159e6a | 1034 | |
ca949c9d LP |
1035 | log_warning("Failed to kill control process %li: %m", (long) s->control_pid); |
1036 | else | |
1037 | wait_for_exit = true; | |
034c6ed7 | 1038 | } |
50159e6a | 1039 | |
ca949c9d LP |
1040 | if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) { |
1041 | ||
1042 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) { | |
1043 | r = -ENOMEM; | |
50159e6a LP |
1044 | goto fail; |
1045 | } | |
ca949c9d LP |
1046 | |
1047 | /* Exclude the control pid from being killed via the cgroup */ | |
1048 | if (s->control_pid > 0) | |
1049 | if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) | |
1050 | goto fail; | |
1051 | ||
1052 | if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) { | |
1053 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
1054 | log_warning("Failed to kill control group: %s", strerror(-r)); | |
1055 | } else if (r > 0) | |
1056 | wait_for_exit = true; | |
1057 | ||
1058 | set_free(pid_set); | |
1059 | } | |
d6ea93e3 | 1060 | } |
034c6ed7 | 1061 | |
ca949c9d | 1062 | if (wait_for_exit) { |
80876c20 LP |
1063 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
1064 | goto fail; | |
d6ea93e3 | 1065 | |
80876c20 LP |
1066 | socket_set_state(s, state); |
1067 | } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
1068 | socket_enter_stop_post(s, true); | |
1069 | else | |
034c6ed7 LP |
1070 | socket_enter_dead(s, true); |
1071 | ||
1072 | return; | |
1073 | ||
1074 | fail: | |
9e2f7c11 | 1075 | log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1076 | |
1077 | if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
1078 | socket_enter_stop_post(s, false); | |
1079 | else | |
1080 | socket_enter_dead(s, false); | |
ca949c9d LP |
1081 | |
1082 | if (pid_set) | |
1083 | set_free(pid_set); | |
034c6ed7 LP |
1084 | } |
1085 | ||
1086 | static void socket_enter_stop_pre(Socket *s, bool success) { | |
1087 | int r; | |
1088 | assert(s); | |
1089 | ||
1090 | if (!success) | |
1091 | s->failure = true; | |
1092 | ||
5e94833f LP |
1093 | socket_unwatch_control_pid(s); |
1094 | ||
a16e1123 LP |
1095 | s->control_command_id = SOCKET_EXEC_STOP_PRE; |
1096 | ||
80876c20 | 1097 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) { |
e537352b | 1098 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1099 | goto fail; |
1100 | ||
80876c20 LP |
1101 | socket_set_state(s, SOCKET_STOP_PRE); |
1102 | } else | |
034c6ed7 LP |
1103 | socket_enter_stop_post(s, true); |
1104 | ||
1105 | return; | |
1106 | ||
1107 | fail: | |
6cf6bbc2 | 1108 | log_warning("%s failed to run 'stop-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1109 | socket_enter_stop_post(s, false); |
1110 | } | |
1111 | ||
e9af15c3 LP |
1112 | static void socket_enter_listening(Socket *s) { |
1113 | int r; | |
1114 | assert(s); | |
1115 | ||
1116 | if ((r = socket_watch_fds(s)) < 0) { | |
9e2f7c11 | 1117 | log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r)); |
e9af15c3 LP |
1118 | goto fail; |
1119 | } | |
1120 | ||
1121 | socket_set_state(s, SOCKET_LISTENING); | |
1122 | return; | |
1123 | ||
1124 | fail: | |
1125 | socket_enter_stop_pre(s, false); | |
1126 | } | |
1127 | ||
034c6ed7 LP |
1128 | static void socket_enter_start_post(Socket *s) { |
1129 | int r; | |
1130 | assert(s); | |
1131 | ||
e9af15c3 | 1132 | if ((r = socket_open_fds(s)) < 0) { |
9e2f7c11 | 1133 | log_warning("%s failed to listen on sockets: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1134 | goto fail; |
1135 | } | |
1136 | ||
5e94833f LP |
1137 | socket_unwatch_control_pid(s); |
1138 | ||
a16e1123 LP |
1139 | s->control_command_id = SOCKET_EXEC_START_POST; |
1140 | ||
80876c20 | 1141 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { |
e537352b | 1142 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) { |
6cf6bbc2 | 1143 | log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1144 | goto fail; |
1145 | } | |
1146 | ||
80876c20 LP |
1147 | socket_set_state(s, SOCKET_START_POST); |
1148 | } else | |
e9af15c3 | 1149 | socket_enter_listening(s); |
034c6ed7 LP |
1150 | |
1151 | return; | |
1152 | ||
1153 | fail: | |
1154 | socket_enter_stop_pre(s, false); | |
1155 | } | |
1156 | ||
1157 | static void socket_enter_start_pre(Socket *s) { | |
1158 | int r; | |
1159 | assert(s); | |
1160 | ||
5e94833f LP |
1161 | socket_unwatch_control_pid(s); |
1162 | ||
a16e1123 LP |
1163 | s->control_command_id = SOCKET_EXEC_START_PRE; |
1164 | ||
80876c20 | 1165 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { |
e537352b | 1166 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1167 | goto fail; |
1168 | ||
80876c20 LP |
1169 | socket_set_state(s, SOCKET_START_PRE); |
1170 | } else | |
034c6ed7 LP |
1171 | socket_enter_start_post(s); |
1172 | ||
1173 | return; | |
1174 | ||
1175 | fail: | |
6cf6bbc2 | 1176 | log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1177 | socket_enter_dead(s, false); |
1178 | } | |
1179 | ||
4f2d528d | 1180 | static void socket_enter_running(Socket *s, int cfd) { |
034c6ed7 | 1181 | int r; |
398ef8ba | 1182 | DBusError error; |
034c6ed7 LP |
1183 | |
1184 | assert(s); | |
398ef8ba | 1185 | dbus_error_init(&error); |
034c6ed7 | 1186 | |
ba3e67a7 LP |
1187 | /* We don't take connections anymore if we are supposed to |
1188 | * shut down anyway */ | |
18ffdfda | 1189 | if (unit_pending_inactive(UNIT(s))) { |
7c610628 LP |
1190 | if (cfd >= 0) |
1191 | close_nointr_nofail(cfd); | |
1192 | else { | |
1193 | /* Flush all sockets by closing and reopening them */ | |
1194 | socket_close_fds(s); | |
1195 | ||
1196 | if ((r = socket_watch_fds(s)) < 0) { | |
1197 | log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r)); | |
1198 | socket_enter_stop_pre(s, false); | |
1199 | } | |
1200 | } | |
1201 | ||
ba3e67a7 LP |
1202 | return; |
1203 | } | |
1204 | ||
4f2d528d | 1205 | if (cfd < 0) { |
f976f3f6 LP |
1206 | bool pending = false; |
1207 | Meta *i; | |
1208 | ||
1209 | /* If there's already a start pending don't bother to | |
1210 | * do anything */ | |
1211 | LIST_FOREACH(units_per_type, i, s->meta.manager->units_per_type[UNIT_SERVICE]) { | |
1212 | Service *service = (Service *) i; | |
1213 | ||
1214 | if (!set_get(service->configured_sockets, s)) | |
1215 | continue; | |
1216 | ||
1217 | if (!unit_pending_active(UNIT(service))) | |
1218 | continue; | |
1219 | ||
1220 | pending = true; | |
1221 | break; | |
1222 | } | |
1223 | ||
1224 | if (!pending) | |
1225 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, &error, NULL)) < 0) | |
1226 | goto fail; | |
4f2d528d LP |
1227 | |
1228 | socket_set_state(s, SOCKET_RUNNING); | |
1229 | } else { | |
b4f10a5e | 1230 | char *prefix, *instance = NULL, *name; |
b15bdda8 | 1231 | Service *service; |
4f2d528d | 1232 | |
6cf6bbc2 LP |
1233 | if (s->n_connections >= s->max_connections) { |
1234 | log_warning("Too many incoming connections (%u)", s->n_connections); | |
1235 | close_nointr_nofail(cfd); | |
1236 | return; | |
1237 | } | |
1238 | ||
b15bdda8 LP |
1239 | if ((r = socket_instantiate_service(s)) < 0) |
1240 | goto fail; | |
1241 | ||
1242 | if ((r = instance_from_socket(cfd, s->n_accepted, &instance)) < 0) | |
4f2d528d LP |
1243 | goto fail; |
1244 | ||
4cd1fbcc | 1245 | if (!(prefix = unit_name_to_prefix(s->meta.id))) { |
4f2d528d LP |
1246 | free(instance); |
1247 | r = -ENOMEM; | |
1248 | goto fail; | |
1249 | } | |
1250 | ||
1251 | name = unit_name_build(prefix, instance, ".service"); | |
1252 | free(prefix); | |
1253 | free(instance); | |
1254 | ||
b6dbbe1c | 1255 | if (!name) { |
4f2d528d | 1256 | r = -ENOMEM; |
b6dbbe1c LP |
1257 | goto fail; |
1258 | } | |
4f2d528d | 1259 | |
b15bdda8 LP |
1260 | if ((r = unit_add_name(UNIT(s->service), name)) < 0) { |
1261 | free(name); | |
4f2d528d | 1262 | goto fail; |
b15bdda8 LP |
1263 | } |
1264 | ||
1265 | service = s->service; | |
1266 | s->service = NULL; | |
1267 | s->n_accepted ++; | |
4f2d528d | 1268 | |
6c073082 LP |
1269 | service->meta.no_gc = false; |
1270 | ||
b15bdda8 LP |
1271 | unit_choose_id(UNIT(service), name); |
1272 | free(name); | |
1273 | ||
1274 | if ((r = service_set_socket_fd(service, cfd, s)) < 0) | |
4f2d528d LP |
1275 | goto fail; |
1276 | ||
1277 | cfd = -1; | |
6cf6bbc2 LP |
1278 | s->n_connections ++; |
1279 | ||
b15bdda8 | 1280 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL)) < 0) |
4f2d528d | 1281 | goto fail; |
c4e2ceae LP |
1282 | |
1283 | /* Notify clients about changed counters */ | |
1284 | unit_add_to_dbus_queue(UNIT(s)); | |
4f2d528d | 1285 | } |
034c6ed7 | 1286 | |
034c6ed7 LP |
1287 | return; |
1288 | ||
1289 | fail: | |
398ef8ba | 1290 | log_warning("%s failed to queue socket startup job: %s", s->meta.id, bus_error(&error, r)); |
80876c20 | 1291 | socket_enter_stop_pre(s, false); |
4f2d528d LP |
1292 | |
1293 | if (cfd >= 0) | |
1294 | close_nointr_nofail(cfd); | |
398ef8ba LP |
1295 | |
1296 | dbus_error_free(&error); | |
034c6ed7 LP |
1297 | } |
1298 | ||
1299 | static void socket_run_next(Socket *s, bool success) { | |
1300 | int r; | |
1301 | ||
1302 | assert(s); | |
1303 | assert(s->control_command); | |
1304 | assert(s->control_command->command_next); | |
1305 | ||
1306 | if (!success) | |
1307 | s->failure = true; | |
1308 | ||
5e94833f LP |
1309 | socket_unwatch_control_pid(s); |
1310 | ||
034c6ed7 LP |
1311 | s->control_command = s->control_command->command_next; |
1312 | ||
e537352b | 1313 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1314 | goto fail; |
1315 | ||
1316 | return; | |
1317 | ||
1318 | fail: | |
6cf6bbc2 | 1319 | log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r)); |
80876c20 LP |
1320 | |
1321 | if (s->state == SOCKET_START_POST) | |
1322 | socket_enter_stop_pre(s, false); | |
034c6ed7 LP |
1323 | else if (s->state == SOCKET_STOP_POST) |
1324 | socket_enter_dead(s, false); | |
1325 | else | |
80876c20 | 1326 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1327 | } |
1328 | ||
87f0e418 LP |
1329 | static int socket_start(Unit *u) { |
1330 | Socket *s = SOCKET(u); | |
83c60c9f LP |
1331 | |
1332 | assert(s); | |
1333 | ||
034c6ed7 LP |
1334 | /* We cannot fulfill this request right now, try again later |
1335 | * please! */ | |
1336 | if (s->state == SOCKET_STOP_PRE || | |
1337 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1338 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1339 | s->state == SOCKET_STOP_POST || | |
80876c20 LP |
1340 | s->state == SOCKET_FINAL_SIGTERM || |
1341 | s->state == SOCKET_FINAL_SIGKILL) | |
034c6ed7 LP |
1342 | return -EAGAIN; |
1343 | ||
83c60c9f LP |
1344 | if (s->state == SOCKET_START_PRE || |
1345 | s->state == SOCKET_START_POST) | |
034c6ed7 | 1346 | return 0; |
83c60c9f | 1347 | |
034c6ed7 | 1348 | /* Cannot run this without the service being around */ |
4f2d528d LP |
1349 | if (s->service) { |
1350 | if (s->service->meta.load_state != UNIT_LOADED) | |
1351 | return -ENOENT; | |
1352 | ||
1353 | /* If the service is alredy actvie we cannot start the | |
1354 | * socket */ | |
1355 | if (s->service->state != SERVICE_DEAD && | |
fdf20a31 | 1356 | s->service->state != SERVICE_FAILED && |
4f2d528d LP |
1357 | s->service->state != SERVICE_AUTO_RESTART) |
1358 | return -EBUSY; | |
1359 | } | |
e537352b | 1360 | |
fdf20a31 | 1361 | assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED); |
83c60c9f | 1362 | |
034c6ed7 LP |
1363 | s->failure = false; |
1364 | socket_enter_start_pre(s); | |
1365 | return 0; | |
1366 | } | |
83c60c9f | 1367 | |
87f0e418 LP |
1368 | static int socket_stop(Unit *u) { |
1369 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
1370 | |
1371 | assert(s); | |
1372 | ||
e537352b LP |
1373 | /* Already on it */ |
1374 | if (s->state == SOCKET_STOP_PRE || | |
1375 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1376 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1377 | s->state == SOCKET_STOP_POST || | |
80876c20 | 1378 | s->state == SOCKET_FINAL_SIGTERM || |
3f6c78dc | 1379 | s->state == SOCKET_FINAL_SIGKILL) |
e537352b LP |
1380 | return 0; |
1381 | ||
3f6c78dc LP |
1382 | /* If there's already something running we go directly into |
1383 | * kill mode. */ | |
1384 | if (s->state == SOCKET_START_PRE || | |
1385 | s->state == SOCKET_START_POST) { | |
1386 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, true); | |
1387 | return -EAGAIN; | |
1388 | } | |
1389 | ||
034c6ed7 | 1390 | assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING); |
83c60c9f | 1391 | |
034c6ed7 | 1392 | socket_enter_stop_pre(s, true); |
542563ba LP |
1393 | return 0; |
1394 | } | |
1395 | ||
a16e1123 LP |
1396 | static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { |
1397 | Socket *s = SOCKET(u); | |
1398 | SocketPort *p; | |
1399 | int r; | |
1400 | ||
1401 | assert(u); | |
1402 | assert(f); | |
1403 | assert(fds); | |
1404 | ||
1405 | unit_serialize_item(u, f, "state", socket_state_to_string(s->state)); | |
1406 | unit_serialize_item(u, f, "failure", yes_no(s->failure)); | |
1407 | unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted); | |
1408 | ||
1409 | if (s->control_pid > 0) | |
5925dd3c | 1410 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); |
a16e1123 LP |
1411 | |
1412 | if (s->control_command_id >= 0) | |
1413 | unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id)); | |
1414 | ||
1415 | LIST_FOREACH(port, p, s->ports) { | |
1416 | int copy; | |
1417 | ||
1418 | if (p->fd < 0) | |
1419 | continue; | |
1420 | ||
1421 | if ((copy = fdset_put_dup(fds, p->fd)) < 0) | |
1422 | return copy; | |
1423 | ||
1424 | if (p->type == SOCKET_SOCKET) { | |
1425 | char *t; | |
1426 | ||
1427 | if ((r = socket_address_print(&p->address, &t)) < 0) | |
1428 | return r; | |
1429 | ||
27ca8d7a | 1430 | unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t); |
a16e1123 LP |
1431 | free(t); |
1432 | } else { | |
1433 | assert(p->type == SOCKET_FIFO); | |
1434 | unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path); | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | return 0; | |
1439 | } | |
1440 | ||
1441 | static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1442 | Socket *s = SOCKET(u); | |
a16e1123 LP |
1443 | |
1444 | assert(u); | |
1445 | assert(key); | |
1446 | assert(value); | |
1447 | assert(fds); | |
1448 | ||
1449 | if (streq(key, "state")) { | |
1450 | SocketState state; | |
1451 | ||
1452 | if ((state = socket_state_from_string(value)) < 0) | |
1453 | log_debug("Failed to parse state value %s", value); | |
1454 | else | |
1455 | s->deserialized_state = state; | |
1456 | } else if (streq(key, "failure")) { | |
1457 | int b; | |
1458 | ||
1459 | if ((b = parse_boolean(value)) < 0) | |
1460 | log_debug("Failed to parse failure value %s", value); | |
1461 | else | |
1462 | s->failure = b || s->failure; | |
1463 | ||
1464 | } else if (streq(key, "n-accepted")) { | |
1465 | unsigned k; | |
1466 | ||
e364ad06 | 1467 | if (safe_atou(value, &k) < 0) |
a16e1123 LP |
1468 | log_debug("Failed to parse n-accepted value %s", value); |
1469 | else | |
1470 | s->n_accepted += k; | |
1471 | } else if (streq(key, "control-pid")) { | |
5925dd3c | 1472 | pid_t pid; |
a16e1123 | 1473 | |
e364ad06 | 1474 | if (parse_pid(value, &pid) < 0) |
a16e1123 LP |
1475 | log_debug("Failed to parse control-pid value %s", value); |
1476 | else | |
5925dd3c | 1477 | s->control_pid = pid; |
a16e1123 LP |
1478 | } else if (streq(key, "control-command")) { |
1479 | SocketExecCommand id; | |
1480 | ||
1481 | if ((id = socket_exec_command_from_string(value)) < 0) | |
1482 | log_debug("Failed to parse exec-command value %s", value); | |
1483 | else { | |
1484 | s->control_command_id = id; | |
1485 | s->control_command = s->exec_command[id]; | |
1486 | } | |
1487 | } else if (streq(key, "fifo")) { | |
1488 | int fd, skip = 0; | |
1489 | SocketPort *p; | |
1490 | ||
1491 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
1492 | log_debug("Failed to parse fifo value %s", value); | |
1493 | else { | |
1494 | ||
1495 | LIST_FOREACH(port, p, s->ports) | |
1496 | if (streq(p->path, value+skip)) | |
1497 | break; | |
1498 | ||
1499 | if (p) { | |
1500 | if (p->fd >= 0) | |
1501 | close_nointr_nofail(p->fd); | |
1502 | p->fd = fdset_remove(fds, fd); | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | } else if (streq(key, "socket")) { | |
27ca8d7a | 1507 | int fd, type, skip = 0; |
a16e1123 LP |
1508 | SocketPort *p; |
1509 | ||
27ca8d7a | 1510 | if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd)) |
a16e1123 LP |
1511 | log_debug("Failed to parse socket value %s", value); |
1512 | else { | |
1513 | ||
1514 | LIST_FOREACH(port, p, s->ports) | |
27ca8d7a | 1515 | if (socket_address_is(&p->address, value+skip, type)) |
a16e1123 LP |
1516 | break; |
1517 | ||
1518 | if (p) { | |
1519 | if (p->fd >= 0) | |
1520 | close_nointr_nofail(p->fd); | |
1521 | p->fd = fdset_remove(fds, fd); | |
1522 | } | |
1523 | } | |
1524 | ||
1525 | } else | |
1526 | log_debug("Unknown serialization key '%s'", key); | |
1527 | ||
1528 | return 0; | |
1529 | } | |
1530 | ||
87f0e418 LP |
1531 | static UnitActiveState socket_active_state(Unit *u) { |
1532 | assert(u); | |
5cb5a6ff | 1533 | |
acbb0225 | 1534 | return state_translation_table[SOCKET(u)->state]; |
5cb5a6ff LP |
1535 | } |
1536 | ||
10a94420 LP |
1537 | static const char *socket_sub_state_to_string(Unit *u) { |
1538 | assert(u); | |
1539 | ||
a16e1123 | 1540 | return socket_state_to_string(SOCKET(u)->state); |
10a94420 LP |
1541 | } |
1542 | ||
6cf6bbc2 LP |
1543 | static bool socket_check_gc(Unit *u) { |
1544 | Socket *s = SOCKET(u); | |
1545 | ||
1546 | assert(u); | |
1547 | ||
1548 | return s->n_connections > 0; | |
1549 | } | |
1550 | ||
acbb0225 | 1551 | static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 | 1552 | Socket *s = SOCKET(u); |
4f2d528d | 1553 | int cfd = -1; |
9152c765 | 1554 | |
034c6ed7 | 1555 | assert(s); |
8d567588 | 1556 | assert(fd >= 0); |
9152c765 | 1557 | |
871d7de4 LP |
1558 | if (s->state != SOCKET_LISTENING) |
1559 | return; | |
1560 | ||
9e2f7c11 | 1561 | log_debug("Incoming traffic on %s", u->meta.id); |
9152c765 | 1562 | |
4f2d528d LP |
1563 | if (events != EPOLLIN) { |
1564 | log_error("Got invalid poll event on socket."); | |
8d567588 | 1565 | goto fail; |
4f2d528d LP |
1566 | } |
1567 | ||
cabab516 | 1568 | if (w->socket_accept) { |
4f2d528d LP |
1569 | for (;;) { |
1570 | ||
1571 | if ((cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK)) < 0) { | |
1572 | ||
1573 | if (errno == EINTR) | |
1574 | continue; | |
1575 | ||
1576 | log_error("Failed to accept socket: %m"); | |
8d567588 | 1577 | goto fail; |
4f2d528d LP |
1578 | } |
1579 | ||
1580 | break; | |
1581 | } | |
4fd5948e LP |
1582 | |
1583 | socket_apply_socket_options(s, cfd); | |
4f2d528d | 1584 | } |
9152c765 | 1585 | |
4f2d528d | 1586 | socket_enter_running(s, cfd); |
8d567588 LP |
1587 | return; |
1588 | ||
1589 | fail: | |
1590 | socket_enter_stop_pre(s, false); | |
9152c765 LP |
1591 | } |
1592 | ||
87f0e418 LP |
1593 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
1594 | Socket *s = SOCKET(u); | |
034c6ed7 | 1595 | bool success; |
5cb5a6ff LP |
1596 | |
1597 | assert(s); | |
034c6ed7 | 1598 | assert(pid >= 0); |
5cb5a6ff | 1599 | |
8c47c732 LP |
1600 | if (pid != s->control_pid) |
1601 | return; | |
542563ba | 1602 | |
034c6ed7 LP |
1603 | s->control_pid = 0; |
1604 | ||
8c47c732 | 1605 | success = is_clean_exit(code, status); |
8c47c732 | 1606 | |
b708e7ce | 1607 | if (s->control_command) { |
169c1bda | 1608 | exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id); |
a16e1123 | 1609 | |
b708e7ce LP |
1610 | if (s->control_command->ignore) |
1611 | success = true; | |
1612 | } | |
1613 | ||
92abbefb LP |
1614 | log_full(success ? LOG_DEBUG : LOG_NOTICE, |
1615 | "%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
b708e7ce | 1616 | s->failure = s->failure || !success; |
034c6ed7 | 1617 | |
a16e1123 LP |
1618 | if (s->control_command && s->control_command->command_next && success) { |
1619 | log_debug("%s running next command for state %s", u->meta.id, socket_state_to_string(s->state)); | |
034c6ed7 | 1620 | socket_run_next(s, success); |
acbb0225 | 1621 | } else { |
a16e1123 LP |
1622 | s->control_command = NULL; |
1623 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
1624 | ||
034c6ed7 LP |
1625 | /* No further commands for this step, so let's figure |
1626 | * out what to do next */ | |
5cb5a6ff | 1627 | |
a16e1123 | 1628 | log_debug("%s got final SIGCHLD for state %s", u->meta.id, socket_state_to_string(s->state)); |
acbb0225 | 1629 | |
034c6ed7 LP |
1630 | switch (s->state) { |
1631 | ||
1632 | case SOCKET_START_PRE: | |
1633 | if (success) | |
acbb0225 | 1634 | socket_enter_start_post(s); |
034c6ed7 | 1635 | else |
80876c20 | 1636 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1637 | break; |
1638 | ||
1639 | case SOCKET_START_POST: | |
1640 | if (success) | |
e9af15c3 | 1641 | socket_enter_listening(s); |
034c6ed7 LP |
1642 | else |
1643 | socket_enter_stop_pre(s, false); | |
1644 | break; | |
1645 | ||
1646 | case SOCKET_STOP_PRE: | |
1647 | case SOCKET_STOP_PRE_SIGTERM: | |
1648 | case SOCKET_STOP_PRE_SIGKILL: | |
1649 | socket_enter_stop_post(s, success); | |
1650 | break; | |
1651 | ||
1652 | case SOCKET_STOP_POST: | |
80876c20 LP |
1653 | case SOCKET_FINAL_SIGTERM: |
1654 | case SOCKET_FINAL_SIGKILL: | |
034c6ed7 LP |
1655 | socket_enter_dead(s, success); |
1656 | break; | |
1657 | ||
1658 | default: | |
1659 | assert_not_reached("Uh, control process died at wrong time."); | |
1660 | } | |
1661 | } | |
c4e2ceae LP |
1662 | |
1663 | /* Notify clients about changed exit status */ | |
1664 | unit_add_to_dbus_queue(u); | |
034c6ed7 | 1665 | } |
5cb5a6ff | 1666 | |
acbb0225 | 1667 | static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) { |
87f0e418 | 1668 | Socket *s = SOCKET(u); |
5cb5a6ff | 1669 | |
034c6ed7 LP |
1670 | assert(s); |
1671 | assert(elapsed == 1); | |
acbb0225 | 1672 | assert(w == &s->timer_watch); |
034c6ed7 LP |
1673 | |
1674 | switch (s->state) { | |
1675 | ||
1676 | case SOCKET_START_PRE: | |
9e2f7c11 | 1677 | log_warning("%s starting timed out. Terminating.", u->meta.id); |
80876c20 LP |
1678 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
1679 | ||
034c6ed7 | 1680 | case SOCKET_START_POST: |
9e2f7c11 | 1681 | log_warning("%s starting timed out. Stopping.", u->meta.id); |
034c6ed7 LP |
1682 | socket_enter_stop_pre(s, false); |
1683 | break; | |
1684 | ||
1685 | case SOCKET_STOP_PRE: | |
9e2f7c11 | 1686 | log_warning("%s stopping timed out. Terminating.", u->meta.id); |
034c6ed7 LP |
1687 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false); |
1688 | break; | |
1689 | ||
1690 | case SOCKET_STOP_PRE_SIGTERM: | |
9e2f7c11 | 1691 | log_warning("%s stopping timed out. Killing.", u->meta.id); |
034c6ed7 LP |
1692 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false); |
1693 | break; | |
1694 | ||
1695 | case SOCKET_STOP_PRE_SIGKILL: | |
9e2f7c11 | 1696 | log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id); |
034c6ed7 LP |
1697 | socket_enter_stop_post(s, false); |
1698 | break; | |
1699 | ||
1700 | case SOCKET_STOP_POST: | |
9e2f7c11 | 1701 | log_warning("%s stopping timed out (2). Terminating.", u->meta.id); |
80876c20 | 1702 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1703 | break; |
1704 | ||
80876c20 | 1705 | case SOCKET_FINAL_SIGTERM: |
9e2f7c11 | 1706 | log_warning("%s stopping timed out (2). Killing.", u->meta.id); |
80876c20 | 1707 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, false); |
034c6ed7 LP |
1708 | break; |
1709 | ||
80876c20 | 1710 | case SOCKET_FINAL_SIGKILL: |
fdf20a31 | 1711 | log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id); |
034c6ed7 LP |
1712 | socket_enter_dead(s, false); |
1713 | break; | |
1714 | ||
1715 | default: | |
1716 | assert_not_reached("Timeout at wrong time."); | |
1717 | } | |
5cb5a6ff LP |
1718 | } |
1719 | ||
44d8db9e LP |
1720 | int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { |
1721 | int *rfds; | |
1722 | unsigned rn_fds, k; | |
1723 | SocketPort *p; | |
1724 | ||
1725 | assert(s); | |
1726 | assert(fds); | |
1727 | assert(n_fds); | |
1728 | ||
1729 | /* Called from the service code for requesting our fds */ | |
1730 | ||
1731 | rn_fds = 0; | |
1732 | LIST_FOREACH(port, p, s->ports) | |
1733 | if (p->fd >= 0) | |
1734 | rn_fds++; | |
1735 | ||
e364ad06 | 1736 | if (!(rfds = new(int, rn_fds))) |
44d8db9e LP |
1737 | return -ENOMEM; |
1738 | ||
1739 | k = 0; | |
1740 | LIST_FOREACH(port, p, s->ports) | |
1741 | if (p->fd >= 0) | |
1742 | rfds[k++] = p->fd; | |
1743 | ||
1744 | assert(k == rn_fds); | |
1745 | ||
1746 | *fds = rfds; | |
1747 | *n_fds = rn_fds; | |
1748 | ||
1749 | return 0; | |
1750 | } | |
1751 | ||
ceee3d82 LP |
1752 | void socket_notify_service_dead(Socket *s) { |
1753 | assert(s); | |
1754 | ||
6cf6bbc2 LP |
1755 | /* The service is dead. Dang! |
1756 | * | |
1757 | * This is strictly for one-instance-for-all-connections | |
1758 | * services. */ | |
ceee3d82 LP |
1759 | |
1760 | if (s->state == SOCKET_RUNNING) { | |
9e2f7c11 | 1761 | log_debug("%s got notified about service death.", s->meta.id); |
ceee3d82 LP |
1762 | socket_enter_listening(s); |
1763 | } | |
1764 | } | |
1765 | ||
6cf6bbc2 LP |
1766 | void socket_connection_unref(Socket *s) { |
1767 | assert(s); | |
1768 | ||
1769 | /* The service is dead. Yay! | |
1770 | * | |
1771 | * This is strictly for one-onstance-per-connection | |
1772 | * services. */ | |
1773 | ||
1774 | assert(s->n_connections > 0); | |
1775 | s->n_connections--; | |
1776 | ||
1777 | log_debug("%s: One connection closed, %u left.", s->meta.id, s->n_connections); | |
1778 | } | |
1779 | ||
fdf20a31 | 1780 | static void socket_reset_failed(Unit *u) { |
5632e374 LP |
1781 | Socket *s = SOCKET(u); |
1782 | ||
1783 | assert(s); | |
1784 | ||
fdf20a31 | 1785 | if (s->state == SOCKET_FAILED) |
5632e374 LP |
1786 | socket_set_state(s, SOCKET_DEAD); |
1787 | ||
1788 | s->failure = false; | |
1789 | } | |
1790 | ||
8a0867d6 LP |
1791 | static int socket_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) { |
1792 | Socket *s = SOCKET(u); | |
1793 | int r = 0; | |
1794 | Set *pid_set = NULL; | |
1795 | ||
1796 | assert(s); | |
1797 | ||
1798 | if (who == KILL_MAIN) { | |
1799 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Socket units have no main processes"); | |
1800 | return -EINVAL; | |
1801 | } | |
1802 | ||
1803 | if (s->control_pid <= 0 && who == KILL_CONTROL) { | |
1804 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); | |
1805 | return -ENOENT; | |
1806 | } | |
1807 | ||
1808 | if (s->control_pid > 0) | |
1809 | if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0) | |
1810 | r = -errno; | |
1811 | ||
1812 | if (mode == KILL_CONTROL_GROUP) { | |
1813 | int q; | |
1814 | ||
1815 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) | |
1816 | return -ENOMEM; | |
1817 | ||
1818 | /* Exclude the control pid from being killed via the cgroup */ | |
1819 | if (s->control_pid > 0) | |
1820 | if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) { | |
1821 | r = q; | |
1822 | goto finish; | |
1823 | } | |
1824 | ||
1825 | if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0) | |
1826 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
1827 | r = q; | |
1828 | } | |
1829 | ||
1830 | finish: | |
1831 | if (pid_set) | |
1832 | set_free(pid_set); | |
1833 | ||
1834 | return r; | |
1835 | } | |
1836 | ||
a16e1123 LP |
1837 | static const char* const socket_state_table[_SOCKET_STATE_MAX] = { |
1838 | [SOCKET_DEAD] = "dead", | |
1839 | [SOCKET_START_PRE] = "start-pre", | |
1840 | [SOCKET_START_POST] = "start-post", | |
1841 | [SOCKET_LISTENING] = "listening", | |
1842 | [SOCKET_RUNNING] = "running", | |
1843 | [SOCKET_STOP_PRE] = "stop-pre", | |
1844 | [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm", | |
1845 | [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill", | |
1846 | [SOCKET_STOP_POST] = "stop-post", | |
1847 | [SOCKET_FINAL_SIGTERM] = "final-sigterm", | |
1848 | [SOCKET_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 1849 | [SOCKET_FAILED] = "failed" |
a16e1123 LP |
1850 | }; |
1851 | ||
1852 | DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState); | |
1853 | ||
1854 | static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { | |
1855 | [SOCKET_EXEC_START_PRE] = "StartPre", | |
1856 | [SOCKET_EXEC_START_POST] = "StartPost", | |
1857 | [SOCKET_EXEC_STOP_PRE] = "StopPre", | |
1858 | [SOCKET_EXEC_STOP_POST] = "StopPost" | |
1859 | }; | |
1860 | ||
1861 | DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); | |
1862 | ||
87f0e418 | 1863 | const UnitVTable socket_vtable = { |
5cb5a6ff LP |
1864 | .suffix = ".socket", |
1865 | ||
034c6ed7 LP |
1866 | .init = socket_init, |
1867 | .done = socket_done, | |
a16e1123 LP |
1868 | .load = socket_load, |
1869 | ||
8a0867d6 LP |
1870 | .kill = socket_kill, |
1871 | ||
a16e1123 | 1872 | .coldplug = socket_coldplug, |
034c6ed7 | 1873 | |
5cb5a6ff LP |
1874 | .dump = socket_dump, |
1875 | ||
542563ba LP |
1876 | .start = socket_start, |
1877 | .stop = socket_stop, | |
5cb5a6ff | 1878 | |
a16e1123 LP |
1879 | .serialize = socket_serialize, |
1880 | .deserialize_item = socket_deserialize_item, | |
1881 | ||
5cb5a6ff | 1882 | .active_state = socket_active_state, |
10a94420 | 1883 | .sub_state_to_string = socket_sub_state_to_string, |
5cb5a6ff | 1884 | |
6cf6bbc2 LP |
1885 | .check_gc = socket_check_gc, |
1886 | ||
9152c765 | 1887 | .fd_event = socket_fd_event, |
034c6ed7 | 1888 | .sigchld_event = socket_sigchld_event, |
4139c1b2 LP |
1889 | .timer_event = socket_timer_event, |
1890 | ||
fdf20a31 | 1891 | .reset_failed = socket_reset_failed, |
5632e374 | 1892 | |
c4e2ceae LP |
1893 | .bus_interface = "org.freedesktop.systemd1.Socket", |
1894 | .bus_message_handler = bus_socket_message_handler, | |
1895 | .bus_invalidating_properties = bus_socket_invalidating_properties | |
5cb5a6ff | 1896 | }; |