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