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