chiark / gitweb /
2001b9f4d8e69e3b21da080b93f98bf51e53472c
[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 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 changing %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
476                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
477                         goto fail;
478
479                 service_set_state(s, SERVICE_STOP_POST);
480         } else
481                 service_enter_dead(s, true, true);
482
483         return;
484
485 fail:
486         log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
487         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
488 }
489
490 static void service_enter_signal(Service *s, ServiceState state, bool success) {
491         int r;
492         bool sent = false;
493
494         assert(s);
495
496         if (!success)
497                 s->failure = true;
498
499         if (s->main_pid > 0 || s->control_pid > 0) {
500                 int sig;
501
502                 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
503
504                 r = 0;
505                 if (s->main_pid > 0) {
506                         if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
507                                 r = -errno;
508                         else
509                                 sent = true;
510                 }
511
512                 if (s->control_pid > 0) {
513                         if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
514                                 r = -errno;
515                         else
516                                 sent = true;
517                 }
518
519                 if (r < 0)
520                         goto fail;
521
522                 service_set_state(s, state);
523         } else
524                 service_enter_dead(s, true, true);
525
526         return;
527
528 fail:
529         log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
530
531         if (sent)  {
532                 s->failure = true;
533                 service_set_state(s, state);
534         } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
535                 service_enter_stop_post(s, false);
536         else
537                 service_enter_dead(s, false, true);
538 }
539
540 static void service_enter_stop(Service *s, bool success) {
541         int r;
542         assert(s);
543
544         if (!success)
545                 s->failure = true;
546
547         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
548
549                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
550                         goto fail;
551
552                 service_set_state(s, SERVICE_STOP);
553         } else
554                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
555
556         return;
557
558 fail:
559         log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
560         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
561 }
562
563 static void service_enter_start_post(Service *s) {
564         int r;
565         assert(s);
566
567         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
568
569                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
570                         goto fail;
571
572                 service_set_state(s, SERVICE_START_POST);
573         } else
574                 service_set_state(s, SERVICE_RUNNING);
575
576         return;
577
578 fail:
579         log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
580         service_enter_stop(s, false);
581 }
582
583 static void service_enter_start(Service *s) {
584         pid_t pid;
585         int r;
586
587         assert(s);
588
589         assert(s->exec_command[SERVICE_EXEC_START]);
590         assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
591
592         if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
593                 goto fail;
594
595         if (s->type == SERVICE_SIMPLE) {
596                 /* For simple services we immediately start
597                  * the START_POST binaries. */
598
599                 s->main_pid = pid;
600                 s->main_pid_known = true;
601                 service_enter_start_post(s);
602
603         } else  if (s->type == SERVICE_FORKING) {
604
605                 /* For forking services we wait until the start
606                  * process exited. */
607
608                 s->control_pid = pid;
609                 s->control_command = s->exec_command[SERVICE_EXEC_START];
610                 service_set_state(s, SERVICE_START);
611         } else
612                 assert_not_reached("Unknown service type");
613
614         return;
615
616 fail:
617         log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
618         service_enter_stop(s, false);
619 }
620
621 static void service_enter_start_pre(Service *s) {
622         int r;
623
624         assert(s);
625
626         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
627
628                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
629                         goto fail;
630
631                 service_set_state(s, SERVICE_START_PRE);
632         } else
633                 service_enter_start(s);
634
635         return;
636
637 fail:
638         log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
639         service_enter_dead(s, false, true);
640 }
641
642 static void service_enter_restart(Service *s) {
643         int r;
644         assert(s);
645
646         if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
647                 goto fail;
648
649         log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
650         service_enter_dead(s, true, false);
651         return;
652
653 fail:
654
655         log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
656         service_enter_dead(s, false, false);
657 }
658
659 static void service_enter_reload(Service *s) {
660         int r;
661
662         assert(s);
663
664         if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
665
666                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
667                         goto fail;
668
669                 service_set_state(s, SERVICE_RELOAD);
670         } else
671                 service_set_state(s, SERVICE_RUNNING);
672
673         return;
674
675 fail:
676         log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
677         service_enter_stop(s, false);
678 }
679
680 static void service_run_next(Service *s, bool success) {
681         int r;
682
683         assert(s);
684         assert(s->control_command);
685         assert(s->control_command->command_next);
686
687         if (!success)
688                 s->failure = true;
689
690         s->control_command = s->control_command->command_next;
691
692         if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
693                 goto fail;
694
695         return;
696
697 fail:
698         log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
699
700         if (s->state == SERVICE_STOP)
701                 service_enter_stop_post(s, false);
702         else if (s->state == SERVICE_STOP_POST)
703                 service_enter_dead(s, false, true);
704         else
705                 service_enter_stop(s, false);
706 }
707
708 static int service_start(Unit *u) {
709         Service *s = SERVICE(u);
710
711         assert(s);
712
713         /* We cannot fulfill this request right now, try again later
714          * please! */
715         if (s->state == SERVICE_STOP ||
716             s->state == SERVICE_STOP_SIGTERM ||
717             s->state == SERVICE_STOP_SIGKILL ||
718             s->state == SERVICE_STOP_POST ||
719             s->state == SERVICE_FINAL_SIGTERM ||
720             s->state == SERVICE_FINAL_SIGKILL)
721                 return -EAGAIN;
722
723         /* Already on it! */
724         if (s->state == SERVICE_START_PRE ||
725             s->state == SERVICE_START ||
726             s->state == SERVICE_START_POST)
727                 return 0;
728
729         assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
730
731         s->failure = false;
732         s->main_pid_known = false;
733
734         service_enter_start_pre(s);
735         return 0;
736 }
737
738 static int service_stop(Unit *u) {
739         Service *s = SERVICE(u);
740
741         assert(s);
742
743         if (s->state == SERVICE_START_PRE ||
744             s->state == SERVICE_START ||
745             s->state == SERVICE_START_POST ||
746             s->state == SERVICE_RELOAD)
747                 return -EAGAIN;
748
749         if (s->state == SERVICE_AUTO_RESTART) {
750                 service_set_state(s, SERVICE_DEAD);
751                 return 0;
752         }
753
754         assert(s->state == SERVICE_RUNNING);
755
756         service_enter_stop(s, true);
757         return 0;
758 }
759
760 static int service_reload(Unit *u) {
761         Service *s = SERVICE(u);
762
763         assert(s);
764
765         assert(s->state == SERVICE_RUNNING);
766
767         service_enter_reload(s);
768         return 0;
769 }
770
771 static bool service_can_reload(Unit *u) {
772         Service *s = SERVICE(u);
773
774         assert(s);
775
776         return !!s->exec_command[SERVICE_EXEC_RELOAD];
777 }
778
779 static UnitActiveState service_active_state(Unit *u) {
780         assert(u);
781
782         return state_translation_table[SERVICE(u)->state];
783 }
784
785 static int main_pid_good(Service *s) {
786         assert(s);
787
788         /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
789          * don't know */
790
791         /* If we know the pid file, then lets just check if it is
792          * still valid */
793         if (s->main_pid_known)
794                 return s->main_pid > 0;
795
796         /* We don't know the pid */
797         return -1;
798 }
799
800 static bool control_pid_good(Service *s) {
801         assert(s);
802
803         return s->control_pid > 0;
804 }
805
806 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
807         Service *s = SERVICE(u);
808         bool success;
809
810         assert(s);
811         assert(pid >= 0);
812
813         success = code == CLD_EXITED && status == 0;
814         s->failure = s->failure || !success;
815
816         if (s->main_pid == pid) {
817
818                 exec_status_fill(&s->main_exec_status, pid, code, status);
819                 s->main_pid = 0;
820
821                 if (s->type == SERVICE_SIMPLE) {
822                         assert(s->exec_command[SERVICE_EXEC_START]);
823                         s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
824                 }
825
826                 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
827
828                 /* The service exited, so the service is officially
829                  * gone. */
830
831                 switch (s->state) {
832
833                 case SERVICE_START_POST:
834                 case SERVICE_RELOAD:
835                 case SERVICE_STOP:
836                         /* Need to wait until the operation is
837                          * done */
838                         break;
839
840                 case SERVICE_RUNNING:
841                         service_enter_stop(s, success);
842                         break;
843
844                 case SERVICE_STOP_SIGTERM:
845                 case SERVICE_STOP_SIGKILL:
846
847                         if (!control_pid_good(s))
848                                 service_enter_stop_post(s, success);
849
850                         /* If there is still a control process, wait for that first */
851                         break;
852
853                 default:
854                         assert_not_reached("Uh, main process died at wrong time.");
855                 }
856
857         } else if (s->control_pid == pid) {
858                 assert(s->control_command);
859
860                 exec_status_fill(&s->control_command->exec_status, pid, code, status);
861                 s->control_pid = 0;
862
863                 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
864
865                 /* If we are shutting things down anyway we
866                  * don't care about failing commands. */
867
868                 if (s->control_command->command_next &&
869                     (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
870
871                         /* There is another command to *
872                          * execute, so let's do that. */
873
874                         service_run_next(s, success);
875
876                 else {
877                         /* No further commands for this step, so let's
878                          * figure out what to do next */
879
880                         log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
881
882                         switch (s->state) {
883
884                         case SERVICE_START_PRE:
885                                 if (success)
886                                         service_enter_start(s);
887                                 else
888                                         service_enter_stop(s, false);
889                                 break;
890
891                         case SERVICE_START:
892                                 assert(s->type == SERVICE_FORKING);
893
894                                 /* Let's try to load the pid
895                                  * file here if we can. We
896                                  * ignore the return value,
897                                  * since the PID file might
898                                  * actually be created by a
899                                  * START_POST script */
900
901                                 if (success) {
902                                         if (s->pid_file)
903                                                 service_load_pid_file(s);
904
905                                         service_enter_start_post(s);
906                                 } else
907                                         service_enter_stop(s, false);
908
909                                 break;
910
911                         case SERVICE_START_POST:
912                                 if (success && s->pid_file && !s->main_pid_known) {
913                                         int r;
914
915                                         /* Hmm, let's see if we can
916                                          * load the pid now after the
917                                          * start-post scripts got
918                                          * executed. */
919
920                                         if ((r = service_load_pid_file(s)) < 0)
921                                                 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
922                                 }
923
924                                 /* Fall through */
925
926                         case SERVICE_RELOAD:
927                                 if (success) {
928                                         if (main_pid_good(s) != 0)
929                                                 service_set_state(s, SERVICE_RUNNING);
930                                         else
931                                                 service_enter_stop(s, true);
932                                 } else
933                                         service_enter_stop(s, false);
934
935                                 break;
936
937                         case SERVICE_STOP:
938                                 if (main_pid_good(s) > 0)
939                                         /* Still not dead and we know the PID? Let's go hunting. */
940                                         service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
941                                 else
942                                         service_enter_stop_post(s, success);
943                                 break;
944
945                         case SERVICE_STOP_SIGTERM:
946                         case SERVICE_STOP_SIGKILL:
947                                 if (main_pid_good(s) <= 0)
948                                         service_enter_stop_post(s, success);
949
950                                 /* If there is still a service
951                                  * process around, wait until
952                                  * that one quit, too */
953                                 break;
954
955                         case SERVICE_STOP_POST:
956                         case SERVICE_FINAL_SIGTERM:
957                         case SERVICE_FINAL_SIGKILL:
958                                 service_enter_dead(s, success, true);
959                                 break;
960
961                         default:
962                                 assert_not_reached("Uh, control process died at wrong time.");
963                         }
964                 }
965         } else
966                 assert_not_reached("Got SIGCHLD for unkown PID");
967 }
968
969 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
970         Service *s = SERVICE(u);
971
972         assert(s);
973         assert(elapsed == 1);
974
975         assert(w == &s->timer_watch);
976
977         switch (s->state) {
978
979         case SERVICE_START_PRE:
980         case SERVICE_START:
981         case SERVICE_START_POST:
982         case SERVICE_RELOAD:
983                 log_warning("%s operation timed out. Stopping.", unit_id(u));
984                 service_enter_stop(s, false);
985                 break;
986
987         case SERVICE_STOP:
988                 log_warning("%s stopping timed out. Terminating.", unit_id(u));
989                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
990                 break;
991
992         case SERVICE_STOP_SIGTERM:
993                 log_warning("%s stopping timed out. Killing.", unit_id(u));
994                 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
995                 break;
996
997         case SERVICE_STOP_SIGKILL:
998                 /* Uh, wie sent a SIGKILL and it is still not gone?
999                  * Must be something we cannot kill, so let's just be
1000                  * weirded out and continue */
1001
1002                 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1003                 service_enter_stop_post(s, false);
1004                 break;
1005
1006         case SERVICE_STOP_POST:
1007                 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1008                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1009                 break;
1010
1011         case SERVICE_FINAL_SIGTERM:
1012                 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1013                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1014                 break;
1015
1016         case SERVICE_FINAL_SIGKILL:
1017                 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1018                 service_enter_dead(s, false, true);
1019                 break;
1020
1021         case SERVICE_AUTO_RESTART:
1022                 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1023                 service_enter_restart(s);
1024                 break;
1025
1026         default:
1027                 assert_not_reached("Timeout at wrong time.");
1028         }
1029 }
1030
1031 const UnitVTable service_vtable = {
1032         .suffix = ".service",
1033
1034         .init = service_init,
1035         .done = service_done,
1036
1037         .dump = service_dump,
1038
1039         .start = service_start,
1040         .stop = service_stop,
1041         .reload = service_reload,
1042
1043         .can_reload = service_can_reload,
1044
1045         .active_state = service_active_state,
1046
1047         .sigchld_event = service_sigchld_event,
1048         .timer_event = service_timer_event,
1049 };