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