]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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> |
83c60c9f | 29 | |
87f0e418 | 30 | #include "unit.h" |
5cb5a6ff | 31 | #include "socket.h" |
83c60c9f | 32 | #include "log.h" |
23a177ef LP |
33 | #include "load-dropin.h" |
34 | #include "load-fragment.h" | |
83c60c9f | 35 | |
acbb0225 | 36 | static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { |
87f0e418 LP |
37 | [SOCKET_DEAD] = UNIT_INACTIVE, |
38 | [SOCKET_START_PRE] = UNIT_ACTIVATING, | |
39 | [SOCKET_START_POST] = UNIT_ACTIVATING, | |
40 | [SOCKET_LISTENING] = UNIT_ACTIVE, | |
41 | [SOCKET_RUNNING] = UNIT_ACTIVE, | |
42 | [SOCKET_STOP_PRE] = UNIT_DEACTIVATING, | |
43 | [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING, | |
44 | [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING, | |
45 | [SOCKET_STOP_POST] = UNIT_DEACTIVATING, | |
46 | [SOCKET_STOP_POST_SIGTERM] = UNIT_DEACTIVATING, | |
47 | [SOCKET_STOP_POST_SIGKILL] = UNIT_DEACTIVATING, | |
48 | [SOCKET_MAINTAINANCE] = UNIT_INACTIVE, | |
83c60c9f | 49 | }; |
5cb5a6ff | 50 | |
acbb0225 LP |
51 | static const char* const state_string_table[_SOCKET_STATE_MAX] = { |
52 | [SOCKET_DEAD] = "dead", | |
53 | [SOCKET_START_PRE] = "start-pre", | |
54 | [SOCKET_START_POST] = "start-post", | |
55 | [SOCKET_LISTENING] = "listening", | |
56 | [SOCKET_RUNNING] = "running", | |
57 | [SOCKET_STOP_PRE] = "stop-pre", | |
58 | [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm", | |
59 | [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill", | |
60 | [SOCKET_STOP_POST] = "stop-post", | |
61 | [SOCKET_STOP_POST_SIGTERM] = "stop-post-sigterm", | |
62 | [SOCKET_STOP_POST_SIGKILL] = "stop-post-sigkill", | |
63 | [SOCKET_MAINTAINANCE] = "maintainance" | |
64 | }; | |
65 | ||
87f0e418 LP |
66 | static void socket_done(Unit *u) { |
67 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
68 | SocketPort *p; |
69 | ||
70 | assert(s); | |
71 | ||
72 | while ((p = s->ports)) { | |
73 | LIST_REMOVE(SocketPort, port, s->ports, p); | |
74 | ||
75 | if (p->fd >= 0) | |
76 | close_nointr(p->fd); | |
77 | free(p->path); | |
78 | free(p); | |
79 | } | |
80 | ||
81 | exec_context_done(&s->exec_context); | |
e537352b | 82 | exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); |
034c6ed7 LP |
83 | s->control_command = NULL; |
84 | ||
85 | if (s->control_pid > 0) { | |
87f0e418 | 86 | unit_unwatch_pid(u, s->control_pid); |
034c6ed7 LP |
87 | s->control_pid = 0; |
88 | } | |
89 | ||
90 | s->service = NULL; | |
91 | ||
acbb0225 | 92 | free(s->bind_to_device); |
e537352b | 93 | s->bind_to_device = NULL; |
acbb0225 LP |
94 | |
95 | unit_unwatch_timer(u, &s->timer_watch); | |
5cb5a6ff LP |
96 | } |
97 | ||
e537352b | 98 | static void socket_init(Unit *u) { |
87f0e418 | 99 | Socket *s = SOCKET(u); |
44d8db9e | 100 | |
e537352b LP |
101 | assert(u); |
102 | assert(u->meta.load_state == UNIT_STUB); | |
44d8db9e LP |
103 | |
104 | s->state = 0; | |
acbb0225 | 105 | s->timer_watch.type = WATCH_INVALID; |
44d8db9e LP |
106 | s->bind_ipv6_only = false; |
107 | s->backlog = SOMAXCONN; | |
108 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
b5a0699f LP |
109 | s->directory_mode = 0755; |
110 | s->socket_mode = 0666; | |
50159e6a LP |
111 | s->kill_mode = 0; |
112 | s->failure = false; | |
113 | s->control_pid = 0; | |
e537352b | 114 | s->service = NULL; |
44d8db9e | 115 | exec_context_init(&s->exec_context); |
e537352b | 116 | } |
44d8db9e | 117 | |
e537352b LP |
118 | static int socket_load(Unit *u) { |
119 | Socket *s = SOCKET(u); | |
120 | int r; | |
44d8db9e | 121 | |
e537352b LP |
122 | assert(u); |
123 | assert(u->meta.load_state == UNIT_STUB); | |
44d8db9e | 124 | |
e537352b | 125 | if ((r = unit_load_fragment_and_dropin(u)) < 0) |
23a177ef | 126 | return r; |
44d8db9e | 127 | |
23a177ef | 128 | /* This is a new unit? Then let's add in some extras */ |
e537352b | 129 | if (u->meta.load_state == UNIT_LOADED) { |
44d8db9e | 130 | |
e537352b | 131 | if ((r = unit_load_related_unit(u, ".service", (Unit**) &s->service))) |
23a177ef LP |
132 | return r; |
133 | ||
134 | if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0) | |
135 | return r; | |
44d8db9e | 136 | |
23a177ef LP |
137 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) |
138 | return r; | |
139 | ||
140 | if ((r = unit_add_default_cgroup(u)) < 0) | |
141 | return r; | |
142 | } | |
143 | ||
144 | return 0; | |
44d8db9e LP |
145 | } |
146 | ||
542563ba LP |
147 | static const char* listen_lookup(int type) { |
148 | ||
149 | if (type == SOCK_STREAM) | |
150 | return "ListenStream"; | |
151 | else if (type == SOCK_DGRAM) | |
152 | return "ListenDatagram"; | |
153 | else if (type == SOCK_SEQPACKET) | |
154 | return "ListenSequentialPacket"; | |
155 | ||
034c6ed7 | 156 | assert_not_reached("Unknown socket type"); |
542563ba LP |
157 | return NULL; |
158 | } | |
159 | ||
87f0e418 | 160 | static void socket_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 161 | |
e537352b | 162 | static const char* const command_table[_SOCKET_EXEC_COMMAND_MAX] = { |
5cb5a6ff LP |
163 | [SOCKET_EXEC_START_PRE] = "StartPre", |
164 | [SOCKET_EXEC_START_POST] = "StartPost", | |
165 | [SOCKET_EXEC_STOP_PRE] = "StopPre", | |
166 | [SOCKET_EXEC_STOP_POST] = "StopPost" | |
167 | }; | |
168 | ||
169 | SocketExecCommand c; | |
87f0e418 | 170 | Socket *s = SOCKET(u); |
542563ba | 171 | SocketPort *p; |
82ba9f08 LP |
172 | const char *prefix2; |
173 | char *p2; | |
5cb5a6ff LP |
174 | |
175 | assert(s); | |
fa068367 | 176 | assert(f); |
5cb5a6ff | 177 | |
82ba9f08 LP |
178 | p2 = strappend(prefix, "\t"); |
179 | prefix2 = p2 ? p2 : prefix; | |
c43d20a0 | 180 | |
5cb5a6ff LP |
181 | fprintf(f, |
182 | "%sSocket State: %s\n" | |
542563ba | 183 | "%sBindIPv6Only: %s\n" |
b5a0699f | 184 | "%sBacklog: %u\n" |
50159e6a | 185 | "%sKillMode: %s\n" |
b5a0699f LP |
186 | "%sSocketMode: %04o\n" |
187 | "%sDirectoryMode: %04o\n", | |
acbb0225 | 188 | prefix, state_string_table[s->state], |
542563ba | 189 | prefix, yes_no(s->bind_ipv6_only), |
b5a0699f | 190 | prefix, s->backlog, |
50159e6a | 191 | prefix, kill_mode_to_string(s->kill_mode), |
b5a0699f LP |
192 | prefix, s->socket_mode, |
193 | prefix, s->directory_mode); | |
542563ba | 194 | |
70123e68 LP |
195 | if (s->control_pid > 0) |
196 | fprintf(f, | |
197 | "%sControl PID: %llu\n", | |
198 | prefix, (unsigned long long) s->control_pid); | |
199 | ||
acbb0225 LP |
200 | if (s->bind_to_device) |
201 | fprintf(f, | |
202 | "%sBindToDevice: %s\n", | |
203 | prefix, s->bind_to_device); | |
204 | ||
034c6ed7 | 205 | LIST_FOREACH(port, p, s->ports) { |
5cb5a6ff | 206 | |
542563ba LP |
207 | if (p->type == SOCKET_SOCKET) { |
208 | const char *t; | |
209 | int r; | |
210 | char *k; | |
211 | ||
212 | if ((r = socket_address_print(&p->address, &k)) < 0) | |
213 | t = strerror(-r); | |
214 | else | |
215 | t = k; | |
216 | ||
217 | fprintf(f, "%s%s: %s\n", prefix, listen_lookup(p->address.type), k); | |
218 | free(k); | |
219 | } else | |
220 | fprintf(f, "%sListenFIFO: %s\n", prefix, p->path); | |
221 | } | |
5cb5a6ff LP |
222 | |
223 | exec_context_dump(&s->exec_context, f, prefix); | |
224 | ||
e537352b | 225 | for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) { |
c43d20a0 LP |
226 | if (!s->exec_command[c]) |
227 | continue; | |
5cb5a6ff | 228 | |
c43d20a0 LP |
229 | fprintf(f, "%s→ %s:\n", |
230 | prefix, command_table[c]); | |
231 | ||
232 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 233 | } |
c43d20a0 | 234 | |
82ba9f08 | 235 | free(p2); |
5cb5a6ff LP |
236 | } |
237 | ||
034c6ed7 | 238 | static void socket_close_fds(Socket *s) { |
83c60c9f LP |
239 | SocketPort *p; |
240 | ||
241 | assert(s); | |
242 | ||
034c6ed7 | 243 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f LP |
244 | if (p->fd < 0) |
245 | continue; | |
246 | ||
acbb0225 | 247 | unit_unwatch_fd(UNIT(s), &p->fd_watch); |
9152c765 LP |
248 | assert_se(close_nointr(p->fd) >= 0); |
249 | ||
83c60c9f LP |
250 | p->fd = -1; |
251 | } | |
252 | } | |
253 | ||
034c6ed7 | 254 | static int socket_open_fds(Socket *s) { |
83c60c9f LP |
255 | SocketPort *p; |
256 | int r; | |
257 | ||
258 | assert(s); | |
259 | ||
034c6ed7 | 260 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f | 261 | |
034c6ed7 LP |
262 | if (p->fd >= 0) |
263 | continue; | |
83c60c9f LP |
264 | |
265 | if (p->type == SOCKET_SOCKET) { | |
266 | ||
b5a0699f LP |
267 | if ((r = socket_address_listen( |
268 | &p->address, | |
269 | s->backlog, | |
270 | s->bind_ipv6_only, | |
271 | s->bind_to_device, | |
272 | s->directory_mode, | |
273 | s->socket_mode, | |
274 | &p->fd)) < 0) | |
83c60c9f LP |
275 | goto rollback; |
276 | ||
277 | } else { | |
278 | struct stat st; | |
279 | assert(p->type == SOCKET_FIFO); | |
280 | ||
8cb45bf8 LP |
281 | mkdir_parents(p->path, s->directory_mode); |
282 | ||
283 | if (mkfifo(p->path, s->socket_mode) < 0 && errno != EEXIST) { | |
83c60c9f LP |
284 | r = -errno; |
285 | goto rollback; | |
286 | } | |
287 | ||
288 | if ((p->fd = open(p->path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) { | |
289 | r = -errno; | |
290 | goto rollback; | |
291 | } | |
292 | ||
293 | if (fstat(p->fd, &st) < 0) { | |
294 | r = -errno; | |
295 | goto rollback; | |
296 | } | |
297 | ||
298 | /* FIXME verify user, access mode */ | |
299 | ||
300 | if (!S_ISFIFO(st.st_mode)) { | |
301 | r = -EEXIST; | |
302 | goto rollback; | |
303 | } | |
304 | } | |
034c6ed7 LP |
305 | } |
306 | ||
307 | return 0; | |
308 | ||
309 | rollback: | |
310 | socket_close_fds(s); | |
311 | return r; | |
312 | } | |
313 | ||
314 | static void socket_unwatch_fds(Socket *s) { | |
315 | SocketPort *p; | |
9152c765 | 316 | |
034c6ed7 LP |
317 | assert(s); |
318 | ||
319 | LIST_FOREACH(port, p, s->ports) { | |
320 | if (p->fd < 0) | |
321 | continue; | |
322 | ||
acbb0225 | 323 | unit_unwatch_fd(UNIT(s), &p->fd_watch); |
83c60c9f | 324 | } |
034c6ed7 LP |
325 | } |
326 | ||
327 | static int socket_watch_fds(Socket *s) { | |
328 | SocketPort *p; | |
329 | int r; | |
330 | ||
331 | assert(s); | |
83c60c9f | 332 | |
034c6ed7 LP |
333 | LIST_FOREACH(port, p, s->ports) { |
334 | if (p->fd < 0) | |
335 | continue; | |
336 | ||
f94ea366 | 337 | if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0) |
034c6ed7 LP |
338 | goto fail; |
339 | } | |
83c60c9f | 340 | |
542563ba | 341 | return 0; |
83c60c9f | 342 | |
034c6ed7 LP |
343 | fail: |
344 | socket_unwatch_fds(s); | |
345 | return r; | |
346 | } | |
347 | ||
348 | static void socket_set_state(Socket *s, SocketState state) { | |
349 | SocketState old_state; | |
350 | assert(s); | |
351 | ||
352 | old_state = s->state; | |
353 | s->state = state; | |
354 | ||
355 | if (state != SOCKET_START_PRE && | |
356 | state != SOCKET_START_POST && | |
357 | state != SOCKET_STOP_PRE && | |
358 | state != SOCKET_STOP_PRE_SIGTERM && | |
359 | state != SOCKET_STOP_PRE_SIGKILL && | |
360 | state != SOCKET_STOP_POST && | |
361 | state != SOCKET_STOP_POST_SIGTERM && | |
e537352b | 362 | state != SOCKET_STOP_POST_SIGKILL) { |
acbb0225 | 363 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 364 | |
acbb0225 | 365 | if (s->control_pid > 0) { |
87f0e418 | 366 | unit_unwatch_pid(UNIT(s), s->control_pid); |
034c6ed7 LP |
367 | s->control_pid = 0; |
368 | } | |
369 | ||
034c6ed7 | 370 | s->control_command = NULL; |
e537352b | 371 | } |
034c6ed7 LP |
372 | |
373 | if (state != SOCKET_START_POST && | |
374 | state != SOCKET_LISTENING && | |
375 | state != SOCKET_RUNNING && | |
376 | state != SOCKET_STOP_PRE && | |
377 | state != SOCKET_STOP_PRE_SIGTERM && | |
378 | state != SOCKET_STOP_PRE_SIGKILL) | |
379 | socket_close_fds(s); | |
380 | ||
381 | if (state != SOCKET_LISTENING) | |
382 | socket_unwatch_fds(s); | |
383 | ||
e537352b LP |
384 | if (state != old_state) |
385 | log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]); | |
acbb0225 LP |
386 | |
387 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); | |
034c6ed7 LP |
388 | } |
389 | ||
e537352b | 390 | static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { |
034c6ed7 LP |
391 | pid_t pid; |
392 | int r; | |
393 | ||
394 | assert(s); | |
395 | assert(c); | |
396 | assert(_pid); | |
397 | ||
e537352b LP |
398 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
399 | goto fail; | |
034c6ed7 | 400 | |
8e274523 LP |
401 | if ((r = exec_spawn(c, |
402 | &s->exec_context, | |
403 | NULL, 0, | |
404 | true, | |
405 | true, | |
406 | UNIT(s)->meta.cgroup_bondings, | |
407 | &pid)) < 0) | |
034c6ed7 LP |
408 | goto fail; |
409 | ||
87f0e418 | 410 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
411 | /* FIXME: we need to do something here */ |
412 | goto fail; | |
83c60c9f | 413 | |
034c6ed7 LP |
414 | *_pid = pid; |
415 | ||
416 | return 0; | |
417 | ||
418 | fail: | |
e537352b | 419 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
83c60c9f LP |
420 | |
421 | return r; | |
542563ba LP |
422 | } |
423 | ||
034c6ed7 LP |
424 | static void socket_enter_dead(Socket *s, bool success) { |
425 | assert(s); | |
426 | ||
427 | if (!success) | |
428 | s->failure = true; | |
429 | ||
430 | socket_set_state(s, s->failure ? SOCKET_MAINTAINANCE : SOCKET_DEAD); | |
431 | } | |
432 | ||
433 | static void socket_enter_stop_post(Socket *s, bool success) { | |
434 | int r; | |
435 | assert(s); | |
436 | ||
437 | if (!success) | |
438 | s->failure = true; | |
439 | ||
d6ea93e3 | 440 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) |
e537352b | 441 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
442 | goto fail; |
443 | ||
d6ea93e3 LP |
444 | socket_set_state(s, SOCKET_STOP_POST); |
445 | ||
446 | if (!s->control_command) | |
034c6ed7 LP |
447 | socket_enter_dead(s, true); |
448 | ||
449 | return; | |
450 | ||
451 | fail: | |
87f0e418 | 452 | log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
453 | socket_enter_dead(s, false); |
454 | } | |
455 | ||
456 | static void socket_enter_signal(Socket *s, SocketState state, bool success) { | |
457 | int r; | |
458 | ||
459 | assert(s); | |
460 | ||
461 | if (!success) | |
462 | s->failure = true; | |
463 | ||
464 | if (s->control_pid > 0) { | |
465 | int sig; | |
50159e6a | 466 | bool sent = false; |
034c6ed7 LP |
467 | |
468 | sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_POST_SIGTERM) ? SIGTERM : SIGKILL; | |
469 | ||
50159e6a LP |
470 | if (s->kill_mode == KILL_CONTROL_GROUP) { |
471 | ||
472 | if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) { | |
473 | if (r != -EAGAIN && r != -ESRCH) | |
474 | goto fail; | |
475 | } else | |
476 | sent = true; | |
034c6ed7 | 477 | } |
50159e6a LP |
478 | |
479 | if (!sent) | |
480 | if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH) { | |
481 | r = -errno; | |
482 | goto fail; | |
483 | } | |
d6ea93e3 | 484 | } |
034c6ed7 | 485 | |
d6ea93e3 LP |
486 | socket_set_state(s, state); |
487 | ||
488 | if (s->control_pid <= 0) | |
034c6ed7 LP |
489 | socket_enter_dead(s, true); |
490 | ||
491 | return; | |
492 | ||
493 | fail: | |
87f0e418 | 494 | log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
495 | |
496 | if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
497 | socket_enter_stop_post(s, false); | |
498 | else | |
499 | socket_enter_dead(s, false); | |
500 | } | |
501 | ||
502 | static void socket_enter_stop_pre(Socket *s, bool success) { | |
503 | int r; | |
504 | assert(s); | |
505 | ||
506 | if (!success) | |
507 | s->failure = true; | |
508 | ||
d6ea93e3 | 509 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) |
e537352b | 510 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
511 | goto fail; |
512 | ||
d6ea93e3 LP |
513 | socket_set_state(s, SOCKET_STOP_PRE); |
514 | ||
515 | if (!s->control_command) | |
034c6ed7 LP |
516 | socket_enter_stop_post(s, true); |
517 | ||
518 | return; | |
519 | ||
520 | fail: | |
87f0e418 | 521 | log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
522 | socket_enter_stop_post(s, false); |
523 | } | |
524 | ||
e9af15c3 LP |
525 | static void socket_enter_listening(Socket *s) { |
526 | int r; | |
527 | assert(s); | |
528 | ||
529 | if ((r = socket_watch_fds(s)) < 0) { | |
530 | log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s)), strerror(-r)); | |
531 | goto fail; | |
532 | } | |
533 | ||
534 | socket_set_state(s, SOCKET_LISTENING); | |
535 | return; | |
536 | ||
537 | fail: | |
538 | socket_enter_stop_pre(s, false); | |
539 | } | |
540 | ||
034c6ed7 LP |
541 | static void socket_enter_start_post(Socket *s) { |
542 | int r; | |
543 | assert(s); | |
544 | ||
e9af15c3 | 545 | if ((r = socket_open_fds(s)) < 0) { |
87f0e418 | 546 | log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
547 | goto fail; |
548 | } | |
549 | ||
d6ea93e3 | 550 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) |
e537352b | 551 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) { |
87f0e418 | 552 | log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
553 | goto fail; |
554 | } | |
555 | ||
d6ea93e3 LP |
556 | socket_set_state(s, SOCKET_START_POST); |
557 | ||
558 | if (!s->control_command) | |
e9af15c3 | 559 | socket_enter_listening(s); |
034c6ed7 LP |
560 | |
561 | return; | |
562 | ||
563 | fail: | |
564 | socket_enter_stop_pre(s, false); | |
565 | } | |
566 | ||
567 | static void socket_enter_start_pre(Socket *s) { | |
568 | int r; | |
569 | assert(s); | |
570 | ||
d6ea93e3 | 571 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) |
e537352b | 572 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
573 | goto fail; |
574 | ||
d6ea93e3 LP |
575 | socket_set_state(s, SOCKET_START_PRE); |
576 | ||
577 | if (!s->control_command) | |
034c6ed7 LP |
578 | socket_enter_start_post(s); |
579 | ||
580 | return; | |
581 | ||
582 | fail: | |
87f0e418 | 583 | log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
584 | socket_enter_dead(s, false); |
585 | } | |
586 | ||
587 | static void socket_enter_running(Socket *s) { | |
588 | int r; | |
589 | ||
590 | assert(s); | |
591 | ||
87f0e418 | 592 | if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, NULL)) < 0) |
034c6ed7 LP |
593 | goto fail; |
594 | ||
595 | socket_set_state(s, SOCKET_RUNNING); | |
596 | return; | |
597 | ||
598 | fail: | |
87f0e418 | 599 | log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
600 | socket_enter_dead(s, false); |
601 | } | |
602 | ||
603 | static void socket_run_next(Socket *s, bool success) { | |
604 | int r; | |
605 | ||
606 | assert(s); | |
607 | assert(s->control_command); | |
608 | assert(s->control_command->command_next); | |
609 | ||
610 | if (!success) | |
611 | s->failure = true; | |
612 | ||
613 | s->control_command = s->control_command->command_next; | |
614 | ||
e537352b | 615 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
616 | goto fail; |
617 | ||
618 | return; | |
619 | ||
620 | fail: | |
621 | if (s->state == SOCKET_STOP_PRE) | |
622 | socket_enter_stop_post(s, false); | |
623 | else if (s->state == SOCKET_STOP_POST) | |
624 | socket_enter_dead(s, false); | |
625 | else | |
626 | socket_enter_stop_pre(s, false); | |
627 | } | |
628 | ||
87f0e418 LP |
629 | static int socket_start(Unit *u) { |
630 | Socket *s = SOCKET(u); | |
83c60c9f LP |
631 | |
632 | assert(s); | |
633 | ||
034c6ed7 LP |
634 | /* We cannot fulfill this request right now, try again later |
635 | * please! */ | |
636 | if (s->state == SOCKET_STOP_PRE || | |
637 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
638 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
639 | s->state == SOCKET_STOP_POST || | |
640 | s->state == SOCKET_STOP_POST_SIGTERM || | |
641 | s->state == SOCKET_STOP_POST_SIGKILL) | |
642 | return -EAGAIN; | |
643 | ||
83c60c9f LP |
644 | if (s->state == SOCKET_START_PRE || |
645 | s->state == SOCKET_START_POST) | |
034c6ed7 | 646 | return 0; |
83c60c9f | 647 | |
034c6ed7 | 648 | /* Cannot run this without the service being around */ |
87f0e418 | 649 | if (s->service->meta.load_state != UNIT_LOADED) |
034c6ed7 | 650 | return -ENOENT; |
83c60c9f | 651 | |
e537352b LP |
652 | /* If the service is alredy actvie we cannot start the |
653 | * socket */ | |
654 | if (s->service->state != SERVICE_DEAD && | |
655 | s->service->state != SERVICE_MAINTAINANCE && | |
656 | s->service->state != SERVICE_AUTO_RESTART) | |
657 | return -EBUSY; | |
658 | ||
034c6ed7 | 659 | assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTAINANCE); |
83c60c9f | 660 | |
034c6ed7 LP |
661 | s->failure = false; |
662 | socket_enter_start_pre(s); | |
663 | return 0; | |
664 | } | |
83c60c9f | 665 | |
87f0e418 LP |
666 | static int socket_stop(Unit *u) { |
667 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
668 | |
669 | assert(s); | |
670 | ||
671 | /* We cannot fulfill this request right now, try again later | |
672 | * please! */ | |
673 | if (s->state == SOCKET_START_PRE || | |
674 | s->state == SOCKET_START_POST) | |
675 | return -EAGAIN; | |
83c60c9f | 676 | |
e537352b LP |
677 | /* Already on it */ |
678 | if (s->state == SOCKET_STOP_PRE || | |
679 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
680 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
681 | s->state == SOCKET_STOP_POST || | |
682 | s->state == SOCKET_STOP_POST_SIGTERM || | |
683 | s->state == SOCKET_STOP_POST_SIGTERM) | |
684 | return 0; | |
685 | ||
034c6ed7 | 686 | assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING); |
83c60c9f | 687 | |
034c6ed7 | 688 | socket_enter_stop_pre(s, true); |
542563ba LP |
689 | return 0; |
690 | } | |
691 | ||
87f0e418 LP |
692 | static UnitActiveState socket_active_state(Unit *u) { |
693 | assert(u); | |
5cb5a6ff | 694 | |
acbb0225 | 695 | return state_translation_table[SOCKET(u)->state]; |
5cb5a6ff LP |
696 | } |
697 | ||
acbb0225 | 698 | static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 | 699 | Socket *s = SOCKET(u); |
9152c765 | 700 | |
034c6ed7 | 701 | assert(s); |
9152c765 | 702 | |
e9af15c3 | 703 | log_debug("Incoming traffic on %s", unit_id(u)); |
9152c765 | 704 | |
f94ea366 | 705 | if (events != EPOLLIN) |
034c6ed7 | 706 | socket_enter_stop_pre(s, false); |
9152c765 | 707 | |
034c6ed7 | 708 | socket_enter_running(s); |
9152c765 LP |
709 | } |
710 | ||
87f0e418 LP |
711 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
712 | Socket *s = SOCKET(u); | |
034c6ed7 | 713 | bool success; |
5cb5a6ff LP |
714 | |
715 | assert(s); | |
034c6ed7 | 716 | assert(pid >= 0); |
5cb5a6ff | 717 | |
bd982a8b | 718 | success = code == CLD_EXITED && status == 0; |
034c6ed7 | 719 | s->failure = s->failure || !success; |
542563ba | 720 | |
034c6ed7 LP |
721 | assert(s->control_pid == pid); |
722 | assert(s->control_command); | |
723 | ||
724 | exec_status_fill(&s->control_command->exec_status, pid, code, status); | |
725 | s->control_pid = 0; | |
726 | ||
94f04347 | 727 | log_debug("%s control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status); |
034c6ed7 LP |
728 | |
729 | if (s->control_command->command_next && | |
b866264a | 730 | (success || (s->state == SOCKET_STOP_PRE || s->state == SOCKET_STOP_POST))) { |
acbb0225 | 731 | log_debug("%s running next command for the state %s", unit_id(u), state_string_table[s->state]); |
034c6ed7 | 732 | socket_run_next(s, success); |
acbb0225 | 733 | } else { |
034c6ed7 LP |
734 | /* No further commands for this step, so let's figure |
735 | * out what to do next */ | |
5cb5a6ff | 736 | |
bd982a8b | 737 | log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]); |
acbb0225 | 738 | |
034c6ed7 LP |
739 | switch (s->state) { |
740 | ||
741 | case SOCKET_START_PRE: | |
742 | if (success) | |
acbb0225 | 743 | socket_enter_start_post(s); |
034c6ed7 LP |
744 | else |
745 | socket_enter_stop_pre(s, false); | |
746 | break; | |
747 | ||
748 | case SOCKET_START_POST: | |
749 | if (success) | |
e9af15c3 | 750 | socket_enter_listening(s); |
034c6ed7 LP |
751 | else |
752 | socket_enter_stop_pre(s, false); | |
753 | break; | |
754 | ||
755 | case SOCKET_STOP_PRE: | |
756 | case SOCKET_STOP_PRE_SIGTERM: | |
757 | case SOCKET_STOP_PRE_SIGKILL: | |
758 | socket_enter_stop_post(s, success); | |
759 | break; | |
760 | ||
761 | case SOCKET_STOP_POST: | |
762 | case SOCKET_STOP_POST_SIGTERM: | |
763 | case SOCKET_STOP_POST_SIGKILL: | |
764 | socket_enter_dead(s, success); | |
765 | break; | |
766 | ||
767 | default: | |
768 | assert_not_reached("Uh, control process died at wrong time."); | |
769 | } | |
770 | } | |
771 | } | |
5cb5a6ff | 772 | |
acbb0225 | 773 | static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) { |
87f0e418 | 774 | Socket *s = SOCKET(u); |
5cb5a6ff | 775 | |
034c6ed7 LP |
776 | assert(s); |
777 | assert(elapsed == 1); | |
acbb0225 | 778 | assert(w == &s->timer_watch); |
034c6ed7 LP |
779 | |
780 | switch (s->state) { | |
781 | ||
782 | case SOCKET_START_PRE: | |
783 | case SOCKET_START_POST: | |
e537352b | 784 | log_warning("%s starting timed out. Stopping.", unit_id(u)); |
034c6ed7 LP |
785 | socket_enter_stop_pre(s, false); |
786 | break; | |
787 | ||
788 | case SOCKET_STOP_PRE: | |
87f0e418 | 789 | log_warning("%s stopping timed out. Terminating.", unit_id(u)); |
034c6ed7 LP |
790 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false); |
791 | break; | |
792 | ||
793 | case SOCKET_STOP_PRE_SIGTERM: | |
87f0e418 | 794 | log_warning("%s stopping timed out. Killing.", unit_id(u)); |
034c6ed7 LP |
795 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false); |
796 | break; | |
797 | ||
798 | case SOCKET_STOP_PRE_SIGKILL: | |
87f0e418 | 799 | log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u)); |
034c6ed7 LP |
800 | socket_enter_stop_post(s, false); |
801 | break; | |
802 | ||
803 | case SOCKET_STOP_POST: | |
87f0e418 | 804 | log_warning("%s stopping timed out (2). Terminating.", unit_id(u)); |
034c6ed7 LP |
805 | socket_enter_signal(s, SOCKET_STOP_POST_SIGTERM, false); |
806 | break; | |
807 | ||
808 | case SOCKET_STOP_POST_SIGTERM: | |
87f0e418 | 809 | log_warning("%s stopping timed out (2). Killing.", unit_id(u)); |
034c6ed7 LP |
810 | socket_enter_signal(s, SOCKET_STOP_POST_SIGKILL, false); |
811 | break; | |
812 | ||
813 | case SOCKET_STOP_POST_SIGKILL: | |
87f0e418 | 814 | log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u)); |
034c6ed7 LP |
815 | socket_enter_dead(s, false); |
816 | break; | |
817 | ||
818 | default: | |
819 | assert_not_reached("Timeout at wrong time."); | |
820 | } | |
5cb5a6ff LP |
821 | } |
822 | ||
44d8db9e LP |
823 | int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { |
824 | int *rfds; | |
825 | unsigned rn_fds, k; | |
826 | SocketPort *p; | |
827 | ||
828 | assert(s); | |
829 | assert(fds); | |
830 | assert(n_fds); | |
831 | ||
832 | /* Called from the service code for requesting our fds */ | |
833 | ||
834 | rn_fds = 0; | |
835 | LIST_FOREACH(port, p, s->ports) | |
836 | if (p->fd >= 0) | |
837 | rn_fds++; | |
838 | ||
839 | if (!(rfds = new(int, rn_fds)) < 0) | |
840 | return -ENOMEM; | |
841 | ||
842 | k = 0; | |
843 | LIST_FOREACH(port, p, s->ports) | |
844 | if (p->fd >= 0) | |
845 | rfds[k++] = p->fd; | |
846 | ||
847 | assert(k == rn_fds); | |
848 | ||
849 | *fds = rfds; | |
850 | *n_fds = rn_fds; | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
ceee3d82 LP |
855 | void socket_notify_service_dead(Socket *s) { |
856 | assert(s); | |
857 | ||
858 | /* The service is dead. Dang. */ | |
859 | ||
860 | if (s->state == SOCKET_RUNNING) { | |
861 | log_debug("%s got notified about service death.", unit_id(UNIT(s))); | |
862 | socket_enter_listening(s); | |
863 | } | |
864 | } | |
865 | ||
87f0e418 | 866 | const UnitVTable socket_vtable = { |
5cb5a6ff LP |
867 | .suffix = ".socket", |
868 | ||
034c6ed7 | 869 | .init = socket_init, |
e537352b | 870 | .load = socket_load, |
034c6ed7 LP |
871 | .done = socket_done, |
872 | ||
5cb5a6ff LP |
873 | .dump = socket_dump, |
874 | ||
542563ba LP |
875 | .start = socket_start, |
876 | .stop = socket_stop, | |
5cb5a6ff LP |
877 | |
878 | .active_state = socket_active_state, | |
879 | ||
9152c765 | 880 | .fd_event = socket_fd_event, |
034c6ed7 LP |
881 | .sigchld_event = socket_sigchld_event, |
882 | .timer_event = socket_timer_event | |
5cb5a6ff | 883 | }; |