1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #include "bus-errors.h"
38 #define COMMENTS "#;\n"
39 #define NEWLINES "\n\r"
41 typedef enum RunlevelType
{
50 const RunlevelType type
;
52 /* Standard SysV runlevels for start-up */
53 { "rc1.d", SPECIAL_RESCUE_TARGET
, RUNLEVEL_UP
},
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET
, RUNLEVEL_UP
},
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET
, RUNLEVEL_UP
},
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET
, RUNLEVEL_UP
},
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET
, RUNLEVEL_UP
},
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
65 /* Debian style rcS.d */
66 { "rcS.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
69 /* Standard SysV runlevels for shutdown */
70 { "rc0.d", SPECIAL_POWEROFF_TARGET
, RUNLEVEL_DOWN
},
71 { "rc6.d", SPECIAL_REBOOT_TARGET
, RUNLEVEL_DOWN
}
73 /* Note that the order here matters, as we read the
74 directories in this order, and we want to make sure that
75 sysv_start_priority is known when we first load the
76 unit. And that value we only know from S links. Hence
77 UP/SYSINIT must be read before DOWN */
80 #define RUNLEVELS_UP "12345"
81 /* #define RUNLEVELS_DOWN "06" */
82 /* #define RUNLEVELS_BOOT "bBsS" */
84 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
85 [SERVICE_DEAD
] = UNIT_INACTIVE
,
86 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
87 [SERVICE_START
] = UNIT_ACTIVATING
,
88 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
89 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
90 [SERVICE_EXITED
] = UNIT_ACTIVE
,
91 [SERVICE_RELOAD
] = UNIT_RELOADING
,
92 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
93 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
94 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
95 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
96 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
97 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
98 [SERVICE_FAILED
] = UNIT_FAILED
,
99 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
102 static void service_init(Unit
*u
) {
103 Service
*s
= SERVICE(u
);
106 assert(u
->meta
.load_state
== UNIT_STUB
);
108 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
109 s
->restart_usec
= DEFAULT_RESTART_USEC
;
110 s
->timer_watch
.type
= WATCH_INVALID
;
111 s
->sysv_start_priority
= -1;
114 exec_context_init(&s
->exec_context
);
116 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
118 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
121 static void service_unwatch_control_pid(Service
*s
) {
124 if (s
->control_pid
<= 0)
127 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
131 static void service_unwatch_main_pid(Service
*s
) {
134 if (s
->main_pid
<= 0)
137 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
141 static int service_set_main_pid(Service
*s
, pid_t pid
) {
152 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid())
153 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
154 s
->meta
.id
, (unsigned long) pid
);
157 s
->main_pid_known
= true;
159 exec_status_start(&s
->main_exec_status
, pid
);
164 static void service_close_socket_fd(Service
*s
) {
167 if (s
->socket_fd
< 0)
170 close_nointr_nofail(s
->socket_fd
);
174 static void service_connection_unref(Service
*s
) {
180 socket_connection_unref(s
->socket
);
184 static void service_done(Unit
*u
) {
185 Service
*s
= SERVICE(u
);
195 free(s
->sysv_runlevels
);
196 s
->sysv_runlevels
= NULL
;
198 free(s
->status_text
);
199 s
->status_text
= NULL
;
201 exec_context_done(&s
->exec_context
);
202 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
203 s
->control_command
= NULL
;
205 /* This will leak a process, but at least no memory or any of
207 service_unwatch_main_pid(s
);
208 service_unwatch_control_pid(s
);
211 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
216 service_close_socket_fd(s
);
217 service_connection_unref(s
);
219 unit_unwatch_timer(u
, &s
->timer_watch
);
222 static char *sysv_translate_name(const char *name
) {
225 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
228 if (startswith(name
, "boot."))
229 /* Drop SuSE-style boot. prefix */
230 strcpy(stpcpy(r
, name
+ 5), ".service");
231 else if (endswith(name
, ".sh"))
232 /* Drop Debian-style .sh suffix */
233 strcpy(stpcpy(r
, name
) - 3, ".service");
235 /* Normal init scripts */
236 strcpy(stpcpy(r
, name
), ".service");
241 static int sysv_translate_facility(const char *name
, char **_r
) {
243 static const char * const table
[] = {
244 /* LSB defined facilities */
245 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
246 "$network", SPECIAL_NETWORK_TARGET
,
247 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
248 "$portmap", SPECIAL_RPCBIND_TARGET
,
249 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
250 "$syslog", SPECIAL_SYSLOG_TARGET
,
251 "$time", SPECIAL_RTC_SET_TARGET
,
253 /* Debian extensions */
255 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
257 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
258 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
,
261 /* Fedora extensions, lacking the $ prefix */
262 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
263 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
264 "httpd", SPECIAL_HTTP_DAEMON_TARGET
,
271 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
272 if (streq(table
[i
], name
)) {
273 if (!(r
= strdup(table
[i
+1])))
280 r
= unit_name_build(name
+1, NULL
, ".target");
282 r
= sysv_translate_name(name
);
295 static int sysv_fix_order(Service
*s
) {
301 if (s
->sysv_start_priority
< 0)
304 /* For each pair of services where at least one lacks a LSB
305 * header, we use the start priority value to order things. */
307 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
310 bool special_s
, special_t
;
312 t
= (Service
*) other
;
317 if (t
->sysv_start_priority
< 0)
320 /* If both units have modern headers we don't care
321 * about the priorities */
322 if ((s
->meta
.fragment_path
|| s
->sysv_has_lsb
) &&
323 (t
->meta
.fragment_path
|| t
->sysv_has_lsb
))
326 special_s
= s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
);
327 special_t
= t
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, t
->sysv_runlevels
);
329 if (special_t
&& !special_s
)
331 else if (special_s
&& !special_t
)
333 else if (t
->sysv_start_priority
< s
->sysv_start_priority
)
335 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
340 /* FIXME: Maybe we should compare the name here lexicographically? */
342 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
349 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
352 if (!(c
= new0(ExecCommand
, 1)))
355 if (!(c
->path
= strdup(path
))) {
360 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
369 static int sysv_exec_commands(Service
*s
) {
373 assert(s
->sysv_path
);
375 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
377 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
379 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
381 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
383 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
385 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
390 static int service_load_sysv_path(Service
*s
, const char *path
) {
407 if (!(f
= fopen(path
, "re"))) {
408 r
= errno
== ENOENT
? 0 : -errno
;
413 if (!(s
->sysv_path
= strdup(path
))) {
419 char l
[LINE_MAX
], *t
;
421 if (!fgets(l
, sizeof(l
), f
)) {
426 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
436 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
438 s
->sysv_has_lsb
= true;
442 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
448 t
+= strspn(t
, WHITESPACE
);
450 if (state
== NORMAL
) {
452 /* Try to parse Red Hat style chkconfig headers */
454 if (startswith_no_case(t
, "chkconfig:")) {
456 char runlevels
[16], *k
;
460 if (sscanf(t
+10, "%15s %i %*i",
462 &start_priority
) != 2) {
464 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
468 /* A start priority gathered from the
469 * symlink farms is preferred over the
470 * data from the LSB header. */
471 if (start_priority
< 0 || start_priority
> 99)
472 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
473 else if (s
->sysv_start_priority
< 0)
474 s
->sysv_start_priority
= start_priority
;
476 char_array_0(runlevels
);
477 k
= delete_chars(runlevels
, WHITESPACE
"-");
482 if (!(d
= strdup(k
))) {
487 free(s
->sysv_runlevels
);
488 s
->sysv_runlevels
= d
;
491 } else if (startswith_no_case(t
, "description:") &&
492 !u
->meta
.description
) {
494 size_t k
= strlen(t
);
497 if (t
[k
-1] == '\\') {
502 if (!(d
= strappend("LSB: ", strstrip(t
+12)))) {
507 free(u
->meta
.description
);
508 u
->meta
.description
= d
;
510 } else if (startswith_no_case(t
, "pidfile:")) {
517 if (!path_is_absolute(fn
)) {
518 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
522 if (!(fn
= strdup(fn
))) {
531 } else if (state
== DESCRIPTION
) {
533 /* Try to parse Red Hat style description
536 size_t k
= strlen(t
);
544 assert(u
->meta
.description
);
545 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
550 free(u
->meta
.description
);
551 u
->meta
.description
= d
;
553 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
555 if (startswith_no_case(t
, "Provides:")) {
561 FOREACH_WORD_QUOTED(w
, z
, t
+9, i
) {
564 if (!(n
= strndup(w
, z
))) {
569 r
= sysv_translate_facility(n
, &m
);
578 if (unit_name_to_type(m
) == UNIT_SERVICE
)
579 r
= unit_add_name(u
, m
);
581 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
583 if (s
->sysv_enabled
) {
586 if ((k
= unit_add_dependency_by_name_inverse(u
, UNIT_WANTS
, m
, NULL
, true)) < 0)
592 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path
, line
, m
, strerror(-r
));
597 } else if (startswith_no_case(t
, "Required-Start:") ||
598 startswith_no_case(t
, "Should-Start:") ||
599 startswith_no_case(t
, "X-Start-Before:") ||
600 startswith_no_case(t
, "X-Start-After:")) {
606 FOREACH_WORD_QUOTED(w
, z
, strchr(t
, ':')+1, i
) {
609 if (!(n
= strndup(w
, z
))) {
614 r
= sysv_translate_facility(n
, &m
);
623 r
= unit_add_dependency_by_name(u
, startswith_no_case(t
, "X-Start-Before:") ? UNIT_BEFORE
: UNIT_AFTER
, m
, NULL
, true);
626 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path
, line
, m
, strerror(-r
));
630 } else if (startswith_no_case(t
, "Default-Start:")) {
635 k
= delete_chars(t
+14, WHITESPACE
"-");
638 if (!(d
= strdup(k
))) {
643 free(s
->sysv_runlevels
);
644 s
->sysv_runlevels
= d
;
647 } else if (startswith_no_case(t
, "Description:") &&
648 !u
->meta
.description
) {
651 /* We use the long description only if
652 * no short description is set. */
654 state
= LSB_DESCRIPTION
;
656 if (!(d
= strappend("LSB: ", strstrip(t
+12)))) {
661 free(u
->meta
.description
);
662 u
->meta
.description
= d
;
664 } else if (startswith_no_case(t
, "Short-Description:")) {
669 if (!(d
= strappend("LSB: ", strstrip(t
+18)))) {
674 free(u
->meta
.description
);
675 u
->meta
.description
= d
;
677 } else if (startswith_no_case(t
, "X-Interactive:")) {
680 if ((b
= parse_boolean(strstrip(t
+14))) < 0) {
681 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path
, line
);
686 s
->exec_context
.std_input
= EXEC_INPUT_TTY
;
688 s
->exec_context
.std_input
= EXEC_INPUT_NULL
;
690 } else if (state
== LSB_DESCRIPTION
) {
692 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
695 assert(u
->meta
.description
);
696 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
701 free(u
->meta
.description
);
702 u
->meta
.description
= d
;
709 if ((r
= sysv_exec_commands(s
)) < 0)
712 if (s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
713 /* If there a runlevels configured for this service
714 * but none of the standard ones, then we assume this
715 * is some special kind of service (which might be
716 * needed for early boot) and don't create any links
719 s
->meta
.default_dependencies
= false;
721 /* Don't timeout special services during boot (like fsck) */
725 /* Special setting for all SysV services */
726 s
->type
= SERVICE_FORKING
;
727 s
->remain_after_exit
= true;
728 s
->restart
= SERVICE_ONCE
;
729 s
->exec_context
.std_output
=
730 (s
->meta
.manager
->sysv_console
|| s
->exec_context
.std_input
== EXEC_INPUT_TTY
)
731 ? EXEC_OUTPUT_TTY
: EXEC_OUTPUT_NULL
;
732 s
->exec_context
.kill_mode
= KILL_PROCESS_GROUP
;
734 u
->meta
.load_state
= UNIT_LOADED
;
745 static int service_load_sysv_name(Service
*s
, const char *name
) {
751 /* For SysV services we strip the boot. or .sh
752 * prefixes/suffixes. */
753 if (startswith(name
, "boot.") ||
754 endswith(name
, ".sh.service"))
757 STRV_FOREACH(p
, s
->meta
.manager
->lookup_paths
.sysvinit_path
) {
761 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
764 assert(endswith(path
, ".service"));
765 path
[strlen(path
)-8] = 0;
767 r
= service_load_sysv_path(s
, path
);
769 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
770 /* Try Debian style xxx.sh source'able init scripts */
772 r
= service_load_sysv_path(s
, path
);
777 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
778 /* Try SUSE style boot.xxx init scripts */
780 if (asprintf(&path
, "%s/boot.%s", *p
, name
) < 0)
783 path
[strlen(path
)-8] = 0;
784 r
= service_load_sysv_path(s
, path
);
791 if ((s
->meta
.load_state
!= UNIT_STUB
))
798 static int service_load_sysv(Service
*s
) {
805 /* Load service data from SysV init scripts, preferably with
808 if (strv_isempty(s
->meta
.manager
->lookup_paths
.sysvinit_path
))
811 if ((t
= s
->meta
.id
))
812 if ((r
= service_load_sysv_name(s
, t
)) < 0)
815 if (s
->meta
.load_state
== UNIT_STUB
)
816 SET_FOREACH(t
, s
->meta
.names
, i
) {
820 if ((r
= service_load_sysv_name(s
, t
)) < 0)
823 if (s
->meta
.load_state
!= UNIT_STUB
)
830 static int service_verify(Service
*s
) {
833 if (s
->meta
.load_state
!= UNIT_LOADED
)
836 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
837 log_error("%s lacks ExecStart setting. Refusing.", s
->meta
.id
);
841 if (s
->type
!= SERVICE_ONESHOT
&&
842 s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
843 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s
->meta
.id
);
847 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
848 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s
->meta
.id
);
852 if (s
->exec_context
.pam_name
&& s
->exec_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
853 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s
->meta
.id
);
860 static int service_add_default_dependencies(Service
*s
) {
865 /* Add a number of automatic dependencies useful for the
866 * majority of services. */
868 /* First, pull in base system */
869 if (s
->meta
.manager
->running_as
== MANAGER_SYSTEM
) {
871 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
874 } else if (s
->meta
.manager
->running_as
== MANAGER_SESSION
) {
876 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
880 /* Second, activate normal shutdown */
881 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTED_BY
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
884 static int service_load(Unit
*u
) {
886 Service
*s
= SERVICE(u
);
890 /* Load a .service file */
891 if ((r
= unit_load_fragment(u
)) < 0)
894 /* Load a classic init script as a fallback, if we couldn't find anything */
895 if (u
->meta
.load_state
== UNIT_STUB
)
896 if ((r
= service_load_sysv(s
)) < 0)
899 /* Still nothing found? Then let's give up */
900 if (u
->meta
.load_state
== UNIT_STUB
)
903 /* We were able to load something, then let's add in the
904 * dropin directories. */
905 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
908 /* This is a new unit? Then let's add in some extras */
909 if (u
->meta
.load_state
== UNIT_LOADED
) {
910 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
913 if ((r
= unit_add_default_cgroup(u
)) < 0)
916 if ((r
= sysv_fix_order(s
)) < 0)
920 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
923 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
924 s
->notify_access
= NOTIFY_MAIN
;
926 if (s
->type
== SERVICE_DBUS
|| s
->bus_name
)
927 if ((r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_DBUS_TARGET
, NULL
, true)) < 0)
930 if (s
->meta
.default_dependencies
)
931 if ((r
= service_add_default_dependencies(s
)) < 0)
935 return service_verify(s
);
938 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
940 ServiceExecCommand c
;
941 Service
*s
= SERVICE(u
);
947 p2
= strappend(prefix
, "\t");
948 prefix2
= p2
? p2
: prefix
;
951 "%sService State: %s\n"
952 "%sPermissionsStartOnly: %s\n"
953 "%sRootDirectoryStartOnly: %s\n"
954 "%sRemainAfterExit: %s\n"
957 "%sNotifyAccess: %s\n",
958 prefix
, service_state_to_string(s
->state
),
959 prefix
, yes_no(s
->permissions_start_only
),
960 prefix
, yes_no(s
->root_directory_start_only
),
961 prefix
, yes_no(s
->remain_after_exit
),
962 prefix
, service_type_to_string(s
->type
),
963 prefix
, service_restart_to_string(s
->restart
),
964 prefix
, notify_access_to_string(s
->notify_access
));
966 if (s
->control_pid
> 0)
968 "%sControl PID: %lu\n",
969 prefix
, (unsigned long) s
->control_pid
);
974 prefix
, (unsigned long) s
->main_pid
);
979 prefix
, s
->pid_file
);
984 "%sBus Name Good: %s\n",
986 prefix
, yes_no(s
->bus_name_good
));
988 exec_context_dump(&s
->exec_context
, f
, prefix
);
990 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
992 if (!s
->exec_command
[c
])
995 fprintf(f
, "%s-> %s:\n",
996 prefix
, service_exec_command_to_string(c
));
998 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1003 "%sSysV Init Script Path: %s\n"
1004 "%sSysV Init Script has LSB Header: %s\n",
1005 prefix
, s
->sysv_path
,
1006 prefix
, yes_no(s
->sysv_has_lsb
));
1008 if (s
->sysv_start_priority
>= 0)
1010 "%sSysVStartPriority: %i\n"
1011 "%sSysVEnabled: %s\n",
1012 prefix
, s
->sysv_start_priority
,
1013 prefix
, yes_no(s
->sysv_enabled
));
1015 if (s
->sysv_runlevels
)
1016 fprintf(f
, "%sSysVRunLevels: %s\n",
1017 prefix
, s
->sysv_runlevels
);
1020 fprintf(f
, "%sStatus Text: %s\n",
1021 prefix
, s
->status_text
);
1026 static int service_load_pid_file(Service
*s
) {
1033 if (s
->main_pid_known
)
1036 assert(s
->main_pid
<= 0);
1041 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
1044 r
= parse_pid(k
, &pid
);
1050 if (kill(pid
, 0) < 0 && errno
!= EPERM
) {
1051 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1052 (unsigned long) pid
, s
->pid_file
);
1056 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1059 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1060 /* FIXME: we need to do something here */
1066 static int service_get_sockets(Service
*s
, Set
**_set
) {
1075 if (s
->socket_fd
>= 0)
1078 /* Collects all Socket objects that belong to this
1079 * service. Note that a service might have multiple sockets
1080 * via multiple names. */
1082 if (!(set
= set_new(NULL
, NULL
)))
1085 SET_FOREACH(t
, s
->meta
.names
, i
) {
1089 /* Look for all socket objects that go by any of our
1090 * units and collect their fds */
1092 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
1097 p
= manager_get_unit(s
->meta
.manager
, k
);
1103 if ((r
= set_put(set
, p
)) < 0)
1115 static int service_notify_sockets_dead(Service
*s
) {
1123 if (s
->socket_fd
>= 0)
1126 /* Notifies all our sockets when we die */
1127 if ((r
= service_get_sockets(s
, &set
)) < 0)
1130 SET_FOREACH(sock
, set
, i
)
1131 socket_notify_service_dead(sock
);
1138 static void service_set_state(Service
*s
, ServiceState state
) {
1139 ServiceState old_state
;
1142 old_state
= s
->state
;
1145 if (state
!= SERVICE_START_PRE
&&
1146 state
!= SERVICE_START
&&
1147 state
!= SERVICE_START_POST
&&
1148 state
!= SERVICE_RELOAD
&&
1149 state
!= SERVICE_STOP
&&
1150 state
!= SERVICE_STOP_SIGTERM
&&
1151 state
!= SERVICE_STOP_SIGKILL
&&
1152 state
!= SERVICE_STOP_POST
&&
1153 state
!= SERVICE_FINAL_SIGTERM
&&
1154 state
!= SERVICE_FINAL_SIGKILL
&&
1155 state
!= SERVICE_AUTO_RESTART
)
1156 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1158 if (state
!= SERVICE_START
&&
1159 state
!= SERVICE_START_POST
&&
1160 state
!= SERVICE_RUNNING
&&
1161 state
!= SERVICE_RELOAD
&&
1162 state
!= SERVICE_STOP
&&
1163 state
!= SERVICE_STOP_SIGTERM
&&
1164 state
!= SERVICE_STOP_SIGKILL
)
1165 service_unwatch_main_pid(s
);
1167 if (state
!= SERVICE_START_PRE
&&
1168 state
!= SERVICE_START
&&
1169 state
!= SERVICE_START_POST
&&
1170 state
!= SERVICE_RELOAD
&&
1171 state
!= SERVICE_STOP
&&
1172 state
!= SERVICE_STOP_SIGTERM
&&
1173 state
!= SERVICE_STOP_SIGKILL
&&
1174 state
!= SERVICE_STOP_POST
&&
1175 state
!= SERVICE_FINAL_SIGTERM
&&
1176 state
!= SERVICE_FINAL_SIGKILL
) {
1177 service_unwatch_control_pid(s
);
1178 s
->control_command
= NULL
;
1179 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1182 if (state
== SERVICE_DEAD
||
1183 state
== SERVICE_STOP
||
1184 state
== SERVICE_STOP_SIGTERM
||
1185 state
== SERVICE_STOP_SIGKILL
||
1186 state
== SERVICE_STOP_POST
||
1187 state
== SERVICE_FINAL_SIGTERM
||
1188 state
== SERVICE_FINAL_SIGKILL
||
1189 state
== SERVICE_FAILED
||
1190 state
== SERVICE_AUTO_RESTART
)
1191 service_notify_sockets_dead(s
);
1193 if (state
!= SERVICE_START_PRE
&&
1194 state
!= SERVICE_START
&&
1195 state
!= SERVICE_START_POST
&&
1196 state
!= SERVICE_RUNNING
&&
1197 state
!= SERVICE_RELOAD
&&
1198 state
!= SERVICE_STOP
&&
1199 state
!= SERVICE_STOP_SIGTERM
&&
1200 state
!= SERVICE_STOP_SIGKILL
&&
1201 state
!= SERVICE_STOP_POST
&&
1202 state
!= SERVICE_FINAL_SIGTERM
&&
1203 state
!= SERVICE_FINAL_SIGKILL
&&
1204 !(state
== SERVICE_DEAD
&& s
->meta
.job
)) {
1205 service_close_socket_fd(s
);
1206 service_connection_unref(s
);
1209 /* For the inactive states unit_notify() will trim the cgroup,
1210 * but for exit we have to do that ourselves... */
1211 if (state
== SERVICE_EXITED
)
1212 cgroup_bonding_trim_list(s
->meta
.cgroup_bondings
, true);
1214 if (old_state
!= state
)
1215 log_debug("%s changed %s -> %s", s
->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1217 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
1220 static int service_coldplug(Unit
*u
) {
1221 Service
*s
= SERVICE(u
);
1225 assert(s
->state
== SERVICE_DEAD
);
1227 if (s
->deserialized_state
!= s
->state
) {
1229 if (s
->deserialized_state
== SERVICE_START_PRE
||
1230 s
->deserialized_state
== SERVICE_START
||
1231 s
->deserialized_state
== SERVICE_START_POST
||
1232 s
->deserialized_state
== SERVICE_RELOAD
||
1233 s
->deserialized_state
== SERVICE_STOP
||
1234 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1235 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1236 s
->deserialized_state
== SERVICE_STOP_POST
||
1237 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1238 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1239 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1241 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1244 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1246 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1251 if ((s
->deserialized_state
== SERVICE_START
&&
1252 (s
->type
== SERVICE_FORKING
||
1253 s
->type
== SERVICE_DBUS
||
1254 s
->type
== SERVICE_ONESHOT
||
1255 s
->type
== SERVICE_NOTIFY
)) ||
1256 s
->deserialized_state
== SERVICE_START_POST
||
1257 s
->deserialized_state
== SERVICE_RUNNING
||
1258 s
->deserialized_state
== SERVICE_RELOAD
||
1259 s
->deserialized_state
== SERVICE_STOP
||
1260 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1261 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1262 if (s
->main_pid
> 0)
1263 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1266 if (s
->deserialized_state
== SERVICE_START_PRE
||
1267 s
->deserialized_state
== SERVICE_START
||
1268 s
->deserialized_state
== SERVICE_START_POST
||
1269 s
->deserialized_state
== SERVICE_RELOAD
||
1270 s
->deserialized_state
== SERVICE_STOP
||
1271 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1272 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1273 s
->deserialized_state
== SERVICE_STOP_POST
||
1274 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1275 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1276 if (s
->control_pid
> 0)
1277 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1280 service_set_state(s
, s
->deserialized_state
);
1286 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1290 unsigned rn_fds
= 0;
1298 if (s
->socket_fd
>= 0)
1301 if ((r
= service_get_sockets(s
, &set
)) < 0)
1304 SET_FOREACH(sock
, set
, i
) {
1308 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1320 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1326 memcpy(t
, rfds
, rn_fds
);
1327 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1332 rn_fds
= rn_fds
+cn_fds
;
1350 static int service_spawn(
1355 bool apply_permissions
,
1357 bool apply_tty_stdin
,
1358 bool set_notify_socket
,
1363 int *fds
= NULL
, *fdsbuf
= NULL
;
1364 unsigned n_fds
= 0, n_env
= 0;
1365 char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1372 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1373 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1374 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1376 if (s
->socket_fd
>= 0) {
1377 fds
= &s
->socket_fd
;
1380 if ((r
= service_collect_fds(s
, &fdsbuf
, &n_fds
)) < 0)
1387 if (timeout
&& s
->timeout_usec
) {
1388 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1391 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1393 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1398 if (!(our_env
= new0(char*, 3))) {
1403 if (set_notify_socket
)
1404 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=@%s", s
->meta
.manager
->notify_socket
) < 0) {
1409 if (s
->main_pid
> 0)
1410 if (asprintf(our_env
+ n_env
++, "MAINPID=%lu", (unsigned long) s
->main_pid
) < 0) {
1415 if (!(final_env
= strv_env_merge(2,
1416 s
->meta
.manager
->environment
,
1431 s
->meta
.manager
->confirm_spawn
,
1432 s
->meta
.cgroup_bondings
,
1439 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1440 /* FIXME: we need to do something here */
1446 strv_free(final_env
);
1456 strv_free(final_env
);
1459 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1464 static int main_pid_good(Service
*s
) {
1467 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1470 /* If we know the pid file, then lets just check if it is
1472 if (s
->main_pid_known
)
1473 return s
->main_pid
> 0;
1475 /* We don't know the pid */
1479 static int control_pid_good(Service
*s
) {
1482 return s
->control_pid
> 0;
1485 static int cgroup_good(Service
*s
) {
1490 if ((r
= cgroup_bonding_is_empty_list(s
->meta
.cgroup_bondings
)) < 0)
1496 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1503 if (allow_restart
&&
1504 !s
->forbid_restart
&&
1505 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1506 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1508 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1511 service_set_state(s
, SERVICE_AUTO_RESTART
);
1513 service_set_state(s
, s
->failure
? SERVICE_FAILED
: SERVICE_DEAD
);
1515 s
->forbid_restart
= false;
1520 log_warning("%s failed to run install restart timer: %s", s
->meta
.id
, strerror(-r
));
1521 service_enter_dead(s
, false, false);
1524 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1526 static void service_enter_stop_post(Service
*s
, bool success
) {
1533 service_unwatch_control_pid(s
);
1535 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1536 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1537 if ((r
= service_spawn(s
,
1541 !s
->permissions_start_only
,
1542 !s
->root_directory_start_only
,
1545 &s
->control_pid
)) < 0)
1549 service_set_state(s
, SERVICE_STOP_POST
);
1551 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1556 log_warning("%s failed to run 'stop-post' task: %s", s
->meta
.id
, strerror(-r
));
1557 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1560 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1562 Set
*pid_set
= NULL
;
1563 bool wait_for_exit
= false;
1570 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
1571 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
1573 if (s
->main_pid
> 0) {
1574 if (kill(s
->exec_context
.kill_mode
== KILL_PROCESS_GROUP
?
1576 s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1578 log_warning("Failed to kill main process %li: %m", (long) s
->main_pid
);
1580 wait_for_exit
= true;
1583 if (s
->control_pid
> 0) {
1584 if (kill(s
->exec_context
.kill_mode
== KILL_PROCESS_GROUP
?
1586 s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1588 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1590 wait_for_exit
= true;
1593 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1595 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
1600 /* Exclude the main/control pids from being killed via the cgroup */
1601 if (s
->main_pid
> 0)
1602 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0)
1605 if (s
->control_pid
> 0)
1606 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
1609 if ((r
= cgroup_bonding_kill_list(s
->meta
.cgroup_bondings
, sig
, pid_set
)) < 0) {
1610 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
1611 log_warning("Failed to kill control group: %s", strerror(-r
));
1613 wait_for_exit
= true;
1619 if (wait_for_exit
) {
1620 if (s
->timeout_usec
> 0)
1621 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1624 service_set_state(s
, state
);
1625 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1626 service_enter_stop_post(s
, true);
1628 service_enter_dead(s
, true, true);
1633 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
1635 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1636 service_enter_stop_post(s
, false);
1638 service_enter_dead(s
, false, true);
1644 static void service_enter_stop(Service
*s
, bool success
) {
1652 service_unwatch_control_pid(s
);
1654 s
->control_command_id
= SERVICE_EXEC_STOP
;
1655 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1656 if ((r
= service_spawn(s
,
1660 !s
->permissions_start_only
,
1661 !s
->root_directory_start_only
,
1664 &s
->control_pid
)) < 0)
1667 service_set_state(s
, SERVICE_STOP
);
1669 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1674 log_warning("%s failed to run 'stop' task: %s", s
->meta
.id
, strerror(-r
));
1675 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1678 static void service_enter_running(Service
*s
, bool success
) {
1679 int main_pid_ok
, cgroup_ok
;
1685 main_pid_ok
= main_pid_good(s
);
1686 cgroup_ok
= cgroup_good(s
);
1688 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1689 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1690 service_set_state(s
, SERVICE_RUNNING
);
1691 else if (s
->remain_after_exit
)
1692 service_set_state(s
, SERVICE_EXITED
);
1694 service_enter_stop(s
, true);
1697 static void service_enter_start_post(Service
*s
) {
1701 service_unwatch_control_pid(s
);
1703 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1704 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1705 if ((r
= service_spawn(s
,
1709 !s
->permissions_start_only
,
1710 !s
->root_directory_start_only
,
1713 &s
->control_pid
)) < 0)
1716 service_set_state(s
, SERVICE_START_POST
);
1718 service_enter_running(s
, true);
1723 log_warning("%s failed to run 'start-post' task: %s", s
->meta
.id
, strerror(-r
));
1724 service_enter_stop(s
, false);
1727 static void service_enter_start(Service
*s
) {
1733 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1734 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
|| s
->type
== SERVICE_ONESHOT
);
1736 if (s
->type
== SERVICE_FORKING
)
1737 service_unwatch_control_pid(s
);
1739 service_unwatch_main_pid(s
);
1741 s
->control_command_id
= SERVICE_EXEC_START
;
1742 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1744 if ((r
= service_spawn(s
,
1746 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
,
1751 s
->notify_access
!= NOTIFY_NONE
,
1755 if (s
->type
== SERVICE_SIMPLE
) {
1756 /* For simple services we immediately start
1757 * the START_POST binaries. */
1759 service_set_main_pid(s
, pid
);
1760 service_enter_start_post(s
);
1762 } else if (s
->type
== SERVICE_FORKING
) {
1764 /* For forking services we wait until the start
1765 * process exited. */
1767 s
->control_pid
= pid
;
1768 service_set_state(s
, SERVICE_START
);
1770 } else if (s
->type
== SERVICE_ONESHOT
||
1771 s
->type
== SERVICE_DBUS
||
1772 s
->type
== SERVICE_NOTIFY
) {
1774 /* For oneshot services we wait until the start
1775 * process exited, too, but it is our main process. */
1777 /* For D-Bus services we know the main pid right away,
1778 * but wait for the bus name to appear on the
1779 * bus. Notify services are similar. */
1781 service_set_main_pid(s
, pid
);
1782 service_set_state(s
, SERVICE_START
);
1784 assert_not_reached("Unknown service type");
1789 log_warning("%s failed to run 'start' task: %s", s
->meta
.id
, strerror(-r
));
1790 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1793 static void service_enter_start_pre(Service
*s
) {
1798 service_unwatch_control_pid(s
);
1800 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1801 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
1802 if ((r
= service_spawn(s
,
1806 !s
->permissions_start_only
,
1807 !s
->root_directory_start_only
,
1810 &s
->control_pid
)) < 0)
1813 service_set_state(s
, SERVICE_START_PRE
);
1815 service_enter_start(s
);
1820 log_warning("%s failed to run 'start-pre' task: %s", s
->meta
.id
, strerror(-r
));
1821 service_enter_dead(s
, false, true);
1824 static void service_enter_restart(Service
*s
) {
1829 dbus_error_init(&error
);
1831 service_enter_dead(s
, true, false);
1833 if ((r
= manager_add_job(s
->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
)) < 0)
1836 log_debug("%s scheduled restart job.", s
->meta
.id
);
1840 log_warning("%s failed to schedule restart job: %s", s
->meta
.id
, bus_error(&error
, -r
));
1841 service_enter_dead(s
, false, false);
1843 dbus_error_free(&error
);
1846 static void service_enter_reload(Service
*s
) {
1851 service_unwatch_control_pid(s
);
1853 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1854 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
1855 if ((r
= service_spawn(s
,
1859 !s
->permissions_start_only
,
1860 !s
->root_directory_start_only
,
1863 &s
->control_pid
)) < 0)
1866 service_set_state(s
, SERVICE_RELOAD
);
1868 service_enter_running(s
, true);
1873 log_warning("%s failed to run 'reload' task: %s", s
->meta
.id
, strerror(-r
));
1874 service_enter_stop(s
, false);
1877 static void service_run_next_control(Service
*s
, bool success
) {
1881 assert(s
->control_command
);
1882 assert(s
->control_command
->command_next
);
1887 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1889 s
->control_command
= s
->control_command
->command_next
;
1890 service_unwatch_control_pid(s
);
1892 if ((r
= service_spawn(s
,
1896 !s
->permissions_start_only
,
1897 !s
->root_directory_start_only
,
1898 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1899 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1901 &s
->control_pid
)) < 0)
1907 log_warning("%s failed to run next control task: %s", s
->meta
.id
, strerror(-r
));
1909 if (s
->state
== SERVICE_START_PRE
)
1910 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1911 else if (s
->state
== SERVICE_STOP
)
1912 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1913 else if (s
->state
== SERVICE_STOP_POST
)
1914 service_enter_dead(s
, false, true);
1916 service_enter_stop(s
, false);
1919 static void service_run_next_main(Service
*s
, bool success
) {
1924 assert(s
->control_command
);
1925 assert(s
->control_command
->command_next
);
1930 assert(s
->control_command_id
== SERVICE_EXEC_START
);
1931 assert(s
->type
== SERVICE_ONESHOT
);
1933 s
->control_command
= s
->control_command
->command_next
;
1934 service_unwatch_main_pid(s
);
1936 if ((r
= service_spawn(s
,
1943 s
->notify_access
!= NOTIFY_NONE
,
1947 service_set_main_pid(s
, pid
);
1952 log_warning("%s failed to run next main task: %s", s
->meta
.id
, strerror(-r
));
1953 service_enter_stop(s
, false);
1956 static int service_start(Unit
*u
) {
1957 Service
*s
= SERVICE(u
);
1961 /* We cannot fulfill this request right now, try again later
1963 if (s
->state
== SERVICE_STOP
||
1964 s
->state
== SERVICE_STOP_SIGTERM
||
1965 s
->state
== SERVICE_STOP_SIGKILL
||
1966 s
->state
== SERVICE_STOP_POST
||
1967 s
->state
== SERVICE_FINAL_SIGTERM
||
1968 s
->state
== SERVICE_FINAL_SIGKILL
)
1971 /* Already on it! */
1972 if (s
->state
== SERVICE_START_PRE
||
1973 s
->state
== SERVICE_START
||
1974 s
->state
== SERVICE_START_POST
)
1977 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_FAILED
|| s
->state
== SERVICE_AUTO_RESTART
);
1979 /* Make sure we don't enter a busy loop of some kind. */
1980 if (!ratelimit_test(&s
->ratelimit
)) {
1981 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
1985 if ((s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1986 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1987 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) &&
1989 log_warning("%s can only be started with a per-connection socket.", u
->meta
.id
);
1994 s
->main_pid_known
= false;
1995 s
->forbid_restart
= false;
1997 service_enter_start_pre(s
);
2001 static int service_stop(Unit
*u
) {
2002 Service
*s
= SERVICE(u
);
2006 /* This is a user request, so don't do restarts on this
2008 s
->forbid_restart
= true;
2011 if (s
->state
== SERVICE_STOP
||
2012 s
->state
== SERVICE_STOP_SIGTERM
||
2013 s
->state
== SERVICE_STOP_SIGKILL
||
2014 s
->state
== SERVICE_STOP_POST
||
2015 s
->state
== SERVICE_FINAL_SIGTERM
||
2016 s
->state
== SERVICE_FINAL_SIGKILL
)
2019 /* Don't allow a restart */
2020 if (s
->state
== SERVICE_AUTO_RESTART
) {
2021 service_set_state(s
, SERVICE_DEAD
);
2025 /* If there's already something running we go directly into
2027 if (s
->state
== SERVICE_START_PRE
||
2028 s
->state
== SERVICE_START
||
2029 s
->state
== SERVICE_START_POST
||
2030 s
->state
== SERVICE_RELOAD
) {
2031 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
2035 assert(s
->state
== SERVICE_RUNNING
||
2036 s
->state
== SERVICE_EXITED
);
2038 service_enter_stop(s
, true);
2042 static int service_reload(Unit
*u
) {
2043 Service
*s
= SERVICE(u
);
2047 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2049 service_enter_reload(s
);
2053 static bool service_can_reload(Unit
*u
) {
2054 Service
*s
= SERVICE(u
);
2058 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2061 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2062 Service
*s
= SERVICE(u
);
2068 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2069 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
2071 if (s
->control_pid
> 0)
2072 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
2074 if (s
->main_pid_known
&& s
->main_pid
> 0)
2075 unit_serialize_item_format(u
, f
, "main-pid", "%lu", (unsigned long) s
->main_pid
);
2077 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2080 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
2082 /* There's a minor uncleanliness here: if there are multiple
2083 * commands attached here, we will start from the first one
2085 if (s
->control_command_id
>= 0)
2086 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2088 if (s
->socket_fd
>= 0) {
2091 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
2094 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2097 if (s
->main_exec_status
.pid
> 0) {
2098 unit_serialize_item_format(u
, f
, "main-exec-status-pid", "%lu", (unsigned long) s
->main_exec_status
.pid
);
2100 if (s
->main_exec_status
.start_timestamp
.realtime
> 0) {
2101 unit_serialize_item_format(u
, f
, "main-exec-status-start-realtime",
2102 "%llu", (unsigned long long) s
->main_exec_status
.start_timestamp
.realtime
);
2104 unit_serialize_item_format(u
, f
, "main-exec-status-start-monotonic",
2105 "%llu", (unsigned long long) s
->main_exec_status
.start_timestamp
.monotonic
);
2108 if (s
->main_exec_status
.exit_timestamp
.realtime
> 0) {
2109 unit_serialize_item_format(u
, f
, "main-exec-status-exit-realtime",
2110 "%llu", (unsigned long long) s
->main_exec_status
.exit_timestamp
.realtime
);
2111 unit_serialize_item_format(u
, f
, "main-exec-status-exit-monotonic",
2112 "%llu", (unsigned long long) s
->main_exec_status
.exit_timestamp
.monotonic
);
2114 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2115 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2122 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2123 Service
*s
= SERVICE(u
);
2130 if (streq(key
, "state")) {
2133 if ((state
= service_state_from_string(value
)) < 0)
2134 log_debug("Failed to parse state value %s", value
);
2136 s
->deserialized_state
= state
;
2137 } else if (streq(key
, "failure")) {
2140 if ((b
= parse_boolean(value
)) < 0)
2141 log_debug("Failed to parse failure value %s", value
);
2143 s
->failure
= b
|| s
->failure
;
2144 } else if (streq(key
, "control-pid")) {
2147 if (parse_pid(value
, &pid
) < 0)
2148 log_debug("Failed to parse control-pid value %s", value
);
2150 s
->control_pid
= pid
;
2151 } else if (streq(key
, "main-pid")) {
2154 if (parse_pid(value
, &pid
) < 0)
2155 log_debug("Failed to parse main-pid value %s", value
);
2157 service_set_main_pid(s
, (pid_t
) pid
);
2158 } else if (streq(key
, "main-pid-known")) {
2161 if ((b
= parse_boolean(value
)) < 0)
2162 log_debug("Failed to parse main-pid-known value %s", value
);
2164 s
->main_pid_known
= b
;
2165 } else if (streq(key
, "status-text")) {
2168 if ((t
= strdup(value
))) {
2169 free(s
->status_text
);
2173 } else if (streq(key
, "control-command")) {
2174 ServiceExecCommand id
;
2176 if ((id
= service_exec_command_from_string(value
)) < 0)
2177 log_debug("Failed to parse exec-command value %s", value
);
2179 s
->control_command_id
= id
;
2180 s
->control_command
= s
->exec_command
[id
];
2182 } else if (streq(key
, "socket-fd")) {
2185 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2186 log_debug("Failed to parse socket-fd value %s", value
);
2189 if (s
->socket_fd
>= 0)
2190 close_nointr_nofail(s
->socket_fd
);
2191 s
->socket_fd
= fdset_remove(fds
, fd
);
2193 } else if (streq(key
, "main-exec-status-pid")) {
2196 if (parse_pid(value
, &pid
) < 0)
2197 log_debug("Failed to parse main-exec-status-pid value %s", value
);
2199 s
->main_exec_status
.pid
= pid
;
2200 } else if (streq(key
, "main-exec-status-code")) {
2203 if (safe_atoi(value
, &i
) < 0)
2204 log_debug("Failed to parse main-exec-status-code value %s", value
);
2206 s
->main_exec_status
.code
= i
;
2207 } else if (streq(key
, "main-exec-status-status")) {
2210 if (safe_atoi(value
, &i
) < 0)
2211 log_debug("Failed to parse main-exec-status-status value %s", value
);
2213 s
->main_exec_status
.status
= i
;
2214 } else if (streq(key
, "main-exec-status-start-realtime")) {
2217 if (safe_atou64(value
, &k
) < 0)
2218 log_debug("Failed to parse main-exec-status-start-realtime value %s", value
);
2220 s
->main_exec_status
.start_timestamp
.realtime
= (usec_t
) k
;
2221 } else if (streq(key
, "main-exec-status-start-monotonic")) {
2224 if (safe_atou64(value
, &k
) < 0)
2225 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value
);
2227 s
->main_exec_status
.start_timestamp
.monotonic
= (usec_t
) k
;
2228 } else if (streq(key
, "main-exec-status-exit-realtime")) {
2231 if (safe_atou64(value
, &k
) < 0)
2232 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value
);
2234 s
->main_exec_status
.exit_timestamp
.realtime
= (usec_t
) k
;
2235 } else if (streq(key
, "main-exec-status-exit-monotonic")) {
2238 if (safe_atou64(value
, &k
) < 0)
2239 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value
);
2241 s
->main_exec_status
.exit_timestamp
.monotonic
= (usec_t
) k
;
2243 log_debug("Unknown serialization key '%s'", key
);
2248 static UnitActiveState
service_active_state(Unit
*u
) {
2251 return state_translation_table
[SERVICE(u
)->state
];
2254 static const char *service_sub_state_to_string(Unit
*u
) {
2257 return service_state_to_string(SERVICE(u
)->state
);
2260 static bool service_check_gc(Unit
*u
) {
2261 Service
*s
= SERVICE(u
);
2265 return !!s
->sysv_path
;
2268 static bool service_check_snapshot(Unit
*u
) {
2269 Service
*s
= SERVICE(u
);
2273 return !s
->got_socket_fd
;
2276 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2277 Service
*s
= SERVICE(u
);
2283 if (!s
->meta
.fragment_path
)
2284 success
= is_clean_exit_lsb(code
, status
);
2286 success
= is_clean_exit(code
, status
);
2288 if (s
->main_pid
== pid
) {
2291 exec_status_exit(&s
->main_exec_status
, pid
, code
, status
);
2293 if (s
->type
!= SERVICE_FORKING
&& s
->control_command
) {
2294 s
->control_command
->exec_status
= s
->main_exec_status
;
2296 if (s
->control_command
->ignore
)
2300 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
2301 "%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2302 s
->failure
= s
->failure
|| !success
;
2304 if (s
->control_command
&&
2305 s
->control_command
->command_next
&&
2308 /* There is another command to *
2309 * execute, so let's do that. */
2311 log_debug("%s running next main command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2312 service_run_next_main(s
, success
);
2316 /* The service exited, so the service is officially
2319 s
->control_command
= NULL
;
2320 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2324 case SERVICE_START_POST
:
2325 case SERVICE_RELOAD
:
2327 /* Need to wait until the operation is
2332 if (s
->type
== SERVICE_ONESHOT
) {
2333 /* This was our main goal, so let's go on */
2335 service_enter_start_post(s
);
2337 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2340 assert(s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
);
2345 case SERVICE_RUNNING
:
2346 service_enter_running(s
, success
);
2349 case SERVICE_STOP_SIGTERM
:
2350 case SERVICE_STOP_SIGKILL
:
2352 if (!control_pid_good(s
))
2353 service_enter_stop_post(s
, success
);
2355 /* If there is still a control process, wait for that first */
2359 assert_not_reached("Uh, main process died at wrong time.");
2363 } else if (s
->control_pid
== pid
) {
2367 if (s
->control_command
) {
2368 exec_status_exit(&s
->control_command
->exec_status
, pid
, code
, status
);
2370 if (s
->control_command
->ignore
)
2374 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
2375 "%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2376 s
->failure
= s
->failure
|| !success
;
2378 if (s
->control_command
&&
2379 s
->control_command
->command_next
&&
2382 /* There is another command to *
2383 * execute, so let's do that. */
2385 log_debug("%s running next control command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2386 service_run_next_control(s
, success
);
2389 /* No further commands for this step, so let's
2390 * figure out what to do next */
2392 s
->control_command
= NULL
;
2393 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2395 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2399 case SERVICE_START_PRE
:
2401 service_enter_start(s
);
2403 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2407 assert(s
->type
== SERVICE_FORKING
);
2409 /* Let's try to load the pid
2410 * file here if we can. We
2411 * ignore the return value,
2412 * since the PID file might
2413 * actually be created by a
2414 * START_POST script */
2418 service_load_pid_file(s
);
2420 service_enter_start_post(s
);
2422 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2426 case SERVICE_START_POST
:
2427 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
2430 /* Hmm, let's see if we can
2431 * load the pid now after the
2432 * start-post scripts got
2435 if ((r
= service_load_pid_file(s
)) < 0)
2436 log_warning("%s: failed to load PID file %s: %s", s
->meta
.id
, s
->pid_file
, strerror(-r
));
2441 case SERVICE_RELOAD
:
2443 service_enter_running(s
, true);
2445 service_enter_stop(s
, false);
2450 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2453 case SERVICE_STOP_SIGTERM
:
2454 case SERVICE_STOP_SIGKILL
:
2455 if (main_pid_good(s
) <= 0)
2456 service_enter_stop_post(s
, success
);
2458 /* If there is still a service
2459 * process around, wait until
2460 * that one quit, too */
2463 case SERVICE_STOP_POST
:
2464 case SERVICE_FINAL_SIGTERM
:
2465 case SERVICE_FINAL_SIGKILL
:
2466 service_enter_dead(s
, success
, true);
2470 assert_not_reached("Uh, control process died at wrong time.");
2475 /* Notify clients about changed exit status */
2476 unit_add_to_dbus_queue(u
);
2479 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2480 Service
*s
= SERVICE(u
);
2483 assert(elapsed
== 1);
2485 assert(w
== &s
->timer_watch
);
2489 case SERVICE_START_PRE
:
2491 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2492 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2495 case SERVICE_START_POST
:
2496 case SERVICE_RELOAD
:
2497 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2498 service_enter_stop(s
, false);
2502 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2503 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2506 case SERVICE_STOP_SIGTERM
:
2507 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2508 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2511 case SERVICE_STOP_SIGKILL
:
2512 /* Uh, wie sent a SIGKILL and it is still not gone?
2513 * Must be something we cannot kill, so let's just be
2514 * weirded out and continue */
2516 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2517 service_enter_stop_post(s
, false);
2520 case SERVICE_STOP_POST
:
2521 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2522 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2525 case SERVICE_FINAL_SIGTERM
:
2526 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2527 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2530 case SERVICE_FINAL_SIGKILL
:
2531 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->meta
.id
);
2532 service_enter_dead(s
, false, true);
2535 case SERVICE_AUTO_RESTART
:
2536 log_info("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2537 service_enter_restart(s
);
2541 assert_not_reached("Timeout at wrong time.");
2545 static void service_cgroup_notify_event(Unit
*u
) {
2546 Service
*s
= SERVICE(u
);
2550 log_debug("%s: cgroup is empty", u
->meta
.id
);
2554 /* Waiting for SIGCHLD is usually more interesting,
2555 * because it includes return codes/signals. Which is
2556 * why we ignore the cgroup events for most cases,
2557 * except when we don't know pid which to expect the
2560 case SERVICE_RUNNING
:
2561 service_enter_running(s
, true);
2564 case SERVICE_STOP_SIGTERM
:
2565 case SERVICE_STOP_SIGKILL
:
2566 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2567 service_enter_stop_post(s
, true);
2571 case SERVICE_FINAL_SIGTERM
:
2572 case SERVICE_FINAL_SIGKILL
:
2573 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2574 service_enter_dead(s
, true, true);
2583 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
) {
2584 Service
*s
= SERVICE(u
);
2589 if (s
->notify_access
== NOTIFY_NONE
) {
2590 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2591 u
->meta
.id
, (unsigned long) pid
);
2595 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2596 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2597 u
->meta
.id
, (unsigned long) pid
, (unsigned long) s
->main_pid
);
2601 log_debug("%s: Got message", u
->meta
.id
);
2603 /* Interpret MAINPID= */
2604 if ((e
= strv_find_prefix(tags
, "MAINPID=")) &&
2605 (s
->state
== SERVICE_START
||
2606 s
->state
== SERVICE_START_POST
||
2607 s
->state
== SERVICE_RUNNING
||
2608 s
->state
== SERVICE_RELOAD
)) {
2610 if (parse_pid(e
+ 8, &pid
) < 0)
2611 log_warning("Failed to parse notification message %s", e
);
2613 log_debug("%s: got %s", u
->meta
.id
, e
);
2614 service_set_main_pid(s
, pid
);
2618 /* Interpret READY= */
2619 if (s
->type
== SERVICE_NOTIFY
&&
2620 s
->state
== SERVICE_START
&&
2621 strv_find(tags
, "READY=1")) {
2622 log_debug("%s: got READY=1", u
->meta
.id
);
2624 service_enter_start_post(s
);
2627 /* Interpret STATUS= */
2628 if ((e
= strv_find_prefix(tags
, "STATUS="))) {
2632 if (!(t
= strdup(e
+7))) {
2633 log_error("Failed to allocate string.");
2637 log_debug("%s: got %s", u
->meta
.id
, e
);
2639 free(s
->status_text
);
2642 free(s
->status_text
);
2643 s
->status_text
= NULL
;
2648 /* Notify clients about changed status or main pid */
2649 unit_add_to_dbus_queue(u
);
2652 static int service_enumerate(Manager
*m
) {
2656 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2657 Set
*runlevel_services
[ELEMENTSOF(rcnd_table
)], *shutdown_services
= NULL
;
2664 zero(runlevel_services
);
2666 STRV_FOREACH(p
, m
->lookup_paths
.sysvrcnd_path
)
2667 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
2672 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
].path
) < 0) {
2680 if (!(d
= opendir(path
))) {
2681 if (errno
!= ENOENT
)
2682 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2687 while ((de
= readdir(d
))) {
2690 if (ignore_file(de
->d_name
))
2693 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2696 if (strlen(de
->d_name
) < 4)
2699 a
= undecchar(de
->d_name
[1]);
2700 b
= undecchar(de
->d_name
[2]);
2707 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
].path
, de
->d_name
) < 0) {
2712 if (access(fpath
, X_OK
) < 0) {
2714 if (errno
!= ENOENT
)
2715 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2721 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
2726 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, NULL
, &service
)) < 0) {
2727 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
2731 if (de
->d_name
[0] == 'S') {
2733 if (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
) {
2734 SERVICE(service
)->sysv_start_priority
=
2735 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority
);
2737 SERVICE(service
)->sysv_enabled
= true;
2740 if ((r
= set_ensure_allocated(&runlevel_services
[i
], trivial_hash_func
, trivial_compare_func
)) < 0)
2743 if ((r
= set_put(runlevel_services
[i
], service
)) < 0)
2746 } else if (de
->d_name
[0] == 'K' &&
2747 (rcnd_table
[i
].type
== RUNLEVEL_DOWN
||
2748 rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
)) {
2750 if ((r
= set_ensure_allocated(&shutdown_services
, trivial_hash_func
, trivial_compare_func
)) < 0)
2753 if ((r
= set_put(shutdown_services
, service
)) < 0)
2759 /* Now we loaded all stubs and are aware of the lowest
2760 start-up priority for all services, not let's actually load
2761 the services, this will also tell us which services are
2762 actually native now */
2763 manager_dispatch_load_queue(m
);
2765 /* If this is a native service, rely on native ways to pull in
2766 * a service, don't pull it in via sysv rcN.d links. */
2767 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
2768 SET_FOREACH(service
, runlevel_services
[i
], j
) {
2769 service
= unit_follow_merge(service
);
2771 if (service
->meta
.fragment_path
)
2774 if ((r
= unit_add_two_dependencies_by_name_inverse(service
, UNIT_AFTER
, UNIT_WANTS
, rcnd_table
[i
].target
, NULL
, true)) < 0)
2778 /* We honour K links only for halt/reboot. For the normal
2779 * runlevels we assume the stop jobs will be implicitly added
2780 * by the core logic. Also, we don't really distuingish here
2781 * between the runlevels 0 and 6 and just add them to the
2782 * special shutdown target. On SUSE the boot.d/ runlevel is
2783 * also used for shutdown, so we add links for that too to the
2784 * shutdown target.*/
2785 SET_FOREACH(service
, shutdown_services
, j
) {
2786 service
= unit_follow_merge(service
);
2788 if (service
->meta
.fragment_path
)
2791 if ((r
= unit_add_two_dependencies_by_name_inverse(service
, UNIT_AFTER
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true)) < 0)
2802 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
2803 set_free(runlevel_services
[i
]);
2804 set_free(shutdown_services
);
2812 static void service_bus_name_owner_change(
2815 const char *old_owner
,
2816 const char *new_owner
) {
2818 Service
*s
= SERVICE(u
);
2823 assert(streq(s
->bus_name
, name
));
2824 assert(old_owner
|| new_owner
);
2826 if (old_owner
&& new_owner
)
2827 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
2829 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
2831 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
2833 s
->bus_name_good
= !!new_owner
;
2835 if (s
->type
== SERVICE_DBUS
) {
2837 /* service_enter_running() will figure out what to
2839 if (s
->state
== SERVICE_RUNNING
)
2840 service_enter_running(s
, true);
2841 else if (s
->state
== SERVICE_START
&& new_owner
)
2842 service_enter_start_post(s
);
2844 } else if (new_owner
&&
2846 (s
->state
== SERVICE_START
||
2847 s
->state
== SERVICE_START_POST
||
2848 s
->state
== SERVICE_RUNNING
||
2849 s
->state
== SERVICE_RELOAD
)) {
2851 /* Try to acquire PID from bus service */
2852 log_debug("Trying to acquire PID from D-Bus name...");
2854 bus_query_pid(u
->meta
.manager
, name
);
2858 static void service_bus_query_pid_done(
2863 Service
*s
= SERVICE(u
);
2868 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
2870 if (s
->main_pid
<= 0 &&
2871 (s
->state
== SERVICE_START
||
2872 s
->state
== SERVICE_START_POST
||
2873 s
->state
== SERVICE_RUNNING
||
2874 s
->state
== SERVICE_RELOAD
))
2875 service_set_main_pid(s
, pid
);
2878 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
) {
2882 /* This is called by the socket code when instantiating a new
2883 * service for a stream socket and the socket needs to be
2886 if (s
->meta
.load_state
!= UNIT_LOADED
)
2889 if (s
->socket_fd
>= 0)
2892 if (s
->state
!= SERVICE_DEAD
)
2896 s
->got_socket_fd
= true;
2902 static void service_reset_failed(Unit
*u
) {
2903 Service
*s
= SERVICE(u
);
2907 if (s
->state
== SERVICE_FAILED
)
2908 service_set_state(s
, SERVICE_DEAD
);
2913 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2914 [SERVICE_DEAD
] = "dead",
2915 [SERVICE_START_PRE
] = "start-pre",
2916 [SERVICE_START
] = "start",
2917 [SERVICE_START_POST
] = "start-post",
2918 [SERVICE_RUNNING
] = "running",
2919 [SERVICE_EXITED
] = "exited",
2920 [SERVICE_RELOAD
] = "reload",
2921 [SERVICE_STOP
] = "stop",
2922 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
2923 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
2924 [SERVICE_STOP_POST
] = "stop-post",
2925 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
2926 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
2927 [SERVICE_FAILED
] = "failed",
2928 [SERVICE_AUTO_RESTART
] = "auto-restart",
2931 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
2933 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
2934 [SERVICE_ONCE
] = "once",
2935 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
2936 [SERVICE_RESTART_ALWAYS
] = "restart-always",
2939 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
2941 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
2942 [SERVICE_SIMPLE
] = "simple",
2943 [SERVICE_FORKING
] = "forking",
2944 [SERVICE_ONESHOT
] = "oneshot",
2945 [SERVICE_DBUS
] = "dbus",
2946 [SERVICE_NOTIFY
] = "notify"
2949 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
2951 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
2952 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
2953 [SERVICE_EXEC_START
] = "ExecStart",
2954 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
2955 [SERVICE_EXEC_RELOAD
] = "ExecReload",
2956 [SERVICE_EXEC_STOP
] = "ExecStop",
2957 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
2960 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
2962 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
2963 [NOTIFY_NONE
] = "none",
2964 [NOTIFY_MAIN
] = "main",
2965 [NOTIFY_ALL
] = "all"
2968 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
2970 const UnitVTable service_vtable
= {
2971 .suffix
= ".service",
2972 .show_status
= true,
2974 .init
= service_init
,
2975 .done
= service_done
,
2976 .load
= service_load
,
2978 .coldplug
= service_coldplug
,
2980 .dump
= service_dump
,
2982 .start
= service_start
,
2983 .stop
= service_stop
,
2984 .reload
= service_reload
,
2986 .can_reload
= service_can_reload
,
2988 .serialize
= service_serialize
,
2989 .deserialize_item
= service_deserialize_item
,
2991 .active_state
= service_active_state
,
2992 .sub_state_to_string
= service_sub_state_to_string
,
2994 .check_gc
= service_check_gc
,
2995 .check_snapshot
= service_check_snapshot
,
2997 .sigchld_event
= service_sigchld_event
,
2998 .timer_event
= service_timer_event
,
3000 .reset_failed
= service_reset_failed
,
3002 .cgroup_notify_empty
= service_cgroup_notify_event
,
3003 .notify_message
= service_notify_message
,
3005 .bus_name_owner_change
= service_bus_name_owner_change
,
3006 .bus_query_pid_done
= service_bus_query_pid_done
,
3008 .bus_interface
= "org.freedesktop.systemd1.Service",
3009 .bus_message_handler
= bus_service_message_handler
,
3010 .bus_invalidating_properties
= bus_service_invalidating_properties
,
3012 .enumerate
= service_enumerate