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