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