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 static const char * const rcnd_table
[] = {
41 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET
,
42 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET
,
43 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET
,
44 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET
,
45 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET
,
46 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET
,
47 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET
,
48 "/boot.d", SPECIAL_BASIC_TARGET
51 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
52 [SERVICE_DEAD
] = UNIT_INACTIVE
,
53 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
54 [SERVICE_START
] = UNIT_ACTIVATING
,
55 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
56 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
57 [SERVICE_EXITED
] = UNIT_ACTIVE
,
58 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
59 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
60 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
61 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
62 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
63 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
64 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
65 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
66 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
69 static void service_init(Unit
*u
) {
70 Service
*s
= SERVICE(u
);
73 assert(u
->meta
.load_state
== UNIT_STUB
);
75 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
76 s
->restart_usec
= DEFAULT_RESTART_USEC
;
77 s
->timer_watch
.type
= WATCH_INVALID
;
78 s
->sysv_start_priority
= -1;
81 exec_context_init(&s
->exec_context
);
83 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
85 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
88 static void service_unwatch_control_pid(Service
*s
) {
91 if (s
->control_pid
<= 0)
94 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
98 static void service_unwatch_main_pid(Service
*s
) {
101 if (s
->main_pid
<= 0)
104 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
108 static void service_close_socket_fd(Service
*s
) {
111 if (s
->socket_fd
< 0)
114 close_nointr_nofail(s
->socket_fd
);
118 static void service_done(Unit
*u
) {
119 Service
*s
= SERVICE(u
);
129 free(s
->sysv_runlevels
);
130 s
->sysv_runlevels
= NULL
;
132 exec_context_done(&s
->exec_context
);
133 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
134 s
->control_command
= NULL
;
136 /* This will leak a process, but at least no memory or any of
138 service_unwatch_main_pid(s
);
139 service_unwatch_control_pid(s
);
142 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
147 service_close_socket_fd(s
);
149 unit_unwatch_timer(u
, &s
->timer_watch
);
152 static int sysv_translate_name(const char *name
, char **_r
) {
154 static const char * const table
[] = {
155 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
156 "$network", SPECIAL_NETWORK_TARGET
,
157 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
158 "$portmap", SPECIAL_RPCBIND_TARGET
,
159 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
160 "$syslog", SPECIAL_SYSLOG_TARGET
,
161 "$time", SPECIAL_RTC_SET_TARGET
167 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
168 if (streq(table
[i
], name
)) {
169 if (!(r
= strdup(table
[i
+1])))
178 if (asprintf(&r
, "%s.service", name
) < 0)
189 static int sysv_chkconfig_order(Service
*s
) {
195 if (s
->sysv_start_priority
< 0)
198 /* For each pair of services where at least one lacks a LSB
199 * header, we use the start priority value to order things. */
201 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
205 t
= (Service
*) other
;
210 if (t
->sysv_start_priority
< 0)
213 /* If both units have modern headers we don't care
214 * about the priorities */
215 if ((!s
->sysv_path
|| s
->sysv_has_lsb
) &&
216 (!t
->sysv_path
|| t
->sysv_has_lsb
))
219 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
221 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
226 /* FIXME: Maybe we should compare the name here lexicographically? */
228 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
235 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
238 if (!(c
= new0(ExecCommand
, 1)))
241 if (!(c
->path
= strdup(path
))) {
246 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
255 static int sysv_exec_commands(Service
*s
) {
259 assert(s
->sysv_path
);
261 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
263 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
265 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
267 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
269 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
271 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
276 static int service_load_sysv_path(Service
*s
, const char *path
) {
294 if (!(f
= fopen(path
, "re"))) {
295 r
= errno
== ENOENT
? 0 : -errno
;
299 s
->type
= SERVICE_FORKING
;
300 s
->restart
= SERVICE_ONCE
;
303 if (!(s
->sysv_path
= strdup(path
))) {
309 char l
[LINE_MAX
], *t
;
311 if (!fgets(l
, sizeof(l
), f
)) {
316 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
326 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
328 s
->sysv_has_lsb
= true;
332 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
338 t
+= strspn(t
, WHITESPACE
);
340 if (state
== NORMAL
) {
342 /* Try to parse Red Hat style chkconfig headers */
344 if (startswith(t
, "chkconfig:")) {
346 char runlevels
[16], *k
;
350 if (sscanf(t
+10, "%15s %i %*i",
352 &start_priority
) != 2) {
354 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
358 if (start_priority
< 0 || start_priority
> 99)
359 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
360 else if (s
->sysv_start_priority
< 0)
361 s
->sysv_start_priority
= start_priority
;
363 char_array_0(runlevels
);
364 k
= delete_chars(runlevels
, WHITESPACE
"-");
369 if (!(d
= strdup(k
))) {
374 free(s
->sysv_runlevels
);
375 s
->sysv_runlevels
= d
;
378 } else if (startswith(t
, "description:")) {
380 size_t k
= strlen(t
);
383 if (t
[k
-1] == '\\') {
388 if (!(d
= strdup(strstrip(t
+12)))) {
393 free(u
->meta
.description
);
394 u
->meta
.description
= d
;
396 } else if (startswith(t
, "pidfile:")) {
403 if (!path_is_absolute(fn
)) {
404 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
408 if (!(fn
= strdup(fn
))) {
417 } else if (state
== DESCRIPTION
) {
419 /* Try to parse Red Hat style description
422 size_t k
= strlen(t
);
430 assert(u
->meta
.description
);
431 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
436 free(u
->meta
.description
);
437 u
->meta
.description
= d
;
439 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
441 if (startswith(t
, "Provides:")) {
447 FOREACH_WORD(w
, z
, t
+9, i
) {
450 if (!(n
= strndup(w
, z
))) {
455 r
= sysv_translate_name(n
, &m
);
464 if (unit_name_to_type(m
) == UNIT_SERVICE
)
465 r
= unit_add_name(u
, m
);
467 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
, NULL
, true)) >= 0)
468 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
477 } else if (startswith(t
, "Required-Start:") ||
478 startswith(t
, "Should-Start:")) {
484 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
487 if (!(n
= strndup(w
, z
))) {
492 r
= sysv_translate_name(n
, &m
);
501 r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
, NULL
, true);
507 } else if (startswith(t
, "Default-Start:")) {
512 k
= delete_chars(t
+14, WHITESPACE
"-");
515 if (!(d
= strdup(k
))) {
520 free(s
->sysv_runlevels
);
521 s
->sysv_runlevels
= d
;
524 } else if (startswith(t
, "Description:")) {
527 state
= LSB_DESCRIPTION
;
529 if (!(d
= strdup(strstrip(t
+12)))) {
534 free(u
->meta
.description
);
535 u
->meta
.description
= d
;
537 } else if (startswith(t
, "Short-Description:") &&
538 !u
->meta
.description
) {
541 /* We use the short description only
542 * if no long description is set. */
546 if (!(d
= strdup(strstrip(t
+18)))) {
551 u
->meta
.description
= d
;
553 } else if (state
== LSB_DESCRIPTION
) {
555 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
558 assert(u
->meta
.description
);
559 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
564 free(u
->meta
.description
);
565 u
->meta
.description
= d
;
572 /* If init scripts have no LSB header, then we enforce the
573 * ordering via the chkconfig priorities. We try to determine
574 * a priority for *all* init scripts here, since they are
575 * needed as soon as at least one non-LSB script is used. */
577 if (s
->sysv_start_priority
< 0)
578 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u
->meta
.id
);
580 if ((r
= sysv_exec_commands(s
)) < 0)
583 normal_service
= !s
->sysv_runlevels
|| chars_intersect("12345", s
->sysv_runlevels
);
585 if (normal_service
) {
586 /* If there a runlevels configured for this service
587 * but none of the standard ones, then we assume this
588 * is some special kind of service (which might be
589 * needed for early boot) and don't create any links
592 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0 ||
593 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
597 /* Special setting for all SysV services */
598 s
->valid_no_process
= true;
599 s
->kill_mode
= KILL_PROCESS_GROUP
;
601 /* Don't timeout special services during boot (like fsck) */
602 if (s
->sysv_runlevels
&& !normal_service
)
605 u
->meta
.load_state
= UNIT_LOADED
;
616 static int service_load_sysv_name(Service
*s
, const char *name
) {
622 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
626 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
629 assert(endswith(path
, ".service"));
630 path
[strlen(path
)-8] = 0;
632 r
= service_load_sysv_path(s
, path
);
638 if ((UNIT(s
)->meta
.load_state
!= UNIT_STUB
))
645 static int service_load_sysv(Service
*s
) {
652 /* Load service data from SysV init scripts, preferably with
655 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
658 if ((t
= UNIT(s
)->meta
.id
))
659 if ((r
= service_load_sysv_name(s
, t
)) < 0)
662 if (UNIT(s
)->meta
.load_state
== UNIT_STUB
)
663 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
664 if (t
== UNIT(s
)->meta
.id
)
667 if ((r
== service_load_sysv_name(s
, t
)) < 0)
670 if (UNIT(s
)->meta
.load_state
!= UNIT_STUB
)
677 static int service_add_bus_name(Service
*s
) {
684 if (asprintf(&n
, "dbus-%s.service", s
->bus_name
) < 0)
687 r
= unit_merge_by_name(UNIT(s
), n
);
693 static int service_verify(Service
*s
) {
696 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
699 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
700 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->meta
.id
);
704 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
705 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->meta
.id
);
712 static int service_load(Unit
*u
) {
714 Service
*s
= SERVICE(u
);
718 /* Load a .service file */
719 if ((r
= unit_load_fragment(u
)) < 0)
722 /* Load a classic init script as a fallback, if we couldn't find anything */
723 if (u
->meta
.load_state
== UNIT_STUB
)
724 if ((r
= service_load_sysv(s
)) < 0)
727 /* Still nothing found? Then let's give up */
728 if (u
->meta
.load_state
== UNIT_STUB
)
731 /* We were able to load something, then let's add in the
732 * dropin directories. */
733 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
736 /* This is a new unit? Then let's add in some extras */
737 if (u
->meta
.load_state
== UNIT_LOADED
) {
738 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
741 if ((r
= unit_add_default_cgroup(u
)) < 0)
744 if ((r
= sysv_chkconfig_order(s
)) < 0)
748 if ((r
= service_add_bus_name(s
)) < 0)
751 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
756 return service_verify(s
);
759 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
761 ServiceExecCommand c
;
762 Service
*s
= SERVICE(u
);
768 p2
= strappend(prefix
, "\t");
769 prefix2
= p2
? p2
: prefix
;
772 "%sService State: %s\n"
773 "%sPermissionsStartOnly: %s\n"
774 "%sRootDirectoryStartOnly: %s\n"
775 "%sValidNoProcess: %s\n"
778 prefix
, service_state_to_string(s
->state
),
779 prefix
, yes_no(s
->permissions_start_only
),
780 prefix
, yes_no(s
->root_directory_start_only
),
781 prefix
, yes_no(s
->valid_no_process
),
782 prefix
, kill_mode_to_string(s
->kill_mode
),
783 prefix
, service_type_to_string(s
->type
));
785 if (s
->control_pid
> 0)
787 "%sControl PID: %llu\n",
788 prefix
, (unsigned long long) s
->control_pid
);
792 "%sMain PID: %llu\n",
793 prefix
, (unsigned long long) s
->main_pid
);
798 prefix
, s
->pid_file
);
803 "%sBus Name Good: %s\n",
805 prefix
, yes_no(s
->bus_name_good
));
807 exec_context_dump(&s
->exec_context
, f
, prefix
);
809 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
811 if (!s
->exec_command
[c
])
814 fprintf(f
, "%s-> %s:\n",
815 prefix
, service_exec_command_to_string(c
));
817 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
822 "%sSysV Init Script Path: %s\n"
823 "%sSysV Init Script has LSB Header: %s\n",
824 prefix
, s
->sysv_path
,
825 prefix
, yes_no(s
->sysv_has_lsb
));
827 if (s
->sysv_start_priority
>= 0)
829 "%sSysVStartPriority: %i\n",
830 prefix
, s
->sysv_start_priority
);
832 if (s
->sysv_runlevels
)
833 fprintf(f
, "%sSysVRunLevels: %s\n",
834 prefix
, s
->sysv_runlevels
);
839 static int service_load_pid_file(Service
*s
) {
846 if (s
->main_pid_known
)
849 assert(s
->main_pid
<= 0);
854 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
857 if ((r
= safe_atolu(k
, &p
)) < 0) {
862 if ((unsigned long) (pid_t
) p
!= p
)
865 if (kill((pid_t
) p
, 0) < 0 && errno
!= EPERM
) {
866 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
867 (unsigned long long) p
, s
->pid_file
);
871 if ((r
= unit_watch_pid(UNIT(s
), (pid_t
) p
)) < 0)
872 /* FIXME: we need to do something here */
875 s
->main_pid
= (pid_t
) p
;
876 s
->main_pid_known
= true;
881 static int service_get_sockets(Service
*s
, Set
**_set
) {
890 /* Collects all Socket objects that belong to this
891 * service. Note that a service might have multiple sockets
892 * via multiple names. */
894 if (!(set
= set_new(NULL
, NULL
)))
897 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
901 /* Look for all socket objects that go by any of our
902 * units and collect their fds */
904 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
909 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
915 if ((r
= set_put(set
, p
)) < 0)
927 static int service_notify_sockets_dead(Service
*s
) {
935 /* Notifies all our sockets when we die */
936 if ((r
= service_get_sockets(s
, &set
)) < 0)
939 SET_FOREACH(sock
, set
, i
)
940 socket_notify_service_dead(sock
);
947 static void service_set_state(Service
*s
, ServiceState state
) {
948 ServiceState old_state
;
951 old_state
= s
->state
;
954 if (state
!= SERVICE_START_PRE
&&
955 state
!= SERVICE_START
&&
956 state
!= SERVICE_START_POST
&&
957 state
!= SERVICE_RELOAD
&&
958 state
!= SERVICE_STOP
&&
959 state
!= SERVICE_STOP_SIGTERM
&&
960 state
!= SERVICE_STOP_SIGKILL
&&
961 state
!= SERVICE_STOP_POST
&&
962 state
!= SERVICE_FINAL_SIGTERM
&&
963 state
!= SERVICE_FINAL_SIGKILL
&&
964 state
!= SERVICE_AUTO_RESTART
)
965 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
967 if (state
!= SERVICE_START
&&
968 state
!= SERVICE_START_POST
&&
969 state
!= SERVICE_RUNNING
&&
970 state
!= SERVICE_RELOAD
&&
971 state
!= SERVICE_STOP
&&
972 state
!= SERVICE_STOP_SIGTERM
&&
973 state
!= SERVICE_STOP_SIGKILL
)
974 service_unwatch_main_pid(s
);
976 if (state
!= SERVICE_START_PRE
&&
977 state
!= SERVICE_START
&&
978 state
!= SERVICE_START_POST
&&
979 state
!= SERVICE_RELOAD
&&
980 state
!= SERVICE_STOP
&&
981 state
!= SERVICE_STOP_SIGTERM
&&
982 state
!= SERVICE_STOP_SIGKILL
&&
983 state
!= SERVICE_STOP_POST
&&
984 state
!= SERVICE_FINAL_SIGTERM
&&
985 state
!= SERVICE_FINAL_SIGKILL
) {
986 service_unwatch_control_pid(s
);
987 s
->control_command
= NULL
;
988 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
991 if (state
== SERVICE_DEAD
||
992 state
== SERVICE_STOP
||
993 state
== SERVICE_STOP_SIGTERM
||
994 state
== SERVICE_STOP_SIGKILL
||
995 state
== SERVICE_STOP_POST
||
996 state
== SERVICE_FINAL_SIGTERM
||
997 state
== SERVICE_FINAL_SIGKILL
||
998 state
== SERVICE_MAINTAINANCE
||
999 state
== SERVICE_AUTO_RESTART
)
1000 service_notify_sockets_dead(s
);
1002 if (state
!= SERVICE_START_PRE
&&
1003 state
!= SERVICE_START
&&
1004 !(state
== SERVICE_DEAD
&& UNIT(s
)->meta
.job
))
1005 service_close_socket_fd(s
);
1007 if (old_state
!= state
)
1008 log_debug("%s changed %s -> %s", UNIT(s
)->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1010 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1013 static int service_coldplug(Unit
*u
) {
1014 Service
*s
= SERVICE(u
);
1018 assert(s
->state
== SERVICE_DEAD
);
1020 if (s
->deserialized_state
!= s
->state
) {
1022 if (s
->deserialized_state
== SERVICE_START_PRE
||
1023 s
->deserialized_state
== SERVICE_START
||
1024 s
->deserialized_state
== SERVICE_START_POST
||
1025 s
->deserialized_state
== SERVICE_RELOAD
||
1026 s
->deserialized_state
== SERVICE_STOP
||
1027 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1028 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1029 s
->deserialized_state
== SERVICE_STOP_POST
||
1030 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1031 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1032 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1034 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1037 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1039 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1044 if ((s
->deserialized_state
== SERVICE_START
&&
1045 (s
->type
== SERVICE_FORKING
||
1046 s
->type
== SERVICE_DBUS
)) ||
1047 s
->deserialized_state
== SERVICE_START_POST
||
1048 s
->deserialized_state
== SERVICE_RUNNING
||
1049 s
->deserialized_state
== SERVICE_RELOAD
||
1050 s
->deserialized_state
== SERVICE_STOP
||
1051 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1052 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1053 if (s
->main_pid
> 0)
1054 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1057 if (s
->deserialized_state
== SERVICE_START_PRE
||
1058 s
->deserialized_state
== SERVICE_START
||
1059 s
->deserialized_state
== SERVICE_START_POST
||
1060 s
->deserialized_state
== SERVICE_RELOAD
||
1061 s
->deserialized_state
== SERVICE_STOP
||
1062 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1063 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1064 s
->deserialized_state
== SERVICE_STOP_POST
||
1065 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1066 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1067 if (s
->control_pid
> 0)
1068 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1071 service_set_state(s
, s
->deserialized_state
);
1077 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1081 unsigned rn_fds
= 0;
1089 if ((r
= service_get_sockets(s
, &set
)) < 0)
1092 SET_FOREACH(sock
, set
, i
) {
1096 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1108 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1114 memcpy(t
, rfds
, rn_fds
);
1115 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1120 rn_fds
= rn_fds
+cn_fds
;
1138 static int service_spawn(
1143 bool apply_permissions
,
1158 if (s
->socket_fd
>= 0) {
1159 fds
= &s
->socket_fd
;
1161 } else if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1165 if (timeout
&& s
->timeout_usec
) {
1166 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1169 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1171 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1182 UNIT(s
)->meta
.manager
->confirm_spawn
,
1183 UNIT(s
)->meta
.cgroup_bondings
,
1191 if (s
->socket_fd
>= 0)
1192 service_close_socket_fd(s
);
1197 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1198 /* FIXME: we need to do something here */
1209 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1214 static int main_pid_good(Service
*s
) {
1217 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1220 /* If we know the pid file, then lets just check if it is
1222 if (s
->main_pid_known
)
1223 return s
->main_pid
> 0;
1225 /* We don't know the pid */
1229 static int control_pid_good(Service
*s
) {
1232 return s
->control_pid
> 0;
1235 static int cgroup_good(Service
*s
) {
1240 if (s
->valid_no_process
)
1243 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
)) < 0)
1249 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1256 if (allow_restart
&&
1258 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1259 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1261 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1264 service_set_state(s
, SERVICE_AUTO_RESTART
);
1266 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1271 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1272 service_enter_dead(s
, false, false);
1275 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1277 static void service_enter_stop_post(Service
*s
, bool success
) {
1284 service_unwatch_control_pid(s
);
1286 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1287 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1288 if ((r
= service_spawn(s
,
1292 !s
->permissions_start_only
,
1293 !s
->root_directory_start_only
,
1294 &s
->control_pid
)) < 0)
1298 service_set_state(s
, SERVICE_STOP_POST
);
1300 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1305 log_warning("%s failed to run stop-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1306 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1309 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1318 if (s
->kill_mode
!= KILL_NONE
) {
1319 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1321 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1323 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1324 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1333 if (s
->main_pid
> 0) {
1334 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1340 if (s
->control_pid
> 0) {
1341 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1352 if (sent
&& (s
->main_pid
> 0 || s
->control_pid
> 0)) {
1353 if (s
->timeout_usec
> 0)
1354 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1357 service_set_state(s
, state
);
1358 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1359 service_enter_stop_post(s
, true);
1361 service_enter_dead(s
, true, true);
1366 log_warning("%s failed to kill processes: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1368 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1369 service_enter_stop_post(s
, false);
1371 service_enter_dead(s
, false, true);
1374 static void service_enter_stop(Service
*s
, bool success
) {
1381 service_unwatch_control_pid(s
);
1383 s
->control_command_id
= SERVICE_EXEC_STOP
;
1384 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1385 if ((r
= service_spawn(s
,
1389 !s
->permissions_start_only
,
1390 !s
->root_directory_start_only
,
1391 &s
->control_pid
)) < 0)
1394 service_set_state(s
, SERVICE_STOP
);
1396 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1401 log_warning("%s failed to run stop executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1402 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1405 static void service_enter_running(Service
*s
, bool success
) {
1411 if (main_pid_good(s
) != 0 &&
1412 cgroup_good(s
) != 0 &&
1413 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1414 service_set_state(s
, SERVICE_RUNNING
);
1415 else if (s
->valid_no_process
)
1416 service_set_state(s
, SERVICE_EXITED
);
1418 service_enter_stop(s
, true);
1421 static void service_enter_start_post(Service
*s
) {
1425 service_unwatch_control_pid(s
);
1427 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1428 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1429 if ((r
= service_spawn(s
,
1433 !s
->permissions_start_only
,
1434 !s
->root_directory_start_only
,
1435 &s
->control_pid
)) < 0)
1439 service_set_state(s
, SERVICE_START_POST
);
1441 service_enter_running(s
, true);
1446 log_warning("%s failed to run start-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1447 service_enter_stop(s
, false);
1450 static void service_enter_start(Service
*s
) {
1456 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1457 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1459 if (s
->type
== SERVICE_FORKING
)
1460 service_unwatch_control_pid(s
);
1462 service_unwatch_main_pid(s
);
1464 if ((r
= service_spawn(s
,
1465 s
->exec_command
[SERVICE_EXEC_START
],
1466 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
,
1473 if (s
->type
== SERVICE_SIMPLE
) {
1474 /* For simple services we immediately start
1475 * the START_POST binaries. */
1478 s
->main_pid_known
= true;
1480 service_enter_start_post(s
);
1482 } else if (s
->type
== SERVICE_FORKING
) {
1484 /* For forking services we wait until the start
1485 * process exited. */
1487 s
->control_pid
= pid
;
1489 s
->control_command_id
= SERVICE_EXEC_START
;
1490 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1491 service_set_state(s
, SERVICE_START
);
1493 } else if (s
->type
== SERVICE_FINISH
||
1494 s
->type
== SERVICE_DBUS
) {
1496 /* For finishing services we wait until the start
1497 * process exited, too, but it is our main process. */
1499 /* For D-Bus services we know the main pid right away,
1500 * but wait for the bus name to appear on the bus. */
1503 s
->main_pid_known
= true;
1505 service_set_state(s
, SERVICE_START
);
1507 assert_not_reached("Unknown service type");
1512 log_warning("%s failed to run start exectuable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1513 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1516 static void service_enter_start_pre(Service
*s
) {
1521 service_unwatch_control_pid(s
);
1523 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1524 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1525 if ((r
= service_spawn(s
,
1529 !s
->permissions_start_only
,
1530 !s
->root_directory_start_only
,
1531 &s
->control_pid
)) < 0)
1534 service_set_state(s
, SERVICE_START_PRE
);
1536 service_enter_start(s
);
1541 log_warning("%s failed to run start-pre executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1542 service_enter_dead(s
, false, true);
1545 static void service_enter_restart(Service
*s
) {
1549 service_enter_dead(s
, true, false);
1551 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1554 log_debug("%s scheduled restart job.", UNIT(s
)->meta
.id
);
1559 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1560 service_enter_dead(s
, false, false);
1563 static void service_enter_reload(Service
*s
) {
1568 service_unwatch_control_pid(s
);
1570 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1571 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1572 if ((r
= service_spawn(s
,
1576 !s
->permissions_start_only
,
1577 !s
->root_directory_start_only
,
1578 &s
->control_pid
)) < 0)
1581 service_set_state(s
, SERVICE_RELOAD
);
1583 service_enter_running(s
, true);
1588 log_warning("%s failed to run reload executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1589 service_enter_stop(s
, false);
1592 static void service_run_next(Service
*s
, bool success
) {
1596 assert(s
->control_command
);
1597 assert(s
->control_command
->command_next
);
1602 s
->control_command
= s
->control_command
->command_next
;
1604 service_unwatch_control_pid(s
);
1606 if ((r
= service_spawn(s
,
1610 !s
->permissions_start_only
,
1611 !s
->root_directory_start_only
,
1612 &s
->control_pid
)) < 0)
1618 log_warning("%s failed to run spawn next executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1620 if (s
->state
== SERVICE_START_PRE
)
1621 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1622 else if (s
->state
== SERVICE_STOP
)
1623 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1624 else if (s
->state
== SERVICE_STOP_POST
)
1625 service_enter_dead(s
, false, true);
1627 service_enter_stop(s
, false);
1630 static int service_start(Unit
*u
) {
1631 Service
*s
= SERVICE(u
);
1635 /* We cannot fulfill this request right now, try again later
1637 if (s
->state
== SERVICE_STOP
||
1638 s
->state
== SERVICE_STOP_SIGTERM
||
1639 s
->state
== SERVICE_STOP_SIGKILL
||
1640 s
->state
== SERVICE_STOP_POST
||
1641 s
->state
== SERVICE_FINAL_SIGTERM
||
1642 s
->state
== SERVICE_FINAL_SIGKILL
)
1645 /* Already on it! */
1646 if (s
->state
== SERVICE_START_PRE
||
1647 s
->state
== SERVICE_START
||
1648 s
->state
== SERVICE_START_POST
)
1651 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1653 /* Make sure we don't enter a busy loop of some kind. */
1654 if (!ratelimit_test(&s
->ratelimit
)) {
1655 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1660 s
->main_pid_known
= false;
1661 s
->allow_restart
= true;
1663 service_enter_start_pre(s
);
1667 static int service_stop(Unit
*u
) {
1668 Service
*s
= SERVICE(u
);
1672 /* Cannot do this now */
1673 if (s
->state
== SERVICE_START_PRE
||
1674 s
->state
== SERVICE_START
||
1675 s
->state
== SERVICE_START_POST
||
1676 s
->state
== SERVICE_RELOAD
)
1680 if (s
->state
== SERVICE_STOP
||
1681 s
->state
== SERVICE_STOP_SIGTERM
||
1682 s
->state
== SERVICE_STOP_SIGKILL
||
1683 s
->state
== SERVICE_STOP_POST
||
1684 s
->state
== SERVICE_FINAL_SIGTERM
||
1685 s
->state
== SERVICE_FINAL_SIGKILL
)
1688 if (s
->state
== SERVICE_AUTO_RESTART
) {
1689 service_set_state(s
, SERVICE_DEAD
);
1693 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1695 /* This is a user request, so don't do restarts on this
1697 s
->allow_restart
= false;
1699 service_enter_stop(s
, true);
1703 static int service_reload(Unit
*u
) {
1704 Service
*s
= SERVICE(u
);
1708 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1710 service_enter_reload(s
);
1714 static bool service_can_reload(Unit
*u
) {
1715 Service
*s
= SERVICE(u
);
1719 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1722 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1723 Service
*s
= SERVICE(u
);
1729 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1730 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1732 if (s
->control_pid
> 0)
1733 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) (s
->control_pid
));
1735 if (s
->main_pid
> 0)
1736 unit_serialize_item_format(u
, f
, "main-pid", "%u", (unsigned) (s
->main_pid
));
1738 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1740 /* There's a minor uncleanliness here: if there are multiple
1741 * commands attached here, we will start from the first one
1743 if (s
->control_command_id
>= 0)
1744 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
1746 if (s
->socket_fd
>= 0) {
1749 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
1752 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1758 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1759 Service
*s
= SERVICE(u
);
1767 if (streq(key
, "state")) {
1770 if ((state
= service_state_from_string(value
)) < 0)
1771 log_debug("Failed to parse state value %s", value
);
1773 s
->deserialized_state
= state
;
1774 } else if (streq(key
, "failure")) {
1777 if ((b
= parse_boolean(value
)) < 0)
1778 log_debug("Failed to parse failure value %s", value
);
1780 s
->failure
= b
|| s
->failure
;
1781 } else if (streq(key
, "control-pid")) {
1784 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1785 log_debug("Failed to parse control-pid value %s", value
);
1787 s
->control_pid
= (pid_t
) pid
;
1788 } else if (streq(key
, "main-pid")) {
1791 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1792 log_debug("Failed to parse main-pid value %s", value
);
1794 s
->main_pid
= (pid_t
) pid
;
1795 } else if (streq(key
, "main-pid-known")) {
1798 if ((b
= parse_boolean(value
)) < 0)
1799 log_debug("Failed to parse main-pid-known value %s", value
);
1801 s
->main_pid_known
= b
;
1802 } else if (streq(key
, "control-command")) {
1803 ServiceExecCommand id
;
1805 if ((id
= service_exec_command_from_string(value
)) < 0)
1806 log_debug("Failed to parse exec-command value %s", value
);
1808 s
->control_command_id
= id
;
1809 s
->control_command
= s
->exec_command
[id
];
1811 } else if (streq(key
, "socket-fd")) {
1814 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
1815 log_debug("Failed to parse socket-fd value %s", value
);
1818 if (s
->socket_fd
>= 0)
1819 close_nointr_nofail(s
->socket_fd
);
1820 s
->socket_fd
= fdset_remove(fds
, fd
);
1823 log_debug("Unknown serialization key '%s'", key
);
1828 static UnitActiveState
service_active_state(Unit
*u
) {
1831 return state_translation_table
[SERVICE(u
)->state
];
1834 static const char *service_sub_state_to_string(Unit
*u
) {
1837 return service_state_to_string(SERVICE(u
)->state
);
1840 static bool service_check_gc(Unit
*u
) {
1841 Service
*s
= SERVICE(u
);
1845 return !!s
->sysv_path
;
1848 static bool service_check_snapshot(Unit
*u
) {
1849 Service
*s
= SERVICE(u
);
1853 return !s
->got_socket_fd
;
1856 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1857 Service
*s
= SERVICE(u
);
1863 success
= code
== CLD_EXITED
&& status
== 0;
1864 s
->failure
= s
->failure
|| !success
;
1866 if (s
->main_pid
== pid
) {
1868 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1871 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1872 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1873 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1876 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1878 /* The service exited, so the service is officially
1883 case SERVICE_START_POST
:
1884 case SERVICE_RELOAD
:
1886 /* Need to wait until the operation is
1891 assert(s
->type
== SERVICE_FINISH
);
1893 /* This was our main goal, so let's go on */
1895 service_enter_start_post(s
);
1897 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1900 case SERVICE_RUNNING
:
1901 service_enter_running(s
, success
);
1904 case SERVICE_STOP_SIGTERM
:
1905 case SERVICE_STOP_SIGKILL
:
1907 if (!control_pid_good(s
))
1908 service_enter_stop_post(s
, success
);
1910 /* If there is still a control process, wait for that first */
1914 assert_not_reached("Uh, main process died at wrong time.");
1917 } else if (s
->control_pid
== pid
) {
1919 if (s
->control_command
)
1920 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1924 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1926 /* If we are shutting things down anyway we
1927 * don't care about failing commands. */
1929 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1931 /* There is another command to *
1932 * execute, so let's do that. */
1934 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1935 service_run_next(s
, success
);
1938 /* No further commands for this step, so let's
1939 * figure out what to do next */
1941 s
->control_command
= NULL
;
1942 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1944 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1948 case SERVICE_START_PRE
:
1950 service_enter_start(s
);
1952 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1956 assert(s
->type
== SERVICE_FORKING
);
1958 /* Let's try to load the pid
1959 * file here if we can. We
1960 * ignore the return value,
1961 * since the PID file might
1962 * actually be created by a
1963 * START_POST script */
1967 service_load_pid_file(s
);
1969 service_enter_start_post(s
);
1971 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1975 case SERVICE_START_POST
:
1976 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1979 /* Hmm, let's see if we can
1980 * load the pid now after the
1981 * start-post scripts got
1984 if ((r
= service_load_pid_file(s
)) < 0)
1985 log_warning("%s: failed to load PID file %s: %s", UNIT(s
)->meta
.id
, s
->pid_file
, strerror(-r
));
1990 case SERVICE_RELOAD
:
1992 service_enter_running(s
, true);
1994 service_enter_stop(s
, false);
1999 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2002 case SERVICE_STOP_SIGTERM
:
2003 case SERVICE_STOP_SIGKILL
:
2004 if (main_pid_good(s
) <= 0)
2005 service_enter_stop_post(s
, success
);
2007 /* If there is still a service
2008 * process around, wait until
2009 * that one quit, too */
2012 case SERVICE_STOP_POST
:
2013 case SERVICE_FINAL_SIGTERM
:
2014 case SERVICE_FINAL_SIGKILL
:
2015 service_enter_dead(s
, success
, true);
2019 assert_not_reached("Uh, control process died at wrong time.");
2023 assert_not_reached("Got SIGCHLD for unkown PID");
2026 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2027 Service
*s
= SERVICE(u
);
2030 assert(elapsed
== 1);
2032 assert(w
== &s
->timer_watch
);
2036 case SERVICE_START_PRE
:
2038 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2039 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2042 case SERVICE_START_POST
:
2043 case SERVICE_RELOAD
:
2044 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2045 service_enter_stop(s
, false);
2049 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2050 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2053 case SERVICE_STOP_SIGTERM
:
2054 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2055 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2058 case SERVICE_STOP_SIGKILL
:
2059 /* Uh, wie sent a SIGKILL and it is still not gone?
2060 * Must be something we cannot kill, so let's just be
2061 * weirded out and continue */
2063 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2064 service_enter_stop_post(s
, false);
2067 case SERVICE_STOP_POST
:
2068 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2069 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2072 case SERVICE_FINAL_SIGTERM
:
2073 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2074 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2077 case SERVICE_FINAL_SIGKILL
:
2078 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
2079 service_enter_dead(s
, false, true);
2082 case SERVICE_AUTO_RESTART
:
2083 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2084 service_enter_restart(s
);
2088 assert_not_reached("Timeout at wrong time.");
2092 static void service_cgroup_notify_event(Unit
*u
) {
2093 Service
*s
= SERVICE(u
);
2097 log_debug("%s: cgroup is empty", u
->meta
.id
);
2101 /* Waiting for SIGCHLD is usually more interesting,
2102 * because it includes return codes/signals. Which is
2103 * why we ignore the cgroup events for most cases,
2104 * except when we don't know pid which to expect the
2107 case SERVICE_RUNNING
:
2108 service_enter_running(s
, true);
2116 static int service_enumerate(Manager
*m
) {
2120 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2125 STRV_FOREACH(p
, m
->sysvrcnd_path
)
2126 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
2131 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
2139 if (!(d
= opendir(path
))) {
2140 if (errno
!= ENOENT
)
2141 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2146 while ((de
= readdir(d
))) {
2150 if (ignore_file(de
->d_name
))
2153 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2156 if (strlen(de
->d_name
) < 4)
2159 a
= undecchar(de
->d_name
[1]);
2160 b
= undecchar(de
->d_name
[2]);
2167 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
2172 if (access(fpath
, X_OK
) < 0) {
2174 if (errno
!= ENOENT
)
2175 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2182 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
2187 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, &service
)) < 0)
2190 if (de
->d_name
[0] == 'S')
2191 SERVICE(service
)->sysv_start_priority
= a
*10 + b
;
2193 manager_dispatch_load_queue(m
);
2194 service
= unit_follow_merge(service
);
2196 if (de
->d_name
[0] == 'S') {
2197 Unit
*runlevel_target
;
2199 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], NULL
, &runlevel_target
)) < 0)
2202 if ((r
= unit_add_dependency(runlevel_target
, UNIT_WANTS
, service
, true)) < 0)
2205 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, runlevel_target
, true)) < 0)
2208 } else if (de
->d_name
[0] == 'K' &&
2209 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
2210 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
2212 Unit
*shutdown_target
;
2214 /* We honour K links only for
2215 * halt/reboot. For the normal
2216 * runlevels we assume the
2218 * implicitly added by the
2219 * core logic. Also, we don't
2220 * really distuingish here
2221 * between the runlevels 0 and
2222 * 6 and just add them to the
2223 * special shutdown target. */
2225 if ((r
= manager_load_unit(m
, SPECIAL_SHUTDOWN_TARGET
, NULL
, &shutdown_target
)) < 0)
2228 if ((r
= unit_add_dependency(service
, UNIT_CONFLICTS
, shutdown_target
, true)) < 0)
2231 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, shutdown_target
, true)) < 0)
2248 static void service_bus_name_owner_change(
2251 const char *old_owner
,
2252 const char *new_owner
) {
2254 Service
*s
= SERVICE(u
);
2259 assert(streq(s
->bus_name
, name
));
2260 assert(old_owner
|| new_owner
);
2262 if (old_owner
&& new_owner
)
2263 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2265 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2267 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2269 s
->bus_name_good
= !!new_owner
;
2271 if (s
->type
== SERVICE_DBUS
) {
2273 /* service_enter_running() will figure out what to
2275 if (s
->state
== SERVICE_RUNNING
)
2276 service_enter_running(s
, true);
2277 else if (s
->state
== SERVICE_START
&& new_owner
)
2278 service_enter_start_post(s
);
2280 } else if (new_owner
&&
2282 (s
->state
== SERVICE_START
||
2283 s
->state
== SERVICE_START_POST
||
2284 s
->state
== SERVICE_RUNNING
||
2285 s
->state
== SERVICE_RELOAD
)) {
2287 /* Try to acquire PID from bus service */
2288 log_debug("Trying to acquire PID from D-Bus name...");
2290 bus_query_pid(u
->meta
.manager
, name
);
2294 static void service_bus_query_pid_done(
2299 Service
*s
= SERVICE(u
);
2304 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2306 if (s
->main_pid
<= 0 &&
2307 (s
->state
== SERVICE_START
||
2308 s
->state
== SERVICE_START_POST
||
2309 s
->state
== SERVICE_RUNNING
||
2310 s
->state
== SERVICE_RELOAD
))
2314 int service_set_socket_fd(Service
*s
, int fd
) {
2318 /* This is called by the socket code when instantiating a new
2319 * service for a stream socket and the socket needs to be
2322 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
2325 if (s
->socket_fd
>= 0)
2328 if (s
->state
!= SERVICE_DEAD
)
2332 s
->got_socket_fd
= true;
2336 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2337 [SERVICE_DEAD
] = "dead",
2338 [SERVICE_START_PRE
] = "start-pre",
2339 [SERVICE_START
] = "start",
2340 [SERVICE_START_POST
] = "start-post",
2341 [SERVICE_RUNNING
] = "running",
2342 [SERVICE_EXITED
] = "exited",
2343 [SERVICE_RELOAD
] = "reload",
2344 [SERVICE_STOP
] = "stop",
2345 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2346 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2347 [SERVICE_STOP_POST
] = "stop-post",
2348 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2349 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2350 [SERVICE_MAINTAINANCE
] = "maintainance",
2351 [SERVICE_AUTO_RESTART
] = "auto-restart",
2354 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2356 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2357 [SERVICE_ONCE
] = "once",
2358 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2359 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2362 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2364 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2365 [SERVICE_FORKING
] = "forking",
2366 [SERVICE_SIMPLE
] = "simple",
2367 [SERVICE_FINISH
] = "finish",
2368 [SERVICE_DBUS
] = "dbus"
2371 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2373 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2374 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2375 [SERVICE_EXEC_START
] = "ExecStart",
2376 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2377 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2378 [SERVICE_EXEC_STOP
] = "ExecStop",
2379 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2382 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2384 const UnitVTable service_vtable
= {
2385 .suffix
= ".service",
2387 .init
= service_init
,
2388 .done
= service_done
,
2389 .load
= service_load
,
2391 .coldplug
= service_coldplug
,
2393 .dump
= service_dump
,
2395 .start
= service_start
,
2396 .stop
= service_stop
,
2397 .reload
= service_reload
,
2399 .can_reload
= service_can_reload
,
2401 .serialize
= service_serialize
,
2402 .deserialize_item
= service_deserialize_item
,
2404 .active_state
= service_active_state
,
2405 .sub_state_to_string
= service_sub_state_to_string
,
2407 .check_gc
= service_check_gc
,
2408 .check_snapshot
= service_check_snapshot
,
2410 .sigchld_event
= service_sigchld_event
,
2411 .timer_event
= service_timer_event
,
2413 .cgroup_notify_empty
= service_cgroup_notify_event
,
2415 .bus_name_owner_change
= service_bus_name_owner_change
,
2416 .bus_query_pid_done
= service_bus_query_pid_done
,
2418 .bus_message_handler
= bus_service_message_handler
,
2420 .enumerate
= service_enumerate