chiark / gitweb /
don't choke if there are no sockets for a service
[elogind.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 (!p)
289                         continue;
290
291                 if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
292                         goto fail;
293
294                 if (!cfds)
295                         continue;
296
297                 if (!rfds) {
298                         rfds = cfds;
299                         rn_fds = cn_fds;
300                 } else {
301                         int *t;
302
303                         if (!(t = new(int, rn_fds+cn_fds))) {
304                                 free(cfds);
305                                 r = -ENOMEM;
306                                 goto fail;
307                         }
308
309                         memcpy(t, rfds, rn_fds);
310                         memcpy(t+rn_fds, cfds, cn_fds);
311                         free(rfds);
312                         free(cfds);
313
314                         rfds = t;
315                         rn_fds = rn_fds+cn_fds;
316                 }
317         }
318
319         *fds = rfds;
320         *n_fds = rn_fds;
321         return 0;
322
323 fail:
324         free(rfds);
325         return r;
326 }
327
328 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
329         pid_t pid;
330         int r;
331         int *fds = NULL;
332         unsigned n_fds = 0;
333
334         assert(s);
335         assert(c);
336         assert(_pid);
337
338         if (pass_fds)
339                 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
340                         goto fail;
341
342         if (timeout) {
343                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
344                         goto fail;
345         } else
346                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
347
348         if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
349                 goto fail;
350
351         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
352                 /* FIXME: we need to do something here */
353                 goto fail;
354
355         free(fds);
356         *_pid = pid;
357
358         return 0;
359
360 fail:
361         free(fds);
362
363         if (timeout)
364                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
365
366         return r;
367 }
368
369 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
370         int r;
371         assert(s);
372
373         if (!success)
374                 s->failure = true;
375
376         if (allow_restart &&
377             (s->restart == SERVICE_RESTART_ALWAYS ||
378              (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
379
380                 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
381                         goto fail;
382
383                 service_set_state(s, SERVICE_AUTO_RESTART);
384         } else
385                 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
386
387         return;
388
389 fail:
390         log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
391         service_enter_dead(s, false, false);
392 }
393
394 static void service_enter_signal(Service *s, ServiceState state, bool success);
395
396 static void service_enter_stop_post(Service *s, bool success) {
397         int r;
398         assert(s);
399
400         if (!success)
401                 s->failure = true;
402
403         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
404
405                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
406                         goto fail;
407
408                 service_set_state(s, SERVICE_STOP_POST);
409         } else
410                 service_enter_dead(s, true, true);
411
412         return;
413
414 fail:
415         log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
416         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
417 }
418
419 static void service_enter_signal(Service *s, ServiceState state, bool success) {
420         int r;
421         bool sent = false;
422
423         assert(s);
424
425         if (!success)
426                 s->failure = true;
427
428         if (s->main_pid > 0 || s->control_pid > 0) {
429                 int sig;
430
431                 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
432
433                 r = 0;
434                 if (s->main_pid > 0) {
435                         if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
436                                 r = -errno;
437                         else
438                                 sent = true;
439                 }
440
441                 if (s->control_pid > 0) {
442                         if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
443                                 r = -errno;
444                         else
445                                 sent = true;
446                 }
447
448                 if (r < 0)
449                         goto fail;
450
451                 service_set_state(s, state);
452         } else
453                 service_enter_dead(s, true, true);
454
455         return;
456
457 fail:
458         log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
459
460         if (sent)  {
461                 s->failure = true;
462                 service_set_state(s, state);
463         } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
464                 service_enter_stop_post(s, false);
465         else
466                 service_enter_dead(s, false, true);
467 }
468
469 static void service_enter_stop(Service *s, bool success) {
470         int r;
471         assert(s);
472
473         if (!success)
474                 s->failure = true;
475
476         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
477
478                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
479                         goto fail;
480
481                 service_set_state(s, SERVICE_STOP);
482         } else
483                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
484
485         return;
486
487 fail:
488         log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
489         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
490 }
491
492 static void service_enter_start_post(Service *s) {
493         int r;
494         assert(s);
495
496         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
497
498                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
499                         goto fail;
500
501                 service_set_state(s, SERVICE_START_POST);
502         } else
503                 service_set_state(s, SERVICE_RUNNING);
504
505         return;
506
507 fail:
508         log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
509         service_enter_stop(s, false);
510 }
511
512 static void service_enter_start(Service *s) {
513         pid_t pid;
514         int r;
515
516         assert(s);
517
518         assert(s->exec_command[SERVICE_EXEC_START]);
519         assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
520
521         if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
522                 goto fail;
523
524         if (s->type == SERVICE_SIMPLE) {
525                 /* For simple services we immediately start
526                  * the START_POST binaries. */
527
528                 s->main_pid = pid;
529                 s->main_pid_known = true;
530                 service_enter_start_post(s);
531
532         } else  if (s->type == SERVICE_FORKING) {
533
534                 /* For forking services we wait until the start
535                  * process exited. */
536
537                 s->control_pid = pid;
538                 s->control_command = s->exec_command[SERVICE_EXEC_START];
539                 service_set_state(s, SERVICE_START);
540         } else
541                 assert_not_reached("Unknown service type");
542
543         return;
544
545 fail:
546         log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
547         service_enter_stop(s, false);
548 }
549
550 static void service_enter_start_pre(Service *s) {
551         int r;
552
553         assert(s);
554
555         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
556
557                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
558                         goto fail;
559
560                 service_set_state(s, SERVICE_START_PRE);
561         } else
562                 service_enter_start(s);
563
564         return;
565
566 fail:
567         log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
568         service_enter_dead(s, false, true);
569 }
570
571 static void service_enter_restart(Service *s) {
572         int r;
573         assert(s);
574
575         if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
576                 goto fail;
577
578         log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
579         service_enter_dead(s, true, false);
580         return;
581
582 fail:
583
584         log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
585         service_enter_dead(s, false, false);
586 }
587
588 static void service_enter_reload(Service *s) {
589         int r;
590
591         assert(s);
592
593         if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
594
595                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
596                         goto fail;
597
598                 service_set_state(s, SERVICE_RELOAD);
599         } else
600                 service_set_state(s, SERVICE_RUNNING);
601
602         return;
603
604 fail:
605         log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
606         service_enter_stop(s, false);
607 }
608
609 static void service_run_next(Service *s, bool success) {
610         int r;
611
612         assert(s);
613         assert(s->control_command);
614         assert(s->control_command->command_next);
615
616         if (!success)
617                 s->failure = true;
618
619         s->control_command = s->control_command->command_next;
620
621         if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
622                 goto fail;
623
624         return;
625
626 fail:
627         log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
628
629         if (s->state == SERVICE_STOP)
630                 service_enter_stop_post(s, false);
631         else if (s->state == SERVICE_STOP_POST)
632                 service_enter_dead(s, false, true);
633         else
634                 service_enter_stop(s, false);
635 }
636
637 static int service_start(Unit *u) {
638         Service *s = SERVICE(u);
639
640         assert(s);
641
642         /* We cannot fulfill this request right now, try again later
643          * please! */
644         if (s->state == SERVICE_STOP ||
645             s->state == SERVICE_STOP_SIGTERM ||
646             s->state == SERVICE_STOP_SIGKILL ||
647             s->state == SERVICE_STOP_POST ||
648             s->state == SERVICE_FINAL_SIGTERM ||
649             s->state == SERVICE_FINAL_SIGKILL)
650                 return -EAGAIN;
651
652         /* Already on it! */
653         if (s->state == SERVICE_START_PRE ||
654             s->state == SERVICE_START ||
655             s->state == SERVICE_START_POST)
656                 return 0;
657
658         assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
659
660         s->failure = false;
661         s->main_pid_known = false;
662
663         service_enter_start_pre(s);
664         return 0;
665 }
666
667 static int service_stop(Unit *u) {
668         Service *s = SERVICE(u);
669
670         assert(s);
671
672         if (s->state == SERVICE_START_PRE ||
673             s->state == SERVICE_START ||
674             s->state == SERVICE_START_POST ||
675             s->state == SERVICE_RELOAD)
676                 return -EAGAIN;
677
678         if (s->state == SERVICE_AUTO_RESTART) {
679                 service_set_state(s, SERVICE_DEAD);
680                 return 0;
681         }
682
683         assert(s->state == SERVICE_RUNNING);
684
685         service_enter_stop(s, true);
686         return 0;
687 }
688
689 static int service_reload(Unit *u) {
690         Service *s = SERVICE(u);
691
692         assert(s);
693
694         assert(s->state == SERVICE_RUNNING);
695
696         service_enter_reload(s);
697         return 0;
698 }
699
700 static bool service_can_reload(Unit *u) {
701         Service *s = SERVICE(u);
702
703         assert(s);
704
705         return !!s->exec_command[SERVICE_EXEC_RELOAD];
706 }
707
708 static UnitActiveState service_active_state(Unit *u) {
709         assert(u);
710
711         return state_translation_table[SERVICE(u)->state];
712 }
713
714 static int main_pid_good(Service *s) {
715         assert(s);
716
717         /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
718          * don't know */
719
720         /* If we know the pid file, then lets just check if it is
721          * still valid */
722         if (s->main_pid_known)
723                 return s->main_pid > 0;
724
725         /* We don't know the pid */
726         return -1;
727 }
728
729 static bool control_pid_good(Service *s) {
730         assert(s);
731
732         return s->control_pid > 0;
733 }
734
735 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
736         Service *s = SERVICE(u);
737         bool success;
738
739         assert(s);
740         assert(pid >= 0);
741
742         success = code == CLD_EXITED || status == 0;
743         s->failure = s->failure || !success;
744
745         if (s->main_pid == pid) {
746
747                 exec_status_fill(&s->main_exec_status, pid, code, status);
748                 s->main_pid = 0;
749
750                 if (s->type == SERVICE_SIMPLE) {
751                         assert(s->exec_command[SERVICE_EXEC_START]);
752                         s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
753                 }
754
755                 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
756
757                 /* The service exited, so the service is officially
758                  * gone. */
759
760                 switch (s->state) {
761
762                 case SERVICE_START_POST:
763                 case SERVICE_RELOAD:
764                 case SERVICE_STOP:
765                         /* Need to wait until the operation is
766                          * done */
767                         break;
768
769                 case SERVICE_RUNNING:
770                         service_enter_stop(s, success);
771                         break;
772
773                 case SERVICE_STOP_SIGTERM:
774                 case SERVICE_STOP_SIGKILL:
775
776                         if (!control_pid_good(s))
777                                 service_enter_stop_post(s, success);
778
779                         /* If there is still a control process, wait for that first */
780                         break;
781
782                 default:
783                         assert_not_reached("Uh, main process died at wrong time.");
784                 }
785
786         } else if (s->control_pid == pid) {
787                 assert(s->control_command);
788
789                 exec_status_fill(&s->control_command->exec_status, pid, code, status);
790                 s->control_pid = 0;
791
792                 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
793
794                 /* If we are shutting things down anyway we
795                  * don't care about failing commands. */
796
797                 if (s->control_command->command_next &&
798                     (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
799
800                         /* There is another command to *
801                          * execute, so let's do that. */
802
803                         service_run_next(s, success);
804
805                 else {
806                         /* No further commands for this step, so let's
807                          * figure out what to do next */
808
809                         switch (s->state) {
810
811                         case SERVICE_START_PRE:
812                                 if (success)
813                                         service_enter_start(s);
814                                 else
815                                         service_enter_stop(s, false);
816                                 break;
817
818                         case SERVICE_START:
819                                 assert(s->type == SERVICE_FORKING);
820
821                                 /* Let's try to load the pid
822                                  * file here if we can. We
823                                  * ignore the return value,
824                                  * since the PID file might
825                                  * actually be created by a
826                                  * START_POST script */
827
828                                 if (success) {
829                                         if (s->pid_file)
830                                                 service_load_pid_file(s);
831
832                                         service_enter_start_post(s);
833                                 } else
834                                         service_enter_stop(s, false);
835
836                                 break;
837
838                         case SERVICE_START_POST:
839                                 if (success && s->pid_file && !s->main_pid_known) {
840                                         int r;
841
842                                         /* Hmm, let's see if we can
843                                          * load the pid now after the
844                                          * start-post scripts got
845                                          * executed. */
846
847                                         if ((r = service_load_pid_file(s)) < 0)
848                                                 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
849                                 }
850
851                                 /* Fall through */
852
853                         case SERVICE_RELOAD:
854                                 if (success) {
855                                         if (main_pid_good(s) != 0)
856                                                 service_set_state(s, SERVICE_RUNNING);
857                                         else
858                                                 service_enter_stop(s, true);
859                                 } else
860                                         service_enter_stop(s, false);
861
862                                 break;
863
864                         case SERVICE_STOP:
865                                 if (main_pid_good(s) > 0)
866                                         /* Still not dead and we know the PID? Let's go hunting. */
867                                         service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
868                                 else
869                                         service_enter_stop_post(s, success);
870                                 break;
871
872                         case SERVICE_STOP_SIGTERM:
873                         case SERVICE_STOP_SIGKILL:
874                                 if (main_pid_good(s) <= 0)
875                                         service_enter_stop_post(s, success);
876
877                                 /* If there is still a service
878                                  * process around, wait until
879                                  * that one quit, too */
880                                 break;
881
882                         case SERVICE_STOP_POST:
883                         case SERVICE_FINAL_SIGTERM:
884                         case SERVICE_FINAL_SIGKILL:
885                                 service_enter_dead(s, success, true);
886                                 break;
887
888                         default:
889                                 assert_not_reached("Uh, control process died at wrong time.");
890                         }
891                 }
892         } else
893                 assert_not_reached("Got SIGCHLD for unkown PID");
894 }
895
896 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
897         Service *s = SERVICE(u);
898
899         assert(s);
900         assert(elapsed == 1);
901
902         assert(w == &s->timer_watch);
903
904         switch (s->state) {
905
906         case SERVICE_START_PRE:
907         case SERVICE_START:
908         case SERVICE_START_POST:
909         case SERVICE_RELOAD:
910                 log_warning("%s operation timed out. Stopping.", unit_id(u));
911                 service_enter_stop(s, false);
912                 break;
913
914         case SERVICE_STOP:
915                 log_warning("%s stopping timed out. Terminating.", unit_id(u));
916                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
917                 break;
918
919         case SERVICE_STOP_SIGTERM:
920                 log_warning("%s stopping timed out. Killing.", unit_id(u));
921                 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
922                 break;
923
924         case SERVICE_STOP_SIGKILL:
925                 /* Uh, wie sent a SIGKILL and it is still not gone?
926                  * Must be something we cannot kill, so let's just be
927                  * weirded out and continue */
928
929                 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
930                 service_enter_stop_post(s, false);
931                 break;
932
933         case SERVICE_STOP_POST:
934                 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
935                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
936                 break;
937
938         case SERVICE_FINAL_SIGTERM:
939                 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
940                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
941                 break;
942
943         case SERVICE_FINAL_SIGKILL:
944                 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
945                 service_enter_dead(s, false, true);
946                 break;
947
948         case SERVICE_AUTO_RESTART:
949                 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
950                 service_enter_restart(s);
951                 break;
952
953         default:
954                 assert_not_reached("Timeout at wrong time.");
955         }
956 }
957
958 const UnitVTable service_vtable = {
959         .suffix = ".service",
960
961         .init = service_init,
962         .done = service_done,
963
964         .dump = service_dump,
965
966         .start = service_start,
967         .stop = service_stop,
968         .reload = service_reload,
969
970         .can_reload = service_can_reload,
971
972         .active_state = service_active_state,
973
974         .sigchld_event = service_sigchld_event,
975         .timer_event = service_timer_event,
976 };