]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | #include <errno.h> | |
034c6ed7 | 4 | #include <signal.h> |
5cb5a6ff | 5 | |
87f0e418 | 6 | #include "unit.h" |
5cb5a6ff LP |
7 | #include "service.h" |
8 | #include "load-fragment.h" | |
9 | #include "load-dropin.h" | |
034c6ed7 LP |
10 | #include "log.h" |
11 | ||
acbb0225 | 12 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
87f0e418 LP |
13 | [SERVICE_DEAD] = UNIT_INACTIVE, |
14 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
15 | [SERVICE_START] = UNIT_ACTIVATING, | |
16 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
17 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
18 | [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING, | |
19 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
20 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
21 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
22 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
23 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
24 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
25 | [SERVICE_MAINTAINANCE] = UNIT_INACTIVE, | |
26 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, | |
034c6ed7 | 27 | }; |
5cb5a6ff | 28 | |
acbb0225 LP |
29 | static const char* const state_string_table[_SERVICE_STATE_MAX] = { |
30 | [SERVICE_DEAD] = "dead", | |
31 | [SERVICE_START_PRE] = "start-pre", | |
32 | [SERVICE_START] = "start", | |
d6ea93e3 | 33 | [SERVICE_START_POST] = "start-post", |
acbb0225 LP |
34 | [SERVICE_RUNNING] = "running", |
35 | [SERVICE_RELOAD] = "reload", | |
36 | [SERVICE_STOP] = "stop", | |
37 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
38 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
39 | [SERVICE_STOP_POST] = "stop-post", | |
40 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
41 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
42 | [SERVICE_MAINTAINANCE] = "maintainance", | |
43 | [SERVICE_AUTO_RESTART] = "auto-restart", | |
44 | }; | |
45 | ||
87f0e418 LP |
46 | static void service_done(Unit *u) { |
47 | Service *s = SERVICE(u); | |
44d8db9e LP |
48 | |
49 | assert(s); | |
50 | ||
51 | free(s->pid_file); | |
52 | s->pid_file = NULL; | |
53 | ||
54 | exec_context_done(&s->exec_context); | |
55 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX); | |
56 | s->control_command = NULL; | |
57 | ||
58 | /* This will leak a process, but at least no memory or any of | |
59 | * our resources */ | |
60 | if (s->main_pid > 0) { | |
87f0e418 | 61 | unit_unwatch_pid(u, s->main_pid); |
44d8db9e LP |
62 | s->main_pid = 0; |
63 | } | |
64 | ||
65 | if (s->control_pid > 0) { | |
87f0e418 | 66 | unit_unwatch_pid(u, s->control_pid); |
44d8db9e LP |
67 | s->control_pid = 0; |
68 | } | |
69 | ||
acbb0225 | 70 | unit_unwatch_timer(u, &s->timer_watch); |
44d8db9e LP |
71 | } |
72 | ||
5cb5a6ff LP |
73 | static int service_load_sysv(Service *s) { |
74 | assert(s); | |
75 | ||
76 | /* Load service data from SysV init scripts, preferably with | |
77 | * LSB headers ... */ | |
78 | ||
79 | return -ENOENT; | |
80 | } | |
81 | ||
87f0e418 | 82 | static int service_init(Unit *u) { |
5cb5a6ff | 83 | int r; |
87f0e418 | 84 | Service *s = SERVICE(u); |
5cb5a6ff LP |
85 | |
86 | assert(s); | |
87 | ||
034c6ed7 LP |
88 | /* First, reset everything to the defaults, in case this is a |
89 | * reload */ | |
90 | ||
91 | s->type = 0; | |
92 | s->restart = 0; | |
93 | ||
94 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
95 | s->restart_usec = DEFAULT_RESTART_USEC; | |
96 | ||
97 | exec_context_init(&s->exec_context); | |
98 | ||
acbb0225 | 99 | s->timer_watch.type = WATCH_INVALID; |
034c6ed7 LP |
100 | |
101 | s->state = SERVICE_DEAD; | |
5cb5a6ff LP |
102 | |
103 | /* Load a .service file */ | |
d46de8a1 | 104 | if ((r = unit_load_fragment(u)) < 0) { |
87f0e418 | 105 | service_done(u); |
5cb5a6ff | 106 | return r; |
44d8db9e | 107 | } |
5cb5a6ff | 108 | |
d46de8a1 LP |
109 | /* Load a classic init script as a fallback, if we couldn*t find anything */ |
110 | if (r == 0) | |
111 | if ((r = service_load_sysv(s)) <= 0) { | |
112 | service_done(u); | |
113 | return r < 0 ? r : -ENOENT; | |
114 | } | |
115 | ||
5cb5a6ff | 116 | /* Load dropin directory data */ |
87f0e418 LP |
117 | if ((r = unit_load_dropin(u)) < 0) { |
118 | service_done(u); | |
5cb5a6ff | 119 | return r; |
034c6ed7 LP |
120 | } |
121 | ||
44d8db9e | 122 | return 0; |
034c6ed7 LP |
123 | } |
124 | ||
87f0e418 | 125 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 126 | |
5cb5a6ff | 127 | static const char* const command_table[_SERVICE_EXEC_MAX] = { |
034c6ed7 LP |
128 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
129 | [SERVICE_EXEC_START] = "ExecStart", | |
130 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
131 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
132 | [SERVICE_EXEC_STOP] = "ExecStop", | |
133 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
5cb5a6ff LP |
134 | }; |
135 | ||
136 | ServiceExecCommand c; | |
87f0e418 | 137 | Service *s = SERVICE(u); |
44d8db9e | 138 | char *prefix2; |
5cb5a6ff LP |
139 | |
140 | assert(s); | |
141 | ||
44d8db9e LP |
142 | prefix2 = strappend(prefix, "\t"); |
143 | if (!prefix2) | |
144 | prefix2 = ""; | |
145 | ||
5cb5a6ff LP |
146 | fprintf(f, |
147 | "%sService State: %s\n", | |
acbb0225 | 148 | prefix, state_string_table[s->state]); |
5cb5a6ff | 149 | |
034c6ed7 LP |
150 | if (s->pid_file) |
151 | fprintf(f, | |
152 | "%sPIDFile: %s\n", | |
153 | prefix, s->pid_file); | |
154 | ||
155 | ||
5cb5a6ff LP |
156 | exec_context_dump(&s->exec_context, f, prefix); |
157 | ||
158 | for (c = 0; c < _SERVICE_EXEC_MAX; c++) { | |
5cb5a6ff | 159 | |
44d8db9e LP |
160 | if (!s->exec_command[c]) |
161 | continue; | |
162 | ||
163 | fprintf(f, "%s→ %s:\n", | |
164 | prefix, command_table[c]); | |
165 | ||
166 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 167 | } |
44d8db9e LP |
168 | |
169 | free(prefix2); | |
5cb5a6ff LP |
170 | } |
171 | ||
034c6ed7 LP |
172 | static int service_load_pid_file(Service *s) { |
173 | char *k; | |
174 | unsigned long p; | |
175 | int r; | |
176 | ||
177 | assert(s); | |
178 | ||
179 | if (s->main_pid_known) | |
180 | return 0; | |
181 | ||
182 | if (!s->pid_file) | |
183 | return -ENOENT; | |
184 | ||
185 | if ((r = read_one_line_file(s->pid_file, &k)) < 0) | |
186 | return r; | |
187 | ||
188 | if ((r = safe_atolu(k, &p)) < 0) { | |
189 | free(k); | |
190 | return r; | |
191 | } | |
192 | ||
193 | if ((unsigned long) (pid_t) p != p) | |
194 | return -ERANGE; | |
195 | ||
196 | s->main_pid = p; | |
197 | s->main_pid_known = true; | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
3e33402a LP |
202 | static int service_get_sockets(Service *s, Set **_set) { |
203 | Set *set; | |
ceee3d82 LP |
204 | Iterator i; |
205 | char *t; | |
3e33402a | 206 | int r; |
ceee3d82 LP |
207 | |
208 | assert(s); | |
3e33402a LP |
209 | assert(_set); |
210 | ||
211 | /* Collects all Socket objects that belong to this | |
212 | * service. Note that a service might have multiple sockets | |
213 | * via multiple names. */ | |
214 | ||
215 | if (!(set = set_new(NULL, NULL))) | |
216 | return -ENOMEM; | |
ceee3d82 LP |
217 | |
218 | SET_FOREACH(t, UNIT(s)->meta.names, i) { | |
219 | char *k; | |
220 | Unit *p; | |
221 | ||
222 | /* Look for all socket objects that go by any of our | |
223 | * units and collect their fds */ | |
224 | ||
3e33402a LP |
225 | if (!(k = unit_name_change_suffix(t, ".socket"))) { |
226 | r = -ENOMEM; | |
227 | goto fail; | |
228 | } | |
ceee3d82 LP |
229 | |
230 | p = manager_get_unit(UNIT(s)->meta.manager, k); | |
231 | free(k); | |
232 | ||
3e33402a | 233 | if (!p) continue; |
ceee3d82 | 234 | |
3e33402a LP |
235 | if ((r = set_put(set, p)) < 0) |
236 | goto fail; | |
ceee3d82 LP |
237 | } |
238 | ||
3e33402a LP |
239 | *_set = set; |
240 | return 0; | |
241 | ||
242 | fail: | |
243 | set_free(set); | |
244 | return r; | |
245 | } | |
246 | ||
247 | ||
248 | static int service_notify_sockets(Service *s) { | |
249 | Iterator i; | |
250 | Set *set; | |
251 | Socket *socket; | |
252 | int r; | |
253 | ||
254 | assert(s); | |
255 | ||
256 | /* Notifies all our sockets when we die */ | |
257 | ||
258 | if ((r = service_get_sockets(s, &set)) < 0) | |
259 | return r; | |
260 | ||
261 | SET_FOREACH(socket, set, i) | |
262 | socket_notify_service_dead(socket); | |
263 | ||
264 | set_free(set); | |
265 | ||
ceee3d82 LP |
266 | return 0; |
267 | } | |
268 | ||
034c6ed7 LP |
269 | static void service_set_state(Service *s, ServiceState state) { |
270 | ServiceState old_state; | |
5cb5a6ff LP |
271 | assert(s); |
272 | ||
034c6ed7 | 273 | old_state = s->state; |
5cb5a6ff | 274 | s->state = state; |
034c6ed7 LP |
275 | |
276 | if (state != SERVICE_START_PRE && | |
277 | state != SERVICE_START && | |
278 | state != SERVICE_START_POST && | |
279 | state != SERVICE_RELOAD && | |
280 | state != SERVICE_STOP && | |
281 | state != SERVICE_STOP_SIGTERM && | |
282 | state != SERVICE_STOP_SIGKILL && | |
283 | state != SERVICE_STOP_POST && | |
284 | state != SERVICE_FINAL_SIGTERM && | |
285 | state != SERVICE_FINAL_SIGKILL && | |
286 | state != SERVICE_AUTO_RESTART) | |
acbb0225 | 287 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 LP |
288 | |
289 | if (state != SERVICE_START_POST && | |
290 | state != SERVICE_RUNNING && | |
291 | state != SERVICE_RELOAD && | |
292 | state != SERVICE_STOP && | |
293 | state != SERVICE_STOP_SIGTERM && | |
294 | state != SERVICE_STOP_SIGKILL) | |
acbb0225 | 295 | if (s->main_pid > 0) { |
87f0e418 | 296 | unit_unwatch_pid(UNIT(s), s->main_pid); |
034c6ed7 LP |
297 | s->main_pid = 0; |
298 | } | |
299 | ||
300 | if (state != SERVICE_START_PRE && | |
301 | state != SERVICE_START && | |
302 | state != SERVICE_START_POST && | |
303 | state != SERVICE_RELOAD && | |
304 | state != SERVICE_STOP && | |
305 | state != SERVICE_STOP_SIGTERM && | |
306 | state != SERVICE_STOP_SIGKILL && | |
307 | state != SERVICE_STOP_POST && | |
308 | state != SERVICE_FINAL_SIGTERM && | |
309 | state != SERVICE_FINAL_SIGKILL) | |
acbb0225 | 310 | if (s->control_pid > 0) { |
87f0e418 | 311 | unit_unwatch_pid(UNIT(s), s->control_pid); |
034c6ed7 LP |
312 | s->control_pid = 0; |
313 | } | |
314 | ||
315 | if (state != SERVICE_START_PRE && | |
316 | state != SERVICE_START && | |
317 | state != SERVICE_START_POST && | |
318 | state != SERVICE_RELOAD && | |
319 | state != SERVICE_STOP && | |
320 | state != SERVICE_STOP_POST) | |
321 | s->control_command = NULL; | |
322 | ||
ceee3d82 LP |
323 | if (state == SERVICE_DEAD || |
324 | state == SERVICE_STOP || | |
325 | state == SERVICE_STOP_SIGTERM || | |
326 | state == SERVICE_STOP_SIGKILL || | |
327 | state == SERVICE_STOP_POST || | |
328 | state == SERVICE_FINAL_SIGTERM || | |
329 | state == SERVICE_FINAL_SIGKILL || | |
330 | state == SERVICE_MAINTAINANCE || | |
331 | state == SERVICE_AUTO_RESTART) | |
332 | service_notify_sockets(s); | |
333 | ||
d6ea93e3 | 334 | log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]); |
acbb0225 LP |
335 | |
336 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); | |
034c6ed7 LP |
337 | } |
338 | ||
44d8db9e LP |
339 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { |
340 | Iterator i; | |
341 | int r; | |
342 | int *rfds = NULL; | |
343 | unsigned rn_fds = 0; | |
3e33402a LP |
344 | Set *set; |
345 | Socket *socket; | |
44d8db9e LP |
346 | |
347 | assert(s); | |
348 | assert(fds); | |
349 | assert(n_fds); | |
350 | ||
3e33402a LP |
351 | if ((r = service_get_sockets(s, &set)) < 0) |
352 | return r; | |
353 | ||
354 | SET_FOREACH(socket, set, i) { | |
44d8db9e LP |
355 | int *cfds; |
356 | unsigned cn_fds; | |
357 | ||
3e33402a | 358 | if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0) |
44d8db9e LP |
359 | goto fail; |
360 | ||
361 | if (!cfds) | |
362 | continue; | |
363 | ||
364 | if (!rfds) { | |
365 | rfds = cfds; | |
366 | rn_fds = cn_fds; | |
367 | } else { | |
368 | int *t; | |
369 | ||
370 | if (!(t = new(int, rn_fds+cn_fds))) { | |
371 | free(cfds); | |
372 | r = -ENOMEM; | |
373 | goto fail; | |
374 | } | |
375 | ||
376 | memcpy(t, rfds, rn_fds); | |
377 | memcpy(t+rn_fds, cfds, cn_fds); | |
378 | free(rfds); | |
379 | free(cfds); | |
380 | ||
381 | rfds = t; | |
382 | rn_fds = rn_fds+cn_fds; | |
383 | } | |
384 | } | |
385 | ||
386 | *fds = rfds; | |
387 | *n_fds = rn_fds; | |
3e33402a LP |
388 | |
389 | set_free(set); | |
390 | ||
44d8db9e LP |
391 | return 0; |
392 | ||
393 | fail: | |
3e33402a | 394 | set_free(set); |
44d8db9e | 395 | free(rfds); |
3e33402a | 396 | |
44d8db9e LP |
397 | return r; |
398 | } | |
399 | ||
400 | static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) { | |
034c6ed7 LP |
401 | pid_t pid; |
402 | int r; | |
44d8db9e LP |
403 | int *fds = NULL; |
404 | unsigned n_fds = 0; | |
034c6ed7 LP |
405 | |
406 | assert(s); | |
407 | assert(c); | |
408 | assert(_pid); | |
409 | ||
44d8db9e LP |
410 | if (pass_fds) |
411 | if ((r = service_collect_fds(s, &fds, &n_fds)) < 0) | |
412 | goto fail; | |
413 | ||
034c6ed7 | 414 | if (timeout) { |
acbb0225 | 415 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
416 | goto fail; |
417 | } else | |
acbb0225 | 418 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 419 | |
44d8db9e | 420 | if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0) |
034c6ed7 LP |
421 | goto fail; |
422 | ||
87f0e418 | 423 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
424 | /* FIXME: we need to do something here */ |
425 | goto fail; | |
426 | ||
44d8db9e | 427 | free(fds); |
034c6ed7 LP |
428 | *_pid = pid; |
429 | ||
5cb5a6ff | 430 | return 0; |
034c6ed7 LP |
431 | |
432 | fail: | |
44d8db9e LP |
433 | free(fds); |
434 | ||
034c6ed7 | 435 | if (timeout) |
acbb0225 | 436 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 LP |
437 | |
438 | return r; | |
439 | } | |
440 | ||
441 | static void service_enter_dead(Service *s, bool success, bool allow_restart) { | |
442 | int r; | |
443 | assert(s); | |
444 | ||
445 | if (!success) | |
446 | s->failure = true; | |
447 | ||
448 | if (allow_restart && | |
449 | (s->restart == SERVICE_RESTART_ALWAYS || | |
450 | (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) { | |
451 | ||
acbb0225 | 452 | if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
453 | goto fail; |
454 | ||
455 | service_set_state(s, SERVICE_AUTO_RESTART); | |
456 | } else | |
457 | service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD); | |
458 | ||
459 | return; | |
460 | ||
461 | fail: | |
87f0e418 | 462 | log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
463 | service_enter_dead(s, false, false); |
464 | } | |
465 | ||
466 | static void service_enter_signal(Service *s, ServiceState state, bool success); | |
467 | ||
468 | static void service_enter_stop_post(Service *s, bool success) { | |
469 | int r; | |
470 | assert(s); | |
471 | ||
472 | if (!success) | |
473 | s->failure = true; | |
474 | ||
d6ea93e3 | 475 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) |
44d8db9e | 476 | if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) |
034c6ed7 LP |
477 | goto fail; |
478 | ||
d6ea93e3 LP |
479 | |
480 | service_set_state(s, SERVICE_STOP_POST); | |
481 | ||
482 | if (!s->control_command) | |
034c6ed7 LP |
483 | service_enter_dead(s, true, true); |
484 | ||
485 | return; | |
486 | ||
487 | fail: | |
87f0e418 | 488 | log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
489 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
490 | } | |
491 | ||
492 | static void service_enter_signal(Service *s, ServiceState state, bool success) { | |
493 | int r; | |
494 | bool sent = false; | |
495 | ||
496 | assert(s); | |
497 | ||
498 | if (!success) | |
499 | s->failure = true; | |
500 | ||
501 | if (s->main_pid > 0 || s->control_pid > 0) { | |
502 | int sig; | |
503 | ||
504 | sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL; | |
505 | ||
506 | r = 0; | |
507 | if (s->main_pid > 0) { | |
508 | if (kill(s->main_pid, sig) < 0 && errno != ESRCH) | |
509 | r = -errno; | |
510 | else | |
511 | sent = true; | |
512 | } | |
513 | ||
514 | if (s->control_pid > 0) { | |
515 | if (kill(s->control_pid, sig) < 0 && errno != ESRCH) | |
516 | r = -errno; | |
517 | else | |
518 | sent = true; | |
519 | } | |
520 | ||
521 | if (r < 0) | |
522 | goto fail; | |
d6ea93e3 | 523 | } |
034c6ed7 | 524 | |
d6ea93e3 LP |
525 | service_set_state(s, state); |
526 | ||
527 | if (s->main_pid <= 0 && s->control_pid <= 0) | |
034c6ed7 LP |
528 | service_enter_dead(s, true, true); |
529 | ||
530 | return; | |
531 | ||
532 | fail: | |
87f0e418 | 533 | log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
534 | |
535 | if (sent) { | |
536 | s->failure = true; | |
537 | service_set_state(s, state); | |
538 | } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) | |
539 | service_enter_stop_post(s, false); | |
540 | else | |
541 | service_enter_dead(s, false, true); | |
542 | } | |
543 | ||
544 | static void service_enter_stop(Service *s, bool success) { | |
545 | int r; | |
546 | assert(s); | |
547 | ||
548 | if (!success) | |
549 | s->failure = true; | |
550 | ||
d6ea93e3 | 551 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) |
44d8db9e | 552 | if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) |
034c6ed7 LP |
553 | goto fail; |
554 | ||
d6ea93e3 LP |
555 | service_set_state(s, SERVICE_STOP); |
556 | ||
557 | if (!s->control_command) | |
034c6ed7 LP |
558 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); |
559 | ||
560 | return; | |
561 | ||
562 | fail: | |
87f0e418 | 563 | log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
564 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
565 | } | |
566 | ||
567 | static void service_enter_start_post(Service *s) { | |
568 | int r; | |
569 | assert(s); | |
570 | ||
d6ea93e3 | 571 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) |
44d8db9e | 572 | if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) |
034c6ed7 LP |
573 | goto fail; |
574 | ||
d6ea93e3 LP |
575 | |
576 | service_set_state(s, SERVICE_START_POST); | |
577 | ||
578 | if (!s->control_command) | |
034c6ed7 LP |
579 | service_set_state(s, SERVICE_RUNNING); |
580 | ||
581 | return; | |
582 | ||
583 | fail: | |
87f0e418 | 584 | log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
585 | service_enter_stop(s, false); |
586 | } | |
587 | ||
588 | static void service_enter_start(Service *s) { | |
589 | pid_t pid; | |
590 | int r; | |
591 | ||
592 | assert(s); | |
593 | ||
594 | assert(s->exec_command[SERVICE_EXEC_START]); | |
595 | assert(!s->exec_command[SERVICE_EXEC_START]->command_next); | |
596 | ||
44d8db9e | 597 | if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0) |
034c6ed7 LP |
598 | goto fail; |
599 | ||
d6ea93e3 LP |
600 | service_set_state(s, SERVICE_START); |
601 | ||
034c6ed7 LP |
602 | if (s->type == SERVICE_SIMPLE) { |
603 | /* For simple services we immediately start | |
604 | * the START_POST binaries. */ | |
605 | ||
606 | s->main_pid = pid; | |
607 | s->main_pid_known = true; | |
608 | service_enter_start_post(s); | |
609 | ||
610 | } else if (s->type == SERVICE_FORKING) { | |
611 | ||
612 | /* For forking services we wait until the start | |
613 | * process exited. */ | |
614 | ||
615 | s->control_pid = pid; | |
616 | s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
034c6ed7 LP |
617 | } else |
618 | assert_not_reached("Unknown service type"); | |
619 | ||
620 | return; | |
621 | ||
622 | fail: | |
87f0e418 | 623 | log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
624 | service_enter_stop(s, false); |
625 | } | |
626 | ||
627 | static void service_enter_start_pre(Service *s) { | |
628 | int r; | |
629 | ||
630 | assert(s); | |
631 | ||
d6ea93e3 | 632 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) |
44d8db9e | 633 | if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) |
034c6ed7 LP |
634 | goto fail; |
635 | ||
d6ea93e3 LP |
636 | service_set_state(s, SERVICE_START_PRE); |
637 | ||
638 | if (!s->control_command) | |
034c6ed7 LP |
639 | service_enter_start(s); |
640 | ||
641 | return; | |
642 | ||
643 | fail: | |
87f0e418 | 644 | log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
645 | service_enter_dead(s, false, true); |
646 | } | |
647 | ||
648 | static void service_enter_restart(Service *s) { | |
649 | int r; | |
650 | assert(s); | |
651 | ||
87f0e418 | 652 | if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0) |
034c6ed7 LP |
653 | goto fail; |
654 | ||
87f0e418 | 655 | log_debug("%s scheduled restart job.", unit_id(UNIT(s))); |
034c6ed7 LP |
656 | service_enter_dead(s, true, false); |
657 | return; | |
658 | ||
659 | fail: | |
660 | ||
87f0e418 | 661 | log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
662 | service_enter_dead(s, false, false); |
663 | } | |
664 | ||
665 | static void service_enter_reload(Service *s) { | |
666 | int r; | |
667 | ||
668 | assert(s); | |
669 | ||
d6ea93e3 | 670 | if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) |
44d8db9e | 671 | if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) |
034c6ed7 LP |
672 | goto fail; |
673 | ||
d6ea93e3 LP |
674 | service_set_state(s, SERVICE_RELOAD); |
675 | ||
676 | if (!s->control_command) | |
034c6ed7 LP |
677 | service_set_state(s, SERVICE_RUNNING); |
678 | ||
679 | return; | |
680 | ||
681 | fail: | |
87f0e418 | 682 | log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
683 | service_enter_stop(s, false); |
684 | } | |
685 | ||
686 | static void service_run_next(Service *s, bool success) { | |
687 | int r; | |
688 | ||
689 | assert(s); | |
690 | assert(s->control_command); | |
691 | assert(s->control_command->command_next); | |
692 | ||
693 | if (!success) | |
694 | s->failure = true; | |
695 | ||
696 | s->control_command = s->control_command->command_next; | |
697 | ||
44d8db9e | 698 | if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0) |
034c6ed7 LP |
699 | goto fail; |
700 | ||
701 | return; | |
702 | ||
703 | fail: | |
87f0e418 | 704 | log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
705 | |
706 | if (s->state == SERVICE_STOP) | |
707 | service_enter_stop_post(s, false); | |
708 | else if (s->state == SERVICE_STOP_POST) | |
709 | service_enter_dead(s, false, true); | |
710 | else | |
711 | service_enter_stop(s, false); | |
5cb5a6ff LP |
712 | } |
713 | ||
87f0e418 LP |
714 | static int service_start(Unit *u) { |
715 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
716 | |
717 | assert(s); | |
718 | ||
034c6ed7 LP |
719 | /* We cannot fulfill this request right now, try again later |
720 | * please! */ | |
721 | if (s->state == SERVICE_STOP || | |
722 | s->state == SERVICE_STOP_SIGTERM || | |
723 | s->state == SERVICE_STOP_SIGKILL || | |
724 | s->state == SERVICE_STOP_POST || | |
725 | s->state == SERVICE_FINAL_SIGTERM || | |
726 | s->state == SERVICE_FINAL_SIGKILL) | |
5cb5a6ff LP |
727 | return -EAGAIN; |
728 | ||
034c6ed7 LP |
729 | /* Already on it! */ |
730 | if (s->state == SERVICE_START_PRE || | |
731 | s->state == SERVICE_START || | |
732 | s->state == SERVICE_START_POST) | |
733 | return 0; | |
734 | ||
735 | assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART); | |
5cb5a6ff | 736 | |
034c6ed7 LP |
737 | s->failure = false; |
738 | s->main_pid_known = false; | |
739 | ||
740 | service_enter_start_pre(s); | |
741 | return 0; | |
5cb5a6ff LP |
742 | } |
743 | ||
87f0e418 LP |
744 | static int service_stop(Unit *u) { |
745 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
746 | |
747 | assert(s); | |
748 | ||
034c6ed7 LP |
749 | if (s->state == SERVICE_START_PRE || |
750 | s->state == SERVICE_START || | |
751 | s->state == SERVICE_START_POST || | |
752 | s->state == SERVICE_RELOAD) | |
753 | return -EAGAIN; | |
754 | ||
755 | if (s->state == SERVICE_AUTO_RESTART) { | |
756 | service_set_state(s, SERVICE_DEAD); | |
757 | return 0; | |
758 | } | |
759 | ||
760 | assert(s->state == SERVICE_RUNNING); | |
5cb5a6ff | 761 | |
034c6ed7 | 762 | service_enter_stop(s, true); |
5cb5a6ff LP |
763 | return 0; |
764 | } | |
765 | ||
87f0e418 LP |
766 | static int service_reload(Unit *u) { |
767 | Service *s = SERVICE(u); | |
034c6ed7 LP |
768 | |
769 | assert(s); | |
770 | ||
771 | assert(s->state == SERVICE_RUNNING); | |
772 | ||
773 | service_enter_reload(s); | |
5cb5a6ff LP |
774 | return 0; |
775 | } | |
776 | ||
87f0e418 LP |
777 | static bool service_can_reload(Unit *u) { |
778 | Service *s = SERVICE(u); | |
034c6ed7 LP |
779 | |
780 | assert(s); | |
781 | ||
782 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
783 | } | |
784 | ||
87f0e418 LP |
785 | static UnitActiveState service_active_state(Unit *u) { |
786 | assert(u); | |
5cb5a6ff | 787 | |
acbb0225 | 788 | return state_translation_table[SERVICE(u)->state]; |
034c6ed7 LP |
789 | } |
790 | ||
791 | static int main_pid_good(Service *s) { | |
792 | assert(s); | |
793 | ||
794 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
795 | * don't know */ | |
796 | ||
797 | /* If we know the pid file, then lets just check if it is | |
798 | * still valid */ | |
799 | if (s->main_pid_known) | |
800 | return s->main_pid > 0; | |
801 | ||
802 | /* We don't know the pid */ | |
803 | return -1; | |
804 | } | |
805 | ||
806 | static bool control_pid_good(Service *s) { | |
807 | assert(s); | |
5cb5a6ff | 808 | |
034c6ed7 | 809 | return s->control_pid > 0; |
5cb5a6ff LP |
810 | } |
811 | ||
87f0e418 LP |
812 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
813 | Service *s = SERVICE(u); | |
034c6ed7 | 814 | bool success; |
5cb5a6ff LP |
815 | |
816 | assert(s); | |
034c6ed7 LP |
817 | assert(pid >= 0); |
818 | ||
bd982a8b | 819 | success = code == CLD_EXITED && status == 0; |
034c6ed7 LP |
820 | s->failure = s->failure || !success; |
821 | ||
822 | if (s->main_pid == pid) { | |
823 | ||
824 | exec_status_fill(&s->main_exec_status, pid, code, status); | |
825 | s->main_pid = 0; | |
826 | ||
827 | if (s->type == SERVICE_SIMPLE) { | |
828 | assert(s->exec_command[SERVICE_EXEC_START]); | |
829 | s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status; | |
830 | } | |
831 | ||
87f0e418 | 832 | log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status); |
034c6ed7 LP |
833 | |
834 | /* The service exited, so the service is officially | |
835 | * gone. */ | |
836 | ||
837 | switch (s->state) { | |
838 | ||
839 | case SERVICE_START_POST: | |
840 | case SERVICE_RELOAD: | |
841 | case SERVICE_STOP: | |
842 | /* Need to wait until the operation is | |
843 | * done */ | |
844 | break; | |
845 | ||
846 | case SERVICE_RUNNING: | |
847 | service_enter_stop(s, success); | |
848 | break; | |
849 | ||
850 | case SERVICE_STOP_SIGTERM: | |
851 | case SERVICE_STOP_SIGKILL: | |
852 | ||
853 | if (!control_pid_good(s)) | |
854 | service_enter_stop_post(s, success); | |
5cb5a6ff | 855 | |
034c6ed7 LP |
856 | /* If there is still a control process, wait for that first */ |
857 | break; | |
5cb5a6ff | 858 | |
034c6ed7 LP |
859 | default: |
860 | assert_not_reached("Uh, main process died at wrong time."); | |
861 | } | |
5cb5a6ff | 862 | |
034c6ed7 LP |
863 | } else if (s->control_pid == pid) { |
864 | assert(s->control_command); | |
865 | ||
866 | exec_status_fill(&s->control_command->exec_status, pid, code, status); | |
867 | s->control_pid = 0; | |
868 | ||
87f0e418 | 869 | log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status); |
034c6ed7 LP |
870 | |
871 | /* If we are shutting things down anyway we | |
872 | * don't care about failing commands. */ | |
873 | ||
874 | if (s->control_command->command_next && | |
875 | (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST))) | |
876 | ||
877 | /* There is another command to * | |
878 | * execute, so let's do that. */ | |
879 | ||
880 | service_run_next(s, success); | |
881 | ||
882 | else { | |
883 | /* No further commands for this step, so let's | |
884 | * figure out what to do next */ | |
885 | ||
bd982a8b LP |
886 | log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]); |
887 | ||
034c6ed7 LP |
888 | switch (s->state) { |
889 | ||
890 | case SERVICE_START_PRE: | |
891 | if (success) | |
892 | service_enter_start(s); | |
893 | else | |
894 | service_enter_stop(s, false); | |
895 | break; | |
896 | ||
897 | case SERVICE_START: | |
898 | assert(s->type == SERVICE_FORKING); | |
899 | ||
900 | /* Let's try to load the pid | |
901 | * file here if we can. We | |
902 | * ignore the return value, | |
903 | * since the PID file might | |
904 | * actually be created by a | |
905 | * START_POST script */ | |
906 | ||
907 | if (success) { | |
908 | if (s->pid_file) | |
909 | service_load_pid_file(s); | |
910 | ||
911 | service_enter_start_post(s); | |
912 | } else | |
913 | service_enter_stop(s, false); | |
914 | ||
915 | break; | |
916 | ||
917 | case SERVICE_START_POST: | |
918 | if (success && s->pid_file && !s->main_pid_known) { | |
919 | int r; | |
920 | ||
921 | /* Hmm, let's see if we can | |
922 | * load the pid now after the | |
923 | * start-post scripts got | |
924 | * executed. */ | |
925 | ||
926 | if ((r = service_load_pid_file(s)) < 0) | |
87f0e418 | 927 | log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r)); |
034c6ed7 LP |
928 | } |
929 | ||
930 | /* Fall through */ | |
931 | ||
932 | case SERVICE_RELOAD: | |
933 | if (success) { | |
934 | if (main_pid_good(s) != 0) | |
935 | service_set_state(s, SERVICE_RUNNING); | |
936 | else | |
937 | service_enter_stop(s, true); | |
938 | } else | |
939 | service_enter_stop(s, false); | |
940 | ||
941 | break; | |
942 | ||
943 | case SERVICE_STOP: | |
944 | if (main_pid_good(s) > 0) | |
945 | /* Still not dead and we know the PID? Let's go hunting. */ | |
946 | service_enter_signal(s, SERVICE_STOP_SIGTERM, success); | |
947 | else | |
948 | service_enter_stop_post(s, success); | |
949 | break; | |
950 | ||
951 | case SERVICE_STOP_SIGTERM: | |
952 | case SERVICE_STOP_SIGKILL: | |
953 | if (main_pid_good(s) <= 0) | |
954 | service_enter_stop_post(s, success); | |
955 | ||
956 | /* If there is still a service | |
957 | * process around, wait until | |
958 | * that one quit, too */ | |
959 | break; | |
960 | ||
961 | case SERVICE_STOP_POST: | |
962 | case SERVICE_FINAL_SIGTERM: | |
963 | case SERVICE_FINAL_SIGKILL: | |
964 | service_enter_dead(s, success, true); | |
965 | break; | |
966 | ||
967 | default: | |
968 | assert_not_reached("Uh, control process died at wrong time."); | |
969 | } | |
970 | } | |
971 | } else | |
972 | assert_not_reached("Got SIGCHLD for unkown PID"); | |
973 | } | |
974 | ||
acbb0225 | 975 | static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) { |
87f0e418 | 976 | Service *s = SERVICE(u); |
034c6ed7 LP |
977 | |
978 | assert(s); | |
979 | assert(elapsed == 1); | |
980 | ||
acbb0225 | 981 | assert(w == &s->timer_watch); |
034c6ed7 LP |
982 | |
983 | switch (s->state) { | |
984 | ||
985 | case SERVICE_START_PRE: | |
986 | case SERVICE_START: | |
987 | case SERVICE_START_POST: | |
988 | case SERVICE_RELOAD: | |
87f0e418 | 989 | log_warning("%s operation timed out. Stopping.", unit_id(u)); |
034c6ed7 LP |
990 | service_enter_stop(s, false); |
991 | break; | |
992 | ||
993 | case SERVICE_STOP: | |
87f0e418 | 994 | log_warning("%s stopping timed out. Terminating.", unit_id(u)); |
034c6ed7 LP |
995 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
996 | break; | |
997 | ||
998 | case SERVICE_STOP_SIGTERM: | |
87f0e418 | 999 | log_warning("%s stopping timed out. Killing.", unit_id(u)); |
034c6ed7 LP |
1000 | service_enter_signal(s, SERVICE_STOP_SIGKILL, false); |
1001 | break; | |
1002 | ||
1003 | case SERVICE_STOP_SIGKILL: | |
1004 | /* Uh, wie sent a SIGKILL and it is still not gone? | |
1005 | * Must be something we cannot kill, so let's just be | |
1006 | * weirded out and continue */ | |
1007 | ||
87f0e418 | 1008 | log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u)); |
034c6ed7 LP |
1009 | service_enter_stop_post(s, false); |
1010 | break; | |
1011 | ||
1012 | case SERVICE_STOP_POST: | |
87f0e418 | 1013 | log_warning("%s stopping timed out (2). Terminating.", unit_id(u)); |
034c6ed7 LP |
1014 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
1015 | break; | |
1016 | ||
1017 | case SERVICE_FINAL_SIGTERM: | |
87f0e418 | 1018 | log_warning("%s stopping timed out (2). Killing.", unit_id(u)); |
034c6ed7 LP |
1019 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, false); |
1020 | break; | |
1021 | ||
1022 | case SERVICE_FINAL_SIGKILL: | |
87f0e418 | 1023 | log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u)); |
034c6ed7 LP |
1024 | service_enter_dead(s, false, true); |
1025 | break; | |
1026 | ||
1027 | case SERVICE_AUTO_RESTART: | |
87f0e418 | 1028 | log_debug("%s holdoff time over, scheduling restart.", unit_id(u)); |
034c6ed7 LP |
1029 | service_enter_restart(s); |
1030 | break; | |
1031 | ||
1032 | default: | |
1033 | assert_not_reached("Timeout at wrong time."); | |
1034 | } | |
5cb5a6ff LP |
1035 | } |
1036 | ||
87f0e418 | 1037 | const UnitVTable service_vtable = { |
5cb5a6ff LP |
1038 | .suffix = ".service", |
1039 | ||
034c6ed7 LP |
1040 | .init = service_init, |
1041 | .done = service_done, | |
1042 | ||
5cb5a6ff LP |
1043 | .dump = service_dump, |
1044 | ||
1045 | .start = service_start, | |
1046 | .stop = service_stop, | |
1047 | .reload = service_reload, | |
1048 | ||
034c6ed7 LP |
1049 | .can_reload = service_can_reload, |
1050 | ||
5cb5a6ff LP |
1051 | .active_state = service_active_state, |
1052 | ||
034c6ed7 LP |
1053 | .sigchld_event = service_sigchld_event, |
1054 | .timer_event = service_timer_event, | |
5cb5a6ff | 1055 | }; |