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