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