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_unwatch_control_pid(Service
*s
) {
72 if (s
->control_pid
<= 0)
75 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
79 static void service_unwatch_main_pid(Service
*s
) {
85 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
89 static void service_close_socket_fd(Service
*s
) {
95 close_nointr_nofail(s
->socket_fd
);
99 static void service_done(Unit
*u
) {
100 Service
*s
= SERVICE(u
);
110 free(s
->sysv_runlevels
);
111 s
->sysv_runlevels
= NULL
;
113 exec_context_done(&s
->exec_context
);
114 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
115 s
->control_command
= NULL
;
117 /* This will leak a process, but at least no memory or any of
119 service_unwatch_main_pid(s
);
120 service_unwatch_control_pid(s
);
123 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
128 service_close_socket_fd(s
);
130 unit_unwatch_timer(u
, &s
->timer_watch
);
133 static int sysv_translate_name(const char *name
, char **_r
) {
135 static const char * const table
[] = {
136 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
137 "$network", SPECIAL_NETWORK_TARGET
,
138 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
139 "$portmap", SPECIAL_RPCBIND_TARGET
,
140 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
141 "$syslog", SPECIAL_SYSLOG_TARGET
,
142 "$time", SPECIAL_RTC_SET_TARGET
148 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
149 if (streq(table
[i
], name
)) {
150 if (!(r
= strdup(table
[i
+1])))
159 if (asprintf(&r
, "%s.service", name
) < 0)
170 static int sysv_chkconfig_order(Service
*s
) {
176 if (s
->sysv_start_priority
< 0)
179 /* For each pair of services where at least one lacks a LSB
180 * header, we use the start priority value to order things. */
182 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
186 t
= (Service
*) other
;
191 if (t
->sysv_start_priority
< 0)
194 /* If both units have modern headers we don't care
195 * about the priorities */
196 if ((!s
->sysv_path
|| s
->sysv_has_lsb
) &&
197 (!t
->sysv_path
|| t
->sysv_has_lsb
))
200 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
202 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
207 /* FIXME: Maybe we should compare the name here lexicographically? */
209 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
))) < 0)
216 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
219 if (!(c
= new0(ExecCommand
, 1)))
222 if (!(c
->path
= strdup(path
))) {
227 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
236 static int sysv_exec_commands(Service
*s
) {
240 assert(s
->sysv_path
);
242 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
244 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
246 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
248 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
250 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
252 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
257 static int priority_from_rcd(Service
*s
, const char *init_script
) {
261 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvrcnd_path
)
262 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
267 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0)
275 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
280 while ((de
= readdir(d
))) {
283 if (ignore_file(de
->d_name
))
286 if (de
->d_name
[0] != 'S')
289 if (strlen(de
->d_name
) < 4)
292 if (!streq(de
->d_name
+ 3, init_script
))
295 /* Yay, we found it! Now decode the priority */
297 a
= undecchar(de
->d_name
[1]);
298 b
= undecchar(de
->d_name
[2]);
303 s
->sysv_start_priority
= a
*10 + b
;
305 log_debug("Determined priority %i from link farm for %s", s
->sysv_start_priority
, UNIT(s
)->meta
.id
);
317 static int service_load_sysv_path(Service
*s
, const char *path
) {
334 if (!(f
= fopen(path
, "re"))) {
335 r
= errno
== ENOENT
? 0 : -errno
;
339 s
->type
= SERVICE_FORKING
;
340 s
->restart
= SERVICE_ONCE
;
343 if (!(s
->sysv_path
= strdup(path
))) {
349 char l
[LINE_MAX
], *t
;
351 if (!fgets(l
, sizeof(l
), f
)) {
356 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
366 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
368 s
->sysv_has_lsb
= true;
372 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
378 t
+= strspn(t
, WHITESPACE
);
380 if (state
== NORMAL
) {
382 /* Try to parse Red Hat style chkconfig headers */
384 if (startswith(t
, "chkconfig:")) {
386 char runlevels
[16], *k
;
390 if (sscanf(t
+10, "%15s %i %*i",
392 &start_priority
) != 2) {
394 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
398 if (start_priority
< 0 || start_priority
> 99)
399 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
401 s
->sysv_start_priority
= start_priority
;
403 char_array_0(runlevels
);
404 k
= delete_chars(runlevels
, WHITESPACE
"-");
409 if (!(d
= strdup(k
))) {
414 free(s
->sysv_runlevels
);
415 s
->sysv_runlevels
= d
;
419 } else if (startswith(t
, "description:")) {
421 size_t k
= strlen(t
);
424 if (t
[k
-1] == '\\') {
429 if (!(d
= strdup(strstrip(t
+12)))) {
434 free(u
->meta
.description
);
435 u
->meta
.description
= d
;
437 } else if (startswith(t
, "pidfile:")) {
444 if (!path_is_absolute(fn
)) {
445 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
449 if (!(fn
= strdup(fn
))) {
458 } else if (state
== DESCRIPTION
) {
460 /* Try to parse Red Hat style description
463 size_t k
= strlen(t
);
471 assert(u
->meta
.description
);
472 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
477 free(u
->meta
.description
);
478 u
->meta
.description
= d
;
480 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
482 if (startswith(t
, "Provides:")) {
488 FOREACH_WORD(w
, z
, t
+9, i
) {
491 if (!(n
= strndup(w
, z
))) {
496 r
= sysv_translate_name(n
, &m
);
505 if (unit_name_to_type(m
) == UNIT_SERVICE
)
506 r
= unit_add_name(u
, m
);
508 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
, NULL
)) >= 0)
509 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
);
518 } else if (startswith(t
, "Required-Start:") ||
519 startswith(t
, "Should-Start:")) {
525 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
528 if (!(n
= strndup(w
, z
))) {
533 r
= sysv_translate_name(n
, &m
);
542 r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
, NULL
);
548 } else if (startswith(t
, "Default-Start:")) {
553 k
= delete_chars(t
+14, WHITESPACE
"-");
556 if (!(d
= strdup(k
))) {
561 free(s
->sysv_runlevels
);
562 s
->sysv_runlevels
= d
;
565 } else if (startswith(t
, "Description:")) {
568 state
= LSB_DESCRIPTION
;
570 if (!(d
= strdup(strstrip(t
+12)))) {
575 free(u
->meta
.description
);
576 u
->meta
.description
= d
;
578 } else if (startswith(t
, "Short-Description:") &&
579 !u
->meta
.description
) {
582 /* We use the short description only
583 * if no long description is set. */
587 if (!(d
= strdup(strstrip(t
+18)))) {
592 u
->meta
.description
= d
;
594 } else if (state
== LSB_DESCRIPTION
) {
596 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
599 assert(u
->meta
.description
);
600 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
605 free(u
->meta
.description
);
606 u
->meta
.description
= d
;
613 /* If init scripts have no LSB header, then we enforce the
614 * ordering via the chkconfig priorities. We try to determine
615 * a priority for *all* init scripts here, since they are
616 * needed as soon as at least one non-LSB script is used. */
618 if (s
->sysv_start_priority
< 0) {
619 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", u
->meta
.id
);
621 if ((r
= priority_from_rcd(s
, file_name_from_path(path
))) < 0)
624 if (s
->sysv_start_priority
< 0)
625 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u
->meta
.id
);
628 if ((r
= sysv_exec_commands(s
)) < 0)
631 if (!s
->sysv_runlevels
|| chars_intersect("12345", s
->sysv_runlevels
)) {
632 /* If there a runlevels configured for this service
633 * but none of the standard ones, then we assume this
634 * is some special kind of service (which might be
635 * needed for early boot) and don't create any links
638 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
)) < 0 ||
639 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
)) < 0)
643 /* Special setting for all SysV services */
644 s
->valid_no_process
= true;
646 /* Don't timeout special services during boot (like fsck) */
647 if (s
->sysv_runlevels
&& !chars_intersect("12345", s
->sysv_runlevels
))
648 s
->timeout_usec
= -1;
650 u
->meta
.load_state
= UNIT_LOADED
;
661 static int service_load_sysv_name(Service
*s
, const char *name
) {
667 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
671 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
674 assert(endswith(path
, ".service"));
675 path
[strlen(path
)-8] = 0;
677 r
= service_load_sysv_path(s
, path
);
683 if ((UNIT(s
)->meta
.load_state
!= UNIT_STUB
))
690 static int service_load_sysv(Service
*s
) {
697 /* Load service data from SysV init scripts, preferably with
700 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
703 if ((t
= UNIT(s
)->meta
.id
))
704 if ((r
= service_load_sysv_name(s
, t
)) < 0)
707 if (UNIT(s
)->meta
.load_state
== UNIT_STUB
)
708 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
709 if (t
== UNIT(s
)->meta
.id
)
712 if ((r
== service_load_sysv_name(s
, t
)) < 0)
715 if (UNIT(s
)->meta
.load_state
!= UNIT_STUB
)
722 static int service_add_bus_name(Service
*s
) {
729 if (asprintf(&n
, "dbus-%s.service", s
->bus_name
) < 0)
732 r
= unit_merge_by_name(UNIT(s
), n
);
738 static void service_init(Unit
*u
) {
739 Service
*s
= SERVICE(u
);
742 assert(u
->meta
.load_state
== UNIT_STUB
);
744 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
745 s
->restart_usec
= DEFAULT_RESTART_USEC
;
746 s
->timer_watch
.type
= WATCH_INVALID
;
747 s
->sysv_start_priority
= -1;
750 exec_context_init(&s
->exec_context
);
752 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
755 static int service_verify(Service
*s
) {
758 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
761 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
762 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->meta
.id
);
766 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
767 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->meta
.id
);
774 static int service_load(Unit
*u
) {
776 Service
*s
= SERVICE(u
);
780 /* Load a .service file */
781 if ((r
= unit_load_fragment(u
)) < 0)
784 /* Load a classic init script as a fallback, if we couldn't find anything */
785 if (u
->meta
.load_state
== UNIT_STUB
)
786 if ((r
= service_load_sysv(s
)) < 0)
789 /* Still nothing found? Then let's give up */
790 if (u
->meta
.load_state
== UNIT_STUB
)
793 /* We were able to load something, then let's add in the
794 * dropin directories. */
795 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
798 /* This is a new unit? Then let's add in some extras */
799 if (u
->meta
.load_state
== UNIT_LOADED
) {
800 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
803 if ((r
= unit_add_default_cgroup(u
)) < 0)
806 if ((r
= sysv_chkconfig_order(s
)) < 0)
810 if ((r
= service_add_bus_name(s
)) < 0)
813 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
818 return service_verify(s
);
821 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
823 ServiceExecCommand c
;
824 Service
*s
= SERVICE(u
);
830 p2
= strappend(prefix
, "\t");
831 prefix2
= p2
? p2
: prefix
;
834 "%sService State: %s\n"
835 "%sPermissionsStartOnly: %s\n"
836 "%sRootDirectoryStartOnly: %s\n"
837 "%sValidNoProcess: %s\n"
840 prefix
, service_state_to_string(s
->state
),
841 prefix
, yes_no(s
->permissions_start_only
),
842 prefix
, yes_no(s
->root_directory_start_only
),
843 prefix
, yes_no(s
->valid_no_process
),
844 prefix
, kill_mode_to_string(s
->kill_mode
),
845 prefix
, service_type_to_string(s
->type
));
847 if (s
->control_pid
> 0)
849 "%sControl PID: %llu\n",
850 prefix
, (unsigned long long) s
->control_pid
);
854 "%sMain PID: %llu\n",
855 prefix
, (unsigned long long) s
->main_pid
);
860 prefix
, s
->pid_file
);
865 "%sBus Name Good: %s\n",
867 prefix
, yes_no(s
->bus_name_good
));
869 exec_context_dump(&s
->exec_context
, f
, prefix
);
871 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
873 if (!s
->exec_command
[c
])
876 fprintf(f
, "%s→ %s:\n",
877 prefix
, service_exec_command_to_string(c
));
879 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
884 "%sSysV Init Script Path: %s\n"
885 "%sSysV Init Script has LSB Header: %s\n",
886 prefix
, s
->sysv_path
,
887 prefix
, yes_no(s
->sysv_has_lsb
));
889 if (s
->sysv_start_priority
>= 0)
891 "%sSysVStartPriority: %i\n",
892 prefix
, s
->sysv_start_priority
);
894 if (s
->sysv_runlevels
)
895 fprintf(f
, "%sSysVRunLevels: %s\n",
896 prefix
, s
->sysv_runlevels
);
901 static int service_load_pid_file(Service
*s
) {
908 if (s
->main_pid_known
)
911 assert(s
->main_pid
<= 0);
916 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
919 if ((r
= safe_atolu(k
, &p
)) < 0) {
924 if ((unsigned long) (pid_t
) p
!= p
)
927 if (kill((pid_t
) p
, 0) < 0 && errno
!= EPERM
) {
928 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
929 (unsigned long long) p
, s
->pid_file
);
933 if ((r
= unit_watch_pid(UNIT(s
), (pid_t
) p
)) < 0)
934 /* FIXME: we need to do something here */
937 s
->main_pid
= (pid_t
) p
;
938 s
->main_pid_known
= true;
943 static int service_get_sockets(Service
*s
, Set
**_set
) {
952 /* Collects all Socket objects that belong to this
953 * service. Note that a service might have multiple sockets
954 * via multiple names. */
956 if (!(set
= set_new(NULL
, NULL
)))
959 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
963 /* Look for all socket objects that go by any of our
964 * units and collect their fds */
966 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
971 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
977 if ((r
= set_put(set
, p
)) < 0)
989 static int service_notify_sockets_dead(Service
*s
) {
997 /* Notifies all our sockets when we die */
998 if ((r
= service_get_sockets(s
, &set
)) < 0)
1001 SET_FOREACH(sock
, set
, i
)
1002 socket_notify_service_dead(sock
);
1009 static void service_set_state(Service
*s
, ServiceState state
) {
1010 ServiceState old_state
;
1013 old_state
= s
->state
;
1016 if (state
!= SERVICE_START_PRE
&&
1017 state
!= SERVICE_START
&&
1018 state
!= SERVICE_START_POST
&&
1019 state
!= SERVICE_RELOAD
&&
1020 state
!= SERVICE_STOP
&&
1021 state
!= SERVICE_STOP_SIGTERM
&&
1022 state
!= SERVICE_STOP_SIGKILL
&&
1023 state
!= SERVICE_STOP_POST
&&
1024 state
!= SERVICE_FINAL_SIGTERM
&&
1025 state
!= SERVICE_FINAL_SIGKILL
&&
1026 state
!= SERVICE_AUTO_RESTART
)
1027 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1029 if (state
!= SERVICE_START
&&
1030 state
!= SERVICE_START_POST
&&
1031 state
!= SERVICE_RUNNING
&&
1032 state
!= SERVICE_RELOAD
&&
1033 state
!= SERVICE_STOP
&&
1034 state
!= SERVICE_STOP_SIGTERM
&&
1035 state
!= SERVICE_STOP_SIGKILL
)
1036 service_unwatch_main_pid(s
);
1038 if (state
!= SERVICE_START_PRE
&&
1039 state
!= SERVICE_START
&&
1040 state
!= SERVICE_START_POST
&&
1041 state
!= SERVICE_RELOAD
&&
1042 state
!= SERVICE_STOP
&&
1043 state
!= SERVICE_STOP_SIGTERM
&&
1044 state
!= SERVICE_STOP_SIGKILL
&&
1045 state
!= SERVICE_STOP_POST
&&
1046 state
!= SERVICE_FINAL_SIGTERM
&&
1047 state
!= SERVICE_FINAL_SIGKILL
) {
1048 service_unwatch_control_pid(s
);
1049 s
->control_command
= NULL
;
1052 if (state
== SERVICE_DEAD
||
1053 state
== SERVICE_STOP
||
1054 state
== SERVICE_STOP_SIGTERM
||
1055 state
== SERVICE_STOP_SIGKILL
||
1056 state
== SERVICE_STOP_POST
||
1057 state
== SERVICE_FINAL_SIGTERM
||
1058 state
== SERVICE_FINAL_SIGKILL
||
1059 state
== SERVICE_MAINTAINANCE
||
1060 state
== SERVICE_AUTO_RESTART
)
1061 service_notify_sockets_dead(s
);
1063 if (state
!= SERVICE_START_PRE
&&
1064 state
!= SERVICE_START
&&
1065 !(state
== SERVICE_DEAD
&& UNIT(s
)->meta
.job
))
1066 service_close_socket_fd(s
);
1068 if (old_state
!= state
)
1069 log_debug("%s changed %s → %s", UNIT(s
)->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1071 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1074 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1078 unsigned rn_fds
= 0;
1086 if ((r
= service_get_sockets(s
, &set
)) < 0)
1089 SET_FOREACH(sock
, set
, i
) {
1093 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1105 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1111 memcpy(t
, rfds
, rn_fds
);
1112 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1117 rn_fds
= rn_fds
+cn_fds
;
1135 static int service_spawn(
1140 bool apply_permissions
,
1155 if (s
->socket_fd
>= 0) {
1156 fds
= &s
->socket_fd
;
1158 } else if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1163 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1166 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1168 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1179 UNIT(s
)->meta
.manager
->confirm_spawn
,
1180 UNIT(s
)->meta
.cgroup_bondings
,
1188 if (s
->socket_fd
>= 0)
1189 service_close_socket_fd(s
);
1194 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1195 /* FIXME: we need to do something here */
1206 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1211 static int main_pid_good(Service
*s
) {
1214 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1217 /* If we know the pid file, then lets just check if it is
1219 if (s
->main_pid_known
)
1220 return s
->main_pid
> 0;
1222 /* We don't know the pid */
1226 static int control_pid_good(Service
*s
) {
1229 return s
->control_pid
> 0;
1232 static int cgroup_good(Service
*s
) {
1237 if (s
->valid_no_process
)
1240 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
)) < 0)
1246 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1253 if (allow_restart
&&
1254 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1255 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1257 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1260 service_set_state(s
, SERVICE_AUTO_RESTART
);
1262 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1267 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1268 service_enter_dead(s
, false, false);
1271 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1273 static void service_enter_stop_post(Service
*s
, bool success
) {
1280 service_unwatch_control_pid(s
);
1282 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1283 if ((r
= service_spawn(s
,
1287 !s
->permissions_start_only
,
1288 !s
->root_directory_start_only
,
1289 &s
->control_pid
)) < 0)
1293 service_set_state(s
, SERVICE_STOP_POST
);
1295 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1300 log_warning("%s failed to run stop-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1301 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1304 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1313 if (s
->kill_mode
!= KILL_NONE
) {
1314 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1316 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1318 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1319 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1328 if (s
->main_pid
> 0) {
1329 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1335 if (s
->control_pid
> 0) {
1336 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1348 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1351 service_set_state(s
, state
);
1352 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1353 service_enter_stop_post(s
, true);
1355 service_enter_dead(s
, true, true);
1360 log_warning("%s failed to kill processes: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1362 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1363 service_enter_stop_post(s
, false);
1365 service_enter_dead(s
, false, true);
1368 static void service_enter_stop(Service
*s
, bool success
) {
1375 service_unwatch_control_pid(s
);
1377 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1378 if ((r
= service_spawn(s
,
1382 !s
->permissions_start_only
,
1383 !s
->root_directory_start_only
,
1384 &s
->control_pid
)) < 0)
1387 service_set_state(s
, SERVICE_STOP
);
1389 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1394 log_warning("%s failed to run stop executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1395 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1398 static void service_enter_running(Service
*s
, bool success
) {
1404 if (main_pid_good(s
) != 0 &&
1405 cgroup_good(s
) != 0 &&
1406 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1407 service_set_state(s
, SERVICE_RUNNING
);
1408 else if (s
->valid_no_process
)
1409 service_set_state(s
, SERVICE_EXITED
);
1411 service_enter_stop(s
, true);
1414 static void service_enter_start_post(Service
*s
) {
1418 service_unwatch_control_pid(s
);
1420 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1421 if ((r
= service_spawn(s
,
1425 !s
->permissions_start_only
,
1426 !s
->root_directory_start_only
,
1427 &s
->control_pid
)) < 0)
1431 service_set_state(s
, SERVICE_START_POST
);
1433 service_enter_running(s
, true);
1438 log_warning("%s failed to run start-post executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1439 service_enter_stop(s
, false);
1442 static void service_enter_start(Service
*s
) {
1448 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1449 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1451 if (s
->type
== SERVICE_FORKING
)
1452 service_unwatch_control_pid(s
);
1454 service_unwatch_main_pid(s
);
1456 if ((r
= service_spawn(s
,
1457 s
->exec_command
[SERVICE_EXEC_START
],
1458 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
,
1465 if (s
->type
== SERVICE_SIMPLE
) {
1466 /* For simple services we immediately start
1467 * the START_POST binaries. */
1470 s
->main_pid_known
= true;
1472 service_enter_start_post(s
);
1474 } else if (s
->type
== SERVICE_FORKING
) {
1476 /* For forking services we wait until the start
1477 * process exited. */
1479 s
->control_pid
= pid
;
1481 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1482 service_set_state(s
, SERVICE_START
);
1484 } else if (s
->type
== SERVICE_FINISH
||
1485 s
->type
== SERVICE_DBUS
) {
1487 /* For finishing services we wait until the start
1488 * process exited, too, but it is our main process. */
1490 /* For D-Bus services we know the main pid right away,
1491 * but wait for the bus name to appear on the bus. */
1494 s
->main_pid_known
= true;
1496 service_set_state(s
, SERVICE_START
);
1498 assert_not_reached("Unknown service type");
1503 log_warning("%s failed to run start exectuable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1504 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1507 static void service_enter_start_pre(Service
*s
) {
1512 service_unwatch_control_pid(s
);
1514 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1515 if ((r
= service_spawn(s
,
1519 !s
->permissions_start_only
,
1520 !s
->root_directory_start_only
,
1521 &s
->control_pid
)) < 0)
1524 service_set_state(s
, SERVICE_START_PRE
);
1526 service_enter_start(s
);
1531 log_warning("%s failed to run start-pre executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1532 service_enter_dead(s
, false, true);
1535 static void service_enter_restart(Service
*s
) {
1539 service_enter_dead(s
, true, false);
1541 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1544 log_debug("%s scheduled restart job.", UNIT(s
)->meta
.id
);
1549 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1550 service_enter_dead(s
, false, false);
1553 static void service_enter_reload(Service
*s
) {
1558 service_unwatch_control_pid(s
);
1560 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1561 if ((r
= service_spawn(s
,
1565 !s
->permissions_start_only
,
1566 !s
->root_directory_start_only
,
1567 &s
->control_pid
)) < 0)
1570 service_set_state(s
, SERVICE_RELOAD
);
1572 service_enter_running(s
, true);
1577 log_warning("%s failed to run reload executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1578 service_enter_stop(s
, false);
1581 static void service_run_next(Service
*s
, bool success
) {
1585 assert(s
->control_command
);
1586 assert(s
->control_command
->command_next
);
1591 s
->control_command
= s
->control_command
->command_next
;
1593 service_unwatch_control_pid(s
);
1595 if ((r
= service_spawn(s
,
1599 !s
->permissions_start_only
,
1600 !s
->root_directory_start_only
,
1601 &s
->control_pid
)) < 0)
1607 log_warning("%s failed to run spawn next executable: %s", UNIT(s
)->meta
.id
, strerror(-r
));
1609 if (s
->state
== SERVICE_START_PRE
)
1610 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1611 else if (s
->state
== SERVICE_STOP
)
1612 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1613 else if (s
->state
== SERVICE_STOP_POST
)
1614 service_enter_dead(s
, false, true);
1616 service_enter_stop(s
, false);
1619 static int service_start(Unit
*u
) {
1620 Service
*s
= SERVICE(u
);
1624 /* We cannot fulfill this request right now, try again later
1626 if (s
->state
== SERVICE_STOP
||
1627 s
->state
== SERVICE_STOP_SIGTERM
||
1628 s
->state
== SERVICE_STOP_SIGKILL
||
1629 s
->state
== SERVICE_STOP_POST
||
1630 s
->state
== SERVICE_FINAL_SIGTERM
||
1631 s
->state
== SERVICE_FINAL_SIGKILL
)
1634 /* Already on it! */
1635 if (s
->state
== SERVICE_START_PRE
||
1636 s
->state
== SERVICE_START
||
1637 s
->state
== SERVICE_START_POST
)
1640 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1642 /* Make sure we don't enter a busy loop of some kind. */
1643 if (!ratelimit_test(&s
->ratelimit
)) {
1644 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1649 s
->main_pid_known
= false;
1651 service_enter_start_pre(s
);
1655 static int service_stop(Unit
*u
) {
1656 Service
*s
= SERVICE(u
);
1660 /* Cannot do this now */
1661 if (s
->state
== SERVICE_START_PRE
||
1662 s
->state
== SERVICE_START
||
1663 s
->state
== SERVICE_START_POST
||
1664 s
->state
== SERVICE_RELOAD
)
1668 if (s
->state
== SERVICE_STOP
||
1669 s
->state
== SERVICE_STOP_SIGTERM
||
1670 s
->state
== SERVICE_STOP_SIGKILL
||
1671 s
->state
== SERVICE_STOP_POST
||
1672 s
->state
== SERVICE_FINAL_SIGTERM
||
1673 s
->state
== SERVICE_FINAL_SIGKILL
)
1676 if (s
->state
== SERVICE_AUTO_RESTART
) {
1677 service_set_state(s
, SERVICE_DEAD
);
1681 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1683 service_enter_stop(s
, true);
1687 static int service_reload(Unit
*u
) {
1688 Service
*s
= SERVICE(u
);
1692 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1694 service_enter_reload(s
);
1698 static bool service_can_reload(Unit
*u
) {
1699 Service
*s
= SERVICE(u
);
1703 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1706 static UnitActiveState
service_active_state(Unit
*u
) {
1709 return state_translation_table
[SERVICE(u
)->state
];
1712 static const char *service_sub_state_to_string(Unit
*u
) {
1715 return service_state_to_string(SERVICE(u
)->state
);
1718 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1719 Service
*s
= SERVICE(u
);
1725 success
= code
== CLD_EXITED
&& status
== 0;
1726 s
->failure
= s
->failure
|| !success
;
1728 if (s
->main_pid
== pid
) {
1730 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1733 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1734 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1735 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1738 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1740 /* The service exited, so the service is officially
1745 case SERVICE_START_POST
:
1746 case SERVICE_RELOAD
:
1748 /* Need to wait until the operation is
1753 assert(s
->type
== SERVICE_FINISH
);
1755 /* This was our main goal, so let's go on */
1757 service_enter_start_post(s
);
1759 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1762 case SERVICE_RUNNING
:
1763 service_enter_running(s
, success
);
1766 case SERVICE_STOP_SIGTERM
:
1767 case SERVICE_STOP_SIGKILL
:
1769 if (!control_pid_good(s
))
1770 service_enter_stop_post(s
, success
);
1772 /* If there is still a control process, wait for that first */
1776 assert_not_reached("Uh, main process died at wrong time.");
1779 } else if (s
->control_pid
== pid
) {
1780 assert(s
->control_command
);
1782 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1785 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1787 /* If we are shutting things down anyway we
1788 * don't care about failing commands. */
1790 if (s
->control_command
->command_next
&& success
) {
1792 /* There is another command to *
1793 * execute, so let's do that. */
1795 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1796 service_run_next(s
, success
);
1799 /* No further commands for this step, so let's
1800 * figure out what to do next */
1802 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
1806 case SERVICE_START_PRE
:
1808 service_enter_start(s
);
1810 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1814 assert(s
->type
== SERVICE_FORKING
);
1816 /* Let's try to load the pid
1817 * file here if we can. We
1818 * ignore the return value,
1819 * since the PID file might
1820 * actually be created by a
1821 * START_POST script */
1825 service_load_pid_file(s
);
1827 service_enter_start_post(s
);
1829 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1833 case SERVICE_START_POST
:
1834 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1837 /* Hmm, let's see if we can
1838 * load the pid now after the
1839 * start-post scripts got
1842 if ((r
= service_load_pid_file(s
)) < 0)
1843 log_warning("%s: failed to load PID file %s: %s", UNIT(s
)->meta
.id
, s
->pid_file
, strerror(-r
));
1848 case SERVICE_RELOAD
:
1850 service_enter_running(s
, true);
1852 service_enter_stop(s
, false);
1857 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
1860 case SERVICE_STOP_SIGTERM
:
1861 case SERVICE_STOP_SIGKILL
:
1862 if (main_pid_good(s
) <= 0)
1863 service_enter_stop_post(s
, success
);
1865 /* If there is still a service
1866 * process around, wait until
1867 * that one quit, too */
1870 case SERVICE_STOP_POST
:
1871 case SERVICE_FINAL_SIGTERM
:
1872 case SERVICE_FINAL_SIGKILL
:
1873 service_enter_dead(s
, success
, true);
1877 assert_not_reached("Uh, control process died at wrong time.");
1881 assert_not_reached("Got SIGCHLD for unkown PID");
1884 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
1885 Service
*s
= SERVICE(u
);
1888 assert(elapsed
== 1);
1890 assert(w
== &s
->timer_watch
);
1894 case SERVICE_START_PRE
:
1896 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
1897 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1900 case SERVICE_START_POST
:
1901 case SERVICE_RELOAD
:
1902 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
1903 service_enter_stop(s
, false);
1907 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
1908 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1911 case SERVICE_STOP_SIGTERM
:
1912 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
1913 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
1916 case SERVICE_STOP_SIGKILL
:
1917 /* Uh, wie sent a SIGKILL and it is still not gone?
1918 * Must be something we cannot kill, so let's just be
1919 * weirded out and continue */
1921 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
1922 service_enter_stop_post(s
, false);
1925 case SERVICE_STOP_POST
:
1926 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
1927 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1930 case SERVICE_FINAL_SIGTERM
:
1931 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
1932 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
1935 case SERVICE_FINAL_SIGKILL
:
1936 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
1937 service_enter_dead(s
, false, true);
1940 case SERVICE_AUTO_RESTART
:
1941 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
1942 service_enter_restart(s
);
1946 assert_not_reached("Timeout at wrong time.");
1950 static void service_cgroup_notify_event(Unit
*u
) {
1951 Service
*s
= SERVICE(u
);
1955 log_debug("%s: cgroup is empty", u
->meta
.id
);
1959 /* Waiting for SIGCHLD is usually more interesting,
1960 * because it includes return codes/signals. Which is
1961 * why we ignore the cgroup events for most cases,
1962 * except when we don't know pid which to expect the
1965 case SERVICE_RUNNING
:
1966 service_enter_running(s
, true);
1974 static int service_enumerate(Manager
*m
) {
1978 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
1983 STRV_FOREACH(p
, m
->sysvrcnd_path
)
1984 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
1989 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
1997 if (!(d
= opendir(path
))) {
1998 if (errno
!= ENOENT
)
1999 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2004 while ((de
= readdir(d
))) {
2005 Unit
*runlevel
, *service
;
2007 if (ignore_file(de
->d_name
))
2010 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2013 if (strlen(de
->d_name
) < 4)
2018 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
2023 if (access(fpath
, X_OK
) < 0) {
2025 if (errno
!= ENOENT
)
2026 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2033 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
2038 if ((r
= manager_load_unit(m
, name
, NULL
, &service
)) < 0)
2041 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], NULL
, &runlevel
)) < 0)
2044 if (de
->d_name
[0] == 'S') {
2045 if ((r
= unit_add_dependency(runlevel
, UNIT_WANTS
, service
)) < 0)
2048 if ((r
= unit_add_dependency(runlevel
, UNIT_AFTER
, service
)) < 0)
2051 } else if (de
->d_name
[0] == 'K' &&
2052 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
2053 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
2055 /* We honour K links only for
2056 * halt/reboot. For the normal
2057 * runlevels we assume the
2059 * implicitly added by the
2062 if ((r
= unit_add_dependency(runlevel
, UNIT_CONFLICTS
, service
)) < 0)
2065 if ((r
= unit_add_dependency(runlevel
, UNIT_BEFORE
, service
)) < 0)
2082 static void service_bus_name_owner_change(
2085 const char *old_owner
,
2086 const char *new_owner
) {
2088 Service
*s
= SERVICE(u
);
2093 assert(streq(s
->bus_name
, name
));
2094 assert(old_owner
|| new_owner
);
2096 if (old_owner
&& new_owner
)
2097 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2099 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2101 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2103 s
->bus_name_good
= !!new_owner
;
2105 if (s
->type
== SERVICE_DBUS
) {
2107 /* service_enter_running() will figure out what to
2109 if (s
->state
== SERVICE_RUNNING
)
2110 service_enter_running(s
, true);
2111 else if (s
->state
== SERVICE_START
&& new_owner
)
2112 service_enter_start_post(s
);
2114 } else if (new_owner
&&
2116 (s
->state
== SERVICE_START
||
2117 s
->state
== SERVICE_START_POST
||
2118 s
->state
== SERVICE_RUNNING
||
2119 s
->state
== SERVICE_RELOAD
)) {
2121 /* Try to acquire PID from bus service */
2122 log_debug("Trying to acquire PID from D-Bus name...");
2124 bus_query_pid(u
->meta
.manager
, name
);
2128 static void service_bus_query_pid_done(
2133 Service
*s
= SERVICE(u
);
2138 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2140 if (s
->main_pid
<= 0 &&
2141 (s
->state
== SERVICE_START
||
2142 s
->state
== SERVICE_START_POST
||
2143 s
->state
== SERVICE_RUNNING
||
2144 s
->state
== SERVICE_RELOAD
))
2148 int service_set_socket_fd(Service
*s
, int fd
) {
2152 /* This is called by the socket code when instantiating a new
2153 * service for a stream socket and the socket needs to be
2156 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
2159 if (s
->socket_fd
>= 0)
2162 if (s
->state
!= SERVICE_DEAD
)
2169 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2170 [SERVICE_DEAD
] = "dead",
2171 [SERVICE_START_PRE
] = "start-pre",
2172 [SERVICE_START
] = "start",
2173 [SERVICE_START_POST
] = "start-post",
2174 [SERVICE_RUNNING
] = "running",
2175 [SERVICE_EXITED
] = "exited",
2176 [SERVICE_RELOAD
] = "reload",
2177 [SERVICE_STOP
] = "stop",
2178 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2179 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2180 [SERVICE_STOP_POST
] = "stop-post",
2181 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2182 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2183 [SERVICE_MAINTAINANCE
] = "maintainance",
2184 [SERVICE_AUTO_RESTART
] = "auto-restart",
2187 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2189 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2190 [SERVICE_ONCE
] = "once",
2191 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2192 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2195 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2197 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2198 [SERVICE_FORKING
] = "forking",
2199 [SERVICE_SIMPLE
] = "simple",
2200 [SERVICE_FINISH
] = "finish",
2201 [SERVICE_DBUS
] = "dbus"
2204 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2206 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2207 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2208 [SERVICE_EXEC_START
] = "ExecStart",
2209 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2210 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2211 [SERVICE_EXEC_STOP
] = "ExecStop",
2212 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2215 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2217 const UnitVTable service_vtable
= {
2218 .suffix
= ".service",
2220 .init
= service_init
,
2221 .load
= service_load
,
2222 .done
= service_done
,
2224 .dump
= service_dump
,
2226 .start
= service_start
,
2227 .stop
= service_stop
,
2228 .reload
= service_reload
,
2230 .can_reload
= service_can_reload
,
2232 .active_state
= service_active_state
,
2233 .sub_state_to_string
= service_sub_state_to_string
,
2235 .sigchld_event
= service_sigchld_event
,
2236 .timer_event
= service_timer_event
,
2238 .cgroup_notify_empty
= service_cgroup_notify_event
,
2240 .bus_name_owner_change
= service_bus_name_owner_change
,
2241 .bus_query_pid_done
= service_bus_query_pid_done
,
2243 .bus_message_handler
= bus_service_message_handler
,
2245 .enumerate
= service_enumerate