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