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