1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #define COMMENTS "#;\n"
37 #define NEWLINES "\n\r"
40 static const char * const rcnd_table
[] = {
41 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET
,
42 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET
,
43 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET
,
44 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET
,
45 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET
,
46 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET
,
47 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET
,
48 "/boot.d", SPECIAL_BASIC_TARGET
51 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
52 [SERVICE_DEAD
] = UNIT_INACTIVE
,
53 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
54 [SERVICE_START
] = UNIT_ACTIVATING
,
55 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
56 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
57 [SERVICE_EXITED
] = UNIT_ACTIVE
,
58 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
59 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
60 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
61 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
62 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
63 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
64 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
65 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
66 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
69 static void service_init(Unit
*u
) {
70 Service
*s
= SERVICE(u
);
73 assert(u
->meta
.load_state
== UNIT_STUB
);
75 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
76 s
->restart_usec
= DEFAULT_RESTART_USEC
;
77 s
->timer_watch
.type
= WATCH_INVALID
;
78 s
->sysv_start_priority
= -1;
81 exec_context_init(&s
->exec_context
);
83 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
85 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
88 static void service_unwatch_control_pid(Service
*s
) {
91 if (s
->control_pid
<= 0)
94 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
98 static void service_unwatch_main_pid(Service
*s
) {
101 if (s
->main_pid
<= 0)
104 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
108 static void service_close_socket_fd(Service
*s
) {
111 if (s
->socket_fd
< 0)
114 close_nointr_nofail(s
->socket_fd
);
118 static void service_done(Unit
*u
) {
119 Service
*s
= SERVICE(u
);
129 free(s
->sysv_runlevels
);
130 s
->sysv_runlevels
= NULL
;
132 exec_context_done(&s
->exec_context
);
133 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
134 s
->control_command
= NULL
;
136 /* This will leak a process, but at least no memory or any of
138 service_unwatch_main_pid(s
);
139 service_unwatch_control_pid(s
);
142 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
147 service_close_socket_fd(s
);
149 unit_unwatch_timer(u
, &s
->timer_watch
);
152 static int sysv_translate_name(const char *name
, char **_r
) {
154 static const char * const table
[] = {
155 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
156 "$network", SPECIAL_NETWORK_TARGET
,
157 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
158 "$portmap", SPECIAL_RPCBIND_TARGET
,
159 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
160 "$syslog", SPECIAL_SYSLOG_TARGET
,
161 "$time", SPECIAL_RTC_SET_TARGET
167 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
168 if (streq(table
[i
], name
)) {
169 if (!(r
= strdup(table
[i
+1])))
178 if (asprintf(&r
, "%s.service", name
) < 0)
189 static int sysv_chkconfig_order(Service
*s
) {
195 if (s
->sysv_start_priority
< 0)
198 /* For each pair of services where at least one lacks a LSB
199 * header, we use the start priority value to order things. */
201 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
205 t
= (Service
*) other
;
210 if (t
->sysv_start_priority
< 0)
213 /* If both units have modern headers we don't care
214 * about the priorities */
215 if ((!s
->sysv_path
|| s
->sysv_has_lsb
) &&
216 (!t
->sysv_path
|| t
->sysv_has_lsb
))
219 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
221 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
226 /* FIXME: Maybe we should compare the name here lexicographically? */
228 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
235 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
238 if (!(c
= new0(ExecCommand
, 1)))
241 if (!(c
->path
= strdup(path
))) {
246 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
255 static int sysv_exec_commands(Service
*s
) {
259 assert(s
->sysv_path
);
261 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
263 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
265 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
267 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
269 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
271 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
276 static int priority_from_rcd(Service
*s
, const char *init_script
) {
280 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvrcnd_path
)
281 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
286 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0)
294 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
299 while ((de
= readdir(d
))) {
302 if (ignore_file(de
->d_name
))
305 if (de
->d_name
[0] != 'S')
308 if (strlen(de
->d_name
) < 4)
311 if (!streq(de
->d_name
+ 3, init_script
))
314 /* Yay, we found it! Now decode the priority */
316 a
= undecchar(de
->d_name
[1]);
317 b
= undecchar(de
->d_name
[2]);
322 s
->sysv_start_priority
= a
*10 + b
;
324 log_debug("Determined priority %i from link farm for %s", s
->sysv_start_priority
, UNIT(s
)->meta
.id
);
336 static int service_load_sysv_path(Service
*s
, const char *path
) {
353 if (!(f
= fopen(path
, "re"))) {
354 r
= errno
== ENOENT
? 0 : -errno
;
358 s
->type
= SERVICE_FORKING
;
359 s
->restart
= SERVICE_ONCE
;
362 if (!(s
->sysv_path
= strdup(path
))) {
368 char l
[LINE_MAX
], *t
;
370 if (!fgets(l
, sizeof(l
), f
)) {
375 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
385 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
387 s
->sysv_has_lsb
= true;
391 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
397 t
+= strspn(t
, WHITESPACE
);
399 if (state
== NORMAL
) {
401 /* Try to parse Red Hat style chkconfig headers */
403 if (startswith(t
, "chkconfig:")) {
405 char runlevels
[16], *k
;
409 if (sscanf(t
+10, "%15s %i %*i",
411 &start_priority
) != 2) {
413 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
417 if (start_priority
< 0 || start_priority
> 99)
418 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
420 s
->sysv_start_priority
= start_priority
;
422 char_array_0(runlevels
);
423 k
= delete_chars(runlevels
, WHITESPACE
"-");
428 if (!(d
= strdup(k
))) {
433 free(s
->sysv_runlevels
);
434 s
->sysv_runlevels
= d
;
438 } else if (startswith(t
, "description:")) {
440 size_t k
= strlen(t
);
443 if (t
[k
-1] == '\\') {
448 if (!(d
= strdup(strstrip(t
+12)))) {
453 free(u
->meta
.description
);
454 u
->meta
.description
= d
;
456 } else if (startswith(t
, "pidfile:")) {
463 if (!path_is_absolute(fn
)) {
464 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
468 if (!(fn
= strdup(fn
))) {
477 } else if (state
== DESCRIPTION
) {
479 /* Try to parse Red Hat style description
482 size_t k
= strlen(t
);
490 assert(u
->meta
.description
);
491 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
496 free(u
->meta
.description
);
497 u
->meta
.description
= d
;
499 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
501 if (startswith(t
, "Provides:")) {
507 FOREACH_WORD(w
, z
, t
+9, i
) {
510 if (!(n
= strndup(w
, z
))) {
515 r
= sysv_translate_name(n
, &m
);
524 if (unit_name_to_type(m
) == UNIT_SERVICE
)
525 r
= unit_add_name(u
, m
);
527 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
, NULL
, true)) >= 0)
528 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
537 } else if (startswith(t
, "Required-Start:") ||
538 startswith(t
, "Should-Start:")) {
544 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
547 if (!(n
= strndup(w
, z
))) {
552 r
= sysv_translate_name(n
, &m
);
561 r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
, NULL
, true);
567 } else if (startswith(t
, "Default-Start:")) {
572 k
= delete_chars(t
+14, WHITESPACE
"-");
575 if (!(d
= strdup(k
))) {
580 free(s
->sysv_runlevels
);
581 s
->sysv_runlevels
= d
;
584 } else if (startswith(t
, "Description:")) {
587 state
= LSB_DESCRIPTION
;
589 if (!(d
= strdup(strstrip(t
+12)))) {
594 free(u
->meta
.description
);
595 u
->meta
.description
= d
;
597 } else if (startswith(t
, "Short-Description:") &&
598 !u
->meta
.description
) {
601 /* We use the short description only
602 * if no long description is set. */
606 if (!(d
= strdup(strstrip(t
+18)))) {
611 u
->meta
.description
= d
;
613 } else if (state
== LSB_DESCRIPTION
) {
615 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
618 assert(u
->meta
.description
);
619 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
624 free(u
->meta
.description
);
625 u
->meta
.description
= d
;
632 /* If init scripts have no LSB header, then we enforce the
633 * ordering via the chkconfig priorities. We try to determine
634 * a priority for *all* init scripts here, since they are
635 * needed as soon as at least one non-LSB script is used. */
637 if (s
->sysv_start_priority
< 0) {
638 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", u
->meta
.id
);
640 if ((r
= priority_from_rcd(s
, file_name_from_path(path
))) < 0)
643 if (s
->sysv_start_priority
< 0)
644 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u
->meta
.id
);
647 if ((r
= sysv_exec_commands(s
)) < 0)
650 if (!s
->sysv_runlevels
|| chars_intersect("12345", s
->sysv_runlevels
)) {
651 /* If there a runlevels configured for this service
652 * but none of the standard ones, then we assume this
653 * is some special kind of service (which might be
654 * needed for early boot) and don't create any links
657 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0 ||
658 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
662 /* Special setting for all SysV services */
663 s
->valid_no_process
= true;
664 s
->kill_mode
= KILL_PROCESS_GROUP
;
666 /* Don't timeout special services during boot (like fsck) */
667 if (s
->sysv_runlevels
&& !chars_intersect("12345", s
->sysv_runlevels
))
668 s
->timeout_usec
= -1;
670 u
->meta
.load_state
= UNIT_LOADED
;
681 static int service_load_sysv_name(Service
*s
, const char *name
) {
687 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
691 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
694 assert(endswith(path
, ".service"));
695 path
[strlen(path
)-8] = 0;
697 r
= service_load_sysv_path(s
, path
);
703 if ((UNIT(s
)->meta
.load_state
!= UNIT_STUB
))
710 static int service_load_sysv(Service
*s
) {
717 /* Load service data from SysV init scripts, preferably with
720 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
723 if ((t
= UNIT(s
)->meta
.id
))
724 if ((r
= service_load_sysv_name(s
, t
)) < 0)
727 if (UNIT(s
)->meta
.load_state
== UNIT_STUB
)
728 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
729 if (t
== UNIT(s
)->meta
.id
)
732 if ((r
== service_load_sysv_name(s
, t
)) < 0)
735 if (UNIT(s
)->meta
.load_state
!= UNIT_STUB
)
742 static int service_add_bus_name(Service
*s
) {
749 if (asprintf(&n
, "dbus-%s.service", s
->bus_name
) < 0)
752 r
= unit_merge_by_name(UNIT(s
), n
);
758 static int service_verify(Service
*s
) {
761 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
764 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
765 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->meta
.id
);
769 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
770 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->meta
.id
);
777 static int service_load(Unit
*u
) {
779 Service
*s
= SERVICE(u
);
783 /* Load a .service file */
784 if ((r
= unit_load_fragment(u
)) < 0)
787 /* Load a classic init script as a fallback, if we couldn't find anything */
788 if (u
->meta
.load_state
== UNIT_STUB
)
789 if ((r
= service_load_sysv(s
)) < 0)
792 /* Still nothing found? Then let's give up */
793 if (u
->meta
.load_state
== UNIT_STUB
)
796 /* We were able to load something, then let's add in the
797 * dropin directories. */
798 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
801 /* This is a new unit? Then let's add in some extras */
802 if (u
->meta
.load_state
== UNIT_LOADED
) {
803 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
806 if ((r
= unit_add_default_cgroup(u
)) < 0)
809 if ((r
= sysv_chkconfig_order(s
)) < 0)
813 if ((r
= service_add_bus_name(s
)) < 0)
816 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
821 return service_verify(s
);
824 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
826 ServiceExecCommand c
;
827 Service
*s
= SERVICE(u
);
833 p2
= strappend(prefix
, "\t");
834 prefix2
= p2
? p2
: prefix
;
837 "%sService State: %s\n"
838 "%sPermissionsStartOnly: %s\n"
839 "%sRootDirectoryStartOnly: %s\n"
840 "%sValidNoProcess: %s\n"
843 prefix
, service_state_to_string(s
->state
),
844 prefix
, yes_no(s
->permissions_start_only
),
845 prefix
, yes_no(s
->root_directory_start_only
),
846 prefix
, yes_no(s
->valid_no_process
),
847 prefix
, kill_mode_to_string(s
->kill_mode
),
848 prefix
, service_type_to_string(s
->type
));
850 if (s
->control_pid
> 0)
852 "%sControl PID: %llu\n",
853 prefix
, (unsigned long long) s
->control_pid
);
857 "%sMain PID: %llu\n",
858 prefix
, (unsigned long long) s
->main_pid
);
863 prefix
, s
->pid_file
);
868 "%sBus Name Good: %s\n",
870 prefix
, yes_no(s
->bus_name_good
));
872 exec_context_dump(&s
->exec_context
, f
, prefix
);
874 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
876 if (!s
->exec_command
[c
])
879 fprintf(f
, "%s-> %s:\n",
880 prefix
, service_exec_command_to_string(c
));
882 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
887 "%sSysV Init Script Path: %s\n"
888 "%sSysV Init Script has LSB Header: %s\n",
889 prefix
, s
->sysv_path
,
890 prefix
, yes_no(s
->sysv_has_lsb
));
892 if (s
->sysv_start_priority
>= 0)
894 "%sSysVStartPriority: %i\n",
895 prefix
, s
->sysv_start_priority
);
897 if (s
->sysv_runlevels
)
898 fprintf(f
, "%sSysVRunLevels: %s\n",
899 prefix
, s
->sysv_runlevels
);
904 static int service_load_pid_file(Service
*s
) {
911 if (s
->main_pid_known
)
914 assert(s
->main_pid
<= 0);
919 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
922 if ((r
= safe_atolu(k
, &p
)) < 0) {
927 if ((unsigned long) (pid_t
) p
!= p
)
930 if (kill((pid_t
) p
, 0) < 0 && errno
!= EPERM
) {
931 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
932 (unsigned long long) p
, s
->pid_file
);
936 if ((r
= unit_watch_pid(UNIT(s
), (pid_t
) p
)) < 0)
937 /* FIXME: we need to do something here */
940 s
->main_pid
= (pid_t
) p
;
941 s
->main_pid_known
= true;
946 static int service_get_sockets(Service
*s
, Set
**_set
) {
955 /* Collects all Socket objects that belong to this
956 * service. Note that a service might have multiple sockets
957 * via multiple names. */
959 if (!(set
= set_new(NULL
, NULL
)))
962 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
966 /* Look for all socket objects that go by any of our
967 * units and collect their fds */
969 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
974 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
980 if ((r
= set_put(set
, p
)) < 0)
992 static int service_notify_sockets_dead(Service
*s
) {
1000 /* Notifies all our sockets when we die */
1001 if ((r
= service_get_sockets(s
, &set
)) < 0)
1004 SET_FOREACH(sock
, set
, i
)
1005 socket_notify_service_dead(sock
);
1012 static void service_set_state(Service
*s
, ServiceState state
) {
1013 ServiceState old_state
;
1016 old_state
= s
->state
;
1019 if (state
!= SERVICE_START_PRE
&&
1020 state
!= SERVICE_START
&&
1021 state
!= SERVICE_START_POST
&&
1022 state
!= SERVICE_RELOAD
&&
1023 state
!= SERVICE_STOP
&&
1024 state
!= SERVICE_STOP_SIGTERM
&&
1025 state
!= SERVICE_STOP_SIGKILL
&&
1026 state
!= SERVICE_STOP_POST
&&
1027 state
!= SERVICE_FINAL_SIGTERM
&&
1028 state
!= SERVICE_FINAL_SIGKILL
&&
1029 state
!= SERVICE_AUTO_RESTART
)
1030 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1032 if (state
!= SERVICE_START
&&
1033 state
!= SERVICE_START_POST
&&
1034 state
!= SERVICE_RUNNING
&&
1035 state
!= SERVICE_RELOAD
&&
1036 state
!= SERVICE_STOP
&&
1037 state
!= SERVICE_STOP_SIGTERM
&&
1038 state
!= SERVICE_STOP_SIGKILL
)
1039 service_unwatch_main_pid(s
);
1041 if (state
!= SERVICE_START_PRE
&&
1042 state
!= SERVICE_START
&&
1043 state
!= SERVICE_START_POST
&&
1044 state
!= SERVICE_RELOAD
&&
1045 state
!= SERVICE_STOP
&&
1046 state
!= SERVICE_STOP_SIGTERM
&&
1047 state
!= SERVICE_STOP_SIGKILL
&&
1048 state
!= SERVICE_STOP_POST
&&
1049 state
!= SERVICE_FINAL_SIGTERM
&&
1050 state
!= SERVICE_FINAL_SIGKILL
) {
1051 service_unwatch_control_pid(s
);
1052 s
->control_command
= NULL
;
1053 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1056 if (state
== SERVICE_DEAD
||
1057 state
== SERVICE_STOP
||
1058 state
== SERVICE_STOP_SIGTERM
||
1059 state
== SERVICE_STOP_SIGKILL
||
1060 state
== SERVICE_STOP_POST
||
1061 state
== SERVICE_FINAL_SIGTERM
||
1062 state
== SERVICE_FINAL_SIGKILL
||
1063 state
== SERVICE_MAINTAINANCE
||
1064 state
== SERVICE_AUTO_RESTART
)
1065 service_notify_sockets_dead(s
);
1067 if (state
!= SERVICE_START_PRE
&&
1068 state
!= SERVICE_START
&&
1069 !(state
== SERVICE_DEAD
&& UNIT(s
)->meta
.job
))
1070 service_close_socket_fd(s
);
1072 if (old_state
!= state
)
1073 log_debug("%s changed %s -> %s", UNIT(s
)->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1075 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1078 static int service_coldplug(Unit
*u
) {
1079 Service
*s
= SERVICE(u
);
1083 assert(s
->state
== SERVICE_DEAD
);
1085 if (s
->deserialized_state
!= s
->state
) {
1087 if (s
->deserialized_state
== SERVICE_START_PRE
||
1088 s
->deserialized_state
== SERVICE_START
||
1089 s
->deserialized_state
== SERVICE_START_POST
||
1090 s
->deserialized_state
== SERVICE_RELOAD
||
1091 s
->deserialized_state
== SERVICE_STOP
||
1092 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1093 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1094 s
->deserialized_state
== SERVICE_STOP_POST
||
1095 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1096 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1097 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1099 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1102 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1104 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1109 if ((s
->deserialized_state
== SERVICE_START
&&
1110 (s
->type
== SERVICE_FORKING
||
1111 s
->type
== SERVICE_DBUS
)) ||
1112 s
->deserialized_state
== SERVICE_START_POST
||
1113 s
->deserialized_state
== SERVICE_RUNNING
||
1114 s
->deserialized_state
== SERVICE_RELOAD
||
1115 s
->deserialized_state
== SERVICE_STOP
||
1116 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1117 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1118 if (s
->main_pid
> 0)
1119 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1122 if (s
->deserialized_state
== SERVICE_START_PRE
||
1123 s
->deserialized_state
== SERVICE_START
||
1124 s
->deserialized_state
== SERVICE_START_POST
||
1125 s
->deserialized_state
== SERVICE_RELOAD
||
1126 s
->deserialized_state
== SERVICE_STOP
||
1127 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1128 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1129 s
->deserialized_state
== SERVICE_STOP_POST
||
1130 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1131 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1132 if (s
->control_pid
> 0)
1133 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1136 service_set_state(s
, s
->deserialized_state
);
1142 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1146 unsigned rn_fds
= 0;
1154 if ((r
= service_get_sockets(s
, &set
)) < 0)
1157 SET_FOREACH(sock
, set
, i
) {
1161 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1173 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1179 memcpy(t
, rfds
, rn_fds
);
1180 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1185 rn_fds
= rn_fds
+cn_fds
;
1203 static int service_spawn(
1208 bool apply_permissions
,
1223 if (s
->socket_fd
>= 0) {
1224 fds
= &s
->socket_fd
;
1226 } else if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1230 if (timeout
&& s
->timeout_usec
) {
1231 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1234 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1236 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1247 UNIT(s
)->meta
.manager
->confirm_spawn
,
1248 UNIT(s
)->meta
.cgroup_bondings
,
1256 if (s
->socket_fd
>= 0)
1257 service_close_socket_fd(s
);
1262 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1263 /* FIXME: we need to do something here */
1274 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1279 static int main_pid_good(Service
*s
) {
1282 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1285 /* If we know the pid file, then lets just check if it is
1287 if (s
->main_pid_known
)
1288 return s
->main_pid
> 0;
1290 /* We don't know the pid */
1294 static int control_pid_good(Service
*s
) {
1297 return s
->control_pid
> 0;
1300 static int cgroup_good(Service
*s
) {
1305 if (s
->valid_no_process
)
1308 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
)) < 0)
1314 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1321 if (allow_restart
&&
1323 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1324 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1326 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1329 service_set_state(s
, SERVICE_AUTO_RESTART
);
1331 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1336 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1337 service_enter_dead(s
, false, false);
1340 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1342 static void service_enter_stop_post(Service
*s
, bool success
) {
1349 service_unwatch_control_pid(s
);
1351 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1352 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1353 if ((r
= service_spawn(s
,
1357 !s
->permissions_start_only
,
1358 !s
->root_directory_start_only
,
1359 &s
->control_pid
)) < 0)
1363 service_set_state(s
, SERVICE_STOP_POST
);
1365 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1370 log_warning("%s failed to run stop-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1371 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1374 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1383 if (s
->kill_mode
!= KILL_NONE
) {
1384 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1386 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1388 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1389 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1398 if (s
->main_pid
> 0) {
1399 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1405 if (s
->control_pid
> 0) {
1406 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1417 if (sent
&& (s
->main_pid
> 0 || s
->control_pid
> 0)) {
1418 if (s
->timeout_usec
> 0)
1419 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1422 service_set_state(s
, state
);
1423 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1424 service_enter_stop_post(s
, true);
1426 service_enter_dead(s
, true, true);
1431 log_warning("%s failed to kill processes: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1433 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1434 service_enter_stop_post(s
, false);
1436 service_enter_dead(s
, false, true);
1439 static void service_enter_stop(Service
*s
, bool success
) {
1446 service_unwatch_control_pid(s
);
1448 s
->control_command_id
= SERVICE_EXEC_STOP
;
1449 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1450 if ((r
= service_spawn(s
,
1454 !s
->permissions_start_only
,
1455 !s
->root_directory_start_only
,
1456 &s
->control_pid
)) < 0)
1459 service_set_state(s
, SERVICE_STOP
);
1461 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1466 log_warning("%s failed to run stop executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1467 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1470 static void service_enter_running(Service
*s
, bool success
) {
1476 if (main_pid_good(s
) != 0 &&
1477 cgroup_good(s
) != 0 &&
1478 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1479 service_set_state(s
, SERVICE_RUNNING
);
1480 else if (s
->valid_no_process
)
1481 service_set_state(s
, SERVICE_EXITED
);
1483 service_enter_stop(s
, true);
1486 static void service_enter_start_post(Service
*s
) {
1490 service_unwatch_control_pid(s
);
1492 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1493 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1494 if ((r
= service_spawn(s
,
1498 !s
->permissions_start_only
,
1499 !s
->root_directory_start_only
,
1500 &s
->control_pid
)) < 0)
1504 service_set_state(s
, SERVICE_START_POST
);
1506 service_enter_running(s
, true);
1511 log_warning("%s failed to run start-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1512 service_enter_stop(s
, false);
1515 static void service_enter_start(Service
*s
) {
1521 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1522 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1524 if (s
->type
== SERVICE_FORKING
)
1525 service_unwatch_control_pid(s
);
1527 service_unwatch_main_pid(s
);
1529 if ((r
= service_spawn(s
,
1530 s
->exec_command
[SERVICE_EXEC_START
],
1531 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
,
1538 if (s
->type
== SERVICE_SIMPLE
) {
1539 /* For simple services we immediately start
1540 * the START_POST binaries. */
1543 s
->main_pid_known
= true;
1545 service_enter_start_post(s
);
1547 } else if (s
->type
== SERVICE_FORKING
) {
1549 /* For forking services we wait until the start
1550 * process exited. */
1552 s
->control_pid
= pid
;
1554 s
->control_command_id
= SERVICE_EXEC_START
;
1555 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1556 service_set_state(s
, SERVICE_START
);
1558 } else if (s
->type
== SERVICE_FINISH
||
1559 s
->type
== SERVICE_DBUS
) {
1561 /* For finishing services we wait until the start
1562 * process exited, too, but it is our main process. */
1564 /* For D-Bus services we know the main pid right away,
1565 * but wait for the bus name to appear on the bus. */
1568 s
->main_pid_known
= true;
1570 service_set_state(s
, SERVICE_START
);
1572 assert_not_reached("Unknown service type");
1577 log_warning("%s failed to run start exectuable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1578 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1581 static void service_enter_start_pre(Service
*s
) {
1586 service_unwatch_control_pid(s
);
1588 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1589 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1590 if ((r
= service_spawn(s
,
1594 !s
->permissions_start_only
,
1595 !s
->root_directory_start_only
,
1596 &s
->control_pid
)) < 0)
1599 service_set_state(s
, SERVICE_START_PRE
);
1601 service_enter_start(s
);
1606 log_warning("%s failed to run start-pre executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1607 service_enter_dead(s
, false, true);
1610 static void service_enter_restart(Service
*s
) {
1614 service_enter_dead(s
, true, false);
1616 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1619 log_debug("%s scheduled restart job.", UNIT(s
)->meta
.id
);
1624 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1625 service_enter_dead(s
, false, false);
1628 static void service_enter_reload(Service
*s
) {
1633 service_unwatch_control_pid(s
);
1635 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1636 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1637 if ((r
= service_spawn(s
,
1641 !s
->permissions_start_only
,
1642 !s
->root_directory_start_only
,
1643 &s
->control_pid
)) < 0)
1646 service_set_state(s
, SERVICE_RELOAD
);
1648 service_enter_running(s
, true);
1653 log_warning("%s failed to run reload executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1654 service_enter_stop(s
, false);
1657 static void service_run_next(Service
*s
, bool success
) {
1661 assert(s
->control_command
);
1662 assert(s
->control_command
->command_next
);
1667 s
->control_command
= s
->control_command
->command_next
;
1669 service_unwatch_control_pid(s
);
1671 if ((r
= service_spawn(s
,
1675 !s
->permissions_start_only
,
1676 !s
->root_directory_start_only
,
1677 &s
->control_pid
)) < 0)
1683 log_warning("%s failed to run spawn next executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1685 if (s
->state
== SERVICE_START_PRE
)
1686 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1687 else if (s
->state
== SERVICE_STOP
)
1688 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1689 else if (s
->state
== SERVICE_STOP_POST
)
1690 service_enter_dead(s
, false, true);
1692 service_enter_stop(s
, false);
1695 static int service_start(Unit
*u
) {
1696 Service
*s
= SERVICE(u
);
1700 /* We cannot fulfill this request right now, try again later
1702 if (s
->state
== SERVICE_STOP
||
1703 s
->state
== SERVICE_STOP_SIGTERM
||
1704 s
->state
== SERVICE_STOP_SIGKILL
||
1705 s
->state
== SERVICE_STOP_POST
||
1706 s
->state
== SERVICE_FINAL_SIGTERM
||
1707 s
->state
== SERVICE_FINAL_SIGKILL
)
1710 /* Already on it! */
1711 if (s
->state
== SERVICE_START_PRE
||
1712 s
->state
== SERVICE_START
||
1713 s
->state
== SERVICE_START_POST
)
1716 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1718 /* Make sure we don't enter a busy loop of some kind. */
1719 if (!ratelimit_test(&s
->ratelimit
)) {
1720 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1725 s
->main_pid_known
= false;
1726 s
->allow_restart
= true;
1728 service_enter_start_pre(s
);
1732 static int service_stop(Unit
*u
) {
1733 Service
*s
= SERVICE(u
);
1737 /* Cannot do this now */
1738 if (s
->state
== SERVICE_START_PRE
||
1739 s
->state
== SERVICE_START
||
1740 s
->state
== SERVICE_START_POST
||
1741 s
->state
== SERVICE_RELOAD
)
1745 if (s
->state
== SERVICE_STOP
||
1746 s
->state
== SERVICE_STOP_SIGTERM
||
1747 s
->state
== SERVICE_STOP_SIGKILL
||
1748 s
->state
== SERVICE_STOP_POST
||
1749 s
->state
== SERVICE_FINAL_SIGTERM
||
1750 s
->state
== SERVICE_FINAL_SIGKILL
)
1753 if (s
->state
== SERVICE_AUTO_RESTART
) {
1754 service_set_state(s
, SERVICE_DEAD
);
1758 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1760 /* This is a user request, so don't do restarts on this
1762 s
->allow_restart
= false;
1764 service_enter_stop(s
, true);
1768 static int service_reload(Unit
*u
) {
1769 Service
*s
= SERVICE(u
);
1773 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1775 service_enter_reload(s
);
1779 static bool service_can_reload(Unit
*u
) {
1780 Service
*s
= SERVICE(u
);
1784 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1787 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1788 Service
*s
= SERVICE(u
);
1794 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1795 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1797 if (s
->control_pid
> 0)
1798 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) (s
->control_pid
));
1800 if (s
->main_pid
> 0)
1801 unit_serialize_item_format(u
, f
, "main-pid", "%u", (unsigned) (s
->main_pid
));
1803 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1805 /* There's a minor uncleanliness here: if there are multiple
1806 * commands attached here, we will start from the first one
1808 if (s
->control_command_id
>= 0)
1809 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
1811 if (s
->socket_fd
>= 0) {
1814 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
1817 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1823 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1824 Service
*s
= SERVICE(u
);
1832 if (streq(key
, "state")) {
1835 if ((state
= service_state_from_string(value
)) < 0)
1836 log_debug("Failed to parse state value %s", value
);
1838 s
->deserialized_state
= state
;
1839 } else if (streq(key
, "failure")) {
1842 if ((b
= parse_boolean(value
)) < 0)
1843 log_debug("Failed to parse failure value %s", value
);
1845 s
->failure
= b
|| s
->failure
;
1846 } else if (streq(key
, "control-pid")) {
1849 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1850 log_debug("Failed to parse control-pid value %s", value
);
1852 s
->control_pid
= (pid_t
) pid
;
1853 } else if (streq(key
, "main-pid")) {
1856 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1857 log_debug("Failed to parse main-pid value %s", value
);
1859 s
->main_pid
= (pid_t
) pid
;
1860 } else if (streq(key
, "main-pid-known")) {
1863 if ((b
= parse_boolean(value
)) < 0)
1864 log_debug("Failed to parse main-pid-known value %s", value
);
1866 s
->main_pid_known
= b
;
1867 } else if (streq(key
, "control-command")) {
1868 ServiceExecCommand id
;
1870 if ((id
= service_exec_command_from_string(value
)) < 0)
1871 log_debug("Failed to parse exec-command value %s", value
);
1873 s
->control_command_id
= id
;
1874 s
->control_command
= s
->exec_command
[id
];
1876 } else if (streq(key
, "socket-fd")) {
1879 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
1880 log_debug("Failed to parse socket-fd value %s", value
);
1883 if (s
->socket_fd
>= 0)
1884 close_nointr_nofail(s
->socket_fd
);
1885 s
->socket_fd
= fdset_remove(fds
, fd
);
1888 log_debug("Unknown serialization key '%s'", key
);
1893 static UnitActiveState
service_active_state(Unit
*u
) {
1896 return state_translation_table
[SERVICE(u
)->state
];
1899 static const char *service_sub_state_to_string(Unit
*u
) {
1902 return service_state_to_string(SERVICE(u
)->state
);
1905 static bool service_check_gc(Unit
*u
) {
1906 Service
*s
= SERVICE(u
);
1910 return !!s
->sysv_path
;
1913 static bool service_check_snapshot(Unit
*u
) {
1914 Service
*s
= SERVICE(u
);
1918 return !s
->got_socket_fd
;
1921 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1922 Service
*s
= SERVICE(u
);
1928 success
= code
== CLD_EXITED
&& status
== 0;
1929 s
->failure
= s
->failure
|| !success
;
1931 if (s
->main_pid
== pid
) {
1933 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1936 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1937 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1938 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1941 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1943 /* The service exited, so the service is officially
1948 case SERVICE_START_POST
:
1949 case SERVICE_RELOAD
:
1951 /* Need to wait until the operation is
1956 assert(s
->type
== SERVICE_FINISH
);
1958 /* This was our main goal, so let's go on */
1960 service_enter_start_post(s
);
1962 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1965 case SERVICE_RUNNING
:
1966 service_enter_running(s
, success
);
1969 case SERVICE_STOP_SIGTERM
:
1970 case SERVICE_STOP_SIGKILL
:
1972 if (!control_pid_good(s
))
1973 service_enter_stop_post(s
, success
);
1975 /* If there is still a control process, wait for that first */
1979 assert_not_reached("Uh, main process died at wrong time.");
1982 } else if (s
->control_pid
== pid
) {
1984 if (s
->control_command
)
1985 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1989 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1991 /* If we are shutting things down anyway we
1992 * don't care about failing commands. */
1994 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1996 /* There is another command to *
1997 * execute, so let's do that. */
1999 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2000 service_run_next(s
, success
);
2003 /* No further commands for this step, so let's
2004 * figure out what to do next */
2006 s
->control_command
= NULL
;
2007 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2009 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2013 case SERVICE_START_PRE
:
2015 service_enter_start(s
);
2017 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2021 assert(s
->type
== SERVICE_FORKING
);
2023 /* Let's try to load the pid
2024 * file here if we can. We
2025 * ignore the return value,
2026 * since the PID file might
2027 * actually be created by a
2028 * START_POST script */
2032 service_load_pid_file(s
);
2034 service_enter_start_post(s
);
2036 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2040 case SERVICE_START_POST
:
2041 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
2044 /* Hmm, let's see if we can
2045 * load the pid now after the
2046 * start-post scripts got
2049 if ((r
= service_load_pid_file(s
)) < 0)
2050 log_warning("%s: failed to load PID file %s: %s", UNIT(s
)->meta
.id
, s
->pid_file
, strerror(-r
));
2055 case SERVICE_RELOAD
:
2057 service_enter_running(s
, true);
2059 service_enter_stop(s
, false);
2064 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2067 case SERVICE_STOP_SIGTERM
:
2068 case SERVICE_STOP_SIGKILL
:
2069 if (main_pid_good(s
) <= 0)
2070 service_enter_stop_post(s
, success
);
2072 /* If there is still a service
2073 * process around, wait until
2074 * that one quit, too */
2077 case SERVICE_STOP_POST
:
2078 case SERVICE_FINAL_SIGTERM
:
2079 case SERVICE_FINAL_SIGKILL
:
2080 service_enter_dead(s
, success
, true);
2084 assert_not_reached("Uh, control process died at wrong time.");
2088 assert_not_reached("Got SIGCHLD for unkown PID");
2091 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2092 Service
*s
= SERVICE(u
);
2095 assert(elapsed
== 1);
2097 assert(w
== &s
->timer_watch
);
2101 case SERVICE_START_PRE
:
2103 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2104 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2107 case SERVICE_START_POST
:
2108 case SERVICE_RELOAD
:
2109 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2110 service_enter_stop(s
, false);
2114 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2115 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2118 case SERVICE_STOP_SIGTERM
:
2119 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2120 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2123 case SERVICE_STOP_SIGKILL
:
2124 /* Uh, wie sent a SIGKILL and it is still not gone?
2125 * Must be something we cannot kill, so let's just be
2126 * weirded out and continue */
2128 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2129 service_enter_stop_post(s
, false);
2132 case SERVICE_STOP_POST
:
2133 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2134 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2137 case SERVICE_FINAL_SIGTERM
:
2138 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2139 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2142 case SERVICE_FINAL_SIGKILL
:
2143 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
2144 service_enter_dead(s
, false, true);
2147 case SERVICE_AUTO_RESTART
:
2148 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2149 service_enter_restart(s
);
2153 assert_not_reached("Timeout at wrong time.");
2157 static void service_cgroup_notify_event(Unit
*u
) {
2158 Service
*s
= SERVICE(u
);
2162 log_debug("%s: cgroup is empty", u
->meta
.id
);
2166 /* Waiting for SIGCHLD is usually more interesting,
2167 * because it includes return codes/signals. Which is
2168 * why we ignore the cgroup events for most cases,
2169 * except when we don't know pid which to expect the
2172 case SERVICE_RUNNING
:
2173 service_enter_running(s
, true);
2181 static int service_enumerate(Manager
*m
) {
2185 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2190 STRV_FOREACH(p
, m
->sysvrcnd_path
)
2191 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
2196 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
2204 if (!(d
= opendir(path
))) {
2205 if (errno
!= ENOENT
)
2206 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2211 while ((de
= readdir(d
))) {
2214 if (ignore_file(de
->d_name
))
2217 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2220 if (strlen(de
->d_name
) < 4)
2225 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
2230 if (access(fpath
, X_OK
) < 0) {
2232 if (errno
!= ENOENT
)
2233 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2240 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
2245 if ((r
= manager_load_unit(m
, name
, NULL
, &service
)) < 0)
2248 if (de
->d_name
[0] == 'S') {
2249 Unit
*runlevel_target
;
2251 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], NULL
, &runlevel_target
)) < 0)
2254 if ((r
= unit_add_dependency(runlevel_target
, UNIT_WANTS
, service
, true)) < 0)
2257 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, runlevel_target
, true)) < 0)
2260 } else if (de
->d_name
[0] == 'K' &&
2261 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
2262 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
2264 Unit
*shutdown_target
;
2266 /* We honour K links only for
2267 * halt/reboot. For the normal
2268 * runlevels we assume the
2270 * implicitly added by the
2271 * core logic. Also, we don't
2272 * really distuingish here
2273 * between the runlevels 0 and
2274 * 6 and just add them to the
2275 * special shutdown target. */
2277 if ((r
= manager_load_unit(m
, SPECIAL_SHUTDOWN_TARGET
, NULL
, &shutdown_target
)) < 0)
2280 if ((r
= unit_add_dependency(service
, UNIT_CONFLICTS
, shutdown_target
, true)) < 0)
2283 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, shutdown_target
, true)) < 0)
2300 static void service_bus_name_owner_change(
2303 const char *old_owner
,
2304 const char *new_owner
) {
2306 Service
*s
= SERVICE(u
);
2311 assert(streq(s
->bus_name
, name
));
2312 assert(old_owner
|| new_owner
);
2314 if (old_owner
&& new_owner
)
2315 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2317 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2319 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2321 s
->bus_name_good
= !!new_owner
;
2323 if (s
->type
== SERVICE_DBUS
) {
2325 /* service_enter_running() will figure out what to
2327 if (s
->state
== SERVICE_RUNNING
)
2328 service_enter_running(s
, true);
2329 else if (s
->state
== SERVICE_START
&& new_owner
)
2330 service_enter_start_post(s
);
2332 } else if (new_owner
&&
2334 (s
->state
== SERVICE_START
||
2335 s
->state
== SERVICE_START_POST
||
2336 s
->state
== SERVICE_RUNNING
||
2337 s
->state
== SERVICE_RELOAD
)) {
2339 /* Try to acquire PID from bus service */
2340 log_debug("Trying to acquire PID from D-Bus name...");
2342 bus_query_pid(u
->meta
.manager
, name
);
2346 static void service_bus_query_pid_done(
2351 Service
*s
= SERVICE(u
);
2356 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2358 if (s
->main_pid
<= 0 &&
2359 (s
->state
== SERVICE_START
||
2360 s
->state
== SERVICE_START_POST
||
2361 s
->state
== SERVICE_RUNNING
||
2362 s
->state
== SERVICE_RELOAD
))
2366 int service_set_socket_fd(Service
*s
, int fd
) {
2370 /* This is called by the socket code when instantiating a new
2371 * service for a stream socket and the socket needs to be
2374 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
2377 if (s
->socket_fd
>= 0)
2380 if (s
->state
!= SERVICE_DEAD
)
2384 s
->got_socket_fd
= true;
2388 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2389 [SERVICE_DEAD
] = "dead",
2390 [SERVICE_START_PRE
] = "start-pre",
2391 [SERVICE_START
] = "start",
2392 [SERVICE_START_POST
] = "start-post",
2393 [SERVICE_RUNNING
] = "running",
2394 [SERVICE_EXITED
] = "exited",
2395 [SERVICE_RELOAD
] = "reload",
2396 [SERVICE_STOP
] = "stop",
2397 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2398 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2399 [SERVICE_STOP_POST
] = "stop-post",
2400 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2401 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2402 [SERVICE_MAINTAINANCE
] = "maintainance",
2403 [SERVICE_AUTO_RESTART
] = "auto-restart",
2406 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2408 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2409 [SERVICE_ONCE
] = "once",
2410 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2411 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2414 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2416 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2417 [SERVICE_FORKING
] = "forking",
2418 [SERVICE_SIMPLE
] = "simple",
2419 [SERVICE_FINISH
] = "finish",
2420 [SERVICE_DBUS
] = "dbus"
2423 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2425 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2426 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2427 [SERVICE_EXEC_START
] = "ExecStart",
2428 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2429 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2430 [SERVICE_EXEC_STOP
] = "ExecStop",
2431 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2434 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2436 const UnitVTable service_vtable
= {
2437 .suffix
= ".service",
2439 .init
= service_init
,
2440 .done
= service_done
,
2441 .load
= service_load
,
2443 .coldplug
= service_coldplug
,
2445 .dump
= service_dump
,
2447 .start
= service_start
,
2448 .stop
= service_stop
,
2449 .reload
= service_reload
,
2451 .can_reload
= service_can_reload
,
2453 .serialize
= service_serialize
,
2454 .deserialize_item
= service_deserialize_item
,
2456 .active_state
= service_active_state
,
2457 .sub_state_to_string
= service_sub_state_to_string
,
2459 .check_gc
= service_check_gc
,
2460 .check_snapshot
= service_check_snapshot
,
2462 .sigchld_event
= service_sigchld_event
,
2463 .timer_event
= service_timer_event
,
2465 .cgroup_notify_empty
= service_cgroup_notify_event
,
2467 .bus_name_owner_change
= service_bus_name_owner_change
,
2468 .bus_query_pid_done
= service_bus_query_pid_done
,
2470 .bus_message_handler
= bus_service_message_handler
,
2472 .enumerate
= service_enumerate