1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #define COMMENTS "#;\n"
37 #define NEWLINES "\n\r"
40 typedef enum RunlevelType
{
49 const RunlevelType type
;
51 /* Standard SysV runlevels */
52 { "rc0.d", SPECIAL_RUNLEVEL0_TARGET
, RUNLEVEL_DOWN
},
53 { "rc1.d", SPECIAL_RUNLEVEL1_TARGET
, RUNLEVEL_UP
},
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET
, RUNLEVEL_UP
},
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET
, RUNLEVEL_UP
},
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET
, RUNLEVEL_UP
},
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET
, RUNLEVEL_UP
},
58 { "rc6.d", SPECIAL_RUNLEVEL6_TARGET
, RUNLEVEL_DOWN
},
60 /* SuSE style boot.d */
61 { "boot.d", SPECIAL_BASIC_TARGET
, RUNLEVEL_BASIC
},
63 /* Debian style rcS.d */
64 { "rcS.d", SPECIAL_BASIC_TARGET
, RUNLEVEL_BASIC
},
67 #define RUNLEVELS_UP "12345"
68 /* #define RUNLEVELS_DOWN "06" */
69 /* #define RUNLEVELS_BOOT "bBsS" */
71 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
72 [SERVICE_DEAD
] = UNIT_INACTIVE
,
73 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
74 [SERVICE_START
] = UNIT_ACTIVATING
,
75 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
76 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
77 [SERVICE_EXITED
] = UNIT_ACTIVE
,
78 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
79 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
80 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
81 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
82 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
83 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
84 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
85 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
86 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
89 static void service_init(Unit
*u
) {
90 Service
*s
= SERVICE(u
);
93 assert(u
->meta
.load_state
== UNIT_STUB
);
95 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
96 s
->restart_usec
= DEFAULT_RESTART_USEC
;
97 s
->timer_watch
.type
= WATCH_INVALID
;
98 s
->sysv_start_priority
= -1;
101 exec_context_init(&s
->exec_context
);
103 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
105 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
108 static void service_unwatch_control_pid(Service
*s
) {
111 if (s
->control_pid
<= 0)
114 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
118 static void service_unwatch_main_pid(Service
*s
) {
121 if (s
->main_pid
<= 0)
124 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
128 static void service_close_socket_fd(Service
*s
) {
131 if (s
->socket_fd
< 0)
134 close_nointr_nofail(s
->socket_fd
);
138 static void service_done(Unit
*u
) {
139 Service
*s
= SERVICE(u
);
149 free(s
->sysv_runlevels
);
150 s
->sysv_runlevels
= NULL
;
152 exec_context_done(&s
->exec_context
);
153 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
154 s
->control_command
= NULL
;
156 /* This will leak a process, but at least no memory or any of
158 service_unwatch_main_pid(s
);
159 service_unwatch_control_pid(s
);
162 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
167 service_close_socket_fd(s
);
169 unit_unwatch_timer(u
, &s
->timer_watch
);
172 static char *sysv_translate_name(const char *name
) {
175 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
178 if (startswith(name
, "boot."))
179 /* Drop SuSE-style boot. prefix */
180 strcpy(stpcpy(r
, name
+ 5), ".service");
181 else if (endswith(name
, ".sh"))
182 /* Drop Debian-style .sh suffix */
183 strcpy(stpcpy(r
, name
) - 3, ".service");
185 /* Normal init scripts */
186 strcpy(stpcpy(r
, name
), ".service");
191 static int sysv_translate_facility(const char *name
, char **_r
) {
193 static const char * const table
[] = {
194 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
195 "$network", SPECIAL_NETWORK_TARGET
,
196 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
197 "$portmap", SPECIAL_RPCBIND_TARGET
,
198 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
199 "$syslog", SPECIAL_SYSLOG_TARGET
,
200 "$time", SPECIAL_RTC_SET_TARGET
206 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
207 if (streq(table
[i
], name
)) {
208 if (!(r
= strdup(table
[i
+1])))
217 if (!(r
= sysv_translate_name(name
)))
228 static int sysv_fix_order(Service
*s
) {
234 if (s
->sysv_start_priority
< 0)
237 /* For each pair of services where at least one lacks a LSB
238 * header, we use the start priority value to order things. */
240 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
244 t
= (Service
*) other
;
249 if (t
->sysv_start_priority
< 0)
252 /* If both units have modern headers we don't care
253 * about the priorities */
254 if ((!s
->sysv_path
|| s
->sysv_has_lsb
) &&
255 (!t
->sysv_path
|| t
->sysv_has_lsb
))
258 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
260 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
265 /* FIXME: Maybe we should compare the name here lexicographically? */
267 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
274 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
277 if (!(c
= new0(ExecCommand
, 1)))
280 if (!(c
->path
= strdup(path
))) {
285 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
294 static int sysv_exec_commands(Service
*s
) {
298 assert(s
->sysv_path
);
300 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
302 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
304 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
306 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
308 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
310 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
315 static int service_load_sysv_path(Service
*s
, const char *path
) {
332 if (!(f
= fopen(path
, "re"))) {
333 r
= errno
== ENOENT
? 0 : -errno
;
337 s
->type
= SERVICE_FORKING
;
338 s
->restart
= SERVICE_ONCE
;
341 if (!(s
->sysv_path
= strdup(path
))) {
347 char l
[LINE_MAX
], *t
;
349 if (!fgets(l
, sizeof(l
), f
)) {
354 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
364 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
366 s
->sysv_has_lsb
= true;
370 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
376 t
+= strspn(t
, WHITESPACE
);
378 if (state
== NORMAL
) {
380 /* Try to parse Red Hat style chkconfig headers */
382 if (startswith(t
, "chkconfig:")) {
384 char runlevels
[16], *k
;
388 if (sscanf(t
+10, "%15s %i %*i",
390 &start_priority
) != 2) {
392 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
396 /* A start priority gathered from the
397 * symlink farms is preferred over the
398 * data from the LSB header. */
399 if (start_priority
< 0 || start_priority
> 99)
400 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
401 else if (s
->sysv_start_priority
< 0)
402 s
->sysv_start_priority
= start_priority
;
404 char_array_0(runlevels
);
405 k
= delete_chars(runlevels
, WHITESPACE
"-");
410 if (!(d
= strdup(k
))) {
415 free(s
->sysv_runlevels
);
416 s
->sysv_runlevels
= d
;
419 } else if (startswith(t
, "description:")) {
421 size_t k
= strlen(t
);
424 if (t
[k
-1] == '\\') {
429 if (!(d
= strdup(strstrip(t
+12)))) {
434 free(u
->meta
.description
);
435 u
->meta
.description
= d
;
437 } else if (startswith(t
, "pidfile:")) {
444 if (!path_is_absolute(fn
)) {
445 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
449 if (!(fn
= strdup(fn
))) {
458 } else if (state
== DESCRIPTION
) {
460 /* Try to parse Red Hat style description
463 size_t k
= strlen(t
);
471 assert(u
->meta
.description
);
472 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
477 free(u
->meta
.description
);
478 u
->meta
.description
= d
;
480 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
482 if (startswith(t
, "Provides:")) {
488 FOREACH_WORD(w
, z
, t
+9, i
) {
491 if (!(n
= strndup(w
, z
))) {
496 r
= sysv_translate_facility(n
, &m
);
505 if (unit_name_to_type(m
) == UNIT_SERVICE
)
506 r
= unit_add_name(u
, m
);
508 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
, NULL
, true)) >= 0)
509 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
518 } else if (startswith(t
, "Required-Start:") ||
519 startswith(t
, "Should-Start:")) {
525 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
528 if (!(n
= strndup(w
, z
))) {
533 r
= sysv_translate_facility(n
, &m
);
542 r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
, NULL
, true);
548 } else if (startswith(t
, "Default-Start:")) {
553 k
= delete_chars(t
+14, WHITESPACE
"-");
556 if (!(d
= strdup(k
))) {
561 free(s
->sysv_runlevels
);
562 s
->sysv_runlevels
= d
;
565 } else if (startswith(t
, "Description:")) {
568 state
= LSB_DESCRIPTION
;
570 if (!(d
= strdup(strstrip(t
+12)))) {
575 free(u
->meta
.description
);
576 u
->meta
.description
= d
;
578 } else if (startswith(t
, "Short-Description:") &&
579 !u
->meta
.description
) {
582 /* We use the short description only
583 * if no long description is set. */
587 if (!(d
= strdup(strstrip(t
+18)))) {
592 u
->meta
.description
= d
;
594 } else if (state
== LSB_DESCRIPTION
) {
596 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
599 assert(u
->meta
.description
);
600 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
605 free(u
->meta
.description
);
606 u
->meta
.description
= d
;
613 if ((r
= sysv_exec_commands(s
)) < 0)
616 if (!s
->sysv_runlevels
|| chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
617 /* If there a runlevels configured for this service
618 * but none of the standard ones, then we assume this
619 * is some special kind of service (which might be
620 * needed for early boot) and don't create any links
623 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0 ||
624 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
628 /* Don't timeout special services during boot (like fsck) */
631 /* Special setting for all SysV services */
632 s
->valid_no_process
= true;
633 s
->kill_mode
= KILL_PROCESS_GROUP
;
635 u
->meta
.load_state
= UNIT_LOADED
;
646 static int service_load_sysv_name(Service
*s
, const char *name
) {
652 /* For SysV services we strip the boot. or .sh
653 * prefixes/suffixes. */
654 if (startswith(name
, "boot.") ||
655 endswith(name
, ".sh.service"))
658 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
662 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
665 assert(endswith(path
, ".service"));
666 path
[strlen(path
)-8] = 0;
668 r
= service_load_sysv_path(s
, path
);
670 if (r
>= 0 && UNIT(s
)->meta
.load_state
== UNIT_STUB
) {
671 /* Try Debian style xxx.sh source'able init scripts */
673 r
= service_load_sysv_path(s
, path
);
678 if (r
>= 0 && UNIT(s
)->meta
.load_state
== UNIT_STUB
) {
679 /* Try Suse style boot.xxx init scripts */
681 if (asprintf(&path
, "%s/boot.%s", *p
, name
) < 0)
684 path
[strlen(path
)-8] = 0;
685 r
= service_load_sysv_path(s
, path
);
692 if ((UNIT(s
)->meta
.load_state
!= UNIT_STUB
))
699 static int service_load_sysv(Service
*s
) {
706 /* Load service data from SysV init scripts, preferably with
709 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
712 if ((t
= UNIT(s
)->meta
.id
))
713 if ((r
= service_load_sysv_name(s
, t
)) < 0)
716 if (UNIT(s
)->meta
.load_state
== UNIT_STUB
)
717 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
718 if (t
== UNIT(s
)->meta
.id
)
721 if ((r
== service_load_sysv_name(s
, t
)) < 0)
724 if (UNIT(s
)->meta
.load_state
!= UNIT_STUB
)
731 static int service_add_bus_name(Service
*s
) {
738 if (asprintf(&n
, "dbus-%s.service", s
->bus_name
) < 0)
741 r
= unit_merge_by_name(UNIT(s
), n
);
747 static int service_verify(Service
*s
) {
750 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
753 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
754 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->meta
.id
);
758 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
759 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->meta
.id
);
766 static int service_load(Unit
*u
) {
768 Service
*s
= SERVICE(u
);
772 /* Load a .service file */
773 if ((r
= unit_load_fragment(u
)) < 0)
776 /* Load a classic init script as a fallback, if we couldn't find anything */
777 if (u
->meta
.load_state
== UNIT_STUB
)
778 if ((r
= service_load_sysv(s
)) < 0)
781 /* Still nothing found? Then let's give up */
782 if (u
->meta
.load_state
== UNIT_STUB
)
785 /* We were able to load something, then let's add in the
786 * dropin directories. */
787 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
790 /* This is a new unit? Then let's add in some extras */
791 if (u
->meta
.load_state
== UNIT_LOADED
) {
792 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
795 if ((r
= unit_add_default_cgroup(u
)) < 0)
798 if ((r
= sysv_fix_order(s
)) < 0)
802 if ((r
= service_add_bus_name(s
)) < 0)
805 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
810 return service_verify(s
);
813 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
815 ServiceExecCommand c
;
816 Service
*s
= SERVICE(u
);
822 p2
= strappend(prefix
, "\t");
823 prefix2
= p2
? p2
: prefix
;
826 "%sService State: %s\n"
827 "%sPermissionsStartOnly: %s\n"
828 "%sRootDirectoryStartOnly: %s\n"
829 "%sValidNoProcess: %s\n"
832 prefix
, service_state_to_string(s
->state
),
833 prefix
, yes_no(s
->permissions_start_only
),
834 prefix
, yes_no(s
->root_directory_start_only
),
835 prefix
, yes_no(s
->valid_no_process
),
836 prefix
, kill_mode_to_string(s
->kill_mode
),
837 prefix
, service_type_to_string(s
->type
));
839 if (s
->control_pid
> 0)
841 "%sControl PID: %llu\n",
842 prefix
, (unsigned long long) s
->control_pid
);
846 "%sMain PID: %llu\n",
847 prefix
, (unsigned long long) s
->main_pid
);
852 prefix
, s
->pid_file
);
857 "%sBus Name Good: %s\n",
859 prefix
, yes_no(s
->bus_name_good
));
861 exec_context_dump(&s
->exec_context
, f
, prefix
);
863 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
865 if (!s
->exec_command
[c
])
868 fprintf(f
, "%s-> %s:\n",
869 prefix
, service_exec_command_to_string(c
));
871 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
876 "%sSysV Init Script Path: %s\n"
877 "%sSysV Init Script has LSB Header: %s\n",
878 prefix
, s
->sysv_path
,
879 prefix
, yes_no(s
->sysv_has_lsb
));
881 if (s
->sysv_start_priority
>= 0)
883 "%sSysVStartPriority: %i\n",
884 prefix
, s
->sysv_start_priority
);
886 if (s
->sysv_runlevels
)
887 fprintf(f
, "%sSysVRunLevels: %s\n",
888 prefix
, s
->sysv_runlevels
);
893 static int service_load_pid_file(Service
*s
) {
900 if (s
->main_pid_known
)
903 assert(s
->main_pid
<= 0);
908 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
911 if ((r
= safe_atolu(k
, &p
)) < 0) {
916 if ((unsigned long) (pid_t
) p
!= p
)
919 if (kill((pid_t
) p
, 0) < 0 && errno
!= EPERM
) {
920 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
921 (unsigned long long) p
, s
->pid_file
);
925 if ((r
= unit_watch_pid(UNIT(s
), (pid_t
) p
)) < 0)
926 /* FIXME: we need to do something here */
929 s
->main_pid
= (pid_t
) p
;
930 s
->main_pid_known
= true;
935 static int service_get_sockets(Service
*s
, Set
**_set
) {
944 /* Collects all Socket objects that belong to this
945 * service. Note that a service might have multiple sockets
946 * via multiple names. */
948 if (!(set
= set_new(NULL
, NULL
)))
951 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
955 /* Look for all socket objects that go by any of our
956 * units and collect their fds */
958 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
963 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
969 if ((r
= set_put(set
, p
)) < 0)
981 static int service_notify_sockets_dead(Service
*s
) {
989 /* Notifies all our sockets when we die */
990 if ((r
= service_get_sockets(s
, &set
)) < 0)
993 SET_FOREACH(sock
, set
, i
)
994 socket_notify_service_dead(sock
);
1001 static void service_set_state(Service
*s
, ServiceState state
) {
1002 ServiceState old_state
;
1005 old_state
= s
->state
;
1008 if (state
!= SERVICE_START_PRE
&&
1009 state
!= SERVICE_START
&&
1010 state
!= SERVICE_START_POST
&&
1011 state
!= SERVICE_RELOAD
&&
1012 state
!= SERVICE_STOP
&&
1013 state
!= SERVICE_STOP_SIGTERM
&&
1014 state
!= SERVICE_STOP_SIGKILL
&&
1015 state
!= SERVICE_STOP_POST
&&
1016 state
!= SERVICE_FINAL_SIGTERM
&&
1017 state
!= SERVICE_FINAL_SIGKILL
&&
1018 state
!= SERVICE_AUTO_RESTART
)
1019 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1021 if (state
!= SERVICE_START
&&
1022 state
!= SERVICE_START_POST
&&
1023 state
!= SERVICE_RUNNING
&&
1024 state
!= SERVICE_RELOAD
&&
1025 state
!= SERVICE_STOP
&&
1026 state
!= SERVICE_STOP_SIGTERM
&&
1027 state
!= SERVICE_STOP_SIGKILL
)
1028 service_unwatch_main_pid(s
);
1030 if (state
!= SERVICE_START_PRE
&&
1031 state
!= SERVICE_START
&&
1032 state
!= SERVICE_START_POST
&&
1033 state
!= SERVICE_RELOAD
&&
1034 state
!= SERVICE_STOP
&&
1035 state
!= SERVICE_STOP_SIGTERM
&&
1036 state
!= SERVICE_STOP_SIGKILL
&&
1037 state
!= SERVICE_STOP_POST
&&
1038 state
!= SERVICE_FINAL_SIGTERM
&&
1039 state
!= SERVICE_FINAL_SIGKILL
) {
1040 service_unwatch_control_pid(s
);
1041 s
->control_command
= NULL
;
1042 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1045 if (state
== SERVICE_DEAD
||
1046 state
== SERVICE_STOP
||
1047 state
== SERVICE_STOP_SIGTERM
||
1048 state
== SERVICE_STOP_SIGKILL
||
1049 state
== SERVICE_STOP_POST
||
1050 state
== SERVICE_FINAL_SIGTERM
||
1051 state
== SERVICE_FINAL_SIGKILL
||
1052 state
== SERVICE_MAINTAINANCE
||
1053 state
== SERVICE_AUTO_RESTART
)
1054 service_notify_sockets_dead(s
);
1056 if (state
!= SERVICE_START_PRE
&&
1057 state
!= SERVICE_START
&&
1058 !(state
== SERVICE_DEAD
&& UNIT(s
)->meta
.job
))
1059 service_close_socket_fd(s
);
1061 if (old_state
!= state
)
1062 log_debug("%s changed %s -> %s", UNIT(s
)->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1064 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1067 static int service_coldplug(Unit
*u
) {
1068 Service
*s
= SERVICE(u
);
1072 assert(s
->state
== SERVICE_DEAD
);
1074 if (s
->deserialized_state
!= s
->state
) {
1076 if (s
->deserialized_state
== SERVICE_START_PRE
||
1077 s
->deserialized_state
== SERVICE_START
||
1078 s
->deserialized_state
== SERVICE_START_POST
||
1079 s
->deserialized_state
== SERVICE_RELOAD
||
1080 s
->deserialized_state
== SERVICE_STOP
||
1081 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1082 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1083 s
->deserialized_state
== SERVICE_STOP_POST
||
1084 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1085 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1086 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1088 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1091 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1093 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1098 if ((s
->deserialized_state
== SERVICE_START
&&
1099 (s
->type
== SERVICE_FORKING
||
1100 s
->type
== SERVICE_DBUS
)) ||
1101 s
->deserialized_state
== SERVICE_START_POST
||
1102 s
->deserialized_state
== SERVICE_RUNNING
||
1103 s
->deserialized_state
== SERVICE_RELOAD
||
1104 s
->deserialized_state
== SERVICE_STOP
||
1105 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1106 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1107 if (s
->main_pid
> 0)
1108 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1111 if (s
->deserialized_state
== SERVICE_START_PRE
||
1112 s
->deserialized_state
== SERVICE_START
||
1113 s
->deserialized_state
== SERVICE_START_POST
||
1114 s
->deserialized_state
== SERVICE_RELOAD
||
1115 s
->deserialized_state
== SERVICE_STOP
||
1116 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1117 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1118 s
->deserialized_state
== SERVICE_STOP_POST
||
1119 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1120 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1121 if (s
->control_pid
> 0)
1122 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1125 service_set_state(s
, s
->deserialized_state
);
1131 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1135 unsigned rn_fds
= 0;
1143 if ((r
= service_get_sockets(s
, &set
)) < 0)
1146 SET_FOREACH(sock
, set
, i
) {
1150 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1162 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1168 memcpy(t
, rfds
, rn_fds
);
1169 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1174 rn_fds
= rn_fds
+cn_fds
;
1192 static int service_spawn(
1197 bool apply_permissions
,
1212 if (s
->socket_fd
>= 0) {
1213 fds
= &s
->socket_fd
;
1215 } else if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1219 if (timeout
&& s
->timeout_usec
) {
1220 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1223 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1225 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1234 s
->meta
.manager
->environment
,
1237 UNIT(s
)->meta
.manager
->confirm_spawn
,
1238 UNIT(s
)->meta
.cgroup_bondings
,
1246 if (s
->socket_fd
>= 0)
1247 service_close_socket_fd(s
);
1252 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1253 /* FIXME: we need to do something here */
1264 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1269 static int main_pid_good(Service
*s
) {
1272 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1275 /* If we know the pid file, then lets just check if it is
1277 if (s
->main_pid_known
)
1278 return s
->main_pid
> 0;
1280 /* We don't know the pid */
1284 static int control_pid_good(Service
*s
) {
1287 return s
->control_pid
> 0;
1290 static int cgroup_good(Service
*s
) {
1295 if (s
->valid_no_process
)
1298 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
)) < 0)
1304 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1311 if (allow_restart
&&
1313 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1314 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1316 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1319 service_set_state(s
, SERVICE_AUTO_RESTART
);
1321 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1326 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1327 service_enter_dead(s
, false, false);
1330 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1332 static void service_enter_stop_post(Service
*s
, bool success
) {
1339 service_unwatch_control_pid(s
);
1341 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1342 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1343 if ((r
= service_spawn(s
,
1347 !s
->permissions_start_only
,
1348 !s
->root_directory_start_only
,
1349 &s
->control_pid
)) < 0)
1353 service_set_state(s
, SERVICE_STOP_POST
);
1355 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1360 log_warning("%s failed to run stop-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1361 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1364 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1373 if (s
->kill_mode
!= KILL_NONE
) {
1374 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1376 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1378 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1379 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1388 if (s
->main_pid
> 0) {
1389 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1395 if (s
->control_pid
> 0) {
1396 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1407 if (sent
&& (s
->main_pid
> 0 || s
->control_pid
> 0)) {
1408 if (s
->timeout_usec
> 0)
1409 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1412 service_set_state(s
, state
);
1413 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1414 service_enter_stop_post(s
, true);
1416 service_enter_dead(s
, true, true);
1421 log_warning("%s failed to kill processes: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1423 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1424 service_enter_stop_post(s
, false);
1426 service_enter_dead(s
, false, true);
1429 static void service_enter_stop(Service
*s
, bool success
) {
1436 service_unwatch_control_pid(s
);
1438 s
->control_command_id
= SERVICE_EXEC_STOP
;
1439 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1440 if ((r
= service_spawn(s
,
1444 !s
->permissions_start_only
,
1445 !s
->root_directory_start_only
,
1446 &s
->control_pid
)) < 0)
1449 service_set_state(s
, SERVICE_STOP
);
1451 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1456 log_warning("%s failed to run stop executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1457 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1460 static void service_enter_running(Service
*s
, bool success
) {
1466 if (main_pid_good(s
) != 0 &&
1467 cgroup_good(s
) != 0 &&
1468 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1469 service_set_state(s
, SERVICE_RUNNING
);
1470 else if (s
->valid_no_process
)
1471 service_set_state(s
, SERVICE_EXITED
);
1473 service_enter_stop(s
, true);
1476 static void service_enter_start_post(Service
*s
) {
1480 service_unwatch_control_pid(s
);
1482 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1483 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1484 if ((r
= service_spawn(s
,
1488 !s
->permissions_start_only
,
1489 !s
->root_directory_start_only
,
1490 &s
->control_pid
)) < 0)
1494 service_set_state(s
, SERVICE_START_POST
);
1496 service_enter_running(s
, true);
1501 log_warning("%s failed to run start-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1502 service_enter_stop(s
, false);
1505 static void service_enter_start(Service
*s
) {
1511 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1512 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1514 if (s
->type
== SERVICE_FORKING
)
1515 service_unwatch_control_pid(s
);
1517 service_unwatch_main_pid(s
);
1519 if ((r
= service_spawn(s
,
1520 s
->exec_command
[SERVICE_EXEC_START
],
1521 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
,
1528 if (s
->type
== SERVICE_SIMPLE
) {
1529 /* For simple services we immediately start
1530 * the START_POST binaries. */
1533 s
->main_pid_known
= true;
1535 service_enter_start_post(s
);
1537 } else if (s
->type
== SERVICE_FORKING
) {
1539 /* For forking services we wait until the start
1540 * process exited. */
1542 s
->control_pid
= pid
;
1544 s
->control_command_id
= SERVICE_EXEC_START
;
1545 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1546 service_set_state(s
, SERVICE_START
);
1548 } else if (s
->type
== SERVICE_FINISH
||
1549 s
->type
== SERVICE_DBUS
) {
1551 /* For finishing services we wait until the start
1552 * process exited, too, but it is our main process. */
1554 /* For D-Bus services we know the main pid right away,
1555 * but wait for the bus name to appear on the bus. */
1558 s
->main_pid_known
= true;
1560 service_set_state(s
, SERVICE_START
);
1562 assert_not_reached("Unknown service type");
1567 log_warning("%s failed to run start exectuable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1568 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1571 static void service_enter_start_pre(Service
*s
) {
1576 service_unwatch_control_pid(s
);
1578 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1579 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1580 if ((r
= service_spawn(s
,
1584 !s
->permissions_start_only
,
1585 !s
->root_directory_start_only
,
1586 &s
->control_pid
)) < 0)
1589 service_set_state(s
, SERVICE_START_PRE
);
1591 service_enter_start(s
);
1596 log_warning("%s failed to run start-pre executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1597 service_enter_dead(s
, false, true);
1600 static void service_enter_restart(Service
*s
) {
1604 service_enter_dead(s
, true, false);
1606 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1609 log_debug("%s scheduled restart job.", UNIT(s
)->meta
.id
);
1614 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1615 service_enter_dead(s
, false, false);
1618 static void service_enter_reload(Service
*s
) {
1623 service_unwatch_control_pid(s
);
1625 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1626 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1627 if ((r
= service_spawn(s
,
1631 !s
->permissions_start_only
,
1632 !s
->root_directory_start_only
,
1633 &s
->control_pid
)) < 0)
1636 service_set_state(s
, SERVICE_RELOAD
);
1638 service_enter_running(s
, true);
1643 log_warning("%s failed to run reload executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1644 service_enter_stop(s
, false);
1647 static void service_run_next(Service
*s
, bool success
) {
1651 assert(s
->control_command
);
1652 assert(s
->control_command
->command_next
);
1657 s
->control_command
= s
->control_command
->command_next
;
1659 service_unwatch_control_pid(s
);
1661 if ((r
= service_spawn(s
,
1665 !s
->permissions_start_only
,
1666 !s
->root_directory_start_only
,
1667 &s
->control_pid
)) < 0)
1673 log_warning("%s failed to run spawn next executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1675 if (s
->state
== SERVICE_START_PRE
)
1676 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1677 else if (s
->state
== SERVICE_STOP
)
1678 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1679 else if (s
->state
== SERVICE_STOP_POST
)
1680 service_enter_dead(s
, false, true);
1682 service_enter_stop(s
, false);
1685 static int service_start(Unit
*u
) {
1686 Service
*s
= SERVICE(u
);
1690 /* We cannot fulfill this request right now, try again later
1692 if (s
->state
== SERVICE_STOP
||
1693 s
->state
== SERVICE_STOP_SIGTERM
||
1694 s
->state
== SERVICE_STOP_SIGKILL
||
1695 s
->state
== SERVICE_STOP_POST
||
1696 s
->state
== SERVICE_FINAL_SIGTERM
||
1697 s
->state
== SERVICE_FINAL_SIGKILL
)
1700 /* Already on it! */
1701 if (s
->state
== SERVICE_START_PRE
||
1702 s
->state
== SERVICE_START
||
1703 s
->state
== SERVICE_START_POST
)
1706 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1708 /* Make sure we don't enter a busy loop of some kind. */
1709 if (!ratelimit_test(&s
->ratelimit
)) {
1710 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1715 s
->main_pid_known
= false;
1716 s
->allow_restart
= true;
1718 service_enter_start_pre(s
);
1722 static int service_stop(Unit
*u
) {
1723 Service
*s
= SERVICE(u
);
1727 /* Cannot do this now */
1728 if (s
->state
== SERVICE_START_PRE
||
1729 s
->state
== SERVICE_START
||
1730 s
->state
== SERVICE_START_POST
||
1731 s
->state
== SERVICE_RELOAD
)
1735 if (s
->state
== SERVICE_STOP
||
1736 s
->state
== SERVICE_STOP_SIGTERM
||
1737 s
->state
== SERVICE_STOP_SIGKILL
||
1738 s
->state
== SERVICE_STOP_POST
||
1739 s
->state
== SERVICE_FINAL_SIGTERM
||
1740 s
->state
== SERVICE_FINAL_SIGKILL
)
1743 if (s
->state
== SERVICE_AUTO_RESTART
) {
1744 service_set_state(s
, SERVICE_DEAD
);
1748 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1750 /* This is a user request, so don't do restarts on this
1752 s
->allow_restart
= false;
1754 service_enter_stop(s
, true);
1758 static int service_reload(Unit
*u
) {
1759 Service
*s
= SERVICE(u
);
1763 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1765 service_enter_reload(s
);
1769 static bool service_can_reload(Unit
*u
) {
1770 Service
*s
= SERVICE(u
);
1774 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1777 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1778 Service
*s
= SERVICE(u
);
1784 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1785 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1787 if (s
->control_pid
> 0)
1788 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) (s
->control_pid
));
1790 if (s
->main_pid
> 0)
1791 unit_serialize_item_format(u
, f
, "main-pid", "%u", (unsigned) (s
->main_pid
));
1793 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1795 /* There's a minor uncleanliness here: if there are multiple
1796 * commands attached here, we will start from the first one
1798 if (s
->control_command_id
>= 0)
1799 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
1801 if (s
->socket_fd
>= 0) {
1804 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
1807 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1813 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1814 Service
*s
= SERVICE(u
);
1822 if (streq(key
, "state")) {
1825 if ((state
= service_state_from_string(value
)) < 0)
1826 log_debug("Failed to parse state value %s", value
);
1828 s
->deserialized_state
= state
;
1829 } else if (streq(key
, "failure")) {
1832 if ((b
= parse_boolean(value
)) < 0)
1833 log_debug("Failed to parse failure value %s", value
);
1835 s
->failure
= b
|| s
->failure
;
1836 } else if (streq(key
, "control-pid")) {
1839 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1840 log_debug("Failed to parse control-pid value %s", value
);
1842 s
->control_pid
= (pid_t
) pid
;
1843 } else if (streq(key
, "main-pid")) {
1846 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1847 log_debug("Failed to parse main-pid value %s", value
);
1849 s
->main_pid
= (pid_t
) pid
;
1850 } else if (streq(key
, "main-pid-known")) {
1853 if ((b
= parse_boolean(value
)) < 0)
1854 log_debug("Failed to parse main-pid-known value %s", value
);
1856 s
->main_pid_known
= b
;
1857 } else if (streq(key
, "control-command")) {
1858 ServiceExecCommand id
;
1860 if ((id
= service_exec_command_from_string(value
)) < 0)
1861 log_debug("Failed to parse exec-command value %s", value
);
1863 s
->control_command_id
= id
;
1864 s
->control_command
= s
->exec_command
[id
];
1866 } else if (streq(key
, "socket-fd")) {
1869 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
1870 log_debug("Failed to parse socket-fd value %s", value
);
1873 if (s
->socket_fd
>= 0)
1874 close_nointr_nofail(s
->socket_fd
);
1875 s
->socket_fd
= fdset_remove(fds
, fd
);
1878 log_debug("Unknown serialization key '%s'", key
);
1883 static UnitActiveState
service_active_state(Unit
*u
) {
1886 return state_translation_table
[SERVICE(u
)->state
];
1889 static const char *service_sub_state_to_string(Unit
*u
) {
1892 return service_state_to_string(SERVICE(u
)->state
);
1895 static bool service_check_gc(Unit
*u
) {
1896 Service
*s
= SERVICE(u
);
1900 return !!s
->sysv_path
;
1903 static bool service_check_snapshot(Unit
*u
) {
1904 Service
*s
= SERVICE(u
);
1908 return !s
->got_socket_fd
;
1911 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1912 Service
*s
= SERVICE(u
);
1918 success
= is_clean_exit(code
, status
);
1919 s
->failure
= s
->failure
|| !success
;
1921 if (s
->main_pid
== pid
) {
1923 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1926 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1927 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1928 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1931 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1933 /* The service exited, so the service is officially
1938 case SERVICE_START_POST
:
1939 case SERVICE_RELOAD
:
1941 /* Need to wait until the operation is
1946 assert(s
->type
== SERVICE_FINISH
);
1948 /* This was our main goal, so let's go on */
1950 service_enter_start_post(s
);
1952 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1955 case SERVICE_RUNNING
:
1956 service_enter_running(s
, success
);
1959 case SERVICE_STOP_SIGTERM
:
1960 case SERVICE_STOP_SIGKILL
:
1962 if (!control_pid_good(s
))
1963 service_enter_stop_post(s
, success
);
1965 /* If there is still a control process, wait for that first */
1969 assert_not_reached("Uh, main process died at wrong time.");
1972 } else if (s
->control_pid
== pid
) {
1974 if (s
->control_command
)
1975 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1979 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1981 /* If we are shutting things down anyway we
1982 * don't care about failing commands. */
1984 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1986 /* There is another command to *
1987 * execute, so let's do that. */
1989 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1990 service_run_next(s
, success
);
1993 /* No further commands for this step, so let's
1994 * figure out what to do next */
1996 s
->control_command
= NULL
;
1997 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1999 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2003 case SERVICE_START_PRE
:
2005 service_enter_start(s
);
2007 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2011 assert(s
->type
== SERVICE_FORKING
);
2013 /* Let's try to load the pid
2014 * file here if we can. We
2015 * ignore the return value,
2016 * since the PID file might
2017 * actually be created by a
2018 * START_POST script */
2022 service_load_pid_file(s
);
2024 service_enter_start_post(s
);
2026 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2030 case SERVICE_START_POST
:
2031 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
2034 /* Hmm, let's see if we can
2035 * load the pid now after the
2036 * start-post scripts got
2039 if ((r
= service_load_pid_file(s
)) < 0)
2040 log_warning("%s: failed to load PID file %s: %s", UNIT(s
)->meta
.id
, s
->pid_file
, strerror(-r
));
2045 case SERVICE_RELOAD
:
2047 service_enter_running(s
, true);
2049 service_enter_stop(s
, false);
2054 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2057 case SERVICE_STOP_SIGTERM
:
2058 case SERVICE_STOP_SIGKILL
:
2059 if (main_pid_good(s
) <= 0)
2060 service_enter_stop_post(s
, success
);
2062 /* If there is still a service
2063 * process around, wait until
2064 * that one quit, too */
2067 case SERVICE_STOP_POST
:
2068 case SERVICE_FINAL_SIGTERM
:
2069 case SERVICE_FINAL_SIGKILL
:
2070 service_enter_dead(s
, success
, true);
2074 assert_not_reached("Uh, control process died at wrong time.");
2078 assert_not_reached("Got SIGCHLD for unkown PID");
2081 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2082 Service
*s
= SERVICE(u
);
2085 assert(elapsed
== 1);
2087 assert(w
== &s
->timer_watch
);
2091 case SERVICE_START_PRE
:
2093 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2094 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2097 case SERVICE_START_POST
:
2098 case SERVICE_RELOAD
:
2099 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2100 service_enter_stop(s
, false);
2104 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2105 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2108 case SERVICE_STOP_SIGTERM
:
2109 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2110 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2113 case SERVICE_STOP_SIGKILL
:
2114 /* Uh, wie sent a SIGKILL and it is still not gone?
2115 * Must be something we cannot kill, so let's just be
2116 * weirded out and continue */
2118 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2119 service_enter_stop_post(s
, false);
2122 case SERVICE_STOP_POST
:
2123 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2124 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2127 case SERVICE_FINAL_SIGTERM
:
2128 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2129 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2132 case SERVICE_FINAL_SIGKILL
:
2133 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
2134 service_enter_dead(s
, false, true);
2137 case SERVICE_AUTO_RESTART
:
2138 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2139 service_enter_restart(s
);
2143 assert_not_reached("Timeout at wrong time.");
2147 static void service_cgroup_notify_event(Unit
*u
) {
2148 Service
*s
= SERVICE(u
);
2152 log_debug("%s: cgroup is empty", u
->meta
.id
);
2156 /* Waiting for SIGCHLD is usually more interesting,
2157 * because it includes return codes/signals. Which is
2158 * why we ignore the cgroup events for most cases,
2159 * except when we don't know pid which to expect the
2162 case SERVICE_RUNNING
:
2163 service_enter_running(s
, true);
2171 static int service_enumerate(Manager
*m
) {
2175 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2180 STRV_FOREACH(p
, m
->sysvrcnd_path
)
2181 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
2186 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
].path
) < 0) {
2194 if (!(d
= opendir(path
))) {
2195 if (errno
!= ENOENT
)
2196 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2201 while ((de
= readdir(d
))) {
2205 if (ignore_file(de
->d_name
))
2208 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2211 if (strlen(de
->d_name
) < 4)
2214 a
= undecchar(de
->d_name
[1]);
2215 b
= undecchar(de
->d_name
[2]);
2222 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
].path
, de
->d_name
) < 0) {
2227 if (access(fpath
, X_OK
) < 0) {
2229 if (errno
!= ENOENT
)
2230 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2236 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
2241 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, &service
)) < 0) {
2242 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
2246 if (de
->d_name
[0] == 'S' &&
2247 (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_BASIC
))
2248 SERVICE(service
)->sysv_start_priority
=
2249 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority
);
2251 manager_dispatch_load_queue(m
);
2252 service
= unit_follow_merge(service
);
2254 if (de
->d_name
[0] == 'S') {
2255 Unit
*runlevel_target
;
2257 if ((r
= manager_load_unit(m
, rcnd_table
[i
].target
, NULL
, &runlevel_target
)) < 0)
2260 if ((r
= unit_add_dependency(runlevel_target
, UNIT_WANTS
, service
, true)) < 0)
2263 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, runlevel_target
, true)) < 0)
2266 } else if (de
->d_name
[0] == 'K' && rcnd_table
[i
].type
== RUNLEVEL_DOWN
) {
2267 Unit
*shutdown_target
;
2269 /* We honour K links only for
2270 * halt/reboot. For the normal
2271 * runlevels we assume the
2273 * implicitly added by the
2274 * core logic. Also, we don't
2275 * really distuingish here
2276 * between the runlevels 0 and
2277 * 6 and just add them to the
2278 * special shutdown target. */
2280 if ((r
= manager_load_unit(m
, SPECIAL_SHUTDOWN_TARGET
, NULL
, &shutdown_target
)) < 0)
2283 if ((r
= unit_add_dependency(service
, UNIT_CONFLICTS
, shutdown_target
, true)) < 0)
2286 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, shutdown_target
, true)) < 0)
2305 static void service_bus_name_owner_change(
2308 const char *old_owner
,
2309 const char *new_owner
) {
2311 Service
*s
= SERVICE(u
);
2316 assert(streq(s
->bus_name
, name
));
2317 assert(old_owner
|| new_owner
);
2319 if (old_owner
&& new_owner
)
2320 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2322 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2324 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2326 s
->bus_name_good
= !!new_owner
;
2328 if (s
->type
== SERVICE_DBUS
) {
2330 /* service_enter_running() will figure out what to
2332 if (s
->state
== SERVICE_RUNNING
)
2333 service_enter_running(s
, true);
2334 else if (s
->state
== SERVICE_START
&& new_owner
)
2335 service_enter_start_post(s
);
2337 } else if (new_owner
&&
2339 (s
->state
== SERVICE_START
||
2340 s
->state
== SERVICE_START_POST
||
2341 s
->state
== SERVICE_RUNNING
||
2342 s
->state
== SERVICE_RELOAD
)) {
2344 /* Try to acquire PID from bus service */
2345 log_debug("Trying to acquire PID from D-Bus name...");
2347 bus_query_pid(u
->meta
.manager
, name
);
2351 static void service_bus_query_pid_done(
2356 Service
*s
= SERVICE(u
);
2361 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2363 if (s
->main_pid
<= 0 &&
2364 (s
->state
== SERVICE_START
||
2365 s
->state
== SERVICE_START_POST
||
2366 s
->state
== SERVICE_RUNNING
||
2367 s
->state
== SERVICE_RELOAD
))
2371 int service_set_socket_fd(Service
*s
, int fd
) {
2375 /* This is called by the socket code when instantiating a new
2376 * service for a stream socket and the socket needs to be
2379 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
2382 if (s
->socket_fd
>= 0)
2385 if (s
->state
!= SERVICE_DEAD
)
2389 s
->got_socket_fd
= true;
2393 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2394 [SERVICE_DEAD
] = "dead",
2395 [SERVICE_START_PRE
] = "start-pre",
2396 [SERVICE_START
] = "start",
2397 [SERVICE_START_POST
] = "start-post",
2398 [SERVICE_RUNNING
] = "running",
2399 [SERVICE_EXITED
] = "exited",
2400 [SERVICE_RELOAD
] = "reload",
2401 [SERVICE_STOP
] = "stop",
2402 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2403 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2404 [SERVICE_STOP_POST
] = "stop-post",
2405 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2406 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2407 [SERVICE_MAINTAINANCE
] = "maintainance",
2408 [SERVICE_AUTO_RESTART
] = "auto-restart",
2411 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2413 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2414 [SERVICE_ONCE
] = "once",
2415 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2416 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2419 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2421 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2422 [SERVICE_FORKING
] = "forking",
2423 [SERVICE_SIMPLE
] = "simple",
2424 [SERVICE_FINISH
] = "finish",
2425 [SERVICE_DBUS
] = "dbus"
2428 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2430 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2431 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2432 [SERVICE_EXEC_START
] = "ExecStart",
2433 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2434 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2435 [SERVICE_EXEC_STOP
] = "ExecStop",
2436 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2439 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2441 const UnitVTable service_vtable
= {
2442 .suffix
= ".service",
2444 .init
= service_init
,
2445 .done
= service_done
,
2446 .load
= service_load
,
2448 .coldplug
= service_coldplug
,
2450 .dump
= service_dump
,
2452 .start
= service_start
,
2453 .stop
= service_stop
,
2454 .reload
= service_reload
,
2456 .can_reload
= service_can_reload
,
2458 .serialize
= service_serialize
,
2459 .deserialize_item
= service_deserialize_item
,
2461 .active_state
= service_active_state
,
2462 .sub_state_to_string
= service_sub_state_to_string
,
2464 .check_gc
= service_check_gc
,
2465 .check_snapshot
= service_check_snapshot
,
2467 .sigchld_event
= service_sigchld_event
,
2468 .timer_event
= service_timer_event
,
2470 .cgroup_notify_empty
= service_cgroup_notify_event
,
2472 .bus_name_owner_change
= service_bus_name_owner_change
,
2473 .bus_query_pid_done
= service_bus_query_pid_done
,
2475 .bus_message_handler
= bus_service_message_handler
,
2477 .enumerate
= service_enumerate