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 set_free(s->restart_ignore_status.code);
270 s->restart_ignore_status.code = NULL;
271 set_free(s->restart_ignore_status.signal);
272 s->restart_ignore_status.signal = NULL;
274 set_free(s->success_status.code);
275 s->success_status.code = NULL;
276 set_free(s->success_status.signal);
277 s->success_status.signal = NULL;
279 /* This will leak a process, but at least no memory or any of
281 service_unwatch_main_pid(s);
282 service_unwatch_control_pid(s);
283 service_unwatch_pid_file(s);
286 unit_unwatch_bus_name(u, s->bus_name);
291 service_close_socket_fd(s);
292 service_connection_unref(s);
294 unit_ref_unset(&s->accept_socket);
296 service_stop_watchdog(s);
298 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
301 static int service_arm_timer(Service *s, usec_t usec) {
306 if (s->timer_event_source) {
307 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
311 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
314 return sd_event_add_time(
315 UNIT(s)->manager->event,
316 &s->timer_event_source,
318 now(CLOCK_MONOTONIC) + usec, 0,
319 service_dispatch_timer, s);
322 static int service_verify(Service *s) {
325 if (UNIT(s)->load_state != UNIT_LOADED)
328 if (!s->exec_command[SERVICE_EXEC_START]) {
329 log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
333 if (s->type != SERVICE_ONESHOT &&
334 s->exec_command[SERVICE_EXEC_START]->command_next) {
335 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);
339 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
340 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);
344 if (s->type == SERVICE_DBUS && !s->bus_name) {
345 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);
349 if (s->bus_name && s->type != SERVICE_DBUS)
350 log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
352 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
353 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_dependencies(Service *s) {
365 /* Add a number of automatic dependencies useful for the
366 * majority of services. */
368 /* First, pull in base system */
369 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
370 SPECIAL_BASIC_TARGET, NULL, true);
374 /* Second, activate normal shutdown */
375 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
376 SPECIAL_SHUTDOWN_TARGET, NULL, true);
380 static void service_fix_output(Service *s) {
383 /* If nothing has been explicitly configured, patch default
384 * output in. If input is socket/tty we avoid this however,
385 * since in that case we want output to default to the same
386 * place as we read input from. */
388 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
389 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
390 s->exec_context.std_input == EXEC_INPUT_NULL)
391 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
393 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
394 s->exec_context.std_input == EXEC_INPUT_NULL)
395 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
398 static int service_load(Unit *u) {
400 Service *s = SERVICE(u);
404 /* Load a .service file */
405 r = unit_load_fragment(u);
409 /* Still nothing found? Then let's give up */
410 if (u->load_state == UNIT_STUB)
413 /* This is a new unit? Then let's add in some extras */
414 if (u->load_state == UNIT_LOADED) {
416 /* We were able to load something, then let's add in
417 * the dropin directories. */
418 r = unit_load_dropin(u);
422 if (s->type == _SERVICE_TYPE_INVALID)
423 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
425 /* Oneshot services have disabled start timeout by default */
426 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
427 s->timeout_start_usec = 0;
429 service_fix_output(s);
431 r = unit_patch_contexts(u);
435 r = unit_add_exec_dependencies(u, &s->exec_context);
439 r = unit_add_default_slice(u, &s->cgroup_context);
443 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
444 s->notify_access = NOTIFY_MAIN;
446 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
447 s->notify_access = NOTIFY_MAIN;
450 r = unit_watch_bus_name(u, s->bus_name);
455 if (u->default_dependencies) {
456 r = service_add_default_dependencies(s);
463 return service_verify(s);
466 static void service_dump(Unit *u, FILE *f, const char *prefix) {
468 ServiceExecCommand c;
469 Service *s = SERVICE(u);
471 _cleanup_free_ char *p2 = NULL;
475 p2 = strappend(prefix, "\t");
476 prefix2 = p2 ? p2 : prefix;
479 "%sService State: %s\n"
481 "%sReload Result: %s\n"
482 "%sPermissionsStartOnly: %s\n"
483 "%sRootDirectoryStartOnly: %s\n"
484 "%sRemainAfterExit: %s\n"
485 "%sGuessMainPID: %s\n"
488 "%sNotifyAccess: %s\n",
489 prefix, service_state_to_string(s->state),
490 prefix, service_result_to_string(s->result),
491 prefix, service_result_to_string(s->reload_result),
492 prefix, yes_no(s->permissions_start_only),
493 prefix, yes_no(s->root_directory_start_only),
494 prefix, yes_no(s->remain_after_exit),
495 prefix, yes_no(s->guess_main_pid),
496 prefix, service_type_to_string(s->type),
497 prefix, service_restart_to_string(s->restart),
498 prefix, notify_access_to_string(s->notify_access));
500 if (s->control_pid > 0)
502 "%sControl PID: "PID_FMT"\n",
503 prefix, s->control_pid);
507 "%sMain PID: "PID_FMT"\n"
508 "%sMain PID Known: %s\n"
509 "%sMain PID Alien: %s\n",
511 prefix, yes_no(s->main_pid_known),
512 prefix, yes_no(s->main_pid_alien));
517 prefix, s->pid_file);
522 "%sBus Name Good: %s\n",
524 prefix, yes_no(s->bus_name_good));
526 kill_context_dump(&s->kill_context, f, prefix);
527 exec_context_dump(&s->exec_context, f, prefix);
529 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
531 if (!s->exec_command[c])
534 fprintf(f, "%s-> %s:\n",
535 prefix, service_exec_command_to_string(c));
537 exec_command_dump_list(s->exec_command[c], f, prefix2);
540 #ifdef HAVE_SYSV_COMPAT
541 if (s->sysv_start_priority >= 0)
543 "%sSysVStartPriority: %i\n",
544 prefix, s->sysv_start_priority);
548 fprintf(f, "%sStatus Text: %s\n",
549 prefix, s->status_text);
552 static int service_load_pid_file(Service *s, bool may_warn) {
553 _cleanup_free_ char *k = NULL;
562 r = read_one_line_file(s->pid_file, &k);
565 log_info_unit(UNIT(s)->id,
566 "PID file %s not readable (yet?) after %s.",
567 s->pid_file, service_state_to_string(s->state));
571 r = parse_pid(k, &pid);
574 log_info_unit(UNIT(s)->id,
575 "Failed to read PID from file %s: %s",
576 s->pid_file, strerror(-r));
580 if (!pid_is_alive(pid)) {
582 log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
587 if (s->main_pid_known) {
588 if (pid == s->main_pid)
591 log_debug_unit(UNIT(s)->id,
592 "Main PID changing: "PID_FMT" -> "PID_FMT,
594 service_unwatch_main_pid(s);
595 s->main_pid_known = false;
597 log_debug_unit(UNIT(s)->id,
598 "Main PID loaded: "PID_FMT, pid);
600 r = service_set_main_pid(s, pid);
604 r = unit_watch_pid(UNIT(s), pid);
606 /* FIXME: we need to do something here */
607 log_warning_unit(UNIT(s)->id,
608 "Failed to watch PID "PID_FMT" from service %s",
616 static int service_search_main_pid(Service *s) {
622 /* If we know it anyway, don't ever fallback to unreliable
624 if (s->main_pid_known)
627 if (!s->guess_main_pid)
630 assert(s->main_pid <= 0);
632 pid = unit_search_main_pid(UNIT(s));
636 log_debug_unit(UNIT(s)->id,
637 "Main PID guessed: "PID_FMT, pid);
638 r = service_set_main_pid(s, pid);
642 r = unit_watch_pid(UNIT(s), pid);
644 /* FIXME: we need to do something here */
645 log_warning_unit(UNIT(s)->id,
646 "Failed to watch PID "PID_FMT" from service %s",
651 static void service_set_state(Service *s, ServiceState state) {
652 ServiceState old_state;
653 const UnitActiveState *table;
657 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
659 old_state = s->state;
662 service_unwatch_pid_file(s);
665 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
667 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
669 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
670 SERVICE_AUTO_RESTART))
671 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
674 SERVICE_START, SERVICE_START_POST,
675 SERVICE_RUNNING, SERVICE_RELOAD,
676 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
678 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
679 service_unwatch_main_pid(s);
680 s->main_command = NULL;
684 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
686 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
688 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
689 service_unwatch_control_pid(s);
690 s->control_command = NULL;
691 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
694 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
695 unit_unwatch_all_pids(UNIT(s));
698 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
699 SERVICE_RUNNING, SERVICE_RELOAD,
700 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
701 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
702 !(state == SERVICE_DEAD && UNIT(s)->job)) {
703 service_close_socket_fd(s);
704 service_connection_unref(s);
707 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
708 service_stop_watchdog(s);
710 /* For the inactive states unit_notify() will trim the cgroup,
711 * but for exit we have to do that ourselves... */
712 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
713 unit_destroy_cgroup(UNIT(s));
715 /* For remain_after_exit services, let's see if we can "release" the
716 * hold on the console, since unit_notify() only does that in case of
718 if (state == SERVICE_EXITED && s->remain_after_exit &&
719 UNIT(s)->manager->n_on_console > 0) {
720 ExecContext *ec = unit_get_exec_context(UNIT(s));
721 if (ec && exec_context_may_touch_console(ec)) {
722 Manager *m = UNIT(s)->manager;
725 if (m->n_on_console == 0)
726 /* unset no_console_output flag, since the console is free */
727 m->no_console_output = false;
731 if (old_state != state)
732 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
734 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
735 s->reload_result = SERVICE_SUCCESS;
738 static int service_coldplug(Unit *u) {
739 Service *s = SERVICE(u);
743 assert(s->state == SERVICE_DEAD);
745 if (s->deserialized_state != s->state) {
747 if (IN_SET(s->deserialized_state,
748 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
750 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
752 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
756 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
758 /* For the start/stop timeouts 0 means off */
760 r = service_arm_timer(s, k);
766 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
768 /* The restart timeouts 0 means immediately */
769 r = service_arm_timer(s, s->restart_usec);
774 if (pid_is_unwaited(s->main_pid) &&
775 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
776 IN_SET(s->deserialized_state,
777 SERVICE_START, SERVICE_START_POST,
778 SERVICE_RUNNING, SERVICE_RELOAD,
779 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
781 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
782 r = unit_watch_pid(UNIT(s), s->main_pid);
787 if (pid_is_unwaited(s->control_pid) &&
788 IN_SET(s->deserialized_state,
789 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
791 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
793 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
794 r = unit_watch_pid(UNIT(s), s->control_pid);
799 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
800 unit_watch_all_pids(UNIT(s));
802 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
803 service_start_watchdog(s);
805 service_set_state(s, s->deserialized_state);
811 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
822 if (s->socket_fd >= 0)
825 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
830 if (u->type != UNIT_SOCKET)
835 r = socket_collect_fds(sock, &cfds, &cn_fds);
848 t = new(int, rn_fds+cn_fds);
855 memcpy(t, rfds, rn_fds * sizeof(int));
856 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
861 rn_fds = rn_fds+cn_fds;
876 static int service_spawn(
881 bool apply_permissions,
883 bool apply_tty_stdin,
884 bool set_notify_socket,
891 _cleanup_free_ int *fdsbuf = NULL;
892 unsigned n_fds = 0, n_env = 0;
893 _cleanup_strv_free_ char
894 **argv = NULL, **final_env = NULL, **our_env = NULL;
901 unit_realize_cgroup(UNIT(s));
903 r = unit_setup_exec_runtime(UNIT(s));
908 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
909 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
910 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
912 if (s->socket_fd >= 0) {
916 r = service_collect_fds(s, &fdsbuf, &n_fds);
924 if (timeout && s->timeout_start_usec > 0) {
925 r = service_arm_timer(s, s->timeout_start_usec);
929 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
931 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
935 our_env = new0(char*, 4);
941 if (set_notify_socket)
942 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
948 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
953 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
954 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
959 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
965 if (is_control && UNIT(s)->cgroup_path) {
966 path = strappenda(UNIT(s)->cgroup_path, "/control");
967 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
969 path = UNIT(s)->cgroup_path;
979 UNIT(s)->manager->confirm_spawn,
980 UNIT(s)->manager->cgroup_supported,
982 manager_get_runtime_prefix(UNIT(s)->manager),
985 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
991 r = unit_watch_pid(UNIT(s), pid);
993 /* FIXME: we need to do something here */
1002 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1007 static int main_pid_good(Service *s) {
1010 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1013 /* If we know the pid file, then lets just check if it is
1015 if (s->main_pid_known) {
1017 /* If it's an alien child let's check if it is still
1019 if (s->main_pid_alien && s->main_pid > 0)
1020 return pid_is_alive(s->main_pid);
1022 /* .. otherwise assume we'll get a SIGCHLD for it,
1023 * which we really should wait for to collect exit
1024 * status and code */
1025 return s->main_pid > 0;
1028 /* We don't know the pid */
1032 _pure_ static int control_pid_good(Service *s) {
1035 return s->control_pid > 0;
1038 static int cgroup_good(Service *s) {
1043 if (!UNIT(s)->cgroup_path)
1046 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1053 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
1055 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1059 if (f != SERVICE_SUCCESS)
1062 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1064 if (s->result != SERVICE_SUCCESS)
1065 service_execute_action(s, s->failure_action, "failed", false);
1067 if (allow_restart &&
1068 !s->forbid_restart &&
1069 (s->restart == SERVICE_RESTART_ALWAYS ||
1070 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1071 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1072 (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
1073 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1074 (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP))) &&
1075 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1076 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1077 (s->result != SERVICE_FAILURE_SIGNAL ||
1078 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1080 r = service_arm_timer(s, s->restart_usec);
1084 service_set_state(s, SERVICE_AUTO_RESTART);
1087 s->forbid_restart = false;
1089 /* We want fresh tmpdirs in case service is started again immediately */
1090 exec_runtime_destroy(s->exec_runtime);
1091 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1093 /* Also, remove the runtime directory in */
1094 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1096 /* Try to delete the pid file. At this point it will be
1097 * out-of-date, and some software might be confused by it, so
1098 * let's remove it. */
1100 unlink_noerrno(s->pid_file);
1105 log_warning_unit(UNIT(s)->id,
1106 "%s failed to run install restart timer: %s",
1107 UNIT(s)->id, strerror(-r));
1108 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1111 static void service_enter_stop_post(Service *s, ServiceResult f) {
1115 if (f != SERVICE_SUCCESS)
1118 service_unwatch_control_pid(s);
1119 unit_watch_all_pids(UNIT(s));
1121 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1122 if (s->control_command) {
1123 s->control_command_id = SERVICE_EXEC_STOP_POST;
1125 r = service_spawn(s,
1129 !s->permissions_start_only,
1130 !s->root_directory_start_only,
1138 service_set_state(s, SERVICE_STOP_POST);
1140 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1145 log_warning_unit(UNIT(s)->id,
1146 "%s failed to run 'stop-post' task: %s",
1147 UNIT(s)->id, strerror(-r));
1148 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1151 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1156 if (f != SERVICE_SUCCESS)
1159 unit_watch_all_pids(UNIT(s));
1161 r = unit_kill_context(
1164 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1173 if (s->timeout_stop_usec > 0) {
1174 r = service_arm_timer(s, s->timeout_stop_usec);
1179 service_set_state(s, state);
1180 } else if (state == SERVICE_STOP_SIGTERM)
1181 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1182 else if (state == SERVICE_STOP_SIGKILL)
1183 service_enter_stop_post(s, SERVICE_SUCCESS);
1184 else if (state == SERVICE_FINAL_SIGTERM)
1185 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1187 service_enter_dead(s, SERVICE_SUCCESS, true);
1192 log_warning_unit(UNIT(s)->id,
1193 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1195 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1196 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1198 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1201 static void service_enter_stop(Service *s, ServiceResult f) {
1206 if (f != SERVICE_SUCCESS)
1209 service_unwatch_control_pid(s);
1210 unit_watch_all_pids(UNIT(s));
1212 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1213 if (s->control_command) {
1214 s->control_command_id = SERVICE_EXEC_STOP;
1216 r = service_spawn(s,
1220 !s->permissions_start_only,
1221 !s->root_directory_start_only,
1229 service_set_state(s, SERVICE_STOP);
1231 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1236 log_warning_unit(UNIT(s)->id,
1237 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
1238 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1241 static void service_enter_running(Service *s, ServiceResult f) {
1242 int main_pid_ok, cgroup_ok;
1245 if (f != SERVICE_SUCCESS)
1248 main_pid_ok = main_pid_good(s);
1249 cgroup_ok = cgroup_good(s);
1251 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1252 (s->bus_name_good || s->type != SERVICE_DBUS))
1253 service_set_state(s, SERVICE_RUNNING);
1254 else if (s->remain_after_exit)
1255 service_set_state(s, SERVICE_EXITED);
1257 service_enter_stop(s, SERVICE_SUCCESS);
1260 static void service_enter_start_post(Service *s) {
1264 service_unwatch_control_pid(s);
1265 service_reset_watchdog(s);
1267 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1268 if (s->control_command) {
1269 s->control_command_id = SERVICE_EXEC_START_POST;
1271 r = service_spawn(s,
1275 !s->permissions_start_only,
1276 !s->root_directory_start_only,
1284 service_set_state(s, SERVICE_START_POST);
1286 service_enter_running(s, SERVICE_SUCCESS);
1291 log_warning_unit(UNIT(s)->id,
1292 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
1293 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1296 static void service_kill_control_processes(Service *s) {
1299 if (!UNIT(s)->cgroup_path)
1302 p = strappenda(UNIT(s)->cgroup_path, "/control");
1303 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1306 static void service_enter_start(Service *s) {
1313 assert(s->exec_command[SERVICE_EXEC_START]);
1314 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1316 service_unwatch_control_pid(s);
1317 service_unwatch_main_pid(s);
1319 /* We want to ensure that nobody leaks processes from
1320 * START_PRE here, so let's go on a killing spree, People
1321 * should not spawn long running processes from START_PRE. */
1322 service_kill_control_processes(s);
1324 if (s->type == SERVICE_FORKING) {
1325 s->control_command_id = SERVICE_EXEC_START;
1326 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1328 s->main_command = NULL;
1330 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1331 s->control_command = NULL;
1333 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1336 r = service_spawn(s,
1338 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
1339 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
1344 s->notify_access != NOTIFY_NONE,
1350 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
1351 /* For simple services we immediately start
1352 * the START_POST binaries. */
1354 service_set_main_pid(s, pid);
1355 service_enter_start_post(s);
1357 } else if (s->type == SERVICE_FORKING) {
1359 /* For forking services we wait until the start
1360 * process exited. */
1362 s->control_pid = pid;
1363 service_set_state(s, SERVICE_START);
1365 } else if (s->type == SERVICE_ONESHOT ||
1366 s->type == SERVICE_DBUS ||
1367 s->type == SERVICE_NOTIFY) {
1369 /* For oneshot services we wait until the start
1370 * process exited, too, but it is our main process. */
1372 /* For D-Bus services we know the main pid right away,
1373 * but wait for the bus name to appear on the
1374 * bus. Notify services are similar. */
1376 service_set_main_pid(s, pid);
1377 service_set_state(s, SERVICE_START);
1379 assert_not_reached("Unknown service type");
1384 log_warning_unit(UNIT(s)->id,
1385 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
1386 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1389 static void service_enter_start_pre(Service *s) {
1394 service_unwatch_control_pid(s);
1396 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1397 if (s->control_command) {
1398 /* Before we start anything, let's clear up what might
1399 * be left from previous runs. */
1400 service_kill_control_processes(s);
1402 s->control_command_id = SERVICE_EXEC_START_PRE;
1404 r = service_spawn(s,
1408 !s->permissions_start_only,
1409 !s->root_directory_start_only,
1417 service_set_state(s, SERVICE_START_PRE);
1419 service_enter_start(s);
1424 log_warning_unit(UNIT(s)->id,
1425 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
1426 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1429 static void service_enter_restart(Service *s) {
1430 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1435 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1436 /* Don't restart things if we are going down anyway */
1437 log_info_unit(UNIT(s)->id,
1438 "Stop job pending for unit, delaying automatic restart.");
1440 r = service_arm_timer(s, s->restart_usec);
1447 /* Any units that are bound to this service must also be
1448 * restarted. We use JOB_RESTART (instead of the more obvious
1449 * JOB_START) here so that those dependency jobs will be added
1451 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1455 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1456 * it will be canceled as part of the service_stop() call that
1457 * is executed as part of JOB_RESTART. */
1459 log_debug_unit(UNIT(s)->id,
1460 "%s scheduled restart job.", UNIT(s)->id);
1464 log_warning_unit(UNIT(s)->id,
1465 "%s failed to schedule restart job: %s",
1466 UNIT(s)->id, bus_error_message(&error, -r));
1467 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1470 static void service_enter_reload(Service *s) {
1475 service_unwatch_control_pid(s);
1477 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1478 if (s->control_command) {
1479 s->control_command_id = SERVICE_EXEC_RELOAD;
1481 r = service_spawn(s,
1485 !s->permissions_start_only,
1486 !s->root_directory_start_only,
1494 service_set_state(s, SERVICE_RELOAD);
1496 service_enter_running(s, SERVICE_SUCCESS);
1501 log_warning_unit(UNIT(s)->id,
1502 "%s failed to run 'reload' task: %s",
1503 UNIT(s)->id, strerror(-r));
1504 s->reload_result = SERVICE_FAILURE_RESOURCES;
1505 service_enter_running(s, SERVICE_SUCCESS);
1508 static void service_run_next_control(Service *s) {
1512 assert(s->control_command);
1513 assert(s->control_command->command_next);
1515 assert(s->control_command_id != SERVICE_EXEC_START);
1517 s->control_command = s->control_command->command_next;
1518 service_unwatch_control_pid(s);
1520 r = service_spawn(s,
1524 !s->permissions_start_only,
1525 !s->root_directory_start_only,
1526 s->control_command_id == SERVICE_EXEC_START_PRE ||
1527 s->control_command_id == SERVICE_EXEC_STOP_POST,
1537 log_warning_unit(UNIT(s)->id,
1538 "%s failed to run next control task: %s",
1539 UNIT(s)->id, strerror(-r));
1541 if (s->state == SERVICE_START_PRE)
1542 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1543 else if (s->state == SERVICE_STOP)
1544 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1545 else if (s->state == SERVICE_STOP_POST)
1546 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1547 else if (s->state == SERVICE_RELOAD) {
1548 s->reload_result = SERVICE_FAILURE_RESOURCES;
1549 service_enter_running(s, SERVICE_SUCCESS);
1551 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1554 static void service_run_next_main(Service *s) {
1559 assert(s->main_command);
1560 assert(s->main_command->command_next);
1561 assert(s->type == SERVICE_ONESHOT);
1563 s->main_command = s->main_command->command_next;
1564 service_unwatch_main_pid(s);
1566 r = service_spawn(s,
1573 s->notify_access != NOTIFY_NONE,
1579 service_set_main_pid(s, pid);
1584 log_warning_unit(UNIT(s)->id,
1585 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
1586 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1589 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
1592 if (action == SERVICE_FAILURE_ACTION_REBOOT ||
1593 action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
1594 update_reboot_param_file(s->reboot_arg);
1598 case SERVICE_FAILURE_ACTION_NONE:
1599 if (log_action_none)
1600 log_warning_unit(UNIT(s)->id,
1601 "%s %s, refusing to start.", UNIT(s)->id, reason);
1604 case SERVICE_FAILURE_ACTION_REBOOT: {
1605 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1608 log_warning_unit(UNIT(s)->id,
1609 "%s %s, rebooting.", UNIT(s)->id, reason);
1611 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
1612 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
1613 true, &error, NULL);
1615 log_error_unit(UNIT(s)->id,
1616 "Failed to reboot: %s.", bus_error_message(&error, r));
1621 case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
1622 log_warning_unit(UNIT(s)->id,
1623 "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
1624 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
1627 case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
1628 log_warning_unit(UNIT(s)->id,
1629 "%s %s, rebooting immediately.", UNIT(s)->id, reason);
1631 if (s->reboot_arg) {
1632 log_info("Rebooting with argument '%s'.", s->reboot_arg);
1633 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
1634 LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
1637 log_info("Rebooting.");
1638 reboot(RB_AUTOBOOT);
1642 log_error_unit(UNIT(s)->id,
1643 "failure action=%i", action);
1644 assert_not_reached("Unknown FailureAction.");
1650 static int service_start_limit_test(Service *s) {
1653 if (ratelimit_test(&s->start_limit))
1656 return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
1659 static int service_start(Unit *u) {
1660 Service *s = SERVICE(u);
1665 /* We cannot fulfill this request right now, try again later
1667 if (s->state == SERVICE_STOP ||
1668 s->state == SERVICE_STOP_SIGTERM ||
1669 s->state == SERVICE_STOP_SIGKILL ||
1670 s->state == SERVICE_STOP_POST ||
1671 s->state == SERVICE_FINAL_SIGTERM ||
1672 s->state == SERVICE_FINAL_SIGKILL)
1675 /* Already on it! */
1676 if (s->state == SERVICE_START_PRE ||
1677 s->state == SERVICE_START ||
1678 s->state == SERVICE_START_POST)
1681 /* A service that will be restarted must be stopped first to
1682 * trigger BindsTo and/or OnFailure dependencies. If a user
1683 * does not want to wait for the holdoff time to elapse, the
1684 * service should be manually restarted, not started. We
1685 * simply return EAGAIN here, so that any start jobs stay
1686 * queued, and assume that the auto restart timer will
1687 * eventually trigger the restart. */
1688 if (s->state == SERVICE_AUTO_RESTART)
1691 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
1693 /* Make sure we don't enter a busy loop of some kind. */
1694 r = service_start_limit_test(s);
1696 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
1700 s->result = SERVICE_SUCCESS;
1701 s->reload_result = SERVICE_SUCCESS;
1702 s->main_pid_known = false;
1703 s->main_pid_alien = false;
1704 s->forbid_restart = false;
1706 service_enter_start_pre(s);
1710 static int service_stop(Unit *u) {
1711 Service *s = SERVICE(u);
1715 /* Don't create restart jobs from here. */
1716 s->forbid_restart = true;
1719 if (s->state == SERVICE_STOP ||
1720 s->state == SERVICE_STOP_SIGTERM ||
1721 s->state == SERVICE_STOP_SIGKILL ||
1722 s->state == SERVICE_STOP_POST ||
1723 s->state == SERVICE_FINAL_SIGTERM ||
1724 s->state == SERVICE_FINAL_SIGKILL)
1727 /* A restart will be scheduled or is in progress. */
1728 if (s->state == SERVICE_AUTO_RESTART) {
1729 service_set_state(s, SERVICE_DEAD);
1733 /* If there's already something running we go directly into
1735 if (s->state == SERVICE_START_PRE ||
1736 s->state == SERVICE_START ||
1737 s->state == SERVICE_START_POST ||
1738 s->state == SERVICE_RELOAD) {
1739 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1743 assert(s->state == SERVICE_RUNNING ||
1744 s->state == SERVICE_EXITED);
1746 service_enter_stop(s, SERVICE_SUCCESS);
1750 static int service_reload(Unit *u) {
1751 Service *s = SERVICE(u);
1755 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1757 service_enter_reload(s);
1761 _pure_ static bool service_can_reload(Unit *u) {
1762 Service *s = SERVICE(u);
1766 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1769 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1770 Service *s = SERVICE(u);
1776 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1777 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1778 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
1780 if (s->control_pid > 0)
1781 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1784 if (s->main_pid_known && s->main_pid > 0)
1785 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
1787 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1790 unit_serialize_item(u, f, "status-text", s->status_text);
1792 /* FIXME: There's a minor uncleanliness here: if there are
1793 * multiple commands attached here, we will start from the
1794 * first one again */
1795 if (s->control_command_id >= 0)
1796 unit_serialize_item(u, f, "control-command",
1797 service_exec_command_to_string(s->control_command_id));
1799 if (s->socket_fd >= 0) {
1802 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1805 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1808 if (s->main_exec_status.pid > 0) {
1809 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1810 s->main_exec_status.pid);
1811 dual_timestamp_serialize(f, "main-exec-status-start",
1812 &s->main_exec_status.start_timestamp);
1813 dual_timestamp_serialize(f, "main-exec-status-exit",
1814 &s->main_exec_status.exit_timestamp);
1816 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
1817 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1818 s->main_exec_status.code);
1819 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1820 s->main_exec_status.status);
1823 if (dual_timestamp_is_set(&s->watchdog_timestamp))
1824 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
1826 if (s->forbid_restart)
1827 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
1832 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1833 Service *s = SERVICE(u);
1840 if (streq(key, "state")) {
1843 state = service_state_from_string(value);
1845 log_debug_unit(u->id, "Failed to parse state value %s", value);
1847 s->deserialized_state = state;
1848 } else if (streq(key, "result")) {
1851 f = service_result_from_string(value);
1853 log_debug_unit(u->id, "Failed to parse result value %s", value);
1854 else if (f != SERVICE_SUCCESS)
1857 } else if (streq(key, "reload-result")) {
1860 f = service_result_from_string(value);
1862 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
1863 else if (f != SERVICE_SUCCESS)
1864 s->reload_result = f;
1866 } else if (streq(key, "control-pid")) {
1869 if (parse_pid(value, &pid) < 0)
1870 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
1872 s->control_pid = pid;
1873 } else if (streq(key, "main-pid")) {
1876 if (parse_pid(value, &pid) < 0)
1877 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
1879 service_set_main_pid(s, pid);
1880 unit_watch_pid(UNIT(s), pid);
1882 } else if (streq(key, "main-pid-known")) {
1885 b = parse_boolean(value);
1887 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
1889 s->main_pid_known = b;
1890 } else if (streq(key, "status-text")) {
1897 free(s->status_text);
1901 } else if (streq(key, "control-command")) {
1902 ServiceExecCommand id;
1904 id = service_exec_command_from_string(value);
1906 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
1908 s->control_command_id = id;
1909 s->control_command = s->exec_command[id];
1911 } else if (streq(key, "socket-fd")) {
1914 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1915 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
1918 asynchronous_close(s->socket_fd);
1919 s->socket_fd = fdset_remove(fds, fd);
1921 } else if (streq(key, "main-exec-status-pid")) {
1924 if (parse_pid(value, &pid) < 0)
1925 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
1927 s->main_exec_status.pid = pid;
1928 } else if (streq(key, "main-exec-status-code")) {
1931 if (safe_atoi(value, &i) < 0)
1932 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
1934 s->main_exec_status.code = i;
1935 } else if (streq(key, "main-exec-status-status")) {
1938 if (safe_atoi(value, &i) < 0)
1939 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
1941 s->main_exec_status.status = i;
1942 } else if (streq(key, "main-exec-status-start"))
1943 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
1944 else if (streq(key, "main-exec-status-exit"))
1945 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
1946 else if (streq(key, "watchdog-timestamp"))
1947 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
1948 else if (streq(key, "forbid-restart")) {
1951 b = parse_boolean(value);
1953 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
1955 s->forbid_restart = b;
1957 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
1962 _pure_ static UnitActiveState service_active_state(Unit *u) {
1963 const UnitActiveState *table;
1967 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1969 return table[SERVICE(u)->state];
1972 static const char *service_sub_state_to_string(Unit *u) {
1975 return service_state_to_string(SERVICE(u)->state);
1978 static bool service_check_gc(Unit *u) {
1979 Service *s = SERVICE(u);
1983 /* Never clean up services that still have a process around,
1984 * even if the service is formally dead. */
1985 if (cgroup_good(s) > 0 ||
1986 main_pid_good(s) > 0 ||
1987 control_pid_good(s) > 0)
1993 _pure_ static bool service_check_snapshot(Unit *u) {
1994 Service *s = SERVICE(u);
1998 return (s->socket_fd < 0);
2001 static int service_retry_pid_file(Service *s) {
2004 assert(s->pid_file);
2005 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2007 r = service_load_pid_file(s, false);
2011 service_unwatch_pid_file(s);
2013 service_enter_running(s, SERVICE_SUCCESS);
2017 static int service_watch_pid_file(Service *s) {
2020 log_debug_unit(UNIT(s)->id,
2021 "Setting watch for %s's PID file %s",
2022 UNIT(s)->id, s->pid_file_pathspec->path);
2023 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2027 /* the pidfile might have appeared just before we set the watch */
2028 log_debug_unit(UNIT(s)->id,
2029 "Trying to read %s's PID file %s in case it changed",
2030 UNIT(s)->id, s->pid_file_pathspec->path);
2031 service_retry_pid_file(s);
2035 log_error_unit(UNIT(s)->id,
2036 "Failed to set a watch for %s's PID file %s: %s",
2037 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2038 service_unwatch_pid_file(s);
2042 static int service_demand_pid_file(Service *s) {
2045 assert(s->pid_file);
2046 assert(!s->pid_file_pathspec);
2048 ps = new0(PathSpec, 1);
2053 ps->path = strdup(s->pid_file);
2059 path_kill_slashes(ps->path);
2061 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2062 * keep their PID file open all the time. */
2063 ps->type = PATH_MODIFIED;
2064 ps->inotify_fd = -1;
2066 s->pid_file_pathspec = ps;
2068 return service_watch_pid_file(s);
2071 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2072 PathSpec *p = userdata;
2077 s = SERVICE(p->unit);
2081 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2082 assert(s->pid_file_pathspec);
2083 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2085 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2087 if (path_spec_fd_event(p, events) < 0)
2090 if (service_retry_pid_file(s) == 0)
2093 if (service_watch_pid_file(s) < 0)
2099 service_unwatch_pid_file(s);
2100 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2104 static void service_notify_cgroup_empty_event(Unit *u) {
2105 Service *s = SERVICE(u);
2109 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2113 /* Waiting for SIGCHLD is usually more interesting,
2114 * because it includes return codes/signals. Which is
2115 * why we ignore the cgroup events for most cases,
2116 * except when we don't know pid which to expect the
2120 case SERVICE_START_POST:
2121 /* If we were hoping for the daemon to write its PID file,
2122 * we can give up now. */
2123 if (s->pid_file_pathspec) {
2124 log_warning_unit(u->id,
2125 "%s never wrote its PID file. Failing.", UNIT(s)->id);
2126 service_unwatch_pid_file(s);
2127 if (s->state == SERVICE_START)
2128 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2130 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2134 case SERVICE_RUNNING:
2135 /* service_enter_running() will figure out what to do */
2136 service_enter_running(s, SERVICE_SUCCESS);
2139 case SERVICE_STOP_SIGTERM:
2140 case SERVICE_STOP_SIGKILL:
2142 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2143 service_enter_stop_post(s, SERVICE_SUCCESS);
2147 case SERVICE_STOP_POST:
2148 case SERVICE_FINAL_SIGTERM:
2149 case SERVICE_FINAL_SIGKILL:
2150 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2151 service_enter_dead(s, SERVICE_SUCCESS, true);
2160 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2161 Service *s = SERVICE(u);
2167 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2168 is_clean_exit_lsb(code, status, &s->success_status))
2169 f = SERVICE_SUCCESS;
2170 else if (code == CLD_EXITED)
2171 f = SERVICE_FAILURE_EXIT_CODE;
2172 else if (code == CLD_KILLED)
2173 f = SERVICE_FAILURE_SIGNAL;
2174 else if (code == CLD_DUMPED)
2175 f = SERVICE_FAILURE_CORE_DUMP;
2177 assert_not_reached("Unknown code");
2179 if (s->main_pid == pid) {
2180 /* Forking services may occasionally move to a new PID.
2181 * As long as they update the PID file before exiting the old
2182 * PID, they're fine. */
2183 if (service_load_pid_file(s, false) == 0)
2187 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2189 if (s->main_command) {
2190 /* If this is not a forking service than the
2191 * main process got started and hence we copy
2192 * the exit status so that it is recorded both
2193 * as main and as control process exit
2196 s->main_command->exec_status = s->main_exec_status;
2198 if (s->main_command->ignore)
2199 f = SERVICE_SUCCESS;
2200 } else if (s->exec_command[SERVICE_EXEC_START]) {
2202 /* If this is a forked process, then we should
2203 * ignore the return value if this was
2204 * configured for the starter process */
2206 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2207 f = SERVICE_SUCCESS;
2210 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2212 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2213 u->id, sigchld_code_to_string(code), status,
2214 strna(code == CLD_EXITED
2215 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2216 : signal_to_string(status)),
2217 "EXIT_CODE=%s", sigchld_code_to_string(code),
2218 "EXIT_STATUS=%i", status,
2221 if (f != SERVICE_SUCCESS)
2224 if (s->main_command &&
2225 s->main_command->command_next &&
2226 f == SERVICE_SUCCESS) {
2228 /* There is another command to *
2229 * execute, so let's do that. */
2231 log_debug_unit(u->id,
2232 "%s running next main command for state %s",
2233 u->id, service_state_to_string(s->state));
2234 service_run_next_main(s);
2238 /* The service exited, so the service is officially
2240 s->main_command = NULL;
2244 case SERVICE_START_POST:
2245 case SERVICE_RELOAD:
2247 /* Need to wait until the operation is
2252 if (s->type == SERVICE_ONESHOT) {
2253 /* This was our main goal, so let's go on */
2254 if (f == SERVICE_SUCCESS)
2255 service_enter_start_post(s);
2257 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2263 case SERVICE_RUNNING:
2264 service_enter_running(s, f);
2267 case SERVICE_STOP_SIGTERM:
2268 case SERVICE_STOP_SIGKILL:
2270 if (!control_pid_good(s))
2271 service_enter_stop_post(s, f);
2273 /* If there is still a control process, wait for that first */
2276 case SERVICE_STOP_POST:
2277 case SERVICE_FINAL_SIGTERM:
2278 case SERVICE_FINAL_SIGKILL:
2280 if (!control_pid_good(s))
2281 service_enter_dead(s, f, true);
2285 assert_not_reached("Uh, main process died at wrong time.");
2289 } else if (s->control_pid == pid) {
2292 if (s->control_command) {
2293 exec_status_exit(&s->control_command->exec_status,
2294 &s->exec_context, pid, code, status);
2296 if (s->control_command->ignore)
2297 f = SERVICE_SUCCESS;
2300 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2301 "%s: control process exited, code=%s status=%i",
2302 u->id, sigchld_code_to_string(code), status);
2304 if (f != SERVICE_SUCCESS)
2307 /* Immediately get rid of the cgroup, so that the
2308 * kernel doesn't delay the cgroup empty messages for
2309 * the service cgroup any longer than necessary */
2310 service_kill_control_processes(s);
2312 if (s->control_command &&
2313 s->control_command->command_next &&
2314 f == SERVICE_SUCCESS) {
2316 /* There is another command to *
2317 * execute, so let's do that. */
2319 log_debug_unit(u->id,
2320 "%s running next control command for state %s",
2321 u->id, service_state_to_string(s->state));
2322 service_run_next_control(s);
2325 /* No further commands for this step, so let's
2326 * figure out what to do next */
2328 s->control_command = NULL;
2329 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2331 log_debug_unit(u->id,
2332 "%s got final SIGCHLD for state %s",
2333 u->id, service_state_to_string(s->state));
2337 case SERVICE_START_PRE:
2338 if (f == SERVICE_SUCCESS)
2339 service_enter_start(s);
2341 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2345 if (s->type != SERVICE_FORKING)
2346 /* Maybe spurious event due to a reload that changed the type? */
2349 if (f != SERVICE_SUCCESS) {
2350 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2355 bool has_start_post;
2358 /* Let's try to load the pid file here if we can.
2359 * The PID file might actually be created by a START_POST
2360 * script. In that case don't worry if the loading fails. */
2362 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2363 r = service_load_pid_file(s, !has_start_post);
2364 if (!has_start_post && r < 0) {
2365 r = service_demand_pid_file(s);
2366 if (r < 0 || !cgroup_good(s))
2367 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2371 service_search_main_pid(s);
2373 service_enter_start_post(s);
2376 case SERVICE_START_POST:
2377 if (f != SERVICE_SUCCESS) {
2378 service_enter_stop(s, f);
2385 r = service_load_pid_file(s, true);
2387 r = service_demand_pid_file(s);
2388 if (r < 0 || !cgroup_good(s))
2389 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2393 service_search_main_pid(s);
2395 service_enter_running(s, SERVICE_SUCCESS);
2398 case SERVICE_RELOAD:
2399 if (f == SERVICE_SUCCESS) {
2400 service_load_pid_file(s, true);
2401 service_search_main_pid(s);
2404 s->reload_result = f;
2405 service_enter_running(s, SERVICE_SUCCESS);
2409 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2412 case SERVICE_STOP_SIGTERM:
2413 case SERVICE_STOP_SIGKILL:
2414 if (main_pid_good(s) <= 0)
2415 service_enter_stop_post(s, f);
2417 /* If there is still a service
2418 * process around, wait until
2419 * that one quit, too */
2422 case SERVICE_STOP_POST:
2423 case SERVICE_FINAL_SIGTERM:
2424 case SERVICE_FINAL_SIGKILL:
2425 if (main_pid_good(s) <= 0)
2426 service_enter_dead(s, f, true);
2430 assert_not_reached("Uh, control process died at wrong time.");
2435 /* Notify clients about changed exit status */
2436 unit_add_to_dbus_queue(u);
2438 /* We got one SIGCHLD for the service, let's watch all
2439 * processes that are now running of the service, and watch
2440 * that. Among the PIDs we then watch will be children
2441 * reassigned to us, which hopefully allows us to identify
2442 * when all children are gone */
2443 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2444 unit_watch_all_pids(u);
2446 /* If the PID set is empty now, then let's finish this off */
2447 if (set_isempty(u->pids))
2448 service_notify_cgroup_empty_event(u);
2451 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2452 Service *s = SERVICE(userdata);
2455 assert(source == s->timer_event_source);
2459 case SERVICE_START_PRE:
2461 log_warning_unit(UNIT(s)->id,
2462 "%s %s operation timed out. Terminating.",
2464 s->state == SERVICE_START ? "start" : "start-pre");
2465 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2468 case SERVICE_START_POST:
2469 log_warning_unit(UNIT(s)->id,
2470 "%s start-post operation timed out. Stopping.", UNIT(s)->id);
2471 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2474 case SERVICE_RELOAD:
2475 log_warning_unit(UNIT(s)->id,
2476 "%s reload operation timed out. Stopping.", UNIT(s)->id);
2477 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2478 service_enter_running(s, SERVICE_SUCCESS);
2482 log_warning_unit(UNIT(s)->id,
2483 "%s stopping timed out. Terminating.", UNIT(s)->id);
2484 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2487 case SERVICE_STOP_SIGTERM:
2488 if (s->kill_context.send_sigkill) {
2489 log_warning_unit(UNIT(s)->id,
2490 "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
2491 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2493 log_warning_unit(UNIT(s)->id,
2494 "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
2495 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2500 case SERVICE_STOP_SIGKILL:
2501 /* Uh, we sent a SIGKILL and it is still not gone?
2502 * Must be something we cannot kill, so let's just be
2503 * weirded out and continue */
2505 log_warning_unit(UNIT(s)->id,
2506 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
2507 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2510 case SERVICE_STOP_POST:
2511 log_warning_unit(UNIT(s)->id,
2512 "%s stop-post timed out. Terminating.", UNIT(s)->id);
2513 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2516 case SERVICE_FINAL_SIGTERM:
2517 if (s->kill_context.send_sigkill) {
2518 log_warning_unit(UNIT(s)->id,
2519 "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
2520 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2522 log_warning_unit(UNIT(s)->id,
2523 "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.",
2525 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2530 case SERVICE_FINAL_SIGKILL:
2531 log_warning_unit(UNIT(s)->id,
2532 "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
2533 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2536 case SERVICE_AUTO_RESTART:
2537 log_info_unit(UNIT(s)->id,
2538 s->restart_usec > 0 ?
2539 "%s holdoff time over, scheduling restart." :
2540 "%s has no holdoff time, scheduling restart.",
2542 service_enter_restart(s);
2546 assert_not_reached("Timeout at wrong time.");
2552 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2553 Service *s = SERVICE(userdata);
2556 assert(source == s->watchdog_event_source);
2558 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
2559 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
2564 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2565 Service *s = SERVICE(u);
2567 bool notify_dbus = false;
2571 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
2572 u->id, pid, tags && *tags ? tags[0] : "(empty)");
2574 if (s->notify_access == NOTIFY_NONE) {
2575 log_warning_unit(u->id,
2576 "%s: Got notification message from PID "PID_FMT", but reception is disabled.",
2581 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2583 if (s->main_pid != 0)
2584 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);
2586 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);
2590 /* Interpret MAINPID= */
2591 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2592 (s->state == SERVICE_START ||
2593 s->state == SERVICE_START_POST ||
2594 s->state == SERVICE_RUNNING ||
2595 s->state == SERVICE_RELOAD)) {
2597 if (parse_pid(e + 8, &pid) < 0)
2598 log_warning_unit(u->id, "Failed to parse notification message %s", e);
2600 log_debug_unit(u->id, "%s: got %s", u->id, e);
2601 service_set_main_pid(s, pid);
2602 unit_watch_pid(UNIT(s), pid);
2607 /* Interpret READY= */
2608 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
2609 log_debug_unit(u->id, "%s: got READY=1", u->id);
2610 service_enter_start_post(s);
2614 /* Interpret STATUS= */
2615 e = strv_find_prefix(tags, "STATUS=");
2620 if (!utf8_is_valid(e+7)) {
2621 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
2625 log_debug_unit(u->id, "%s: got %s", u->id, e);
2636 if (!streq_ptr(s->status_text, t)) {
2637 free(s->status_text);
2644 /* Interpret WATCHDOG= */
2645 if (strv_find(tags, "WATCHDOG=1")) {
2646 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
2647 service_reset_watchdog(s);
2650 /* Notify clients about changed status or main pid */
2652 unit_add_to_dbus_queue(u);
2655 static int service_get_timeout(Unit *u, uint64_t *timeout) {
2656 Service *s = SERVICE(u);
2659 if (!s->timer_event_source)
2662 r = sd_event_source_get_time(s->timer_event_source, timeout);
2669 static void service_bus_name_owner_change(
2672 const char *old_owner,
2673 const char *new_owner) {
2675 Service *s = SERVICE(u);
2681 assert(streq(s->bus_name, name));
2682 assert(old_owner || new_owner);
2684 if (old_owner && new_owner)
2685 log_debug_unit(u->id,
2686 "%s's D-Bus name %s changed owner from %s to %s",
2687 u->id, name, old_owner, new_owner);
2689 log_debug_unit(u->id,
2690 "%s's D-Bus name %s no longer registered by %s",
2691 u->id, name, old_owner);
2693 log_debug_unit(u->id,
2694 "%s's D-Bus name %s now registered by %s",
2695 u->id, name, new_owner);
2697 s->bus_name_good = !!new_owner;
2699 if (s->type == SERVICE_DBUS) {
2701 /* service_enter_running() will figure out what to
2703 if (s->state == SERVICE_RUNNING)
2704 service_enter_running(s, SERVICE_SUCCESS);
2705 else if (s->state == SERVICE_START && new_owner)
2706 service_enter_start_post(s);
2708 } else if (new_owner &&
2710 (s->state == SERVICE_START ||
2711 s->state == SERVICE_START_POST ||
2712 s->state == SERVICE_RUNNING ||
2713 s->state == SERVICE_RELOAD)) {
2715 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
2718 /* Try to acquire PID from bus service */
2720 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
2722 r = sd_bus_creds_get_pid(creds, &pid);
2724 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
2726 service_set_main_pid(s, pid);
2727 unit_watch_pid(UNIT(s), pid);
2732 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2733 _cleanup_free_ char *peer = NULL;
2739 /* This is called by the socket code when instantiating a new
2740 * service for a stream socket and the socket needs to be
2743 if (UNIT(s)->load_state != UNIT_LOADED)
2746 if (s->socket_fd >= 0)
2749 if (s->state != SERVICE_DEAD)
2752 if (getpeername_pretty(fd, &peer) >= 0) {
2754 if (UNIT(s)->description) {
2755 _cleanup_free_ char *a;
2757 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2761 r = unit_set_description(UNIT(s), a);
2763 r = unit_set_description(UNIT(s), peer);
2771 unit_ref_set(&s->accept_socket, UNIT(sock));
2773 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
2776 static void service_reset_failed(Unit *u) {
2777 Service *s = SERVICE(u);
2781 if (s->state == SERVICE_FAILED)
2782 service_set_state(s, SERVICE_DEAD);
2784 s->result = SERVICE_SUCCESS;
2785 s->reload_result = SERVICE_SUCCESS;
2787 RATELIMIT_RESET(s->start_limit);
2790 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2791 Service *s = SERVICE(u);
2793 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
2796 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2797 [SERVICE_DEAD] = "dead",
2798 [SERVICE_START_PRE] = "start-pre",
2799 [SERVICE_START] = "start",
2800 [SERVICE_START_POST] = "start-post",
2801 [SERVICE_RUNNING] = "running",
2802 [SERVICE_EXITED] = "exited",
2803 [SERVICE_RELOAD] = "reload",
2804 [SERVICE_STOP] = "stop",
2805 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2806 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2807 [SERVICE_STOP_POST] = "stop-post",
2808 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2809 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2810 [SERVICE_FAILED] = "failed",
2811 [SERVICE_AUTO_RESTART] = "auto-restart",
2814 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2816 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2817 [SERVICE_RESTART_NO] = "no",
2818 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
2819 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
2820 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
2821 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
2822 [SERVICE_RESTART_ON_ABORT] = "on-abort",
2823 [SERVICE_RESTART_ALWAYS] = "always",
2826 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2828 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2829 [SERVICE_SIMPLE] = "simple",
2830 [SERVICE_FORKING] = "forking",
2831 [SERVICE_ONESHOT] = "oneshot",
2832 [SERVICE_DBUS] = "dbus",
2833 [SERVICE_NOTIFY] = "notify",
2834 [SERVICE_IDLE] = "idle"
2837 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2839 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2840 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2841 [SERVICE_EXEC_START] = "ExecStart",
2842 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2843 [SERVICE_EXEC_RELOAD] = "ExecReload",
2844 [SERVICE_EXEC_STOP] = "ExecStop",
2845 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2848 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2850 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2851 [NOTIFY_NONE] = "none",
2852 [NOTIFY_MAIN] = "main",
2853 [NOTIFY_ALL] = "all"
2856 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2858 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
2859 [SERVICE_SUCCESS] = "success",
2860 [SERVICE_FAILURE_RESOURCES] = "resources",
2861 [SERVICE_FAILURE_TIMEOUT] = "timeout",
2862 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
2863 [SERVICE_FAILURE_SIGNAL] = "signal",
2864 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
2865 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
2866 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
2869 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
2871 static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
2872 [SERVICE_FAILURE_ACTION_NONE] = "none",
2873 [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
2874 [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
2875 [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
2877 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
2879 const UnitVTable service_vtable = {
2880 .object_size = sizeof(Service),
2881 .exec_context_offset = offsetof(Service, exec_context),
2882 .cgroup_context_offset = offsetof(Service, cgroup_context),
2883 .kill_context_offset = offsetof(Service, kill_context),
2884 .exec_runtime_offset = offsetof(Service, exec_runtime),
2890 .private_section = "Service",
2892 .init = service_init,
2893 .done = service_done,
2894 .load = service_load,
2896 .coldplug = service_coldplug,
2898 .dump = service_dump,
2900 .start = service_start,
2901 .stop = service_stop,
2902 .reload = service_reload,
2904 .can_reload = service_can_reload,
2906 .kill = service_kill,
2908 .serialize = service_serialize,
2909 .deserialize_item = service_deserialize_item,
2911 .active_state = service_active_state,
2912 .sub_state_to_string = service_sub_state_to_string,
2914 .check_gc = service_check_gc,
2915 .check_snapshot = service_check_snapshot,
2917 .sigchld_event = service_sigchld_event,
2919 .reset_failed = service_reset_failed,
2921 .notify_cgroup_empty = service_notify_cgroup_empty_event,
2922 .notify_message = service_notify_message,
2924 .bus_name_owner_change = service_bus_name_owner_change,
2926 .bus_interface = "org.freedesktop.systemd1.Service",
2927 .bus_vtable = bus_service_vtable,
2928 .bus_set_property = bus_service_set_property,
2929 .bus_commit_properties = bus_service_commit_properties,
2931 .get_timeout = service_get_timeout,
2932 .can_transient = true,
2934 .status_message_formats = {
2935 .starting_stopping = {
2936 [0] = "Starting %s...",
2937 [1] = "Stopping %s...",
2939 .finished_start_job = {
2940 [JOB_DONE] = "Started %s.",
2941 [JOB_FAILED] = "Failed to start %s.",
2942 [JOB_DEPENDENCY] = "Dependency failed for %s.",
2943 [JOB_TIMEOUT] = "Timed out starting %s.",
2945 .finished_stop_job = {
2946 [JOB_DONE] = "Stopped %s.",
2947 [JOB_FAILED] = "Stopped (with error) %s.",
2948 [JOB_TIMEOUT] = "Timed out stopping %s.",