chiark / gitweb /
when shortcutting states do not skip state transitions
[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] = "start-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 int service_get_sockets(Service *s, Set **_set) {
202         Set *set;
203         Iterator i;
204         char *t;
205         int r;
206
207         assert(s);
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;
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
224                 if (!(k = unit_name_change_suffix(t, ".socket"))) {
225                         r = -ENOMEM;
226                         goto fail;
227                 }
228
229                 p = manager_get_unit(UNIT(s)->meta.manager, k);
230                 free(k);
231
232                 if (!p) continue;
233
234                 if ((r = set_put(set, p)) < 0)
235                         goto fail;
236         }
237
238         *_set = set;
239         return 0;
240
241 fail:
242         set_free(set);
243         return r;
244 }
245
246
247 static 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
265         return 0;
266 }
267
268 static void service_set_state(Service *s, ServiceState state) {
269         ServiceState old_state;
270         assert(s);
271
272         old_state = s->state;
273         s->state = state;
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)
286                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
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)
294                 if (s->main_pid > 0) {
295                         unit_unwatch_pid(UNIT(s), s->main_pid);
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)
309                 if (s->control_pid > 0) {
310                         unit_unwatch_pid(UNIT(s), s->control_pid);
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
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
333         log_debug("%s changed %s ā†’ %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
334
335         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
336 }
337
338 static 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;
343         Set *set;
344         Socket *socket;
345
346         assert(s);
347         assert(fds);
348         assert(n_fds);
349
350         if ((r = service_get_sockets(s, &set)) < 0)
351                 return r;
352
353         SET_FOREACH(socket, set, i) {
354                 int *cfds;
355                 unsigned cn_fds;
356
357                 if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0)
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;
387
388         set_free(set);
389
390         return 0;
391
392 fail:
393         set_free(set);
394         free(rfds);
395
396         return r;
397 }
398
399 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
400         pid_t pid;
401         int r;
402         int *fds = NULL;
403         unsigned n_fds = 0;
404
405         assert(s);
406         assert(c);
407         assert(_pid);
408
409         if (pass_fds)
410                 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
411                         goto fail;
412
413         if (timeout) {
414                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
415                         goto fail;
416         } else
417                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
418
419         if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
420                 goto fail;
421
422         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
423                 /* FIXME: we need to do something here */
424                 goto fail;
425
426         free(fds);
427         *_pid = pid;
428
429         return 0;
430
431 fail:
432         free(fds);
433
434         if (timeout)
435                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
436
437         return r;
438 }
439
440 static 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
451                 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
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
460 fail:
461         log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
462         service_enter_dead(s, false, false);
463 }
464
465 static void service_enter_signal(Service *s, ServiceState state, bool success);
466
467 static void service_enter_stop_post(Service *s, bool success) {
468         int r;
469         assert(s);
470
471         if (!success)
472                 s->failure = true;
473
474         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
475                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
476                         goto fail;
477
478
479         service_set_state(s, SERVICE_STOP_POST);
480
481         if (!s->control_command)
482                 service_enter_dead(s, true, true);
483
484         return;
485
486 fail:
487         log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
488         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
489 }
490
491 static 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;
522         }
523
524         service_set_state(s, state);
525
526         if (s->main_pid <= 0 && s->control_pid <= 0)
527                 service_enter_dead(s, true, true);
528
529         return;
530
531 fail:
532         log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
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
543 static void service_enter_stop(Service *s, bool success) {
544         int r;
545         assert(s);
546
547         if (!success)
548                 s->failure = true;
549
550         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
551                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
552                         goto fail;
553
554         service_set_state(s, SERVICE_STOP);
555
556         if (!s->control_command)
557                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
558
559         return;
560
561 fail:
562         log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
563         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
564 }
565
566 static void service_enter_start_post(Service *s) {
567         int r;
568         assert(s);
569
570         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
571                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
572                         goto fail;
573
574
575         service_set_state(s, SERVICE_START_POST);
576
577         if (!s->control_command)
578                 service_set_state(s, SERVICE_RUNNING);
579
580         return;
581
582 fail:
583         log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
584         service_enter_stop(s, false);
585 }
586
587 static 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
596         if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
597                 goto fail;
598
599         service_set_state(s, SERVICE_START);
600
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];
616         } else
617                 assert_not_reached("Unknown service type");
618
619         return;
620
621 fail:
622         log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
623         service_enter_stop(s, false);
624 }
625
626 static void service_enter_start_pre(Service *s) {
627         int r;
628
629         assert(s);
630
631         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
632                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
633                         goto fail;
634
635         service_set_state(s, SERVICE_START_PRE);
636
637         if (!s->control_command)
638                 service_enter_start(s);
639
640         return;
641
642 fail:
643         log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
644         service_enter_dead(s, false, true);
645 }
646
647 static void service_enter_restart(Service *s) {
648         int r;
649         assert(s);
650
651         if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
652                 goto fail;
653
654         log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
655         service_enter_dead(s, true, false);
656         return;
657
658 fail:
659
660         log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
661         service_enter_dead(s, false, false);
662 }
663
664 static void service_enter_reload(Service *s) {
665         int r;
666
667         assert(s);
668
669         if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
670                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
671                         goto fail;
672
673         service_set_state(s, SERVICE_RELOAD);
674
675         if (!s->control_command)
676                 service_set_state(s, SERVICE_RUNNING);
677
678         return;
679
680 fail:
681         log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
682         service_enter_stop(s, false);
683 }
684
685 static 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
697         if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
698                 goto fail;
699
700         return;
701
702 fail:
703         log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
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);
711 }
712
713 static int service_start(Unit *u) {
714         Service *s = SERVICE(u);
715
716         assert(s);
717
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)
726                 return -EAGAIN;
727
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);
735
736         s->failure = false;
737         s->main_pid_known = false;
738
739         service_enter_start_pre(s);
740         return 0;
741 }
742
743 static int service_stop(Unit *u) {
744         Service *s = SERVICE(u);
745
746         assert(s);
747
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);
760
761         service_enter_stop(s, true);
762         return 0;
763 }
764
765 static int service_reload(Unit *u) {
766         Service *s = SERVICE(u);
767
768         assert(s);
769
770         assert(s->state == SERVICE_RUNNING);
771
772         service_enter_reload(s);
773         return 0;
774 }
775
776 static bool service_can_reload(Unit *u) {
777         Service *s = SERVICE(u);
778
779         assert(s);
780
781         return !!s->exec_command[SERVICE_EXEC_RELOAD];
782 }
783
784 static UnitActiveState service_active_state(Unit *u) {
785         assert(u);
786
787         return state_translation_table[SERVICE(u)->state];
788 }
789
790 static 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
805 static bool control_pid_good(Service *s) {
806         assert(s);
807
808         return s->control_pid > 0;
809 }
810
811 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
812         Service *s = SERVICE(u);
813         bool success;
814
815         assert(s);
816         assert(pid >= 0);
817
818         success = code == CLD_EXITED && status == 0;
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
831                 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
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);
854
855                         /* If there is still a control process, wait for that first */
856                         break;
857
858                 default:
859                         assert_not_reached("Uh, main process died at wrong time.");
860                 }
861
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
868                 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
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
885                         log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
886
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)
926                                                 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
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
974 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
975         Service *s = SERVICE(u);
976
977         assert(s);
978         assert(elapsed == 1);
979
980         assert(w == &s->timer_watch);
981
982         switch (s->state) {
983
984         case SERVICE_START_PRE:
985         case SERVICE_START:
986         case SERVICE_START_POST:
987         case SERVICE_RELOAD:
988                 log_warning("%s operation timed out. Stopping.", unit_id(u));
989                 service_enter_stop(s, false);
990                 break;
991
992         case SERVICE_STOP:
993                 log_warning("%s stopping timed out. Terminating.", unit_id(u));
994                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
995                 break;
996
997         case SERVICE_STOP_SIGTERM:
998                 log_warning("%s stopping timed out. Killing.", unit_id(u));
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
1007                 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1008                 service_enter_stop_post(s, false);
1009                 break;
1010
1011         case SERVICE_STOP_POST:
1012                 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1013                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1014                 break;
1015
1016         case SERVICE_FINAL_SIGTERM:
1017                 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1018                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1019                 break;
1020
1021         case SERVICE_FINAL_SIGKILL:
1022                 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1023                 service_enter_dead(s, false, true);
1024                 break;
1025
1026         case SERVICE_AUTO_RESTART:
1027                 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1028                 service_enter_restart(s);
1029                 break;
1030
1031         default:
1032                 assert_not_reached("Timeout at wrong time.");
1033         }
1034 }
1035
1036 const UnitVTable service_vtable = {
1037         .suffix = ".service",
1038
1039         .init = service_init,
1040         .done = service_done,
1041
1042         .dump = service_dump,
1043
1044         .start = service_start,
1045         .stop = service_stop,
1046         .reload = service_reload,
1047
1048         .can_reload = service_can_reload,
1049
1050         .active_state = service_active_state,
1051
1052         .sigchld_event = service_sigchld_event,
1053         .timer_event = service_timer_event,
1054 };