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