]>
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, | |
032ff4af | 57 | [SOCKET_MAINTENANCE] = UNIT_MAINTENANCE |
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 TT |
387 | prefix, yes_no(s->free_bind), |
388 | prefix, 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)); | |
637 | x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl)); | |
638 | ||
639 | if (r < 0 && x < 0) | |
640 | log_warning("IP_TTL/IPV6_UNICAST_HOPS failed: %m"); | |
641 | } | |
cebf8b20 TT |
642 | |
643 | if (s->tcp_congestion) | |
644 | if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0) | |
645 | log_warning("TCP_CONGESTION failed: %m"); | |
4fd5948e LP |
646 | } |
647 | ||
b15bdda8 | 648 | static void socket_apply_fifo_options(Socket *s, int fd) { |
4fd5948e LP |
649 | assert(s); |
650 | assert(fd >= 0); | |
651 | ||
652 | if (s->pipe_size > 0) | |
653 | if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0) | |
654 | log_warning("F_SETPIPE_SZ: %m"); | |
655 | } | |
656 | ||
7a58bfa4 | 657 | |
b15bdda8 LP |
658 | static int fifo_address_create( |
659 | const char *path, | |
660 | mode_t directory_mode, | |
661 | mode_t socket_mode, | |
56cf987f | 662 | const char *label, |
b15bdda8 LP |
663 | int *_fd) { |
664 | ||
7a58bfa4 | 665 | int fd = -1, r = 0; |
b15bdda8 LP |
666 | struct stat st; |
667 | mode_t old_mask; | |
668 | ||
669 | assert(path); | |
670 | assert(_fd); | |
671 | ||
672 | mkdir_parents(path, directory_mode); | |
673 | ||
56cf987f DW |
674 | if ((r = label_fifofile_set(label, path)) < 0) |
675 | goto fail; | |
b15bdda8 LP |
676 | |
677 | /* Enforce the right access mode for the fifo */ | |
678 | old_mask = umask(~ socket_mode); | |
679 | ||
680 | /* Include the original umask in our mask */ | |
681 | umask(~socket_mode | old_mask); | |
682 | ||
683 | r = mkfifo(path, socket_mode); | |
684 | umask(old_mask); | |
685 | ||
686 | if (r < 0) { | |
687 | r = -errno; | |
688 | goto fail; | |
689 | } | |
690 | ||
691 | if ((fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) { | |
692 | r = -errno; | |
693 | goto fail; | |
694 | } | |
695 | ||
56cf987f | 696 | label_file_clear(); |
7a58bfa4 | 697 | |
b15bdda8 LP |
698 | if (fstat(fd, &st) < 0) { |
699 | r = -errno; | |
700 | goto fail; | |
701 | } | |
702 | ||
703 | if (!S_ISFIFO(st.st_mode) || | |
de0200fc | 704 | (st.st_mode & 0777) != (socket_mode & ~old_mask) || |
b15bdda8 LP |
705 | st.st_uid != getuid() || |
706 | st.st_gid != getgid()) { | |
707 | ||
708 | r = -EEXIST; | |
709 | goto fail; | |
710 | } | |
711 | ||
712 | *_fd = fd; | |
713 | return 0; | |
714 | ||
715 | fail: | |
56cf987f DW |
716 | label_file_clear(); |
717 | ||
b15bdda8 LP |
718 | if (fd >= 0) |
719 | close_nointr_nofail(fd); | |
720 | ||
721 | return r; | |
722 | } | |
723 | ||
034c6ed7 | 724 | static int socket_open_fds(Socket *s) { |
83c60c9f LP |
725 | SocketPort *p; |
726 | int r; | |
56cf987f | 727 | char *label = NULL; |
83c60c9f LP |
728 | |
729 | assert(s); | |
730 | ||
b15bdda8 LP |
731 | if ((r = socket_instantiate_service(s)) < 0) |
732 | return r; | |
733 | ||
56cf987f DW |
734 | if ((r = label_get_socket_label_from_exe(s->service->exec_command[SERVICE_EXEC_START]->path, &label)) < 0) |
735 | return r; | |
b15bdda8 | 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 | ||
b5a0699f LP |
744 | if ((r = socket_address_listen( |
745 | &p->address, | |
746 | s->backlog, | |
747 | s->bind_ipv6_only, | |
748 | s->bind_to_device, | |
4fd5948e | 749 | s->free_bind, |
b5a0699f LP |
750 | s->directory_mode, |
751 | s->socket_mode, | |
56cf987f | 752 | label, |
b5a0699f | 753 | &p->fd)) < 0) |
83c60c9f LP |
754 | goto rollback; |
755 | ||
4fd5948e LP |
756 | socket_apply_socket_options(s, p->fd); |
757 | ||
b15bdda8 | 758 | } else if (p->type == SOCKET_FIFO) { |
83c60c9f | 759 | |
b15bdda8 LP |
760 | if ((r = fifo_address_create( |
761 | p->path, | |
762 | s->directory_mode, | |
763 | s->socket_mode, | |
56cf987f | 764 | label, |
b15bdda8 | 765 | &p->fd)) < 0) |
83c60c9f | 766 | goto rollback; |
83c60c9f | 767 | |
b15bdda8 | 768 | socket_apply_fifo_options(s, p->fd); |
83c60c9f | 769 | |
b15bdda8 LP |
770 | } else |
771 | assert_not_reached("Unknown port type"); | |
034c6ed7 LP |
772 | } |
773 | ||
56cf987f | 774 | label_free(label); |
034c6ed7 LP |
775 | return 0; |
776 | ||
777 | rollback: | |
778 | socket_close_fds(s); | |
56cf987f | 779 | label_free(label); |
034c6ed7 LP |
780 | return r; |
781 | } | |
782 | ||
783 | static void socket_unwatch_fds(Socket *s) { | |
784 | SocketPort *p; | |
9152c765 | 785 | |
034c6ed7 LP |
786 | assert(s); |
787 | ||
788 | LIST_FOREACH(port, p, s->ports) { | |
789 | if (p->fd < 0) | |
790 | continue; | |
791 | ||
acbb0225 | 792 | unit_unwatch_fd(UNIT(s), &p->fd_watch); |
83c60c9f | 793 | } |
034c6ed7 LP |
794 | } |
795 | ||
796 | static int socket_watch_fds(Socket *s) { | |
797 | SocketPort *p; | |
798 | int r; | |
799 | ||
800 | assert(s); | |
83c60c9f | 801 | |
034c6ed7 LP |
802 | LIST_FOREACH(port, p, s->ports) { |
803 | if (p->fd < 0) | |
804 | continue; | |
805 | ||
cabab516 | 806 | p->fd_watch.socket_accept = |
4f2d528d | 807 | s->accept && |
dd5ad9d4 | 808 | p->type == SOCKET_SOCKET && |
4f2d528d LP |
809 | socket_address_can_accept(&p->address); |
810 | ||
f94ea366 | 811 | if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0) |
034c6ed7 LP |
812 | goto fail; |
813 | } | |
83c60c9f | 814 | |
542563ba | 815 | return 0; |
83c60c9f | 816 | |
034c6ed7 LP |
817 | fail: |
818 | socket_unwatch_fds(s); | |
819 | return r; | |
820 | } | |
821 | ||
822 | static void socket_set_state(Socket *s, SocketState state) { | |
823 | SocketState old_state; | |
824 | assert(s); | |
825 | ||
826 | old_state = s->state; | |
827 | s->state = state; | |
828 | ||
829 | if (state != SOCKET_START_PRE && | |
830 | state != SOCKET_START_POST && | |
831 | state != SOCKET_STOP_PRE && | |
832 | state != SOCKET_STOP_PRE_SIGTERM && | |
833 | state != SOCKET_STOP_PRE_SIGKILL && | |
834 | state != SOCKET_STOP_POST && | |
80876c20 LP |
835 | state != SOCKET_FINAL_SIGTERM && |
836 | state != SOCKET_FINAL_SIGKILL) { | |
acbb0225 | 837 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
5e94833f | 838 | socket_unwatch_control_pid(s); |
034c6ed7 | 839 | s->control_command = NULL; |
a16e1123 | 840 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; |
e537352b | 841 | } |
034c6ed7 | 842 | |
a16e1123 LP |
843 | if (state != SOCKET_LISTENING) |
844 | socket_unwatch_fds(s); | |
845 | ||
034c6ed7 LP |
846 | if (state != SOCKET_START_POST && |
847 | state != SOCKET_LISTENING && | |
848 | state != SOCKET_RUNNING && | |
849 | state != SOCKET_STOP_PRE && | |
850 | state != SOCKET_STOP_PRE_SIGTERM && | |
851 | state != SOCKET_STOP_PRE_SIGKILL) | |
852 | socket_close_fds(s); | |
853 | ||
e537352b | 854 | if (state != old_state) |
40d50879 | 855 | log_debug("%s changed %s -> %s", |
a16e1123 LP |
856 | s->meta.id, |
857 | socket_state_to_string(old_state), | |
858 | socket_state_to_string(state)); | |
acbb0225 LP |
859 | |
860 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); | |
034c6ed7 LP |
861 | } |
862 | ||
a16e1123 LP |
863 | static int socket_coldplug(Unit *u) { |
864 | Socket *s = SOCKET(u); | |
865 | int r; | |
866 | ||
867 | assert(s); | |
868 | assert(s->state == SOCKET_DEAD); | |
869 | ||
870 | if (s->deserialized_state != s->state) { | |
871 | ||
872 | if (s->deserialized_state == SOCKET_START_PRE || | |
873 | s->deserialized_state == SOCKET_START_POST || | |
874 | s->deserialized_state == SOCKET_STOP_PRE || | |
875 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
876 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL || | |
877 | s->deserialized_state == SOCKET_STOP_POST || | |
878 | s->deserialized_state == SOCKET_FINAL_SIGTERM || | |
879 | s->deserialized_state == SOCKET_FINAL_SIGKILL) { | |
880 | ||
881 | if (s->control_pid <= 0) | |
882 | return -EBADMSG; | |
883 | ||
884 | if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) | |
885 | return r; | |
886 | ||
887 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
888 | return r; | |
889 | } | |
890 | ||
891 | if (s->deserialized_state == SOCKET_START_POST || | |
892 | s->deserialized_state == SOCKET_LISTENING || | |
893 | s->deserialized_state == SOCKET_RUNNING || | |
894 | s->deserialized_state == SOCKET_STOP_PRE || | |
895 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
896 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) | |
897 | if ((r = socket_open_fds(s)) < 0) | |
898 | return r; | |
899 | ||
900 | if (s->deserialized_state == SOCKET_LISTENING) | |
901 | if ((r = socket_watch_fds(s)) < 0) | |
902 | return r; | |
903 | ||
904 | socket_set_state(s, s->deserialized_state); | |
905 | } | |
906 | ||
907 | return 0; | |
908 | } | |
909 | ||
e537352b | 910 | static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { |
034c6ed7 LP |
911 | pid_t pid; |
912 | int r; | |
9e2f7c11 | 913 | char **argv; |
034c6ed7 LP |
914 | |
915 | assert(s); | |
916 | assert(c); | |
917 | assert(_pid); | |
918 | ||
e537352b LP |
919 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
920 | goto fail; | |
034c6ed7 | 921 | |
9e2f7c11 LP |
922 | if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) { |
923 | r = -ENOMEM; | |
924 | goto fail; | |
925 | } | |
926 | ||
927 | r = exec_spawn(c, | |
928 | argv, | |
929 | &s->exec_context, | |
930 | NULL, 0, | |
1137a57c | 931 | s->meta.manager->environment, |
9e2f7c11 LP |
932 | true, |
933 | true, | |
1e3ad081 | 934 | true, |
4cd1fbcc LP |
935 | s->meta.manager->confirm_spawn, |
936 | s->meta.cgroup_bondings, | |
9e2f7c11 LP |
937 | &pid); |
938 | ||
939 | strv_free(argv); | |
940 | if (r < 0) | |
034c6ed7 LP |
941 | goto fail; |
942 | ||
87f0e418 | 943 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
944 | /* FIXME: we need to do something here */ |
945 | goto fail; | |
83c60c9f | 946 | |
034c6ed7 LP |
947 | *_pid = pid; |
948 | ||
949 | return 0; | |
950 | ||
951 | fail: | |
e537352b | 952 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
83c60c9f LP |
953 | |
954 | return r; | |
542563ba LP |
955 | } |
956 | ||
034c6ed7 LP |
957 | static void socket_enter_dead(Socket *s, bool success) { |
958 | assert(s); | |
959 | ||
960 | if (!success) | |
961 | s->failure = true; | |
962 | ||
18c78fb1 | 963 | socket_set_state(s, s->failure ? SOCKET_MAINTENANCE : SOCKET_DEAD); |
034c6ed7 LP |
964 | } |
965 | ||
80876c20 LP |
966 | static void socket_enter_signal(Socket *s, SocketState state, bool success); |
967 | ||
034c6ed7 LP |
968 | static void socket_enter_stop_post(Socket *s, bool success) { |
969 | int r; | |
970 | assert(s); | |
971 | ||
972 | if (!success) | |
973 | s->failure = true; | |
974 | ||
5e94833f LP |
975 | socket_unwatch_control_pid(s); |
976 | ||
a16e1123 LP |
977 | s->control_command_id = SOCKET_EXEC_STOP_POST; |
978 | ||
80876c20 | 979 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) { |
e537352b | 980 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
981 | goto fail; |
982 | ||
80876c20 LP |
983 | socket_set_state(s, SOCKET_STOP_POST); |
984 | } else | |
985 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, true); | |
034c6ed7 LP |
986 | |
987 | return; | |
988 | ||
989 | fail: | |
6cf6bbc2 | 990 | log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r)); |
80876c20 | 991 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
992 | } |
993 | ||
994 | static void socket_enter_signal(Socket *s, SocketState state, bool success) { | |
995 | int r; | |
80876c20 | 996 | bool sent = false; |
034c6ed7 LP |
997 | |
998 | assert(s); | |
999 | ||
1000 | if (!success) | |
1001 | s->failure = true; | |
1002 | ||
2e22afe9 LP |
1003 | if (s->exec_context.kill_mode != KILL_NONE) { |
1004 | int sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL; | |
034c6ed7 | 1005 | |
2e22afe9 | 1006 | if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) { |
50159e6a | 1007 | |
4cd1fbcc | 1008 | if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) { |
50159e6a LP |
1009 | if (r != -EAGAIN && r != -ESRCH) |
1010 | goto fail; | |
1011 | } else | |
1012 | sent = true; | |
034c6ed7 | 1013 | } |
50159e6a | 1014 | |
80876c20 | 1015 | if (!sent && s->control_pid > 0) |
2e22afe9 | 1016 | if (kill(s->exec_context.kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH) { |
50159e6a LP |
1017 | r = -errno; |
1018 | goto fail; | |
1019 | } | |
d6ea93e3 | 1020 | } |
034c6ed7 | 1021 | |
e93bc5a6 | 1022 | if (sent && s->control_pid > 0) { |
80876c20 LP |
1023 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
1024 | goto fail; | |
d6ea93e3 | 1025 | |
80876c20 LP |
1026 | socket_set_state(s, state); |
1027 | } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
1028 | socket_enter_stop_post(s, true); | |
1029 | else | |
034c6ed7 LP |
1030 | socket_enter_dead(s, true); |
1031 | ||
1032 | return; | |
1033 | ||
1034 | fail: | |
9e2f7c11 | 1035 | log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1036 | |
1037 | if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
1038 | socket_enter_stop_post(s, false); | |
1039 | else | |
1040 | socket_enter_dead(s, false); | |
1041 | } | |
1042 | ||
1043 | static void socket_enter_stop_pre(Socket *s, bool success) { | |
1044 | int r; | |
1045 | assert(s); | |
1046 | ||
1047 | if (!success) | |
1048 | s->failure = true; | |
1049 | ||
5e94833f LP |
1050 | socket_unwatch_control_pid(s); |
1051 | ||
a16e1123 LP |
1052 | s->control_command_id = SOCKET_EXEC_STOP_PRE; |
1053 | ||
80876c20 | 1054 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) { |
e537352b | 1055 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1056 | goto fail; |
1057 | ||
80876c20 LP |
1058 | socket_set_state(s, SOCKET_STOP_PRE); |
1059 | } else | |
034c6ed7 LP |
1060 | socket_enter_stop_post(s, true); |
1061 | ||
1062 | return; | |
1063 | ||
1064 | fail: | |
6cf6bbc2 | 1065 | log_warning("%s failed to run 'stop-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1066 | socket_enter_stop_post(s, false); |
1067 | } | |
1068 | ||
e9af15c3 LP |
1069 | static void socket_enter_listening(Socket *s) { |
1070 | int r; | |
1071 | assert(s); | |
1072 | ||
1073 | if ((r = socket_watch_fds(s)) < 0) { | |
9e2f7c11 | 1074 | log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r)); |
e9af15c3 LP |
1075 | goto fail; |
1076 | } | |
1077 | ||
1078 | socket_set_state(s, SOCKET_LISTENING); | |
1079 | return; | |
1080 | ||
1081 | fail: | |
1082 | socket_enter_stop_pre(s, false); | |
1083 | } | |
1084 | ||
034c6ed7 LP |
1085 | static void socket_enter_start_post(Socket *s) { |
1086 | int r; | |
1087 | assert(s); | |
1088 | ||
e9af15c3 | 1089 | if ((r = socket_open_fds(s)) < 0) { |
9e2f7c11 | 1090 | log_warning("%s failed to listen on sockets: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1091 | goto fail; |
1092 | } | |
1093 | ||
5e94833f LP |
1094 | socket_unwatch_control_pid(s); |
1095 | ||
a16e1123 LP |
1096 | s->control_command_id = SOCKET_EXEC_START_POST; |
1097 | ||
80876c20 | 1098 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { |
e537352b | 1099 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) { |
6cf6bbc2 | 1100 | log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1101 | goto fail; |
1102 | } | |
1103 | ||
80876c20 LP |
1104 | socket_set_state(s, SOCKET_START_POST); |
1105 | } else | |
e9af15c3 | 1106 | socket_enter_listening(s); |
034c6ed7 LP |
1107 | |
1108 | return; | |
1109 | ||
1110 | fail: | |
1111 | socket_enter_stop_pre(s, false); | |
1112 | } | |
1113 | ||
1114 | static void socket_enter_start_pre(Socket *s) { | |
1115 | int r; | |
1116 | assert(s); | |
1117 | ||
5e94833f LP |
1118 | socket_unwatch_control_pid(s); |
1119 | ||
a16e1123 LP |
1120 | s->control_command_id = SOCKET_EXEC_START_PRE; |
1121 | ||
80876c20 | 1122 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { |
e537352b | 1123 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1124 | goto fail; |
1125 | ||
80876c20 LP |
1126 | socket_set_state(s, SOCKET_START_PRE); |
1127 | } else | |
034c6ed7 LP |
1128 | socket_enter_start_post(s); |
1129 | ||
1130 | return; | |
1131 | ||
1132 | fail: | |
6cf6bbc2 | 1133 | log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1134 | socket_enter_dead(s, false); |
1135 | } | |
1136 | ||
4f2d528d | 1137 | static void socket_enter_running(Socket *s, int cfd) { |
034c6ed7 | 1138 | int r; |
398ef8ba | 1139 | DBusError error; |
034c6ed7 LP |
1140 | |
1141 | assert(s); | |
398ef8ba | 1142 | dbus_error_init(&error); |
034c6ed7 | 1143 | |
ba3e67a7 LP |
1144 | /* We don't take connections anymore if we are supposed to |
1145 | * shut down anyway */ | |
1146 | if (s->meta.job && s->meta.job->type == JOB_STOP) { | |
7c610628 LP |
1147 | if (cfd >= 0) |
1148 | close_nointr_nofail(cfd); | |
1149 | else { | |
1150 | /* Flush all sockets by closing and reopening them */ | |
1151 | socket_close_fds(s); | |
1152 | ||
1153 | if ((r = socket_watch_fds(s)) < 0) { | |
1154 | log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r)); | |
1155 | socket_enter_stop_pre(s, false); | |
1156 | } | |
1157 | } | |
1158 | ||
ba3e67a7 LP |
1159 | return; |
1160 | } | |
1161 | ||
4f2d528d | 1162 | if (cfd < 0) { |
398ef8ba | 1163 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, &error, NULL)) < 0) |
4f2d528d LP |
1164 | goto fail; |
1165 | ||
1166 | socket_set_state(s, SOCKET_RUNNING); | |
1167 | } else { | |
b4f10a5e | 1168 | char *prefix, *instance = NULL, *name; |
b15bdda8 | 1169 | Service *service; |
4f2d528d | 1170 | |
6cf6bbc2 LP |
1171 | if (s->n_connections >= s->max_connections) { |
1172 | log_warning("Too many incoming connections (%u)", s->n_connections); | |
1173 | close_nointr_nofail(cfd); | |
1174 | return; | |
1175 | } | |
1176 | ||
b15bdda8 LP |
1177 | if ((r = socket_instantiate_service(s)) < 0) |
1178 | goto fail; | |
1179 | ||
1180 | if ((r = instance_from_socket(cfd, s->n_accepted, &instance)) < 0) | |
4f2d528d LP |
1181 | goto fail; |
1182 | ||
4cd1fbcc | 1183 | if (!(prefix = unit_name_to_prefix(s->meta.id))) { |
4f2d528d LP |
1184 | free(instance); |
1185 | r = -ENOMEM; | |
1186 | goto fail; | |
1187 | } | |
1188 | ||
1189 | name = unit_name_build(prefix, instance, ".service"); | |
1190 | free(prefix); | |
1191 | free(instance); | |
1192 | ||
b6dbbe1c | 1193 | if (!name) { |
4f2d528d | 1194 | r = -ENOMEM; |
b6dbbe1c LP |
1195 | goto fail; |
1196 | } | |
4f2d528d | 1197 | |
b15bdda8 LP |
1198 | if ((r = unit_add_name(UNIT(s->service), name)) < 0) { |
1199 | free(name); | |
4f2d528d | 1200 | goto fail; |
b15bdda8 LP |
1201 | } |
1202 | ||
1203 | service = s->service; | |
1204 | s->service = NULL; | |
1205 | s->n_accepted ++; | |
4f2d528d | 1206 | |
6c073082 LP |
1207 | service->meta.no_gc = false; |
1208 | ||
b15bdda8 LP |
1209 | unit_choose_id(UNIT(service), name); |
1210 | free(name); | |
1211 | ||
1212 | if ((r = service_set_socket_fd(service, cfd, s)) < 0) | |
4f2d528d LP |
1213 | goto fail; |
1214 | ||
1215 | cfd = -1; | |
6cf6bbc2 LP |
1216 | s->n_connections ++; |
1217 | ||
b15bdda8 | 1218 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL)) < 0) |
4f2d528d LP |
1219 | goto fail; |
1220 | } | |
034c6ed7 | 1221 | |
034c6ed7 LP |
1222 | return; |
1223 | ||
1224 | fail: | |
398ef8ba | 1225 | log_warning("%s failed to queue socket startup job: %s", s->meta.id, bus_error(&error, r)); |
80876c20 | 1226 | socket_enter_stop_pre(s, false); |
4f2d528d LP |
1227 | |
1228 | if (cfd >= 0) | |
1229 | close_nointr_nofail(cfd); | |
398ef8ba LP |
1230 | |
1231 | dbus_error_free(&error); | |
034c6ed7 LP |
1232 | } |
1233 | ||
1234 | static void socket_run_next(Socket *s, bool success) { | |
1235 | int r; | |
1236 | ||
1237 | assert(s); | |
1238 | assert(s->control_command); | |
1239 | assert(s->control_command->command_next); | |
1240 | ||
1241 | if (!success) | |
1242 | s->failure = true; | |
1243 | ||
5e94833f LP |
1244 | socket_unwatch_control_pid(s); |
1245 | ||
034c6ed7 LP |
1246 | s->control_command = s->control_command->command_next; |
1247 | ||
e537352b | 1248 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1249 | goto fail; |
1250 | ||
1251 | return; | |
1252 | ||
1253 | fail: | |
6cf6bbc2 | 1254 | log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r)); |
80876c20 LP |
1255 | |
1256 | if (s->state == SOCKET_START_POST) | |
1257 | socket_enter_stop_pre(s, false); | |
034c6ed7 LP |
1258 | else if (s->state == SOCKET_STOP_POST) |
1259 | socket_enter_dead(s, false); | |
1260 | else | |
80876c20 | 1261 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1262 | } |
1263 | ||
87f0e418 LP |
1264 | static int socket_start(Unit *u) { |
1265 | Socket *s = SOCKET(u); | |
83c60c9f LP |
1266 | |
1267 | assert(s); | |
1268 | ||
034c6ed7 LP |
1269 | /* We cannot fulfill this request right now, try again later |
1270 | * please! */ | |
1271 | if (s->state == SOCKET_STOP_PRE || | |
1272 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1273 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1274 | s->state == SOCKET_STOP_POST || | |
80876c20 LP |
1275 | s->state == SOCKET_FINAL_SIGTERM || |
1276 | s->state == SOCKET_FINAL_SIGKILL) | |
034c6ed7 LP |
1277 | return -EAGAIN; |
1278 | ||
83c60c9f LP |
1279 | if (s->state == SOCKET_START_PRE || |
1280 | s->state == SOCKET_START_POST) | |
034c6ed7 | 1281 | return 0; |
83c60c9f | 1282 | |
034c6ed7 | 1283 | /* Cannot run this without the service being around */ |
4f2d528d LP |
1284 | if (s->service) { |
1285 | if (s->service->meta.load_state != UNIT_LOADED) | |
1286 | return -ENOENT; | |
1287 | ||
1288 | /* If the service is alredy actvie we cannot start the | |
1289 | * socket */ | |
1290 | if (s->service->state != SERVICE_DEAD && | |
18c78fb1 | 1291 | s->service->state != SERVICE_MAINTENANCE && |
4f2d528d LP |
1292 | s->service->state != SERVICE_AUTO_RESTART) |
1293 | return -EBUSY; | |
1294 | } | |
e537352b | 1295 | |
18c78fb1 | 1296 | assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTENANCE); |
83c60c9f | 1297 | |
034c6ed7 LP |
1298 | s->failure = false; |
1299 | socket_enter_start_pre(s); | |
1300 | return 0; | |
1301 | } | |
83c60c9f | 1302 | |
87f0e418 LP |
1303 | static int socket_stop(Unit *u) { |
1304 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
1305 | |
1306 | assert(s); | |
1307 | ||
e537352b LP |
1308 | /* Already on it */ |
1309 | if (s->state == SOCKET_STOP_PRE || | |
1310 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1311 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1312 | s->state == SOCKET_STOP_POST || | |
80876c20 | 1313 | s->state == SOCKET_FINAL_SIGTERM || |
3f6c78dc | 1314 | s->state == SOCKET_FINAL_SIGKILL) |
e537352b LP |
1315 | return 0; |
1316 | ||
3f6c78dc LP |
1317 | /* If there's already something running we go directly into |
1318 | * kill mode. */ | |
1319 | if (s->state == SOCKET_START_PRE || | |
1320 | s->state == SOCKET_START_POST) { | |
1321 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, true); | |
1322 | return -EAGAIN; | |
1323 | } | |
1324 | ||
034c6ed7 | 1325 | assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING); |
83c60c9f | 1326 | |
034c6ed7 | 1327 | socket_enter_stop_pre(s, true); |
542563ba LP |
1328 | return 0; |
1329 | } | |
1330 | ||
a16e1123 LP |
1331 | static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { |
1332 | Socket *s = SOCKET(u); | |
1333 | SocketPort *p; | |
1334 | int r; | |
1335 | ||
1336 | assert(u); | |
1337 | assert(f); | |
1338 | assert(fds); | |
1339 | ||
1340 | unit_serialize_item(u, f, "state", socket_state_to_string(s->state)); | |
1341 | unit_serialize_item(u, f, "failure", yes_no(s->failure)); | |
1342 | unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted); | |
1343 | ||
1344 | if (s->control_pid > 0) | |
5925dd3c | 1345 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); |
a16e1123 LP |
1346 | |
1347 | if (s->control_command_id >= 0) | |
1348 | unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id)); | |
1349 | ||
1350 | LIST_FOREACH(port, p, s->ports) { | |
1351 | int copy; | |
1352 | ||
1353 | if (p->fd < 0) | |
1354 | continue; | |
1355 | ||
1356 | if ((copy = fdset_put_dup(fds, p->fd)) < 0) | |
1357 | return copy; | |
1358 | ||
1359 | if (p->type == SOCKET_SOCKET) { | |
1360 | char *t; | |
1361 | ||
1362 | if ((r = socket_address_print(&p->address, &t)) < 0) | |
1363 | return r; | |
1364 | ||
27ca8d7a | 1365 | unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t); |
a16e1123 LP |
1366 | free(t); |
1367 | } else { | |
1368 | assert(p->type == SOCKET_FIFO); | |
1369 | unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path); | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | return 0; | |
1374 | } | |
1375 | ||
1376 | static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1377 | Socket *s = SOCKET(u); | |
a16e1123 LP |
1378 | |
1379 | assert(u); | |
1380 | assert(key); | |
1381 | assert(value); | |
1382 | assert(fds); | |
1383 | ||
1384 | if (streq(key, "state")) { | |
1385 | SocketState state; | |
1386 | ||
1387 | if ((state = socket_state_from_string(value)) < 0) | |
1388 | log_debug("Failed to parse state value %s", value); | |
1389 | else | |
1390 | s->deserialized_state = state; | |
1391 | } else if (streq(key, "failure")) { | |
1392 | int b; | |
1393 | ||
1394 | if ((b = parse_boolean(value)) < 0) | |
1395 | log_debug("Failed to parse failure value %s", value); | |
1396 | else | |
1397 | s->failure = b || s->failure; | |
1398 | ||
1399 | } else if (streq(key, "n-accepted")) { | |
1400 | unsigned k; | |
1401 | ||
e364ad06 | 1402 | if (safe_atou(value, &k) < 0) |
a16e1123 LP |
1403 | log_debug("Failed to parse n-accepted value %s", value); |
1404 | else | |
1405 | s->n_accepted += k; | |
1406 | } else if (streq(key, "control-pid")) { | |
5925dd3c | 1407 | pid_t pid; |
a16e1123 | 1408 | |
e364ad06 | 1409 | if (parse_pid(value, &pid) < 0) |
a16e1123 LP |
1410 | log_debug("Failed to parse control-pid value %s", value); |
1411 | else | |
5925dd3c | 1412 | s->control_pid = pid; |
a16e1123 LP |
1413 | } else if (streq(key, "control-command")) { |
1414 | SocketExecCommand id; | |
1415 | ||
1416 | if ((id = socket_exec_command_from_string(value)) < 0) | |
1417 | log_debug("Failed to parse exec-command value %s", value); | |
1418 | else { | |
1419 | s->control_command_id = id; | |
1420 | s->control_command = s->exec_command[id]; | |
1421 | } | |
1422 | } else if (streq(key, "fifo")) { | |
1423 | int fd, skip = 0; | |
1424 | SocketPort *p; | |
1425 | ||
1426 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
1427 | log_debug("Failed to parse fifo value %s", value); | |
1428 | else { | |
1429 | ||
1430 | LIST_FOREACH(port, p, s->ports) | |
1431 | if (streq(p->path, value+skip)) | |
1432 | break; | |
1433 | ||
1434 | if (p) { | |
1435 | if (p->fd >= 0) | |
1436 | close_nointr_nofail(p->fd); | |
1437 | p->fd = fdset_remove(fds, fd); | |
1438 | } | |
1439 | } | |
1440 | ||
1441 | } else if (streq(key, "socket")) { | |
27ca8d7a | 1442 | int fd, type, skip = 0; |
a16e1123 LP |
1443 | SocketPort *p; |
1444 | ||
27ca8d7a | 1445 | if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd)) |
a16e1123 LP |
1446 | log_debug("Failed to parse socket value %s", value); |
1447 | else { | |
1448 | ||
1449 | LIST_FOREACH(port, p, s->ports) | |
27ca8d7a | 1450 | if (socket_address_is(&p->address, value+skip, type)) |
a16e1123 LP |
1451 | break; |
1452 | ||
1453 | if (p) { | |
1454 | if (p->fd >= 0) | |
1455 | close_nointr_nofail(p->fd); | |
1456 | p->fd = fdset_remove(fds, fd); | |
1457 | } | |
1458 | } | |
1459 | ||
1460 | } else | |
1461 | log_debug("Unknown serialization key '%s'", key); | |
1462 | ||
1463 | return 0; | |
1464 | } | |
1465 | ||
87f0e418 LP |
1466 | static UnitActiveState socket_active_state(Unit *u) { |
1467 | assert(u); | |
5cb5a6ff | 1468 | |
acbb0225 | 1469 | return state_translation_table[SOCKET(u)->state]; |
5cb5a6ff LP |
1470 | } |
1471 | ||
10a94420 LP |
1472 | static const char *socket_sub_state_to_string(Unit *u) { |
1473 | assert(u); | |
1474 | ||
a16e1123 | 1475 | return socket_state_to_string(SOCKET(u)->state); |
10a94420 LP |
1476 | } |
1477 | ||
6cf6bbc2 LP |
1478 | static bool socket_check_gc(Unit *u) { |
1479 | Socket *s = SOCKET(u); | |
1480 | ||
1481 | assert(u); | |
1482 | ||
1483 | return s->n_connections > 0; | |
1484 | } | |
1485 | ||
acbb0225 | 1486 | static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 | 1487 | Socket *s = SOCKET(u); |
4f2d528d | 1488 | int cfd = -1; |
9152c765 | 1489 | |
034c6ed7 | 1490 | assert(s); |
8d567588 | 1491 | assert(fd >= 0); |
9152c765 | 1492 | |
871d7de4 LP |
1493 | if (s->state != SOCKET_LISTENING) |
1494 | return; | |
1495 | ||
9e2f7c11 | 1496 | log_debug("Incoming traffic on %s", u->meta.id); |
9152c765 | 1497 | |
4f2d528d LP |
1498 | if (events != EPOLLIN) { |
1499 | log_error("Got invalid poll event on socket."); | |
8d567588 | 1500 | goto fail; |
4f2d528d LP |
1501 | } |
1502 | ||
cabab516 | 1503 | if (w->socket_accept) { |
4f2d528d LP |
1504 | for (;;) { |
1505 | ||
1506 | if ((cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK)) < 0) { | |
1507 | ||
1508 | if (errno == EINTR) | |
1509 | continue; | |
1510 | ||
1511 | log_error("Failed to accept socket: %m"); | |
8d567588 | 1512 | goto fail; |
4f2d528d LP |
1513 | } |
1514 | ||
1515 | break; | |
1516 | } | |
4fd5948e LP |
1517 | |
1518 | socket_apply_socket_options(s, cfd); | |
4f2d528d | 1519 | } |
9152c765 | 1520 | |
4f2d528d | 1521 | socket_enter_running(s, cfd); |
8d567588 LP |
1522 | return; |
1523 | ||
1524 | fail: | |
1525 | socket_enter_stop_pre(s, false); | |
9152c765 LP |
1526 | } |
1527 | ||
87f0e418 LP |
1528 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
1529 | Socket *s = SOCKET(u); | |
034c6ed7 | 1530 | bool success; |
5cb5a6ff LP |
1531 | |
1532 | assert(s); | |
034c6ed7 | 1533 | assert(pid >= 0); |
5cb5a6ff | 1534 | |
8c47c732 LP |
1535 | if (pid != s->control_pid) |
1536 | return; | |
542563ba | 1537 | |
034c6ed7 LP |
1538 | s->control_pid = 0; |
1539 | ||
8c47c732 | 1540 | success = is_clean_exit(code, status); |
8c47c732 | 1541 | |
b708e7ce | 1542 | if (s->control_command) { |
b58b4116 | 1543 | exec_status_exit(&s->control_command->exec_status, pid, code, status); |
a16e1123 | 1544 | |
b708e7ce LP |
1545 | if (s->control_command->ignore) |
1546 | success = true; | |
1547 | } | |
1548 | ||
92abbefb LP |
1549 | log_full(success ? LOG_DEBUG : LOG_NOTICE, |
1550 | "%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
b708e7ce | 1551 | s->failure = s->failure || !success; |
034c6ed7 | 1552 | |
a16e1123 LP |
1553 | if (s->control_command && s->control_command->command_next && success) { |
1554 | log_debug("%s running next command for state %s", u->meta.id, socket_state_to_string(s->state)); | |
034c6ed7 | 1555 | socket_run_next(s, success); |
acbb0225 | 1556 | } else { |
a16e1123 LP |
1557 | s->control_command = NULL; |
1558 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
1559 | ||
034c6ed7 LP |
1560 | /* No further commands for this step, so let's figure |
1561 | * out what to do next */ | |
5cb5a6ff | 1562 | |
a16e1123 | 1563 | log_debug("%s got final SIGCHLD for state %s", u->meta.id, socket_state_to_string(s->state)); |
acbb0225 | 1564 | |
034c6ed7 LP |
1565 | switch (s->state) { |
1566 | ||
1567 | case SOCKET_START_PRE: | |
1568 | if (success) | |
acbb0225 | 1569 | socket_enter_start_post(s); |
034c6ed7 | 1570 | else |
80876c20 | 1571 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1572 | break; |
1573 | ||
1574 | case SOCKET_START_POST: | |
1575 | if (success) | |
e9af15c3 | 1576 | socket_enter_listening(s); |
034c6ed7 LP |
1577 | else |
1578 | socket_enter_stop_pre(s, false); | |
1579 | break; | |
1580 | ||
1581 | case SOCKET_STOP_PRE: | |
1582 | case SOCKET_STOP_PRE_SIGTERM: | |
1583 | case SOCKET_STOP_PRE_SIGKILL: | |
1584 | socket_enter_stop_post(s, success); | |
1585 | break; | |
1586 | ||
1587 | case SOCKET_STOP_POST: | |
80876c20 LP |
1588 | case SOCKET_FINAL_SIGTERM: |
1589 | case SOCKET_FINAL_SIGKILL: | |
034c6ed7 LP |
1590 | socket_enter_dead(s, success); |
1591 | break; | |
1592 | ||
1593 | default: | |
1594 | assert_not_reached("Uh, control process died at wrong time."); | |
1595 | } | |
1596 | } | |
1597 | } | |
5cb5a6ff | 1598 | |
acbb0225 | 1599 | static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) { |
87f0e418 | 1600 | Socket *s = SOCKET(u); |
5cb5a6ff | 1601 | |
034c6ed7 LP |
1602 | assert(s); |
1603 | assert(elapsed == 1); | |
acbb0225 | 1604 | assert(w == &s->timer_watch); |
034c6ed7 LP |
1605 | |
1606 | switch (s->state) { | |
1607 | ||
1608 | case SOCKET_START_PRE: | |
9e2f7c11 | 1609 | log_warning("%s starting timed out. Terminating.", u->meta.id); |
80876c20 LP |
1610 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
1611 | ||
034c6ed7 | 1612 | case SOCKET_START_POST: |
9e2f7c11 | 1613 | log_warning("%s starting timed out. Stopping.", u->meta.id); |
034c6ed7 LP |
1614 | socket_enter_stop_pre(s, false); |
1615 | break; | |
1616 | ||
1617 | case SOCKET_STOP_PRE: | |
9e2f7c11 | 1618 | log_warning("%s stopping timed out. Terminating.", u->meta.id); |
034c6ed7 LP |
1619 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false); |
1620 | break; | |
1621 | ||
1622 | case SOCKET_STOP_PRE_SIGTERM: | |
9e2f7c11 | 1623 | log_warning("%s stopping timed out. Killing.", u->meta.id); |
034c6ed7 LP |
1624 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false); |
1625 | break; | |
1626 | ||
1627 | case SOCKET_STOP_PRE_SIGKILL: | |
9e2f7c11 | 1628 | log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id); |
034c6ed7 LP |
1629 | socket_enter_stop_post(s, false); |
1630 | break; | |
1631 | ||
1632 | case SOCKET_STOP_POST: | |
9e2f7c11 | 1633 | log_warning("%s stopping timed out (2). Terminating.", u->meta.id); |
80876c20 | 1634 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1635 | break; |
1636 | ||
80876c20 | 1637 | case SOCKET_FINAL_SIGTERM: |
9e2f7c11 | 1638 | log_warning("%s stopping timed out (2). Killing.", u->meta.id); |
80876c20 | 1639 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, false); |
034c6ed7 LP |
1640 | break; |
1641 | ||
80876c20 | 1642 | case SOCKET_FINAL_SIGKILL: |
18c78fb1 | 1643 | log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id); |
034c6ed7 LP |
1644 | socket_enter_dead(s, false); |
1645 | break; | |
1646 | ||
1647 | default: | |
1648 | assert_not_reached("Timeout at wrong time."); | |
1649 | } | |
5cb5a6ff LP |
1650 | } |
1651 | ||
44d8db9e LP |
1652 | int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { |
1653 | int *rfds; | |
1654 | unsigned rn_fds, k; | |
1655 | SocketPort *p; | |
1656 | ||
1657 | assert(s); | |
1658 | assert(fds); | |
1659 | assert(n_fds); | |
1660 | ||
1661 | /* Called from the service code for requesting our fds */ | |
1662 | ||
1663 | rn_fds = 0; | |
1664 | LIST_FOREACH(port, p, s->ports) | |
1665 | if (p->fd >= 0) | |
1666 | rn_fds++; | |
1667 | ||
e364ad06 | 1668 | if (!(rfds = new(int, rn_fds))) |
44d8db9e LP |
1669 | return -ENOMEM; |
1670 | ||
1671 | k = 0; | |
1672 | LIST_FOREACH(port, p, s->ports) | |
1673 | if (p->fd >= 0) | |
1674 | rfds[k++] = p->fd; | |
1675 | ||
1676 | assert(k == rn_fds); | |
1677 | ||
1678 | *fds = rfds; | |
1679 | *n_fds = rn_fds; | |
1680 | ||
1681 | return 0; | |
1682 | } | |
1683 | ||
ceee3d82 LP |
1684 | void socket_notify_service_dead(Socket *s) { |
1685 | assert(s); | |
1686 | ||
6cf6bbc2 LP |
1687 | /* The service is dead. Dang! |
1688 | * | |
1689 | * This is strictly for one-instance-for-all-connections | |
1690 | * services. */ | |
ceee3d82 LP |
1691 | |
1692 | if (s->state == SOCKET_RUNNING) { | |
9e2f7c11 | 1693 | log_debug("%s got notified about service death.", s->meta.id); |
ceee3d82 LP |
1694 | socket_enter_listening(s); |
1695 | } | |
1696 | } | |
1697 | ||
6cf6bbc2 LP |
1698 | void socket_connection_unref(Socket *s) { |
1699 | assert(s); | |
1700 | ||
1701 | /* The service is dead. Yay! | |
1702 | * | |
1703 | * This is strictly for one-onstance-per-connection | |
1704 | * services. */ | |
1705 | ||
1706 | assert(s->n_connections > 0); | |
1707 | s->n_connections--; | |
1708 | ||
1709 | log_debug("%s: One connection closed, %u left.", s->meta.id, s->n_connections); | |
1710 | } | |
1711 | ||
5632e374 LP |
1712 | static void socket_reset_maintenance(Unit *u) { |
1713 | Socket *s = SOCKET(u); | |
1714 | ||
1715 | assert(s); | |
1716 | ||
1717 | if (s->state == SOCKET_MAINTENANCE) | |
1718 | socket_set_state(s, SOCKET_DEAD); | |
1719 | ||
1720 | s->failure = false; | |
1721 | } | |
1722 | ||
a16e1123 LP |
1723 | static const char* const socket_state_table[_SOCKET_STATE_MAX] = { |
1724 | [SOCKET_DEAD] = "dead", | |
1725 | [SOCKET_START_PRE] = "start-pre", | |
1726 | [SOCKET_START_POST] = "start-post", | |
1727 | [SOCKET_LISTENING] = "listening", | |
1728 | [SOCKET_RUNNING] = "running", | |
1729 | [SOCKET_STOP_PRE] = "stop-pre", | |
1730 | [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm", | |
1731 | [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill", | |
1732 | [SOCKET_STOP_POST] = "stop-post", | |
1733 | [SOCKET_FINAL_SIGTERM] = "final-sigterm", | |
1734 | [SOCKET_FINAL_SIGKILL] = "final-sigkill", | |
18c78fb1 | 1735 | [SOCKET_MAINTENANCE] = "maintenance" |
a16e1123 LP |
1736 | }; |
1737 | ||
1738 | DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState); | |
1739 | ||
1740 | static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { | |
1741 | [SOCKET_EXEC_START_PRE] = "StartPre", | |
1742 | [SOCKET_EXEC_START_POST] = "StartPost", | |
1743 | [SOCKET_EXEC_STOP_PRE] = "StopPre", | |
1744 | [SOCKET_EXEC_STOP_POST] = "StopPost" | |
1745 | }; | |
1746 | ||
1747 | DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); | |
1748 | ||
87f0e418 | 1749 | const UnitVTable socket_vtable = { |
5cb5a6ff LP |
1750 | .suffix = ".socket", |
1751 | ||
034c6ed7 LP |
1752 | .init = socket_init, |
1753 | .done = socket_done, | |
a16e1123 LP |
1754 | .load = socket_load, |
1755 | ||
1756 | .coldplug = socket_coldplug, | |
034c6ed7 | 1757 | |
5cb5a6ff LP |
1758 | .dump = socket_dump, |
1759 | ||
542563ba LP |
1760 | .start = socket_start, |
1761 | .stop = socket_stop, | |
5cb5a6ff | 1762 | |
a16e1123 LP |
1763 | .serialize = socket_serialize, |
1764 | .deserialize_item = socket_deserialize_item, | |
1765 | ||
5cb5a6ff | 1766 | .active_state = socket_active_state, |
10a94420 | 1767 | .sub_state_to_string = socket_sub_state_to_string, |
5cb5a6ff | 1768 | |
6cf6bbc2 LP |
1769 | .check_gc = socket_check_gc, |
1770 | ||
9152c765 | 1771 | .fd_event = socket_fd_event, |
034c6ed7 | 1772 | .sigchld_event = socket_sigchld_event, |
4139c1b2 LP |
1773 | .timer_event = socket_timer_event, |
1774 | ||
5632e374 LP |
1775 | .reset_maintenance = socket_reset_maintenance, |
1776 | ||
4139c1b2 | 1777 | .bus_message_handler = bus_socket_message_handler |
5cb5a6ff | 1778 | }; |