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