1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/reboot.h>
27 #include <linux/reboot.h>
28 #include <sys/syscall.h>
34 #include "load-fragment.h"
35 #include "load-dropin.h"
38 #include "unit-name.h"
39 #include "unit-printf.h"
40 #include "dbus-service.h"
42 #include "exit-status.h"
44 #include "path-util.h"
49 #include "bus-error.h"
52 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
53 [SERVICE_DEAD] = UNIT_INACTIVE,
54 [SERVICE_START_PRE] = UNIT_ACTIVATING,
55 [SERVICE_START] = UNIT_ACTIVATING,
56 [SERVICE_START_POST] = UNIT_ACTIVATING,
57 [SERVICE_RUNNING] = UNIT_ACTIVE,
58 [SERVICE_EXITED] = UNIT_ACTIVE,
59 [SERVICE_RELOAD] = UNIT_RELOADING,
60 [SERVICE_STOP] = UNIT_DEACTIVATING,
61 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
62 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
63 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
64 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
65 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
66 [SERVICE_FAILED] = UNIT_FAILED,
67 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
70 /* For Type=idle we never want to delay any other jobs, hence we
71 * consider idle jobs active as soon as we start working on them */
72 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
73 [SERVICE_DEAD] = UNIT_INACTIVE,
74 [SERVICE_START_PRE] = UNIT_ACTIVE,
75 [SERVICE_START] = UNIT_ACTIVE,
76 [SERVICE_START_POST] = UNIT_ACTIVE,
77 [SERVICE_RUNNING] = UNIT_ACTIVE,
78 [SERVICE_EXITED] = UNIT_ACTIVE,
79 [SERVICE_RELOAD] = UNIT_RELOADING,
80 [SERVICE_STOP] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
83 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
85 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
86 [SERVICE_FAILED] = UNIT_FAILED,
87 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
90 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
91 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
92 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
94 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
96 static void service_init(Unit *u) {
97 Service *s = SERVICE(u);
100 assert(u->load_state == UNIT_STUB);
102 s->timeout_start_usec = u->manager->default_timeout_start_usec;
103 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
104 s->restart_usec = u->manager->default_restart_usec;
105 s->type = _SERVICE_TYPE_INVALID;
107 s->guess_main_pid = true;
109 RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
111 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
114 static void service_unwatch_control_pid(Service *s) {
117 if (s->control_pid <= 0)
120 unit_unwatch_pid(UNIT(s), s->control_pid);
124 static void service_unwatch_main_pid(Service *s) {
127 if (s->main_pid <= 0)
130 unit_unwatch_pid(UNIT(s), s->main_pid);
134 static void service_unwatch_pid_file(Service *s) {
135 if (!s->pid_file_pathspec)
138 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
139 UNIT(s)->id, s->pid_file_pathspec->path);
140 path_spec_unwatch(s->pid_file_pathspec);
141 path_spec_done(s->pid_file_pathspec);
142 free(s->pid_file_pathspec);
143 s->pid_file_pathspec = NULL;
146 static int service_set_main_pid(Service *s, pid_t pid) {
157 if (s->main_pid == pid && s->main_pid_known)
160 if (s->main_pid != pid) {
161 service_unwatch_main_pid(s);
162 exec_status_start(&s->main_exec_status, pid);
166 s->main_pid_known = true;
168 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
169 log_warning_unit(UNIT(s)->id,
170 "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.",
173 s->main_pid_alien = true;
175 s->main_pid_alien = false;
180 static void service_close_socket_fd(Service *s) {
183 if (s->socket_fd < 0)
186 s->socket_fd = asynchronous_close(s->socket_fd);
189 static void service_connection_unref(Service *s) {
192 if (!UNIT_ISSET(s->accept_socket))
195 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
196 unit_ref_unset(&s->accept_socket);
199 static void service_stop_watchdog(Service *s) {
202 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
203 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
206 static void service_start_watchdog(Service *s) {
211 if (s->watchdog_usec <= 0)
214 if (s->watchdog_event_source) {
215 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
217 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
221 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
223 r = sd_event_add_time(
224 UNIT(s)->manager->event,
225 &s->watchdog_event_source,
227 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
228 service_dispatch_watchdog, s);
230 log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
234 /* Let's process everything else which might be a sign
235 * of living before we consider a service died. */
236 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
240 log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
243 static void service_reset_watchdog(Service *s) {
246 dual_timestamp_get(&s->watchdog_timestamp);
247 service_start_watchdog(s);
250 static void service_done(Unit *u) {
251 Service *s = SERVICE(u);
258 free(s->status_text);
259 s->status_text = NULL;
262 s->reboot_arg = NULL;
264 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
265 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
266 s->control_command = NULL;
267 s->main_command = NULL;
269 exit_status_set_free(&s->restart_prevent_status);
270 exit_status_set_free(&s->restart_force_status);
271 exit_status_set_free(&s->success_status);
273 /* This will leak a process, but at least no memory or any of
275 service_unwatch_main_pid(s);
276 service_unwatch_control_pid(s);
277 service_unwatch_pid_file(s);
280 unit_unwatch_bus_name(u, s->bus_name);
285 service_close_socket_fd(s);
286 service_connection_unref(s);
288 unit_ref_unset(&s->accept_socket);
290 service_stop_watchdog(s);
292 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
295 static int service_arm_timer(Service *s, usec_t usec) {
300 if (s->timer_event_source) {
301 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
305 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
308 return sd_event_add_time(
309 UNIT(s)->manager->event,
310 &s->timer_event_source,
312 now(CLOCK_MONOTONIC) + usec, 0,
313 service_dispatch_timer, s);
316 static int service_verify(Service *s) {
319 if (UNIT(s)->load_state != UNIT_LOADED)
322 if (!s->exec_command[SERVICE_EXEC_START]) {
323 log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
327 if (s->type != SERVICE_ONESHOT &&
328 s->exec_command[SERVICE_EXEC_START]->command_next) {
329 log_error_unit(UNIT(s)->id, "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
333 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
334 log_error_unit(UNIT(s)->id, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
338 if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) {
339 log_error_unit(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
343 if (s->type == SERVICE_DBUS && !s->bus_name) {
344 log_error_unit(UNIT(s)->id, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
348 if (s->bus_name && s->type != SERVICE_DBUS)
349 log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
351 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
352 log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id);
359 static int service_add_default_dependencies(Service *s) {
364 /* Add a number of automatic dependencies useful for the
365 * majority of services. */
367 /* First, pull in base system */
368 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES,
369 SPECIAL_BASIC_TARGET, NULL, true);
373 /* Second, activate normal shutdown */
374 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS,
375 SPECIAL_SHUTDOWN_TARGET, NULL, true);
379 static void service_fix_output(Service *s) {
382 /* If nothing has been explicitly configured, patch default
383 * output in. If input is socket/tty we avoid this however,
384 * since in that case we want output to default to the same
385 * place as we read input from. */
387 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
388 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
389 s->exec_context.std_input == EXEC_INPUT_NULL)
390 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
392 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
393 s->exec_context.std_input == EXEC_INPUT_NULL)
394 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
397 static int service_load(Unit *u) {
399 Service *s = SERVICE(u);
403 /* Load a .service file */
404 r = unit_load_fragment(u);
408 /* Still nothing found? Then let's give up */
409 if (u->load_state == UNIT_STUB)
412 /* This is a new unit? Then let's add in some extras */
413 if (u->load_state == UNIT_LOADED) {
415 /* We were able to load something, then let's add in
416 * the dropin directories. */
417 r = unit_load_dropin(u);
421 if (s->type == _SERVICE_TYPE_INVALID)
422 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
424 /* Oneshot services have disabled start timeout by default */
425 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
426 s->timeout_start_usec = 0;
428 service_fix_output(s);
430 r = unit_patch_contexts(u);
434 r = unit_add_exec_dependencies(u, &s->exec_context);
438 r = unit_add_default_slice(u, &s->cgroup_context);
442 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
443 s->notify_access = NOTIFY_MAIN;
445 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
446 s->notify_access = NOTIFY_MAIN;
449 r = unit_watch_bus_name(u, s->bus_name);
454 if (u->default_dependencies) {
455 r = service_add_default_dependencies(s);
462 return service_verify(s);
465 static void service_dump(Unit *u, FILE *f, const char *prefix) {
466 ServiceExecCommand c;
467 Service *s = SERVICE(u);
472 prefix = strempty(prefix);
473 prefix2 = strappenda(prefix, "\t");
476 "%sService State: %s\n"
478 "%sReload Result: %s\n"
479 "%sPermissionsStartOnly: %s\n"
480 "%sRootDirectoryStartOnly: %s\n"
481 "%sRemainAfterExit: %s\n"
482 "%sGuessMainPID: %s\n"
485 "%sNotifyAccess: %s\n",
486 prefix, service_state_to_string(s->state),
487 prefix, service_result_to_string(s->result),
488 prefix, service_result_to_string(s->reload_result),
489 prefix, yes_no(s->permissions_start_only),
490 prefix, yes_no(s->root_directory_start_only),
491 prefix, yes_no(s->remain_after_exit),
492 prefix, yes_no(s->guess_main_pid),
493 prefix, service_type_to_string(s->type),
494 prefix, service_restart_to_string(s->restart),
495 prefix, notify_access_to_string(s->notify_access));
497 if (s->control_pid > 0)
499 "%sControl PID: "PID_FMT"\n",
500 prefix, s->control_pid);
504 "%sMain PID: "PID_FMT"\n"
505 "%sMain PID Known: %s\n"
506 "%sMain PID Alien: %s\n",
508 prefix, yes_no(s->main_pid_known),
509 prefix, yes_no(s->main_pid_alien));
514 prefix, s->pid_file);
519 "%sBus Name Good: %s\n",
521 prefix, yes_no(s->bus_name_good));
523 kill_context_dump(&s->kill_context, f, prefix);
524 exec_context_dump(&s->exec_context, f, prefix);
526 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
528 if (!s->exec_command[c])
531 fprintf(f, "%s-> %s:\n",
532 prefix, service_exec_command_to_string(c));
534 exec_command_dump_list(s->exec_command[c], f, prefix2);
537 #ifdef HAVE_SYSV_COMPAT
538 if (s->sysv_start_priority >= 0)
540 "%sSysVStartPriority: %i\n",
541 prefix, s->sysv_start_priority);
545 fprintf(f, "%sStatus Text: %s\n",
546 prefix, s->status_text);
549 static int service_load_pid_file(Service *s, bool may_warn) {
550 _cleanup_free_ char *k = NULL;
559 r = read_one_line_file(s->pid_file, &k);
562 log_info_unit(UNIT(s)->id,
563 "PID file %s not readable (yet?) after %s.",
564 s->pid_file, service_state_to_string(s->state));
568 r = parse_pid(k, &pid);
571 log_info_unit(UNIT(s)->id,
572 "Failed to read PID from file %s: %s",
573 s->pid_file, strerror(-r));
577 if (!pid_is_alive(pid)) {
579 log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
584 if (s->main_pid_known) {
585 if (pid == s->main_pid)
588 log_debug_unit(UNIT(s)->id,
589 "Main PID changing: "PID_FMT" -> "PID_FMT,
591 service_unwatch_main_pid(s);
592 s->main_pid_known = false;
594 log_debug_unit(UNIT(s)->id,
595 "Main PID loaded: "PID_FMT, pid);
597 r = service_set_main_pid(s, pid);
601 r = unit_watch_pid(UNIT(s), pid);
603 /* FIXME: we need to do something here */
604 log_warning_unit(UNIT(s)->id,
605 "Failed to watch PID "PID_FMT" from service %s",
613 static int service_search_main_pid(Service *s) {
619 /* If we know it anyway, don't ever fallback to unreliable
621 if (s->main_pid_known)
624 if (!s->guess_main_pid)
627 assert(s->main_pid <= 0);
629 pid = unit_search_main_pid(UNIT(s));
633 log_debug_unit(UNIT(s)->id,
634 "Main PID guessed: "PID_FMT, pid);
635 r = service_set_main_pid(s, pid);
639 r = unit_watch_pid(UNIT(s), pid);
641 /* FIXME: we need to do something here */
642 log_warning_unit(UNIT(s)->id,
643 "Failed to watch PID "PID_FMT" from service %s",
648 static void service_set_state(Service *s, ServiceState state) {
649 ServiceState old_state;
650 const UnitActiveState *table;
654 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
656 old_state = s->state;
659 service_unwatch_pid_file(s);
662 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
664 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
666 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
667 SERVICE_AUTO_RESTART))
668 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
671 SERVICE_START, SERVICE_START_POST,
672 SERVICE_RUNNING, SERVICE_RELOAD,
673 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
675 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
676 service_unwatch_main_pid(s);
677 s->main_command = NULL;
681 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
683 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
685 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
686 service_unwatch_control_pid(s);
687 s->control_command = NULL;
688 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
691 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
692 unit_unwatch_all_pids(UNIT(s));
695 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
696 SERVICE_RUNNING, SERVICE_RELOAD,
697 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
698 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
699 !(state == SERVICE_DEAD && UNIT(s)->job)) {
700 service_close_socket_fd(s);
701 service_connection_unref(s);
704 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
705 service_stop_watchdog(s);
707 /* For the inactive states unit_notify() will trim the cgroup,
708 * but for exit we have to do that ourselves... */
709 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
710 unit_destroy_cgroup(UNIT(s));
712 /* For remain_after_exit services, let's see if we can "release" the
713 * hold on the console, since unit_notify() only does that in case of
715 if (state == SERVICE_EXITED && s->remain_after_exit &&
716 UNIT(s)->manager->n_on_console > 0) {
717 ExecContext *ec = unit_get_exec_context(UNIT(s));
718 if (ec && exec_context_may_touch_console(ec)) {
719 Manager *m = UNIT(s)->manager;
722 if (m->n_on_console == 0)
723 /* unset no_console_output flag, since the console is free */
724 m->no_console_output = false;
728 if (old_state != state)
729 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
731 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
732 s->reload_result = SERVICE_SUCCESS;
735 static int service_coldplug(Unit *u) {
736 Service *s = SERVICE(u);
740 assert(s->state == SERVICE_DEAD);
742 if (s->deserialized_state != s->state) {
744 if (IN_SET(s->deserialized_state,
745 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
747 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
749 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
753 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
755 /* For the start/stop timeouts 0 means off */
757 r = service_arm_timer(s, k);
763 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
765 /* The restart timeouts 0 means immediately */
766 r = service_arm_timer(s, s->restart_usec);
771 if (pid_is_unwaited(s->main_pid) &&
772 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
773 IN_SET(s->deserialized_state,
774 SERVICE_START, SERVICE_START_POST,
775 SERVICE_RUNNING, SERVICE_RELOAD,
776 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
778 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
779 r = unit_watch_pid(UNIT(s), s->main_pid);
784 if (pid_is_unwaited(s->control_pid) &&
785 IN_SET(s->deserialized_state,
786 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
788 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
790 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
791 r = unit_watch_pid(UNIT(s), s->control_pid);
796 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
797 unit_watch_all_pids(UNIT(s));
799 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
800 service_start_watchdog(s);
802 service_set_state(s, s->deserialized_state);
808 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
819 if (s->socket_fd >= 0)
822 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
827 if (u->type != UNIT_SOCKET)
832 r = socket_collect_fds(sock, &cfds, &cn_fds);
845 t = new(int, rn_fds+cn_fds);
852 memcpy(t, rfds, rn_fds * sizeof(int));
853 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
858 rn_fds = rn_fds+cn_fds;
873 static int service_spawn(
878 bool apply_permissions,
880 bool apply_tty_stdin,
881 bool set_notify_socket,
888 _cleanup_free_ int *fdsbuf = NULL;
889 unsigned n_fds = 0, n_env = 0;
890 _cleanup_strv_free_ char
891 **argv = NULL, **final_env = NULL, **our_env = NULL;
898 unit_realize_cgroup(UNIT(s));
900 r = unit_setup_exec_runtime(UNIT(s));
905 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
906 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
907 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
909 if (s->socket_fd >= 0) {
913 r = service_collect_fds(s, &fdsbuf, &n_fds);
921 if (timeout && s->timeout_start_usec > 0) {
922 r = service_arm_timer(s, s->timeout_start_usec);
926 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
928 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
932 our_env = new0(char*, 4);
938 if (set_notify_socket)
939 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
945 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
950 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
951 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
956 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
962 if (is_control && UNIT(s)->cgroup_path) {
963 path = strappenda(UNIT(s)->cgroup_path, "/control");
964 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
966 path = UNIT(s)->cgroup_path;
976 UNIT(s)->manager->confirm_spawn,
977 UNIT(s)->manager->cgroup_supported,
979 manager_get_runtime_prefix(UNIT(s)->manager),
982 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
988 r = unit_watch_pid(UNIT(s), pid);
990 /* FIXME: we need to do something here */
999 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1004 static int main_pid_good(Service *s) {
1007 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1010 /* If we know the pid file, then lets just check if it is
1012 if (s->main_pid_known) {
1014 /* If it's an alien child let's check if it is still
1016 if (s->main_pid_alien && s->main_pid > 0)
1017 return pid_is_alive(s->main_pid);
1019 /* .. otherwise assume we'll get a SIGCHLD for it,
1020 * which we really should wait for to collect exit
1021 * status and code */
1022 return s->main_pid > 0;
1025 /* We don't know the pid */
1029 _pure_ static int control_pid_good(Service *s) {
1032 return s->control_pid > 0;
1035 static int cgroup_good(Service *s) {
1040 if (!UNIT(s)->cgroup_path)
1043 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1050 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
1052 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1056 if (f != SERVICE_SUCCESS)
1059 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1061 if (s->result != SERVICE_SUCCESS)
1062 service_execute_action(s, s->failure_action, "failed", false);
1064 if (allow_restart &&
1065 !s->forbid_restart &&
1066 (s->restart == SERVICE_RESTART_ALWAYS ||
1067 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1068 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1069 (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
1070 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1071 (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) ||
1072 (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) ||
1073 (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)))) &&
1074 (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) &&
1075 (!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)))) {
1077 r = service_arm_timer(s, s->restart_usec);
1081 service_set_state(s, SERVICE_AUTO_RESTART);
1084 s->forbid_restart = false;
1086 /* We want fresh tmpdirs in case service is started again immediately */
1087 exec_runtime_destroy(s->exec_runtime);
1088 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1090 /* Also, remove the runtime directory in */
1091 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1093 /* Try to delete the pid file. At this point it will be
1094 * out-of-date, and some software might be confused by it, so
1095 * let's remove it. */
1097 unlink_noerrno(s->pid_file);
1102 log_warning_unit(UNIT(s)->id,
1103 "%s failed to run install restart timer: %s",
1104 UNIT(s)->id, strerror(-r));
1105 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1108 static void service_enter_stop_post(Service *s, ServiceResult f) {
1112 if (f != SERVICE_SUCCESS)
1115 service_unwatch_control_pid(s);
1116 unit_watch_all_pids(UNIT(s));
1118 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1119 if (s->control_command) {
1120 s->control_command_id = SERVICE_EXEC_STOP_POST;
1122 r = service_spawn(s,
1126 !s->permissions_start_only,
1127 !s->root_directory_start_only,
1135 service_set_state(s, SERVICE_STOP_POST);
1137 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1142 log_warning_unit(UNIT(s)->id,
1143 "%s failed to run 'stop-post' task: %s",
1144 UNIT(s)->id, strerror(-r));
1145 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1148 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1153 if (f != SERVICE_SUCCESS)
1156 unit_watch_all_pids(UNIT(s));
1158 r = unit_kill_context(
1161 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1170 if (s->timeout_stop_usec > 0) {
1171 r = service_arm_timer(s, s->timeout_stop_usec);
1176 service_set_state(s, state);
1177 } else if (state == SERVICE_STOP_SIGTERM)
1178 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1179 else if (state == SERVICE_STOP_SIGKILL)
1180 service_enter_stop_post(s, SERVICE_SUCCESS);
1181 else if (state == SERVICE_FINAL_SIGTERM)
1182 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1184 service_enter_dead(s, SERVICE_SUCCESS, true);
1189 log_warning_unit(UNIT(s)->id,
1190 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1192 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1193 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1195 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1198 static void service_enter_stop(Service *s, ServiceResult f) {
1203 if (f != SERVICE_SUCCESS)
1206 service_unwatch_control_pid(s);
1207 unit_watch_all_pids(UNIT(s));
1209 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1210 if (s->control_command) {
1211 s->control_command_id = SERVICE_EXEC_STOP;
1213 r = service_spawn(s,
1217 !s->permissions_start_only,
1218 !s->root_directory_start_only,
1226 service_set_state(s, SERVICE_STOP);
1228 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1233 log_warning_unit(UNIT(s)->id,
1234 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
1235 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1238 static void service_enter_running(Service *s, ServiceResult f) {
1239 int main_pid_ok, cgroup_ok;
1242 if (f != SERVICE_SUCCESS)
1245 main_pid_ok = main_pid_good(s);
1246 cgroup_ok = cgroup_good(s);
1248 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1249 (s->bus_name_good || s->type != SERVICE_DBUS))
1250 service_set_state(s, SERVICE_RUNNING);
1251 else if (s->remain_after_exit)
1252 service_set_state(s, SERVICE_EXITED);
1254 service_enter_stop(s, SERVICE_SUCCESS);
1257 static void service_enter_start_post(Service *s) {
1261 service_unwatch_control_pid(s);
1262 service_reset_watchdog(s);
1264 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1265 if (s->control_command) {
1266 s->control_command_id = SERVICE_EXEC_START_POST;
1268 r = service_spawn(s,
1272 !s->permissions_start_only,
1273 !s->root_directory_start_only,
1281 service_set_state(s, SERVICE_START_POST);
1283 service_enter_running(s, SERVICE_SUCCESS);
1288 log_warning_unit(UNIT(s)->id,
1289 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
1290 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1293 static void service_kill_control_processes(Service *s) {
1296 if (!UNIT(s)->cgroup_path)
1299 p = strappenda(UNIT(s)->cgroup_path, "/control");
1300 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1303 static void service_enter_start(Service *s) {
1310 assert(s->exec_command[SERVICE_EXEC_START]);
1311 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1313 service_unwatch_control_pid(s);
1314 service_unwatch_main_pid(s);
1316 /* We want to ensure that nobody leaks processes from
1317 * START_PRE here, so let's go on a killing spree, People
1318 * should not spawn long running processes from START_PRE. */
1319 service_kill_control_processes(s);
1321 if (s->type == SERVICE_FORKING) {
1322 s->control_command_id = SERVICE_EXEC_START;
1323 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1325 s->main_command = NULL;
1327 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1328 s->control_command = NULL;
1330 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1333 r = service_spawn(s,
1335 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS ||
1336 s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
1341 s->notify_access != NOTIFY_NONE,
1347 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
1348 /* For simple services we immediately start
1349 * the START_POST binaries. */
1351 service_set_main_pid(s, pid);
1352 service_enter_start_post(s);
1354 } else if (s->type == SERVICE_FORKING) {
1356 /* For forking services we wait until the start
1357 * process exited. */
1359 s->control_pid = pid;
1360 service_set_state(s, SERVICE_START);
1362 } else if (s->type == SERVICE_ONESHOT ||
1363 s->type == SERVICE_DBUS ||
1364 s->type == SERVICE_NOTIFY) {
1366 /* For oneshot services we wait until the start
1367 * process exited, too, but it is our main process. */
1369 /* For D-Bus services we know the main pid right away,
1370 * but wait for the bus name to appear on the
1371 * bus. Notify services are similar. */
1373 service_set_main_pid(s, pid);
1374 service_set_state(s, SERVICE_START);
1376 assert_not_reached("Unknown service type");
1381 log_warning_unit(UNIT(s)->id,
1382 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
1383 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1386 static void service_enter_start_pre(Service *s) {
1391 service_unwatch_control_pid(s);
1393 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1394 if (s->control_command) {
1395 /* Before we start anything, let's clear up what might
1396 * be left from previous runs. */
1397 service_kill_control_processes(s);
1399 s->control_command_id = SERVICE_EXEC_START_PRE;
1401 r = service_spawn(s,
1405 !s->permissions_start_only,
1406 !s->root_directory_start_only,
1414 service_set_state(s, SERVICE_START_PRE);
1416 service_enter_start(s);
1421 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
1422 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1425 static void service_enter_restart(Service *s) {
1426 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1431 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1432 /* Don't restart things if we are going down anyway */
1433 log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart.");
1435 r = service_arm_timer(s, s->restart_usec);
1442 /* Any units that are bound to this service must also be
1443 * restarted. We use JOB_RESTART (instead of the more obvious
1444 * JOB_START) here so that those dependency jobs will be added
1446 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1450 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1451 * it will be canceled as part of the service_stop() call that
1452 * is executed as part of JOB_RESTART. */
1454 log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id);
1458 log_warning_unit(UNIT(s)->id,
1459 "%s failed to schedule restart job: %s",
1460 UNIT(s)->id, bus_error_message(&error, -r));
1461 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1464 static void service_enter_reload(Service *s) {
1469 service_unwatch_control_pid(s);
1471 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1472 if (s->control_command) {
1473 s->control_command_id = SERVICE_EXEC_RELOAD;
1475 r = service_spawn(s,
1479 !s->permissions_start_only,
1480 !s->root_directory_start_only,
1488 service_set_state(s, SERVICE_RELOAD);
1490 service_enter_running(s, SERVICE_SUCCESS);
1495 log_warning_unit(UNIT(s)->id,
1496 "%s failed to run 'reload' task: %s",
1497 UNIT(s)->id, strerror(-r));
1498 s->reload_result = SERVICE_FAILURE_RESOURCES;
1499 service_enter_running(s, SERVICE_SUCCESS);
1502 static void service_run_next_control(Service *s) {
1506 assert(s->control_command);
1507 assert(s->control_command->command_next);
1509 assert(s->control_command_id != SERVICE_EXEC_START);
1511 s->control_command = s->control_command->command_next;
1512 service_unwatch_control_pid(s);
1514 r = service_spawn(s,
1518 !s->permissions_start_only,
1519 !s->root_directory_start_only,
1520 s->control_command_id == SERVICE_EXEC_START_PRE ||
1521 s->control_command_id == SERVICE_EXEC_STOP_POST,
1531 log_warning_unit(UNIT(s)->id,
1532 "%s failed to run next control task: %s",
1533 UNIT(s)->id, strerror(-r));
1535 if (s->state == SERVICE_START_PRE)
1536 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1537 else if (s->state == SERVICE_STOP)
1538 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1539 else if (s->state == SERVICE_STOP_POST)
1540 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1541 else if (s->state == SERVICE_RELOAD) {
1542 s->reload_result = SERVICE_FAILURE_RESOURCES;
1543 service_enter_running(s, SERVICE_SUCCESS);
1545 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1548 static void service_run_next_main(Service *s) {
1553 assert(s->main_command);
1554 assert(s->main_command->command_next);
1555 assert(s->type == SERVICE_ONESHOT);
1557 s->main_command = s->main_command->command_next;
1558 service_unwatch_main_pid(s);
1560 r = service_spawn(s,
1567 s->notify_access != NOTIFY_NONE,
1573 service_set_main_pid(s, pid);
1578 log_warning_unit(UNIT(s)->id,
1579 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
1580 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1583 static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
1586 if (action == SERVICE_FAILURE_ACTION_REBOOT ||
1587 action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
1588 update_reboot_param_file(s->reboot_arg);
1592 case SERVICE_FAILURE_ACTION_NONE:
1593 if (log_action_none)
1594 log_warning_unit(UNIT(s)->id,
1595 "%s %s, refusing to start.", UNIT(s)->id, reason);
1598 case SERVICE_FAILURE_ACTION_REBOOT: {
1599 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1602 log_warning_unit(UNIT(s)->id,
1603 "%s %s, rebooting.", UNIT(s)->id, reason);
1605 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
1606 SPECIAL_REBOOT_TARGET, JOB_REPLACE,
1607 true, &error, NULL);
1609 log_error_unit(UNIT(s)->id,
1610 "Failed to reboot: %s.", bus_error_message(&error, r));
1615 case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
1616 log_warning_unit(UNIT(s)->id,
1617 "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
1618 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
1621 case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
1622 log_warning_unit(UNIT(s)->id,
1623 "%s %s, rebooting immediately.", UNIT(s)->id, reason);
1625 if (s->reboot_arg) {
1626 log_info("Rebooting with argument '%s'.", s->reboot_arg);
1627 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
1628 LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
1631 log_info("Rebooting.");
1632 reboot(RB_AUTOBOOT);
1636 log_error_unit(UNIT(s)->id,
1637 "failure action=%i", action);
1638 assert_not_reached("Unknown FailureAction.");
1644 static int service_start_limit_test(Service *s) {
1647 if (ratelimit_test(&s->start_limit))
1650 return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
1653 static int service_start(Unit *u) {
1654 Service *s = SERVICE(u);
1659 /* We cannot fulfill this request right now, try again later
1661 if (s->state == SERVICE_STOP ||
1662 s->state == SERVICE_STOP_SIGTERM ||
1663 s->state == SERVICE_STOP_SIGKILL ||
1664 s->state == SERVICE_STOP_POST ||
1665 s->state == SERVICE_FINAL_SIGTERM ||
1666 s->state == SERVICE_FINAL_SIGKILL)
1669 /* Already on it! */
1670 if (s->state == SERVICE_START_PRE ||
1671 s->state == SERVICE_START ||
1672 s->state == SERVICE_START_POST)
1675 /* A service that will be restarted must be stopped first to
1676 * trigger BindsTo and/or OnFailure dependencies. If a user
1677 * does not want to wait for the holdoff time to elapse, the
1678 * service should be manually restarted, not started. We
1679 * simply return EAGAIN here, so that any start jobs stay
1680 * queued, and assume that the auto restart timer will
1681 * eventually trigger the restart. */
1682 if (s->state == SERVICE_AUTO_RESTART)
1685 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
1687 /* Make sure we don't enter a busy loop of some kind. */
1688 r = service_start_limit_test(s);
1690 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
1694 s->result = SERVICE_SUCCESS;
1695 s->reload_result = SERVICE_SUCCESS;
1696 s->main_pid_known = false;
1697 s->main_pid_alien = false;
1698 s->forbid_restart = false;
1700 free(s->status_text);
1701 s->status_text = NULL;
1702 s->status_errno = 0;
1704 service_enter_start_pre(s);
1708 static int service_stop(Unit *u) {
1709 Service *s = SERVICE(u);
1713 /* Don't create restart jobs from here. */
1714 s->forbid_restart = true;
1717 if (s->state == SERVICE_STOP ||
1718 s->state == SERVICE_STOP_SIGTERM ||
1719 s->state == SERVICE_STOP_SIGKILL ||
1720 s->state == SERVICE_STOP_POST ||
1721 s->state == SERVICE_FINAL_SIGTERM ||
1722 s->state == SERVICE_FINAL_SIGKILL)
1725 /* A restart will be scheduled or is in progress. */
1726 if (s->state == SERVICE_AUTO_RESTART) {
1727 service_set_state(s, SERVICE_DEAD);
1731 /* If there's already something running we go directly into
1733 if (s->state == SERVICE_START_PRE ||
1734 s->state == SERVICE_START ||
1735 s->state == SERVICE_START_POST ||
1736 s->state == SERVICE_RELOAD) {
1737 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1741 assert(s->state == SERVICE_RUNNING ||
1742 s->state == SERVICE_EXITED);
1744 service_enter_stop(s, SERVICE_SUCCESS);
1748 static int service_reload(Unit *u) {
1749 Service *s = SERVICE(u);
1753 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1755 service_enter_reload(s);
1759 _pure_ static bool service_can_reload(Unit *u) {
1760 Service *s = SERVICE(u);
1764 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1767 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1768 Service *s = SERVICE(u);
1774 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1775 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1776 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
1778 if (s->control_pid > 0)
1779 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1782 if (s->main_pid_known && s->main_pid > 0)
1783 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
1785 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1788 unit_serialize_item(u, f, "status-text", s->status_text);
1790 /* FIXME: There's a minor uncleanliness here: if there are
1791 * multiple commands attached here, we will start from the
1792 * first one again */
1793 if (s->control_command_id >= 0)
1794 unit_serialize_item(u, f, "control-command",
1795 service_exec_command_to_string(s->control_command_id));
1797 if (s->socket_fd >= 0) {
1800 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1803 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1806 if (s->main_exec_status.pid > 0) {
1807 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1808 s->main_exec_status.pid);
1809 dual_timestamp_serialize(f, "main-exec-status-start",
1810 &s->main_exec_status.start_timestamp);
1811 dual_timestamp_serialize(f, "main-exec-status-exit",
1812 &s->main_exec_status.exit_timestamp);
1814 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
1815 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1816 s->main_exec_status.code);
1817 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1818 s->main_exec_status.status);
1821 if (dual_timestamp_is_set(&s->watchdog_timestamp))
1822 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
1824 if (s->forbid_restart)
1825 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
1830 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1831 Service *s = SERVICE(u);
1838 if (streq(key, "state")) {
1841 state = service_state_from_string(value);
1843 log_debug_unit(u->id, "Failed to parse state value %s", value);
1845 s->deserialized_state = state;
1846 } else if (streq(key, "result")) {
1849 f = service_result_from_string(value);
1851 log_debug_unit(u->id, "Failed to parse result value %s", value);
1852 else if (f != SERVICE_SUCCESS)
1855 } else if (streq(key, "reload-result")) {
1858 f = service_result_from_string(value);
1860 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
1861 else if (f != SERVICE_SUCCESS)
1862 s->reload_result = f;
1864 } else if (streq(key, "control-pid")) {
1867 if (parse_pid(value, &pid) < 0)
1868 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
1870 s->control_pid = pid;
1871 } else if (streq(key, "main-pid")) {
1874 if (parse_pid(value, &pid) < 0)
1875 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
1877 service_set_main_pid(s, pid);
1878 unit_watch_pid(UNIT(s), pid);
1880 } else if (streq(key, "main-pid-known")) {
1883 b = parse_boolean(value);
1885 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
1887 s->main_pid_known = b;
1888 } else if (streq(key, "status-text")) {
1895 free(s->status_text);
1899 } else if (streq(key, "control-command")) {
1900 ServiceExecCommand id;
1902 id = service_exec_command_from_string(value);
1904 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
1906 s->control_command_id = id;
1907 s->control_command = s->exec_command[id];
1909 } else if (streq(key, "socket-fd")) {
1912 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1913 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
1916 asynchronous_close(s->socket_fd);
1917 s->socket_fd = fdset_remove(fds, fd);
1919 } else if (streq(key, "main-exec-status-pid")) {
1922 if (parse_pid(value, &pid) < 0)
1923 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
1925 s->main_exec_status.pid = pid;
1926 } else if (streq(key, "main-exec-status-code")) {
1929 if (safe_atoi(value, &i) < 0)
1930 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
1932 s->main_exec_status.code = i;
1933 } else if (streq(key, "main-exec-status-status")) {
1936 if (safe_atoi(value, &i) < 0)
1937 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
1939 s->main_exec_status.status = i;
1940 } else if (streq(key, "main-exec-status-start"))
1941 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
1942 else if (streq(key, "main-exec-status-exit"))
1943 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
1944 else if (streq(key, "watchdog-timestamp"))
1945 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
1946 else if (streq(key, "forbid-restart")) {
1949 b = parse_boolean(value);
1951 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
1953 s->forbid_restart = b;
1955 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
1960 _pure_ static UnitActiveState service_active_state(Unit *u) {
1961 const UnitActiveState *table;
1965 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1967 return table[SERVICE(u)->state];
1970 static const char *service_sub_state_to_string(Unit *u) {
1973 return service_state_to_string(SERVICE(u)->state);
1976 static bool service_check_gc(Unit *u) {
1977 Service *s = SERVICE(u);
1981 /* Never clean up services that still have a process around,
1982 * even if the service is formally dead. */
1983 if (cgroup_good(s) > 0 ||
1984 main_pid_good(s) > 0 ||
1985 control_pid_good(s) > 0)
1991 _pure_ static bool service_check_snapshot(Unit *u) {
1992 Service *s = SERVICE(u);
1996 return (s->socket_fd < 0);
1999 static int service_retry_pid_file(Service *s) {
2002 assert(s->pid_file);
2003 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2005 r = service_load_pid_file(s, false);
2009 service_unwatch_pid_file(s);
2011 service_enter_running(s, SERVICE_SUCCESS);
2015 static int service_watch_pid_file(Service *s) {
2018 log_debug_unit(UNIT(s)->id,
2019 "Setting watch for %s's PID file %s",
2020 UNIT(s)->id, s->pid_file_pathspec->path);
2021 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2025 /* the pidfile might have appeared just before we set the watch */
2026 log_debug_unit(UNIT(s)->id,
2027 "Trying to read %s's PID file %s in case it changed",
2028 UNIT(s)->id, s->pid_file_pathspec->path);
2029 service_retry_pid_file(s);
2033 log_error_unit(UNIT(s)->id,
2034 "Failed to set a watch for %s's PID file %s: %s",
2035 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
2036 service_unwatch_pid_file(s);
2040 static int service_demand_pid_file(Service *s) {
2043 assert(s->pid_file);
2044 assert(!s->pid_file_pathspec);
2046 ps = new0(PathSpec, 1);
2051 ps->path = strdup(s->pid_file);
2057 path_kill_slashes(ps->path);
2059 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2060 * keep their PID file open all the time. */
2061 ps->type = PATH_MODIFIED;
2062 ps->inotify_fd = -1;
2064 s->pid_file_pathspec = ps;
2066 return service_watch_pid_file(s);
2069 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2070 PathSpec *p = userdata;
2075 s = SERVICE(p->unit);
2079 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2080 assert(s->pid_file_pathspec);
2081 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2083 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2085 if (path_spec_fd_event(p, events) < 0)
2088 if (service_retry_pid_file(s) == 0)
2091 if (service_watch_pid_file(s) < 0)
2097 service_unwatch_pid_file(s);
2098 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2102 static void service_notify_cgroup_empty_event(Unit *u) {
2103 Service *s = SERVICE(u);
2107 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2111 /* Waiting for SIGCHLD is usually more interesting,
2112 * because it includes return codes/signals. Which is
2113 * why we ignore the cgroup events for most cases,
2114 * except when we don't know pid which to expect the
2118 case SERVICE_START_POST:
2119 /* If we were hoping for the daemon to write its PID file,
2120 * we can give up now. */
2121 if (s->pid_file_pathspec) {
2122 log_warning_unit(u->id,
2123 "%s never wrote its PID file. Failing.", UNIT(s)->id);
2124 service_unwatch_pid_file(s);
2125 if (s->state == SERVICE_START)
2126 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2128 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2132 case SERVICE_RUNNING:
2133 /* service_enter_running() will figure out what to do */
2134 service_enter_running(s, SERVICE_SUCCESS);
2137 case SERVICE_STOP_SIGTERM:
2138 case SERVICE_STOP_SIGKILL:
2140 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2141 service_enter_stop_post(s, SERVICE_SUCCESS);
2145 case SERVICE_STOP_POST:
2146 case SERVICE_FINAL_SIGTERM:
2147 case SERVICE_FINAL_SIGKILL:
2148 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2149 service_enter_dead(s, SERVICE_SUCCESS, true);
2158 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2159 Service *s = SERVICE(u);
2165 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2166 is_clean_exit_lsb(code, status, &s->success_status))
2167 f = SERVICE_SUCCESS;
2168 else if (code == CLD_EXITED)
2169 f = SERVICE_FAILURE_EXIT_CODE;
2170 else if (code == CLD_KILLED)
2171 f = SERVICE_FAILURE_SIGNAL;
2172 else if (code == CLD_DUMPED)
2173 f = SERVICE_FAILURE_CORE_DUMP;
2175 assert_not_reached("Unknown code");
2177 if (s->main_pid == pid) {
2178 /* Forking services may occasionally move to a new PID.
2179 * As long as they update the PID file before exiting the old
2180 * PID, they're fine. */
2181 if (service_load_pid_file(s, false) == 0)
2185 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2187 if (s->main_command) {
2188 /* If this is not a forking service than the
2189 * main process got started and hence we copy
2190 * the exit status so that it is recorded both
2191 * as main and as control process exit
2194 s->main_command->exec_status = s->main_exec_status;
2196 if (s->main_command->ignore)
2197 f = SERVICE_SUCCESS;
2198 } else if (s->exec_command[SERVICE_EXEC_START]) {
2200 /* If this is a forked process, then we should
2201 * ignore the return value if this was
2202 * configured for the starter process */
2204 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2205 f = SERVICE_SUCCESS;
2208 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2210 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2211 u->id, sigchld_code_to_string(code), status,
2212 strna(code == CLD_EXITED
2213 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2214 : signal_to_string(status)),
2215 "EXIT_CODE=%s", sigchld_code_to_string(code),
2216 "EXIT_STATUS=%i", status,
2219 if (f != SERVICE_SUCCESS)
2222 if (s->main_command &&
2223 s->main_command->command_next &&
2224 f == SERVICE_SUCCESS) {
2226 /* There is another command to *
2227 * execute, so let's do that. */
2229 log_debug_unit(u->id,
2230 "%s running next main command for state %s",
2231 u->id, service_state_to_string(s->state));
2232 service_run_next_main(s);
2236 /* The service exited, so the service is officially
2238 s->main_command = NULL;
2242 case SERVICE_START_POST:
2243 case SERVICE_RELOAD:
2245 /* Need to wait until the operation is
2250 if (s->type == SERVICE_ONESHOT) {
2251 /* This was our main goal, so let's go on */
2252 if (f == SERVICE_SUCCESS)
2253 service_enter_start_post(s);
2255 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2261 case SERVICE_RUNNING:
2262 service_enter_running(s, f);
2265 case SERVICE_STOP_SIGTERM:
2266 case SERVICE_STOP_SIGKILL:
2268 if (!control_pid_good(s))
2269 service_enter_stop_post(s, f);
2271 /* If there is still a control process, wait for that first */
2274 case SERVICE_STOP_POST:
2275 case SERVICE_FINAL_SIGTERM:
2276 case SERVICE_FINAL_SIGKILL:
2278 if (!control_pid_good(s))
2279 service_enter_dead(s, f, true);
2283 assert_not_reached("Uh, main process died at wrong time.");
2287 } else if (s->control_pid == pid) {
2290 if (s->control_command) {
2291 exec_status_exit(&s->control_command->exec_status,
2292 &s->exec_context, pid, code, status);
2294 if (s->control_command->ignore)
2295 f = SERVICE_SUCCESS;
2298 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2299 "%s: control process exited, code=%s status=%i",
2300 u->id, sigchld_code_to_string(code), status);
2302 if (f != SERVICE_SUCCESS)
2305 /* Immediately get rid of the cgroup, so that the
2306 * kernel doesn't delay the cgroup empty messages for
2307 * the service cgroup any longer than necessary */
2308 service_kill_control_processes(s);
2310 if (s->control_command &&
2311 s->control_command->command_next &&
2312 f == SERVICE_SUCCESS) {
2314 /* There is another command to *
2315 * execute, so let's do that. */
2317 log_debug_unit(u->id,
2318 "%s running next control command for state %s",
2319 u->id, service_state_to_string(s->state));
2320 service_run_next_control(s);
2323 /* No further commands for this step, so let's
2324 * figure out what to do next */
2326 s->control_command = NULL;
2327 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2329 log_debug_unit(u->id,
2330 "%s got final SIGCHLD for state %s",
2331 u->id, service_state_to_string(s->state));
2335 case SERVICE_START_PRE:
2336 if (f == SERVICE_SUCCESS)
2337 service_enter_start(s);
2339 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2343 if (s->type != SERVICE_FORKING)
2344 /* Maybe spurious event due to a reload that changed the type? */
2347 if (f != SERVICE_SUCCESS) {
2348 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2353 bool has_start_post;
2356 /* Let's try to load the pid file here if we can.
2357 * The PID file might actually be created by a START_POST
2358 * script. In that case don't worry if the loading fails. */
2360 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2361 r = service_load_pid_file(s, !has_start_post);
2362 if (!has_start_post && r < 0) {
2363 r = service_demand_pid_file(s);
2364 if (r < 0 || !cgroup_good(s))
2365 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2369 service_search_main_pid(s);
2371 service_enter_start_post(s);
2374 case SERVICE_START_POST:
2375 if (f != SERVICE_SUCCESS) {
2376 service_enter_stop(s, f);
2383 r = service_load_pid_file(s, true);
2385 r = service_demand_pid_file(s);
2386 if (r < 0 || !cgroup_good(s))
2387 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2391 service_search_main_pid(s);
2393 service_enter_running(s, SERVICE_SUCCESS);
2396 case SERVICE_RELOAD:
2397 if (f == SERVICE_SUCCESS) {
2398 service_load_pid_file(s, true);
2399 service_search_main_pid(s);
2402 s->reload_result = f;
2403 service_enter_running(s, SERVICE_SUCCESS);
2407 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2410 case SERVICE_STOP_SIGTERM:
2411 case SERVICE_STOP_SIGKILL:
2412 if (main_pid_good(s) <= 0)
2413 service_enter_stop_post(s, f);
2415 /* If there is still a service
2416 * process around, wait until
2417 * that one quit, too */
2420 case SERVICE_STOP_POST:
2421 case SERVICE_FINAL_SIGTERM:
2422 case SERVICE_FINAL_SIGKILL:
2423 if (main_pid_good(s) <= 0)
2424 service_enter_dead(s, f, true);
2428 assert_not_reached("Uh, control process died at wrong time.");
2433 /* Notify clients about changed exit status */
2434 unit_add_to_dbus_queue(u);
2436 /* We got one SIGCHLD for the service, let's watch all
2437 * processes that are now running of the service, and watch
2438 * that. Among the PIDs we then watch will be children
2439 * reassigned to us, which hopefully allows us to identify
2440 * when all children are gone */
2441 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2442 unit_watch_all_pids(u);
2444 /* If the PID set is empty now, then let's finish this off */
2445 if (set_isempty(u->pids))
2446 service_notify_cgroup_empty_event(u);
2449 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2450 Service *s = SERVICE(userdata);
2453 assert(source == s->timer_event_source);
2457 case SERVICE_START_PRE:
2459 log_warning_unit(UNIT(s)->id,
2460 "%s %s operation timed out. Terminating.",
2462 s->state == SERVICE_START ? "start" : "start-pre");
2463 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2466 case SERVICE_START_POST:
2467 log_warning_unit(UNIT(s)->id,
2468 "%s start-post operation timed out. Stopping.", UNIT(s)->id);
2469 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2472 case SERVICE_RELOAD:
2473 log_warning_unit(UNIT(s)->id,
2474 "%s reload operation timed out. Stopping.", UNIT(s)->id);
2475 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2476 service_enter_running(s, SERVICE_SUCCESS);
2480 log_warning_unit(UNIT(s)->id,
2481 "%s stopping timed out. Terminating.", UNIT(s)->id);
2482 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2485 case SERVICE_STOP_SIGTERM:
2486 if (s->kill_context.send_sigkill) {
2487 log_warning_unit(UNIT(s)->id,
2488 "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
2489 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2491 log_warning_unit(UNIT(s)->id,
2492 "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
2493 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2498 case SERVICE_STOP_SIGKILL:
2499 /* Uh, we sent a SIGKILL and it is still not gone?
2500 * Must be something we cannot kill, so let's just be
2501 * weirded out and continue */
2503 log_warning_unit(UNIT(s)->id,
2504 "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
2505 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2508 case SERVICE_STOP_POST:
2509 log_warning_unit(UNIT(s)->id,
2510 "%s stop-post timed out. Terminating.", UNIT(s)->id);
2511 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2514 case SERVICE_FINAL_SIGTERM:
2515 if (s->kill_context.send_sigkill) {
2516 log_warning_unit(UNIT(s)->id,
2517 "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
2518 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2520 log_warning_unit(UNIT(s)->id,
2521 "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.",
2523 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2528 case SERVICE_FINAL_SIGKILL:
2529 log_warning_unit(UNIT(s)->id,
2530 "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
2531 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2534 case SERVICE_AUTO_RESTART:
2535 log_info_unit(UNIT(s)->id,
2536 s->restart_usec > 0 ?
2537 "%s holdoff time over, scheduling restart." :
2538 "%s has no holdoff time, scheduling restart.",
2540 service_enter_restart(s);
2544 assert_not_reached("Timeout at wrong time.");
2550 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2551 Service *s = SERVICE(userdata);
2552 char t[FORMAT_TIMESPAN_MAX];
2555 assert(source == s->watchdog_event_source);
2557 log_error_unit(UNIT(s)->id,
2558 "%s watchdog timeout (limit %s)!",
2560 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
2561 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
2566 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2567 Service *s = SERVICE(u);
2569 bool notify_dbus = false;
2573 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
2574 u->id, pid, tags && *tags ? tags[0] : "(empty)");
2576 if (s->notify_access == NOTIFY_NONE) {
2577 log_warning_unit(u->id,
2578 "%s: Got notification message from PID "PID_FMT", but reception is disabled.",
2583 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2584 if (s->main_pid != 0)
2585 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);
2587 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);
2591 /* Interpret MAINPID= */
2592 e = strv_find_prefix(tags, "MAINPID=");
2593 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
2594 if (parse_pid(e + 8, &pid) < 0)
2595 log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
2597 log_debug_unit(u->id, "%s: got %s", u->id, e);
2598 service_set_main_pid(s, pid);
2599 unit_watch_pid(UNIT(s), pid);
2604 /* Interpret READY= */
2605 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) {
2606 log_debug_unit(u->id, "%s: got READY=1", u->id);
2607 service_enter_start_post(s);
2611 /* Interpret STATUS= */
2612 e = strv_find_prefix(tags, "STATUS=");
2617 if (!utf8_is_valid(e+7)) {
2618 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
2622 log_debug_unit(u->id, "%s: got %s", u->id, e);
2633 if (!streq_ptr(s->status_text, t)) {
2634 free(s->status_text);
2641 /* Interpret ERRNO= */
2642 e = strv_find_prefix(tags, "ERRNO=");
2646 if (safe_atoi(e + 6, &status_errno) < 0 || status_errno < 0)
2647 log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
2649 log_debug_unit(u->id, "%s: got %s", u->id, e);
2651 if (s->status_errno != status_errno) {
2652 s->status_errno = status_errno;
2658 /* Interpret WATCHDOG= */
2659 if (strv_find(tags, "WATCHDOG=1")) {
2660 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
2661 service_reset_watchdog(s);
2664 /* Notify clients about changed status or main pid */
2666 unit_add_to_dbus_queue(u);
2669 static int service_get_timeout(Unit *u, uint64_t *timeout) {
2670 Service *s = SERVICE(u);
2673 if (!s->timer_event_source)
2676 r = sd_event_source_get_time(s->timer_event_source, timeout);
2683 static void service_bus_name_owner_change(
2686 const char *old_owner,
2687 const char *new_owner) {
2689 Service *s = SERVICE(u);
2695 assert(streq(s->bus_name, name));
2696 assert(old_owner || new_owner);
2698 if (old_owner && new_owner)
2699 log_debug_unit(u->id,
2700 "%s's D-Bus name %s changed owner from %s to %s",
2701 u->id, name, old_owner, new_owner);
2703 log_debug_unit(u->id,
2704 "%s's D-Bus name %s no longer registered by %s",
2705 u->id, name, old_owner);
2707 log_debug_unit(u->id,
2708 "%s's D-Bus name %s now registered by %s",
2709 u->id, name, new_owner);
2711 s->bus_name_good = !!new_owner;
2713 if (s->type == SERVICE_DBUS) {
2715 /* service_enter_running() will figure out what to
2717 if (s->state == SERVICE_RUNNING)
2718 service_enter_running(s, SERVICE_SUCCESS);
2719 else if (s->state == SERVICE_START && new_owner)
2720 service_enter_start_post(s);
2722 } else if (new_owner &&
2724 (s->state == SERVICE_START ||
2725 s->state == SERVICE_START_POST ||
2726 s->state == SERVICE_RUNNING ||
2727 s->state == SERVICE_RELOAD)) {
2729 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
2732 /* Try to acquire PID from bus service */
2734 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
2736 r = sd_bus_creds_get_pid(creds, &pid);
2738 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
2740 service_set_main_pid(s, pid);
2741 unit_watch_pid(UNIT(s), pid);
2746 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2747 _cleanup_free_ char *peer = NULL;
2753 /* This is called by the socket code when instantiating a new
2754 * service for a stream socket and the socket needs to be
2757 if (UNIT(s)->load_state != UNIT_LOADED)
2760 if (s->socket_fd >= 0)
2763 if (s->state != SERVICE_DEAD)
2766 if (getpeername_pretty(fd, &peer) >= 0) {
2768 if (UNIT(s)->description) {
2769 _cleanup_free_ char *a;
2771 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2775 r = unit_set_description(UNIT(s), a);
2777 r = unit_set_description(UNIT(s), peer);
2785 unit_ref_set(&s->accept_socket, UNIT(sock));
2787 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
2790 static void service_reset_failed(Unit *u) {
2791 Service *s = SERVICE(u);
2795 if (s->state == SERVICE_FAILED)
2796 service_set_state(s, SERVICE_DEAD);
2798 s->result = SERVICE_SUCCESS;
2799 s->reload_result = SERVICE_SUCCESS;
2801 RATELIMIT_RESET(s->start_limit);
2804 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2805 Service *s = SERVICE(u);
2807 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
2810 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2811 [SERVICE_DEAD] = "dead",
2812 [SERVICE_START_PRE] = "start-pre",
2813 [SERVICE_START] = "start",
2814 [SERVICE_START_POST] = "start-post",
2815 [SERVICE_RUNNING] = "running",
2816 [SERVICE_EXITED] = "exited",
2817 [SERVICE_RELOAD] = "reload",
2818 [SERVICE_STOP] = "stop",
2819 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2820 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2821 [SERVICE_STOP_POST] = "stop-post",
2822 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2823 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2824 [SERVICE_FAILED] = "failed",
2825 [SERVICE_AUTO_RESTART] = "auto-restart",
2828 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2830 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2831 [SERVICE_RESTART_NO] = "no",
2832 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
2833 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
2834 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
2835 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
2836 [SERVICE_RESTART_ON_ABORT] = "on-abort",
2837 [SERVICE_RESTART_ALWAYS] = "always",
2840 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2842 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2843 [SERVICE_SIMPLE] = "simple",
2844 [SERVICE_FORKING] = "forking",
2845 [SERVICE_ONESHOT] = "oneshot",
2846 [SERVICE_DBUS] = "dbus",
2847 [SERVICE_NOTIFY] = "notify",
2848 [SERVICE_IDLE] = "idle"
2851 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2853 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2854 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2855 [SERVICE_EXEC_START] = "ExecStart",
2856 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2857 [SERVICE_EXEC_RELOAD] = "ExecReload",
2858 [SERVICE_EXEC_STOP] = "ExecStop",
2859 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2862 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2864 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2865 [NOTIFY_NONE] = "none",
2866 [NOTIFY_MAIN] = "main",
2867 [NOTIFY_ALL] = "all"
2870 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2872 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
2873 [SERVICE_SUCCESS] = "success",
2874 [SERVICE_FAILURE_RESOURCES] = "resources",
2875 [SERVICE_FAILURE_TIMEOUT] = "timeout",
2876 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
2877 [SERVICE_FAILURE_SIGNAL] = "signal",
2878 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
2879 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
2880 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
2883 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
2885 static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
2886 [SERVICE_FAILURE_ACTION_NONE] = "none",
2887 [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
2888 [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
2889 [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
2891 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
2893 const UnitVTable service_vtable = {
2894 .object_size = sizeof(Service),
2895 .exec_context_offset = offsetof(Service, exec_context),
2896 .cgroup_context_offset = offsetof(Service, cgroup_context),
2897 .kill_context_offset = offsetof(Service, kill_context),
2898 .exec_runtime_offset = offsetof(Service, exec_runtime),
2904 .private_section = "Service",
2906 .init = service_init,
2907 .done = service_done,
2908 .load = service_load,
2910 .coldplug = service_coldplug,
2912 .dump = service_dump,
2914 .start = service_start,
2915 .stop = service_stop,
2916 .reload = service_reload,
2918 .can_reload = service_can_reload,
2920 .kill = service_kill,
2922 .serialize = service_serialize,
2923 .deserialize_item = service_deserialize_item,
2925 .active_state = service_active_state,
2926 .sub_state_to_string = service_sub_state_to_string,
2928 .check_gc = service_check_gc,
2929 .check_snapshot = service_check_snapshot,
2931 .sigchld_event = service_sigchld_event,
2933 .reset_failed = service_reset_failed,
2935 .notify_cgroup_empty = service_notify_cgroup_empty_event,
2936 .notify_message = service_notify_message,
2938 .bus_name_owner_change = service_bus_name_owner_change,
2940 .bus_interface = "org.freedesktop.systemd1.Service",
2941 .bus_vtable = bus_service_vtable,
2942 .bus_set_property = bus_service_set_property,
2943 .bus_commit_properties = bus_service_commit_properties,
2945 .get_timeout = service_get_timeout,
2946 .can_transient = true,
2948 .status_message_formats = {
2949 .starting_stopping = {
2950 [0] = "Starting %s...",
2951 [1] = "Stopping %s...",
2953 .finished_start_job = {
2954 [JOB_DONE] = "Started %s.",
2955 [JOB_FAILED] = "Failed to start %s.",
2956 [JOB_DEPENDENCY] = "Dependency failed for %s.",
2957 [JOB_TIMEOUT] = "Timed out starting %s.",
2959 .finished_stop_job = {
2960 [JOB_DONE] = "Stopped %s.",
2961 [JOB_FAILED] = "Stopped (with error) %s.",
2962 [JOB_TIMEOUT] = "Timed out stopping %s.",