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