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 && s->remain_after_exit &&
698 UNIT(s)->manager->n_on_console > 0) {
699 ExecContext *ec = unit_get_exec_context(UNIT(s));
700 if (ec && exec_context_may_touch_console(ec)) {
701 Manager *m = UNIT(s)->manager;
704 if (m->n_on_console == 0)
705 /* unset no_console_output flag, since the console is free */
706 m->no_console_output = false;
710 if (old_state != state)
711 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
713 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
714 s->reload_result = SERVICE_SUCCESS;
717 static int service_coldplug(Unit *u) {
718 Service *s = SERVICE(u);
722 assert(s->state == SERVICE_DEAD);
724 if (s->deserialized_state != s->state) {
726 if (IN_SET(s->deserialized_state,
727 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
729 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
731 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
735 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
737 /* For the start/stop timeouts 0 means off */
739 r = service_arm_timer(s, k);
745 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
747 /* The restart timeouts 0 means immediately */
748 r = service_arm_timer(s, s->restart_usec);
753 if (pid_is_unwaited(s->main_pid) &&
754 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
755 IN_SET(s->deserialized_state,
756 SERVICE_START, SERVICE_START_POST,
757 SERVICE_RUNNING, SERVICE_RELOAD,
758 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
760 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
761 r = unit_watch_pid(UNIT(s), s->main_pid);
766 if (pid_is_unwaited(s->control_pid) &&
767 IN_SET(s->deserialized_state,
768 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
770 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
772 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
773 r = unit_watch_pid(UNIT(s), s->control_pid);
778 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
779 unit_watch_all_pids(UNIT(s));
781 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
782 service_start_watchdog(s);
784 service_set_state(s, s->deserialized_state);
790 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
801 if (s->socket_fd >= 0)
804 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
809 if (u->type != UNIT_SOCKET)
814 r = socket_collect_fds(sock, &cfds, &cn_fds);
827 t = new(int, rn_fds+cn_fds);
834 memcpy(t, rfds, rn_fds * sizeof(int));
835 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
840 rn_fds = rn_fds+cn_fds;
855 static int service_spawn(
860 bool apply_permissions,
862 bool apply_tty_stdin,
863 bool set_notify_socket,
870 _cleanup_free_ int *fdsbuf = NULL;
871 unsigned n_fds = 0, n_env = 0;
872 _cleanup_strv_free_ char
873 **argv = NULL, **final_env = NULL, **our_env = NULL;
880 unit_realize_cgroup(UNIT(s));
882 r = unit_setup_exec_runtime(UNIT(s));
887 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
888 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
889 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
891 if (s->socket_fd >= 0) {
895 r = service_collect_fds(s, &fdsbuf, &n_fds);
903 if (timeout && s->timeout_start_usec > 0) {
904 r = service_arm_timer(s, s->timeout_start_usec);
908 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
910 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
914 our_env = new0(char*, 4);
920 if (set_notify_socket)
921 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
927 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
932 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
933 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
938 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
944 if (is_control && UNIT(s)->cgroup_path) {
945 path = strappenda(UNIT(s)->cgroup_path, "/control");
946 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
948 path = UNIT(s)->cgroup_path;
958 UNIT(s)->manager->confirm_spawn,
959 UNIT(s)->manager->cgroup_supported,
961 manager_get_runtime_prefix(UNIT(s)->manager),
964 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
970 r = unit_watch_pid(UNIT(s), pid);
972 /* FIXME: we need to do something here */
981 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
986 static int main_pid_good(Service *s) {
989 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
992 /* If we know the pid file, then lets just check if it is
994 if (s->main_pid_known) {
996 /* If it's an alien child let's check if it is still
998 if (s->main_pid_alien && s->main_pid > 0)
999 return pid_is_alive(s->main_pid);
1001 /* .. otherwise assume we'll get a SIGCHLD for it,
1002 * which we really should wait for to collect exit
1003 * status and code */
1004 return s->main_pid > 0;
1007 /* We don't know the pid */
1011 _pure_ static int control_pid_good(Service *s) {
1014 return s->control_pid > 0;
1017 static int cgroup_good(Service *s) {
1022 if (!UNIT(s)->cgroup_path)
1025 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1032 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
1034 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1038 if (f != SERVICE_SUCCESS)
1041 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1043 if (s->result != SERVICE_SUCCESS)
1044 service_execute_action(s, s->failure_action, "failed", false);
1046 if (allow_restart &&
1047 !s->forbid_restart &&
1048 (s->restart == SERVICE_RESTART_ALWAYS ||
1049 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1050 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1051 (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
1052 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1053 (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) ||
1054 (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) ||
1055 (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)))) &&
1056 (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) &&
1057 (!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)))) {
1059 r = service_arm_timer(s, s->restart_usec);
1063 service_set_state(s, SERVICE_AUTO_RESTART);
1066 s->forbid_restart = false;
1068 /* We want fresh tmpdirs in case service is started again immediately */
1069 exec_runtime_destroy(s->exec_runtime);
1070 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1072 /* Also, remove the runtime directory in */
1073 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1075 /* Try to delete the pid file. At this point it will be
1076 * out-of-date, and some software might be confused by it, so
1077 * let's remove it. */
1079 unlink_noerrno(s->pid_file);
1084 log_warning_unit(UNIT(s)->id, "%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1085 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1088 static void service_enter_stop_post(Service *s, ServiceResult f) {
1092 if (f != SERVICE_SUCCESS)
1095 service_unwatch_control_pid(s);
1096 unit_watch_all_pids(UNIT(s));
1098 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1099 if (s->control_command) {
1100 s->control_command_id = SERVICE_EXEC_STOP_POST;
1102 r = service_spawn(s,
1106 !s->permissions_start_only,
1107 !s->root_directory_start_only,
1115 service_set_state(s, SERVICE_STOP_POST);
1117 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1122 log_warning_unit(UNIT(s)->id, "%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1123 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1126 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1131 if (f != SERVICE_SUCCESS)
1134 unit_watch_all_pids(UNIT(s));
1136 r = unit_kill_context(
1139 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1148 if (s->timeout_stop_usec > 0) {
1149 r = service_arm_timer(s, s->timeout_stop_usec);
1154 service_set_state(s, state);
1155 } else if (state == SERVICE_STOP_SIGTERM)
1156 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1157 else if (state == SERVICE_STOP_SIGKILL)
1158 service_enter_stop_post(s, SERVICE_SUCCESS);
1159 else if (state == SERVICE_FINAL_SIGTERM)
1160 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1162 service_enter_dead(s, SERVICE_SUCCESS, true);
1167 log_warning_unit(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1169 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1170 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1172 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1175 static void service_enter_stop(Service *s, ServiceResult f) {
1180 if (f != SERVICE_SUCCESS)
1183 service_unwatch_control_pid(s);
1184 unit_watch_all_pids(UNIT(s));
1186 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1187 if (s->control_command) {
1188 s->control_command_id = SERVICE_EXEC_STOP;
1190 r = service_spawn(s,
1194 !s->permissions_start_only,
1195 !s->root_directory_start_only,
1203 service_set_state(s, SERVICE_STOP);
1205 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1210 log_warning_unit(UNIT(s)->id, "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
1211 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1214 static void service_enter_running(Service *s, ServiceResult f) {
1215 int main_pid_ok, cgroup_ok;
1218 if (f != SERVICE_SUCCESS)
1221 main_pid_ok = main_pid_good(s);
1222 cgroup_ok = cgroup_good(s);
1224 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1225 (s->bus_name_good || s->type != SERVICE_DBUS))
1226 service_set_state(s, SERVICE_RUNNING);
1227 else if (s->remain_after_exit)
1228 service_set_state(s, SERVICE_EXITED);
1230 service_enter_stop(s, SERVICE_SUCCESS);
1233 static void service_enter_start_post(Service *s) {
1237 service_unwatch_control_pid(s);
1238 service_reset_watchdog(s);
1240 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1241 if (s->control_command) {
1242 s->control_command_id = SERVICE_EXEC_START_POST;
1244 r = service_spawn(s,
1248 !s->permissions_start_only,
1249 !s->root_directory_start_only,
1257 service_set_state(s, SERVICE_START_POST);
1259 service_enter_running(s, SERVICE_SUCCESS);
1264 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
1265 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1268 static void service_kill_control_processes(Service *s) {
1271 if (!UNIT(s)->cgroup_path)
1274 p = strappenda(UNIT(s)->cgroup_path, "/control");
1275 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1278 static void service_enter_start(Service *s) {
1285 assert(s->exec_command[SERVICE_EXEC_START]);
1286 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1288 service_unwatch_control_pid(s);
1289 service_unwatch_main_pid(s);
1291 /* We want to ensure that nobody leaks processes from
1292 * START_PRE here, so let's go on a killing spree, People
1293 * should not spawn long running processes from START_PRE. */
1294 service_kill_control_processes(s);
1296 if (s->type == SERVICE_FORKING) {
1297 s->control_command_id = SERVICE_EXEC_START;
1298 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1300 s->main_command = NULL;
1302 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1303 s->control_command = NULL;
1305 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1308 r = service_spawn(s,
1310 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
1311 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
1316 s->notify_access != NOTIFY_NONE,
1322 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
1323 /* For simple services we immediately start
1324 * the START_POST binaries. */
1326 service_set_main_pid(s, pid);
1327 service_enter_start_post(s);
1329 } else if (s->type == SERVICE_FORKING) {
1331 /* For forking services we wait until the start
1332 * process exited. */
1334 s->control_pid = pid;
1335 service_set_state(s, SERVICE_START);
1337 } else if (s->type == SERVICE_ONESHOT ||
1338 s->type == SERVICE_DBUS ||
1339 s->type == SERVICE_NOTIFY) {
1341 /* For oneshot services we wait until the start
1342 * process exited, too, but it is our main process. */
1344 /* For D-Bus services we know the main pid right away,
1345 * but wait for the bus name to appear on the
1346 * bus. Notify services are similar. */
1348 service_set_main_pid(s, pid);
1349 service_set_state(s, SERVICE_START);
1351 assert_not_reached("Unknown service type");
1356 log_warning_unit(UNIT(s)->id, "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
1357 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1360 static void service_enter_start_pre(Service *s) {
1365 service_unwatch_control_pid(s);
1367 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1368 if (s->control_command) {
1369 /* Before we start anything, let's clear up what might
1370 * be left from previous runs. */
1371 service_kill_control_processes(s);
1373 s->control_command_id = SERVICE_EXEC_START_PRE;
1375 r = service_spawn(s,
1379 !s->permissions_start_only,
1380 !s->root_directory_start_only,
1388 service_set_state(s, SERVICE_START_PRE);
1390 service_enter_start(s);
1395 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
1396 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1399 static void service_enter_restart(Service *s) {
1400 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1405 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1406 /* Don't restart things if we are going down anyway */
1407 log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart.");
1409 r = service_arm_timer(s, s->restart_usec);
1416 /* Any units that are bound to this service must also be
1417 * restarted. We use JOB_RESTART (instead of the more obvious
1418 * JOB_START) here so that those dependency jobs will be added
1420 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1424 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1425 * it will be canceled as part of the service_stop() call that
1426 * is executed as part of JOB_RESTART. */
1428 log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id);
1432 log_warning_unit(UNIT(s)->id,
1433 "%s failed to schedule restart job: %s",
1434 UNIT(s)->id, bus_error_message(&error, -r));
1435 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1438 static void service_enter_reload(Service *s) {
1443 service_unwatch_control_pid(s);
1445 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1446 if (s->control_command) {
1447 s->control_command_id = SERVICE_EXEC_RELOAD;
1449 r = service_spawn(s,
1453 !s->permissions_start_only,
1454 !s->root_directory_start_only,
1462 service_set_state(s, SERVICE_RELOAD);
1464 service_enter_running(s, SERVICE_SUCCESS);
1469 log_warning_unit(UNIT(s)->id, "%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
1470 s->reload_result = SERVICE_FAILURE_RESOURCES;
1471 service_enter_running(s, SERVICE_SUCCESS);
1474 static void service_run_next_control(Service *s) {
1478 assert(s->control_command);
1479 assert(s->control_command->command_next);
1481 assert(s->control_command_id != SERVICE_EXEC_START);
1483 s->control_command = s->control_command->command_next;
1484 service_unwatch_control_pid(s);
1486 r = service_spawn(s,
1490 !s->permissions_start_only,
1491 !s->root_directory_start_only,
1492 s->control_command_id == SERVICE_EXEC_START_PRE ||
1493 s->control_command_id == SERVICE_EXEC_STOP_POST,
1503 log_warning_unit(UNIT(s)->id, "%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
1505 if (s->state == SERVICE_START_PRE)
1506 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1507 else if (s->state == SERVICE_STOP)
1508 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1509 else if (s->state == SERVICE_STOP_POST)
1510 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1511 else if (s->state == SERVICE_RELOAD) {
1512 s->reload_result = SERVICE_FAILURE_RESOURCES;
1513 service_enter_running(s, SERVICE_SUCCESS);
1515 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1518 static void service_run_next_main(Service *s) {
1523 assert(s->main_command);
1524 assert(s->main_command->command_next);
1525 assert(s->type == SERVICE_ONESHOT);
1527 s->main_command = s->main_command->command_next;
1528 service_unwatch_main_pid(s);
1530 r = service_spawn(s,
1537 s->notify_access != NOTIFY_NONE,
1543 service_set_main_pid(s, pid);
1548 log_warning_unit(UNIT(s)->id, "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
1549 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1552 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
1555 if (action == SERVICE_FAILURE_ACTION_REBOOT ||
1556 action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
1557 update_reboot_param_file(s->reboot_arg);
1561 case SERVICE_FAILURE_ACTION_NONE:
1562 if (log_action_none)
1563 log_warning_unit(UNIT(s)->id, "%s %s, refusing to start.", UNIT(s)->id, reason);
1566 case SERVICE_FAILURE_ACTION_REBOOT: {
1567 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1570 log_warning_unit(UNIT(s)->id, "%s %s, rebooting.", UNIT(s)->id, reason);
1572 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
1574 log_error_unit(UNIT(s)->id, "Failed to reboot: %s.", bus_error_message(&error, r));
1579 case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
1580 log_warning_unit(UNIT(s)->id, "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
1581 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
1584 case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
1585 log_warning_unit(UNIT(s)->id, "%s %s, rebooting immediately.", UNIT(s)->id, reason);
1589 if (s->reboot_arg) {
1590 log_info("Rebooting with argument '%s'.", s->reboot_arg);
1591 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
1594 log_info("Rebooting.");
1595 reboot(RB_AUTOBOOT);
1599 log_error_unit(UNIT(s)->id, "failure action=%i", action);
1600 assert_not_reached("Unknown FailureAction.");
1606 static int service_start_limit_test(Service *s) {
1609 if (ratelimit_test(&s->start_limit))
1612 return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
1615 static int service_start(Unit *u) {
1616 Service *s = SERVICE(u);
1621 /* We cannot fulfill this request right now, try again later
1623 if (s->state == SERVICE_STOP ||
1624 s->state == SERVICE_STOP_SIGTERM ||
1625 s->state == SERVICE_STOP_SIGKILL ||
1626 s->state == SERVICE_STOP_POST ||
1627 s->state == SERVICE_FINAL_SIGTERM ||
1628 s->state == SERVICE_FINAL_SIGKILL)
1631 /* Already on it! */
1632 if (s->state == SERVICE_START_PRE ||
1633 s->state == SERVICE_START ||
1634 s->state == SERVICE_START_POST)
1637 /* A service that will be restarted must be stopped first to
1638 * trigger BindsTo and/or OnFailure dependencies. If a user
1639 * does not want to wait for the holdoff time to elapse, the
1640 * service should be manually restarted, not started. We
1641 * simply return EAGAIN here, so that any start jobs stay
1642 * queued, and assume that the auto restart timer will
1643 * eventually trigger the restart. */
1644 if (s->state == SERVICE_AUTO_RESTART)
1647 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
1649 /* Make sure we don't enter a busy loop of some kind. */
1650 r = service_start_limit_test(s);
1652 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
1656 s->result = SERVICE_SUCCESS;
1657 s->reload_result = SERVICE_SUCCESS;
1658 s->main_pid_known = false;
1659 s->main_pid_alien = false;
1660 s->forbid_restart = false;
1662 free(s->status_text);
1663 s->status_text = NULL;
1664 s->status_errno = 0;
1666 service_enter_start_pre(s);
1670 static int service_stop(Unit *u) {
1671 Service *s = SERVICE(u);
1675 /* Don't create restart jobs from here. */
1676 s->forbid_restart = true;
1679 if (s->state == SERVICE_STOP ||
1680 s->state == SERVICE_STOP_SIGTERM ||
1681 s->state == SERVICE_STOP_SIGKILL ||
1682 s->state == SERVICE_STOP_POST ||
1683 s->state == SERVICE_FINAL_SIGTERM ||
1684 s->state == SERVICE_FINAL_SIGKILL)
1687 /* A restart will be scheduled or is in progress. */
1688 if (s->state == SERVICE_AUTO_RESTART) {
1689 service_set_state(s, SERVICE_DEAD);
1693 /* If there's already something running we go directly into
1695 if (s->state == SERVICE_START_PRE ||
1696 s->state == SERVICE_START ||
1697 s->state == SERVICE_START_POST ||
1698 s->state == SERVICE_RELOAD) {
1699 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1703 assert(s->state == SERVICE_RUNNING ||
1704 s->state == SERVICE_EXITED);
1706 service_enter_stop(s, SERVICE_SUCCESS);
1710 static int service_reload(Unit *u) {
1711 Service *s = SERVICE(u);
1715 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1717 service_enter_reload(s);
1721 _pure_ static bool service_can_reload(Unit *u) {
1722 Service *s = SERVICE(u);
1726 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1729 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1730 Service *s = SERVICE(u);
1736 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1737 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1738 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
1740 if (s->control_pid > 0)
1741 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1744 if (s->main_pid_known && s->main_pid > 0)
1745 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
1747 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1750 unit_serialize_item(u, f, "status-text", s->status_text);
1752 /* FIXME: There's a minor uncleanliness here: if there are
1753 * multiple commands attached here, we will start from the
1754 * first one again */
1755 if (s->control_command_id >= 0)
1756 unit_serialize_item(u, f, "control-command",
1757 service_exec_command_to_string(s->control_command_id));
1759 if (s->socket_fd >= 0) {
1762 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1765 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1768 if (s->main_exec_status.pid > 0) {
1769 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1770 s->main_exec_status.pid);
1771 dual_timestamp_serialize(f, "main-exec-status-start",
1772 &s->main_exec_status.start_timestamp);
1773 dual_timestamp_serialize(f, "main-exec-status-exit",
1774 &s->main_exec_status.exit_timestamp);
1776 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
1777 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1778 s->main_exec_status.code);
1779 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1780 s->main_exec_status.status);
1783 if (dual_timestamp_is_set(&s->watchdog_timestamp))
1784 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
1786 if (s->forbid_restart)
1787 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
1792 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1793 Service *s = SERVICE(u);
1800 if (streq(key, "state")) {
1803 state = service_state_from_string(value);
1805 log_debug_unit(u->id, "Failed to parse state value %s", value);
1807 s->deserialized_state = state;
1808 } else if (streq(key, "result")) {
1811 f = service_result_from_string(value);
1813 log_debug_unit(u->id, "Failed to parse result value %s", value);
1814 else if (f != SERVICE_SUCCESS)
1817 } else if (streq(key, "reload-result")) {
1820 f = service_result_from_string(value);
1822 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
1823 else if (f != SERVICE_SUCCESS)
1824 s->reload_result = f;
1826 } else if (streq(key, "control-pid")) {
1829 if (parse_pid(value, &pid) < 0)
1830 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
1832 s->control_pid = pid;
1833 } else if (streq(key, "main-pid")) {
1836 if (parse_pid(value, &pid) < 0)
1837 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
1839 service_set_main_pid(s, pid);
1840 unit_watch_pid(UNIT(s), pid);
1842 } else if (streq(key, "main-pid-known")) {
1845 b = parse_boolean(value);
1847 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
1849 s->main_pid_known = b;
1850 } else if (streq(key, "status-text")) {
1857 free(s->status_text);
1861 } else if (streq(key, "control-command")) {
1862 ServiceExecCommand id;
1864 id = service_exec_command_from_string(value);
1866 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
1868 s->control_command_id = id;
1869 s->control_command = s->exec_command[id];
1871 } else if (streq(key, "socket-fd")) {
1874 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1875 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
1878 asynchronous_close(s->socket_fd);
1879 s->socket_fd = fdset_remove(fds, fd);
1881 } else if (streq(key, "main-exec-status-pid")) {
1884 if (parse_pid(value, &pid) < 0)
1885 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
1887 s->main_exec_status.pid = pid;
1888 } else if (streq(key, "main-exec-status-code")) {
1891 if (safe_atoi(value, &i) < 0)
1892 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
1894 s->main_exec_status.code = i;
1895 } else if (streq(key, "main-exec-status-status")) {
1898 if (safe_atoi(value, &i) < 0)
1899 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
1901 s->main_exec_status.status = i;
1902 } else if (streq(key, "main-exec-status-start"))
1903 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
1904 else if (streq(key, "main-exec-status-exit"))
1905 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
1906 else if (streq(key, "watchdog-timestamp"))
1907 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
1908 else if (streq(key, "forbid-restart")) {
1911 b = parse_boolean(value);
1913 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
1915 s->forbid_restart = b;
1917 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
1922 _pure_ static UnitActiveState service_active_state(Unit *u) {
1923 const UnitActiveState *table;
1927 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1929 return table[SERVICE(u)->state];
1932 static const char *service_sub_state_to_string(Unit *u) {
1935 return service_state_to_string(SERVICE(u)->state);
1938 static bool service_check_gc(Unit *u) {
1939 Service *s = SERVICE(u);
1943 /* Never clean up services that still have a process around,
1944 * even if the service is formally dead. */
1945 if (cgroup_good(s) > 0 ||
1946 main_pid_good(s) > 0 ||
1947 control_pid_good(s) > 0)
1953 _pure_ static bool service_check_snapshot(Unit *u) {
1954 Service *s = SERVICE(u);
1958 return s->socket_fd < 0;
1961 static int service_retry_pid_file(Service *s) {
1964 assert(s->pid_file);
1965 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
1967 r = service_load_pid_file(s, false);
1971 service_unwatch_pid_file(s);
1973 service_enter_running(s, SERVICE_SUCCESS);
1977 static int service_watch_pid_file(Service *s) {
1980 log_debug_unit(UNIT(s)->id, "Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
1982 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
1986 /* the pidfile might have appeared just before we set the watch */
1987 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);
1988 service_retry_pid_file(s);
1992 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));
1993 service_unwatch_pid_file(s);
1997 static int service_demand_pid_file(Service *s) {
2000 assert(s->pid_file);
2001 assert(!s->pid_file_pathspec);
2003 ps = new0(PathSpec, 1);
2008 ps->path = strdup(s->pid_file);
2014 path_kill_slashes(ps->path);
2016 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2017 * keep their PID file open all the time. */
2018 ps->type = PATH_MODIFIED;
2019 ps->inotify_fd = -1;
2021 s->pid_file_pathspec = ps;
2023 return service_watch_pid_file(s);
2026 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2027 PathSpec *p = userdata;
2032 s = SERVICE(p->unit);
2036 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2037 assert(s->pid_file_pathspec);
2038 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2040 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2042 if (path_spec_fd_event(p, events) < 0)
2045 if (service_retry_pid_file(s) == 0)
2048 if (service_watch_pid_file(s) < 0)
2054 service_unwatch_pid_file(s);
2055 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2059 static void service_notify_cgroup_empty_event(Unit *u) {
2060 Service *s = SERVICE(u);
2064 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2068 /* Waiting for SIGCHLD is usually more interesting,
2069 * because it includes return codes/signals. Which is
2070 * why we ignore the cgroup events for most cases,
2071 * except when we don't know pid which to expect the
2075 case SERVICE_START_POST:
2076 /* If we were hoping for the daemon to write its PID file,
2077 * we can give up now. */
2078 if (s->pid_file_pathspec) {
2079 log_warning_unit(u->id, "%s never wrote its PID file. Failing.", UNIT(s)->id);
2081 service_unwatch_pid_file(s);
2082 if (s->state == SERVICE_START)
2083 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2085 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2089 case SERVICE_RUNNING:
2090 /* service_enter_running() will figure out what to do */
2091 service_enter_running(s, SERVICE_SUCCESS);
2094 case SERVICE_STOP_SIGTERM:
2095 case SERVICE_STOP_SIGKILL:
2097 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2098 service_enter_stop_post(s, SERVICE_SUCCESS);
2102 case SERVICE_STOP_POST:
2103 case SERVICE_FINAL_SIGTERM:
2104 case SERVICE_FINAL_SIGKILL:
2105 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2106 service_enter_dead(s, SERVICE_SUCCESS, true);
2115 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2116 Service *s = SERVICE(u);
2122 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2123 is_clean_exit_lsb(code, status, &s->success_status))
2124 f = SERVICE_SUCCESS;
2125 else if (code == CLD_EXITED)
2126 f = SERVICE_FAILURE_EXIT_CODE;
2127 else if (code == CLD_KILLED)
2128 f = SERVICE_FAILURE_SIGNAL;
2129 else if (code == CLD_DUMPED)
2130 f = SERVICE_FAILURE_CORE_DUMP;
2132 assert_not_reached("Unknown code");
2134 if (s->main_pid == pid) {
2135 /* Forking services may occasionally move to a new PID.
2136 * As long as they update the PID file before exiting the old
2137 * PID, they're fine. */
2138 if (service_load_pid_file(s, false) == 0)
2142 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2144 if (s->main_command) {
2145 /* If this is not a forking service than the
2146 * main process got started and hence we copy
2147 * the exit status so that it is recorded both
2148 * as main and as control process exit
2151 s->main_command->exec_status = s->main_exec_status;
2153 if (s->main_command->ignore)
2154 f = SERVICE_SUCCESS;
2155 } else if (s->exec_command[SERVICE_EXEC_START]) {
2157 /* If this is a forked process, then we should
2158 * ignore the return value if this was
2159 * configured for the starter process */
2161 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2162 f = SERVICE_SUCCESS;
2165 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2167 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2168 u->id, sigchld_code_to_string(code), status,
2169 strna(code == CLD_EXITED
2170 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2171 : signal_to_string(status)),
2172 "EXIT_CODE=%s", sigchld_code_to_string(code),
2173 "EXIT_STATUS=%i", status,
2176 if (f != SERVICE_SUCCESS)
2179 if (s->main_command &&
2180 s->main_command->command_next &&
2181 f == SERVICE_SUCCESS) {
2183 /* There is another command to *
2184 * execute, so let's do that. */
2186 log_debug_unit(u->id, "%s running next main command for state %s", u->id, service_state_to_string(s->state));
2187 service_run_next_main(s);
2191 /* The service exited, so the service is officially
2193 s->main_command = NULL;
2197 case SERVICE_START_POST:
2198 case SERVICE_RELOAD:
2200 /* Need to wait until the operation is
2205 if (s->type == SERVICE_ONESHOT) {
2206 /* This was our main goal, so let's go on */
2207 if (f == SERVICE_SUCCESS)
2208 service_enter_start_post(s);
2210 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2216 case SERVICE_RUNNING:
2217 service_enter_running(s, f);
2220 case SERVICE_STOP_SIGTERM:
2221 case SERVICE_STOP_SIGKILL:
2223 if (!control_pid_good(s))
2224 service_enter_stop_post(s, f);
2226 /* If there is still a control process, wait for that first */
2229 case SERVICE_STOP_POST:
2230 case SERVICE_FINAL_SIGTERM:
2231 case SERVICE_FINAL_SIGKILL:
2233 if (!control_pid_good(s))
2234 service_enter_dead(s, f, true);
2238 assert_not_reached("Uh, main process died at wrong time.");
2242 } else if (s->control_pid == pid) {
2245 if (s->control_command) {
2246 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2248 if (s->control_command->ignore)
2249 f = SERVICE_SUCCESS;
2252 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2253 "%s: control process exited, code=%s status=%i",
2254 u->id, sigchld_code_to_string(code), status);
2256 if (f != SERVICE_SUCCESS)
2259 /* Immediately get rid of the cgroup, so that the
2260 * kernel doesn't delay the cgroup empty messages for
2261 * the service cgroup any longer than necessary */
2262 service_kill_control_processes(s);
2264 if (s->control_command &&
2265 s->control_command->command_next &&
2266 f == SERVICE_SUCCESS) {
2268 /* There is another command to *
2269 * execute, so let's do that. */
2271 log_debug_unit(u->id, "%s running next control command for state %s", u->id, service_state_to_string(s->state));
2272 service_run_next_control(s);
2275 /* No further commands for this step, so let's
2276 * figure out what to do next */
2278 s->control_command = NULL;
2279 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2281 log_debug_unit(u->id, "%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2285 case SERVICE_START_PRE:
2286 if (f == SERVICE_SUCCESS)
2287 service_enter_start(s);
2289 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2293 if (s->type != SERVICE_FORKING)
2294 /* Maybe spurious event due to a reload that changed the type? */
2297 if (f != SERVICE_SUCCESS) {
2298 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2303 bool has_start_post;
2306 /* Let's try to load the pid file here if we can.
2307 * The PID file might actually be created by a START_POST
2308 * script. In that case don't worry if the loading fails. */
2310 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2311 r = service_load_pid_file(s, !has_start_post);
2312 if (!has_start_post && r < 0) {
2313 r = service_demand_pid_file(s);
2314 if (r < 0 || !cgroup_good(s))
2315 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2319 service_search_main_pid(s);
2321 service_enter_start_post(s);
2324 case SERVICE_START_POST:
2325 if (f != SERVICE_SUCCESS) {
2326 service_enter_stop(s, f);
2333 r = service_load_pid_file(s, true);
2335 r = service_demand_pid_file(s);
2336 if (r < 0 || !cgroup_good(s))
2337 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2341 service_search_main_pid(s);
2343 service_enter_running(s, SERVICE_SUCCESS);
2346 case SERVICE_RELOAD:
2347 if (f == SERVICE_SUCCESS) {
2348 service_load_pid_file(s, true);
2349 service_search_main_pid(s);
2352 s->reload_result = f;
2353 service_enter_running(s, SERVICE_SUCCESS);
2357 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2360 case SERVICE_STOP_SIGTERM:
2361 case SERVICE_STOP_SIGKILL:
2362 if (main_pid_good(s) <= 0)
2363 service_enter_stop_post(s, f);
2365 /* If there is still a service
2366 * process around, wait until
2367 * that one quit, too */
2370 case SERVICE_STOP_POST:
2371 case SERVICE_FINAL_SIGTERM:
2372 case SERVICE_FINAL_SIGKILL:
2373 if (main_pid_good(s) <= 0)
2374 service_enter_dead(s, f, true);
2378 assert_not_reached("Uh, control process died at wrong time.");
2383 /* Notify clients about changed exit status */
2384 unit_add_to_dbus_queue(u);
2386 /* We got one SIGCHLD for the service, let's watch all
2387 * processes that are now running of the service, and watch
2388 * that. Among the PIDs we then watch will be children
2389 * reassigned to us, which hopefully allows us to identify
2390 * when all children are gone */
2391 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2392 unit_watch_all_pids(u);
2394 /* If the PID set is empty now, then let's finish this off */
2395 if (set_isempty(u->pids))
2396 service_notify_cgroup_empty_event(u);
2399 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2400 Service *s = SERVICE(userdata);
2403 assert(source == s->timer_event_source);
2407 case SERVICE_START_PRE:
2409 log_warning_unit(UNIT(s)->id, "%s %s operation timed out. Terminating.", UNIT(s)->id, s->state == SERVICE_START ? "start" : "start-pre");
2410 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2413 case SERVICE_START_POST:
2414 log_warning_unit(UNIT(s)->id, "%s start-post operation timed out. Stopping.", UNIT(s)->id);
2415 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2418 case SERVICE_RELOAD:
2419 log_warning_unit(UNIT(s)->id, "%s reload operation timed out. Stopping.", UNIT(s)->id);
2420 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2421 service_enter_running(s, SERVICE_SUCCESS);
2425 log_warning_unit(UNIT(s)->id, "%s stopping timed out. Terminating.", UNIT(s)->id);
2426 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2429 case SERVICE_STOP_SIGTERM:
2430 if (s->kill_context.send_sigkill) {
2431 log_warning_unit(UNIT(s)->id, "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
2432 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2434 log_warning_unit(UNIT(s)->id, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
2435 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2440 case SERVICE_STOP_SIGKILL:
2441 /* Uh, we sent a SIGKILL and it is still not gone?
2442 * Must be something we cannot kill, so let's just be
2443 * weirded out and continue */
2445 log_warning_unit(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
2446 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2449 case SERVICE_STOP_POST:
2450 log_warning_unit(UNIT(s)->id, "%s stop-post timed out. Terminating.", UNIT(s)->id);
2451 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2454 case SERVICE_FINAL_SIGTERM:
2455 if (s->kill_context.send_sigkill) {
2456 log_warning_unit(UNIT(s)->id, "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
2457 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2459 log_warning_unit(UNIT(s)->id, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s)->id);
2460 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2465 case SERVICE_FINAL_SIGKILL:
2466 log_warning_unit(UNIT(s)->id, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
2467 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2470 case SERVICE_AUTO_RESTART:
2471 log_info_unit(UNIT(s)->id,
2472 s->restart_usec > 0 ?
2473 "%s holdoff time over, scheduling restart." :
2474 "%s has no holdoff time, scheduling restart.",
2476 service_enter_restart(s);
2480 assert_not_reached("Timeout at wrong time.");
2486 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2487 Service *s = SERVICE(userdata);
2488 char t[FORMAT_TIMESPAN_MAX];
2491 assert(source == s->watchdog_event_source);
2493 log_error_unit(UNIT(s)->id, "%s watchdog timeout (limit %s)!", UNIT(s)->id,
2494 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
2496 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
2501 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2502 Service *s = SERVICE(u);
2504 bool notify_dbus = false;
2508 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
2509 u->id, pid, tags && *tags ? tags[0] : "(empty)");
2511 if (s->notify_access == NOTIFY_NONE) {
2512 log_warning_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception is disabled.", u->id, pid);
2516 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2517 if (s->main_pid != 0)
2518 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);
2520 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);
2524 /* Interpret MAINPID= */
2525 e = strv_find_prefix(tags, "MAINPID=");
2526 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
2527 if (parse_pid(e + 8, &pid) < 0)
2528 log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
2530 log_debug_unit(u->id, "%s: got %s", u->id, e);
2531 service_set_main_pid(s, pid);
2532 unit_watch_pid(UNIT(s), pid);
2537 /* Interpret READY= */
2538 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
2539 log_debug_unit(u->id, "%s: got READY=1", u->id);
2540 service_enter_start_post(s);
2544 /* Interpret STATUS= */
2545 e = strv_find_prefix(tags, "STATUS=");
2550 if (!utf8_is_valid(e+7)) {
2551 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
2555 log_debug_unit(u->id, "%s: got %s", u->id, e);
2566 if (!streq_ptr(s->status_text, t)) {
2567 free(s->status_text);
2574 /* Interpret ERRNO= */
2575 e = strv_find_prefix(tags, "ERRNO=");
2579 if (safe_atoi(e + 6, &status_errno) < 0 || status_errno < 0)
2580 log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
2582 log_debug_unit(u->id, "%s: got %s", u->id, e);
2584 if (s->status_errno != status_errno) {
2585 s->status_errno = status_errno;
2591 /* Interpret WATCHDOG= */
2592 if (strv_find(tags, "WATCHDOG=1")) {
2593 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
2594 service_reset_watchdog(s);
2597 /* Notify clients about changed status or main pid */
2599 unit_add_to_dbus_queue(u);
2602 static int service_get_timeout(Unit *u, uint64_t *timeout) {
2603 Service *s = SERVICE(u);
2606 if (!s->timer_event_source)
2609 r = sd_event_source_get_time(s->timer_event_source, timeout);
2616 static void service_bus_name_owner_change(
2619 const char *old_owner,
2620 const char *new_owner) {
2622 Service *s = SERVICE(u);
2628 assert(streq(s->bus_name, name));
2629 assert(old_owner || new_owner);
2631 if (old_owner && new_owner)
2632 log_debug_unit(u->id, "%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
2634 log_debug_unit(u->id, "%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
2636 log_debug_unit(u->id, "%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
2638 s->bus_name_good = !!new_owner;
2640 if (s->type == SERVICE_DBUS) {
2642 /* service_enter_running() will figure out what to
2644 if (s->state == SERVICE_RUNNING)
2645 service_enter_running(s, SERVICE_SUCCESS);
2646 else if (s->state == SERVICE_START && new_owner)
2647 service_enter_start_post(s);
2649 } else if (new_owner &&
2651 (s->state == SERVICE_START ||
2652 s->state == SERVICE_START_POST ||
2653 s->state == SERVICE_RUNNING ||
2654 s->state == SERVICE_RELOAD)) {
2656 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
2659 /* Try to acquire PID from bus service */
2661 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
2663 r = sd_bus_creds_get_pid(creds, &pid);
2665 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
2667 service_set_main_pid(s, pid);
2668 unit_watch_pid(UNIT(s), pid);
2673 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2674 _cleanup_free_ char *peer = NULL;
2680 /* This is called by the socket code when instantiating a new
2681 * service for a stream socket and the socket needs to be
2684 if (UNIT(s)->load_state != UNIT_LOADED)
2687 if (s->socket_fd >= 0)
2690 if (s->state != SERVICE_DEAD)
2693 if (getpeername_pretty(fd, &peer) >= 0) {
2695 if (UNIT(s)->description) {
2696 _cleanup_free_ char *a;
2698 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2702 r = unit_set_description(UNIT(s), a);
2704 r = unit_set_description(UNIT(s), peer);
2712 unit_ref_set(&s->accept_socket, UNIT(sock));
2714 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
2717 static void service_reset_failed(Unit *u) {
2718 Service *s = SERVICE(u);
2722 if (s->state == SERVICE_FAILED)
2723 service_set_state(s, SERVICE_DEAD);
2725 s->result = SERVICE_SUCCESS;
2726 s->reload_result = SERVICE_SUCCESS;
2728 RATELIMIT_RESET(s->start_limit);
2731 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2732 Service *s = SERVICE(u);
2734 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
2737 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2738 [SERVICE_DEAD] = "dead",
2739 [SERVICE_START_PRE] = "start-pre",
2740 [SERVICE_START] = "start",
2741 [SERVICE_START_POST] = "start-post",
2742 [SERVICE_RUNNING] = "running",
2743 [SERVICE_EXITED] = "exited",
2744 [SERVICE_RELOAD] = "reload",
2745 [SERVICE_STOP] = "stop",
2746 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2747 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2748 [SERVICE_STOP_POST] = "stop-post",
2749 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2750 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2751 [SERVICE_FAILED] = "failed",
2752 [SERVICE_AUTO_RESTART] = "auto-restart",
2755 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2757 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2758 [SERVICE_RESTART_NO] = "no",
2759 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
2760 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
2761 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
2762 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
2763 [SERVICE_RESTART_ON_ABORT] = "on-abort",
2764 [SERVICE_RESTART_ALWAYS] = "always",
2767 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2769 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2770 [SERVICE_SIMPLE] = "simple",
2771 [SERVICE_FORKING] = "forking",
2772 [SERVICE_ONESHOT] = "oneshot",
2773 [SERVICE_DBUS] = "dbus",
2774 [SERVICE_NOTIFY] = "notify",
2775 [SERVICE_IDLE] = "idle"
2778 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2780 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2781 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2782 [SERVICE_EXEC_START] = "ExecStart",
2783 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2784 [SERVICE_EXEC_RELOAD] = "ExecReload",
2785 [SERVICE_EXEC_STOP] = "ExecStop",
2786 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2789 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2791 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2792 [NOTIFY_NONE] = "none",
2793 [NOTIFY_MAIN] = "main",
2794 [NOTIFY_ALL] = "all"
2797 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2799 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
2800 [SERVICE_SUCCESS] = "success",
2801 [SERVICE_FAILURE_RESOURCES] = "resources",
2802 [SERVICE_FAILURE_TIMEOUT] = "timeout",
2803 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
2804 [SERVICE_FAILURE_SIGNAL] = "signal",
2805 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
2806 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
2807 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
2810 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
2812 static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
2813 [SERVICE_FAILURE_ACTION_NONE] = "none",
2814 [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
2815 [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
2816 [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
2818 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
2820 const UnitVTable service_vtable = {
2821 .object_size = sizeof(Service),
2822 .exec_context_offset = offsetof(Service, exec_context),
2823 .cgroup_context_offset = offsetof(Service, cgroup_context),
2824 .kill_context_offset = offsetof(Service, kill_context),
2825 .exec_runtime_offset = offsetof(Service, exec_runtime),
2831 .private_section = "Service",
2833 .init = service_init,
2834 .done = service_done,
2835 .load = service_load,
2837 .coldplug = service_coldplug,
2839 .dump = service_dump,
2841 .start = service_start,
2842 .stop = service_stop,
2843 .reload = service_reload,
2845 .can_reload = service_can_reload,
2847 .kill = service_kill,
2849 .serialize = service_serialize,
2850 .deserialize_item = service_deserialize_item,
2852 .active_state = service_active_state,
2853 .sub_state_to_string = service_sub_state_to_string,
2855 .check_gc = service_check_gc,
2856 .check_snapshot = service_check_snapshot,
2858 .sigchld_event = service_sigchld_event,
2860 .reset_failed = service_reset_failed,
2862 .notify_cgroup_empty = service_notify_cgroup_empty_event,
2863 .notify_message = service_notify_message,
2865 .bus_name_owner_change = service_bus_name_owner_change,
2867 .bus_interface = "org.freedesktop.systemd1.Service",
2868 .bus_vtable = bus_service_vtable,
2869 .bus_set_property = bus_service_set_property,
2870 .bus_commit_properties = bus_service_commit_properties,
2872 .get_timeout = service_get_timeout,
2873 .can_transient = true,
2875 .status_message_formats = {
2876 .starting_stopping = {
2877 [0] = "Starting %s...",
2878 [1] = "Stopping %s...",
2880 .finished_start_job = {
2881 [JOB_DONE] = "Started %s.",
2882 [JOB_FAILED] = "Failed to start %s.",
2883 [JOB_DEPENDENCY] = "Dependency failed for %s.",
2884 [JOB_TIMEOUT] = "Timed out starting %s.",
2886 .finished_stop_job = {
2887 [JOB_DONE] = "Stopped %s.",
2888 [JOB_FAILED] = "Stopped (with error) %s.",
2889 [JOB_TIMEOUT] = "Timed out stopping %s.",