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