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
))) {
1192 UNIT(s
)->meta
.manager
->confirm_spawn
,
1193 UNIT(s
)->meta
.cgroup_bondings
,
1201 if (s
->socket_fd
>= 0)
1202 service_close_socket_fd(s
);
1207 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1208 /* FIXME: we need to do something here */
1219 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1224 static int main_pid_good(Service
*s
) {
1227 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1230 /* If we know the pid file, then lets just check if it is
1232 if (s
->main_pid_known
)
1233 return s
->main_pid
> 0;
1235 /* We don't know the pid */
1239 static int control_pid_good(Service
*s
) {
1242 return s
->control_pid
> 0;
1245 static int cgroup_good(Service
*s
) {
1250 if (s
->valid_no_process
)
1253 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
)) < 0)
1259 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1266 if (allow_restart
&&
1268 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1269 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1271 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1274 service_set_state(s
, SERVICE_AUTO_RESTART
);
1276 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1281 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1282 service_enter_dead(s
, false, false);
1285 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1287 static void service_enter_stop_post(Service
*s
, bool success
) {
1294 service_unwatch_control_pid(s
);
1296 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1297 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1298 if ((r
= service_spawn(s
,
1302 !s
->permissions_start_only
,
1303 !s
->root_directory_start_only
,
1304 &s
->control_pid
)) < 0)
1308 service_set_state(s
, SERVICE_STOP_POST
);
1310 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1315 log_warning("%s failed to run stop-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1316 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1319 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1328 if (s
->kill_mode
!= KILL_NONE
) {
1329 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1331 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1333 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1334 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1343 if (s
->main_pid
> 0) {
1344 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1350 if (s
->control_pid
> 0) {
1351 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1362 if (sent
&& (s
->main_pid
> 0 || s
->control_pid
> 0)) {
1363 if (s
->timeout_usec
> 0)
1364 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1367 service_set_state(s
, state
);
1368 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1369 service_enter_stop_post(s
, true);
1371 service_enter_dead(s
, true, true);
1376 log_warning("%s failed to kill processes: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1378 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1379 service_enter_stop_post(s
, false);
1381 service_enter_dead(s
, false, true);
1384 static void service_enter_stop(Service
*s
, bool success
) {
1391 service_unwatch_control_pid(s
);
1393 s
->control_command_id
= SERVICE_EXEC_STOP
;
1394 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1395 if ((r
= service_spawn(s
,
1399 !s
->permissions_start_only
,
1400 !s
->root_directory_start_only
,
1401 &s
->control_pid
)) < 0)
1404 service_set_state(s
, SERVICE_STOP
);
1406 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1411 log_warning("%s failed to run stop executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1412 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1415 static void service_enter_running(Service
*s
, bool success
) {
1421 if (main_pid_good(s
) != 0 &&
1422 cgroup_good(s
) != 0 &&
1423 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1424 service_set_state(s
, SERVICE_RUNNING
);
1425 else if (s
->valid_no_process
)
1426 service_set_state(s
, SERVICE_EXITED
);
1428 service_enter_stop(s
, true);
1431 static void service_enter_start_post(Service
*s
) {
1435 service_unwatch_control_pid(s
);
1437 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1438 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1439 if ((r
= service_spawn(s
,
1443 !s
->permissions_start_only
,
1444 !s
->root_directory_start_only
,
1445 &s
->control_pid
)) < 0)
1449 service_set_state(s
, SERVICE_START_POST
);
1451 service_enter_running(s
, true);
1456 log_warning("%s failed to run start-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1457 service_enter_stop(s
, false);
1460 static void service_enter_start(Service
*s
) {
1466 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1467 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1469 if (s
->type
== SERVICE_FORKING
)
1470 service_unwatch_control_pid(s
);
1472 service_unwatch_main_pid(s
);
1474 if ((r
= service_spawn(s
,
1475 s
->exec_command
[SERVICE_EXEC_START
],
1476 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
,
1483 if (s
->type
== SERVICE_SIMPLE
) {
1484 /* For simple services we immediately start
1485 * the START_POST binaries. */
1488 s
->main_pid_known
= true;
1490 service_enter_start_post(s
);
1492 } else if (s
->type
== SERVICE_FORKING
) {
1494 /* For forking services we wait until the start
1495 * process exited. */
1497 s
->control_pid
= pid
;
1499 s
->control_command_id
= SERVICE_EXEC_START
;
1500 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1501 service_set_state(s
, SERVICE_START
);
1503 } else if (s
->type
== SERVICE_FINISH
||
1504 s
->type
== SERVICE_DBUS
) {
1506 /* For finishing services we wait until the start
1507 * process exited, too, but it is our main process. */
1509 /* For D-Bus services we know the main pid right away,
1510 * but wait for the bus name to appear on the bus. */
1513 s
->main_pid_known
= true;
1515 service_set_state(s
, SERVICE_START
);
1517 assert_not_reached("Unknown service type");
1522 log_warning("%s failed to run start exectuable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1523 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1526 static void service_enter_start_pre(Service
*s
) {
1531 service_unwatch_control_pid(s
);
1533 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1534 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1535 if ((r
= service_spawn(s
,
1539 !s
->permissions_start_only
,
1540 !s
->root_directory_start_only
,
1541 &s
->control_pid
)) < 0)
1544 service_set_state(s
, SERVICE_START_PRE
);
1546 service_enter_start(s
);
1551 log_warning("%s failed to run start-pre executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1552 service_enter_dead(s
, false, true);
1555 static void service_enter_restart(Service
*s
) {
1559 service_enter_dead(s
, true, false);
1561 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1564 log_debug("%s scheduled restart job.", UNIT(s
)->meta
.id
);
1569 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1570 service_enter_dead(s
, false, false);
1573 static void service_enter_reload(Service
*s
) {
1578 service_unwatch_control_pid(s
);
1580 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1581 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1582 if ((r
= service_spawn(s
,
1586 !s
->permissions_start_only
,
1587 !s
->root_directory_start_only
,
1588 &s
->control_pid
)) < 0)
1591 service_set_state(s
, SERVICE_RELOAD
);
1593 service_enter_running(s
, true);
1598 log_warning("%s failed to run reload executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1599 service_enter_stop(s
, false);
1602 static void service_run_next(Service
*s
, bool success
) {
1606 assert(s
->control_command
);
1607 assert(s
->control_command
->command_next
);
1612 s
->control_command
= s
->control_command
->command_next
;
1614 service_unwatch_control_pid(s
);
1616 if ((r
= service_spawn(s
,
1620 !s
->permissions_start_only
,
1621 !s
->root_directory_start_only
,
1622 &s
->control_pid
)) < 0)
1628 log_warning("%s failed to run spawn next executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1630 if (s
->state
== SERVICE_START_PRE
)
1631 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1632 else if (s
->state
== SERVICE_STOP
)
1633 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1634 else if (s
->state
== SERVICE_STOP_POST
)
1635 service_enter_dead(s
, false, true);
1637 service_enter_stop(s
, false);
1640 static int service_start(Unit
*u
) {
1641 Service
*s
= SERVICE(u
);
1645 /* We cannot fulfill this request right now, try again later
1647 if (s
->state
== SERVICE_STOP
||
1648 s
->state
== SERVICE_STOP_SIGTERM
||
1649 s
->state
== SERVICE_STOP_SIGKILL
||
1650 s
->state
== SERVICE_STOP_POST
||
1651 s
->state
== SERVICE_FINAL_SIGTERM
||
1652 s
->state
== SERVICE_FINAL_SIGKILL
)
1655 /* Already on it! */
1656 if (s
->state
== SERVICE_START_PRE
||
1657 s
->state
== SERVICE_START
||
1658 s
->state
== SERVICE_START_POST
)
1661 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1663 /* Make sure we don't enter a busy loop of some kind. */
1664 if (!ratelimit_test(&s
->ratelimit
)) {
1665 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1670 s
->main_pid_known
= false;
1671 s
->allow_restart
= true;
1673 service_enter_start_pre(s
);
1677 static int service_stop(Unit
*u
) {
1678 Service
*s
= SERVICE(u
);
1682 /* Cannot do this now */
1683 if (s
->state
== SERVICE_START_PRE
||
1684 s
->state
== SERVICE_START
||
1685 s
->state
== SERVICE_START_POST
||
1686 s
->state
== SERVICE_RELOAD
)
1690 if (s
->state
== SERVICE_STOP
||
1691 s
->state
== SERVICE_STOP_SIGTERM
||
1692 s
->state
== SERVICE_STOP_SIGKILL
||
1693 s
->state
== SERVICE_STOP_POST
||
1694 s
->state
== SERVICE_FINAL_SIGTERM
||
1695 s
->state
== SERVICE_FINAL_SIGKILL
)
1698 if (s
->state
== SERVICE_AUTO_RESTART
) {
1699 service_set_state(s
, SERVICE_DEAD
);
1703 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1705 /* This is a user request, so don't do restarts on this
1707 s
->allow_restart
= false;
1709 service_enter_stop(s
, true);
1713 static int service_reload(Unit
*u
) {
1714 Service
*s
= SERVICE(u
);
1718 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1720 service_enter_reload(s
);
1724 static bool service_can_reload(Unit
*u
) {
1725 Service
*s
= SERVICE(u
);
1729 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1732 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1733 Service
*s
= SERVICE(u
);
1739 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1740 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1742 if (s
->control_pid
> 0)
1743 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) (s
->control_pid
));
1745 if (s
->main_pid
> 0)
1746 unit_serialize_item_format(u
, f
, "main-pid", "%u", (unsigned) (s
->main_pid
));
1748 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1750 /* There's a minor uncleanliness here: if there are multiple
1751 * commands attached here, we will start from the first one
1753 if (s
->control_command_id
>= 0)
1754 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
1756 if (s
->socket_fd
>= 0) {
1759 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
1762 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1768 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1769 Service
*s
= SERVICE(u
);
1777 if (streq(key
, "state")) {
1780 if ((state
= service_state_from_string(value
)) < 0)
1781 log_debug("Failed to parse state value %s", value
);
1783 s
->deserialized_state
= state
;
1784 } else if (streq(key
, "failure")) {
1787 if ((b
= parse_boolean(value
)) < 0)
1788 log_debug("Failed to parse failure value %s", value
);
1790 s
->failure
= b
|| s
->failure
;
1791 } else if (streq(key
, "control-pid")) {
1794 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1795 log_debug("Failed to parse control-pid value %s", value
);
1797 s
->control_pid
= (pid_t
) pid
;
1798 } else if (streq(key
, "main-pid")) {
1801 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1802 log_debug("Failed to parse main-pid value %s", value
);
1804 s
->main_pid
= (pid_t
) pid
;
1805 } else if (streq(key
, "main-pid-known")) {
1808 if ((b
= parse_boolean(value
)) < 0)
1809 log_debug("Failed to parse main-pid-known value %s", value
);
1811 s
->main_pid_known
= b
;
1812 } else if (streq(key
, "control-command")) {
1813 ServiceExecCommand id
;
1815 if ((id
= service_exec_command_from_string(value
)) < 0)
1816 log_debug("Failed to parse exec-command value %s", value
);
1818 s
->control_command_id
= id
;
1819 s
->control_command
= s
->exec_command
[id
];
1821 } else if (streq(key
, "socket-fd")) {
1824 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
1825 log_debug("Failed to parse socket-fd value %s", value
);
1828 if (s
->socket_fd
>= 0)
1829 close_nointr_nofail(s
->socket_fd
);
1830 s
->socket_fd
= fdset_remove(fds
, fd
);
1833 log_debug("Unknown serialization key '%s'", key
);
1838 static UnitActiveState
service_active_state(Unit
*u
) {
1841 return state_translation_table
[SERVICE(u
)->state
];
1844 static const char *service_sub_state_to_string(Unit
*u
) {
1847 return service_state_to_string(SERVICE(u
)->state
);
1850 static bool service_check_gc(Unit
*u
) {
1851 Service
*s
= SERVICE(u
);
1855 return !!s
->sysv_path
;
1858 static bool service_check_snapshot(Unit
*u
) {
1859 Service
*s
= SERVICE(u
);
1863 return !s
->got_socket_fd
;
1866 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1867 Service
*s
= SERVICE(u
);
1873 success
= code
== CLD_EXITED
&& status
== 0;
1874 s
->failure
= s
->failure
|| !success
;
1876 if (s
->main_pid
== pid
) {
1878 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1881 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1882 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1883 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1886 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1888 /* The service exited, so the service is officially
1893 case SERVICE_START_POST
:
1894 case SERVICE_RELOAD
:
1896 /* Need to wait until the operation is
1901 assert(s
->type
== SERVICE_FINISH
);
1903 /* This was our main goal, so let's go on */
1905 service_enter_start_post(s
);
1907 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1910 case SERVICE_RUNNING
:
1911 service_enter_running(s
, success
);
1914 case SERVICE_STOP_SIGTERM
:
1915 case SERVICE_STOP_SIGKILL
:
1917 if (!control_pid_good(s
))
1918 service_enter_stop_post(s
, success
);
1920 /* If there is still a control process, wait for that first */
1924 assert_not_reached("Uh, main process died at wrong time.");
1927 } else if (s
->control_pid
== pid
) {
1929 if (s
->control_command
)
1930 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1934 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1936 /* If we are shutting things down anyway we
1937 * don't care about failing commands. */
1939 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1941 /* There is another command to *
1942 * execute, so let's do that. */
1944 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1945 service_run_next(s
, success
);
1948 /* No further commands for this step, so let's
1949 * figure out what to do next */
1951 s
->control_command
= NULL
;
1952 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1954 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1958 case SERVICE_START_PRE
:
1960 service_enter_start(s
);
1962 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1966 assert(s
->type
== SERVICE_FORKING
);
1968 /* Let's try to load the pid
1969 * file here if we can. We
1970 * ignore the return value,
1971 * since the PID file might
1972 * actually be created by a
1973 * START_POST script */
1977 service_load_pid_file(s
);
1979 service_enter_start_post(s
);
1981 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1985 case SERVICE_START_POST
:
1986 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1989 /* Hmm, let's see if we can
1990 * load the pid now after the
1991 * start-post scripts got
1994 if ((r
= service_load_pid_file(s
)) < 0)
1995 log_warning("%s: failed to load PID file %s: %s", UNIT(s
)->meta
.id
, s
->pid_file
, strerror(-r
));
2000 case SERVICE_RELOAD
:
2002 service_enter_running(s
, true);
2004 service_enter_stop(s
, false);
2009 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2012 case SERVICE_STOP_SIGTERM
:
2013 case SERVICE_STOP_SIGKILL
:
2014 if (main_pid_good(s
) <= 0)
2015 service_enter_stop_post(s
, success
);
2017 /* If there is still a service
2018 * process around, wait until
2019 * that one quit, too */
2022 case SERVICE_STOP_POST
:
2023 case SERVICE_FINAL_SIGTERM
:
2024 case SERVICE_FINAL_SIGKILL
:
2025 service_enter_dead(s
, success
, true);
2029 assert_not_reached("Uh, control process died at wrong time.");
2033 assert_not_reached("Got SIGCHLD for unkown PID");
2036 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2037 Service
*s
= SERVICE(u
);
2040 assert(elapsed
== 1);
2042 assert(w
== &s
->timer_watch
);
2046 case SERVICE_START_PRE
:
2048 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2049 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2052 case SERVICE_START_POST
:
2053 case SERVICE_RELOAD
:
2054 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2055 service_enter_stop(s
, false);
2059 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2060 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2063 case SERVICE_STOP_SIGTERM
:
2064 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2065 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2068 case SERVICE_STOP_SIGKILL
:
2069 /* Uh, wie sent a SIGKILL and it is still not gone?
2070 * Must be something we cannot kill, so let's just be
2071 * weirded out and continue */
2073 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2074 service_enter_stop_post(s
, false);
2077 case SERVICE_STOP_POST
:
2078 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2079 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2082 case SERVICE_FINAL_SIGTERM
:
2083 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2084 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2087 case SERVICE_FINAL_SIGKILL
:
2088 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
2089 service_enter_dead(s
, false, true);
2092 case SERVICE_AUTO_RESTART
:
2093 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2094 service_enter_restart(s
);
2098 assert_not_reached("Timeout at wrong time.");
2102 static void service_cgroup_notify_event(Unit
*u
) {
2103 Service
*s
= SERVICE(u
);
2107 log_debug("%s: cgroup is empty", u
->meta
.id
);
2111 /* Waiting for SIGCHLD is usually more interesting,
2112 * because it includes return codes/signals. Which is
2113 * why we ignore the cgroup events for most cases,
2114 * except when we don't know pid which to expect the
2117 case SERVICE_RUNNING
:
2118 service_enter_running(s
, true);
2126 static int service_enumerate(Manager
*m
) {
2130 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2135 STRV_FOREACH(p
, m
->sysvrcnd_path
)
2136 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
2141 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
].path
) < 0) {
2149 if (!(d
= opendir(path
))) {
2150 if (errno
!= ENOENT
)
2151 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2156 while ((de
= readdir(d
))) {
2160 if (ignore_file(de
->d_name
))
2163 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2166 if (strlen(de
->d_name
) < 4)
2169 a
= undecchar(de
->d_name
[1]);
2170 b
= undecchar(de
->d_name
[2]);
2177 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
].path
, de
->d_name
) < 0) {
2182 if (access(fpath
, X_OK
) < 0) {
2184 if (errno
!= ENOENT
)
2185 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2192 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
2197 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, &service
)) < 0)
2200 if (de
->d_name
[0] == 'S' &&
2201 (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_BASIC
))
2202 SERVICE(service
)->sysv_start_priority
= a
*10 + b
;
2204 manager_dispatch_load_queue(m
);
2205 service
= unit_follow_merge(service
);
2207 if (de
->d_name
[0] == 'S') {
2208 Unit
*runlevel_target
;
2210 if ((r
= manager_load_unit(m
, rcnd_table
[i
].target
, NULL
, &runlevel_target
)) < 0)
2213 if ((r
= unit_add_dependency(runlevel_target
, UNIT_WANTS
, service
, true)) < 0)
2216 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, runlevel_target
, true)) < 0)
2219 } else if (de
->d_name
[0] == 'K' && rcnd_table
[i
].type
== RUNLEVEL_DOWN
) {
2220 Unit
*shutdown_target
;
2222 /* We honour K links only for
2223 * halt/reboot. For the normal
2224 * runlevels we assume the
2226 * implicitly added by the
2227 * core logic. Also, we don't
2228 * really distuingish here
2229 * between the runlevels 0 and
2230 * 6 and just add them to the
2231 * special shutdown target. */
2233 if ((r
= manager_load_unit(m
, SPECIAL_SHUTDOWN_TARGET
, NULL
, &shutdown_target
)) < 0)
2236 if ((r
= unit_add_dependency(service
, UNIT_CONFLICTS
, shutdown_target
, true)) < 0)
2239 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, shutdown_target
, true)) < 0)
2256 static void service_bus_name_owner_change(
2259 const char *old_owner
,
2260 const char *new_owner
) {
2262 Service
*s
= SERVICE(u
);
2267 assert(streq(s
->bus_name
, name
));
2268 assert(old_owner
|| new_owner
);
2270 if (old_owner
&& new_owner
)
2271 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2273 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2275 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2277 s
->bus_name_good
= !!new_owner
;
2279 if (s
->type
== SERVICE_DBUS
) {
2281 /* service_enter_running() will figure out what to
2283 if (s
->state
== SERVICE_RUNNING
)
2284 service_enter_running(s
, true);
2285 else if (s
->state
== SERVICE_START
&& new_owner
)
2286 service_enter_start_post(s
);
2288 } else if (new_owner
&&
2290 (s
->state
== SERVICE_START
||
2291 s
->state
== SERVICE_START_POST
||
2292 s
->state
== SERVICE_RUNNING
||
2293 s
->state
== SERVICE_RELOAD
)) {
2295 /* Try to acquire PID from bus service */
2296 log_debug("Trying to acquire PID from D-Bus name...");
2298 bus_query_pid(u
->meta
.manager
, name
);
2302 static void service_bus_query_pid_done(
2307 Service
*s
= SERVICE(u
);
2312 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2314 if (s
->main_pid
<= 0 &&
2315 (s
->state
== SERVICE_START
||
2316 s
->state
== SERVICE_START_POST
||
2317 s
->state
== SERVICE_RUNNING
||
2318 s
->state
== SERVICE_RELOAD
))
2322 int service_set_socket_fd(Service
*s
, int fd
) {
2326 /* This is called by the socket code when instantiating a new
2327 * service for a stream socket and the socket needs to be
2330 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
2333 if (s
->socket_fd
>= 0)
2336 if (s
->state
!= SERVICE_DEAD
)
2340 s
->got_socket_fd
= true;
2344 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2345 [SERVICE_DEAD
] = "dead",
2346 [SERVICE_START_PRE
] = "start-pre",
2347 [SERVICE_START
] = "start",
2348 [SERVICE_START_POST
] = "start-post",
2349 [SERVICE_RUNNING
] = "running",
2350 [SERVICE_EXITED
] = "exited",
2351 [SERVICE_RELOAD
] = "reload",
2352 [SERVICE_STOP
] = "stop",
2353 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2354 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2355 [SERVICE_STOP_POST
] = "stop-post",
2356 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2357 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2358 [SERVICE_MAINTAINANCE
] = "maintainance",
2359 [SERVICE_AUTO_RESTART
] = "auto-restart",
2362 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2364 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2365 [SERVICE_ONCE
] = "once",
2366 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2367 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2370 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2372 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2373 [SERVICE_FORKING
] = "forking",
2374 [SERVICE_SIMPLE
] = "simple",
2375 [SERVICE_FINISH
] = "finish",
2376 [SERVICE_DBUS
] = "dbus"
2379 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2381 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2382 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2383 [SERVICE_EXEC_START
] = "ExecStart",
2384 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2385 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2386 [SERVICE_EXEC_STOP
] = "ExecStop",
2387 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2390 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2392 const UnitVTable service_vtable
= {
2393 .suffix
= ".service",
2395 .init
= service_init
,
2396 .done
= service_done
,
2397 .load
= service_load
,
2399 .coldplug
= service_coldplug
,
2401 .dump
= service_dump
,
2403 .start
= service_start
,
2404 .stop
= service_stop
,
2405 .reload
= service_reload
,
2407 .can_reload
= service_can_reload
,
2409 .serialize
= service_serialize
,
2410 .deserialize_item
= service_deserialize_item
,
2412 .active_state
= service_active_state
,
2413 .sub_state_to_string
= service_sub_state_to_string
,
2415 .check_gc
= service_check_gc
,
2416 .check_snapshot
= service_check_snapshot
,
2418 .sigchld_event
= service_sigchld_event
,
2419 .timer_event
= service_timer_event
,
2421 .cgroup_notify_empty
= service_cgroup_notify_event
,
2423 .bus_name_owner_change
= service_bus_name_owner_change
,
2424 .bus_query_pid_done
= service_bus_query_pid_done
,
2426 .bus_message_handler
= bus_service_message_handler
,
2428 .enumerate
= service_enumerate