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"
41 typedef enum RunlevelType
{
50 const RunlevelType type
;
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
},
59 { "/boot.d", SPECIAL_BASIC_TARGET
, RUNLEVEL_BASIC
},
62 #define RUNLEVELS_UP "12345"
63 #define RUNLEVELS_DOWN "06"
65 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
66 [SERVICE_DEAD
] = UNIT_INACTIVE
,
67 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
68 [SERVICE_START
] = UNIT_ACTIVATING
,
69 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
70 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
71 [SERVICE_EXITED
] = UNIT_ACTIVE
,
72 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
73 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
74 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
75 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
76 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
77 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
78 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
79 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
80 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
83 static void service_init(Unit
*u
) {
84 Service
*s
= SERVICE(u
);
87 assert(u
->meta
.load_state
== UNIT_STUB
);
89 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
90 s
->restart_usec
= DEFAULT_RESTART_USEC
;
91 s
->timer_watch
.type
= WATCH_INVALID
;
92 s
->sysv_start_priority
= -1;
95 exec_context_init(&s
->exec_context
);
97 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
99 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
102 static void service_unwatch_control_pid(Service
*s
) {
105 if (s
->control_pid
<= 0)
108 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
112 static void service_unwatch_main_pid(Service
*s
) {
115 if (s
->main_pid
<= 0)
118 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
122 static void service_close_socket_fd(Service
*s
) {
125 if (s
->socket_fd
< 0)
128 close_nointr_nofail(s
->socket_fd
);
132 static void service_done(Unit
*u
) {
133 Service
*s
= SERVICE(u
);
143 free(s
->sysv_runlevels
);
144 s
->sysv_runlevels
= NULL
;
146 exec_context_done(&s
->exec_context
);
147 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
148 s
->control_command
= NULL
;
150 /* This will leak a process, but at least no memory or any of
152 service_unwatch_main_pid(s
);
153 service_unwatch_control_pid(s
);
156 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
161 service_close_socket_fd(s
);
163 unit_unwatch_timer(u
, &s
->timer_watch
);
166 static int sysv_translate_name(const char *name
, char **_r
) {
168 static const char * const table
[] = {
169 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
170 "$network", SPECIAL_NETWORK_TARGET
,
171 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
172 "$portmap", SPECIAL_RPCBIND_TARGET
,
173 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
174 "$syslog", SPECIAL_SYSLOG_TARGET
,
175 "$time", SPECIAL_RTC_SET_TARGET
181 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
182 if (streq(table
[i
], name
)) {
183 if (!(r
= strdup(table
[i
+1])))
192 if (asprintf(&r
, "%s.service", name
) < 0)
203 static int sysv_chkconfig_order(Service
*s
) {
209 if (s
->sysv_start_priority
< 0)
212 /* For each pair of services where at least one lacks a LSB
213 * header, we use the start priority value to order things. */
215 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
219 t
= (Service
*) other
;
224 if (t
->sysv_start_priority
< 0)
227 /* If both units have modern headers we don't care
228 * about the priorities */
229 if ((!s
->sysv_path
|| s
->sysv_has_lsb
) &&
230 (!t
->sysv_path
|| t
->sysv_has_lsb
))
233 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
235 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
240 /* FIXME: Maybe we should compare the name here lexicographically? */
242 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
249 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
252 if (!(c
= new0(ExecCommand
, 1)))
255 if (!(c
->path
= strdup(path
))) {
260 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
269 static int sysv_exec_commands(Service
*s
) {
273 assert(s
->sysv_path
);
275 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
277 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
279 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
281 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
283 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
285 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
290 static int service_load_sysv_path(Service
*s
, const char *path
) {
307 if (!(f
= fopen(path
, "re"))) {
308 r
= errno
== ENOENT
? 0 : -errno
;
312 s
->type
= SERVICE_FORKING
;
313 s
->restart
= SERVICE_ONCE
;
316 if (!(s
->sysv_path
= strdup(path
))) {
322 char l
[LINE_MAX
], *t
;
324 if (!fgets(l
, sizeof(l
), f
)) {
329 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
339 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
341 s
->sysv_has_lsb
= true;
345 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
351 t
+= strspn(t
, WHITESPACE
);
353 if (state
== NORMAL
) {
355 /* Try to parse Red Hat style chkconfig headers */
357 if (startswith(t
, "chkconfig:")) {
359 char runlevels
[16], *k
;
363 if (sscanf(t
+10, "%15s %i %*i",
365 &start_priority
) != 2) {
367 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
371 if (start_priority
< 0 || start_priority
> 99)
372 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
373 else if (s
->sysv_start_priority
< 0)
374 s
->sysv_start_priority
= start_priority
;
376 char_array_0(runlevels
);
377 k
= delete_chars(runlevels
, WHITESPACE
"-");
382 if (!(d
= strdup(k
))) {
387 free(s
->sysv_runlevels
);
388 s
->sysv_runlevels
= d
;
391 } else if (startswith(t
, "description:")) {
393 size_t k
= strlen(t
);
396 if (t
[k
-1] == '\\') {
401 if (!(d
= strdup(strstrip(t
+12)))) {
406 free(u
->meta
.description
);
407 u
->meta
.description
= d
;
409 } else if (startswith(t
, "pidfile:")) {
416 if (!path_is_absolute(fn
)) {
417 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
421 if (!(fn
= strdup(fn
))) {
430 } else if (state
== DESCRIPTION
) {
432 /* Try to parse Red Hat style description
435 size_t k
= strlen(t
);
443 assert(u
->meta
.description
);
444 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
449 free(u
->meta
.description
);
450 u
->meta
.description
= d
;
452 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
454 if (startswith(t
, "Provides:")) {
460 FOREACH_WORD(w
, z
, t
+9, i
) {
463 if (!(n
= strndup(w
, z
))) {
468 r
= sysv_translate_name(n
, &m
);
477 if (unit_name_to_type(m
) == UNIT_SERVICE
)
478 r
= unit_add_name(u
, m
);
480 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
, NULL
, true)) >= 0)
481 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
490 } else if (startswith(t
, "Required-Start:") ||
491 startswith(t
, "Should-Start:")) {
497 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
500 if (!(n
= strndup(w
, z
))) {
505 r
= sysv_translate_name(n
, &m
);
514 r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
, NULL
, true);
520 } else if (startswith(t
, "Default-Start:")) {
525 k
= delete_chars(t
+14, WHITESPACE
"-");
528 if (!(d
= strdup(k
))) {
533 free(s
->sysv_runlevels
);
534 s
->sysv_runlevels
= d
;
537 } else if (startswith(t
, "Description:")) {
540 state
= LSB_DESCRIPTION
;
542 if (!(d
= strdup(strstrip(t
+12)))) {
547 free(u
->meta
.description
);
548 u
->meta
.description
= d
;
550 } else if (startswith(t
, "Short-Description:") &&
551 !u
->meta
.description
) {
554 /* We use the short description only
555 * if no long description is set. */
559 if (!(d
= strdup(strstrip(t
+18)))) {
564 u
->meta
.description
= d
;
566 } else if (state
== LSB_DESCRIPTION
) {
568 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
571 assert(u
->meta
.description
);
572 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
577 free(u
->meta
.description
);
578 u
->meta
.description
= d
;
585 /* If init scripts have no LSB header, then we enforce the
586 * ordering via the chkconfig priorities. We try to determine
587 * a priority for *all* init scripts here, since they are
588 * needed as soon as at least one non-LSB script is used. */
590 if (s
->sysv_start_priority
< 0)
591 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u
->meta
.id
);
593 if ((r
= sysv_exec_commands(s
)) < 0)
596 if (!s
->sysv_runlevels
|| chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
597 /* If there a runlevels configured for this service
598 * but none of the standard ones, then we assume this
599 * is some special kind of service (which might be
600 * needed for early boot) and don't create any links
603 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0 ||
604 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
608 /* Don't timeout special services during boot (like fsck) */
611 /* Special setting for all SysV services */
612 s
->valid_no_process
= true;
613 s
->kill_mode
= KILL_PROCESS_GROUP
;
615 u
->meta
.load_state
= UNIT_LOADED
;
626 static int service_load_sysv_name(Service
*s
, const char *name
) {
632 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
636 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
639 assert(endswith(path
, ".service"));
640 path
[strlen(path
)-8] = 0;
642 r
= service_load_sysv_path(s
, path
);
648 if ((UNIT(s
)->meta
.load_state
!= UNIT_STUB
))
655 static int service_load_sysv(Service
*s
) {
662 /* Load service data from SysV init scripts, preferably with
665 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
668 if ((t
= UNIT(s
)->meta
.id
))
669 if ((r
= service_load_sysv_name(s
, t
)) < 0)
672 if (UNIT(s
)->meta
.load_state
== UNIT_STUB
)
673 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
674 if (t
== UNIT(s
)->meta
.id
)
677 if ((r
== service_load_sysv_name(s
, t
)) < 0)
680 if (UNIT(s
)->meta
.load_state
!= UNIT_STUB
)
687 static int service_add_bus_name(Service
*s
) {
694 if (asprintf(&n
, "dbus-%s.service", s
->bus_name
) < 0)
697 r
= unit_merge_by_name(UNIT(s
), n
);
703 static int service_verify(Service
*s
) {
706 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
709 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
710 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->meta
.id
);
714 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
715 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->meta
.id
);
722 static int service_load(Unit
*u
) {
724 Service
*s
= SERVICE(u
);
728 /* Load a .service file */
729 if ((r
= unit_load_fragment(u
)) < 0)
732 /* Load a classic init script as a fallback, if we couldn't find anything */
733 if (u
->meta
.load_state
== UNIT_STUB
)
734 if ((r
= service_load_sysv(s
)) < 0)
737 /* Still nothing found? Then let's give up */
738 if (u
->meta
.load_state
== UNIT_STUB
)
741 /* We were able to load something, then let's add in the
742 * dropin directories. */
743 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
746 /* This is a new unit? Then let's add in some extras */
747 if (u
->meta
.load_state
== UNIT_LOADED
) {
748 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
751 if ((r
= unit_add_default_cgroup(u
)) < 0)
754 if ((r
= sysv_chkconfig_order(s
)) < 0)
758 if ((r
= service_add_bus_name(s
)) < 0)
761 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
766 return service_verify(s
);
769 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
771 ServiceExecCommand c
;
772 Service
*s
= SERVICE(u
);
778 p2
= strappend(prefix
, "\t");
779 prefix2
= p2
? p2
: prefix
;
782 "%sService State: %s\n"
783 "%sPermissionsStartOnly: %s\n"
784 "%sRootDirectoryStartOnly: %s\n"
785 "%sValidNoProcess: %s\n"
788 prefix
, service_state_to_string(s
->state
),
789 prefix
, yes_no(s
->permissions_start_only
),
790 prefix
, yes_no(s
->root_directory_start_only
),
791 prefix
, yes_no(s
->valid_no_process
),
792 prefix
, kill_mode_to_string(s
->kill_mode
),
793 prefix
, service_type_to_string(s
->type
));
795 if (s
->control_pid
> 0)
797 "%sControl PID: %llu\n",
798 prefix
, (unsigned long long) s
->control_pid
);
802 "%sMain PID: %llu\n",
803 prefix
, (unsigned long long) s
->main_pid
);
808 prefix
, s
->pid_file
);
813 "%sBus Name Good: %s\n",
815 prefix
, yes_no(s
->bus_name_good
));
817 exec_context_dump(&s
->exec_context
, f
, prefix
);
819 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
821 if (!s
->exec_command
[c
])
824 fprintf(f
, "%s-> %s:\n",
825 prefix
, service_exec_command_to_string(c
));
827 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
832 "%sSysV Init Script Path: %s\n"
833 "%sSysV Init Script has LSB Header: %s\n",
834 prefix
, s
->sysv_path
,
835 prefix
, yes_no(s
->sysv_has_lsb
));
837 if (s
->sysv_start_priority
>= 0)
839 "%sSysVStartPriority: %i\n",
840 prefix
, s
->sysv_start_priority
);
842 if (s
->sysv_runlevels
)
843 fprintf(f
, "%sSysVRunLevels: %s\n",
844 prefix
, s
->sysv_runlevels
);
849 static int service_load_pid_file(Service
*s
) {
856 if (s
->main_pid_known
)
859 assert(s
->main_pid
<= 0);
864 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
867 if ((r
= safe_atolu(k
, &p
)) < 0) {
872 if ((unsigned long) (pid_t
) p
!= p
)
875 if (kill((pid_t
) p
, 0) < 0 && errno
!= EPERM
) {
876 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
877 (unsigned long long) p
, s
->pid_file
);
881 if ((r
= unit_watch_pid(UNIT(s
), (pid_t
) p
)) < 0)
882 /* FIXME: we need to do something here */
885 s
->main_pid
= (pid_t
) p
;
886 s
->main_pid_known
= true;
891 static int service_get_sockets(Service
*s
, Set
**_set
) {
900 /* Collects all Socket objects that belong to this
901 * service. Note that a service might have multiple sockets
902 * via multiple names. */
904 if (!(set
= set_new(NULL
, NULL
)))
907 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
911 /* Look for all socket objects that go by any of our
912 * units and collect their fds */
914 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
919 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
925 if ((r
= set_put(set
, p
)) < 0)
937 static int service_notify_sockets_dead(Service
*s
) {
945 /* Notifies all our sockets when we die */
946 if ((r
= service_get_sockets(s
, &set
)) < 0)
949 SET_FOREACH(sock
, set
, i
)
950 socket_notify_service_dead(sock
);
957 static void service_set_state(Service
*s
, ServiceState state
) {
958 ServiceState old_state
;
961 old_state
= s
->state
;
964 if (state
!= SERVICE_START_PRE
&&
965 state
!= SERVICE_START
&&
966 state
!= SERVICE_START_POST
&&
967 state
!= SERVICE_RELOAD
&&
968 state
!= SERVICE_STOP
&&
969 state
!= SERVICE_STOP_SIGTERM
&&
970 state
!= SERVICE_STOP_SIGKILL
&&
971 state
!= SERVICE_STOP_POST
&&
972 state
!= SERVICE_FINAL_SIGTERM
&&
973 state
!= SERVICE_FINAL_SIGKILL
&&
974 state
!= SERVICE_AUTO_RESTART
)
975 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
977 if (state
!= SERVICE_START
&&
978 state
!= SERVICE_START_POST
&&
979 state
!= SERVICE_RUNNING
&&
980 state
!= SERVICE_RELOAD
&&
981 state
!= SERVICE_STOP
&&
982 state
!= SERVICE_STOP_SIGTERM
&&
983 state
!= SERVICE_STOP_SIGKILL
)
984 service_unwatch_main_pid(s
);
986 if (state
!= SERVICE_START_PRE
&&
987 state
!= SERVICE_START
&&
988 state
!= SERVICE_START_POST
&&
989 state
!= SERVICE_RELOAD
&&
990 state
!= SERVICE_STOP
&&
991 state
!= SERVICE_STOP_SIGTERM
&&
992 state
!= SERVICE_STOP_SIGKILL
&&
993 state
!= SERVICE_STOP_POST
&&
994 state
!= SERVICE_FINAL_SIGTERM
&&
995 state
!= SERVICE_FINAL_SIGKILL
) {
996 service_unwatch_control_pid(s
);
997 s
->control_command
= NULL
;
998 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1001 if (state
== SERVICE_DEAD
||
1002 state
== SERVICE_STOP
||
1003 state
== SERVICE_STOP_SIGTERM
||
1004 state
== SERVICE_STOP_SIGKILL
||
1005 state
== SERVICE_STOP_POST
||
1006 state
== SERVICE_FINAL_SIGTERM
||
1007 state
== SERVICE_FINAL_SIGKILL
||
1008 state
== SERVICE_MAINTAINANCE
||
1009 state
== SERVICE_AUTO_RESTART
)
1010 service_notify_sockets_dead(s
);
1012 if (state
!= SERVICE_START_PRE
&&
1013 state
!= SERVICE_START
&&
1014 !(state
== SERVICE_DEAD
&& UNIT(s
)->meta
.job
))
1015 service_close_socket_fd(s
);
1017 if (old_state
!= state
)
1018 log_debug("%s changed %s -> %s", UNIT(s
)->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1020 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1023 static int service_coldplug(Unit
*u
) {
1024 Service
*s
= SERVICE(u
);
1028 assert(s
->state
== SERVICE_DEAD
);
1030 if (s
->deserialized_state
!= s
->state
) {
1032 if (s
->deserialized_state
== SERVICE_START_PRE
||
1033 s
->deserialized_state
== SERVICE_START
||
1034 s
->deserialized_state
== SERVICE_START_POST
||
1035 s
->deserialized_state
== SERVICE_RELOAD
||
1036 s
->deserialized_state
== SERVICE_STOP
||
1037 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1038 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1039 s
->deserialized_state
== SERVICE_STOP_POST
||
1040 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1041 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1042 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1044 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1047 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1049 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1054 if ((s
->deserialized_state
== SERVICE_START
&&
1055 (s
->type
== SERVICE_FORKING
||
1056 s
->type
== SERVICE_DBUS
)) ||
1057 s
->deserialized_state
== SERVICE_START_POST
||
1058 s
->deserialized_state
== SERVICE_RUNNING
||
1059 s
->deserialized_state
== SERVICE_RELOAD
||
1060 s
->deserialized_state
== SERVICE_STOP
||
1061 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1062 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1063 if (s
->main_pid
> 0)
1064 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1067 if (s
->deserialized_state
== SERVICE_START_PRE
||
1068 s
->deserialized_state
== SERVICE_START
||
1069 s
->deserialized_state
== SERVICE_START_POST
||
1070 s
->deserialized_state
== SERVICE_RELOAD
||
1071 s
->deserialized_state
== SERVICE_STOP
||
1072 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1073 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1074 s
->deserialized_state
== SERVICE_STOP_POST
||
1075 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1076 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1077 if (s
->control_pid
> 0)
1078 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1081 service_set_state(s
, s
->deserialized_state
);
1087 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1091 unsigned rn_fds
= 0;
1099 if ((r
= service_get_sockets(s
, &set
)) < 0)
1102 SET_FOREACH(sock
, set
, i
) {
1106 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1118 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1124 memcpy(t
, rfds
, rn_fds
);
1125 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1130 rn_fds
= rn_fds
+cn_fds
;
1148 static int service_spawn(
1153 bool apply_permissions
,
1168 if (s
->socket_fd
>= 0) {
1169 fds
= &s
->socket_fd
;
1171 } else if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1175 if (timeout
&& s
->timeout_usec
) {
1176 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1179 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1181 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1190 s
->meta
.manager
->environment
,
1193 UNIT(s
)->meta
.manager
->confirm_spawn
,
1194 UNIT(s
)->meta
.cgroup_bondings
,
1202 if (s
->socket_fd
>= 0)
1203 service_close_socket_fd(s
);
1208 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1209 /* FIXME: we need to do something here */
1220 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1225 static int main_pid_good(Service
*s
) {
1228 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1231 /* If we know the pid file, then lets just check if it is
1233 if (s
->main_pid_known
)
1234 return s
->main_pid
> 0;
1236 /* We don't know the pid */
1240 static int control_pid_good(Service
*s
) {
1243 return s
->control_pid
> 0;
1246 static int cgroup_good(Service
*s
) {
1251 if (s
->valid_no_process
)
1254 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
)) < 0)
1260 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1267 if (allow_restart
&&
1269 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1270 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1272 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1275 service_set_state(s
, SERVICE_AUTO_RESTART
);
1277 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1282 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1283 service_enter_dead(s
, false, false);
1286 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1288 static void service_enter_stop_post(Service
*s
, bool success
) {
1295 service_unwatch_control_pid(s
);
1297 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1298 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1299 if ((r
= service_spawn(s
,
1303 !s
->permissions_start_only
,
1304 !s
->root_directory_start_only
,
1305 &s
->control_pid
)) < 0)
1309 service_set_state(s
, SERVICE_STOP_POST
);
1311 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1316 log_warning("%s failed to run stop-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1317 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1320 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1329 if (s
->kill_mode
!= KILL_NONE
) {
1330 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1332 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1334 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1335 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1344 if (s
->main_pid
> 0) {
1345 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1351 if (s
->control_pid
> 0) {
1352 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1363 if (sent
&& (s
->main_pid
> 0 || s
->control_pid
> 0)) {
1364 if (s
->timeout_usec
> 0)
1365 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1368 service_set_state(s
, state
);
1369 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1370 service_enter_stop_post(s
, true);
1372 service_enter_dead(s
, true, true);
1377 log_warning("%s failed to kill processes: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1379 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1380 service_enter_stop_post(s
, false);
1382 service_enter_dead(s
, false, true);
1385 static void service_enter_stop(Service
*s
, bool success
) {
1392 service_unwatch_control_pid(s
);
1394 s
->control_command_id
= SERVICE_EXEC_STOP
;
1395 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1396 if ((r
= service_spawn(s
,
1400 !s
->permissions_start_only
,
1401 !s
->root_directory_start_only
,
1402 &s
->control_pid
)) < 0)
1405 service_set_state(s
, SERVICE_STOP
);
1407 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1412 log_warning("%s failed to run stop executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1413 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1416 static void service_enter_running(Service
*s
, bool success
) {
1422 if (main_pid_good(s
) != 0 &&
1423 cgroup_good(s
) != 0 &&
1424 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1425 service_set_state(s
, SERVICE_RUNNING
);
1426 else if (s
->valid_no_process
)
1427 service_set_state(s
, SERVICE_EXITED
);
1429 service_enter_stop(s
, true);
1432 static void service_enter_start_post(Service
*s
) {
1436 service_unwatch_control_pid(s
);
1438 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1439 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1440 if ((r
= service_spawn(s
,
1444 !s
->permissions_start_only
,
1445 !s
->root_directory_start_only
,
1446 &s
->control_pid
)) < 0)
1450 service_set_state(s
, SERVICE_START_POST
);
1452 service_enter_running(s
, true);
1457 log_warning("%s failed to run start-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1458 service_enter_stop(s
, false);
1461 static void service_enter_start(Service
*s
) {
1467 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1468 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1470 if (s
->type
== SERVICE_FORKING
)
1471 service_unwatch_control_pid(s
);
1473 service_unwatch_main_pid(s
);
1475 if ((r
= service_spawn(s
,
1476 s
->exec_command
[SERVICE_EXEC_START
],
1477 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
,
1484 if (s
->type
== SERVICE_SIMPLE
) {
1485 /* For simple services we immediately start
1486 * the START_POST binaries. */
1489 s
->main_pid_known
= true;
1491 service_enter_start_post(s
);
1493 } else if (s
->type
== SERVICE_FORKING
) {
1495 /* For forking services we wait until the start
1496 * process exited. */
1498 s
->control_pid
= pid
;
1500 s
->control_command_id
= SERVICE_EXEC_START
;
1501 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1502 service_set_state(s
, SERVICE_START
);
1504 } else if (s
->type
== SERVICE_FINISH
||
1505 s
->type
== SERVICE_DBUS
) {
1507 /* For finishing services we wait until the start
1508 * process exited, too, but it is our main process. */
1510 /* For D-Bus services we know the main pid right away,
1511 * but wait for the bus name to appear on the bus. */
1514 s
->main_pid_known
= true;
1516 service_set_state(s
, SERVICE_START
);
1518 assert_not_reached("Unknown service type");
1523 log_warning("%s failed to run start exectuable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1524 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1527 static void service_enter_start_pre(Service
*s
) {
1532 service_unwatch_control_pid(s
);
1534 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1535 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1536 if ((r
= service_spawn(s
,
1540 !s
->permissions_start_only
,
1541 !s
->root_directory_start_only
,
1542 &s
->control_pid
)) < 0)
1545 service_set_state(s
, SERVICE_START_PRE
);
1547 service_enter_start(s
);
1552 log_warning("%s failed to run start-pre executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1553 service_enter_dead(s
, false, true);
1556 static void service_enter_restart(Service
*s
) {
1560 service_enter_dead(s
, true, false);
1562 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1565 log_debug("%s scheduled restart job.", UNIT(s
)->meta
.id
);
1570 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1571 service_enter_dead(s
, false, false);
1574 static void service_enter_reload(Service
*s
) {
1579 service_unwatch_control_pid(s
);
1581 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1582 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1583 if ((r
= service_spawn(s
,
1587 !s
->permissions_start_only
,
1588 !s
->root_directory_start_only
,
1589 &s
->control_pid
)) < 0)
1592 service_set_state(s
, SERVICE_RELOAD
);
1594 service_enter_running(s
, true);
1599 log_warning("%s failed to run reload executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1600 service_enter_stop(s
, false);
1603 static void service_run_next(Service
*s
, bool success
) {
1607 assert(s
->control_command
);
1608 assert(s
->control_command
->command_next
);
1613 s
->control_command
= s
->control_command
->command_next
;
1615 service_unwatch_control_pid(s
);
1617 if ((r
= service_spawn(s
,
1621 !s
->permissions_start_only
,
1622 !s
->root_directory_start_only
,
1623 &s
->control_pid
)) < 0)
1629 log_warning("%s failed to run spawn next executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1631 if (s
->state
== SERVICE_START_PRE
)
1632 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1633 else if (s
->state
== SERVICE_STOP
)
1634 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1635 else if (s
->state
== SERVICE_STOP_POST
)
1636 service_enter_dead(s
, false, true);
1638 service_enter_stop(s
, false);
1641 static int service_start(Unit
*u
) {
1642 Service
*s
= SERVICE(u
);
1646 /* We cannot fulfill this request right now, try again later
1648 if (s
->state
== SERVICE_STOP
||
1649 s
->state
== SERVICE_STOP_SIGTERM
||
1650 s
->state
== SERVICE_STOP_SIGKILL
||
1651 s
->state
== SERVICE_STOP_POST
||
1652 s
->state
== SERVICE_FINAL_SIGTERM
||
1653 s
->state
== SERVICE_FINAL_SIGKILL
)
1656 /* Already on it! */
1657 if (s
->state
== SERVICE_START_PRE
||
1658 s
->state
== SERVICE_START
||
1659 s
->state
== SERVICE_START_POST
)
1662 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1664 /* Make sure we don't enter a busy loop of some kind. */
1665 if (!ratelimit_test(&s
->ratelimit
)) {
1666 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1671 s
->main_pid_known
= false;
1672 s
->allow_restart
= true;
1674 service_enter_start_pre(s
);
1678 static int service_stop(Unit
*u
) {
1679 Service
*s
= SERVICE(u
);
1683 /* Cannot do this now */
1684 if (s
->state
== SERVICE_START_PRE
||
1685 s
->state
== SERVICE_START
||
1686 s
->state
== SERVICE_START_POST
||
1687 s
->state
== SERVICE_RELOAD
)
1691 if (s
->state
== SERVICE_STOP
||
1692 s
->state
== SERVICE_STOP_SIGTERM
||
1693 s
->state
== SERVICE_STOP_SIGKILL
||
1694 s
->state
== SERVICE_STOP_POST
||
1695 s
->state
== SERVICE_FINAL_SIGTERM
||
1696 s
->state
== SERVICE_FINAL_SIGKILL
)
1699 if (s
->state
== SERVICE_AUTO_RESTART
) {
1700 service_set_state(s
, SERVICE_DEAD
);
1704 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1706 /* This is a user request, so don't do restarts on this
1708 s
->allow_restart
= false;
1710 service_enter_stop(s
, true);
1714 static int service_reload(Unit
*u
) {
1715 Service
*s
= SERVICE(u
);
1719 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1721 service_enter_reload(s
);
1725 static bool service_can_reload(Unit
*u
) {
1726 Service
*s
= SERVICE(u
);
1730 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1733 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1734 Service
*s
= SERVICE(u
);
1740 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1741 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1743 if (s
->control_pid
> 0)
1744 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) (s
->control_pid
));
1746 if (s
->main_pid
> 0)
1747 unit_serialize_item_format(u
, f
, "main-pid", "%u", (unsigned) (s
->main_pid
));
1749 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1751 /* There's a minor uncleanliness here: if there are multiple
1752 * commands attached here, we will start from the first one
1754 if (s
->control_command_id
>= 0)
1755 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
1757 if (s
->socket_fd
>= 0) {
1760 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
1763 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1769 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1770 Service
*s
= SERVICE(u
);
1778 if (streq(key
, "state")) {
1781 if ((state
= service_state_from_string(value
)) < 0)
1782 log_debug("Failed to parse state value %s", value
);
1784 s
->deserialized_state
= state
;
1785 } else if (streq(key
, "failure")) {
1788 if ((b
= parse_boolean(value
)) < 0)
1789 log_debug("Failed to parse failure value %s", value
);
1791 s
->failure
= b
|| s
->failure
;
1792 } else if (streq(key
, "control-pid")) {
1795 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1796 log_debug("Failed to parse control-pid value %s", value
);
1798 s
->control_pid
= (pid_t
) pid
;
1799 } else if (streq(key
, "main-pid")) {
1802 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1803 log_debug("Failed to parse main-pid value %s", value
);
1805 s
->main_pid
= (pid_t
) pid
;
1806 } else if (streq(key
, "main-pid-known")) {
1809 if ((b
= parse_boolean(value
)) < 0)
1810 log_debug("Failed to parse main-pid-known value %s", value
);
1812 s
->main_pid_known
= b
;
1813 } else if (streq(key
, "control-command")) {
1814 ServiceExecCommand id
;
1816 if ((id
= service_exec_command_from_string(value
)) < 0)
1817 log_debug("Failed to parse exec-command value %s", value
);
1819 s
->control_command_id
= id
;
1820 s
->control_command
= s
->exec_command
[id
];
1822 } else if (streq(key
, "socket-fd")) {
1825 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
1826 log_debug("Failed to parse socket-fd value %s", value
);
1829 if (s
->socket_fd
>= 0)
1830 close_nointr_nofail(s
->socket_fd
);
1831 s
->socket_fd
= fdset_remove(fds
, fd
);
1834 log_debug("Unknown serialization key '%s'", key
);
1839 static UnitActiveState
service_active_state(Unit
*u
) {
1842 return state_translation_table
[SERVICE(u
)->state
];
1845 static const char *service_sub_state_to_string(Unit
*u
) {
1848 return service_state_to_string(SERVICE(u
)->state
);
1851 static bool service_check_gc(Unit
*u
) {
1852 Service
*s
= SERVICE(u
);
1856 return !!s
->sysv_path
;
1859 static bool service_check_snapshot(Unit
*u
) {
1860 Service
*s
= SERVICE(u
);
1864 return !s
->got_socket_fd
;
1867 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1868 Service
*s
= SERVICE(u
);
1874 success
= code
== CLD_EXITED
&& status
== 0;
1875 s
->failure
= s
->failure
|| !success
;
1877 if (s
->main_pid
== pid
) {
1879 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1882 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1883 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1884 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1887 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1889 /* The service exited, so the service is officially
1894 case SERVICE_START_POST
:
1895 case SERVICE_RELOAD
:
1897 /* Need to wait until the operation is
1902 assert(s
->type
== SERVICE_FINISH
);
1904 /* This was our main goal, so let's go on */
1906 service_enter_start_post(s
);
1908 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1911 case SERVICE_RUNNING
:
1912 service_enter_running(s
, success
);
1915 case SERVICE_STOP_SIGTERM
:
1916 case SERVICE_STOP_SIGKILL
:
1918 if (!control_pid_good(s
))
1919 service_enter_stop_post(s
, success
);
1921 /* If there is still a control process, wait for that first */
1925 assert_not_reached("Uh, main process died at wrong time.");
1928 } else if (s
->control_pid
== pid
) {
1930 if (s
->control_command
)
1931 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1935 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1937 /* If we are shutting things down anyway we
1938 * don't care about failing commands. */
1940 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1942 /* There is another command to *
1943 * execute, so let's do that. */
1945 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1946 service_run_next(s
, success
);
1949 /* No further commands for this step, so let's
1950 * figure out what to do next */
1952 s
->control_command
= NULL
;
1953 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1955 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1959 case SERVICE_START_PRE
:
1961 service_enter_start(s
);
1963 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1967 assert(s
->type
== SERVICE_FORKING
);
1969 /* Let's try to load the pid
1970 * file here if we can. We
1971 * ignore the return value,
1972 * since the PID file might
1973 * actually be created by a
1974 * START_POST script */
1978 service_load_pid_file(s
);
1980 service_enter_start_post(s
);
1982 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1986 case SERVICE_START_POST
:
1987 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1990 /* Hmm, let's see if we can
1991 * load the pid now after the
1992 * start-post scripts got
1995 if ((r
= service_load_pid_file(s
)) < 0)
1996 log_warning("%s: failed to load PID file %s: %s", UNIT(s
)->meta
.id
, s
->pid_file
, strerror(-r
));
2001 case SERVICE_RELOAD
:
2003 service_enter_running(s
, true);
2005 service_enter_stop(s
, false);
2010 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2013 case SERVICE_STOP_SIGTERM
:
2014 case SERVICE_STOP_SIGKILL
:
2015 if (main_pid_good(s
) <= 0)
2016 service_enter_stop_post(s
, success
);
2018 /* If there is still a service
2019 * process around, wait until
2020 * that one quit, too */
2023 case SERVICE_STOP_POST
:
2024 case SERVICE_FINAL_SIGTERM
:
2025 case SERVICE_FINAL_SIGKILL
:
2026 service_enter_dead(s
, success
, true);
2030 assert_not_reached("Uh, control process died at wrong time.");
2034 assert_not_reached("Got SIGCHLD for unkown PID");
2037 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2038 Service
*s
= SERVICE(u
);
2041 assert(elapsed
== 1);
2043 assert(w
== &s
->timer_watch
);
2047 case SERVICE_START_PRE
:
2049 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2050 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2053 case SERVICE_START_POST
:
2054 case SERVICE_RELOAD
:
2055 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2056 service_enter_stop(s
, false);
2060 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2061 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2064 case SERVICE_STOP_SIGTERM
:
2065 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2066 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2069 case SERVICE_STOP_SIGKILL
:
2070 /* Uh, wie sent a SIGKILL and it is still not gone?
2071 * Must be something we cannot kill, so let's just be
2072 * weirded out and continue */
2074 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2075 service_enter_stop_post(s
, false);
2078 case SERVICE_STOP_POST
:
2079 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2080 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2083 case SERVICE_FINAL_SIGTERM
:
2084 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2085 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2088 case SERVICE_FINAL_SIGKILL
:
2089 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
2090 service_enter_dead(s
, false, true);
2093 case SERVICE_AUTO_RESTART
:
2094 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2095 service_enter_restart(s
);
2099 assert_not_reached("Timeout at wrong time.");
2103 static void service_cgroup_notify_event(Unit
*u
) {
2104 Service
*s
= SERVICE(u
);
2108 log_debug("%s: cgroup is empty", u
->meta
.id
);
2112 /* Waiting for SIGCHLD is usually more interesting,
2113 * because it includes return codes/signals. Which is
2114 * why we ignore the cgroup events for most cases,
2115 * except when we don't know pid which to expect the
2118 case SERVICE_RUNNING
:
2119 service_enter_running(s
, true);
2127 static int service_enumerate(Manager
*m
) {
2131 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2136 STRV_FOREACH(p
, m
->sysvrcnd_path
)
2137 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
2142 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
].path
) < 0) {
2150 if (!(d
= opendir(path
))) {
2151 if (errno
!= ENOENT
)
2152 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2157 while ((de
= readdir(d
))) {
2161 if (ignore_file(de
->d_name
))
2164 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2167 if (strlen(de
->d_name
) < 4)
2170 a
= undecchar(de
->d_name
[1]);
2171 b
= undecchar(de
->d_name
[2]);
2178 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
].path
, de
->d_name
) < 0) {
2183 if (access(fpath
, X_OK
) < 0) {
2185 if (errno
!= ENOENT
)
2186 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2193 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
2198 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, &service
)) < 0)
2201 if (de
->d_name
[0] == 'S' &&
2202 (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_BASIC
))
2203 SERVICE(service
)->sysv_start_priority
= a
*10 + b
;
2205 manager_dispatch_load_queue(m
);
2206 service
= unit_follow_merge(service
);
2208 if (de
->d_name
[0] == 'S') {
2209 Unit
*runlevel_target
;
2211 if ((r
= manager_load_unit(m
, rcnd_table
[i
].target
, NULL
, &runlevel_target
)) < 0)
2214 if ((r
= unit_add_dependency(runlevel_target
, UNIT_WANTS
, service
, true)) < 0)
2217 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, runlevel_target
, true)) < 0)
2220 } else if (de
->d_name
[0] == 'K' && rcnd_table
[i
].type
== RUNLEVEL_DOWN
) {
2221 Unit
*shutdown_target
;
2223 /* We honour K links only for
2224 * halt/reboot. For the normal
2225 * runlevels we assume the
2227 * implicitly added by the
2228 * core logic. Also, we don't
2229 * really distuingish here
2230 * between the runlevels 0 and
2231 * 6 and just add them to the
2232 * special shutdown target. */
2234 if ((r
= manager_load_unit(m
, SPECIAL_SHUTDOWN_TARGET
, NULL
, &shutdown_target
)) < 0)
2237 if ((r
= unit_add_dependency(service
, UNIT_CONFLICTS
, shutdown_target
, true)) < 0)
2240 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, shutdown_target
, true)) < 0)
2257 static void service_bus_name_owner_change(
2260 const char *old_owner
,
2261 const char *new_owner
) {
2263 Service
*s
= SERVICE(u
);
2268 assert(streq(s
->bus_name
, name
));
2269 assert(old_owner
|| new_owner
);
2271 if (old_owner
&& new_owner
)
2272 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2274 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2276 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2278 s
->bus_name_good
= !!new_owner
;
2280 if (s
->type
== SERVICE_DBUS
) {
2282 /* service_enter_running() will figure out what to
2284 if (s
->state
== SERVICE_RUNNING
)
2285 service_enter_running(s
, true);
2286 else if (s
->state
== SERVICE_START
&& new_owner
)
2287 service_enter_start_post(s
);
2289 } else if (new_owner
&&
2291 (s
->state
== SERVICE_START
||
2292 s
->state
== SERVICE_START_POST
||
2293 s
->state
== SERVICE_RUNNING
||
2294 s
->state
== SERVICE_RELOAD
)) {
2296 /* Try to acquire PID from bus service */
2297 log_debug("Trying to acquire PID from D-Bus name...");
2299 bus_query_pid(u
->meta
.manager
, name
);
2303 static void service_bus_query_pid_done(
2308 Service
*s
= SERVICE(u
);
2313 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2315 if (s
->main_pid
<= 0 &&
2316 (s
->state
== SERVICE_START
||
2317 s
->state
== SERVICE_START_POST
||
2318 s
->state
== SERVICE_RUNNING
||
2319 s
->state
== SERVICE_RELOAD
))
2323 int service_set_socket_fd(Service
*s
, int fd
) {
2327 /* This is called by the socket code when instantiating a new
2328 * service for a stream socket and the socket needs to be
2331 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
2334 if (s
->socket_fd
>= 0)
2337 if (s
->state
!= SERVICE_DEAD
)
2341 s
->got_socket_fd
= true;
2345 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2346 [SERVICE_DEAD
] = "dead",
2347 [SERVICE_START_PRE
] = "start-pre",
2348 [SERVICE_START
] = "start",
2349 [SERVICE_START_POST
] = "start-post",
2350 [SERVICE_RUNNING
] = "running",
2351 [SERVICE_EXITED
] = "exited",
2352 [SERVICE_RELOAD
] = "reload",
2353 [SERVICE_STOP
] = "stop",
2354 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2355 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2356 [SERVICE_STOP_POST
] = "stop-post",
2357 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2358 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2359 [SERVICE_MAINTAINANCE
] = "maintainance",
2360 [SERVICE_AUTO_RESTART
] = "auto-restart",
2363 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2365 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2366 [SERVICE_ONCE
] = "once",
2367 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2368 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2371 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2373 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2374 [SERVICE_FORKING
] = "forking",
2375 [SERVICE_SIMPLE
] = "simple",
2376 [SERVICE_FINISH
] = "finish",
2377 [SERVICE_DBUS
] = "dbus"
2380 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2382 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2383 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2384 [SERVICE_EXEC_START
] = "ExecStart",
2385 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2386 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2387 [SERVICE_EXEC_STOP
] = "ExecStop",
2388 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2391 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2393 const UnitVTable service_vtable
= {
2394 .suffix
= ".service",
2396 .init
= service_init
,
2397 .done
= service_done
,
2398 .load
= service_load
,
2400 .coldplug
= service_coldplug
,
2402 .dump
= service_dump
,
2404 .start
= service_start
,
2405 .stop
= service_stop
,
2406 .reload
= service_reload
,
2408 .can_reload
= service_can_reload
,
2410 .serialize
= service_serialize
,
2411 .deserialize_item
= service_deserialize_item
,
2413 .active_state
= service_active_state
,
2414 .sub_state_to_string
= service_sub_state_to_string
,
2416 .check_gc
= service_check_gc
,
2417 .check_snapshot
= service_check_snapshot
,
2419 .sigchld_event
= service_sigchld_event
,
2420 .timer_event
= service_timer_event
,
2422 .cgroup_notify_empty
= service_cgroup_notify_event
,
2424 .bus_name_owner_change
= service_bus_name_owner_change
,
2425 .bus_query_pid_done
= service_bus_query_pid_done
,
2427 .bus_message_handler
= bus_service_message_handler
,
2429 .enumerate
= service_enumerate