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