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