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