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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/reboot.h>
27 #include <linux/reboot.h>
28 #include <sys/syscall.h>
34 #include "load-fragment.h"
35 #include "load-dropin.h"
38 #include "unit-name.h"
39 #include "unit-printf.h"
40 #include "dbus-service.h"
42 #include "exit-status.h"
44 #include "path-util.h"
49 #include "bus-error.h"
52 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
53 [SERVICE_DEAD] = UNIT_INACTIVE,
54 [SERVICE_START_PRE] = UNIT_ACTIVATING,
55 [SERVICE_START] = UNIT_ACTIVATING,
56 [SERVICE_START_POST] = UNIT_ACTIVATING,
57 [SERVICE_RUNNING] = UNIT_ACTIVE,
58 [SERVICE_EXITED] = UNIT_ACTIVE,
59 [SERVICE_RELOAD] = UNIT_RELOADING,
60 [SERVICE_STOP] = UNIT_DEACTIVATING,
61 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
62 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
63 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
64 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
65 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
66 [SERVICE_FAILED] = UNIT_FAILED,
67 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
70 /* For Type=idle we never want to delay any other jobs, hence we
71 * consider idle jobs active as soon as we start working on them */
72 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
73 [SERVICE_DEAD] = UNIT_INACTIVE,
74 [SERVICE_START_PRE] = UNIT_ACTIVE,
75 [SERVICE_START] = UNIT_ACTIVE,
76 [SERVICE_START_POST] = UNIT_ACTIVE,
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_FAILED] = UNIT_FAILED,
87 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
90 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
91 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
92 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
94 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
96 static void service_init(Unit *u) {
97 Service *s = SERVICE(u);
100 assert(u->load_state == UNIT_STUB);
102 s->timeout_start_usec = u->manager->default_timeout_start_usec;
103 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
104 s->restart_usec = u->manager->default_restart_usec;
105 s->type = _SERVICE_TYPE_INVALID;
107 s->guess_main_pid = true;
109 RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
111 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
114 static void service_unwatch_control_pid(Service *s) {
117 if (s->control_pid <= 0)
120 unit_unwatch_pid(UNIT(s), s->control_pid);
124 static void service_unwatch_main_pid(Service *s) {
127 if (s->main_pid <= 0)
130 unit_unwatch_pid(UNIT(s), s->main_pid);
134 static void service_unwatch_pid_file(Service *s) {
135 if (!s->pid_file_pathspec)
138 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
139 UNIT(s)->id, s->pid_file_pathspec->path);
140 path_spec_unwatch(s->pid_file_pathspec);
141 path_spec_done(s->pid_file_pathspec);
142 free(s->pid_file_pathspec);
143 s->pid_file_pathspec = NULL;
146 static int service_set_main_pid(Service *s, pid_t pid) {
157 if (s->main_pid == pid && s->main_pid_known)
160 if (s->main_pid != pid) {
161 service_unwatch_main_pid(s);
162 exec_status_start(&s->main_exec_status, pid);
166 s->main_pid_known = true;
168 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
169 log_warning_unit(UNIT(s)->id,
170 "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.",
173 s->main_pid_alien = true;
175 s->main_pid_alien = false;
180 static void service_close_socket_fd(Service *s) {
183 if (s->socket_fd < 0)
186 s->socket_fd = asynchronous_close(s->socket_fd);
189 static void service_connection_unref(Service *s) {
192 if (!UNIT_ISSET(s->accept_socket))
195 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
196 unit_ref_unset(&s->accept_socket);
199 static void service_stop_watchdog(Service *s) {
202 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
203 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
206 static void service_start_watchdog(Service *s) {
211 if (s->watchdog_usec <= 0)
214 if (s->watchdog_event_source) {
215 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
217 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
221 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
223 r = sd_event_add_time(
224 UNIT(s)->manager->event,
225 &s->watchdog_event_source,
227 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
228 service_dispatch_watchdog, s);
230 log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
234 /* Let's process everything else which might be a sign
235 * of living before we consider a service died. */
236 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
240 log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
243 static void service_reset_watchdog(Service *s) {
246 dual_timestamp_get(&s->watchdog_timestamp);
247 service_start_watchdog(s);
250 static void service_done(Unit *u) {
251 Service *s = SERVICE(u);
258 free(s->status_text);
259 s->status_text = NULL;
262 s->reboot_arg = NULL;
264 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
265 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
266 s->control_command = NULL;
267 s->main_command = NULL;
269 exit_status_set_free(&s->restart_prevent_status);
270 exit_status_set_free(&s->restart_force_status);
271 exit_status_set_free(&s->success_status);
273 /* This will leak a process, but at least no memory or any of
275 service_unwatch_main_pid(s);
276 service_unwatch_control_pid(s);
277 service_unwatch_pid_file(s);
280 unit_unwatch_bus_name(u, s->bus_name);
285 service_close_socket_fd(s);
286 service_connection_unref(s);
288 unit_ref_unset(&s->accept_socket);
290 service_stop_watchdog(s);
292 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
295 static int service_arm_timer(Service *s, usec_t usec) {
300 if (s->timer_event_source) {
301 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
305 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
308 return sd_event_add_time(
309 UNIT(s)->manager->event,
310 &s->timer_event_source,
312 now(CLOCK_MONOTONIC) + usec, 0,
313 service_dispatch_timer, s);
316 static int service_verify(Service *s) {
319 if (UNIT(s)->load_state != UNIT_LOADED)
322 if (!s->exec_command[SERVICE_EXEC_START]) {
323 log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
327 if (s->type != SERVICE_ONESHOT &&
328 s->exec_command[SERVICE_EXEC_START]->command_next) {
329 log_error_unit(UNIT(s)->id, "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
333 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
334 log_error_unit(UNIT(s)->id, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
338 if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) {
339 log_error_unit(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
343 if (s->type == SERVICE_DBUS && !s->bus_name) {
344 log_error_unit(UNIT(s)->id, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
348 if (s->bus_name && s->type != SERVICE_DBUS)
349 log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
351 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
352 log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id);
360 static int service_add_default_extra_dependencies(Service *s) {
366 if (!(UNIT(s)->manager->dependencies))
369 /* Do NOT add dependencies for systemd unit files */
370 if (startswith(UNIT(s)->id, "systemd-")
371 || startswith(UNIT(s)->id, "serial-getty"))
374 t = unit_name_template(UNIT(s)->id);
378 ignore = hashmap_contains(UNIT(s)->manager->dep_ignore_list, t);
383 STRV_FOREACH(d, UNIT(s)->manager->dependencies) {
384 r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, *d, NULL, true);
393 static int service_add_default_dependencies(Service *s) {
398 /* Add a number of automatic dependencies useful for the
399 * majority of services. */
401 /* First, pull in base system */
402 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
403 SPECIAL_BASIC_TARGET, NULL, true);
408 r = service_add_default_extra_dependencies(s);
413 /* Second, activate normal shutdown */
414 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
415 SPECIAL_SHUTDOWN_TARGET, NULL, true);
419 static void service_fix_output(Service *s) {
422 /* If nothing has been explicitly configured, patch default
423 * output in. If input is socket/tty we avoid this however,
424 * since in that case we want output to default to the same
425 * place as we read input from. */
427 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
428 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
429 s->exec_context.std_input == EXEC_INPUT_NULL)
430 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
432 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
433 s->exec_context.std_input == EXEC_INPUT_NULL)
434 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
437 static int service_load(Unit *u) {
439 Service *s = SERVICE(u);
443 /* Load a .service file */
444 r = unit_load_fragment(u);
448 /* Still nothing found? Then let's give up */
449 if (u->load_state == UNIT_STUB)
452 /* This is a new unit? Then let's add in some extras */
453 if (u->load_state == UNIT_LOADED) {
455 /* We were able to load something, then let's add in
456 * the dropin directories. */
457 r = unit_load_dropin(u);
461 if (s->type == _SERVICE_TYPE_INVALID)
462 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
464 /* Oneshot services have disabled start timeout by default */
465 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
466 s->timeout_start_usec = 0;
468 service_fix_output(s);
470 r = unit_patch_contexts(u);
474 r = unit_add_exec_dependencies(u, &s->exec_context);
478 r = unit_add_default_slice(u, &s->cgroup_context);
482 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
483 s->notify_access = NOTIFY_MAIN;
485 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
486 s->notify_access = NOTIFY_MAIN;
489 r = unit_watch_bus_name(u, s->bus_name);
494 if (u->default_dependencies) {
495 r = service_add_default_dependencies(s);
502 return service_verify(s);
505 static void service_dump(Unit *u, FILE *f, const char *prefix) {
507 ServiceExecCommand c;
508 Service *s = SERVICE(u);
510 _cleanup_free_ char *p2 = NULL;
514 p2 = strappend(prefix, "\t");
515 prefix2 = p2 ? p2 : prefix;
518 "%sService State: %s\n"
520 "%sReload Result: %s\n"
521 "%sPermissionsStartOnly: %s\n"
522 "%sRootDirectoryStartOnly: %s\n"
523 "%sRemainAfterExit: %s\n"
524 "%sGuessMainPID: %s\n"
527 "%sNotifyAccess: %s\n",
528 prefix, service_state_to_string(s->state),
529 prefix, service_result_to_string(s->result),
530 prefix, service_result_to_string(s->reload_result),
531 prefix, yes_no(s->permissions_start_only),
532 prefix, yes_no(s->root_directory_start_only),
533 prefix, yes_no(s->remain_after_exit),
534 prefix, yes_no(s->guess_main_pid),
535 prefix, service_type_to_string(s->type),
536 prefix, service_restart_to_string(s->restart),
537 prefix, notify_access_to_string(s->notify_access));
539 if (s->control_pid > 0)
541 "%sControl PID: "PID_FMT"\n",
542 prefix, s->control_pid);
546 "%sMain PID: "PID_FMT"\n"
547 "%sMain PID Known: %s\n"
548 "%sMain PID Alien: %s\n",
550 prefix, yes_no(s->main_pid_known),
551 prefix, yes_no(s->main_pid_alien));
556 prefix, s->pid_file);
561 "%sBus Name Good: %s\n",
563 prefix, yes_no(s->bus_name_good));
565 kill_context_dump(&s->kill_context, f, prefix);
566 exec_context_dump(&s->exec_context, f, prefix);
568 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
570 if (!s->exec_command[c])
573 fprintf(f, "%s-> %s:\n",
574 prefix, service_exec_command_to_string(c));
576 exec_command_dump_list(s->exec_command[c], f, prefix2);
579 #ifdef HAVE_SYSV_COMPAT
580 if (s->sysv_start_priority >= 0)
582 "%sSysVStartPriority: %i\n",
583 prefix, s->sysv_start_priority);
587 fprintf(f, "%sStatus Text: %s\n",
588 prefix, s->status_text);
591 static int service_load_pid_file(Service *s, bool may_warn) {
592 _cleanup_free_ char *k = NULL;
601 r = read_one_line_file(s->pid_file, &k);
604 log_info_unit(UNIT(s)->id,
605 "PID file %s not readable (yet?) after %s.",
606 s->pid_file, service_state_to_string(s->state));
610 r = parse_pid(k, &pid);
613 log_info_unit(UNIT(s)->id,
614 "Failed to read PID from file %s: %s",
615 s->pid_file, strerror(-r));
619 if (!pid_is_alive(pid)) {
621 log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
626 if (s->main_pid_known) {
627 if (pid == s->main_pid)
630 log_debug_unit(UNIT(s)->id,
631 "Main PID changing: "PID_FMT" -> "PID_FMT,
633 service_unwatch_main_pid(s);
634 s->main_pid_known = false;
636 log_debug_unit(UNIT(s)->id,
637 "Main PID loaded: "PID_FMT, pid);
639 r = service_set_main_pid(s, pid);
643 r = unit_watch_pid(UNIT(s), pid);
645 /* FIXME: we need to do something here */
646 log_warning_unit(UNIT(s)->id,
647 "Failed to watch PID "PID_FMT" from service %s",
655 static int service_search_main_pid(Service *s) {
661 /* If we know it anyway, don't ever fallback to unreliable
663 if (s->main_pid_known)
666 if (!s->guess_main_pid)
669 assert(s->main_pid <= 0);
671 pid = unit_search_main_pid(UNIT(s));
675 log_debug_unit(UNIT(s)->id,
676 "Main PID guessed: "PID_FMT, pid);
677 r = service_set_main_pid(s, pid);
681 r = unit_watch_pid(UNIT(s), pid);
683 /* FIXME: we need to do something here */
684 log_warning_unit(UNIT(s)->id,
685 "Failed to watch PID "PID_FMT" from service %s",
690 static void service_set_state(Service *s, ServiceState state) {
691 ServiceState old_state;
692 const UnitActiveState *table;
696 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
698 old_state = s->state;
701 service_unwatch_pid_file(s);
704 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
706 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
708 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
709 SERVICE_AUTO_RESTART))
710 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
713 SERVICE_START, SERVICE_START_POST,
714 SERVICE_RUNNING, SERVICE_RELOAD,
715 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
717 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
718 service_unwatch_main_pid(s);
719 s->main_command = NULL;
723 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
725 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
727 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
728 service_unwatch_control_pid(s);
729 s->control_command = NULL;
730 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
733 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
734 unit_unwatch_all_pids(UNIT(s));
737 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
738 SERVICE_RUNNING, SERVICE_RELOAD,
739 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
740 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
741 !(state == SERVICE_DEAD && UNIT(s)->job)) {
742 service_close_socket_fd(s);
743 service_connection_unref(s);
746 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
747 service_stop_watchdog(s);
749 /* For the inactive states unit_notify() will trim the cgroup,
750 * but for exit we have to do that ourselves... */
751 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
752 unit_destroy_cgroup(UNIT(s));
754 /* For remain_after_exit services, let's see if we can "release" the
755 * hold on the console, since unit_notify() only does that in case of
757 if (state == SERVICE_EXITED && s->remain_after_exit &&
758 UNIT(s)->manager->n_on_console > 0) {
759 ExecContext *ec = unit_get_exec_context(UNIT(s));
760 if (ec && exec_context_may_touch_console(ec)) {
761 Manager *m = UNIT(s)->manager;
764 if (m->n_on_console == 0)
765 /* unset no_console_output flag, since the console is free */
766 m->no_console_output = false;
770 if (old_state != state)
771 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
773 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
774 s->reload_result = SERVICE_SUCCESS;
777 static int service_coldplug(Unit *u) {
778 Service *s = SERVICE(u);
782 assert(s->state == SERVICE_DEAD);
784 if (s->deserialized_state != s->state) {
786 if (IN_SET(s->deserialized_state,
787 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
789 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
791 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
795 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
797 /* For the start/stop timeouts 0 means off */
799 r = service_arm_timer(s, k);
805 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
807 /* The restart timeouts 0 means immediately */
808 r = service_arm_timer(s, s->restart_usec);
813 if (pid_is_unwaited(s->main_pid) &&
814 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
815 IN_SET(s->deserialized_state,
816 SERVICE_START, SERVICE_START_POST,
817 SERVICE_RUNNING, SERVICE_RELOAD,
818 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
820 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
821 r = unit_watch_pid(UNIT(s), s->main_pid);
826 if (pid_is_unwaited(s->control_pid) &&
827 IN_SET(s->deserialized_state,
828 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
830 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
832 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
833 r = unit_watch_pid(UNIT(s), s->control_pid);
838 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
839 unit_watch_all_pids(UNIT(s));
841 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
842 service_start_watchdog(s);
844 service_set_state(s, s->deserialized_state);
850 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
861 if (s->socket_fd >= 0)
864 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
869 if (u->type != UNIT_SOCKET)
874 r = socket_collect_fds(sock, &cfds, &cn_fds);
887 t = new(int, rn_fds+cn_fds);
894 memcpy(t, rfds, rn_fds * sizeof(int));
895 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
900 rn_fds = rn_fds+cn_fds;
915 static int service_spawn(
920 bool apply_permissions,
922 bool apply_tty_stdin,
923 bool set_notify_socket,
930 _cleanup_free_ int *fdsbuf = NULL;
931 unsigned n_fds = 0, n_env = 0;
932 _cleanup_strv_free_ char
933 **argv = NULL, **final_env = NULL, **our_env = NULL;
940 unit_realize_cgroup(UNIT(s));
942 r = unit_setup_exec_runtime(UNIT(s));
947 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
948 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
949 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
951 if (s->socket_fd >= 0) {
955 r = service_collect_fds(s, &fdsbuf, &n_fds);
963 if (timeout && s->timeout_start_usec > 0) {
964 r = service_arm_timer(s, s->timeout_start_usec);
968 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
970 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
974 our_env = new0(char*, 4);
980 if (set_notify_socket)
981 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
987 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
992 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
993 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
998 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1004 if (is_control && UNIT(s)->cgroup_path) {
1005 path = strappenda(UNIT(s)->cgroup_path, "/control");
1006 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1008 path = UNIT(s)->cgroup_path;
1018 UNIT(s)->manager->confirm_spawn,
1019 UNIT(s)->manager->cgroup_supported,
1021 manager_get_runtime_prefix(UNIT(s)->manager),
1024 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
1030 r = unit_watch_pid(UNIT(s), pid);
1032 /* FIXME: we need to do something here */
1041 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1046 static int main_pid_good(Service *s) {
1049 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1052 /* If we know the pid file, then lets just check if it is
1054 if (s->main_pid_known) {
1056 /* If it's an alien child let's check if it is still
1058 if (s->main_pid_alien && s->main_pid > 0)
1059 return pid_is_alive(s->main_pid);
1061 /* .. otherwise assume we'll get a SIGCHLD for it,
1062 * which we really should wait for to collect exit
1063 * status and code */
1064 return s->main_pid > 0;
1067 /* We don't know the pid */
1071 _pure_ static int control_pid_good(Service *s) {
1074 return s->control_pid > 0;
1077 static int cgroup_good(Service *s) {
1082 if (!UNIT(s)->cgroup_path)
1085 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1092 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
1094 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1098 if (f != SERVICE_SUCCESS)
1101 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1103 if (s->result != SERVICE_SUCCESS)
1104 service_execute_action(s, s->failure_action, "failed", false);
1106 if (allow_restart &&
1107 !s->forbid_restart &&
1108 (s->restart == SERVICE_RESTART_ALWAYS ||
1109 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1110 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1111 (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
1112 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1113 (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) ||
1114 (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) ||
1115 (IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) && set_contains(s->restart_force_status.signal, INT_TO_PTR(s->main_exec_status.status)))) &&
1116 (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) &&
1117 (!IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) || !set_contains(s->restart_prevent_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1119 r = service_arm_timer(s, s->restart_usec);
1123 service_set_state(s, SERVICE_AUTO_RESTART);
1126 s->forbid_restart = false;
1128 /* We want fresh tmpdirs in case service is started again immediately */
1129 exec_runtime_destroy(s->exec_runtime);
1130 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1132 /* Also, remove the runtime directory in */
1133 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1135 /* Try to delete the pid file. At this point it will be
1136 * out-of-date, and some software might be confused by it, so
1137 * let's remove it. */
1139 unlink_noerrno(s->pid_file);
1144 log_warning_unit(UNIT(s)->id,
1145 "%s failed to run install restart timer: %s",
1146 UNIT(s)->id, strerror(-r));
1147 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1150 static void service_enter_stop_post(Service *s, ServiceResult f) {
1154 if (f != SERVICE_SUCCESS)
1157 service_unwatch_control_pid(s);
1158 unit_watch_all_pids(UNIT(s));
1160 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1161 if (s->control_command) {
1162 s->control_command_id = SERVICE_EXEC_STOP_POST;
1164 r = service_spawn(s,
1168 !s->permissions_start_only,
1169 !s->root_directory_start_only,
1177 service_set_state(s, SERVICE_STOP_POST);
1179 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1184 log_warning_unit(UNIT(s)->id,
1185 "%s failed to run 'stop-post' task: %s",
1186 UNIT(s)->id, strerror(-r));
1187 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1190 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1195 if (f != SERVICE_SUCCESS)
1198 unit_watch_all_pids(UNIT(s));
1200 r = unit_kill_context(
1203 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1212 if (s->timeout_stop_usec > 0) {
1213 r = service_arm_timer(s, s->timeout_stop_usec);
1218 service_set_state(s, state);
1219 } else if (state == SERVICE_STOP_SIGTERM)
1220 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1221 else if (state == SERVICE_STOP_SIGKILL)
1222 service_enter_stop_post(s, SERVICE_SUCCESS);
1223 else if (state == SERVICE_FINAL_SIGTERM)
1224 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1226 service_enter_dead(s, SERVICE_SUCCESS, true);
1231 log_warning_unit(UNIT(s)->id,
1232 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1234 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1235 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1237 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1240 static void service_enter_stop(Service *s, ServiceResult f) {
1245 if (f != SERVICE_SUCCESS)
1248 service_unwatch_control_pid(s);
1249 unit_watch_all_pids(UNIT(s));
1251 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1252 if (s->control_command) {
1253 s->control_command_id = SERVICE_EXEC_STOP;
1255 r = service_spawn(s,
1259 !s->permissions_start_only,
1260 !s->root_directory_start_only,
1268 service_set_state(s, SERVICE_STOP);
1270 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1275 log_warning_unit(UNIT(s)->id,
1276 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
1277 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1280 static void service_enter_running(Service *s, ServiceResult f) {
1281 int main_pid_ok, cgroup_ok;
1284 if (f != SERVICE_SUCCESS)
1287 main_pid_ok = main_pid_good(s);
1288 cgroup_ok = cgroup_good(s);
1290 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1291 (s->bus_name_good || s->type != SERVICE_DBUS))
1292 service_set_state(s, SERVICE_RUNNING);
1293 else if (s->remain_after_exit)
1294 service_set_state(s, SERVICE_EXITED);
1296 service_enter_stop(s, SERVICE_SUCCESS);
1299 static void service_enter_start_post(Service *s) {
1303 service_unwatch_control_pid(s);
1304 service_reset_watchdog(s);
1306 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1307 if (s->control_command) {
1308 s->control_command_id = SERVICE_EXEC_START_POST;
1310 r = service_spawn(s,
1314 !s->permissions_start_only,
1315 !s->root_directory_start_only,
1323 service_set_state(s, SERVICE_START_POST);
1325 service_enter_running(s, SERVICE_SUCCESS);
1330 log_warning_unit(UNIT(s)->id,
1331 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
1332 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1335 static void service_kill_control_processes(Service *s) {
1338 if (!UNIT(s)->cgroup_path)
1341 p = strappenda(UNIT(s)->cgroup_path, "/control");
1342 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1345 static void service_enter_start(Service *s) {
1352 assert(s->exec_command[SERVICE_EXEC_START]);
1353 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1355 service_unwatch_control_pid(s);
1356 service_unwatch_main_pid(s);
1358 /* We want to ensure that nobody leaks processes from
1359 * START_PRE here, so let's go on a killing spree, People
1360 * should not spawn long running processes from START_PRE. */
1361 service_kill_control_processes(s);
1363 if (s->type == SERVICE_FORKING) {
1364 s->control_command_id = SERVICE_EXEC_START;
1365 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1367 s->main_command = NULL;
1369 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1370 s->control_command = NULL;
1372 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1375 r = service_spawn(s,
1377 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
1378 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
1383 s->notify_access != NOTIFY_NONE,
1389 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
1390 /* For simple services we immediately start
1391 * the START_POST binaries. */
1393 service_set_main_pid(s, pid);
1394 service_enter_start_post(s);
1396 } else if (s->type == SERVICE_FORKING) {
1398 /* For forking services we wait until the start
1399 * process exited. */
1401 s->control_pid = pid;
1402 service_set_state(s, SERVICE_START);
1404 } else if (s->type == SERVICE_ONESHOT ||
1405 s->type == SERVICE_DBUS ||
1406 s->type == SERVICE_NOTIFY) {
1408 /* For oneshot services we wait until the start
1409 * process exited, too, but it is our main process. */
1411 /* For D-Bus services we know the main pid right away,
1412 * but wait for the bus name to appear on the
1413 * bus. Notify services are similar. */
1415 service_set_main_pid(s, pid);
1416 service_set_state(s, SERVICE_START);
1418 assert_not_reached("Unknown service type");
1423 log_warning_unit(UNIT(s)->id,
1424 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
1425 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1428 static void service_enter_start_pre(Service *s) {
1433 service_unwatch_control_pid(s);
1435 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1436 if (s->control_command) {
1437 /* Before we start anything, let's clear up what might
1438 * be left from previous runs. */
1439 service_kill_control_processes(s);
1441 s->control_command_id = SERVICE_EXEC_START_PRE;
1443 r = service_spawn(s,
1447 !s->permissions_start_only,
1448 !s->root_directory_start_only,
1456 service_set_state(s, SERVICE_START_PRE);
1458 service_enter_start(s);
1463 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
1464 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1467 static void service_enter_restart(Service *s) {
1468 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1473 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1474 /* Don't restart things if we are going down anyway */
1475 log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart.");
1477 r = service_arm_timer(s, s->restart_usec);
1484 /* Any units that are bound to this service must also be
1485 * restarted. We use JOB_RESTART (instead of the more obvious
1486 * JOB_START) here so that those dependency jobs will be added
1488 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1492 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1493 * it will be canceled as part of the service_stop() call that
1494 * is executed as part of JOB_RESTART. */
1496 log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id);
1500 log_warning_unit(UNIT(s)->id,
1501 "%s failed to schedule restart job: %s",
1502 UNIT(s)->id, bus_error_message(&error, -r));
1503 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1506 static void service_enter_reload(Service *s) {
1511 service_unwatch_control_pid(s);
1513 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1514 if (s->control_command) {
1515 s->control_command_id = SERVICE_EXEC_RELOAD;
1517 r = service_spawn(s,
1521 !s->permissions_start_only,
1522 !s->root_directory_start_only,
1530 service_set_state(s, SERVICE_RELOAD);
1532 service_enter_running(s, SERVICE_SUCCESS);
1537 log_warning_unit(UNIT(s)->id,
1538 "%s failed to run 'reload' task: %s",
1539 UNIT(s)->id, strerror(-r));
1540 s->reload_result = SERVICE_FAILURE_RESOURCES;
1541 service_enter_running(s, SERVICE_SUCCESS);
1544 static void service_run_next_control(Service *s) {
1548 assert(s->control_command);
1549 assert(s->control_command->command_next);
1551 assert(s->control_command_id != SERVICE_EXEC_START);
1553 s->control_command = s->control_command->command_next;
1554 service_unwatch_control_pid(s);
1556 r = service_spawn(s,
1560 !s->permissions_start_only,
1561 !s->root_directory_start_only,
1562 s->control_command_id == SERVICE_EXEC_START_PRE ||
1563 s->control_command_id == SERVICE_EXEC_STOP_POST,
1573 log_warning_unit(UNIT(s)->id,
1574 "%s failed to run next control task: %s",
1575 UNIT(s)->id, strerror(-r));
1577 if (s->state == SERVICE_START_PRE)
1578 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1579 else if (s->state == SERVICE_STOP)
1580 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1581 else if (s->state == SERVICE_STOP_POST)
1582 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1583 else if (s->state == SERVICE_RELOAD) {
1584 s->reload_result = SERVICE_FAILURE_RESOURCES;
1585 service_enter_running(s, SERVICE_SUCCESS);
1587 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1590 static void service_run_next_main(Service *s) {
1595 assert(s->main_command);
1596 assert(s->main_command->command_next);
1597 assert(s->type == SERVICE_ONESHOT);
1599 s->main_command = s->main_command->command_next;
1600 service_unwatch_main_pid(s);
1602 r = service_spawn(s,
1609 s->notify_access != NOTIFY_NONE,
1615 service_set_main_pid(s, pid);
1620 log_warning_unit(UNIT(s)->id,
1621 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
1622 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1625 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
1628 if (action == SERVICE_FAILURE_ACTION_REBOOT ||
1629 action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
1630 update_reboot_param_file(s->reboot_arg);
1634 case SERVICE_FAILURE_ACTION_NONE:
1635 if (log_action_none)
1636 log_warning_unit(UNIT(s)->id,
1637 "%s %s, refusing to start.", UNIT(s)->id, reason);
1640 case SERVICE_FAILURE_ACTION_REBOOT: {
1641 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1644 log_warning_unit(UNIT(s)->id,
1645 "%s %s, rebooting.", UNIT(s)->id, reason);
1647 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
1648 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
1649 true, &error, NULL);
1651 log_error_unit(UNIT(s)->id,
1652 "Failed to reboot: %s.", bus_error_message(&error, r));
1657 case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
1658 log_warning_unit(UNIT(s)->id,
1659 "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
1660 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
1663 case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
1664 log_warning_unit(UNIT(s)->id,
1665 "%s %s, rebooting immediately.", UNIT(s)->id, reason);
1667 if (s->reboot_arg) {
1668 log_info("Rebooting with argument '%s'.", s->reboot_arg);
1669 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
1670 LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
1673 log_info("Rebooting.");
1674 reboot(RB_AUTOBOOT);
1678 log_error_unit(UNIT(s)->id,
1679 "failure action=%i", action);
1680 assert_not_reached("Unknown FailureAction.");
1686 static int service_start_limit_test(Service *s) {
1689 if (ratelimit_test(&s->start_limit))
1692 return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
1695 static int service_start(Unit *u) {
1696 Service *s = SERVICE(u);
1701 /* We cannot fulfill this request right now, try again later
1703 if (s->state == SERVICE_STOP ||
1704 s->state == SERVICE_STOP_SIGTERM ||
1705 s->state == SERVICE_STOP_SIGKILL ||
1706 s->state == SERVICE_STOP_POST ||
1707 s->state == SERVICE_FINAL_SIGTERM ||
1708 s->state == SERVICE_FINAL_SIGKILL)
1711 /* Already on it! */
1712 if (s->state == SERVICE_START_PRE ||
1713 s->state == SERVICE_START ||
1714 s->state == SERVICE_START_POST)
1717 /* A service that will be restarted must be stopped first to
1718 * trigger BindsTo and/or OnFailure dependencies. If a user
1719 * does not want to wait for the holdoff time to elapse, the
1720 * service should be manually restarted, not started. We
1721 * simply return EAGAIN here, so that any start jobs stay
1722 * queued, and assume that the auto restart timer will
1723 * eventually trigger the restart. */
1724 if (s->state == SERVICE_AUTO_RESTART)
1727 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
1729 /* Make sure we don't enter a busy loop of some kind. */
1730 r = service_start_limit_test(s);
1732 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
1736 s->result = SERVICE_SUCCESS;
1737 s->reload_result = SERVICE_SUCCESS;
1738 s->main_pid_known = false;
1739 s->main_pid_alien = false;
1740 s->forbid_restart = false;
1742 free(s->status_text);
1743 s->status_text = NULL;
1744 s->status_errno = 0;
1746 service_enter_start_pre(s);
1750 static int service_stop(Unit *u) {
1751 Service *s = SERVICE(u);
1755 /* Don't create restart jobs from here. */
1756 s->forbid_restart = true;
1759 if (s->state == SERVICE_STOP ||
1760 s->state == SERVICE_STOP_SIGTERM ||
1761 s->state == SERVICE_STOP_SIGKILL ||
1762 s->state == SERVICE_STOP_POST ||
1763 s->state == SERVICE_FINAL_SIGTERM ||
1764 s->state == SERVICE_FINAL_SIGKILL)
1767 /* A restart will be scheduled or is in progress. */
1768 if (s->state == SERVICE_AUTO_RESTART) {
1769 service_set_state(s, SERVICE_DEAD);
1773 /* If there's already something running we go directly into
1775 if (s->state == SERVICE_START_PRE ||
1776 s->state == SERVICE_START ||
1777 s->state == SERVICE_START_POST ||
1778 s->state == SERVICE_RELOAD) {
1779 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1783 assert(s->state == SERVICE_RUNNING ||
1784 s->state == SERVICE_EXITED);
1786 service_enter_stop(s, SERVICE_SUCCESS);
1790 static int service_reload(Unit *u) {
1791 Service *s = SERVICE(u);
1795 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1797 service_enter_reload(s);
1801 _pure_ static bool service_can_reload(Unit *u) {
1802 Service *s = SERVICE(u);
1806 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1809 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1810 Service *s = SERVICE(u);
1816 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1817 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1818 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
1820 if (s->control_pid > 0)
1821 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1824 if (s->main_pid_known && s->main_pid > 0)
1825 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
1827 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1830 unit_serialize_item(u, f, "status-text", s->status_text);
1832 /* FIXME: There's a minor uncleanliness here: if there are
1833 * multiple commands attached here, we will start from the
1834 * first one again */
1835 if (s->control_command_id >= 0)
1836 unit_serialize_item(u, f, "control-command",
1837 service_exec_command_to_string(s->control_command_id));
1839 if (s->socket_fd >= 0) {
1842 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1845 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1848 if (s->main_exec_status.pid > 0) {
1849 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1850 s->main_exec_status.pid);
1851 dual_timestamp_serialize(f, "main-exec-status-start",
1852 &s->main_exec_status.start_timestamp);
1853 dual_timestamp_serialize(f, "main-exec-status-exit",
1854 &s->main_exec_status.exit_timestamp);
1856 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
1857 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1858 s->main_exec_status.code);
1859 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1860 s->main_exec_status.status);
1863 if (dual_timestamp_is_set(&s->watchdog_timestamp))
1864 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
1866 if (s->forbid_restart)
1867 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
1872 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1873 Service *s = SERVICE(u);
1880 if (streq(key, "state")) {
1883 state = service_state_from_string(value);
1885 log_debug_unit(u->id, "Failed to parse state value %s", value);
1887 s->deserialized_state = state;
1888 } else if (streq(key, "result")) {
1891 f = service_result_from_string(value);
1893 log_debug_unit(u->id, "Failed to parse result value %s", value);
1894 else if (f != SERVICE_SUCCESS)
1897 } else if (streq(key, "reload-result")) {
1900 f = service_result_from_string(value);
1902 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
1903 else if (f != SERVICE_SUCCESS)
1904 s->reload_result = f;
1906 } else if (streq(key, "control-pid")) {
1909 if (parse_pid(value, &pid) < 0)
1910 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
1912 s->control_pid = pid;
1913 } else if (streq(key, "main-pid")) {
1916 if (parse_pid(value, &pid) < 0)
1917 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
1919 service_set_main_pid(s, pid);
1920 unit_watch_pid(UNIT(s), pid);
1922 } else if (streq(key, "main-pid-known")) {
1925 b = parse_boolean(value);
1927 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
1929 s->main_pid_known = b;
1930 } else if (streq(key, "status-text")) {
1937 free(s->status_text);
1941 } else if (streq(key, "control-command")) {
1942 ServiceExecCommand id;
1944 id = service_exec_command_from_string(value);
1946 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
1948 s->control_command_id = id;
1949 s->control_command = s->exec_command[id];
1951 } else if (streq(key, "socket-fd")) {
1954 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1955 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
1958 asynchronous_close(s->socket_fd);
1959 s->socket_fd = fdset_remove(fds, fd);
1961 } else if (streq(key, "main-exec-status-pid")) {
1964 if (parse_pid(value, &pid) < 0)
1965 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
1967 s->main_exec_status.pid = pid;
1968 } else if (streq(key, "main-exec-status-code")) {
1971 if (safe_atoi(value, &i) < 0)
1972 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
1974 s->main_exec_status.code = i;
1975 } else if (streq(key, "main-exec-status-status")) {
1978 if (safe_atoi(value, &i) < 0)
1979 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
1981 s->main_exec_status.status = i;
1982 } else if (streq(key, "main-exec-status-start"))
1983 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
1984 else if (streq(key, "main-exec-status-exit"))
1985 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
1986 else if (streq(key, "watchdog-timestamp"))
1987 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
1988 else if (streq(key, "forbid-restart")) {
1991 b = parse_boolean(value);
1993 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
1995 s->forbid_restart = b;
1997 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2002 _pure_ static UnitActiveState service_active_state(Unit *u) {
2003 const UnitActiveState *table;
2007 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2009 return table[SERVICE(u)->state];
2012 static const char *service_sub_state_to_string(Unit *u) {
2015 return service_state_to_string(SERVICE(u)->state);
2018 static bool service_check_gc(Unit *u) {
2019 Service *s = SERVICE(u);
2023 /* Never clean up services that still have a process around,
2024 * even if the service is formally dead. */
2025 if (cgroup_good(s) > 0 ||
2026 main_pid_good(s) > 0 ||
2027 control_pid_good(s) > 0)
2033 _pure_ static bool service_check_snapshot(Unit *u) {
2034 Service *s = SERVICE(u);
2038 return (s->socket_fd < 0);
2041 static int service_retry_pid_file(Service *s) {
2044 assert(s->pid_file);
2045 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2047 r = service_load_pid_file(s, false);
2051 service_unwatch_pid_file(s);
2053 service_enter_running(s, SERVICE_SUCCESS);
2057 static int service_watch_pid_file(Service *s) {
2060 log_debug_unit(UNIT(s)->id,
2061 "Setting watch for %s's PID file %s",
2062 UNIT(s)->id, s->pid_file_pathspec->path);
2063 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2067 /* the pidfile might have appeared just before we set the watch */
2068 log_debug_unit(UNIT(s)->id,
2069 "Trying to read %s's PID file %s in case it changed",
2070 UNIT(s)->id, s->pid_file_pathspec->path);
2071 service_retry_pid_file(s);
2075 log_error_unit(UNIT(s)->id,
2076 "Failed to set a watch for %s's PID file %s: %s",
2077 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2078 service_unwatch_pid_file(s);
2082 static int service_demand_pid_file(Service *s) {
2085 assert(s->pid_file);
2086 assert(!s->pid_file_pathspec);
2088 ps = new0(PathSpec, 1);
2093 ps->path = strdup(s->pid_file);
2099 path_kill_slashes(ps->path);
2101 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2102 * keep their PID file open all the time. */
2103 ps->type = PATH_MODIFIED;
2104 ps->inotify_fd = -1;
2106 s->pid_file_pathspec = ps;
2108 return service_watch_pid_file(s);
2111 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2112 PathSpec *p = userdata;
2117 s = SERVICE(p->unit);
2121 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2122 assert(s->pid_file_pathspec);
2123 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2125 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2127 if (path_spec_fd_event(p, events) < 0)
2130 if (service_retry_pid_file(s) == 0)
2133 if (service_watch_pid_file(s) < 0)
2139 service_unwatch_pid_file(s);
2140 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2144 static void service_notify_cgroup_empty_event(Unit *u) {
2145 Service *s = SERVICE(u);
2149 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2153 /* Waiting for SIGCHLD is usually more interesting,
2154 * because it includes return codes/signals. Which is
2155 * why we ignore the cgroup events for most cases,
2156 * except when we don't know pid which to expect the
2160 case SERVICE_START_POST:
2161 /* If we were hoping for the daemon to write its PID file,
2162 * we can give up now. */
2163 if (s->pid_file_pathspec) {
2164 log_warning_unit(u->id,
2165 "%s never wrote its PID file. Failing.", UNIT(s)->id);
2166 service_unwatch_pid_file(s);
2167 if (s->state == SERVICE_START)
2168 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2170 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2174 case SERVICE_RUNNING:
2175 /* service_enter_running() will figure out what to do */
2176 service_enter_running(s, SERVICE_SUCCESS);
2179 case SERVICE_STOP_SIGTERM:
2180 case SERVICE_STOP_SIGKILL:
2182 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2183 service_enter_stop_post(s, SERVICE_SUCCESS);
2187 case SERVICE_STOP_POST:
2188 case SERVICE_FINAL_SIGTERM:
2189 case SERVICE_FINAL_SIGKILL:
2190 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2191 service_enter_dead(s, SERVICE_SUCCESS, true);
2200 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2201 Service *s = SERVICE(u);
2207 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2208 is_clean_exit_lsb(code, status, &s->success_status))
2209 f = SERVICE_SUCCESS;
2210 else if (code == CLD_EXITED)
2211 f = SERVICE_FAILURE_EXIT_CODE;
2212 else if (code == CLD_KILLED)
2213 f = SERVICE_FAILURE_SIGNAL;
2214 else if (code == CLD_DUMPED)
2215 f = SERVICE_FAILURE_CORE_DUMP;
2217 assert_not_reached("Unknown code");
2219 if (s->main_pid == pid) {
2220 /* Forking services may occasionally move to a new PID.
2221 * As long as they update the PID file before exiting the old
2222 * PID, they're fine. */
2223 if (service_load_pid_file(s, false) == 0)
2227 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2229 if (s->main_command) {
2230 /* If this is not a forking service than the
2231 * main process got started and hence we copy
2232 * the exit status so that it is recorded both
2233 * as main and as control process exit
2236 s->main_command->exec_status = s->main_exec_status;
2238 if (s->main_command->ignore)
2239 f = SERVICE_SUCCESS;
2240 } else if (s->exec_command[SERVICE_EXEC_START]) {
2242 /* If this is a forked process, then we should
2243 * ignore the return value if this was
2244 * configured for the starter process */
2246 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2247 f = SERVICE_SUCCESS;
2250 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2252 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2253 u->id, sigchld_code_to_string(code), status,
2254 strna(code == CLD_EXITED
2255 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2256 : signal_to_string(status)),
2257 "EXIT_CODE=%s", sigchld_code_to_string(code),
2258 "EXIT_STATUS=%i", status,
2261 if (f != SERVICE_SUCCESS)
2264 if (s->main_command &&
2265 s->main_command->command_next &&
2266 f == SERVICE_SUCCESS) {
2268 /* There is another command to *
2269 * execute, so let's do that. */
2271 log_debug_unit(u->id,
2272 "%s running next main command for state %s",
2273 u->id, service_state_to_string(s->state));
2274 service_run_next_main(s);
2278 /* The service exited, so the service is officially
2280 s->main_command = NULL;
2284 case SERVICE_START_POST:
2285 case SERVICE_RELOAD:
2287 /* Need to wait until the operation is
2292 if (s->type == SERVICE_ONESHOT) {
2293 /* This was our main goal, so let's go on */
2294 if (f == SERVICE_SUCCESS)
2295 service_enter_start_post(s);
2297 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2303 case SERVICE_RUNNING:
2304 service_enter_running(s, f);
2307 case SERVICE_STOP_SIGTERM:
2308 case SERVICE_STOP_SIGKILL:
2310 if (!control_pid_good(s))
2311 service_enter_stop_post(s, f);
2313 /* If there is still a control process, wait for that first */
2316 case SERVICE_STOP_POST:
2317 case SERVICE_FINAL_SIGTERM:
2318 case SERVICE_FINAL_SIGKILL:
2320 if (!control_pid_good(s))
2321 service_enter_dead(s, f, true);
2325 assert_not_reached("Uh, main process died at wrong time.");
2329 } else if (s->control_pid == pid) {
2332 if (s->control_command) {
2333 exec_status_exit(&s->control_command->exec_status,
2334 &s->exec_context, pid, code, status);
2336 if (s->control_command->ignore)
2337 f = SERVICE_SUCCESS;
2340 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2341 "%s: control process exited, code=%s status=%i",
2342 u->id, sigchld_code_to_string(code), status);
2344 if (f != SERVICE_SUCCESS)
2347 /* Immediately get rid of the cgroup, so that the
2348 * kernel doesn't delay the cgroup empty messages for
2349 * the service cgroup any longer than necessary */
2350 service_kill_control_processes(s);
2352 if (s->control_command &&
2353 s->control_command->command_next &&
2354 f == SERVICE_SUCCESS) {
2356 /* There is another command to *
2357 * execute, so let's do that. */
2359 log_debug_unit(u->id,
2360 "%s running next control command for state %s",
2361 u->id, service_state_to_string(s->state));
2362 service_run_next_control(s);
2365 /* No further commands for this step, so let's
2366 * figure out what to do next */
2368 s->control_command = NULL;
2369 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2371 log_debug_unit(u->id,
2372 "%s got final SIGCHLD for state %s",
2373 u->id, service_state_to_string(s->state));
2377 case SERVICE_START_PRE:
2378 if (f == SERVICE_SUCCESS)
2379 service_enter_start(s);
2381 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2385 if (s->type != SERVICE_FORKING)
2386 /* Maybe spurious event due to a reload that changed the type? */
2389 if (f != SERVICE_SUCCESS) {
2390 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2395 bool has_start_post;
2398 /* Let's try to load the pid file here if we can.
2399 * The PID file might actually be created by a START_POST
2400 * script. In that case don't worry if the loading fails. */
2402 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2403 r = service_load_pid_file(s, !has_start_post);
2404 if (!has_start_post && r < 0) {
2405 r = service_demand_pid_file(s);
2406 if (r < 0 || !cgroup_good(s))
2407 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2411 service_search_main_pid(s);
2413 service_enter_start_post(s);
2416 case SERVICE_START_POST:
2417 if (f != SERVICE_SUCCESS) {
2418 service_enter_stop(s, f);
2425 r = service_load_pid_file(s, true);
2427 r = service_demand_pid_file(s);
2428 if (r < 0 || !cgroup_good(s))
2429 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2433 service_search_main_pid(s);
2435 service_enter_running(s, SERVICE_SUCCESS);
2438 case SERVICE_RELOAD:
2439 if (f == SERVICE_SUCCESS) {
2440 service_load_pid_file(s, true);
2441 service_search_main_pid(s);
2444 s->reload_result = f;
2445 service_enter_running(s, SERVICE_SUCCESS);
2449 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2452 case SERVICE_STOP_SIGTERM:
2453 case SERVICE_STOP_SIGKILL:
2454 if (main_pid_good(s) <= 0)
2455 service_enter_stop_post(s, f);
2457 /* If there is still a service
2458 * process around, wait until
2459 * that one quit, too */
2462 case SERVICE_STOP_POST:
2463 case SERVICE_FINAL_SIGTERM:
2464 case SERVICE_FINAL_SIGKILL:
2465 if (main_pid_good(s) <= 0)
2466 service_enter_dead(s, f, 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);
2478 /* We got one SIGCHLD for the service, let's watch all
2479 * processes that are now running of the service, and watch
2480 * that. Among the PIDs we then watch will be children
2481 * reassigned to us, which hopefully allows us to identify
2482 * when all children are gone */
2483 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2484 unit_watch_all_pids(u);
2486 /* If the PID set is empty now, then let's finish this off */
2487 if (set_isempty(u->pids))
2488 service_notify_cgroup_empty_event(u);
2491 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2492 Service *s = SERVICE(userdata);
2495 assert(source == s->timer_event_source);
2499 case SERVICE_START_PRE:
2501 log_warning_unit(UNIT(s)->id,
2502 "%s %s operation timed out. Terminating.",
2504 s->state == SERVICE_START ? "start" : "start-pre");
2505 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2508 case SERVICE_START_POST:
2509 log_warning_unit(UNIT(s)->id,
2510 "%s start-post operation timed out. Stopping.", UNIT(s)->id);
2511 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2514 case SERVICE_RELOAD:
2515 log_warning_unit(UNIT(s)->id,
2516 "%s reload operation timed out. Stopping.", UNIT(s)->id);
2517 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2518 service_enter_running(s, SERVICE_SUCCESS);
2522 log_warning_unit(UNIT(s)->id,
2523 "%s stopping timed out. Terminating.", UNIT(s)->id);
2524 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2527 case SERVICE_STOP_SIGTERM:
2528 if (s->kill_context.send_sigkill) {
2529 log_warning_unit(UNIT(s)->id,
2530 "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
2531 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2533 log_warning_unit(UNIT(s)->id,
2534 "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
2535 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2540 case SERVICE_STOP_SIGKILL:
2541 /* Uh, we sent a SIGKILL and it is still not gone?
2542 * Must be something we cannot kill, so let's just be
2543 * weirded out and continue */
2545 log_warning_unit(UNIT(s)->id,
2546 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
2547 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2550 case SERVICE_STOP_POST:
2551 log_warning_unit(UNIT(s)->id,
2552 "%s stop-post timed out. Terminating.", UNIT(s)->id);
2553 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2556 case SERVICE_FINAL_SIGTERM:
2557 if (s->kill_context.send_sigkill) {
2558 log_warning_unit(UNIT(s)->id,
2559 "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
2560 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2562 log_warning_unit(UNIT(s)->id,
2563 "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.",
2565 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2570 case SERVICE_FINAL_SIGKILL:
2571 log_warning_unit(UNIT(s)->id,
2572 "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
2573 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2576 case SERVICE_AUTO_RESTART:
2577 log_info_unit(UNIT(s)->id,
2578 s->restart_usec > 0 ?
2579 "%s holdoff time over, scheduling restart." :
2580 "%s has no holdoff time, scheduling restart.",
2582 service_enter_restart(s);
2586 assert_not_reached("Timeout at wrong time.");
2592 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2593 Service *s = SERVICE(userdata);
2594 char t[FORMAT_TIMESPAN_MAX];
2597 assert(source == s->watchdog_event_source);
2599 log_error_unit(UNIT(s)->id,
2600 "%s watchdog timeout (limit %s)!",
2602 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
2603 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
2608 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2609 Service *s = SERVICE(u);
2611 bool notify_dbus = false;
2615 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
2616 u->id, pid, tags && *tags ? tags[0] : "(empty)");
2618 if (s->notify_access == NOTIFY_NONE) {
2619 log_warning_unit(u->id,
2620 "%s: Got notification message from PID "PID_FMT", but reception is disabled.",
2625 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2626 if (s->main_pid != 0)
2627 log_warning_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, u->id, pid, s->main_pid);
2629 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", u->id, pid);
2633 /* Interpret MAINPID= */
2634 e = strv_find_prefix(tags, "MAINPID=");
2635 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
2636 if (parse_pid(e + 8, &pid) < 0)
2637 log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
2639 log_debug_unit(u->id, "%s: got %s", u->id, e);
2640 service_set_main_pid(s, pid);
2641 unit_watch_pid(UNIT(s), pid);
2646 /* Interpret READY= */
2647 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
2648 log_debug_unit(u->id, "%s: got READY=1", u->id);
2649 service_enter_start_post(s);
2653 /* Interpret STATUS= */
2654 e = strv_find_prefix(tags, "STATUS=");
2659 if (!utf8_is_valid(e+7)) {
2660 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
2664 log_debug_unit(u->id, "%s: got %s", u->id, e);
2675 if (!streq_ptr(s->status_text, t)) {
2676 free(s->status_text);
2683 /* Interpret ERRNO= */
2684 e = strv_find_prefix(tags, "ERRNO=");
2688 if (safe_atoi(e + 6, &status_errno) < 0 || status_errno < 0)
2689 log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
2691 log_debug_unit(u->id, "%s: got %s", u->id, e);
2693 if (s->status_errno != status_errno) {
2694 s->status_errno = status_errno;
2700 /* Interpret WATCHDOG= */
2701 if (strv_find(tags, "WATCHDOG=1")) {
2702 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
2703 service_reset_watchdog(s);
2706 /* Notify clients about changed status or main pid */
2708 unit_add_to_dbus_queue(u);
2711 static int service_get_timeout(Unit *u, uint64_t *timeout) {
2712 Service *s = SERVICE(u);
2715 if (!s->timer_event_source)
2718 r = sd_event_source_get_time(s->timer_event_source, timeout);
2725 static void service_bus_name_owner_change(
2728 const char *old_owner,
2729 const char *new_owner) {
2731 Service *s = SERVICE(u);
2737 assert(streq(s->bus_name, name));
2738 assert(old_owner || new_owner);
2740 if (old_owner && new_owner)
2741 log_debug_unit(u->id,
2742 "%s's D-Bus name %s changed owner from %s to %s",
2743 u->id, name, old_owner, new_owner);
2745 log_debug_unit(u->id,
2746 "%s's D-Bus name %s no longer registered by %s",
2747 u->id, name, old_owner);
2749 log_debug_unit(u->id,
2750 "%s's D-Bus name %s now registered by %s",
2751 u->id, name, new_owner);
2753 s->bus_name_good = !!new_owner;
2755 if (s->type == SERVICE_DBUS) {
2757 /* service_enter_running() will figure out what to
2759 if (s->state == SERVICE_RUNNING)
2760 service_enter_running(s, SERVICE_SUCCESS);
2761 else if (s->state == SERVICE_START && new_owner)
2762 service_enter_start_post(s);
2764 } else if (new_owner &&
2766 (s->state == SERVICE_START ||
2767 s->state == SERVICE_START_POST ||
2768 s->state == SERVICE_RUNNING ||
2769 s->state == SERVICE_RELOAD)) {
2771 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
2774 /* Try to acquire PID from bus service */
2776 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
2778 r = sd_bus_creds_get_pid(creds, &pid);
2780 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
2782 service_set_main_pid(s, pid);
2783 unit_watch_pid(UNIT(s), pid);
2788 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2789 _cleanup_free_ char *peer = NULL;
2795 /* This is called by the socket code when instantiating a new
2796 * service for a stream socket and the socket needs to be
2799 if (UNIT(s)->load_state != UNIT_LOADED)
2802 if (s->socket_fd >= 0)
2805 if (s->state != SERVICE_DEAD)
2808 if (getpeername_pretty(fd, &peer) >= 0) {
2810 if (UNIT(s)->description) {
2811 _cleanup_free_ char *a;
2813 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2817 r = unit_set_description(UNIT(s), a);
2819 r = unit_set_description(UNIT(s), peer);
2827 unit_ref_set(&s->accept_socket, UNIT(sock));
2829 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
2832 static void service_reset_failed(Unit *u) {
2833 Service *s = SERVICE(u);
2837 if (s->state == SERVICE_FAILED)
2838 service_set_state(s, SERVICE_DEAD);
2840 s->result = SERVICE_SUCCESS;
2841 s->reload_result = SERVICE_SUCCESS;
2843 RATELIMIT_RESET(s->start_limit);
2846 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2847 Service *s = SERVICE(u);
2849 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
2852 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2853 [SERVICE_DEAD] = "dead",
2854 [SERVICE_START_PRE] = "start-pre",
2855 [SERVICE_START] = "start",
2856 [SERVICE_START_POST] = "start-post",
2857 [SERVICE_RUNNING] = "running",
2858 [SERVICE_EXITED] = "exited",
2859 [SERVICE_RELOAD] = "reload",
2860 [SERVICE_STOP] = "stop",
2861 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2862 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2863 [SERVICE_STOP_POST] = "stop-post",
2864 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2865 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2866 [SERVICE_FAILED] = "failed",
2867 [SERVICE_AUTO_RESTART] = "auto-restart",
2870 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2872 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2873 [SERVICE_RESTART_NO] = "no",
2874 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
2875 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
2876 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
2877 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
2878 [SERVICE_RESTART_ON_ABORT] = "on-abort",
2879 [SERVICE_RESTART_ALWAYS] = "always",
2882 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2884 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2885 [SERVICE_SIMPLE] = "simple",
2886 [SERVICE_FORKING] = "forking",
2887 [SERVICE_ONESHOT] = "oneshot",
2888 [SERVICE_DBUS] = "dbus",
2889 [SERVICE_NOTIFY] = "notify",
2890 [SERVICE_IDLE] = "idle"
2893 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2895 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2896 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2897 [SERVICE_EXEC_START] = "ExecStart",
2898 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2899 [SERVICE_EXEC_RELOAD] = "ExecReload",
2900 [SERVICE_EXEC_STOP] = "ExecStop",
2901 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2904 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2906 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2907 [NOTIFY_NONE] = "none",
2908 [NOTIFY_MAIN] = "main",
2909 [NOTIFY_ALL] = "all"
2912 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2914 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
2915 [SERVICE_SUCCESS] = "success",
2916 [SERVICE_FAILURE_RESOURCES] = "resources",
2917 [SERVICE_FAILURE_TIMEOUT] = "timeout",
2918 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
2919 [SERVICE_FAILURE_SIGNAL] = "signal",
2920 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
2921 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
2922 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
2925 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
2927 static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
2928 [SERVICE_FAILURE_ACTION_NONE] = "none",
2929 [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
2930 [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
2931 [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
2933 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
2935 const UnitVTable service_vtable = {
2936 .object_size = sizeof(Service),
2937 .exec_context_offset = offsetof(Service, exec_context),
2938 .cgroup_context_offset = offsetof(Service, cgroup_context),
2939 .kill_context_offset = offsetof(Service, kill_context),
2940 .exec_runtime_offset = offsetof(Service, exec_runtime),
2946 .private_section = "Service",
2948 .init = service_init,
2949 .done = service_done,
2950 .load = service_load,
2952 .coldplug = service_coldplug,
2954 .dump = service_dump,
2956 .start = service_start,
2957 .stop = service_stop,
2958 .reload = service_reload,
2960 .can_reload = service_can_reload,
2962 .kill = service_kill,
2964 .serialize = service_serialize,
2965 .deserialize_item = service_deserialize_item,
2967 .active_state = service_active_state,
2968 .sub_state_to_string = service_sub_state_to_string,
2970 .check_gc = service_check_gc,
2971 .check_snapshot = service_check_snapshot,
2973 .sigchld_event = service_sigchld_event,
2975 .reset_failed = service_reset_failed,
2977 .notify_cgroup_empty = service_notify_cgroup_empty_event,
2978 .notify_message = service_notify_message,
2980 .bus_name_owner_change = service_bus_name_owner_change,
2982 .bus_interface = "org.freedesktop.systemd1.Service",
2983 .bus_vtable = bus_service_vtable,
2984 .bus_set_property = bus_service_set_property,
2985 .bus_commit_properties = bus_service_commit_properties,
2987 .get_timeout = service_get_timeout,
2988 .can_transient = true,
2990 .status_message_formats = {
2991 .starting_stopping = {
2992 [0] = "Starting %s...",
2993 [1] = "Stopping %s...",
2995 .finished_start_job = {
2996 [JOB_DONE] = "Started %s.",
2997 [JOB_FAILED] = "Failed to start %s.",
2998 [JOB_DEPENDENCY] = "Dependency failed for %s.",
2999 [JOB_TIMEOUT] = "Timed out starting %s.",
3001 .finished_stop_job = {
3002 [JOB_DONE] = "Stopped %s.",
3003 [JOB_FAILED] = "Stopped (with error) %s.",
3004 [JOB_TIMEOUT] = "Timed out stopping %s.",