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", UNIT(s)->id, s->pid_file_pathspec->path);
139 path_spec_unwatch(s->pid_file_pathspec);
140 path_spec_done(s->pid_file_pathspec);
141 free(s->pid_file_pathspec);
142 s->pid_file_pathspec = NULL;
145 static int service_set_main_pid(Service *s, pid_t pid) {
156 if (s->main_pid == pid && s->main_pid_known)
159 if (s->main_pid != pid) {
160 service_unwatch_main_pid(s);
161 exec_status_start(&s->main_exec_status, pid);
165 s->main_pid_known = true;
167 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
168 log_warning_unit(UNIT(s)->id, "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", UNIT(s)->id, pid);
169 s->main_pid_alien = true;
171 s->main_pid_alien = false;
176 static void service_close_socket_fd(Service *s) {
179 s->socket_fd = asynchronous_close(s->socket_fd);
182 static void service_connection_unref(Service *s) {
185 if (!UNIT_ISSET(s->accept_socket))
188 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
189 unit_ref_unset(&s->accept_socket);
192 static void service_stop_watchdog(Service *s) {
195 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
196 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
199 static void service_start_watchdog(Service *s) {
204 if (s->watchdog_usec <= 0)
207 if (s->watchdog_event_source) {
208 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
210 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
214 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
216 r = sd_event_add_time(
217 UNIT(s)->manager->event,
218 &s->watchdog_event_source,
220 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
221 service_dispatch_watchdog, s);
223 log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
227 /* Let's process everything else which might be a sign
228 * of living before we consider a service died. */
229 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
233 log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
236 static void service_reset_watchdog(Service *s) {
239 dual_timestamp_get(&s->watchdog_timestamp);
240 service_start_watchdog(s);
243 static void service_done(Unit *u) {
244 Service *s = SERVICE(u);
251 free(s->status_text);
252 s->status_text = NULL;
255 s->reboot_arg = NULL;
257 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
258 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
259 s->control_command = NULL;
260 s->main_command = NULL;
262 exit_status_set_free(&s->restart_prevent_status);
263 exit_status_set_free(&s->restart_force_status);
264 exit_status_set_free(&s->success_status);
266 /* This will leak a process, but at least no memory or any of
268 service_unwatch_main_pid(s);
269 service_unwatch_control_pid(s);
270 service_unwatch_pid_file(s);
273 unit_unwatch_bus_name(u, s->bus_name);
278 service_close_socket_fd(s);
279 service_connection_unref(s);
281 unit_ref_unset(&s->accept_socket);
283 service_stop_watchdog(s);
285 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
288 static int service_arm_timer(Service *s, usec_t usec) {
293 if (s->timer_event_source) {
294 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
298 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
301 return sd_event_add_time(
302 UNIT(s)->manager->event,
303 &s->timer_event_source,
305 now(CLOCK_MONOTONIC) + usec, 0,
306 service_dispatch_timer, s);
309 static int service_verify(Service *s) {
312 if (UNIT(s)->load_state != UNIT_LOADED)
315 if (!s->exec_command[SERVICE_EXEC_START]) {
316 log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
320 if (s->type != SERVICE_ONESHOT &&
321 s->exec_command[SERVICE_EXEC_START]->command_next) {
322 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);
326 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
327 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);
331 if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) {
332 log_error_unit(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
336 if (s->type == SERVICE_DBUS && !s->bus_name) {
337 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);
341 if (s->bus_name && s->type != SERVICE_DBUS)
342 log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
344 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
345 log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id);
352 static int service_add_default_dependencies(Service *s) {
357 /* Add a number of automatic dependencies useful for the
358 * majority of services. */
360 /* First, pull in base system */
361 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true);
365 /* Second, activate normal shutdown */
366 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
370 static void service_fix_output(Service *s) {
373 /* If nothing has been explicitly configured, patch default
374 * output in. If input is socket/tty we avoid this however,
375 * since in that case we want output to default to the same
376 * place as we read input from. */
378 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
379 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
380 s->exec_context.std_input == EXEC_INPUT_NULL)
381 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
383 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
384 s->exec_context.std_input == EXEC_INPUT_NULL)
385 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
388 static int service_load(Unit *u) {
389 Service *s = SERVICE(u);
394 /* Load a .service file */
395 r = unit_load_fragment(u);
399 /* Still nothing found? Then let's give up */
400 if (u->load_state == UNIT_STUB)
403 /* This is a new unit? Then let's add in some extras */
404 if (u->load_state == UNIT_LOADED) {
406 /* We were able to load something, then let's add in
407 * the dropin directories. */
408 r = unit_load_dropin(u);
412 if (s->type == _SERVICE_TYPE_INVALID)
413 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
415 /* Oneshot services have disabled start timeout by default */
416 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
417 s->timeout_start_usec = 0;
419 service_fix_output(s);
421 r = unit_patch_contexts(u);
425 r = unit_add_exec_dependencies(u, &s->exec_context);
429 r = unit_add_default_slice(u, &s->cgroup_context);
433 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
434 s->notify_access = NOTIFY_MAIN;
436 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
437 s->notify_access = NOTIFY_MAIN;
440 r = unit_watch_bus_name(u, s->bus_name);
445 if (u->default_dependencies) {
446 r = service_add_default_dependencies(s);
453 return service_verify(s);
456 static void service_dump(Unit *u, FILE *f, const char *prefix) {
457 ServiceExecCommand c;
458 Service *s = SERVICE(u);
463 prefix = strempty(prefix);
464 prefix2 = strappenda(prefix, "\t");
467 "%sService State: %s\n"
469 "%sReload Result: %s\n"
470 "%sPermissionsStartOnly: %s\n"
471 "%sRootDirectoryStartOnly: %s\n"
472 "%sRemainAfterExit: %s\n"
473 "%sGuessMainPID: %s\n"
476 "%sNotifyAccess: %s\n",
477 prefix, service_state_to_string(s->state),
478 prefix, service_result_to_string(s->result),
479 prefix, service_result_to_string(s->reload_result),
480 prefix, yes_no(s->permissions_start_only),
481 prefix, yes_no(s->root_directory_start_only),
482 prefix, yes_no(s->remain_after_exit),
483 prefix, yes_no(s->guess_main_pid),
484 prefix, service_type_to_string(s->type),
485 prefix, service_restart_to_string(s->restart),
486 prefix, notify_access_to_string(s->notify_access));
488 if (s->control_pid > 0)
490 "%sControl PID: "PID_FMT"\n",
491 prefix, s->control_pid);
495 "%sMain PID: "PID_FMT"\n"
496 "%sMain PID Known: %s\n"
497 "%sMain PID Alien: %s\n",
499 prefix, yes_no(s->main_pid_known),
500 prefix, yes_no(s->main_pid_alien));
505 prefix, s->pid_file);
510 "%sBus Name Good: %s\n",
512 prefix, yes_no(s->bus_name_good));
514 kill_context_dump(&s->kill_context, f, prefix);
515 exec_context_dump(&s->exec_context, f, prefix);
517 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
519 if (!s->exec_command[c])
522 fprintf(f, "%s-> %s:\n",
523 prefix, service_exec_command_to_string(c));
525 exec_command_dump_list(s->exec_command[c], f, prefix2);
528 #ifdef HAVE_SYSV_COMPAT
529 if (s->sysv_start_priority >= 0)
531 "%sSysVStartPriority: %i\n",
532 prefix, s->sysv_start_priority);
536 fprintf(f, "%sStatus Text: %s\n",
537 prefix, s->status_text);
540 static int service_load_pid_file(Service *s, bool may_warn) {
541 _cleanup_free_ char *k = NULL;
550 r = read_one_line_file(s->pid_file, &k);
553 log_info_unit(UNIT(s)->id, "PID file %s not readable (yet?) after %s.", s->pid_file, service_state_to_string(s->state));
557 r = parse_pid(k, &pid);
560 log_info_unit(UNIT(s)->id, "Failed to read PID from file %s: %s", s->pid_file, strerror(-r));
564 if (!pid_is_alive(pid)) {
566 log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
570 if (s->main_pid_known) {
571 if (pid == s->main_pid)
574 log_debug_unit(UNIT(s)->id, "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid);
576 service_unwatch_main_pid(s);
577 s->main_pid_known = false;
579 log_debug_unit(UNIT(s)->id, "Main PID loaded: "PID_FMT, pid);
581 r = service_set_main_pid(s, pid);
585 r = unit_watch_pid(UNIT(s), pid);
587 /* FIXME: we need to do something here */
588 log_warning_unit(UNIT(s)->id, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
595 static int service_search_main_pid(Service *s) {
601 /* If we know it anyway, don't ever fallback to unreliable
603 if (s->main_pid_known)
606 if (!s->guess_main_pid)
609 assert(s->main_pid <= 0);
611 pid = unit_search_main_pid(UNIT(s));
615 log_debug_unit(UNIT(s)->id, "Main PID guessed: "PID_FMT, pid);
616 r = service_set_main_pid(s, pid);
620 r = unit_watch_pid(UNIT(s), pid);
622 /* FIXME: we need to do something here */
623 log_warning_unit(UNIT(s)->id, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
630 static void service_set_state(Service *s, ServiceState state) {
631 ServiceState old_state;
632 const UnitActiveState *table;
636 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
638 old_state = s->state;
641 service_unwatch_pid_file(s);
644 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
646 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
648 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
649 SERVICE_AUTO_RESTART))
650 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
653 SERVICE_START, SERVICE_START_POST,
654 SERVICE_RUNNING, SERVICE_RELOAD,
655 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
657 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
658 service_unwatch_main_pid(s);
659 s->main_command = NULL;
663 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
665 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
667 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
668 service_unwatch_control_pid(s);
669 s->control_command = NULL;
670 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
673 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
674 unit_unwatch_all_pids(UNIT(s));
677 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
678 SERVICE_RUNNING, SERVICE_RELOAD,
679 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
680 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
681 !(state == SERVICE_DEAD && UNIT(s)->job)) {
682 service_close_socket_fd(s);
683 service_connection_unref(s);
686 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
687 service_stop_watchdog(s);
689 /* For the inactive states unit_notify() will trim the cgroup,
690 * but for exit we have to do that ourselves... */
691 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
692 unit_destroy_cgroup(UNIT(s));
694 /* For remain_after_exit services, let's see if we can "release" the
695 * hold on the console, since unit_notify() only does that in case of
697 if (state == SERVICE_EXITED &&
698 s->remain_after_exit &&
699 UNIT(s)->manager->n_on_console > 0) {
703 ec = unit_get_exec_context(UNIT(s));
704 if (ec && exec_context_may_touch_console(ec)) {
705 Manager *m = UNIT(s)->manager;
708 if (m->n_on_console == 0)
709 /* unset no_console_output flag, since the console is free */
710 m->no_console_output = false;
714 if (old_state != state)
715 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
717 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
718 s->reload_result = SERVICE_SUCCESS;
721 static int service_coldplug(Unit *u) {
722 Service *s = SERVICE(u);
726 assert(s->state == SERVICE_DEAD);
728 if (s->deserialized_state != s->state) {
730 if (IN_SET(s->deserialized_state,
731 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
733 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
735 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
739 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
741 /* For the start/stop timeouts 0 means off */
743 r = service_arm_timer(s, k);
749 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
751 /* The restart timeouts 0 means immediately */
752 r = service_arm_timer(s, s->restart_usec);
757 if (pid_is_unwaited(s->main_pid) &&
758 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
759 IN_SET(s->deserialized_state,
760 SERVICE_START, SERVICE_START_POST,
761 SERVICE_RUNNING, SERVICE_RELOAD,
762 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
764 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
765 r = unit_watch_pid(UNIT(s), s->main_pid);
770 if (pid_is_unwaited(s->control_pid) &&
771 IN_SET(s->deserialized_state,
772 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
774 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
776 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
777 r = unit_watch_pid(UNIT(s), s->control_pid);
782 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
783 unit_watch_all_pids(UNIT(s));
785 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
786 service_start_watchdog(s);
788 service_set_state(s, s->deserialized_state);
794 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
805 if (s->socket_fd >= 0)
808 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
813 if (u->type != UNIT_SOCKET)
818 r = socket_collect_fds(sock, &cfds, &cn_fds);
831 t = new(int, rn_fds+cn_fds);
838 memcpy(t, rfds, rn_fds * sizeof(int));
839 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
844 rn_fds = rn_fds+cn_fds;
859 static int service_spawn(
864 bool apply_permissions,
866 bool apply_tty_stdin,
867 bool set_notify_socket,
874 _cleanup_free_ int *fdsbuf = NULL;
875 unsigned n_fds = 0, n_env = 0;
876 _cleanup_strv_free_ char
877 **argv = NULL, **final_env = NULL, **our_env = NULL;
884 unit_realize_cgroup(UNIT(s));
886 r = unit_setup_exec_runtime(UNIT(s));
891 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
892 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
893 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
895 if (s->socket_fd >= 0) {
899 r = service_collect_fds(s, &fdsbuf, &n_fds);
907 if (timeout && s->timeout_start_usec > 0) {
908 r = service_arm_timer(s, s->timeout_start_usec);
912 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
914 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
918 our_env = new0(char*, 4);
924 if (set_notify_socket)
925 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
931 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
936 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
937 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
942 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
948 if (is_control && UNIT(s)->cgroup_path) {
949 path = strappenda(UNIT(s)->cgroup_path, "/control");
950 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
952 path = UNIT(s)->cgroup_path;
962 UNIT(s)->manager->confirm_spawn,
963 UNIT(s)->manager->cgroup_supported,
965 manager_get_runtime_prefix(UNIT(s)->manager),
968 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
974 r = unit_watch_pid(UNIT(s), pid);
976 /* FIXME: we need to do something here */
985 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
990 static int main_pid_good(Service *s) {
993 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
996 /* If we know the pid file, then lets just check if it is
998 if (s->main_pid_known) {
1000 /* If it's an alien child let's check if it is still
1002 if (s->main_pid_alien && s->main_pid > 0)
1003 return pid_is_alive(s->main_pid);
1005 /* .. otherwise assume we'll get a SIGCHLD for it,
1006 * which we really should wait for to collect exit
1007 * status and code */
1008 return s->main_pid > 0;
1011 /* We don't know the pid */
1015 _pure_ static int control_pid_good(Service *s) {
1018 return s->control_pid > 0;
1021 static int cgroup_good(Service *s) {
1026 if (!UNIT(s)->cgroup_path)
1029 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1036 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
1038 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1042 if (f != SERVICE_SUCCESS)
1045 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1047 if (s->result != SERVICE_SUCCESS)
1048 service_execute_action(s, s->failure_action, "failed", false);
1050 if (allow_restart &&
1051 !s->forbid_restart &&
1052 (s->restart == SERVICE_RESTART_ALWAYS ||
1053 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1054 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1055 (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
1056 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1057 (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) ||
1058 (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) ||
1059 (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)))) &&
1060 (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) &&
1061 (!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)))) {
1063 r = service_arm_timer(s, s->restart_usec);
1067 service_set_state(s, SERVICE_AUTO_RESTART);
1070 s->forbid_restart = false;
1072 /* We want fresh tmpdirs in case service is started again immediately */
1073 exec_runtime_destroy(s->exec_runtime);
1074 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1076 /* Also, remove the runtime directory in */
1077 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1079 /* Try to delete the pid file. At this point it will be
1080 * out-of-date, and some software might be confused by it, so
1081 * let's remove it. */
1083 unlink_noerrno(s->pid_file);
1088 log_warning_unit(UNIT(s)->id, "%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1089 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1092 static void service_enter_stop_post(Service *s, ServiceResult f) {
1096 if (f != SERVICE_SUCCESS)
1099 service_unwatch_control_pid(s);
1100 unit_watch_all_pids(UNIT(s));
1102 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1103 if (s->control_command) {
1104 s->control_command_id = SERVICE_EXEC_STOP_POST;
1106 r = service_spawn(s,
1110 !s->permissions_start_only,
1111 !s->root_directory_start_only,
1119 service_set_state(s, SERVICE_STOP_POST);
1121 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1126 log_warning_unit(UNIT(s)->id, "%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1127 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1130 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1135 if (f != SERVICE_SUCCESS)
1138 unit_watch_all_pids(UNIT(s));
1140 r = unit_kill_context(
1143 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1152 if (s->timeout_stop_usec > 0) {
1153 r = service_arm_timer(s, s->timeout_stop_usec);
1158 service_set_state(s, state);
1159 } else if (state == SERVICE_STOP_SIGTERM)
1160 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1161 else if (state == SERVICE_STOP_SIGKILL)
1162 service_enter_stop_post(s, SERVICE_SUCCESS);
1163 else if (state == SERVICE_FINAL_SIGTERM)
1164 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1166 service_enter_dead(s, SERVICE_SUCCESS, true);
1171 log_warning_unit(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1173 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1174 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1176 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1179 static void service_enter_stop(Service *s, ServiceResult f) {
1184 if (f != SERVICE_SUCCESS)
1187 service_unwatch_control_pid(s);
1188 unit_watch_all_pids(UNIT(s));
1190 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1191 if (s->control_command) {
1192 s->control_command_id = SERVICE_EXEC_STOP;
1194 r = service_spawn(s,
1198 !s->permissions_start_only,
1199 !s->root_directory_start_only,
1207 service_set_state(s, SERVICE_STOP);
1209 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1214 log_warning_unit(UNIT(s)->id, "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
1215 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1218 static void service_enter_running(Service *s, ServiceResult f) {
1219 int main_pid_ok, cgroup_ok;
1222 if (f != SERVICE_SUCCESS)
1225 main_pid_ok = main_pid_good(s);
1226 cgroup_ok = cgroup_good(s);
1228 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1229 (s->bus_name_good || s->type != SERVICE_DBUS))
1230 service_set_state(s, SERVICE_RUNNING);
1231 else if (s->remain_after_exit)
1232 service_set_state(s, SERVICE_EXITED);
1234 service_enter_stop(s, SERVICE_SUCCESS);
1237 static void service_enter_start_post(Service *s) {
1241 service_unwatch_control_pid(s);
1242 service_reset_watchdog(s);
1244 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1245 if (s->control_command) {
1246 s->control_command_id = SERVICE_EXEC_START_POST;
1248 r = service_spawn(s,
1252 !s->permissions_start_only,
1253 !s->root_directory_start_only,
1261 service_set_state(s, SERVICE_START_POST);
1263 service_enter_running(s, SERVICE_SUCCESS);
1268 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
1269 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1272 static void service_kill_control_processes(Service *s) {
1275 if (!UNIT(s)->cgroup_path)
1278 p = strappenda(UNIT(s)->cgroup_path, "/control");
1279 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1282 static void service_enter_start(Service *s) {
1289 assert(s->exec_command[SERVICE_EXEC_START]);
1290 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1292 service_unwatch_control_pid(s);
1293 service_unwatch_main_pid(s);
1295 /* We want to ensure that nobody leaks processes from
1296 * START_PRE here, so let's go on a killing spree, People
1297 * should not spawn long running processes from START_PRE. */
1298 service_kill_control_processes(s);
1300 if (s->type == SERVICE_FORKING) {
1301 s->control_command_id = SERVICE_EXEC_START;
1302 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1304 s->main_command = NULL;
1306 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1307 s->control_command = NULL;
1309 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1312 r = service_spawn(s,
1314 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
1315 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
1320 s->notify_access != NOTIFY_NONE,
1326 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
1327 /* For simple services we immediately start
1328 * the START_POST binaries. */
1330 service_set_main_pid(s, pid);
1331 service_enter_start_post(s);
1333 } else if (s->type == SERVICE_FORKING) {
1335 /* For forking services we wait until the start
1336 * process exited. */
1338 s->control_pid = pid;
1339 service_set_state(s, SERVICE_START);
1341 } else if (s->type == SERVICE_ONESHOT ||
1342 s->type == SERVICE_DBUS ||
1343 s->type == SERVICE_NOTIFY) {
1345 /* For oneshot services we wait until the start
1346 * process exited, too, but it is our main process. */
1348 /* For D-Bus services we know the main pid right away,
1349 * but wait for the bus name to appear on the
1350 * bus. Notify services are similar. */
1352 service_set_main_pid(s, pid);
1353 service_set_state(s, SERVICE_START);
1355 assert_not_reached("Unknown service type");
1360 log_warning_unit(UNIT(s)->id, "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
1361 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1364 static void service_enter_start_pre(Service *s) {
1369 service_unwatch_control_pid(s);
1371 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1372 if (s->control_command) {
1373 /* Before we start anything, let's clear up what might
1374 * be left from previous runs. */
1375 service_kill_control_processes(s);
1377 s->control_command_id = SERVICE_EXEC_START_PRE;
1379 r = service_spawn(s,
1383 !s->permissions_start_only,
1384 !s->root_directory_start_only,
1392 service_set_state(s, SERVICE_START_PRE);
1394 service_enter_start(s);
1399 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
1400 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1403 static void service_enter_restart(Service *s) {
1404 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1409 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1410 /* Don't restart things if we are going down anyway */
1411 log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart.");
1413 r = service_arm_timer(s, s->restart_usec);
1420 /* Any units that are bound to this service must also be
1421 * restarted. We use JOB_RESTART (instead of the more obvious
1422 * JOB_START) here so that those dependency jobs will be added
1424 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1428 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1429 * it will be canceled as part of the service_stop() call that
1430 * is executed as part of JOB_RESTART. */
1432 log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id);
1436 log_warning_unit(UNIT(s)->id,
1437 "%s failed to schedule restart job: %s",
1438 UNIT(s)->id, bus_error_message(&error, -r));
1439 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1442 static void service_enter_reload(Service *s) {
1447 service_unwatch_control_pid(s);
1449 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1450 if (s->control_command) {
1451 s->control_command_id = SERVICE_EXEC_RELOAD;
1453 r = service_spawn(s,
1457 !s->permissions_start_only,
1458 !s->root_directory_start_only,
1466 service_set_state(s, SERVICE_RELOAD);
1468 service_enter_running(s, SERVICE_SUCCESS);
1473 log_warning_unit(UNIT(s)->id, "%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
1474 s->reload_result = SERVICE_FAILURE_RESOURCES;
1475 service_enter_running(s, SERVICE_SUCCESS);
1478 static void service_run_next_control(Service *s) {
1482 assert(s->control_command);
1483 assert(s->control_command->command_next);
1485 assert(s->control_command_id != SERVICE_EXEC_START);
1487 s->control_command = s->control_command->command_next;
1488 service_unwatch_control_pid(s);
1490 r = service_spawn(s,
1494 !s->permissions_start_only,
1495 !s->root_directory_start_only,
1496 s->control_command_id == SERVICE_EXEC_START_PRE ||
1497 s->control_command_id == SERVICE_EXEC_STOP_POST,
1507 log_warning_unit(UNIT(s)->id, "%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
1509 if (s->state == SERVICE_START_PRE)
1510 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1511 else if (s->state == SERVICE_STOP)
1512 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1513 else if (s->state == SERVICE_STOP_POST)
1514 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1515 else if (s->state == SERVICE_RELOAD) {
1516 s->reload_result = SERVICE_FAILURE_RESOURCES;
1517 service_enter_running(s, SERVICE_SUCCESS);
1519 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1522 static void service_run_next_main(Service *s) {
1527 assert(s->main_command);
1528 assert(s->main_command->command_next);
1529 assert(s->type == SERVICE_ONESHOT);
1531 s->main_command = s->main_command->command_next;
1532 service_unwatch_main_pid(s);
1534 r = service_spawn(s,
1541 s->notify_access != NOTIFY_NONE,
1547 service_set_main_pid(s, pid);
1552 log_warning_unit(UNIT(s)->id, "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
1553 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1556 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
1559 if (action == SERVICE_FAILURE_ACTION_REBOOT ||
1560 action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
1561 update_reboot_param_file(s->reboot_arg);
1565 case SERVICE_FAILURE_ACTION_NONE:
1566 if (log_action_none)
1567 log_warning_unit(UNIT(s)->id, "%s %s, refusing to start.", UNIT(s)->id, reason);
1570 case SERVICE_FAILURE_ACTION_REBOOT: {
1571 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1574 log_warning_unit(UNIT(s)->id, "%s %s, rebooting.", UNIT(s)->id, reason);
1576 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
1578 log_error_unit(UNIT(s)->id, "Failed to reboot: %s.", bus_error_message(&error, r));
1583 case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
1584 log_warning_unit(UNIT(s)->id, "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
1585 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
1588 case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
1589 log_warning_unit(UNIT(s)->id, "%s %s, rebooting immediately.", UNIT(s)->id, reason);
1593 if (s->reboot_arg) {
1594 log_info("Rebooting with argument '%s'.", s->reboot_arg);
1595 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
1598 log_info("Rebooting.");
1599 reboot(RB_AUTOBOOT);
1603 log_error_unit(UNIT(s)->id, "failure action=%i", action);
1604 assert_not_reached("Unknown FailureAction.");
1610 static int service_start_limit_test(Service *s) {
1613 if (ratelimit_test(&s->start_limit))
1616 return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
1619 static int service_start(Unit *u) {
1620 Service *s = SERVICE(u);
1625 /* We cannot fulfill this request right now, try again later
1627 if (s->state == SERVICE_STOP ||
1628 s->state == SERVICE_STOP_SIGTERM ||
1629 s->state == SERVICE_STOP_SIGKILL ||
1630 s->state == SERVICE_STOP_POST ||
1631 s->state == SERVICE_FINAL_SIGTERM ||
1632 s->state == SERVICE_FINAL_SIGKILL)
1635 /* Already on it! */
1636 if (s->state == SERVICE_START_PRE ||
1637 s->state == SERVICE_START ||
1638 s->state == SERVICE_START_POST)
1641 /* A service that will be restarted must be stopped first to
1642 * trigger BindsTo and/or OnFailure dependencies. If a user
1643 * does not want to wait for the holdoff time to elapse, the
1644 * service should be manually restarted, not started. We
1645 * simply return EAGAIN here, so that any start jobs stay
1646 * queued, and assume that the auto restart timer will
1647 * eventually trigger the restart. */
1648 if (s->state == SERVICE_AUTO_RESTART)
1651 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
1653 /* Make sure we don't enter a busy loop of some kind. */
1654 r = service_start_limit_test(s);
1656 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
1660 s->result = SERVICE_SUCCESS;
1661 s->reload_result = SERVICE_SUCCESS;
1662 s->main_pid_known = false;
1663 s->main_pid_alien = false;
1664 s->forbid_restart = false;
1666 free(s->status_text);
1667 s->status_text = NULL;
1668 s->status_errno = 0;
1670 service_enter_start_pre(s);
1674 static int service_stop(Unit *u) {
1675 Service *s = SERVICE(u);
1679 /* Don't create restart jobs from here. */
1680 s->forbid_restart = true;
1683 if (s->state == SERVICE_STOP ||
1684 s->state == SERVICE_STOP_SIGTERM ||
1685 s->state == SERVICE_STOP_SIGKILL ||
1686 s->state == SERVICE_STOP_POST ||
1687 s->state == SERVICE_FINAL_SIGTERM ||
1688 s->state == SERVICE_FINAL_SIGKILL)
1691 /* A restart will be scheduled or is in progress. */
1692 if (s->state == SERVICE_AUTO_RESTART) {
1693 service_set_state(s, SERVICE_DEAD);
1697 /* If there's already something running we go directly into
1699 if (s->state == SERVICE_START_PRE ||
1700 s->state == SERVICE_START ||
1701 s->state == SERVICE_START_POST ||
1702 s->state == SERVICE_RELOAD) {
1703 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1707 assert(s->state == SERVICE_RUNNING ||
1708 s->state == SERVICE_EXITED);
1710 service_enter_stop(s, SERVICE_SUCCESS);
1714 static int service_reload(Unit *u) {
1715 Service *s = SERVICE(u);
1719 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1721 service_enter_reload(s);
1725 _pure_ static bool service_can_reload(Unit *u) {
1726 Service *s = SERVICE(u);
1730 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1733 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1734 Service *s = SERVICE(u);
1740 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1741 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1742 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
1744 if (s->control_pid > 0)
1745 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1748 if (s->main_pid_known && s->main_pid > 0)
1749 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
1751 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1754 unit_serialize_item(u, f, "status-text", s->status_text);
1756 /* FIXME: There's a minor uncleanliness here: if there are
1757 * multiple commands attached here, we will start from the
1758 * first one again */
1759 if (s->control_command_id >= 0)
1760 unit_serialize_item(u, f, "control-command",
1761 service_exec_command_to_string(s->control_command_id));
1763 if (s->socket_fd >= 0) {
1766 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1769 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1772 if (s->main_exec_status.pid > 0) {
1773 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1774 s->main_exec_status.pid);
1775 dual_timestamp_serialize(f, "main-exec-status-start",
1776 &s->main_exec_status.start_timestamp);
1777 dual_timestamp_serialize(f, "main-exec-status-exit",
1778 &s->main_exec_status.exit_timestamp);
1780 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
1781 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1782 s->main_exec_status.code);
1783 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1784 s->main_exec_status.status);
1787 if (dual_timestamp_is_set(&s->watchdog_timestamp))
1788 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
1790 if (s->forbid_restart)
1791 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
1796 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1797 Service *s = SERVICE(u);
1804 if (streq(key, "state")) {
1807 state = service_state_from_string(value);
1809 log_debug_unit(u->id, "Failed to parse state value %s", value);
1811 s->deserialized_state = state;
1812 } else if (streq(key, "result")) {
1815 f = service_result_from_string(value);
1817 log_debug_unit(u->id, "Failed to parse result value %s", value);
1818 else if (f != SERVICE_SUCCESS)
1821 } else if (streq(key, "reload-result")) {
1824 f = service_result_from_string(value);
1826 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
1827 else if (f != SERVICE_SUCCESS)
1828 s->reload_result = f;
1830 } else if (streq(key, "control-pid")) {
1833 if (parse_pid(value, &pid) < 0)
1834 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
1836 s->control_pid = pid;
1837 } else if (streq(key, "main-pid")) {
1840 if (parse_pid(value, &pid) < 0)
1841 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
1843 service_set_main_pid(s, pid);
1844 unit_watch_pid(UNIT(s), pid);
1846 } else if (streq(key, "main-pid-known")) {
1849 b = parse_boolean(value);
1851 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
1853 s->main_pid_known = b;
1854 } else if (streq(key, "status-text")) {
1861 free(s->status_text);
1865 } else if (streq(key, "control-command")) {
1866 ServiceExecCommand id;
1868 id = service_exec_command_from_string(value);
1870 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
1872 s->control_command_id = id;
1873 s->control_command = s->exec_command[id];
1875 } else if (streq(key, "socket-fd")) {
1878 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1879 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
1882 asynchronous_close(s->socket_fd);
1883 s->socket_fd = fdset_remove(fds, fd);
1885 } else if (streq(key, "main-exec-status-pid")) {
1888 if (parse_pid(value, &pid) < 0)
1889 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
1891 s->main_exec_status.pid = pid;
1892 } else if (streq(key, "main-exec-status-code")) {
1895 if (safe_atoi(value, &i) < 0)
1896 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
1898 s->main_exec_status.code = i;
1899 } else if (streq(key, "main-exec-status-status")) {
1902 if (safe_atoi(value, &i) < 0)
1903 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
1905 s->main_exec_status.status = i;
1906 } else if (streq(key, "main-exec-status-start"))
1907 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
1908 else if (streq(key, "main-exec-status-exit"))
1909 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
1910 else if (streq(key, "watchdog-timestamp"))
1911 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
1912 else if (streq(key, "forbid-restart")) {
1915 b = parse_boolean(value);
1917 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
1919 s->forbid_restart = b;
1921 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
1926 _pure_ static UnitActiveState service_active_state(Unit *u) {
1927 const UnitActiveState *table;
1931 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1933 return table[SERVICE(u)->state];
1936 static const char *service_sub_state_to_string(Unit *u) {
1939 return service_state_to_string(SERVICE(u)->state);
1942 static bool service_check_gc(Unit *u) {
1943 Service *s = SERVICE(u);
1947 /* Never clean up services that still have a process around,
1948 * even if the service is formally dead. */
1949 if (cgroup_good(s) > 0 ||
1950 main_pid_good(s) > 0 ||
1951 control_pid_good(s) > 0)
1957 _pure_ static bool service_check_snapshot(Unit *u) {
1958 Service *s = SERVICE(u);
1962 return s->socket_fd < 0;
1965 static int service_retry_pid_file(Service *s) {
1968 assert(s->pid_file);
1969 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
1971 r = service_load_pid_file(s, false);
1975 service_unwatch_pid_file(s);
1977 service_enter_running(s, SERVICE_SUCCESS);
1981 static int service_watch_pid_file(Service *s) {
1984 log_debug_unit(UNIT(s)->id, "Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
1986 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
1990 /* the pidfile might have appeared just before we set the watch */
1991 log_debug_unit(UNIT(s)->id, "Trying to read %s's PID file %s in case it changed", UNIT(s)->id, s->pid_file_pathspec->path);
1992 service_retry_pid_file(s);
1996 log_error_unit(UNIT(s)->id, "Failed to set a watch for %s's PID file %s: %s", UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
1997 service_unwatch_pid_file(s);
2001 static int service_demand_pid_file(Service *s) {
2004 assert(s->pid_file);
2005 assert(!s->pid_file_pathspec);
2007 ps = new0(PathSpec, 1);
2012 ps->path = strdup(s->pid_file);
2018 path_kill_slashes(ps->path);
2020 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2021 * keep their PID file open all the time. */
2022 ps->type = PATH_MODIFIED;
2023 ps->inotify_fd = -1;
2025 s->pid_file_pathspec = ps;
2027 return service_watch_pid_file(s);
2030 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2031 PathSpec *p = userdata;
2036 s = SERVICE(p->unit);
2040 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2041 assert(s->pid_file_pathspec);
2042 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2044 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2046 if (path_spec_fd_event(p, events) < 0)
2049 if (service_retry_pid_file(s) == 0)
2052 if (service_watch_pid_file(s) < 0)
2058 service_unwatch_pid_file(s);
2059 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2063 static void service_notify_cgroup_empty_event(Unit *u) {
2064 Service *s = SERVICE(u);
2068 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2072 /* Waiting for SIGCHLD is usually more interesting,
2073 * because it includes return codes/signals. Which is
2074 * why we ignore the cgroup events for most cases,
2075 * except when we don't know pid which to expect the
2079 case SERVICE_START_POST:
2080 /* If we were hoping for the daemon to write its PID file,
2081 * we can give up now. */
2082 if (s->pid_file_pathspec) {
2083 log_warning_unit(u->id, "%s never wrote its PID file. Failing.", UNIT(s)->id);
2085 service_unwatch_pid_file(s);
2086 if (s->state == SERVICE_START)
2087 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2089 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2093 case SERVICE_RUNNING:
2094 /* service_enter_running() will figure out what to do */
2095 service_enter_running(s, SERVICE_SUCCESS);
2098 case SERVICE_STOP_SIGTERM:
2099 case SERVICE_STOP_SIGKILL:
2101 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2102 service_enter_stop_post(s, SERVICE_SUCCESS);
2106 case SERVICE_STOP_POST:
2107 case SERVICE_FINAL_SIGTERM:
2108 case SERVICE_FINAL_SIGKILL:
2109 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2110 service_enter_dead(s, SERVICE_SUCCESS, true);
2119 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2120 Service *s = SERVICE(u);
2126 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2127 is_clean_exit_lsb(code, status, &s->success_status))
2128 f = SERVICE_SUCCESS;
2129 else if (code == CLD_EXITED)
2130 f = SERVICE_FAILURE_EXIT_CODE;
2131 else if (code == CLD_KILLED)
2132 f = SERVICE_FAILURE_SIGNAL;
2133 else if (code == CLD_DUMPED)
2134 f = SERVICE_FAILURE_CORE_DUMP;
2136 assert_not_reached("Unknown code");
2138 if (s->main_pid == pid) {
2139 /* Forking services may occasionally move to a new PID.
2140 * As long as they update the PID file before exiting the old
2141 * PID, they're fine. */
2142 if (service_load_pid_file(s, false) == 0)
2146 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2148 if (s->main_command) {
2149 /* If this is not a forking service than the
2150 * main process got started and hence we copy
2151 * the exit status so that it is recorded both
2152 * as main and as control process exit
2155 s->main_command->exec_status = s->main_exec_status;
2157 if (s->main_command->ignore)
2158 f = SERVICE_SUCCESS;
2159 } else if (s->exec_command[SERVICE_EXEC_START]) {
2161 /* If this is a forked process, then we should
2162 * ignore the return value if this was
2163 * configured for the starter process */
2165 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2166 f = SERVICE_SUCCESS;
2169 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2171 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2172 u->id, sigchld_code_to_string(code), status,
2173 strna(code == CLD_EXITED
2174 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2175 : signal_to_string(status)),
2176 "EXIT_CODE=%s", sigchld_code_to_string(code),
2177 "EXIT_STATUS=%i", status,
2180 if (f != SERVICE_SUCCESS)
2183 if (s->main_command &&
2184 s->main_command->command_next &&
2185 f == SERVICE_SUCCESS) {
2187 /* There is another command to *
2188 * execute, so let's do that. */
2190 log_debug_unit(u->id, "%s running next main command for state %s", u->id, service_state_to_string(s->state));
2191 service_run_next_main(s);
2195 /* The service exited, so the service is officially
2197 s->main_command = NULL;
2201 case SERVICE_START_POST:
2202 case SERVICE_RELOAD:
2204 /* Need to wait until the operation is
2209 if (s->type == SERVICE_ONESHOT) {
2210 /* This was our main goal, so let's go on */
2211 if (f == SERVICE_SUCCESS)
2212 service_enter_start_post(s);
2214 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2220 case SERVICE_RUNNING:
2221 service_enter_running(s, f);
2224 case SERVICE_STOP_SIGTERM:
2225 case SERVICE_STOP_SIGKILL:
2227 if (!control_pid_good(s))
2228 service_enter_stop_post(s, f);
2230 /* If there is still a control process, wait for that first */
2233 case SERVICE_STOP_POST:
2234 case SERVICE_FINAL_SIGTERM:
2235 case SERVICE_FINAL_SIGKILL:
2237 if (!control_pid_good(s))
2238 service_enter_dead(s, f, true);
2242 assert_not_reached("Uh, main process died at wrong time.");
2246 } else if (s->control_pid == pid) {
2249 if (s->control_command) {
2250 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2252 if (s->control_command->ignore)
2253 f = SERVICE_SUCCESS;
2256 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2257 "%s: control process exited, code=%s status=%i",
2258 u->id, sigchld_code_to_string(code), status);
2260 if (f != SERVICE_SUCCESS)
2263 /* Immediately get rid of the cgroup, so that the
2264 * kernel doesn't delay the cgroup empty messages for
2265 * the service cgroup any longer than necessary */
2266 service_kill_control_processes(s);
2268 if (s->control_command &&
2269 s->control_command->command_next &&
2270 f == SERVICE_SUCCESS) {
2272 /* There is another command to *
2273 * execute, so let's do that. */
2275 log_debug_unit(u->id, "%s running next control command for state %s", u->id, service_state_to_string(s->state));
2276 service_run_next_control(s);
2279 /* No further commands for this step, so let's
2280 * figure out what to do next */
2282 s->control_command = NULL;
2283 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2285 log_debug_unit(u->id, "%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2289 case SERVICE_START_PRE:
2290 if (f == SERVICE_SUCCESS)
2291 service_enter_start(s);
2293 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2297 if (s->type != SERVICE_FORKING)
2298 /* Maybe spurious event due to a reload that changed the type? */
2301 if (f != SERVICE_SUCCESS) {
2302 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2307 bool has_start_post;
2310 /* Let's try to load the pid file here if we can.
2311 * The PID file might actually be created by a START_POST
2312 * script. In that case don't worry if the loading fails. */
2314 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2315 r = service_load_pid_file(s, !has_start_post);
2316 if (!has_start_post && r < 0) {
2317 r = service_demand_pid_file(s);
2318 if (r < 0 || !cgroup_good(s))
2319 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2323 service_search_main_pid(s);
2325 service_enter_start_post(s);
2328 case SERVICE_START_POST:
2329 if (f != SERVICE_SUCCESS) {
2330 service_enter_stop(s, f);
2337 r = service_load_pid_file(s, true);
2339 r = service_demand_pid_file(s);
2340 if (r < 0 || !cgroup_good(s))
2341 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2345 service_search_main_pid(s);
2347 service_enter_running(s, SERVICE_SUCCESS);
2350 case SERVICE_RELOAD:
2351 if (f == SERVICE_SUCCESS) {
2352 service_load_pid_file(s, true);
2353 service_search_main_pid(s);
2356 s->reload_result = f;
2357 service_enter_running(s, SERVICE_SUCCESS);
2361 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2364 case SERVICE_STOP_SIGTERM:
2365 case SERVICE_STOP_SIGKILL:
2366 if (main_pid_good(s) <= 0)
2367 service_enter_stop_post(s, f);
2369 /* If there is still a service
2370 * process around, wait until
2371 * that one quit, too */
2374 case SERVICE_STOP_POST:
2375 case SERVICE_FINAL_SIGTERM:
2376 case SERVICE_FINAL_SIGKILL:
2377 if (main_pid_good(s) <= 0)
2378 service_enter_dead(s, f, true);
2382 assert_not_reached("Uh, control process died at wrong time.");
2387 /* Notify clients about changed exit status */
2388 unit_add_to_dbus_queue(u);
2390 /* We got one SIGCHLD for the service, let's watch all
2391 * processes that are now running of the service, and watch
2392 * that. Among the PIDs we then watch will be children
2393 * reassigned to us, which hopefully allows us to identify
2394 * when all children are gone */
2395 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2396 unit_watch_all_pids(u);
2398 /* If the PID set is empty now, then let's finish this off */
2399 if (set_isempty(u->pids))
2400 service_notify_cgroup_empty_event(u);
2403 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2404 Service *s = SERVICE(userdata);
2407 assert(source == s->timer_event_source);
2411 case SERVICE_START_PRE:
2413 log_warning_unit(UNIT(s)->id, "%s %s operation timed out. Terminating.", UNIT(s)->id, s->state == SERVICE_START ? "start" : "start-pre");
2414 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2417 case SERVICE_START_POST:
2418 log_warning_unit(UNIT(s)->id, "%s start-post operation timed out. Stopping.", UNIT(s)->id);
2419 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2422 case SERVICE_RELOAD:
2423 log_warning_unit(UNIT(s)->id, "%s reload operation timed out. Stopping.", UNIT(s)->id);
2424 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2425 service_enter_running(s, SERVICE_SUCCESS);
2429 log_warning_unit(UNIT(s)->id, "%s stopping timed out. Terminating.", UNIT(s)->id);
2430 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2433 case SERVICE_STOP_SIGTERM:
2434 if (s->kill_context.send_sigkill) {
2435 log_warning_unit(UNIT(s)->id, "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
2436 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2438 log_warning_unit(UNIT(s)->id, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
2439 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2444 case SERVICE_STOP_SIGKILL:
2445 /* Uh, we sent a SIGKILL and it is still not gone?
2446 * Must be something we cannot kill, so let's just be
2447 * weirded out and continue */
2449 log_warning_unit(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
2450 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2453 case SERVICE_STOP_POST:
2454 log_warning_unit(UNIT(s)->id, "%s stop-post timed out. Terminating.", UNIT(s)->id);
2455 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2458 case SERVICE_FINAL_SIGTERM:
2459 if (s->kill_context.send_sigkill) {
2460 log_warning_unit(UNIT(s)->id, "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
2461 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2463 log_warning_unit(UNIT(s)->id, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s)->id);
2464 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2469 case SERVICE_FINAL_SIGKILL:
2470 log_warning_unit(UNIT(s)->id, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
2471 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2474 case SERVICE_AUTO_RESTART:
2475 log_info_unit(UNIT(s)->id,
2476 s->restart_usec > 0 ?
2477 "%s holdoff time over, scheduling restart." :
2478 "%s has no holdoff time, scheduling restart.",
2480 service_enter_restart(s);
2484 assert_not_reached("Timeout at wrong time.");
2490 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2491 Service *s = SERVICE(userdata);
2492 char t[FORMAT_TIMESPAN_MAX];
2495 assert(source == s->watchdog_event_source);
2497 log_error_unit(UNIT(s)->id, "%s watchdog timeout (limit %s)!", UNIT(s)->id,
2498 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
2500 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
2505 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2506 Service *s = SERVICE(u);
2508 bool notify_dbus = false;
2512 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
2513 u->id, pid, tags && *tags ? tags[0] : "(empty)");
2515 if (s->notify_access == NOTIFY_NONE) {
2516 log_warning_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception is disabled.", u->id, pid);
2520 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2521 if (s->main_pid != 0)
2522 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);
2524 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);
2528 /* Interpret MAINPID= */
2529 e = strv_find_startswith(tags, "MAINPID=");
2530 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
2531 if (parse_pid(e, &pid) < 0)
2532 log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
2534 log_debug_unit(u->id, "%s: got MAINPID=%s", u->id, e);
2536 service_set_main_pid(s, pid);
2537 unit_watch_pid(UNIT(s), pid);
2542 /* Interpret READY= */
2543 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
2544 log_debug_unit(u->id, "%s: got READY=1", u->id);
2545 service_enter_start_post(s);
2549 /* Interpret STATUS= */
2550 e = strv_find_startswith(tags, "STATUS=");
2552 _cleanup_free_ char *t = NULL;
2555 if (!utf8_is_valid(e))
2556 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
2558 log_debug_unit(u->id, "%s: got STATUS=%s", u->id, e);
2566 if (!streq_ptr(s->status_text, t)) {
2568 free(s->status_text);
2576 /* Interpret ERRNO= */
2577 e = strv_find_startswith(tags, "ERRNO=");
2581 if (safe_atoi(e, &status_errno) < 0 || status_errno < 0)
2582 log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
2584 log_debug_unit(u->id, "%s: got ERRNO=%s", u->id, e);
2586 if (s->status_errno != status_errno) {
2587 s->status_errno = status_errno;
2593 /* Interpret WATCHDOG= */
2594 if (strv_find(tags, "WATCHDOG=1")) {
2595 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
2596 service_reset_watchdog(s);
2599 /* Notify clients about changed status or main pid */
2601 unit_add_to_dbus_queue(u);
2604 static int service_get_timeout(Unit *u, uint64_t *timeout) {
2605 Service *s = SERVICE(u);
2608 if (!s->timer_event_source)
2611 r = sd_event_source_get_time(s->timer_event_source, timeout);
2618 static void service_bus_name_owner_change(
2621 const char *old_owner,
2622 const char *new_owner) {
2624 Service *s = SERVICE(u);
2630 assert(streq(s->bus_name, name));
2631 assert(old_owner || new_owner);
2633 if (old_owner && new_owner)
2634 log_debug_unit(u->id, "%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
2636 log_debug_unit(u->id, "%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
2638 log_debug_unit(u->id, "%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
2640 s->bus_name_good = !!new_owner;
2642 if (s->type == SERVICE_DBUS) {
2644 /* service_enter_running() will figure out what to
2646 if (s->state == SERVICE_RUNNING)
2647 service_enter_running(s, SERVICE_SUCCESS);
2648 else if (s->state == SERVICE_START && new_owner)
2649 service_enter_start_post(s);
2651 } else if (new_owner &&
2653 (s->state == SERVICE_START ||
2654 s->state == SERVICE_START_POST ||
2655 s->state == SERVICE_RUNNING ||
2656 s->state == SERVICE_RELOAD)) {
2658 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
2661 /* Try to acquire PID from bus service */
2663 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
2665 r = sd_bus_creds_get_pid(creds, &pid);
2667 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
2669 service_set_main_pid(s, pid);
2670 unit_watch_pid(UNIT(s), pid);
2675 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2676 _cleanup_free_ char *peer = NULL;
2682 /* This is called by the socket code when instantiating a new
2683 * service for a stream socket and the socket needs to be
2686 if (UNIT(s)->load_state != UNIT_LOADED)
2689 if (s->socket_fd >= 0)
2692 if (s->state != SERVICE_DEAD)
2695 if (getpeername_pretty(fd, &peer) >= 0) {
2697 if (UNIT(s)->description) {
2698 _cleanup_free_ char *a;
2700 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2704 r = unit_set_description(UNIT(s), a);
2706 r = unit_set_description(UNIT(s), peer);
2714 unit_ref_set(&s->accept_socket, UNIT(sock));
2716 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
2719 static void service_reset_failed(Unit *u) {
2720 Service *s = SERVICE(u);
2724 if (s->state == SERVICE_FAILED)
2725 service_set_state(s, SERVICE_DEAD);
2727 s->result = SERVICE_SUCCESS;
2728 s->reload_result = SERVICE_SUCCESS;
2730 RATELIMIT_RESET(s->start_limit);
2733 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2734 Service *s = SERVICE(u);
2736 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
2739 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2740 [SERVICE_DEAD] = "dead",
2741 [SERVICE_START_PRE] = "start-pre",
2742 [SERVICE_START] = "start",
2743 [SERVICE_START_POST] = "start-post",
2744 [SERVICE_RUNNING] = "running",
2745 [SERVICE_EXITED] = "exited",
2746 [SERVICE_RELOAD] = "reload",
2747 [SERVICE_STOP] = "stop",
2748 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2749 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2750 [SERVICE_STOP_POST] = "stop-post",
2751 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2752 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2753 [SERVICE_FAILED] = "failed",
2754 [SERVICE_AUTO_RESTART] = "auto-restart",
2757 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2759 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2760 [SERVICE_RESTART_NO] = "no",
2761 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
2762 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
2763 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
2764 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
2765 [SERVICE_RESTART_ON_ABORT] = "on-abort",
2766 [SERVICE_RESTART_ALWAYS] = "always",
2769 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2771 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2772 [SERVICE_SIMPLE] = "simple",
2773 [SERVICE_FORKING] = "forking",
2774 [SERVICE_ONESHOT] = "oneshot",
2775 [SERVICE_DBUS] = "dbus",
2776 [SERVICE_NOTIFY] = "notify",
2777 [SERVICE_IDLE] = "idle"
2780 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2782 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2783 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2784 [SERVICE_EXEC_START] = "ExecStart",
2785 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2786 [SERVICE_EXEC_RELOAD] = "ExecReload",
2787 [SERVICE_EXEC_STOP] = "ExecStop",
2788 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2791 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2793 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2794 [NOTIFY_NONE] = "none",
2795 [NOTIFY_MAIN] = "main",
2796 [NOTIFY_ALL] = "all"
2799 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2801 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
2802 [SERVICE_SUCCESS] = "success",
2803 [SERVICE_FAILURE_RESOURCES] = "resources",
2804 [SERVICE_FAILURE_TIMEOUT] = "timeout",
2805 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
2806 [SERVICE_FAILURE_SIGNAL] = "signal",
2807 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
2808 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
2809 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
2812 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
2814 static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
2815 [SERVICE_FAILURE_ACTION_NONE] = "none",
2816 [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
2817 [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
2818 [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
2820 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
2822 const UnitVTable service_vtable = {
2823 .object_size = sizeof(Service),
2824 .exec_context_offset = offsetof(Service, exec_context),
2825 .cgroup_context_offset = offsetof(Service, cgroup_context),
2826 .kill_context_offset = offsetof(Service, kill_context),
2827 .exec_runtime_offset = offsetof(Service, exec_runtime),
2833 .private_section = "Service",
2835 .init = service_init,
2836 .done = service_done,
2837 .load = service_load,
2839 .coldplug = service_coldplug,
2841 .dump = service_dump,
2843 .start = service_start,
2844 .stop = service_stop,
2845 .reload = service_reload,
2847 .can_reload = service_can_reload,
2849 .kill = service_kill,
2851 .serialize = service_serialize,
2852 .deserialize_item = service_deserialize_item,
2854 .active_state = service_active_state,
2855 .sub_state_to_string = service_sub_state_to_string,
2857 .check_gc = service_check_gc,
2858 .check_snapshot = service_check_snapshot,
2860 .sigchld_event = service_sigchld_event,
2862 .reset_failed = service_reset_failed,
2864 .notify_cgroup_empty = service_notify_cgroup_empty_event,
2865 .notify_message = service_notify_message,
2867 .bus_name_owner_change = service_bus_name_owner_change,
2869 .bus_interface = "org.freedesktop.systemd1.Service",
2870 .bus_vtable = bus_service_vtable,
2871 .bus_set_property = bus_service_set_property,
2872 .bus_commit_properties = bus_service_commit_properties,
2874 .get_timeout = service_get_timeout,
2875 .can_transient = true,
2877 .status_message_formats = {
2878 .starting_stopping = {
2879 [0] = "Starting %s...",
2880 [1] = "Stopping %s...",
2882 .finished_start_job = {
2883 [JOB_DONE] = "Started %s.",
2884 [JOB_FAILED] = "Failed to start %s.",
2885 [JOB_DEPENDENCY] = "Dependency failed for %s.",
2886 [JOB_TIMEOUT] = "Timed out starting %s.",
2888 .finished_stop_job = {
2889 [JOB_DONE] = "Stopped %s.",
2890 [JOB_FAILED] = "Stopped (with error) %s.",
2891 [JOB_TIMEOUT] = "Timed out stopping %s.",