1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #include "load-fragment.h"
9 #include "load-dropin.h"
12 static const NameActiveState state_table
[_SERVICE_STATE_MAX
] = {
13 [SERVICE_DEAD
] = NAME_INACTIVE
,
14 [SERVICE_START_PRE
] = NAME_ACTIVATING
,
15 [SERVICE_START
] = NAME_ACTIVATING
,
16 [SERVICE_START_POST
] = NAME_ACTIVATING
,
17 [SERVICE_RUNNING
] = NAME_ACTIVE
,
18 [SERVICE_RELOAD
] = NAME_ACTIVE_RELOADING
,
19 [SERVICE_STOP
] = NAME_DEACTIVATING
,
20 [SERVICE_STOP_SIGTERM
] = NAME_DEACTIVATING
,
21 [SERVICE_STOP_SIGKILL
] = NAME_DEACTIVATING
,
22 [SERVICE_STOP_POST
] = NAME_DEACTIVATING
,
23 [SERVICE_FINAL_SIGTERM
] = NAME_DEACTIVATING
,
24 [SERVICE_FINAL_SIGKILL
] = NAME_DEACTIVATING
,
25 [SERVICE_MAINTAINANCE
] = NAME_INACTIVE
,
26 [SERVICE_AUTO_RESTART
] = NAME_ACTIVATING
,
29 static void service_done(Name
*n
) {
30 Service
*s
= SERVICE(n
);
37 exec_context_done(&s
->exec_context
);
38 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_MAX
);
39 s
->control_command
= NULL
;
41 /* This will leak a process, but at least no memory or any of
43 if (s
->main_pid
> 0) {
44 name_unwatch_pid(n
, s
->main_pid
);
48 if (s
->control_pid
> 0) {
49 name_unwatch_pid(n
, s
->control_pid
);
53 name_unwatch_timer(n
, &s
->timer_id
);
56 static int service_load_sysv(Service
*s
) {
59 /* Load service data from SysV init scripts, preferably with
65 static int service_init(Name
*n
) {
67 Service
*s
= SERVICE(n
);
71 /* First, reset everything to the defaults, in case this is a
77 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
78 s
->restart_usec
= DEFAULT_RESTART_USEC
;
80 exec_context_init(&s
->exec_context
);
84 s
->state
= SERVICE_DEAD
;
86 /* Load a .service file */
87 r
= name_load_fragment(n
);
89 /* Load a classic init script as a fallback */
91 r
= service_load_sysv(s
);
98 /* Load dropin directory data */
99 if ((r
= name_load_dropin(n
)) < 0) {
107 static void service_dump(Name
*n
, FILE *f
, const char *prefix
) {
109 static const char* const state_table
[_SERVICE_STATE_MAX
] = {
110 [SERVICE_DEAD
] = "dead",
111 [SERVICE_START_PRE
] = "start-pre",
112 [SERVICE_START
] = "start",
113 [SERVICE_START_POST
] = "post",
114 [SERVICE_RUNNING
] = "running",
115 [SERVICE_RELOAD
] = "reload",
116 [SERVICE_STOP
] = "stop",
117 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
118 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
119 [SERVICE_STOP_POST
] = "stop-post",
120 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
121 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
122 [SERVICE_MAINTAINANCE
] = "maintainance",
123 [SERVICE_AUTO_RESTART
] = "auto-restart",
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",
135 ServiceExecCommand c
;
136 Service
*s
= SERVICE(n
);
141 prefix2
= strappend(prefix
, "\t");
146 "%sService State: %s\n",
147 prefix
, state_table
[s
->state
]);
152 prefix
, s
->pid_file
);
155 exec_context_dump(&s
->exec_context
, f
, prefix
);
157 for (c
= 0; c
< _SERVICE_EXEC_MAX
; c
++) {
159 if (!s
->exec_command
[c
])
162 fprintf(f
, "%s→ %s:\n",
163 prefix
, command_table
[c
]);
165 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
171 static int service_load_pid_file(Service
*s
) {
178 if (s
->main_pid_known
)
184 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
187 if ((r
= safe_atolu(k
, &p
)) < 0) {
192 if ((unsigned long) (pid_t
) p
!= p
)
196 s
->main_pid_known
= true;
201 static void service_set_state(Service
*s
, ServiceState state
) {
202 ServiceState old_state
;
205 old_state
= s
->state
;
208 if (state
!= SERVICE_START_PRE
&&
209 state
!= SERVICE_START
&&
210 state
!= SERVICE_START_POST
&&
211 state
!= SERVICE_RELOAD
&&
212 state
!= SERVICE_STOP
&&
213 state
!= SERVICE_STOP_SIGTERM
&&
214 state
!= SERVICE_STOP_SIGKILL
&&
215 state
!= SERVICE_STOP_POST
&&
216 state
!= SERVICE_FINAL_SIGTERM
&&
217 state
!= SERVICE_FINAL_SIGKILL
&&
218 state
!= SERVICE_AUTO_RESTART
)
219 name_unwatch_timer(NAME(s
), &s
->timer_id
);
221 if (state
!= SERVICE_START_POST
&&
222 state
!= SERVICE_RUNNING
&&
223 state
!= SERVICE_RELOAD
&&
224 state
!= SERVICE_STOP
&&
225 state
!= SERVICE_STOP_SIGTERM
&&
226 state
!= SERVICE_STOP_SIGKILL
)
227 if (s
->main_pid
>= 0) {
228 name_unwatch_pid(NAME(s
), s
->main_pid
);
232 if (state
!= SERVICE_START_PRE
&&
233 state
!= SERVICE_START
&&
234 state
!= SERVICE_START_POST
&&
235 state
!= SERVICE_RELOAD
&&
236 state
!= SERVICE_STOP
&&
237 state
!= SERVICE_STOP_SIGTERM
&&
238 state
!= SERVICE_STOP_SIGKILL
&&
239 state
!= SERVICE_STOP_POST
&&
240 state
!= SERVICE_FINAL_SIGTERM
&&
241 state
!= SERVICE_FINAL_SIGKILL
)
242 if (s
->control_pid
>= 0) {
243 name_unwatch_pid(NAME(s
), s
->control_pid
);
247 if (state
!= SERVICE_START_PRE
&&
248 state
!= SERVICE_START
&&
249 state
!= SERVICE_START_POST
&&
250 state
!= SERVICE_RELOAD
&&
251 state
!= SERVICE_STOP
&&
252 state
!= SERVICE_STOP_POST
)
253 s
->control_command
= NULL
;
255 name_notify(NAME(s
), state_table
[old_state
], state_table
[s
->state
]);
258 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
269 SET_FOREACH(t
, NAME(s
)->meta
.names
, i
) {
275 /* Look for all socket objects that go by any of our
276 * names and collect their fds */
278 if (!(k
= name_change_suffix(t
, ".socket"))) {
283 p
= manager_get_name(NAME(s
)->meta
.manager
, k
);
286 if ((r
= socket_collect_fds(SOCKET(p
), &cfds
, &cn_fds
)) < 0)
298 if (!(t
= new(int, rn_fds
+cn_fds
))) {
304 memcpy(t
, rfds
, rn_fds
);
305 memcpy(t
+rn_fds
, cfds
, cn_fds
);
310 rn_fds
= rn_fds
+cn_fds
;
323 static int service_spawn(Service
*s
, ExecCommand
*c
, bool timeout
, bool pass_fds
, pid_t
*_pid
) {
334 if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
338 if ((r
= name_watch_timer(NAME(s
), s
->timeout_usec
, &s
->timer_id
)) < 0)
341 name_unwatch_timer(NAME(s
), &s
->timer_id
);
343 if ((r
= exec_spawn(c
, &s
->exec_context
, fds
, n_fds
, &pid
)) < 0)
346 if ((r
= name_watch_pid(NAME(s
), pid
)) < 0)
347 /* FIXME: we need to do something here */
359 name_unwatch_timer(NAME(s
), &s
->timer_id
);
364 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
372 (s
->restart
== SERVICE_RESTART_ALWAYS
||
373 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
375 if ((r
= name_watch_timer(NAME(s
), s
->restart_usec
, &s
->timer_id
)) < 0)
378 service_set_state(s
, SERVICE_AUTO_RESTART
);
380 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
385 log_warning("%s failed to run install restart timer: %s", name_id(NAME(s
)), strerror(-r
));
386 service_enter_dead(s
, false, false);
389 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
391 static void service_enter_stop_post(Service
*s
, bool success
) {
398 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
400 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
403 service_set_state(s
, SERVICE_STOP_POST
);
405 service_enter_dead(s
, true, true);
410 log_warning("%s failed to run stop executable: %s", name_id(NAME(s
)), strerror(-r
));
411 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
414 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
423 if (s
->main_pid
> 0 || s
->control_pid
> 0) {
426 sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
429 if (s
->main_pid
> 0) {
430 if (kill(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
436 if (s
->control_pid
> 0) {
437 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
446 service_set_state(s
, state
);
448 service_enter_dead(s
, true, true);
453 log_warning("%s failed to kill processes: %s", name_id(NAME(s
)), strerror(-r
));
457 service_set_state(s
, state
);
458 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
459 service_enter_stop_post(s
, false);
461 service_enter_dead(s
, false, true);
464 static void service_enter_stop(Service
*s
, bool success
) {
471 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
473 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
476 service_set_state(s
, SERVICE_STOP
);
478 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
483 log_warning("%s failed to run stop executable: %s", name_id(NAME(s
)), strerror(-r
));
484 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
487 static void service_enter_start_post(Service
*s
) {
491 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
493 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
496 service_set_state(s
, SERVICE_START_POST
);
498 service_set_state(s
, SERVICE_RUNNING
);
503 log_warning("%s failed to run start-post executable: %s", name_id(NAME(s
)), strerror(-r
));
504 service_enter_stop(s
, false);
507 static void service_enter_start(Service
*s
) {
513 assert(s
->exec_command
[SERVICE_EXEC_START
]);
514 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
516 if ((r
= service_spawn(s
, s
->exec_command
[SERVICE_EXEC_START
], s
->type
== SERVICE_FORKING
, true, &pid
)) < 0)
519 if (s
->type
== SERVICE_SIMPLE
) {
520 /* For simple services we immediately start
521 * the START_POST binaries. */
524 s
->main_pid_known
= true;
525 service_enter_start_post(s
);
527 } else if (s
->type
== SERVICE_FORKING
) {
529 /* For forking services we wait until the start
532 s
->control_pid
= pid
;
533 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
534 service_set_state(s
, SERVICE_START
);
536 assert_not_reached("Unknown service type");
541 log_warning("%s failed to run start exectuable: %s", name_id(NAME(s
)), strerror(-r
));
542 service_enter_stop(s
, false);
545 static void service_enter_start_pre(Service
*s
) {
550 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
552 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
555 service_set_state(s
, SERVICE_START_PRE
);
557 service_enter_start(s
);
562 log_warning("%s failed to run start-pre executable: %s", name_id(NAME(s
)), strerror(-r
));
563 service_enter_dead(s
, false, true);
566 static void service_enter_restart(Service
*s
) {
570 if ((r
= manager_add_job(NAME(s
)->meta
.manager
, JOB_START
, NAME(s
), JOB_FAIL
, false, NULL
)) < 0)
573 log_debug("%s scheduled restart job.", name_id(NAME(s
)));
574 service_enter_dead(s
, true, false);
579 log_warning("%s failed to schedule restart job: %s", name_id(NAME(s
)), strerror(-r
));
580 service_enter_dead(s
, false, false);
583 static void service_enter_reload(Service
*s
) {
588 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
590 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
593 service_set_state(s
, SERVICE_RELOAD
);
595 service_set_state(s
, SERVICE_RUNNING
);
600 log_warning("%s failed to run reload executable: %s", name_id(NAME(s
)), strerror(-r
));
601 service_enter_stop(s
, false);
604 static void service_run_next(Service
*s
, bool success
) {
608 assert(s
->control_command
);
609 assert(s
->control_command
->command_next
);
614 s
->control_command
= s
->control_command
->command_next
;
616 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
622 log_warning("%s failed to run spawn next executable: %s", name_id(NAME(s
)), strerror(-r
));
624 if (s
->state
== SERVICE_STOP
)
625 service_enter_stop_post(s
, false);
626 else if (s
->state
== SERVICE_STOP_POST
)
627 service_enter_dead(s
, false, true);
629 service_enter_stop(s
, false);
632 static int service_start(Name
*n
) {
633 Service
*s
= SERVICE(n
);
637 /* We cannot fulfill this request right now, try again later
639 if (s
->state
== SERVICE_STOP
||
640 s
->state
== SERVICE_STOP_SIGTERM
||
641 s
->state
== SERVICE_STOP_SIGKILL
||
642 s
->state
== SERVICE_STOP_POST
||
643 s
->state
== SERVICE_FINAL_SIGTERM
||
644 s
->state
== SERVICE_FINAL_SIGKILL
)
648 if (s
->state
== SERVICE_START_PRE
||
649 s
->state
== SERVICE_START
||
650 s
->state
== SERVICE_START_POST
)
653 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
656 s
->main_pid_known
= false;
658 service_enter_start_pre(s
);
662 static int service_stop(Name
*n
) {
663 Service
*s
= SERVICE(n
);
667 if (s
->state
== SERVICE_START_PRE
||
668 s
->state
== SERVICE_START
||
669 s
->state
== SERVICE_START_POST
||
670 s
->state
== SERVICE_RELOAD
)
673 if (s
->state
== SERVICE_AUTO_RESTART
) {
674 service_set_state(s
, SERVICE_DEAD
);
678 assert(s
->state
== SERVICE_RUNNING
);
680 service_enter_stop(s
, true);
684 static int service_reload(Name
*n
) {
685 Service
*s
= SERVICE(n
);
689 assert(s
->state
== SERVICE_RUNNING
);
691 service_enter_reload(s
);
695 static bool service_can_reload(Name
*n
) {
696 Service
*s
= SERVICE(n
);
700 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
703 static NameActiveState
service_active_state(Name
*n
) {
706 return state_table
[SERVICE(n
)->state
];
709 static int main_pid_good(Service
*s
) {
712 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
715 /* If we know the pid file, then lets just check if it is
717 if (s
->main_pid_known
)
718 return s
->main_pid
> 0;
720 /* We don't know the pid */
724 static bool control_pid_good(Service
*s
) {
727 return s
->control_pid
> 0;
730 static void service_sigchld_event(Name
*n
, pid_t pid
, int code
, int status
) {
731 Service
*s
= SERVICE(n
);
737 success
= code
== CLD_EXITED
|| status
== 0;
738 s
->failure
= s
->failure
|| !success
;
740 if (s
->main_pid
== pid
) {
742 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
745 if (s
->type
== SERVICE_SIMPLE
) {
746 assert(s
->exec_command
[SERVICE_EXEC_START
]);
747 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
750 log_debug("%s: main process exited, code=%s status=%i", name_id(n
), sigchld_code(code
), status
);
752 /* The service exited, so the service is officially
757 case SERVICE_START_POST
:
760 /* Need to wait until the operation is
764 case SERVICE_RUNNING
:
765 service_enter_stop(s
, success
);
768 case SERVICE_STOP_SIGTERM
:
769 case SERVICE_STOP_SIGKILL
:
771 if (!control_pid_good(s
))
772 service_enter_stop_post(s
, success
);
774 /* If there is still a control process, wait for that first */
778 assert_not_reached("Uh, main process died at wrong time.");
781 } else if (s
->control_pid
== pid
) {
782 assert(s
->control_command
);
784 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
787 log_debug("%s: control process exited, code=%s status=%i", name_id(n
), sigchld_code(code
), status
);
789 /* If we are shutting things down anyway we
790 * don't care about failing commands. */
792 if (s
->control_command
->command_next
&&
793 (success
|| (s
->state
== SERVICE_EXEC_STOP
|| s
->state
== SERVICE_EXEC_STOP_POST
)))
795 /* There is another command to *
796 * execute, so let's do that. */
798 service_run_next(s
, success
);
801 /* No further commands for this step, so let's
802 * figure out what to do next */
806 case SERVICE_START_PRE
:
808 service_enter_start(s
);
810 service_enter_stop(s
, false);
814 assert(s
->type
== SERVICE_FORKING
);
816 /* Let's try to load the pid
817 * file here if we can. We
818 * ignore the return value,
819 * since the PID file might
820 * actually be created by a
821 * START_POST script */
825 service_load_pid_file(s
);
827 service_enter_start_post(s
);
829 service_enter_stop(s
, false);
833 case SERVICE_START_POST
:
834 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
837 /* Hmm, let's see if we can
838 * load the pid now after the
839 * start-post scripts got
842 if ((r
= service_load_pid_file(s
)) < 0)
843 log_warning("%s: failed to load PID file %s: %s", name_id(NAME(s
)), s
->pid_file
, strerror(-r
));
850 if (main_pid_good(s
) != 0)
851 service_set_state(s
, SERVICE_RUNNING
);
853 service_enter_stop(s
, true);
855 service_enter_stop(s
, false);
860 if (main_pid_good(s
) > 0)
861 /* Still not dead and we know the PID? Let's go hunting. */
862 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
864 service_enter_stop_post(s
, success
);
867 case SERVICE_STOP_SIGTERM
:
868 case SERVICE_STOP_SIGKILL
:
869 if (main_pid_good(s
) <= 0)
870 service_enter_stop_post(s
, success
);
872 /* If there is still a service
873 * process around, wait until
874 * that one quit, too */
877 case SERVICE_STOP_POST
:
878 case SERVICE_FINAL_SIGTERM
:
879 case SERVICE_FINAL_SIGKILL
:
880 service_enter_dead(s
, success
, true);
884 assert_not_reached("Uh, control process died at wrong time.");
888 assert_not_reached("Got SIGCHLD for unkown PID");
891 static void service_timer_event(Name
*n
, int id
, uint64_t elapsed
) {
892 Service
*s
= SERVICE(n
);
895 assert(elapsed
== 1);
897 assert(s
->timer_id
== id
);
901 case SERVICE_START_PRE
:
903 case SERVICE_START_POST
:
905 log_warning("%s operation timed out. Stopping.", name_id(n
));
906 service_enter_stop(s
, false);
910 log_warning("%s stopping timed out. Terminating.", name_id(n
));
911 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
914 case SERVICE_STOP_SIGTERM
:
915 log_warning("%s stopping timed out. Killing.", name_id(n
));
916 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
919 case SERVICE_STOP_SIGKILL
:
920 /* Uh, wie sent a SIGKILL and it is still not gone?
921 * Must be something we cannot kill, so let's just be
922 * weirded out and continue */
924 log_warning("%s still around after SIGKILL. Ignoring.", name_id(n
));
925 service_enter_stop_post(s
, false);
928 case SERVICE_STOP_POST
:
929 log_warning("%s stopping timed out (2). Terminating.", name_id(n
));
930 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
933 case SERVICE_FINAL_SIGTERM
:
934 log_warning("%s stopping timed out (2). Killing.", name_id(n
));
935 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
938 case SERVICE_FINAL_SIGKILL
:
939 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", name_id(n
));
940 service_enter_dead(s
, false, true);
943 case SERVICE_AUTO_RESTART
:
944 log_debug("%s holdoff time over, scheduling restart.", name_id(n
));
945 service_enter_restart(s
);
949 assert_not_reached("Timeout at wrong time.");
953 const NameVTable service_vtable
= {
954 .suffix
= ".service",
956 .init
= service_init
,
957 .done
= service_done
,
959 .dump
= service_dump
,
961 .start
= service_start
,
962 .stop
= service_stop
,
963 .reload
= service_reload
,
965 .can_reload
= service_can_reload
,
967 .active_state
= service_active_state
,
969 .sigchld_event
= service_sigchld_event
,
970 .timer_event
= service_timer_event
,