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"
35 #define COMMENTS "#;\n"
36 #define NEWLINES "\n\r"
39 static const char * const rcnd_table
[] = {
40 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET
,
41 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET
,
42 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET
,
43 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET
,
44 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET
,
45 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET
,
46 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET
,
47 "/boot.d", SPECIAL_BASIC_TARGET
50 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
51 [SERVICE_DEAD
] = UNIT_INACTIVE
,
52 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
53 [SERVICE_START
] = UNIT_ACTIVATING
,
54 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
55 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
56 [SERVICE_EXITED
] = UNIT_ACTIVE
,
57 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
58 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
59 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
60 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
61 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
62 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
63 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
64 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
65 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
68 static void service_unwatch_control_pid(Service
*s
) {
71 if (s
->control_pid
<= 0)
74 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
78 static void service_unwatch_main_pid(Service
*s
) {
84 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
88 static void service_close_socket_fd(Service
*s
) {
94 close_nointr_nofail(s
->socket_fd
);
98 static void service_done(Unit
*u
) {
99 Service
*s
= SERVICE(u
);
109 free(s
->sysv_runlevels
);
110 s
->sysv_runlevels
= NULL
;
112 exec_context_done(&s
->exec_context
);
113 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
114 s
->control_command
= NULL
;
116 /* This will leak a process, but at least no memory or any of
118 service_unwatch_main_pid(s
);
119 service_unwatch_control_pid(s
);
122 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
127 service_close_socket_fd(s
);
129 unit_unwatch_timer(u
, &s
->timer_watch
);
132 static int sysv_translate_name(const char *name
, char **_r
) {
134 static const char * const table
[] = {
135 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
136 "$network", SPECIAL_NETWORK_TARGET
,
137 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
138 "$portmap", SPECIAL_RPCBIND_TARGET
,
139 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
140 "$syslog", SPECIAL_SYSLOG_TARGET
,
141 "$time", SPECIAL_RTC_SET_TARGET
147 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
148 if (streq(table
[i
], name
)) {
149 if (!(r
= strdup(table
[i
+1])))
158 if (asprintf(&r
, "%s.service", name
) < 0)
169 static int sysv_chkconfig_order(Service
*s
) {
175 if (s
->sysv_start_priority
< 0)
178 /* For each pair of services where at least one lacks a LSB
179 * header, we use the start priority value to order things. */
181 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
185 t
= (Service
*) other
;
190 if (t
->sysv_start_priority
< 0)
193 /* If both units have modern headers we don't care
194 * about the priorities */
195 if ((!s
->sysv_path
|| s
->sysv_has_lsb
) &&
196 (!t
->sysv_path
|| t
->sysv_has_lsb
))
199 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
201 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
206 /* FIXME: Maybe we should compare the name here lexicographically? */
208 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
))) < 0)
215 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
218 if (!(c
= new0(ExecCommand
, 1)))
221 if (!(c
->path
= strdup(path
))) {
226 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
235 static int sysv_exec_commands(Service
*s
) {
239 assert(s
->sysv_path
);
241 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
243 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
245 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
247 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
249 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
251 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
256 static int priority_from_rcd(Service
*s
, const char *init_script
) {
260 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvrcnd_path
)
261 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
266 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0)
274 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
279 while ((de
= readdir(d
))) {
282 if (ignore_file(de
->d_name
))
285 if (de
->d_name
[0] != 'S')
288 if (strlen(de
->d_name
) < 4)
291 if (!streq(de
->d_name
+ 3, init_script
))
294 /* Yay, we found it! Now decode the priority */
296 a
= undecchar(de
->d_name
[1]);
297 b
= undecchar(de
->d_name
[2]);
302 s
->sysv_start_priority
= a
*10 + b
;
304 log_debug("Determined priority %i from link farm for %s", s
->sysv_start_priority
, UNIT(s
)->meta
.id
);
316 static int service_load_sysv_path(Service
*s
, const char *path
) {
333 if (!(f
= fopen(path
, "re"))) {
334 r
= errno
== ENOENT
? 0 : -errno
;
338 s
->type
= SERVICE_FORKING
;
339 s
->restart
= SERVICE_ONCE
;
342 if (!(s
->sysv_path
= strdup(path
))) {
348 char l
[LINE_MAX
], *t
;
350 if (!fgets(l
, sizeof(l
), f
)) {
355 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
365 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
367 s
->sysv_has_lsb
= true;
371 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
377 t
+= strspn(t
, WHITESPACE
);
379 if (state
== NORMAL
) {
381 /* Try to parse Red Hat style chkconfig headers */
383 if (startswith(t
, "chkconfig:")) {
385 char runlevels
[16], *k
;
389 if (sscanf(t
+10, "%15s %i %*i",
391 &start_priority
) != 2) {
393 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
397 if (start_priority
< 0 || start_priority
> 99)
398 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
400 s
->sysv_start_priority
= start_priority
;
402 char_array_0(runlevels
);
403 k
= delete_chars(runlevels
, WHITESPACE
"-");
408 if (!(d
= strdup(k
))) {
413 free(s
->sysv_runlevels
);
414 s
->sysv_runlevels
= d
;
418 } else if (startswith(t
, "description:")) {
420 size_t k
= strlen(t
);
423 if (t
[k
-1] == '\\') {
428 if (!(d
= strdup(strstrip(t
+12)))) {
433 free(u
->meta
.description
);
434 u
->meta
.description
= d
;
436 } else if (startswith(t
, "pidfile:")) {
443 if (!path_is_absolute(fn
)) {
444 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
448 if (!(fn
= strdup(fn
))) {
457 } else if (state
== DESCRIPTION
) {
459 /* Try to parse Red Hat style description
462 size_t k
= strlen(t
);
470 assert(u
->meta
.description
);
471 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
476 free(u
->meta
.description
);
477 u
->meta
.description
= d
;
479 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
481 if (startswith(t
, "Provides:")) {
487 FOREACH_WORD(w
, z
, t
+9, i
) {
490 if (!(n
= strndup(w
, z
))) {
495 r
= sysv_translate_name(n
, &m
);
504 if (unit_name_to_type(m
) == UNIT_SERVICE
)
505 r
= unit_add_name(u
, m
);
507 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
, NULL
)) >= 0)
508 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
);
517 } else if (startswith(t
, "Required-Start:") ||
518 startswith(t
, "Should-Start:")) {
524 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
527 if (!(n
= strndup(w
, z
))) {
532 r
= sysv_translate_name(n
, &m
);
541 r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
, NULL
);
547 } else if (startswith(t
, "Default-Start:")) {
552 k
= delete_chars(t
+14, WHITESPACE
"-");
555 if (!(d
= strdup(k
))) {
560 free(s
->sysv_runlevels
);
561 s
->sysv_runlevels
= d
;
564 } else if (startswith(t
, "Description:")) {
567 state
= LSB_DESCRIPTION
;
569 if (!(d
= strdup(strstrip(t
+12)))) {
574 free(u
->meta
.description
);
575 u
->meta
.description
= d
;
577 } else if (startswith(t
, "Short-Description:") &&
578 !u
->meta
.description
) {
581 /* We use the short description only
582 * if no long description is set. */
586 if (!(d
= strdup(strstrip(t
+18)))) {
591 u
->meta
.description
= d
;
593 } else if (state
== LSB_DESCRIPTION
) {
595 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
598 assert(u
->meta
.description
);
599 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
604 free(u
->meta
.description
);
605 u
->meta
.description
= d
;
612 /* If init scripts have no LSB header, then we enforce the
613 * ordering via the chkconfig priorities. We try to determine
614 * a priority for *all* init scripts here, since they are
615 * needed as soon as at least one non-LSB script is used. */
617 if (s
->sysv_start_priority
< 0) {
618 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", u
->meta
.id
);
620 if ((r
= priority_from_rcd(s
, file_name_from_path(path
))) < 0)
623 if (s
->sysv_start_priority
< 0)
624 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u
->meta
.id
);
627 if ((r
= sysv_exec_commands(s
)) < 0)
630 if (!s
->sysv_runlevels
|| chars_intersect("12345", s
->sysv_runlevels
)) {
631 /* If there a runlevels configured for this service
632 * but none of the standard ones, then we assume this
633 * is some special kind of service (which might be
634 * needed for early boot) and don't create any links
637 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
)) < 0 ||
638 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
)) < 0)
642 /* Special setting for all SysV services */
643 s
->valid_no_process
= true;
645 /* Don't timeout special services during boot (like fsck) */
646 if (s
->sysv_runlevels
&& !chars_intersect("12345", s
->sysv_runlevels
))
647 s
->timeout_usec
= -1;
649 u
->meta
.load_state
= UNIT_LOADED
;
660 static int service_load_sysv_name(Service
*s
, const char *name
) {
666 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
670 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
673 assert(endswith(path
, ".service"));
674 path
[strlen(path
)-8] = 0;
676 r
= service_load_sysv_path(s
, path
);
682 if ((UNIT(s
)->meta
.load_state
!= UNIT_STUB
))
689 static int service_load_sysv(Service
*s
) {
696 /* Load service data from SysV init scripts, preferably with
699 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
702 if ((t
= UNIT(s
)->meta
.id
))
703 if ((r
= service_load_sysv_name(s
, t
)) < 0)
706 if (UNIT(s
)->meta
.load_state
== UNIT_STUB
)
707 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
708 if (t
== UNIT(s
)->meta
.id
)
711 if ((r
== service_load_sysv_name(s
, t
)) < 0)
714 if (UNIT(s
)->meta
.load_state
!= UNIT_STUB
)
721 static int service_add_bus_name(Service
*s
) {
728 if (asprintf(&n
, "dbus-%s.service", s
->bus_name
) < 0)
731 r
= unit_merge_by_name(UNIT(s
), n
);
737 static void service_init(Unit
*u
) {
738 Service
*s
= SERVICE(u
);
741 assert(u
->meta
.load_state
== UNIT_STUB
);
743 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
744 s
->restart_usec
= DEFAULT_RESTART_USEC
;
745 s
->timer_watch
.type
= WATCH_INVALID
;
746 s
->sysv_start_priority
= -1;
749 exec_context_init(&s
->exec_context
);
751 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
754 static int service_verify(Service
*s
) {
757 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
760 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
761 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->meta
.id
);
765 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
766 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->meta
.id
);
773 static int service_load(Unit
*u
) {
775 Service
*s
= SERVICE(u
);
779 /* Load a .service file */
780 if ((r
= unit_load_fragment(u
)) < 0)
783 /* Load a classic init script as a fallback, if we couldn't find anything */
784 if (u
->meta
.load_state
== UNIT_STUB
)
785 if ((r
= service_load_sysv(s
)) < 0)
788 /* Still nothing found? Then let's give up */
789 if (u
->meta
.load_state
== UNIT_STUB
)
792 /* We were able to load something, then let's add in the
793 * dropin directories. */
794 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
797 /* This is a new unit? Then let's add in some extras */
798 if (u
->meta
.load_state
== UNIT_LOADED
) {
799 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
802 if ((r
= unit_add_default_cgroup(u
)) < 0)
805 if ((r
= sysv_chkconfig_order(s
)) < 0)
809 if ((r
= service_add_bus_name(s
)) < 0)
812 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
817 return service_verify(s
);
820 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
822 ServiceExecCommand c
;
823 Service
*s
= SERVICE(u
);
829 p2
= strappend(prefix
, "\t");
830 prefix2
= p2
? p2
: prefix
;
833 "%sService State: %s\n"
834 "%sPermissionsStartOnly: %s\n"
835 "%sRootDirectoryStartOnly: %s\n"
836 "%sValidNoProcess: %s\n"
839 prefix
, service_state_to_string(s
->state
),
840 prefix
, yes_no(s
->permissions_start_only
),
841 prefix
, yes_no(s
->root_directory_start_only
),
842 prefix
, yes_no(s
->valid_no_process
),
843 prefix
, kill_mode_to_string(s
->kill_mode
),
844 prefix
, service_type_to_string(s
->type
));
846 if (s
->control_pid
> 0)
848 "%sControl PID: %llu\n",
849 prefix
, (unsigned long long) s
->control_pid
);
853 "%sMain PID: %llu\n",
854 prefix
, (unsigned long long) s
->main_pid
);
859 prefix
, s
->pid_file
);
864 "%sBus Name Good: %s\n",
866 prefix
, yes_no(s
->bus_name_good
));
868 exec_context_dump(&s
->exec_context
, f
, prefix
);
870 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
872 if (!s
->exec_command
[c
])
875 fprintf(f
, "%s→ %s:\n",
876 prefix
, service_exec_command_to_string(c
));
878 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
883 "%sSysV Init Script Path: %s\n"
884 "%sSysV Init Script has LSB Header: %s\n",
885 prefix
, s
->sysv_path
,
886 prefix
, yes_no(s
->sysv_has_lsb
));
888 if (s
->sysv_start_priority
>= 0)
890 "%sSysVStartPriority: %i\n",
891 prefix
, s
->sysv_start_priority
);
893 if (s
->sysv_runlevels
)
894 fprintf(f
, "%sSysVRunLevels: %s\n",
895 prefix
, s
->sysv_runlevels
);
900 static int service_load_pid_file(Service
*s
) {
907 if (s
->main_pid_known
)
910 assert(s
->main_pid
<= 0);
915 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
918 if ((r
= safe_atolu(k
, &p
)) < 0) {
923 if ((unsigned long) (pid_t
) p
!= p
)
926 if (kill((pid_t
) p
, 0) < 0 && errno
!= EPERM
) {
927 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
928 (unsigned long long) p
, s
->pid_file
);
932 if ((r
= unit_watch_pid(UNIT(s
), (pid_t
) p
)) < 0)
933 /* FIXME: we need to do something here */
936 s
->main_pid
= (pid_t
) p
;
937 s
->main_pid_known
= true;
942 static int service_get_sockets(Service
*s
, Set
**_set
) {
951 /* Collects all Socket objects that belong to this
952 * service. Note that a service might have multiple sockets
953 * via multiple names. */
955 if (!(set
= set_new(NULL
, NULL
)))
958 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
962 /* Look for all socket objects that go by any of our
963 * units and collect their fds */
965 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
970 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
976 if ((r
= set_put(set
, p
)) < 0)
988 static int service_notify_sockets_dead(Service
*s
) {
996 /* Notifies all our sockets when we die */
997 if ((r
= service_get_sockets(s
, &set
)) < 0)
1000 SET_FOREACH(sock
, set
, i
)
1001 socket_notify_service_dead(sock
);
1008 static void service_set_state(Service
*s
, ServiceState state
) {
1009 ServiceState old_state
;
1012 old_state
= s
->state
;
1015 if (state
!= SERVICE_START_PRE
&&
1016 state
!= SERVICE_START
&&
1017 state
!= SERVICE_START_POST
&&
1018 state
!= SERVICE_RELOAD
&&
1019 state
!= SERVICE_STOP
&&
1020 state
!= SERVICE_STOP_SIGTERM
&&
1021 state
!= SERVICE_STOP_SIGKILL
&&
1022 state
!= SERVICE_STOP_POST
&&
1023 state
!= SERVICE_FINAL_SIGTERM
&&
1024 state
!= SERVICE_FINAL_SIGKILL
&&
1025 state
!= SERVICE_AUTO_RESTART
)
1026 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1028 if (state
!= SERVICE_START
&&
1029 state
!= SERVICE_START_POST
&&
1030 state
!= SERVICE_RUNNING
&&
1031 state
!= SERVICE_RELOAD
&&
1032 state
!= SERVICE_STOP
&&
1033 state
!= SERVICE_STOP_SIGTERM
&&
1034 state
!= SERVICE_STOP_SIGKILL
)
1035 service_unwatch_main_pid(s
);
1037 if (state
!= SERVICE_START_PRE
&&
1038 state
!= SERVICE_START
&&
1039 state
!= SERVICE_START_POST
&&
1040 state
!= SERVICE_RELOAD
&&
1041 state
!= SERVICE_STOP
&&
1042 state
!= SERVICE_STOP_SIGTERM
&&
1043 state
!= SERVICE_STOP_SIGKILL
&&
1044 state
!= SERVICE_STOP_POST
&&
1045 state
!= SERVICE_FINAL_SIGTERM
&&
1046 state
!= SERVICE_FINAL_SIGKILL
) {
1047 service_unwatch_control_pid(s
);
1048 s
->control_command
= NULL
;
1051 if (state
== SERVICE_DEAD
||
1052 state
== SERVICE_STOP
||
1053 state
== SERVICE_STOP_SIGTERM
||
1054 state
== SERVICE_STOP_SIGKILL
||
1055 state
== SERVICE_STOP_POST
||
1056 state
== SERVICE_FINAL_SIGTERM
||
1057 state
== SERVICE_FINAL_SIGKILL
||
1058 state
== SERVICE_MAINTAINANCE
||
1059 state
== SERVICE_AUTO_RESTART
)
1060 service_notify_sockets_dead(s
);
1062 if (state
!= SERVICE_START_PRE
&&
1063 state
!= SERVICE_START
&&
1064 !(state
== SERVICE_DEAD
&& UNIT(s
)->meta
.job
))
1065 service_close_socket_fd(s
);
1067 if (old_state
!= state
)
1068 log_debug("%s changed %s → %s", UNIT(s
)->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1070 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1073 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1077 unsigned rn_fds
= 0;
1085 if ((r
= service_get_sockets(s
, &set
)) < 0)
1088 SET_FOREACH(sock
, set
, i
) {
1092 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1104 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1110 memcpy(t
, rfds
, rn_fds
);
1111 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1116 rn_fds
= rn_fds
+cn_fds
;
1134 static int service_spawn(
1139 bool apply_permissions
,
1154 if (s
->socket_fd
>= 0) {
1155 fds
= &s
->socket_fd
;
1157 } else if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1162 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1165 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1167 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1178 UNIT(s
)->meta
.manager
->confirm_spawn
,
1179 UNIT(s
)->meta
.cgroup_bondings
,
1187 if (s
->socket_fd
>= 0)
1188 service_close_socket_fd(s
);
1193 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1194 /* FIXME: we need to do something here */
1205 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1210 static int main_pid_good(Service
*s
) {
1213 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1216 /* If we know the pid file, then lets just check if it is
1218 if (s
->main_pid_known
)
1219 return s
->main_pid
> 0;
1221 /* We don't know the pid */
1225 static int control_pid_good(Service
*s
) {
1228 return s
->control_pid
> 0;
1231 static int cgroup_good(Service
*s
) {
1236 if (s
->valid_no_process
)
1239 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
)) < 0)
1245 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1252 if (allow_restart
&&
1253 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1254 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1256 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1259 service_set_state(s
, SERVICE_AUTO_RESTART
);
1261 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1266 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1267 service_enter_dead(s
, false, false);
1270 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1272 static void service_enter_stop_post(Service
*s
, bool success
) {
1279 service_unwatch_control_pid(s
);
1281 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1282 if ((r
= service_spawn(s
,
1286 !s
->permissions_start_only
,
1287 !s
->root_directory_start_only
,
1288 &s
->control_pid
)) < 0)
1292 service_set_state(s
, SERVICE_STOP_POST
);
1294 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1299 log_warning("%s failed to run stop-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1300 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1303 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1312 if (s
->kill_mode
!= KILL_NONE
) {
1313 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1315 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1317 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1318 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1327 if (s
->main_pid
> 0) {
1328 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1334 if (s
->control_pid
> 0) {
1335 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1347 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1350 service_set_state(s
, state
);
1351 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1352 service_enter_stop_post(s
, true);
1354 service_enter_dead(s
, true, true);
1359 log_warning("%s failed to kill processes: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1361 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1362 service_enter_stop_post(s
, false);
1364 service_enter_dead(s
, false, true);
1367 static void service_enter_stop(Service
*s
, bool success
) {
1374 service_unwatch_control_pid(s
);
1376 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1377 if ((r
= service_spawn(s
,
1381 !s
->permissions_start_only
,
1382 !s
->root_directory_start_only
,
1383 &s
->control_pid
)) < 0)
1386 service_set_state(s
, SERVICE_STOP
);
1388 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1393 log_warning("%s failed to run stop executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1394 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1397 static void service_enter_running(Service
*s
, bool success
) {
1403 if (main_pid_good(s
) != 0 &&
1404 cgroup_good(s
) != 0 &&
1405 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1406 service_set_state(s
, SERVICE_RUNNING
);
1407 else if (s
->valid_no_process
)
1408 service_set_state(s
, SERVICE_EXITED
);
1410 service_enter_stop(s
, true);
1413 static void service_enter_start_post(Service
*s
) {
1417 service_unwatch_control_pid(s
);
1419 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1420 if ((r
= service_spawn(s
,
1424 !s
->permissions_start_only
,
1425 !s
->root_directory_start_only
,
1426 &s
->control_pid
)) < 0)
1430 service_set_state(s
, SERVICE_START_POST
);
1432 service_enter_running(s
, true);
1437 log_warning("%s failed to run start-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1438 service_enter_stop(s
, false);
1441 static void service_enter_start(Service
*s
) {
1447 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1448 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1450 if (s
->type
== SERVICE_FORKING
)
1451 service_unwatch_control_pid(s
);
1453 service_unwatch_main_pid(s
);
1455 if ((r
= service_spawn(s
,
1456 s
->exec_command
[SERVICE_EXEC_START
],
1457 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
,
1464 if (s
->type
== SERVICE_SIMPLE
) {
1465 /* For simple services we immediately start
1466 * the START_POST binaries. */
1469 s
->main_pid_known
= true;
1471 service_enter_start_post(s
);
1473 } else if (s
->type
== SERVICE_FORKING
) {
1475 /* For forking services we wait until the start
1476 * process exited. */
1478 s
->control_pid
= pid
;
1480 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1481 service_set_state(s
, SERVICE_START
);
1483 } else if (s
->type
== SERVICE_FINISH
||
1484 s
->type
== SERVICE_DBUS
) {
1486 /* For finishing services we wait until the start
1487 * process exited, too, but it is our main process. */
1489 /* For D-Bus services we know the main pid right away,
1490 * but wait for the bus name to appear on the bus. */
1493 s
->main_pid_known
= true;
1495 service_set_state(s
, SERVICE_START
);
1497 assert_not_reached("Unknown service type");
1502 log_warning("%s failed to run start exectuable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1503 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1506 static void service_enter_start_pre(Service
*s
) {
1511 service_unwatch_control_pid(s
);
1513 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1514 if ((r
= service_spawn(s
,
1518 !s
->permissions_start_only
,
1519 !s
->root_directory_start_only
,
1520 &s
->control_pid
)) < 0)
1523 service_set_state(s
, SERVICE_START_PRE
);
1525 service_enter_start(s
);
1530 log_warning("%s failed to run start-pre executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1531 service_enter_dead(s
, false, true);
1534 static void service_enter_restart(Service
*s
) {
1538 service_enter_dead(s
, true, false);
1540 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1543 log_debug("%s scheduled restart job.", UNIT(s
)->meta
.id
);
1548 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1549 service_enter_dead(s
, false, false);
1552 static void service_enter_reload(Service
*s
) {
1557 service_unwatch_control_pid(s
);
1559 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1560 if ((r
= service_spawn(s
,
1564 !s
->permissions_start_only
,
1565 !s
->root_directory_start_only
,
1566 &s
->control_pid
)) < 0)
1569 service_set_state(s
, SERVICE_RELOAD
);
1571 service_enter_running(s
, true);
1576 log_warning("%s failed to run reload executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1577 service_enter_stop(s
, false);
1580 static void service_run_next(Service
*s
, bool success
) {
1584 assert(s
->control_command
);
1585 assert(s
->control_command
->command_next
);
1590 s
->control_command
= s
->control_command
->command_next
;
1592 service_unwatch_control_pid(s
);
1594 if ((r
= service_spawn(s
,
1598 !s
->permissions_start_only
,
1599 !s
->root_directory_start_only
,
1600 &s
->control_pid
)) < 0)
1606 log_warning("%s failed to run spawn next executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1608 if (s
->state
== SERVICE_START_PRE
)
1609 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1610 else if (s
->state
== SERVICE_STOP
)
1611 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1612 else if (s
->state
== SERVICE_STOP_POST
)
1613 service_enter_dead(s
, false, true);
1615 service_enter_stop(s
, false);
1618 static int service_start(Unit
*u
) {
1619 Service
*s
= SERVICE(u
);
1623 /* We cannot fulfill this request right now, try again later
1625 if (s
->state
== SERVICE_STOP
||
1626 s
->state
== SERVICE_STOP_SIGTERM
||
1627 s
->state
== SERVICE_STOP_SIGKILL
||
1628 s
->state
== SERVICE_STOP_POST
||
1629 s
->state
== SERVICE_FINAL_SIGTERM
||
1630 s
->state
== SERVICE_FINAL_SIGKILL
)
1633 /* Already on it! */
1634 if (s
->state
== SERVICE_START_PRE
||
1635 s
->state
== SERVICE_START
||
1636 s
->state
== SERVICE_START_POST
)
1639 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1641 /* Make sure we don't enter a busy loop of some kind. */
1642 if (!ratelimit_test(&s
->ratelimit
)) {
1643 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1648 s
->main_pid_known
= false;
1650 service_enter_start_pre(s
);
1654 static int service_stop(Unit
*u
) {
1655 Service
*s
= SERVICE(u
);
1659 /* Cannot do this now */
1660 if (s
->state
== SERVICE_START_PRE
||
1661 s
->state
== SERVICE_START
||
1662 s
->state
== SERVICE_START_POST
||
1663 s
->state
== SERVICE_RELOAD
)
1667 if (s
->state
== SERVICE_STOP
||
1668 s
->state
== SERVICE_STOP_SIGTERM
||
1669 s
->state
== SERVICE_STOP_SIGKILL
||
1670 s
->state
== SERVICE_STOP_POST
||
1671 s
->state
== SERVICE_FINAL_SIGTERM
||
1672 s
->state
== SERVICE_FINAL_SIGKILL
)
1675 if (s
->state
== SERVICE_AUTO_RESTART
) {
1676 service_set_state(s
, SERVICE_DEAD
);
1680 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1682 service_enter_stop(s
, true);
1686 static int service_reload(Unit
*u
) {
1687 Service
*s
= SERVICE(u
);
1691 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1693 service_enter_reload(s
);
1697 static bool service_can_reload(Unit
*u
) {
1698 Service
*s
= SERVICE(u
);
1702 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1705 static UnitActiveState
service_active_state(Unit
*u
) {
1708 return state_translation_table
[SERVICE(u
)->state
];
1711 static const char *service_sub_state_to_string(Unit
*u
) {
1714 return service_state_to_string(SERVICE(u
)->state
);
1717 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1718 Service
*s
= SERVICE(u
);
1724 success
= code
== CLD_EXITED
&& status
== 0;
1725 s
->failure
= s
->failure
|| !success
;
1727 if (s
->main_pid
== pid
) {
1729 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1732 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1733 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1734 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1737 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1739 /* The service exited, so the service is officially
1744 case SERVICE_START_POST
:
1745 case SERVICE_RELOAD
:
1747 /* Need to wait until the operation is
1752 assert(s
->type
== SERVICE_FINISH
);
1754 /* This was our main goal, so let's go on */
1756 service_enter_start_post(s
);
1758 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1761 case SERVICE_RUNNING
:
1762 service_enter_running(s
, success
);
1765 case SERVICE_STOP_SIGTERM
:
1766 case SERVICE_STOP_SIGKILL
:
1768 if (!control_pid_good(s
))
1769 service_enter_stop_post(s
, success
);
1771 /* If there is still a control process, wait for that first */
1775 assert_not_reached("Uh, main process died at wrong time.");
1778 } else if (s
->control_pid
== pid
) {
1779 assert(s
->control_command
);
1781 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1784 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1786 /* If we are shutting things down anyway we
1787 * don't care about failing commands. */
1789 if (s
->control_command
->command_next
&& success
) {
1791 /* There is another command to *
1792 * execute, so let's do that. */
1794 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1795 service_run_next(s
, success
);
1798 /* No further commands for this step, so let's
1799 * figure out what to do next */
1801 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1805 case SERVICE_START_PRE
:
1807 service_enter_start(s
);
1809 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1813 assert(s
->type
== SERVICE_FORKING
);
1815 /* Let's try to load the pid
1816 * file here if we can. We
1817 * ignore the return value,
1818 * since the PID file might
1819 * actually be created by a
1820 * START_POST script */
1824 service_load_pid_file(s
);
1826 service_enter_start_post(s
);
1828 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1832 case SERVICE_START_POST
:
1833 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1836 /* Hmm, let's see if we can
1837 * load the pid now after the
1838 * start-post scripts got
1841 if ((r
= service_load_pid_file(s
)) < 0)
1842 log_warning("%s: failed to load PID file %s: %s", UNIT(s
)->meta
.id
, s
->pid_file
, strerror(-r
));
1847 case SERVICE_RELOAD
:
1849 service_enter_running(s
, true);
1851 service_enter_stop(s
, false);
1856 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
1859 case SERVICE_STOP_SIGTERM
:
1860 case SERVICE_STOP_SIGKILL
:
1861 if (main_pid_good(s
) <= 0)
1862 service_enter_stop_post(s
, success
);
1864 /* If there is still a service
1865 * process around, wait until
1866 * that one quit, too */
1869 case SERVICE_STOP_POST
:
1870 case SERVICE_FINAL_SIGTERM
:
1871 case SERVICE_FINAL_SIGKILL
:
1872 service_enter_dead(s
, success
, true);
1876 assert_not_reached("Uh, control process died at wrong time.");
1880 assert_not_reached("Got SIGCHLD for unkown PID");
1883 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
1884 Service
*s
= SERVICE(u
);
1887 assert(elapsed
== 1);
1889 assert(w
== &s
->timer_watch
);
1893 case SERVICE_START_PRE
:
1895 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
1896 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1899 case SERVICE_START_POST
:
1900 case SERVICE_RELOAD
:
1901 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
1902 service_enter_stop(s
, false);
1906 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
1907 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1910 case SERVICE_STOP_SIGTERM
:
1911 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
1912 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
1915 case SERVICE_STOP_SIGKILL
:
1916 /* Uh, wie sent a SIGKILL and it is still not gone?
1917 * Must be something we cannot kill, so let's just be
1918 * weirded out and continue */
1920 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
1921 service_enter_stop_post(s
, false);
1924 case SERVICE_STOP_POST
:
1925 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
1926 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1929 case SERVICE_FINAL_SIGTERM
:
1930 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
1931 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
1934 case SERVICE_FINAL_SIGKILL
:
1935 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
1936 service_enter_dead(s
, false, true);
1939 case SERVICE_AUTO_RESTART
:
1940 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
1941 service_enter_restart(s
);
1945 assert_not_reached("Timeout at wrong time.");
1949 static void service_cgroup_notify_event(Unit
*u
) {
1950 Service
*s
= SERVICE(u
);
1954 log_debug("%s: cgroup is empty", u
->meta
.id
);
1958 /* Waiting for SIGCHLD is usually more interesting,
1959 * because it includes return codes/signals. Which is
1960 * why we ignore the cgroup events for most cases,
1961 * except when we don't know pid which to expect the
1964 case SERVICE_RUNNING
:
1965 service_enter_running(s
, true);
1973 static int service_enumerate(Manager
*m
) {
1977 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
1982 STRV_FOREACH(p
, m
->sysvrcnd_path
)
1983 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
1988 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
1996 if (!(d
= opendir(path
))) {
1997 if (errno
!= ENOENT
)
1998 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2003 while ((de
= readdir(d
))) {
2004 Unit
*runlevel
, *service
;
2006 if (ignore_file(de
->d_name
))
2009 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2012 if (strlen(de
->d_name
) < 4)
2017 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
2022 if (access(fpath
, X_OK
) < 0) {
2024 if (errno
!= ENOENT
)
2025 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2032 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
2037 if ((r
= manager_load_unit(m
, name
, NULL
, &service
)) < 0)
2040 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], NULL
, &runlevel
)) < 0)
2043 if (de
->d_name
[0] == 'S') {
2044 if ((r
= unit_add_dependency(runlevel
, UNIT_WANTS
, service
)) < 0)
2047 if ((r
= unit_add_dependency(runlevel
, UNIT_AFTER
, service
)) < 0)
2050 } else if (de
->d_name
[0] == 'K' &&
2051 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
2052 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
2054 /* We honour K links only for
2055 * halt/reboot. For the normal
2056 * runlevels we assume the
2058 * implicitly added by the
2061 if ((r
= unit_add_dependency(runlevel
, UNIT_CONFLICTS
, service
)) < 0)
2064 if ((r
= unit_add_dependency(runlevel
, UNIT_BEFORE
, service
)) < 0)
2081 static void service_bus_name_owner_change(
2084 const char *old_owner
,
2085 const char *new_owner
) {
2087 Service
*s
= SERVICE(u
);
2092 assert(streq(s
->bus_name
, name
));
2093 assert(old_owner
|| new_owner
);
2095 if (old_owner
&& new_owner
)
2096 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2098 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2100 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2102 s
->bus_name_good
= !!new_owner
;
2104 if (s
->type
== SERVICE_DBUS
) {
2106 /* service_enter_running() will figure out what to
2108 if (s
->state
== SERVICE_RUNNING
)
2109 service_enter_running(s
, true);
2110 else if (s
->state
== SERVICE_START
&& new_owner
)
2111 service_enter_start_post(s
);
2113 } else if (new_owner
&&
2115 (s
->state
== SERVICE_START
||
2116 s
->state
== SERVICE_START_POST
||
2117 s
->state
== SERVICE_RUNNING
||
2118 s
->state
== SERVICE_RELOAD
)) {
2120 /* Try to acquire PID from bus service */
2121 log_debug("Trying to acquire PID from D-Bus name...");
2123 bus_query_pid(u
->meta
.manager
, name
);
2127 static void service_bus_query_pid_done(
2132 Service
*s
= SERVICE(u
);
2137 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2139 if (s
->main_pid
<= 0 &&
2140 (s
->state
== SERVICE_START
||
2141 s
->state
== SERVICE_START_POST
||
2142 s
->state
== SERVICE_RUNNING
||
2143 s
->state
== SERVICE_RELOAD
))
2147 int service_set_socket_fd(Service
*s
, int fd
) {
2151 /* This is called by the socket code when instantiating a new
2152 * service for a stream socket and the socket needs to be
2155 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
2158 if (s
->socket_fd
>= 0)
2161 if (s
->state
!= SERVICE_DEAD
)
2168 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2169 [SERVICE_DEAD
] = "dead",
2170 [SERVICE_START_PRE
] = "start-pre",
2171 [SERVICE_START
] = "start",
2172 [SERVICE_START_POST
] = "start-post",
2173 [SERVICE_RUNNING
] = "running",
2174 [SERVICE_EXITED
] = "exited",
2175 [SERVICE_RELOAD
] = "reload",
2176 [SERVICE_STOP
] = "stop",
2177 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2178 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2179 [SERVICE_STOP_POST
] = "stop-post",
2180 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2181 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2182 [SERVICE_MAINTAINANCE
] = "maintainance",
2183 [SERVICE_AUTO_RESTART
] = "auto-restart",
2186 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2188 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2189 [SERVICE_ONCE
] = "once",
2190 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2191 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2194 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2196 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2197 [SERVICE_FORKING
] = "forking",
2198 [SERVICE_SIMPLE
] = "simple",
2199 [SERVICE_FINISH
] = "finish",
2200 [SERVICE_DBUS
] = "dbus"
2203 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2205 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2206 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2207 [SERVICE_EXEC_START
] = "ExecStart",
2208 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2209 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2210 [SERVICE_EXEC_STOP
] = "ExecStop",
2211 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2214 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2216 const UnitVTable service_vtable
= {
2217 .suffix
= ".service",
2219 .init
= service_init
,
2220 .load
= service_load
,
2221 .done
= service_done
,
2223 .dump
= service_dump
,
2225 .start
= service_start
,
2226 .stop
= service_stop
,
2227 .reload
= service_reload
,
2229 .can_reload
= service_can_reload
,
2231 .active_state
= service_active_state
,
2232 .sub_state_to_string
= service_sub_state_to_string
,
2234 .sigchld_event
= service_sigchld_event
,
2235 .timer_event
= service_timer_event
,
2237 .cgroup_notify_empty
= service_cgroup_notify_event
,
2239 .bus_name_owner_change
= service_bus_name_owner_change
,
2240 .bus_query_pid_done
= service_bus_query_pid_done
,
2242 .enumerate
= service_enumerate