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"
37 #define COMMENTS "#;\n"
38 #define NEWLINES "\n\r"
41 typedef enum RunlevelType
{
50 const RunlevelType type
;
52 /* Standard SysV runlevels */
53 { "rc0.d", SPECIAL_POWEROFF_TARGET
, RUNLEVEL_DOWN
},
54 { "rc1.d", SPECIAL_RESCUE_TARGET
, RUNLEVEL_UP
},
55 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET
, RUNLEVEL_UP
},
56 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET
, RUNLEVEL_UP
},
57 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET
, RUNLEVEL_UP
},
58 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET
, RUNLEVEL_UP
},
59 { "rc6.d", SPECIAL_REBOOT_TARGET
, RUNLEVEL_DOWN
},
61 /* SUSE style boot.d */
62 { "boot.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
64 /* Debian style rcS.d */
65 { "rcS.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
68 #define RUNLEVELS_UP "12345"
69 /* #define RUNLEVELS_DOWN "06" */
70 /* #define RUNLEVELS_BOOT "bBsS" */
72 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
73 [SERVICE_DEAD
] = UNIT_INACTIVE
,
74 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
75 [SERVICE_START
] = UNIT_ACTIVATING
,
76 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
77 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
78 [SERVICE_EXITED
] = UNIT_ACTIVE
,
79 [SERVICE_RELOAD
] = UNIT_RELOADING
,
80 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
81 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
82 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
83 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
84 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
85 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
86 [SERVICE_MAINTENANCE
] = UNIT_MAINTENANCE
,
87 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
90 static void service_init(Unit
*u
) {
91 Service
*s
= SERVICE(u
);
94 assert(u
->meta
.load_state
== UNIT_STUB
);
96 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
97 s
->restart_usec
= DEFAULT_RESTART_USEC
;
98 s
->timer_watch
.type
= WATCH_INVALID
;
99 s
->sysv_start_priority
= -1;
102 exec_context_init(&s
->exec_context
);
104 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
106 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
109 static void service_unwatch_control_pid(Service
*s
) {
112 if (s
->control_pid
<= 0)
115 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
119 static void service_unwatch_main_pid(Service
*s
) {
122 if (s
->main_pid
<= 0)
125 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
129 static int service_set_main_pid(Service
*s
, pid_t pid
) {
140 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid())
141 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
142 s
->meta
.id
, (unsigned long) pid
);
145 s
->main_pid_known
= true;
150 static void service_close_socket_fd(Service
*s
) {
153 if (s
->socket_fd
< 0)
156 close_nointr_nofail(s
->socket_fd
);
160 static void service_connection_unref(Service
*s
) {
166 socket_connection_unref(s
->socket
);
170 static void service_done(Unit
*u
) {
171 Service
*s
= SERVICE(u
);
181 free(s
->sysv_runlevels
);
182 s
->sysv_runlevels
= NULL
;
184 free(s
->status_text
);
185 s
->status_text
= NULL
;
187 exec_context_done(&s
->exec_context
);
188 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
189 s
->control_command
= NULL
;
191 /* This will leak a process, but at least no memory or any of
193 service_unwatch_main_pid(s
);
194 service_unwatch_control_pid(s
);
197 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
202 service_close_socket_fd(s
);
203 service_connection_unref(s
);
205 unit_unwatch_timer(u
, &s
->timer_watch
);
208 static char *sysv_translate_name(const char *name
) {
211 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
214 if (startswith(name
, "boot."))
215 /* Drop SuSE-style boot. prefix */
216 strcpy(stpcpy(r
, name
+ 5), ".service");
217 else if (endswith(name
, ".sh"))
218 /* Drop Debian-style .sh suffix */
219 strcpy(stpcpy(r
, name
) - 3, ".service");
221 /* Normal init scripts */
222 strcpy(stpcpy(r
, name
), ".service");
227 static int sysv_translate_facility(const char *name
, char **_r
) {
229 static const char * const table
[] = {
230 /* LSB defined facilities */
231 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
232 "$network", SPECIAL_NETWORK_TARGET
,
233 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
234 "$portmap", SPECIAL_RPCBIND_TARGET
,
235 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
236 "$syslog", SPECIAL_SYSLOG_TARGET
,
237 "$time", SPECIAL_RTC_SET_TARGET
,
239 /* Debian extensions */
240 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
241 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
242 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
248 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
249 if (streq(table
[i
], name
)) {
250 if (!(r
= strdup(table
[i
+1])))
259 if (!(r
= sysv_translate_name(name
)))
270 static int sysv_fix_order(Service
*s
) {
276 if (s
->sysv_start_priority
< 0)
279 /* For each pair of services where at least one lacks a LSB
280 * header, we use the start priority value to order things. */
282 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
286 t
= (Service
*) other
;
291 if (t
->sysv_start_priority
< 0)
294 /* If both units have modern headers we don't care
295 * about the priorities */
296 if ((!s
->sysv_path
|| s
->sysv_has_lsb
) &&
297 (!t
->sysv_path
|| t
->sysv_has_lsb
))
300 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
302 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
307 /* FIXME: Maybe we should compare the name here lexicographically? */
309 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
316 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
319 if (!(c
= new0(ExecCommand
, 1)))
322 if (!(c
->path
= strdup(path
))) {
327 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
336 static int sysv_exec_commands(Service
*s
) {
340 assert(s
->sysv_path
);
342 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
344 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
346 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
348 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
350 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
352 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
357 static int service_load_sysv_path(Service
*s
, const char *path
) {
374 if (!(f
= fopen(path
, "re"))) {
375 r
= errno
== ENOENT
? 0 : -errno
;
380 if (!(s
->sysv_path
= strdup(path
))) {
386 char l
[LINE_MAX
], *t
;
388 if (!fgets(l
, sizeof(l
), f
)) {
393 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
403 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
405 s
->sysv_has_lsb
= true;
409 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
415 t
+= strspn(t
, WHITESPACE
);
417 if (state
== NORMAL
) {
419 /* Try to parse Red Hat style chkconfig headers */
421 if (startswith_no_case(t
, "chkconfig:")) {
423 char runlevels
[16], *k
;
427 if (sscanf(t
+10, "%15s %i %*i",
429 &start_priority
) != 2) {
431 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
435 /* A start priority gathered from the
436 * symlink farms is preferred over the
437 * data from the LSB header. */
438 if (start_priority
< 0 || start_priority
> 99)
439 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
440 else if (s
->sysv_start_priority
< 0)
441 s
->sysv_start_priority
= start_priority
;
443 char_array_0(runlevels
);
444 k
= delete_chars(runlevels
, WHITESPACE
"-");
449 if (!(d
= strdup(k
))) {
454 free(s
->sysv_runlevels
);
455 s
->sysv_runlevels
= d
;
458 } else if (startswith_no_case(t
, "description:")) {
460 size_t k
= strlen(t
);
463 if (t
[k
-1] == '\\') {
468 if (!(d
= strdup(strstrip(t
+12)))) {
473 free(u
->meta
.description
);
474 u
->meta
.description
= d
;
476 } else if (startswith_no_case(t
, "pidfile:")) {
483 if (!path_is_absolute(fn
)) {
484 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
488 if (!(fn
= strdup(fn
))) {
497 } else if (state
== DESCRIPTION
) {
499 /* Try to parse Red Hat style description
502 size_t k
= strlen(t
);
510 assert(u
->meta
.description
);
511 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
516 free(u
->meta
.description
);
517 u
->meta
.description
= d
;
519 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
521 if (startswith_no_case(t
, "Provides:")) {
527 FOREACH_WORD(w
, z
, t
+9, i
) {
530 if (!(n
= strndup(w
, z
))) {
535 r
= sysv_translate_facility(n
, &m
);
544 if (unit_name_to_type(m
) == UNIT_SERVICE
)
545 r
= unit_add_name(u
, m
);
547 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
, NULL
, true)) >= 0)
548 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
557 } else if (startswith_no_case(t
, "Required-Start:") ||
558 startswith_no_case(t
, "Should-Start:") ||
559 startswith_no_case(t
, "X-Start-Before:") ||
560 startswith_no_case(t
, "X-Start-After:")) {
566 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
569 if (!(n
= strndup(w
, z
))) {
574 r
= sysv_translate_facility(n
, &m
);
583 r
= unit_add_dependency_by_name(u
, startswith_no_case(t
, "X-Start-Before:") ? UNIT_BEFORE
: UNIT_AFTER
, m
, NULL
, true);
589 } else if (startswith_no_case(t
, "Default-Start:")) {
594 k
= delete_chars(t
+14, WHITESPACE
"-");
597 if (!(d
= strdup(k
))) {
602 free(s
->sysv_runlevels
);
603 s
->sysv_runlevels
= d
;
606 } else if (startswith_no_case(t
, "Description:")) {
609 state
= LSB_DESCRIPTION
;
611 if (!(d
= strdup(strstrip(t
+12)))) {
616 free(u
->meta
.description
);
617 u
->meta
.description
= d
;
619 } else if (startswith_no_case(t
, "Short-Description:") &&
620 !u
->meta
.description
) {
623 /* We use the short description only
624 * if no long description is set. */
628 if (!(d
= strdup(strstrip(t
+18)))) {
633 u
->meta
.description
= d
;
635 } else if (startswith_no_case(t
, "X-Interactive:")) {
638 if ((b
= parse_boolean(strstrip(t
+14))) < 0) {
639 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path
, line
);
644 s
->exec_context
.std_input
= EXEC_INPUT_TTY
;
646 s
->exec_context
.std_input
= EXEC_INPUT_NULL
;
648 } else if (state
== LSB_DESCRIPTION
) {
650 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
653 assert(u
->meta
.description
);
654 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
659 free(u
->meta
.description
);
660 u
->meta
.description
= d
;
667 if ((r
= sysv_exec_commands(s
)) < 0)
670 if (!s
->sysv_runlevels
|| chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
671 /* If there a runlevels configured for this service
672 * but none of the standard ones, then we assume this
673 * is some special kind of service (which might be
674 * needed for early boot) and don't create any links
677 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0 ||
678 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
682 /* Don't timeout special services during boot (like fsck) */
685 /* Special setting for all SysV services */
686 s
->type
= SERVICE_FORKING
;
687 s
->valid_no_process
= true;
688 s
->kill_mode
= KILL_PROCESS_GROUP
;
689 s
->restart
= SERVICE_ONCE
;
691 u
->meta
.load_state
= UNIT_LOADED
;
702 static int service_load_sysv_name(Service
*s
, const char *name
) {
708 /* For SysV services we strip the boot. or .sh
709 * prefixes/suffixes. */
710 if (startswith(name
, "boot.") ||
711 endswith(name
, ".sh.service"))
714 STRV_FOREACH(p
, s
->meta
.manager
->lookup_paths
.sysvinit_path
) {
718 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
721 assert(endswith(path
, ".service"));
722 path
[strlen(path
)-8] = 0;
724 r
= service_load_sysv_path(s
, path
);
726 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
727 /* Try Debian style xxx.sh source'able init scripts */
729 r
= service_load_sysv_path(s
, path
);
734 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
735 /* Try SUSE style boot.xxx init scripts */
737 if (asprintf(&path
, "%s/boot.%s", *p
, name
) < 0)
740 path
[strlen(path
)-8] = 0;
741 r
= service_load_sysv_path(s
, path
);
748 if ((s
->meta
.load_state
!= UNIT_STUB
))
755 static int service_load_sysv(Service
*s
) {
762 /* Load service data from SysV init scripts, preferably with
765 if (strv_isempty(s
->meta
.manager
->lookup_paths
.sysvinit_path
))
768 if ((t
= s
->meta
.id
))
769 if ((r
= service_load_sysv_name(s
, t
)) < 0)
772 if (s
->meta
.load_state
== UNIT_STUB
)
773 SET_FOREACH(t
, s
->meta
.names
, i
) {
777 if ((r
== service_load_sysv_name(s
, t
)) < 0)
780 if (s
->meta
.load_state
!= UNIT_STUB
)
787 static int service_add_bus_name(Service
*s
) {
794 if (asprintf(&n
, "dbus-%s.service", s
->bus_name
) < 0)
797 r
= unit_merge_by_name(UNIT(s
), n
);
803 static int service_verify(Service
*s
) {
806 if (s
->meta
.load_state
!= UNIT_LOADED
)
809 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
810 log_error("%s lacks ExecStart setting. Refusing.", s
->meta
.id
);
814 if (s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
815 log_error("%s has more than one ExecStart setting. Refusing.", s
->meta
.id
);
819 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
820 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s
->meta
.id
);
824 if (s
->exec_context
.pam_name
&& s
->kill_mode
!= KILL_CONTROL_GROUP
) {
825 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s
->meta
.id
);
832 static int service_load(Unit
*u
) {
834 Service
*s
= SERVICE(u
);
838 /* Load a .service file */
839 if ((r
= unit_load_fragment(u
)) < 0)
842 /* Load a classic init script as a fallback, if we couldn't find anything */
843 if (u
->meta
.load_state
== UNIT_STUB
)
844 if ((r
= service_load_sysv(s
)) < 0)
847 /* Still nothing found? Then let's give up */
848 if (u
->meta
.load_state
== UNIT_STUB
)
851 /* We were able to load something, then let's add in the
852 * dropin directories. */
853 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
856 /* This is a new unit? Then let's add in some extras */
857 if (u
->meta
.load_state
== UNIT_LOADED
) {
858 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
861 if ((r
= unit_add_default_cgroup(u
)) < 0)
864 if ((r
= sysv_fix_order(s
)) < 0)
868 if ((r
= service_add_bus_name(s
)) < 0)
871 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
875 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
876 s
->notify_access
= NOTIFY_MAIN
;
879 return service_verify(s
);
882 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
884 ServiceExecCommand c
;
885 Service
*s
= SERVICE(u
);
891 p2
= strappend(prefix
, "\t");
892 prefix2
= p2
? p2
: prefix
;
895 "%sService State: %s\n"
896 "%sPermissionsStartOnly: %s\n"
897 "%sRootDirectoryStartOnly: %s\n"
898 "%sValidNoProcess: %s\n"
901 "%sNotifyAccess: %s\n",
902 prefix
, service_state_to_string(s
->state
),
903 prefix
, yes_no(s
->permissions_start_only
),
904 prefix
, yes_no(s
->root_directory_start_only
),
905 prefix
, yes_no(s
->valid_no_process
),
906 prefix
, kill_mode_to_string(s
->kill_mode
),
907 prefix
, service_type_to_string(s
->type
),
908 prefix
, notify_access_to_string(s
->notify_access
));
910 if (s
->control_pid
> 0)
912 "%sControl PID: %lu\n",
913 prefix
, (unsigned long) s
->control_pid
);
918 prefix
, (unsigned long) s
->main_pid
);
923 prefix
, s
->pid_file
);
928 "%sBus Name Good: %s\n",
930 prefix
, yes_no(s
->bus_name_good
));
932 exec_context_dump(&s
->exec_context
, f
, prefix
);
934 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
936 if (!s
->exec_command
[c
])
939 fprintf(f
, "%s-> %s:\n",
940 prefix
, service_exec_command_to_string(c
));
942 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
947 "%sSysV Init Script Path: %s\n"
948 "%sSysV Init Script has LSB Header: %s\n",
949 prefix
, s
->sysv_path
,
950 prefix
, yes_no(s
->sysv_has_lsb
));
952 if (s
->sysv_start_priority
>= 0)
954 "%sSysVStartPriority: %i\n",
955 prefix
, s
->sysv_start_priority
);
957 if (s
->sysv_runlevels
)
958 fprintf(f
, "%sSysVRunLevels: %s\n",
959 prefix
, s
->sysv_runlevels
);
962 fprintf(f
, "%sStatus Text: %s\n",
963 prefix
, s
->status_text
);
968 static int service_load_pid_file(Service
*s
) {
975 if (s
->main_pid_known
)
978 assert(s
->main_pid
<= 0);
983 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
986 r
= parse_pid(k
, &pid
);
992 if (kill(pid
, 0) < 0 && errno
!= EPERM
) {
993 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
994 (unsigned long) pid
, s
->pid_file
);
998 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1001 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1002 /* FIXME: we need to do something here */
1008 static int service_get_sockets(Service
*s
, Set
**_set
) {
1017 if (s
->socket_fd
>= 0)
1020 /* Collects all Socket objects that belong to this
1021 * service. Note that a service might have multiple sockets
1022 * via multiple names. */
1024 if (!(set
= set_new(NULL
, NULL
)))
1027 SET_FOREACH(t
, s
->meta
.names
, i
) {
1031 /* Look for all socket objects that go by any of our
1032 * units and collect their fds */
1034 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
1039 p
= manager_get_unit(s
->meta
.manager
, k
);
1045 if ((r
= set_put(set
, p
)) < 0)
1057 static int service_notify_sockets_dead(Service
*s
) {
1065 if (s
->socket_fd
>= 0)
1068 /* Notifies all our sockets when we die */
1069 if ((r
= service_get_sockets(s
, &set
)) < 0)
1072 SET_FOREACH(sock
, set
, i
)
1073 socket_notify_service_dead(sock
);
1080 static void service_set_state(Service
*s
, ServiceState state
) {
1081 ServiceState old_state
;
1084 old_state
= s
->state
;
1087 if (state
!= SERVICE_START_PRE
&&
1088 state
!= SERVICE_START
&&
1089 state
!= SERVICE_START_POST
&&
1090 state
!= SERVICE_RELOAD
&&
1091 state
!= SERVICE_STOP
&&
1092 state
!= SERVICE_STOP_SIGTERM
&&
1093 state
!= SERVICE_STOP_SIGKILL
&&
1094 state
!= SERVICE_STOP_POST
&&
1095 state
!= SERVICE_FINAL_SIGTERM
&&
1096 state
!= SERVICE_FINAL_SIGKILL
&&
1097 state
!= SERVICE_AUTO_RESTART
)
1098 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1100 if (state
!= SERVICE_START
&&
1101 state
!= SERVICE_START_POST
&&
1102 state
!= SERVICE_RUNNING
&&
1103 state
!= SERVICE_RELOAD
&&
1104 state
!= SERVICE_STOP
&&
1105 state
!= SERVICE_STOP_SIGTERM
&&
1106 state
!= SERVICE_STOP_SIGKILL
)
1107 service_unwatch_main_pid(s
);
1109 if (state
!= SERVICE_START_PRE
&&
1110 state
!= SERVICE_START
&&
1111 state
!= SERVICE_START_POST
&&
1112 state
!= SERVICE_RELOAD
&&
1113 state
!= SERVICE_STOP
&&
1114 state
!= SERVICE_STOP_SIGTERM
&&
1115 state
!= SERVICE_STOP_SIGKILL
&&
1116 state
!= SERVICE_STOP_POST
&&
1117 state
!= SERVICE_FINAL_SIGTERM
&&
1118 state
!= SERVICE_FINAL_SIGKILL
) {
1119 service_unwatch_control_pid(s
);
1120 s
->control_command
= NULL
;
1121 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1124 if (state
== SERVICE_DEAD
||
1125 state
== SERVICE_STOP
||
1126 state
== SERVICE_STOP_SIGTERM
||
1127 state
== SERVICE_STOP_SIGKILL
||
1128 state
== SERVICE_STOP_POST
||
1129 state
== SERVICE_FINAL_SIGTERM
||
1130 state
== SERVICE_FINAL_SIGKILL
||
1131 state
== SERVICE_MAINTENANCE
||
1132 state
== SERVICE_AUTO_RESTART
)
1133 service_notify_sockets_dead(s
);
1135 if (state
!= SERVICE_START_PRE
&&
1136 state
!= SERVICE_START
&&
1137 state
!= SERVICE_START_POST
&&
1138 state
!= SERVICE_RUNNING
&&
1139 state
!= SERVICE_RELOAD
&&
1140 state
!= SERVICE_STOP
&&
1141 state
!= SERVICE_STOP_SIGTERM
&&
1142 state
!= SERVICE_STOP_SIGKILL
&&
1143 state
!= SERVICE_STOP_POST
&&
1144 state
!= SERVICE_FINAL_SIGTERM
&&
1145 state
!= SERVICE_FINAL_SIGKILL
&&
1146 !(state
== SERVICE_DEAD
&& s
->meta
.job
)) {
1147 service_close_socket_fd(s
);
1148 service_connection_unref(s
);
1151 if (old_state
!= state
)
1152 log_debug("%s changed %s -> %s", s
->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1154 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1157 static int service_coldplug(Unit
*u
) {
1158 Service
*s
= SERVICE(u
);
1162 assert(s
->state
== SERVICE_DEAD
);
1164 if (s
->deserialized_state
!= s
->state
) {
1166 if (s
->deserialized_state
== SERVICE_START_PRE
||
1167 s
->deserialized_state
== SERVICE_START
||
1168 s
->deserialized_state
== SERVICE_START_POST
||
1169 s
->deserialized_state
== SERVICE_RELOAD
||
1170 s
->deserialized_state
== SERVICE_STOP
||
1171 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1172 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1173 s
->deserialized_state
== SERVICE_STOP_POST
||
1174 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1175 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1176 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1178 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1181 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1183 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1188 if ((s
->deserialized_state
== SERVICE_START
&&
1189 (s
->type
== SERVICE_FORKING
||
1190 s
->type
== SERVICE_DBUS
||
1191 s
->type
== SERVICE_FINISH
||
1192 s
->type
== SERVICE_NOTIFY
)) ||
1193 s
->deserialized_state
== SERVICE_START_POST
||
1194 s
->deserialized_state
== SERVICE_RUNNING
||
1195 s
->deserialized_state
== SERVICE_RELOAD
||
1196 s
->deserialized_state
== SERVICE_STOP
||
1197 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1198 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1199 if (s
->main_pid
> 0)
1200 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1203 if (s
->deserialized_state
== SERVICE_START_PRE
||
1204 s
->deserialized_state
== SERVICE_START
||
1205 s
->deserialized_state
== SERVICE_START_POST
||
1206 s
->deserialized_state
== SERVICE_RELOAD
||
1207 s
->deserialized_state
== SERVICE_STOP
||
1208 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1209 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1210 s
->deserialized_state
== SERVICE_STOP_POST
||
1211 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1212 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1213 if (s
->control_pid
> 0)
1214 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1217 service_set_state(s
, s
->deserialized_state
);
1223 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1227 unsigned rn_fds
= 0;
1235 if (s
->socket_fd
>= 0)
1238 if ((r
= service_get_sockets(s
, &set
)) < 0)
1241 SET_FOREACH(sock
, set
, i
) {
1245 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1257 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1263 memcpy(t
, rfds
, rn_fds
);
1264 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1269 rn_fds
= rn_fds
+cn_fds
;
1287 static int service_spawn(
1292 bool apply_permissions
,
1294 bool set_notify_socket
,
1299 int *fds
= NULL
, *fdsbuf
= NULL
;
1301 char **argv
= NULL
, **env
= NULL
;
1308 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1309 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1310 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1312 if (s
->socket_fd
>= 0) {
1313 fds
= &s
->socket_fd
;
1316 if ((r
= service_collect_fds(s
, &fdsbuf
, &n_fds
)) < 0)
1323 if (timeout
&& s
->timeout_usec
) {
1324 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1327 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1329 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1334 if (set_notify_socket
) {
1337 if (asprintf(&t
, "NOTIFY_SOCKET=@%s", s
->meta
.manager
->notify_socket
) < 0) {
1342 env
= strv_env_set(s
->meta
.manager
->environment
, t
);
1350 env
= s
->meta
.manager
->environment
;
1359 s
->meta
.manager
->confirm_spawn
,
1360 s
->meta
.cgroup_bondings
,
1366 if (set_notify_socket
)
1376 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1377 /* FIXME: we need to do something here */
1389 if (set_notify_socket
)
1393 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1398 static int main_pid_good(Service
*s
) {
1401 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1404 /* If we know the pid file, then lets just check if it is
1406 if (s
->main_pid_known
)
1407 return s
->main_pid
> 0;
1409 /* We don't know the pid */
1413 static int control_pid_good(Service
*s
) {
1416 return s
->control_pid
> 0;
1419 static int cgroup_good(Service
*s
) {
1424 if ((r
= cgroup_bonding_is_empty_list(s
->meta
.cgroup_bondings
)) < 0)
1430 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1437 if (allow_restart
&&
1439 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1440 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1442 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1445 service_set_state(s
, SERVICE_AUTO_RESTART
);
1447 service_set_state(s
, s
->failure
? SERVICE_MAINTENANCE
: SERVICE_DEAD
);
1452 log_warning("%s failed to run install restart timer: %s", s
->meta
.id
, strerror(-r
));
1453 service_enter_dead(s
, false, false);
1456 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1458 static void service_enter_stop_post(Service
*s
, bool success
) {
1465 service_unwatch_control_pid(s
);
1467 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1468 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1469 if ((r
= service_spawn(s
,
1473 !s
->permissions_start_only
,
1474 !s
->root_directory_start_only
,
1476 &s
->control_pid
)) < 0)
1480 service_set_state(s
, SERVICE_STOP_POST
);
1482 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1487 log_warning("%s failed to run 'stop-post' task: %s", s
->meta
.id
, strerror(-r
));
1488 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1491 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1500 if (s
->kill_mode
!= KILL_NONE
) {
1501 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1503 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1505 if ((r
= cgroup_bonding_kill_list(s
->meta
.cgroup_bondings
, sig
)) < 0) {
1506 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1515 if (s
->main_pid
> 0) {
1516 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1522 if (s
->control_pid
> 0) {
1523 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1534 if (sent
&& (s
->main_pid
> 0 || s
->control_pid
> 0)) {
1535 if (s
->timeout_usec
> 0)
1536 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1539 service_set_state(s
, state
);
1540 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1541 service_enter_stop_post(s
, true);
1543 service_enter_dead(s
, true, true);
1548 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
1550 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1551 service_enter_stop_post(s
, false);
1553 service_enter_dead(s
, false, true);
1556 static void service_enter_stop(Service
*s
, bool success
) {
1564 service_unwatch_control_pid(s
);
1566 s
->control_command_id
= SERVICE_EXEC_STOP
;
1567 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1568 if ((r
= service_spawn(s
,
1572 !s
->permissions_start_only
,
1573 !s
->root_directory_start_only
,
1575 &s
->control_pid
)) < 0)
1578 service_set_state(s
, SERVICE_STOP
);
1580 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1585 log_warning("%s failed to run 'stop' task: %s", s
->meta
.id
, strerror(-r
));
1586 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1589 static void service_enter_running(Service
*s
, bool success
) {
1590 int main_pid_ok
, cgroup_ok
;
1596 main_pid_ok
= main_pid_good(s
);
1597 cgroup_ok
= cgroup_good(s
);
1599 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1600 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1601 service_set_state(s
, SERVICE_RUNNING
);
1602 else if (s
->valid_no_process
)
1603 service_set_state(s
, SERVICE_EXITED
);
1605 service_enter_stop(s
, true);
1608 static void service_enter_start_post(Service
*s
) {
1612 service_unwatch_control_pid(s
);
1614 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1615 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1616 if ((r
= service_spawn(s
,
1620 !s
->permissions_start_only
,
1621 !s
->root_directory_start_only
,
1623 &s
->control_pid
)) < 0)
1626 service_set_state(s
, SERVICE_START_POST
);
1628 service_enter_running(s
, true);
1633 log_warning("%s failed to run 'start-post' task: %s", s
->meta
.id
, strerror(-r
));
1634 service_enter_stop(s
, false);
1637 static void service_enter_start(Service
*s
) {
1643 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1644 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1646 if (s
->type
== SERVICE_FORKING
)
1647 service_unwatch_control_pid(s
);
1649 service_unwatch_main_pid(s
);
1651 if ((r
= service_spawn(s
,
1652 s
->exec_command
[SERVICE_EXEC_START
],
1653 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
,
1657 s
->notify_access
!= NOTIFY_NONE
,
1661 if (s
->type
== SERVICE_SIMPLE
) {
1662 /* For simple services we immediately start
1663 * the START_POST binaries. */
1665 service_set_main_pid(s
, pid
);
1666 service_enter_start_post(s
);
1668 } else if (s
->type
== SERVICE_FORKING
) {
1670 /* For forking services we wait until the start
1671 * process exited. */
1673 s
->control_command_id
= SERVICE_EXEC_START
;
1674 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1676 s
->control_pid
= pid
;
1677 service_set_state(s
, SERVICE_START
);
1679 } else if (s
->type
== SERVICE_FINISH
||
1680 s
->type
== SERVICE_DBUS
||
1681 s
->type
== SERVICE_NOTIFY
) {
1683 /* For finishing services we wait until the start
1684 * process exited, too, but it is our main process. */
1686 /* For D-Bus services we know the main pid right away,
1687 * but wait for the bus name to appear on the
1688 * bus. Notify services are similar. */
1690 service_set_main_pid(s
, pid
);
1691 service_set_state(s
, SERVICE_START
);
1693 assert_not_reached("Unknown service type");
1698 log_warning("%s failed to run 'start' task: %s", s
->meta
.id
, strerror(-r
));
1699 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1702 static void service_enter_start_pre(Service
*s
) {
1707 service_unwatch_control_pid(s
);
1709 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1710 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1711 if ((r
= service_spawn(s
,
1715 !s
->permissions_start_only
,
1716 !s
->root_directory_start_only
,
1718 &s
->control_pid
)) < 0)
1721 service_set_state(s
, SERVICE_START_PRE
);
1723 service_enter_start(s
);
1728 log_warning("%s failed to run 'start-pre' task: %s", s
->meta
.id
, strerror(-r
));
1729 service_enter_dead(s
, false, true);
1732 static void service_enter_restart(Service
*s
) {
1736 service_enter_dead(s
, true, false);
1738 if ((r
= manager_add_job(s
->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1741 log_debug("%s scheduled restart job.", s
->meta
.id
);
1746 log_warning("%s failed to schedule restart job: %s", s
->meta
.id
, strerror(-r
));
1747 service_enter_dead(s
, false, false);
1750 static void service_enter_reload(Service
*s
) {
1755 service_unwatch_control_pid(s
);
1757 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1758 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1759 if ((r
= service_spawn(s
,
1763 !s
->permissions_start_only
,
1764 !s
->root_directory_start_only
,
1766 &s
->control_pid
)) < 0)
1769 service_set_state(s
, SERVICE_RELOAD
);
1771 service_enter_running(s
, true);
1776 log_warning("%s failed to run 'reload' task: %s", s
->meta
.id
, strerror(-r
));
1777 service_enter_stop(s
, false);
1780 static void service_run_next(Service
*s
, bool success
) {
1784 assert(s
->control_command
);
1785 assert(s
->control_command
->command_next
);
1790 s
->control_command
= s
->control_command
->command_next
;
1792 service_unwatch_control_pid(s
);
1794 if ((r
= service_spawn(s
,
1798 !s
->permissions_start_only
,
1799 !s
->root_directory_start_only
,
1801 &s
->control_pid
)) < 0)
1807 log_warning("%s failed to run next task: %s", s
->meta
.id
, strerror(-r
));
1809 if (s
->state
== SERVICE_START_PRE
)
1810 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1811 else if (s
->state
== SERVICE_STOP
)
1812 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1813 else if (s
->state
== SERVICE_STOP_POST
)
1814 service_enter_dead(s
, false, true);
1816 service_enter_stop(s
, false);
1819 static int service_start(Unit
*u
) {
1820 Service
*s
= SERVICE(u
);
1824 /* We cannot fulfill this request right now, try again later
1826 if (s
->state
== SERVICE_STOP
||
1827 s
->state
== SERVICE_STOP_SIGTERM
||
1828 s
->state
== SERVICE_STOP_SIGKILL
||
1829 s
->state
== SERVICE_STOP_POST
||
1830 s
->state
== SERVICE_FINAL_SIGTERM
||
1831 s
->state
== SERVICE_FINAL_SIGKILL
)
1834 /* Already on it! */
1835 if (s
->state
== SERVICE_START_PRE
||
1836 s
->state
== SERVICE_START
||
1837 s
->state
== SERVICE_START_POST
)
1840 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTENANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1842 /* Make sure we don't enter a busy loop of some kind. */
1843 if (!ratelimit_test(&s
->ratelimit
)) {
1844 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1849 s
->main_pid_known
= false;
1850 s
->allow_restart
= true;
1852 service_enter_start_pre(s
);
1856 static int service_stop(Unit
*u
) {
1857 Service
*s
= SERVICE(u
);
1861 /* Cannot do this now */
1862 if (s
->state
== SERVICE_START_PRE
||
1863 s
->state
== SERVICE_START
||
1864 s
->state
== SERVICE_START_POST
||
1865 s
->state
== SERVICE_RELOAD
)
1869 if (s
->state
== SERVICE_STOP
||
1870 s
->state
== SERVICE_STOP_SIGTERM
||
1871 s
->state
== SERVICE_STOP_SIGKILL
||
1872 s
->state
== SERVICE_STOP_POST
||
1873 s
->state
== SERVICE_FINAL_SIGTERM
||
1874 s
->state
== SERVICE_FINAL_SIGKILL
)
1877 if (s
->state
== SERVICE_AUTO_RESTART
) {
1878 service_set_state(s
, SERVICE_DEAD
);
1882 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1884 /* This is a user request, so don't do restarts on this
1886 s
->allow_restart
= false;
1888 service_enter_stop(s
, true);
1892 static int service_reload(Unit
*u
) {
1893 Service
*s
= SERVICE(u
);
1897 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1899 service_enter_reload(s
);
1903 static bool service_can_reload(Unit
*u
) {
1904 Service
*s
= SERVICE(u
);
1908 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1911 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1912 Service
*s
= SERVICE(u
);
1918 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1919 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1921 if (s
->control_pid
> 0)
1922 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
1924 if (s
->main_pid_known
&& s
->main_pid
> 0)
1925 unit_serialize_item_format(u
, f
, "main-pid", "%lu", (unsigned long) s
->main_pid
);
1927 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1929 /* There's a minor uncleanliness here: if there are multiple
1930 * commands attached here, we will start from the first one
1932 if (s
->control_command_id
>= 0)
1933 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
1935 if (s
->socket_fd
>= 0) {
1938 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
1941 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1947 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1948 Service
*s
= SERVICE(u
);
1956 if (streq(key
, "state")) {
1959 if ((state
= service_state_from_string(value
)) < 0)
1960 log_debug("Failed to parse state value %s", value
);
1962 s
->deserialized_state
= state
;
1963 } else if (streq(key
, "failure")) {
1966 if ((b
= parse_boolean(value
)) < 0)
1967 log_debug("Failed to parse failure value %s", value
);
1969 s
->failure
= b
|| s
->failure
;
1970 } else if (streq(key
, "control-pid")) {
1973 if ((r
= parse_pid(value
, &pid
)) < 0)
1974 log_debug("Failed to parse control-pid value %s", value
);
1976 s
->control_pid
= pid
;
1977 } else if (streq(key
, "main-pid")) {
1980 if ((r
= parse_pid(value
, &pid
)) < 0)
1981 log_debug("Failed to parse main-pid value %s", value
);
1983 service_set_main_pid(s
, (pid_t
) pid
);
1984 } else if (streq(key
, "main-pid-known")) {
1987 if ((b
= parse_boolean(value
)) < 0)
1988 log_debug("Failed to parse main-pid-known value %s", value
);
1990 s
->main_pid_known
= b
;
1991 } else if (streq(key
, "control-command")) {
1992 ServiceExecCommand id
;
1994 if ((id
= service_exec_command_from_string(value
)) < 0)
1995 log_debug("Failed to parse exec-command value %s", value
);
1997 s
->control_command_id
= id
;
1998 s
->control_command
= s
->exec_command
[id
];
2000 } else if (streq(key
, "socket-fd")) {
2003 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2004 log_debug("Failed to parse socket-fd value %s", value
);
2007 if (s
->socket_fd
>= 0)
2008 close_nointr_nofail(s
->socket_fd
);
2009 s
->socket_fd
= fdset_remove(fds
, fd
);
2012 log_debug("Unknown serialization key '%s'", key
);
2017 static UnitActiveState
service_active_state(Unit
*u
) {
2020 return state_translation_table
[SERVICE(u
)->state
];
2023 static const char *service_sub_state_to_string(Unit
*u
) {
2026 return service_state_to_string(SERVICE(u
)->state
);
2029 static bool service_check_gc(Unit
*u
) {
2030 Service
*s
= SERVICE(u
);
2034 return !!s
->sysv_path
;
2037 static bool service_check_snapshot(Unit
*u
) {
2038 Service
*s
= SERVICE(u
);
2042 return !s
->got_socket_fd
;
2045 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2046 Service
*s
= SERVICE(u
);
2052 success
= is_clean_exit(code
, status
);
2053 s
->failure
= s
->failure
|| !success
;
2055 if (s
->main_pid
== pid
) {
2057 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
2060 if (s
->type
!= SERVICE_FORKING
) {
2061 assert(s
->exec_command
[SERVICE_EXEC_START
]);
2062 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
2065 log_debug("%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2067 /* The service exited, so the service is officially
2072 case SERVICE_START_POST
:
2073 case SERVICE_RELOAD
:
2075 /* Need to wait until the operation is
2080 if (s
->type
== SERVICE_FINISH
) {
2081 /* This was our main goal, so let's go on */
2083 service_enter_start_post(s
);
2085 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2088 assert(s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
);
2093 case SERVICE_RUNNING
:
2094 service_enter_running(s
, success
);
2097 case SERVICE_STOP_SIGTERM
:
2098 case SERVICE_STOP_SIGKILL
:
2100 if (!control_pid_good(s
))
2101 service_enter_stop_post(s
, success
);
2103 /* If there is still a control process, wait for that first */
2107 assert_not_reached("Uh, main process died at wrong time.");
2110 } else if (s
->control_pid
== pid
) {
2112 if (s
->control_command
)
2113 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
2117 log_debug("%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2119 /* If we are shutting things down anyway we
2120 * don't care about failing commands. */
2122 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
2124 /* There is another command to *
2125 * execute, so let's do that. */
2127 log_debug("%s running next command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2128 service_run_next(s
, success
);
2131 /* No further commands for this step, so let's
2132 * figure out what to do next */
2134 s
->control_command
= NULL
;
2135 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2137 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2141 case SERVICE_START_PRE
:
2143 service_enter_start(s
);
2145 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2149 assert(s
->type
== SERVICE_FORKING
);
2151 /* Let's try to load the pid
2152 * file here if we can. We
2153 * ignore the return value,
2154 * since the PID file might
2155 * actually be created by a
2156 * START_POST script */
2160 service_load_pid_file(s
);
2162 service_enter_start_post(s
);
2164 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2168 case SERVICE_START_POST
:
2169 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
2172 /* Hmm, let's see if we can
2173 * load the pid now after the
2174 * start-post scripts got
2177 if ((r
= service_load_pid_file(s
)) < 0)
2178 log_warning("%s: failed to load PID file %s: %s", s
->meta
.id
, s
->pid_file
, strerror(-r
));
2183 case SERVICE_RELOAD
:
2185 service_enter_running(s
, true);
2187 service_enter_stop(s
, false);
2192 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2195 case SERVICE_STOP_SIGTERM
:
2196 case SERVICE_STOP_SIGKILL
:
2197 if (main_pid_good(s
) <= 0)
2198 service_enter_stop_post(s
, success
);
2200 /* If there is still a service
2201 * process around, wait until
2202 * that one quit, too */
2205 case SERVICE_STOP_POST
:
2206 case SERVICE_FINAL_SIGTERM
:
2207 case SERVICE_FINAL_SIGKILL
:
2208 service_enter_dead(s
, success
, true);
2212 assert_not_reached("Uh, control process died at wrong time.");
2218 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2219 Service
*s
= SERVICE(u
);
2222 assert(elapsed
== 1);
2224 assert(w
== &s
->timer_watch
);
2228 case SERVICE_START_PRE
:
2230 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2231 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2234 case SERVICE_START_POST
:
2235 case SERVICE_RELOAD
:
2236 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2237 service_enter_stop(s
, false);
2241 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2242 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2245 case SERVICE_STOP_SIGTERM
:
2246 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2247 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2250 case SERVICE_STOP_SIGKILL
:
2251 /* Uh, wie sent a SIGKILL and it is still not gone?
2252 * Must be something we cannot kill, so let's just be
2253 * weirded out and continue */
2255 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2256 service_enter_stop_post(s
, false);
2259 case SERVICE_STOP_POST
:
2260 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2261 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2264 case SERVICE_FINAL_SIGTERM
:
2265 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2266 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2269 case SERVICE_FINAL_SIGKILL
:
2270 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u
->meta
.id
);
2271 service_enter_dead(s
, false, true);
2274 case SERVICE_AUTO_RESTART
:
2275 log_debug("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2276 service_enter_restart(s
);
2280 assert_not_reached("Timeout at wrong time.");
2284 static void service_cgroup_notify_event(Unit
*u
) {
2285 Service
*s
= SERVICE(u
);
2289 log_debug("%s: cgroup is empty", u
->meta
.id
);
2293 /* Waiting for SIGCHLD is usually more interesting,
2294 * because it includes return codes/signals. Which is
2295 * why we ignore the cgroup events for most cases,
2296 * except when we don't know pid which to expect the
2299 case SERVICE_RUNNING
:
2300 service_enter_running(s
, true);
2308 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
) {
2309 Service
*s
= SERVICE(u
);
2314 if (s
->notify_access
== NOTIFY_NONE
) {
2315 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2316 u
->meta
.id
, (unsigned long) pid
);
2320 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2321 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2322 u
->meta
.id
, (unsigned long) pid
, (unsigned long) s
->main_pid
);
2326 log_debug("%s: Got message", u
->meta
.id
);
2328 /* Interpret MAINPID= */
2329 if ((e
= strv_find_prefix(tags
, "MAINPID=")) &&
2330 (s
->state
== SERVICE_START
||
2331 s
->state
== SERVICE_START_POST
||
2332 s
->state
== SERVICE_RUNNING
||
2333 s
->state
== SERVICE_RELOAD
)) {
2335 if (parse_pid(e
+ 8, &pid
) < 0)
2336 log_warning("Failed to parse %s", e
);
2338 log_debug("%s: got %s", u
->meta
.id
, e
);
2339 service_set_main_pid(s
, pid
);
2343 /* Interpret READY= */
2344 if (s
->type
== SERVICE_NOTIFY
&&
2345 s
->state
== SERVICE_START
&&
2346 strv_find(tags
, "READY=1")) {
2347 log_debug("%s: got READY=1", u
->meta
.id
);
2349 service_enter_start_post(s
);
2352 /* Interpret STATUS= */
2353 if ((e
= strv_find_prefix(tags
, "STATUS="))) {
2356 if (!(t
= strdup(e
+7))) {
2357 log_error("Failed to allocate string.");
2361 log_debug("%s: got %s", u
->meta
.id
, e
);
2363 free(s
->status_text
);
2368 static int service_enumerate(Manager
*m
) {
2372 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2377 STRV_FOREACH(p
, m
->lookup_paths
.sysvrcnd_path
)
2378 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
2383 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
].path
) < 0) {
2391 if (!(d
= opendir(path
))) {
2392 if (errno
!= ENOENT
)
2393 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2398 while ((de
= readdir(d
))) {
2402 if (ignore_file(de
->d_name
))
2405 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2408 if (strlen(de
->d_name
) < 4)
2411 a
= undecchar(de
->d_name
[1]);
2412 b
= undecchar(de
->d_name
[2]);
2419 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
].path
, de
->d_name
) < 0) {
2424 if (access(fpath
, X_OK
) < 0) {
2426 if (errno
!= ENOENT
)
2427 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2433 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
2438 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, &service
)) < 0) {
2439 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
2443 if (de
->d_name
[0] == 'S' &&
2444 (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
))
2445 SERVICE(service
)->sysv_start_priority
=
2446 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority
);
2448 manager_dispatch_load_queue(m
);
2449 service
= unit_follow_merge(service
);
2451 if (de
->d_name
[0] == 'S') {
2452 Unit
*runlevel_target
;
2454 if ((r
= manager_load_unit(m
, rcnd_table
[i
].target
, NULL
, &runlevel_target
)) < 0)
2457 if ((r
= unit_add_dependency(runlevel_target
, UNIT_WANTS
, service
, true)) < 0)
2460 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, runlevel_target
, true)) < 0)
2463 } else if (de
->d_name
[0] == 'K' &&
2464 (rcnd_table
[i
].type
== RUNLEVEL_DOWN
||
2465 rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
)) {
2466 Unit
*shutdown_target
;
2468 /* We honour K links only for
2469 * halt/reboot. For the normal
2470 * runlevels we assume the
2472 * implicitly added by the
2473 * core logic. Also, we don't
2474 * really distuingish here
2475 * between the runlevels 0 and
2476 * 6 and just add them to the
2477 * special shutdown target. On
2478 * SUSE the boot.d/ runlevel
2479 * is also used for shutdown,
2480 * so we add links for that
2481 * too to the shutdown
2484 if ((r
= manager_load_unit(m
, SPECIAL_SHUTDOWN_TARGET
, NULL
, &shutdown_target
)) < 0)
2487 if ((r
= unit_add_dependency(service
, UNIT_CONFLICTS
, shutdown_target
, true)) < 0)
2490 if ((r
= unit_add_dependency(service
, UNIT_BEFORE
, shutdown_target
, true)) < 0)
2509 static void service_bus_name_owner_change(
2512 const char *old_owner
,
2513 const char *new_owner
) {
2515 Service
*s
= SERVICE(u
);
2520 assert(streq(s
->bus_name
, name
));
2521 assert(old_owner
|| new_owner
);
2523 if (old_owner
&& new_owner
)
2524 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2526 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2528 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2530 s
->bus_name_good
= !!new_owner
;
2532 if (s
->type
== SERVICE_DBUS
) {
2534 /* service_enter_running() will figure out what to
2536 if (s
->state
== SERVICE_RUNNING
)
2537 service_enter_running(s
, true);
2538 else if (s
->state
== SERVICE_START
&& new_owner
)
2539 service_enter_start_post(s
);
2541 } else if (new_owner
&&
2543 (s
->state
== SERVICE_START
||
2544 s
->state
== SERVICE_START_POST
||
2545 s
->state
== SERVICE_RUNNING
||
2546 s
->state
== SERVICE_RELOAD
)) {
2548 /* Try to acquire PID from bus service */
2549 log_debug("Trying to acquire PID from D-Bus name...");
2551 bus_query_pid(u
->meta
.manager
, name
);
2555 static void service_bus_query_pid_done(
2560 Service
*s
= SERVICE(u
);
2565 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2567 if (s
->main_pid
<= 0 &&
2568 (s
->state
== SERVICE_START
||
2569 s
->state
== SERVICE_START_POST
||
2570 s
->state
== SERVICE_RUNNING
||
2571 s
->state
== SERVICE_RELOAD
))
2572 service_set_main_pid(s
, pid
);
2575 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
) {
2579 /* This is called by the socket code when instantiating a new
2580 * service for a stream socket and the socket needs to be
2583 if (s
->meta
.load_state
!= UNIT_LOADED
)
2586 if (s
->socket_fd
>= 0)
2589 if (s
->state
!= SERVICE_DEAD
)
2593 s
->got_socket_fd
= true;
2599 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2600 [SERVICE_DEAD
] = "dead",
2601 [SERVICE_START_PRE
] = "start-pre",
2602 [SERVICE_START
] = "start",
2603 [SERVICE_START_POST
] = "start-post",
2604 [SERVICE_RUNNING
] = "running",
2605 [SERVICE_EXITED
] = "exited",
2606 [SERVICE_RELOAD
] = "reload",
2607 [SERVICE_STOP
] = "stop",
2608 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2609 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2610 [SERVICE_STOP_POST
] = "stop-post",
2611 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2612 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2613 [SERVICE_MAINTENANCE
] = "maintenance",
2614 [SERVICE_AUTO_RESTART
] = "auto-restart",
2617 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2619 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2620 [SERVICE_ONCE
] = "once",
2621 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2622 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2625 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2627 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2628 [SERVICE_FORKING
] = "forking",
2629 [SERVICE_SIMPLE
] = "simple",
2630 [SERVICE_FINISH
] = "finish",
2631 [SERVICE_DBUS
] = "dbus",
2632 [SERVICE_NOTIFY
] = "notify"
2635 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2637 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2638 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2639 [SERVICE_EXEC_START
] = "ExecStart",
2640 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2641 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2642 [SERVICE_EXEC_STOP
] = "ExecStop",
2643 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2646 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2648 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
2649 [NOTIFY_NONE
] = "none",
2650 [NOTIFY_MAIN
] = "main",
2651 [NOTIFY_ALL
] = "all"
2654 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
2656 const UnitVTable service_vtable
= {
2657 .suffix
= ".service",
2659 .init
= service_init
,
2660 .done
= service_done
,
2661 .load
= service_load
,
2663 .coldplug
= service_coldplug
,
2665 .dump
= service_dump
,
2667 .start
= service_start
,
2668 .stop
= service_stop
,
2669 .reload
= service_reload
,
2671 .can_reload
= service_can_reload
,
2673 .serialize
= service_serialize
,
2674 .deserialize_item
= service_deserialize_item
,
2676 .active_state
= service_active_state
,
2677 .sub_state_to_string
= service_sub_state_to_string
,
2679 .check_gc
= service_check_gc
,
2680 .check_snapshot
= service_check_snapshot
,
2682 .sigchld_event
= service_sigchld_event
,
2683 .timer_event
= service_timer_event
,
2685 .cgroup_notify_empty
= service_cgroup_notify_event
,
2686 .notify_message
= service_notify_message
,
2688 .bus_name_owner_change
= service_bus_name_owner_change
,
2689 .bus_query_pid_done
= service_bus_query_pid_done
,
2691 .bus_message_handler
= bus_service_message_handler
,
2693 .enumerate
= service_enumerate