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/>.
31 #include "load-fragment.h"
32 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "unit-printf.h"
37 #include "dbus-service.h"
39 #include "exit-status.h"
41 #include "path-util.h"
46 #include "bus-error.h"
49 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
50 [SERVICE_DEAD] = UNIT_INACTIVE,
51 [SERVICE_START_PRE] = UNIT_ACTIVATING,
52 [SERVICE_START] = UNIT_ACTIVATING,
53 [SERVICE_START_POST] = UNIT_ACTIVATING,
54 [SERVICE_RUNNING] = UNIT_ACTIVE,
55 [SERVICE_EXITED] = UNIT_ACTIVE,
56 [SERVICE_RELOAD] = UNIT_RELOADING,
57 [SERVICE_STOP] = UNIT_DEACTIVATING,
58 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
59 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
60 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
61 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
62 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
63 [SERVICE_FAILED] = UNIT_FAILED,
64 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
67 /* For Type=idle we never want to delay any other jobs, hence we
68 * consider idle jobs active as soon as we start working on them */
69 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
70 [SERVICE_DEAD] = UNIT_INACTIVE,
71 [SERVICE_START_PRE] = UNIT_ACTIVE,
72 [SERVICE_START] = UNIT_ACTIVE,
73 [SERVICE_START_POST] = UNIT_ACTIVE,
74 [SERVICE_RUNNING] = UNIT_ACTIVE,
75 [SERVICE_EXITED] = UNIT_ACTIVE,
76 [SERVICE_RELOAD] = UNIT_RELOADING,
77 [SERVICE_STOP] = UNIT_DEACTIVATING,
78 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
79 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
80 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
81 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
82 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
83 [SERVICE_FAILED] = UNIT_FAILED,
84 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
87 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
88 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
89 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
91 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
92 static void service_enter_reload_by_notify(Service *s);
94 static void service_init(Unit *u) {
95 Service *s = SERVICE(u);
98 assert(u->load_state == UNIT_STUB);
100 s->timeout_start_usec = u->manager->default_timeout_start_usec;
101 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
102 s->restart_usec = u->manager->default_restart_usec;
103 s->type = _SERVICE_TYPE_INVALID;
105 s->guess_main_pid = true;
107 RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
109 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
112 static void service_unwatch_control_pid(Service *s) {
115 if (s->control_pid <= 0)
118 unit_unwatch_pid(UNIT(s), s->control_pid);
122 static void service_unwatch_main_pid(Service *s) {
125 if (s->main_pid <= 0)
128 unit_unwatch_pid(UNIT(s), s->main_pid);
132 static void service_unwatch_pid_file(Service *s) {
133 if (!s->pid_file_pathspec)
136 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
137 path_spec_unwatch(s->pid_file_pathspec);
138 path_spec_done(s->pid_file_pathspec);
139 free(s->pid_file_pathspec);
140 s->pid_file_pathspec = NULL;
143 static int service_set_main_pid(Service *s, pid_t pid) {
154 if (s->main_pid == pid && s->main_pid_known)
157 if (s->main_pid != pid) {
158 service_unwatch_main_pid(s);
159 exec_status_start(&s->main_exec_status, pid);
163 s->main_pid_known = true;
165 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
166 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);
167 s->main_pid_alien = true;
169 s->main_pid_alien = false;
174 static void service_close_socket_fd(Service *s) {
177 s->socket_fd = asynchronous_close(s->socket_fd);
180 static void service_connection_unref(Service *s) {
183 if (!UNIT_ISSET(s->accept_socket))
186 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
187 unit_ref_unset(&s->accept_socket);
190 static void service_stop_watchdog(Service *s) {
193 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
194 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
197 static void service_start_watchdog(Service *s) {
202 if (s->watchdog_usec <= 0)
205 if (s->watchdog_event_source) {
206 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
208 log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
212 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
214 r = sd_event_add_time(
215 UNIT(s)->manager->event,
216 &s->watchdog_event_source,
218 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
219 service_dispatch_watchdog, s);
221 log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
225 /* Let's process everything else which might be a sign
226 * of living before we consider a service died. */
227 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
231 log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
234 static void service_reset_watchdog(Service *s) {
237 dual_timestamp_get(&s->watchdog_timestamp);
238 service_start_watchdog(s);
241 static void service_done(Unit *u) {
242 Service *s = SERVICE(u);
249 free(s->status_text);
250 s->status_text = NULL;
253 s->reboot_arg = NULL;
255 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
256 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
257 s->control_command = NULL;
258 s->main_command = NULL;
260 exit_status_set_free(&s->restart_prevent_status);
261 exit_status_set_free(&s->restart_force_status);
262 exit_status_set_free(&s->success_status);
264 /* This will leak a process, but at least no memory or any of
266 service_unwatch_main_pid(s);
267 service_unwatch_control_pid(s);
268 service_unwatch_pid_file(s);
271 unit_unwatch_bus_name(u, s->bus_name);
276 service_close_socket_fd(s);
277 service_connection_unref(s);
279 unit_ref_unset(&s->accept_socket);
281 service_stop_watchdog(s);
283 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
286 static int service_arm_timer(Service *s, usec_t usec) {
291 if (s->timer_event_source) {
292 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
296 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
299 return sd_event_add_time(
300 UNIT(s)->manager->event,
301 &s->timer_event_source,
303 now(CLOCK_MONOTONIC) + usec, 0,
304 service_dispatch_timer, s);
307 static int service_verify(Service *s) {
310 if (UNIT(s)->load_state != UNIT_LOADED)
313 if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]) {
314 log_error_unit(UNIT(s)->id, "%s lacks both ExecStart= and ExecStop= setting. Refusing.", UNIT(s)->id);
318 if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) {
319 log_error_unit(UNIT(s)->id, "%s has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
323 if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START]) {
324 log_error_unit(UNIT(s)->id, "%s has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.", UNIT(s)->id);
328 if (s->type != SERVICE_ONESHOT && 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, SPECIAL_BASIC_TARGET, NULL, true);
372 /* Second, activate normal shutdown */
373 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
377 static void service_fix_output(Service *s) {
380 /* If nothing has been explicitly configured, patch default
381 * output in. If input is socket/tty we avoid this however,
382 * since in that case we want output to default to the same
383 * place as we read input from. */
385 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
386 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
387 s->exec_context.std_input == EXEC_INPUT_NULL)
388 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
390 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
391 s->exec_context.std_input == EXEC_INPUT_NULL)
392 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
395 static int service_load(Unit *u) {
396 Service *s = SERVICE(u);
401 /* Load a .service file */
402 r = unit_load_fragment(u);
406 /* Still nothing found? Then let's give up */
407 if (u->load_state == UNIT_STUB)
410 /* This is a new unit? Then let's add in some extras */
411 if (u->load_state == UNIT_LOADED) {
413 /* We were able to load something, then let's add in
414 * the dropin directories. */
415 r = unit_load_dropin(u);
419 if (s->type == _SERVICE_TYPE_INVALID) {
420 /* Figure out a type automatically */
422 s->type = SERVICE_DBUS;
423 else if (s->exec_command[SERVICE_EXEC_START])
424 s->type = SERVICE_SIMPLE;
426 s->type = SERVICE_ONESHOT;
429 /* Oneshot services have disabled start timeout by default */
430 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
431 s->timeout_start_usec = 0;
433 service_fix_output(s);
435 r = unit_patch_contexts(u);
439 r = unit_add_exec_dependencies(u, &s->exec_context);
443 r = unit_add_default_slice(u, &s->cgroup_context);
447 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
448 s->notify_access = NOTIFY_MAIN;
450 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
451 s->notify_access = NOTIFY_MAIN;
454 r = unit_watch_bus_name(u, s->bus_name);
459 if (u->default_dependencies) {
460 r = service_add_default_dependencies(s);
467 return service_verify(s);
470 static void service_dump(Unit *u, FILE *f, const char *prefix) {
471 ServiceExecCommand c;
472 Service *s = SERVICE(u);
477 prefix = strempty(prefix);
478 prefix2 = strappenda(prefix, "\t");
481 "%sService State: %s\n"
483 "%sReload Result: %s\n"
484 "%sPermissionsStartOnly: %s\n"
485 "%sRootDirectoryStartOnly: %s\n"
486 "%sRemainAfterExit: %s\n"
487 "%sGuessMainPID: %s\n"
490 "%sNotifyAccess: %s\n"
491 "%sNotifyState: %s\n",
492 prefix, service_state_to_string(s->state),
493 prefix, service_result_to_string(s->result),
494 prefix, service_result_to_string(s->reload_result),
495 prefix, yes_no(s->permissions_start_only),
496 prefix, yes_no(s->root_directory_start_only),
497 prefix, yes_no(s->remain_after_exit),
498 prefix, yes_no(s->guess_main_pid),
499 prefix, service_type_to_string(s->type),
500 prefix, service_restart_to_string(s->restart),
501 prefix, notify_access_to_string(s->notify_access),
502 prefix, notify_state_to_string(s->notify_state));
504 if (s->control_pid > 0)
506 "%sControl PID: "PID_FMT"\n",
507 prefix, s->control_pid);
511 "%sMain PID: "PID_FMT"\n"
512 "%sMain PID Known: %s\n"
513 "%sMain PID Alien: %s\n",
515 prefix, yes_no(s->main_pid_known),
516 prefix, yes_no(s->main_pid_alien));
521 prefix, s->pid_file);
526 "%sBus Name Good: %s\n",
528 prefix, yes_no(s->bus_name_good));
530 kill_context_dump(&s->kill_context, f, prefix);
531 exec_context_dump(&s->exec_context, f, prefix);
533 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
535 if (!s->exec_command[c])
538 fprintf(f, "%s-> %s:\n",
539 prefix, service_exec_command_to_string(c));
541 exec_command_dump_list(s->exec_command[c], f, prefix2);
544 #ifdef HAVE_SYSV_COMPAT
545 if (s->sysv_start_priority >= 0)
547 "%sSysVStartPriority: %i\n",
548 prefix, s->sysv_start_priority);
552 fprintf(f, "%sStatus Text: %s\n",
553 prefix, s->status_text);
556 static int service_load_pid_file(Service *s, bool may_warn) {
557 _cleanup_free_ char *k = NULL;
566 r = read_one_line_file(s->pid_file, &k);
569 log_info_unit(UNIT(s)->id, "PID file %s not readable (yet?) after %s.", s->pid_file, service_state_to_string(s->state));
573 r = parse_pid(k, &pid);
576 log_info_unit(UNIT(s)->id, "Failed to read PID from file %s: %s", s->pid_file, strerror(-r));
580 if (!pid_is_alive(pid)) {
582 log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
586 if (s->main_pid_known) {
587 if (pid == s->main_pid)
590 log_debug_unit(UNIT(s)->id, "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid);
592 service_unwatch_main_pid(s);
593 s->main_pid_known = false;
595 log_debug_unit(UNIT(s)->id, "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, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
611 static int service_search_main_pid(Service *s) {
617 /* If we know it anyway, don't ever fallback to unreliable
619 if (s->main_pid_known)
622 if (!s->guess_main_pid)
625 assert(s->main_pid <= 0);
627 pid = unit_search_main_pid(UNIT(s));
631 log_debug_unit(UNIT(s)->id, "Main PID guessed: "PID_FMT, pid);
632 r = service_set_main_pid(s, pid);
636 r = unit_watch_pid(UNIT(s), pid);
638 /* FIXME: we need to do something here */
639 log_warning_unit(UNIT(s)->id, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
646 static void service_set_state(Service *s, ServiceState state) {
647 ServiceState old_state;
648 const UnitActiveState *table;
652 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
654 old_state = s->state;
657 service_unwatch_pid_file(s);
660 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
662 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
664 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
665 SERVICE_AUTO_RESTART))
666 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
669 SERVICE_START, SERVICE_START_POST,
670 SERVICE_RUNNING, SERVICE_RELOAD,
671 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
673 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
674 service_unwatch_main_pid(s);
675 s->main_command = NULL;
679 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
681 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
683 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
684 service_unwatch_control_pid(s);
685 s->control_command = NULL;
686 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
689 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
690 unit_unwatch_all_pids(UNIT(s));
693 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
694 SERVICE_RUNNING, SERVICE_RELOAD,
695 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
696 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
697 !(state == SERVICE_DEAD && UNIT(s)->job)) {
698 service_close_socket_fd(s);
699 service_connection_unref(s);
702 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
703 service_stop_watchdog(s);
705 /* For the inactive states unit_notify() will trim the cgroup,
706 * but for exit we have to do that ourselves... */
707 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
708 unit_destroy_cgroup(UNIT(s));
710 /* For remain_after_exit services, let's see if we can "release" the
711 * hold on the console, since unit_notify() only does that in case of
713 if (state == SERVICE_EXITED &&
714 s->remain_after_exit &&
715 UNIT(s)->manager->n_on_console > 0) {
719 ec = unit_get_exec_context(UNIT(s));
720 if (ec && exec_context_may_touch_console(ec)) {
721 Manager *m = UNIT(s)->manager;
724 if (m->n_on_console == 0)
725 /* unset no_console_output flag, since the console is free */
726 m->no_console_output = false;
730 if (old_state != state)
731 log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
733 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
734 s->reload_result = SERVICE_SUCCESS;
737 static int service_coldplug(Unit *u) {
738 Service *s = SERVICE(u);
742 assert(s->state == SERVICE_DEAD);
744 if (s->deserialized_state != s->state) {
746 if (IN_SET(s->deserialized_state,
747 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
749 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
751 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
755 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
757 /* For the start/stop timeouts 0 means off */
759 r = service_arm_timer(s, k);
765 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
767 /* The restart timeouts 0 means immediately */
768 r = service_arm_timer(s, s->restart_usec);
773 if (pid_is_unwaited(s->main_pid) &&
774 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
775 IN_SET(s->deserialized_state,
776 SERVICE_START, SERVICE_START_POST,
777 SERVICE_RUNNING, SERVICE_RELOAD,
778 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
780 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
781 r = unit_watch_pid(UNIT(s), s->main_pid);
786 if (pid_is_unwaited(s->control_pid) &&
787 IN_SET(s->deserialized_state,
788 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
790 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
792 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
793 r = unit_watch_pid(UNIT(s), s->control_pid);
798 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
799 unit_watch_all_pids(UNIT(s));
801 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
802 service_start_watchdog(s);
804 service_set_state(s, s->deserialized_state);
810 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
821 if (s->socket_fd >= 0)
824 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
829 if (u->type != UNIT_SOCKET)
834 r = socket_collect_fds(sock, &cfds, &cn_fds);
847 t = new(int, rn_fds+cn_fds);
854 memcpy(t, rfds, rn_fds * sizeof(int));
855 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
860 rn_fds = rn_fds+cn_fds;
875 static int service_spawn(
880 bool apply_permissions,
882 bool apply_tty_stdin,
883 bool set_notify_socket,
890 _cleanup_free_ int *fdsbuf = NULL;
891 unsigned n_fds = 0, n_env = 0;
892 _cleanup_strv_free_ char
893 **argv = NULL, **final_env = NULL, **our_env = NULL;
895 ExecParameters exec_params = {
896 .apply_permissions = apply_permissions,
897 .apply_chroot = apply_chroot,
898 .apply_tty_stdin = apply_tty_stdin,
905 unit_realize_cgroup(UNIT(s));
907 r = unit_setup_exec_runtime(UNIT(s));
912 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
913 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
914 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
916 if (s->socket_fd >= 0) {
920 r = service_collect_fds(s, &fdsbuf, &n_fds);
929 r = service_arm_timer(s, timeout);
933 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
935 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
939 our_env = new0(char*, 4);
945 if (set_notify_socket)
946 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
952 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
957 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
958 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
963 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
969 if (is_control && UNIT(s)->cgroup_path) {
970 path = strappenda(UNIT(s)->cgroup_path, "/control");
971 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
973 path = UNIT(s)->cgroup_path;
975 exec_params.argv = argv;
976 exec_params.fds = fds;
977 exec_params.n_fds = n_fds;
978 exec_params.environment = final_env;
979 exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
980 exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
981 exec_params.cgroup_path = path;
982 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
983 exec_params.unit_id = UNIT(s)->id;
984 exec_params.watchdog_usec = s->watchdog_usec;
985 if (s->type == SERVICE_IDLE)
986 exec_params.idle_pipe = UNIT(s)->manager->idle_pipe;
996 r = unit_watch_pid(UNIT(s), pid);
998 /* FIXME: we need to do something here */
1007 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1012 static int main_pid_good(Service *s) {
1015 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1018 /* If we know the pid file, then lets just check if it is
1020 if (s->main_pid_known) {
1022 /* If it's an alien child let's check if it is still
1024 if (s->main_pid_alien && s->main_pid > 0)
1025 return pid_is_alive(s->main_pid);
1027 /* .. otherwise assume we'll get a SIGCHLD for it,
1028 * which we really should wait for to collect exit
1029 * status and code */
1030 return s->main_pid > 0;
1033 /* We don't know the pid */
1037 _pure_ static int control_pid_good(Service *s) {
1040 return s->control_pid > 0;
1043 static int cgroup_good(Service *s) {
1048 if (!UNIT(s)->cgroup_path)
1051 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1058 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1062 if (f != SERVICE_SUCCESS)
1065 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1067 if (s->result != SERVICE_SUCCESS) {
1068 log_warning_unit(UNIT(s)->id, "%s failed.", UNIT(s)->id);
1069 failure_action(UNIT(s)->manager, s->failure_action, s->reboot_arg);
1072 if (allow_restart &&
1073 !s->forbid_restart &&
1074 (s->restart == SERVICE_RESTART_ALWAYS ||
1075 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1076 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1077 (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
1078 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1079 (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) ||
1080 (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) ||
1081 (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)))) &&
1082 (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) &&
1083 (!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)))) {
1085 r = service_arm_timer(s, s->restart_usec);
1089 service_set_state(s, SERVICE_AUTO_RESTART);
1092 s->forbid_restart = false;
1094 /* We want fresh tmpdirs in case service is started again immediately */
1095 exec_runtime_destroy(s->exec_runtime);
1096 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1098 /* Also, remove the runtime directory in */
1099 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1101 /* Try to delete the pid file. At this point it will be
1102 * out-of-date, and some software might be confused by it, so
1103 * let's remove it. */
1105 unlink_noerrno(s->pid_file);
1110 log_warning_unit(UNIT(s)->id, "%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
1111 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1114 static void service_enter_stop_post(Service *s, ServiceResult f) {
1118 if (f != SERVICE_SUCCESS)
1121 service_unwatch_control_pid(s);
1122 unit_watch_all_pids(UNIT(s));
1124 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1125 if (s->control_command) {
1126 s->control_command_id = SERVICE_EXEC_STOP_POST;
1128 r = service_spawn(s,
1130 s->timeout_stop_usec,
1132 !s->permissions_start_only,
1133 !s->root_directory_start_only,
1141 service_set_state(s, SERVICE_STOP_POST);
1143 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1148 log_warning_unit(UNIT(s)->id, "%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
1149 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1152 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1157 if (f != SERVICE_SUCCESS)
1160 unit_watch_all_pids(UNIT(s));
1162 r = unit_kill_context(
1165 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1174 if (s->timeout_stop_usec > 0) {
1175 r = service_arm_timer(s, s->timeout_stop_usec);
1180 service_set_state(s, state);
1181 } else if (state == SERVICE_STOP_SIGTERM)
1182 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1183 else if (state == SERVICE_STOP_SIGKILL)
1184 service_enter_stop_post(s, SERVICE_SUCCESS);
1185 else if (state == SERVICE_FINAL_SIGTERM)
1186 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1188 service_enter_dead(s, SERVICE_SUCCESS, true);
1193 log_warning_unit(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
1195 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1196 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1198 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1201 static void service_enter_stop_by_notify(Service *s) {
1204 unit_watch_all_pids(UNIT(s));
1206 if (s->timeout_stop_usec > 0)
1207 service_arm_timer(s, s->timeout_stop_usec);
1209 service_set_state(s, SERVICE_STOP);
1212 static void service_enter_stop(Service *s, ServiceResult f) {
1217 if (f != SERVICE_SUCCESS)
1220 service_unwatch_control_pid(s);
1221 unit_watch_all_pids(UNIT(s));
1223 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1224 if (s->control_command) {
1225 s->control_command_id = SERVICE_EXEC_STOP;
1227 r = service_spawn(s,
1229 s->timeout_stop_usec,
1231 !s->permissions_start_only,
1232 !s->root_directory_start_only,
1240 service_set_state(s, SERVICE_STOP);
1242 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1247 log_warning_unit(UNIT(s)->id, "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
1248 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1251 static void service_enter_running(Service *s, ServiceResult f) {
1252 int main_pid_ok, cgroup_ok;
1255 if (f != SERVICE_SUCCESS)
1258 main_pid_ok = main_pid_good(s);
1259 cgroup_ok = cgroup_good(s);
1261 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1262 (s->bus_name_good || s->type != SERVICE_DBUS)) {
1264 /* If there are any queued up sd_notify()
1265 * notifications, process them now */
1266 if (s->notify_state == NOTIFY_RELOADING)
1267 service_enter_reload_by_notify(s);
1268 else if (s->notify_state == NOTIFY_STOPPING)
1269 service_enter_stop_by_notify(s);
1271 service_set_state(s, SERVICE_RUNNING);
1273 } else if (s->remain_after_exit)
1274 service_set_state(s, SERVICE_EXITED);
1276 service_enter_stop(s, SERVICE_SUCCESS);
1279 static void service_enter_start_post(Service *s) {
1283 service_unwatch_control_pid(s);
1284 service_reset_watchdog(s);
1286 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1287 if (s->control_command) {
1288 s->control_command_id = SERVICE_EXEC_START_POST;
1290 r = service_spawn(s,
1292 s->timeout_start_usec,
1294 !s->permissions_start_only,
1295 !s->root_directory_start_only,
1303 service_set_state(s, SERVICE_START_POST);
1305 service_enter_running(s, SERVICE_SUCCESS);
1310 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
1311 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1314 static void service_kill_control_processes(Service *s) {
1317 if (!UNIT(s)->cgroup_path)
1320 p = strappenda(UNIT(s)->cgroup_path, "/control");
1321 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1324 static void service_enter_start(Service *s) {
1331 service_unwatch_control_pid(s);
1332 service_unwatch_main_pid(s);
1334 /* We want to ensure that nobody leaks processes from
1335 * START_PRE here, so let's go on a killing spree, People
1336 * should not spawn long running processes from START_PRE. */
1337 service_kill_control_processes(s);
1339 if (s->type == SERVICE_FORKING) {
1340 s->control_command_id = SERVICE_EXEC_START;
1341 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1343 s->main_command = NULL;
1345 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1346 s->control_command = NULL;
1348 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1352 assert(s->type == SERVICE_ONESHOT);
1353 service_enter_start_post(s);
1357 r = service_spawn(s,
1359 IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_ONESHOT) ? s->timeout_start_usec : 0,
1364 s->notify_access != NOTIFY_NONE,
1370 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
1371 /* For simple services we immediately start
1372 * the START_POST binaries. */
1374 service_set_main_pid(s, pid);
1375 service_enter_start_post(s);
1377 } else if (s->type == SERVICE_FORKING) {
1379 /* For forking services we wait until the start
1380 * process exited. */
1382 s->control_pid = pid;
1383 service_set_state(s, SERVICE_START);
1385 } else if (s->type == SERVICE_ONESHOT ||
1386 s->type == SERVICE_DBUS ||
1387 s->type == SERVICE_NOTIFY) {
1389 /* For oneshot services we wait until the start
1390 * process exited, too, but it is our main process. */
1392 /* For D-Bus services we know the main pid right away,
1393 * but wait for the bus name to appear on the
1394 * bus. Notify services are similar. */
1396 service_set_main_pid(s, pid);
1397 service_set_state(s, SERVICE_START);
1399 assert_not_reached("Unknown service type");
1404 log_warning_unit(UNIT(s)->id, "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
1405 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1408 static void service_enter_start_pre(Service *s) {
1413 service_unwatch_control_pid(s);
1415 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1416 if (s->control_command) {
1417 /* Before we start anything, let's clear up what might
1418 * be left from previous runs. */
1419 service_kill_control_processes(s);
1421 s->control_command_id = SERVICE_EXEC_START_PRE;
1423 r = service_spawn(s,
1425 s->timeout_start_usec,
1427 !s->permissions_start_only,
1428 !s->root_directory_start_only,
1436 service_set_state(s, SERVICE_START_PRE);
1438 service_enter_start(s);
1443 log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
1444 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1447 static void service_enter_restart(Service *s) {
1448 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1453 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1454 /* Don't restart things if we are going down anyway */
1455 log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart.");
1457 r = service_arm_timer(s, s->restart_usec);
1464 /* Any units that are bound to this service must also be
1465 * restarted. We use JOB_RESTART (instead of the more obvious
1466 * JOB_START) here so that those dependency jobs will be added
1468 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1472 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1473 * it will be canceled as part of the service_stop() call that
1474 * is executed as part of JOB_RESTART. */
1476 log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id);
1480 log_warning_unit(UNIT(s)->id, "%s failed to schedule restart job: %s", UNIT(s)->id, bus_error_message(&error, -r));
1481 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1484 static void service_enter_reload_by_notify(Service *s) {
1487 if (s->timeout_start_usec > 0)
1488 service_arm_timer(s, s->timeout_start_usec);
1490 service_set_state(s, SERVICE_RELOAD);
1493 static void service_enter_reload(Service *s) {
1498 service_unwatch_control_pid(s);
1500 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1501 if (s->control_command) {
1502 s->control_command_id = SERVICE_EXEC_RELOAD;
1504 r = service_spawn(s,
1506 s->timeout_start_usec,
1508 !s->permissions_start_only,
1509 !s->root_directory_start_only,
1517 service_set_state(s, SERVICE_RELOAD);
1519 service_enter_running(s, SERVICE_SUCCESS);
1524 log_warning_unit(UNIT(s)->id, "%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
1525 s->reload_result = SERVICE_FAILURE_RESOURCES;
1526 service_enter_running(s, SERVICE_SUCCESS);
1529 static void service_run_next_control(Service *s) {
1533 assert(s->control_command);
1534 assert(s->control_command->command_next);
1536 assert(s->control_command_id != SERVICE_EXEC_START);
1538 s->control_command = s->control_command->command_next;
1539 service_unwatch_control_pid(s);
1541 r = service_spawn(s,
1543 IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec,
1545 !s->permissions_start_only,
1546 !s->root_directory_start_only,
1547 s->control_command_id == SERVICE_EXEC_START_PRE ||
1548 s->control_command_id == SERVICE_EXEC_STOP_POST,
1558 log_warning_unit(UNIT(s)->id, "%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
1560 if (s->state == SERVICE_START_PRE)
1561 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1562 else if (s->state == SERVICE_STOP)
1563 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1564 else if (s->state == SERVICE_STOP_POST)
1565 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1566 else if (s->state == SERVICE_RELOAD) {
1567 s->reload_result = SERVICE_FAILURE_RESOURCES;
1568 service_enter_running(s, SERVICE_SUCCESS);
1570 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1573 static void service_run_next_main(Service *s) {
1578 assert(s->main_command);
1579 assert(s->main_command->command_next);
1580 assert(s->type == SERVICE_ONESHOT);
1582 s->main_command = s->main_command->command_next;
1583 service_unwatch_main_pid(s);
1585 r = service_spawn(s,
1587 s->timeout_start_usec,
1592 s->notify_access != NOTIFY_NONE,
1598 service_set_main_pid(s, pid);
1603 log_warning_unit(UNIT(s)->id, "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
1604 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1607 static int service_start_limit_test(Service *s) {
1610 if (ratelimit_test(&s->start_limit))
1613 log_warning_unit(UNIT(s)->id, "start request repeated too quickly for %s", UNIT(s)->id);
1615 return failure_action(UNIT(s)->manager, s->start_limit_action, s->reboot_arg);
1618 static int service_start(Unit *u) {
1619 Service *s = SERVICE(u);
1624 /* We cannot fulfill this request right now, try again later
1626 if (s->state == SERVICE_STOP ||
1627 s->state == SERVICE_STOP_SIGTERM ||
1628 s->state == SERVICE_STOP_SIGKILL ||
1629 s->state == SERVICE_STOP_POST ||
1630 s->state == SERVICE_FINAL_SIGTERM ||
1631 s->state == SERVICE_FINAL_SIGKILL)
1634 /* Already on it! */
1635 if (s->state == SERVICE_START_PRE ||
1636 s->state == SERVICE_START ||
1637 s->state == SERVICE_START_POST)
1640 /* A service that will be restarted must be stopped first to
1641 * trigger BindsTo and/or OnFailure dependencies. If a user
1642 * does not want to wait for the holdoff time to elapse, the
1643 * service should be manually restarted, not started. We
1644 * simply return EAGAIN here, so that any start jobs stay
1645 * queued, and assume that the auto restart timer will
1646 * eventually trigger the restart. */
1647 if (s->state == SERVICE_AUTO_RESTART)
1650 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
1652 /* Make sure we don't enter a busy loop of some kind. */
1653 r = service_start_limit_test(s);
1655 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
1659 s->result = SERVICE_SUCCESS;
1660 s->reload_result = SERVICE_SUCCESS;
1661 s->main_pid_known = false;
1662 s->main_pid_alien = false;
1663 s->forbid_restart = false;
1665 free(s->status_text);
1666 s->status_text = NULL;
1667 s->status_errno = 0;
1669 s->notify_state = NOTIFY_UNKNOWN;
1671 service_enter_start_pre(s);
1675 static int service_stop(Unit *u) {
1676 Service *s = SERVICE(u);
1680 /* Don't create restart jobs from here. */
1681 s->forbid_restart = true;
1684 if (s->state == SERVICE_STOP ||
1685 s->state == SERVICE_STOP_SIGTERM ||
1686 s->state == SERVICE_STOP_SIGKILL ||
1687 s->state == SERVICE_STOP_POST ||
1688 s->state == SERVICE_FINAL_SIGTERM ||
1689 s->state == SERVICE_FINAL_SIGKILL)
1692 /* A restart will be scheduled or is in progress. */
1693 if (s->state == SERVICE_AUTO_RESTART) {
1694 service_set_state(s, SERVICE_DEAD);
1698 /* If there's already something running we go directly into
1700 if (s->state == SERVICE_START_PRE ||
1701 s->state == SERVICE_START ||
1702 s->state == SERVICE_START_POST ||
1703 s->state == SERVICE_RELOAD) {
1704 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1708 assert(s->state == SERVICE_RUNNING ||
1709 s->state == SERVICE_EXITED);
1711 service_enter_stop(s, SERVICE_SUCCESS);
1715 static int service_reload(Unit *u) {
1716 Service *s = SERVICE(u);
1720 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1722 service_enter_reload(s);
1726 _pure_ static bool service_can_reload(Unit *u) {
1727 Service *s = SERVICE(u);
1731 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1734 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1735 Service *s = SERVICE(u);
1741 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1742 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1743 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
1745 if (s->control_pid > 0)
1746 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1749 if (s->main_pid_known && s->main_pid > 0)
1750 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
1752 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1755 unit_serialize_item(u, f, "status-text", s->status_text);
1757 /* FIXME: There's a minor uncleanliness here: if there are
1758 * multiple commands attached here, we will start from the
1759 * first one again */
1760 if (s->control_command_id >= 0)
1761 unit_serialize_item(u, f, "control-command",
1762 service_exec_command_to_string(s->control_command_id));
1764 if (s->socket_fd >= 0) {
1767 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1770 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1773 if (s->main_exec_status.pid > 0) {
1774 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1775 s->main_exec_status.pid);
1776 dual_timestamp_serialize(f, "main-exec-status-start",
1777 &s->main_exec_status.start_timestamp);
1778 dual_timestamp_serialize(f, "main-exec-status-exit",
1779 &s->main_exec_status.exit_timestamp);
1781 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
1782 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1783 s->main_exec_status.code);
1784 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1785 s->main_exec_status.status);
1788 if (dual_timestamp_is_set(&s->watchdog_timestamp))
1789 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
1791 if (s->forbid_restart)
1792 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
1797 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1798 Service *s = SERVICE(u);
1805 if (streq(key, "state")) {
1808 state = service_state_from_string(value);
1810 log_debug_unit(u->id, "Failed to parse state value %s", value);
1812 s->deserialized_state = state;
1813 } else if (streq(key, "result")) {
1816 f = service_result_from_string(value);
1818 log_debug_unit(u->id, "Failed to parse result value %s", value);
1819 else if (f != SERVICE_SUCCESS)
1822 } else if (streq(key, "reload-result")) {
1825 f = service_result_from_string(value);
1827 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
1828 else if (f != SERVICE_SUCCESS)
1829 s->reload_result = f;
1831 } else if (streq(key, "control-pid")) {
1834 if (parse_pid(value, &pid) < 0)
1835 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
1837 s->control_pid = pid;
1838 } else if (streq(key, "main-pid")) {
1841 if (parse_pid(value, &pid) < 0)
1842 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
1844 service_set_main_pid(s, pid);
1845 unit_watch_pid(UNIT(s), pid);
1847 } else if (streq(key, "main-pid-known")) {
1850 b = parse_boolean(value);
1852 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
1854 s->main_pid_known = b;
1855 } else if (streq(key, "status-text")) {
1862 free(s->status_text);
1866 } else if (streq(key, "control-command")) {
1867 ServiceExecCommand id;
1869 id = service_exec_command_from_string(value);
1871 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
1873 s->control_command_id = id;
1874 s->control_command = s->exec_command[id];
1876 } else if (streq(key, "socket-fd")) {
1879 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1880 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
1883 asynchronous_close(s->socket_fd);
1884 s->socket_fd = fdset_remove(fds, fd);
1886 } else if (streq(key, "main-exec-status-pid")) {
1889 if (parse_pid(value, &pid) < 0)
1890 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
1892 s->main_exec_status.pid = pid;
1893 } else if (streq(key, "main-exec-status-code")) {
1896 if (safe_atoi(value, &i) < 0)
1897 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
1899 s->main_exec_status.code = i;
1900 } else if (streq(key, "main-exec-status-status")) {
1903 if (safe_atoi(value, &i) < 0)
1904 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
1906 s->main_exec_status.status = i;
1907 } else if (streq(key, "main-exec-status-start"))
1908 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
1909 else if (streq(key, "main-exec-status-exit"))
1910 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
1911 else if (streq(key, "watchdog-timestamp"))
1912 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
1913 else if (streq(key, "forbid-restart")) {
1916 b = parse_boolean(value);
1918 log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value);
1920 s->forbid_restart = b;
1922 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
1927 _pure_ static UnitActiveState service_active_state(Unit *u) {
1928 const UnitActiveState *table;
1932 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1934 return table[SERVICE(u)->state];
1937 static const char *service_sub_state_to_string(Unit *u) {
1940 return service_state_to_string(SERVICE(u)->state);
1943 static bool service_check_gc(Unit *u) {
1944 Service *s = SERVICE(u);
1948 /* Never clean up services that still have a process around,
1949 * even if the service is formally dead. */
1950 if (cgroup_good(s) > 0 ||
1951 main_pid_good(s) > 0 ||
1952 control_pid_good(s) > 0)
1958 _pure_ static bool service_check_snapshot(Unit *u) {
1959 Service *s = SERVICE(u);
1963 return s->socket_fd < 0;
1966 static int service_retry_pid_file(Service *s) {
1969 assert(s->pid_file);
1970 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
1972 r = service_load_pid_file(s, false);
1976 service_unwatch_pid_file(s);
1978 service_enter_running(s, SERVICE_SUCCESS);
1982 static int service_watch_pid_file(Service *s) {
1985 log_debug_unit(UNIT(s)->id, "Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
1987 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
1991 /* the pidfile might have appeared just before we set the watch */
1992 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);
1993 service_retry_pid_file(s);
1997 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));
1998 service_unwatch_pid_file(s);
2002 static int service_demand_pid_file(Service *s) {
2005 assert(s->pid_file);
2006 assert(!s->pid_file_pathspec);
2008 ps = new0(PathSpec, 1);
2013 ps->path = strdup(s->pid_file);
2019 path_kill_slashes(ps->path);
2021 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2022 * keep their PID file open all the time. */
2023 ps->type = PATH_MODIFIED;
2024 ps->inotify_fd = -1;
2026 s->pid_file_pathspec = ps;
2028 return service_watch_pid_file(s);
2031 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2032 PathSpec *p = userdata;
2037 s = SERVICE(p->unit);
2041 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2042 assert(s->pid_file_pathspec);
2043 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2045 log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2047 if (path_spec_fd_event(p, events) < 0)
2050 if (service_retry_pid_file(s) == 0)
2053 if (service_watch_pid_file(s) < 0)
2059 service_unwatch_pid_file(s);
2060 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2064 static void service_notify_cgroup_empty_event(Unit *u) {
2065 Service *s = SERVICE(u);
2069 log_debug_unit(u->id, "%s: cgroup is empty", u->id);
2073 /* Waiting for SIGCHLD is usually more interesting,
2074 * because it includes return codes/signals. Which is
2075 * why we ignore the cgroup events for most cases,
2076 * except when we don't know pid which to expect the
2080 case SERVICE_START_POST:
2081 /* If we were hoping for the daemon to write its PID file,
2082 * we can give up now. */
2083 if (s->pid_file_pathspec) {
2084 log_warning_unit(u->id, "%s never wrote its PID file. Failing.", UNIT(s)->id);
2086 service_unwatch_pid_file(s);
2087 if (s->state == SERVICE_START)
2088 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2090 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2094 case SERVICE_RUNNING:
2095 /* service_enter_running() will figure out what to do */
2096 service_enter_running(s, SERVICE_SUCCESS);
2099 case SERVICE_STOP_SIGTERM:
2100 case SERVICE_STOP_SIGKILL:
2102 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2103 service_enter_stop_post(s, SERVICE_SUCCESS);
2107 case SERVICE_STOP_POST:
2108 case SERVICE_FINAL_SIGTERM:
2109 case SERVICE_FINAL_SIGKILL:
2110 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2111 service_enter_dead(s, SERVICE_SUCCESS, true);
2120 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2121 Service *s = SERVICE(u);
2127 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2128 is_clean_exit_lsb(code, status, &s->success_status))
2129 f = SERVICE_SUCCESS;
2130 else if (code == CLD_EXITED)
2131 f = SERVICE_FAILURE_EXIT_CODE;
2132 else if (code == CLD_KILLED)
2133 f = SERVICE_FAILURE_SIGNAL;
2134 else if (code == CLD_DUMPED)
2135 f = SERVICE_FAILURE_CORE_DUMP;
2137 assert_not_reached("Unknown code");
2139 if (s->main_pid == pid) {
2140 /* Forking services may occasionally move to a new PID.
2141 * As long as they update the PID file before exiting the old
2142 * PID, they're fine. */
2143 if (service_load_pid_file(s, false) == 0)
2147 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2149 if (s->main_command) {
2150 /* If this is not a forking service than the
2151 * main process got started and hence we copy
2152 * the exit status so that it is recorded both
2153 * as main and as control process exit
2156 s->main_command->exec_status = s->main_exec_status;
2158 if (s->main_command->ignore)
2159 f = SERVICE_SUCCESS;
2160 } else if (s->exec_command[SERVICE_EXEC_START]) {
2162 /* If this is a forked process, then we should
2163 * ignore the return value if this was
2164 * configured for the starter process */
2166 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2167 f = SERVICE_SUCCESS;
2170 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2172 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2173 u->id, sigchld_code_to_string(code), status,
2174 strna(code == CLD_EXITED
2175 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2176 : signal_to_string(status)),
2177 "EXIT_CODE=%s", sigchld_code_to_string(code),
2178 "EXIT_STATUS=%i", status,
2181 if (f != SERVICE_SUCCESS)
2184 if (s->main_command &&
2185 s->main_command->command_next &&
2186 f == SERVICE_SUCCESS) {
2188 /* There is another command to *
2189 * execute, so let's do that. */
2191 log_debug_unit(u->id, "%s running next main command for state %s", u->id, service_state_to_string(s->state));
2192 service_run_next_main(s);
2196 /* The service exited, so the service is officially
2198 s->main_command = NULL;
2202 case SERVICE_START_POST:
2203 case SERVICE_RELOAD:
2205 /* Need to wait until the operation is
2210 if (s->type == SERVICE_ONESHOT) {
2211 /* This was our main goal, so let's go on */
2212 if (f == SERVICE_SUCCESS)
2213 service_enter_start_post(s);
2215 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2221 case SERVICE_RUNNING:
2222 service_enter_running(s, f);
2225 case SERVICE_STOP_SIGTERM:
2226 case SERVICE_STOP_SIGKILL:
2228 if (!control_pid_good(s))
2229 service_enter_stop_post(s, f);
2231 /* If there is still a control process, wait for that first */
2234 case SERVICE_STOP_POST:
2235 case SERVICE_FINAL_SIGTERM:
2236 case SERVICE_FINAL_SIGKILL:
2238 if (!control_pid_good(s))
2239 service_enter_dead(s, f, true);
2243 assert_not_reached("Uh, main process died at wrong time.");
2247 } else if (s->control_pid == pid) {
2250 if (s->control_command) {
2251 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2253 if (s->control_command->ignore)
2254 f = SERVICE_SUCCESS;
2257 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2258 "%s: control process exited, code=%s status=%i",
2259 u->id, sigchld_code_to_string(code), status);
2261 if (f != SERVICE_SUCCESS)
2264 /* Immediately get rid of the cgroup, so that the
2265 * kernel doesn't delay the cgroup empty messages for
2266 * the service cgroup any longer than necessary */
2267 service_kill_control_processes(s);
2269 if (s->control_command &&
2270 s->control_command->command_next &&
2271 f == SERVICE_SUCCESS) {
2273 /* There is another command to *
2274 * execute, so let's do that. */
2276 log_debug_unit(u->id, "%s running next control command for state %s", u->id, service_state_to_string(s->state));
2277 service_run_next_control(s);
2280 /* No further commands for this step, so let's
2281 * figure out what to do next */
2283 s->control_command = NULL;
2284 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2286 log_debug_unit(u->id, "%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2290 case SERVICE_START_PRE:
2291 if (f == SERVICE_SUCCESS)
2292 service_enter_start(s);
2294 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2298 if (s->type != SERVICE_FORKING)
2299 /* Maybe spurious event due to a reload that changed the type? */
2302 if (f != SERVICE_SUCCESS) {
2303 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2308 bool has_start_post;
2311 /* Let's try to load the pid file here if we can.
2312 * The PID file might actually be created by a START_POST
2313 * script. In that case don't worry if the loading fails. */
2315 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2316 r = service_load_pid_file(s, !has_start_post);
2317 if (!has_start_post && r < 0) {
2318 r = service_demand_pid_file(s);
2319 if (r < 0 || !cgroup_good(s))
2320 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2324 service_search_main_pid(s);
2326 service_enter_start_post(s);
2329 case SERVICE_START_POST:
2330 if (f != SERVICE_SUCCESS) {
2331 service_enter_stop(s, f);
2338 r = service_load_pid_file(s, true);
2340 r = service_demand_pid_file(s);
2341 if (r < 0 || !cgroup_good(s))
2342 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2346 service_search_main_pid(s);
2348 service_enter_running(s, SERVICE_SUCCESS);
2351 case SERVICE_RELOAD:
2352 if (f == SERVICE_SUCCESS) {
2353 service_load_pid_file(s, true);
2354 service_search_main_pid(s);
2357 s->reload_result = f;
2358 service_enter_running(s, SERVICE_SUCCESS);
2362 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2365 case SERVICE_STOP_SIGTERM:
2366 case SERVICE_STOP_SIGKILL:
2367 if (main_pid_good(s) <= 0)
2368 service_enter_stop_post(s, f);
2370 /* If there is still a service
2371 * process around, wait until
2372 * that one quit, too */
2375 case SERVICE_STOP_POST:
2376 case SERVICE_FINAL_SIGTERM:
2377 case SERVICE_FINAL_SIGKILL:
2378 if (main_pid_good(s) <= 0)
2379 service_enter_dead(s, f, true);
2383 assert_not_reached("Uh, control process died at wrong time.");
2388 /* Notify clients about changed exit status */
2389 unit_add_to_dbus_queue(u);
2391 /* We got one SIGCHLD for the service, let's watch all
2392 * processes that are now running of the service, and watch
2393 * that. Among the PIDs we then watch will be children
2394 * reassigned to us, which hopefully allows us to identify
2395 * when all children are gone */
2396 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2397 unit_watch_all_pids(u);
2399 /* If the PID set is empty now, then let's finish this off */
2400 if (set_isempty(u->pids))
2401 service_notify_cgroup_empty_event(u);
2404 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2405 Service *s = SERVICE(userdata);
2408 assert(source == s->timer_event_source);
2412 case SERVICE_START_PRE:
2414 log_warning_unit(UNIT(s)->id, "%s %s operation timed out. Terminating.", UNIT(s)->id, s->state == SERVICE_START ? "start" : "start-pre");
2415 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2418 case SERVICE_START_POST:
2419 log_warning_unit(UNIT(s)->id, "%s start-post operation timed out. Stopping.", UNIT(s)->id);
2420 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2423 case SERVICE_RELOAD:
2424 log_warning_unit(UNIT(s)->id, "%s reload operation timed out. Stopping.", UNIT(s)->id);
2425 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2426 service_enter_running(s, SERVICE_SUCCESS);
2430 log_warning_unit(UNIT(s)->id, "%s stopping timed out. Terminating.", UNIT(s)->id);
2431 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2434 case SERVICE_STOP_SIGTERM:
2435 if (s->kill_context.send_sigkill) {
2436 log_warning_unit(UNIT(s)->id, "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
2437 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2439 log_warning_unit(UNIT(s)->id, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
2440 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2445 case SERVICE_STOP_SIGKILL:
2446 /* Uh, we sent a SIGKILL and it is still not gone?
2447 * Must be something we cannot kill, so let's just be
2448 * weirded out and continue */
2450 log_warning_unit(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
2451 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2454 case SERVICE_STOP_POST:
2455 log_warning_unit(UNIT(s)->id, "%s stop-post timed out. Terminating.", UNIT(s)->id);
2456 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2459 case SERVICE_FINAL_SIGTERM:
2460 if (s->kill_context.send_sigkill) {
2461 log_warning_unit(UNIT(s)->id, "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
2462 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2464 log_warning_unit(UNIT(s)->id, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s)->id);
2465 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2470 case SERVICE_FINAL_SIGKILL:
2471 log_warning_unit(UNIT(s)->id, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
2472 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2475 case SERVICE_AUTO_RESTART:
2476 log_info_unit(UNIT(s)->id,
2477 s->restart_usec > 0 ?
2478 "%s holdoff time over, scheduling restart." :
2479 "%s has no holdoff time, scheduling restart.",
2481 service_enter_restart(s);
2485 assert_not_reached("Timeout at wrong time.");
2491 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2492 Service *s = SERVICE(userdata);
2493 char t[FORMAT_TIMESPAN_MAX];
2496 assert(source == s->watchdog_event_source);
2498 log_error_unit(UNIT(s)->id, "%s watchdog timeout (limit %s)!", UNIT(s)->id,
2499 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
2501 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG);
2506 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2507 Service *s = SERVICE(u);
2508 _cleanup_free_ char *cc = NULL;
2509 bool notify_dbus = false;
2514 cc = strv_join(tags, ", ");
2515 log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s)",
2516 u->id, pid, isempty(cc) ? "n/a" : cc);
2518 if (s->notify_access == NOTIFY_NONE) {
2519 log_warning_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception is disabled.", u->id, pid);
2523 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2524 if (s->main_pid != 0)
2525 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);
2527 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);
2531 /* Interpret MAINPID= */
2532 e = strv_find_startswith(tags, "MAINPID=");
2533 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
2534 if (parse_pid(e, &pid) < 0)
2535 log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
2537 log_debug_unit(u->id, "%s: got MAINPID=%s", u->id, e);
2539 service_set_main_pid(s, pid);
2540 unit_watch_pid(UNIT(s), pid);
2545 /* Interpret RELOADING= */
2546 if (strv_find(tags, "RELOADING=1")) {
2548 log_debug_unit(u->id, "%s: got RELOADING=1", u->id);
2549 s->notify_state = NOTIFY_RELOADING;
2551 if (s->state == SERVICE_RUNNING)
2552 service_enter_reload_by_notify(s);
2557 /* Interpret READY= */
2558 if (strv_find(tags, "READY=1")) {
2560 log_debug_unit(u->id, "%s: got READY=1", u->id);
2561 s->notify_state = NOTIFY_READY;
2563 /* Type=notify services inform us about completed
2564 * initialization with READY=1 */
2565 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START)
2566 service_enter_start_post(s);
2568 /* Sending READY=1 while we are reloading informs us
2569 * that the reloading is complete */
2570 if (s->state == SERVICE_RELOAD && s->control_pid == 0)
2571 service_enter_running(s, SERVICE_SUCCESS);
2576 /* Interpret STOPPING= */
2577 if (strv_find(tags, "STOPPING=1")) {
2579 log_debug_unit(u->id, "%s: got STOPPING=1", u->id);
2580 s->notify_state = NOTIFY_STOPPING;
2582 if (s->state == SERVICE_RUNNING)
2583 service_enter_stop_by_notify(s);
2588 /* Interpret STATUS= */
2589 e = strv_find_startswith(tags, "STATUS=");
2591 _cleanup_free_ char *t = NULL;
2594 if (!utf8_is_valid(e))
2595 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
2597 log_debug_unit(u->id, "%s: got STATUS=%s", u->id, e);
2605 if (!streq_ptr(s->status_text, t)) {
2607 free(s->status_text);
2615 /* Interpret ERRNO= */
2616 e = strv_find_startswith(tags, "ERRNO=");
2620 if (safe_atoi(e, &status_errno) < 0 || status_errno < 0)
2621 log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
2623 log_debug_unit(u->id, "%s: got ERRNO=%s", u->id, e);
2625 if (s->status_errno != status_errno) {
2626 s->status_errno = status_errno;
2632 /* Interpret WATCHDOG= */
2633 if (strv_find(tags, "WATCHDOG=1")) {
2634 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
2635 service_reset_watchdog(s);
2638 /* Notify clients about changed status or main pid */
2640 unit_add_to_dbus_queue(u);
2643 static int service_get_timeout(Unit *u, uint64_t *timeout) {
2644 Service *s = SERVICE(u);
2647 if (!s->timer_event_source)
2650 r = sd_event_source_get_time(s->timer_event_source, timeout);
2657 static void service_bus_name_owner_change(
2660 const char *old_owner,
2661 const char *new_owner) {
2663 Service *s = SERVICE(u);
2669 assert(streq(s->bus_name, name));
2670 assert(old_owner || new_owner);
2672 if (old_owner && new_owner)
2673 log_debug_unit(u->id, "%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
2675 log_debug_unit(u->id, "%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
2677 log_debug_unit(u->id, "%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
2679 s->bus_name_good = !!new_owner;
2681 if (s->type == SERVICE_DBUS) {
2683 /* service_enter_running() will figure out what to
2685 if (s->state == SERVICE_RUNNING)
2686 service_enter_running(s, SERVICE_SUCCESS);
2687 else if (s->state == SERVICE_START && new_owner)
2688 service_enter_start_post(s);
2690 } else if (new_owner &&
2692 (s->state == SERVICE_START ||
2693 s->state == SERVICE_START_POST ||
2694 s->state == SERVICE_RUNNING ||
2695 s->state == SERVICE_RELOAD)) {
2697 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
2700 /* Try to acquire PID from bus service */
2702 r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
2704 r = sd_bus_creds_get_pid(creds, &pid);
2706 log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
2708 service_set_main_pid(s, pid);
2709 unit_watch_pid(UNIT(s), pid);
2714 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2715 _cleanup_free_ char *peer = NULL;
2721 /* This is called by the socket code when instantiating a new
2722 * service for a stream socket and the socket needs to be
2725 if (UNIT(s)->load_state != UNIT_LOADED)
2728 if (s->socket_fd >= 0)
2731 if (s->state != SERVICE_DEAD)
2734 if (getpeername_pretty(fd, &peer) >= 0) {
2736 if (UNIT(s)->description) {
2737 _cleanup_free_ char *a;
2739 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2743 r = unit_set_description(UNIT(s), a);
2745 r = unit_set_description(UNIT(s), peer);
2753 unit_ref_set(&s->accept_socket, UNIT(sock));
2755 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
2758 static void service_reset_failed(Unit *u) {
2759 Service *s = SERVICE(u);
2763 if (s->state == SERVICE_FAILED)
2764 service_set_state(s, SERVICE_DEAD);
2766 s->result = SERVICE_SUCCESS;
2767 s->reload_result = SERVICE_SUCCESS;
2769 RATELIMIT_RESET(s->start_limit);
2772 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2773 Service *s = SERVICE(u);
2775 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
2778 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2779 [SERVICE_DEAD] = "dead",
2780 [SERVICE_START_PRE] = "start-pre",
2781 [SERVICE_START] = "start",
2782 [SERVICE_START_POST] = "start-post",
2783 [SERVICE_RUNNING] = "running",
2784 [SERVICE_EXITED] = "exited",
2785 [SERVICE_RELOAD] = "reload",
2786 [SERVICE_STOP] = "stop",
2787 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2788 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2789 [SERVICE_STOP_POST] = "stop-post",
2790 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2791 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2792 [SERVICE_FAILED] = "failed",
2793 [SERVICE_AUTO_RESTART] = "auto-restart",
2796 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2798 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2799 [SERVICE_RESTART_NO] = "no",
2800 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
2801 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
2802 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
2803 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
2804 [SERVICE_RESTART_ON_ABORT] = "on-abort",
2805 [SERVICE_RESTART_ALWAYS] = "always",
2808 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2810 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2811 [SERVICE_SIMPLE] = "simple",
2812 [SERVICE_FORKING] = "forking",
2813 [SERVICE_ONESHOT] = "oneshot",
2814 [SERVICE_DBUS] = "dbus",
2815 [SERVICE_NOTIFY] = "notify",
2816 [SERVICE_IDLE] = "idle"
2819 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2821 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2822 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2823 [SERVICE_EXEC_START] = "ExecStart",
2824 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2825 [SERVICE_EXEC_RELOAD] = "ExecReload",
2826 [SERVICE_EXEC_STOP] = "ExecStop",
2827 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2830 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2832 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2833 [NOTIFY_NONE] = "none",
2834 [NOTIFY_MAIN] = "main",
2835 [NOTIFY_ALL] = "all"
2838 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2840 static const char* const notify_state_table[_NOTIFY_STATE_MAX] = {
2841 [NOTIFY_UNKNOWN] = "unknown",
2842 [NOTIFY_READY] = "ready",
2843 [NOTIFY_RELOADING] = "reloading",
2844 [NOTIFY_STOPPING] = "stopping",
2847 DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState);
2849 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
2850 [SERVICE_SUCCESS] = "success",
2851 [SERVICE_FAILURE_RESOURCES] = "resources",
2852 [SERVICE_FAILURE_TIMEOUT] = "timeout",
2853 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
2854 [SERVICE_FAILURE_SIGNAL] = "signal",
2855 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
2856 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
2857 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
2860 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
2862 const UnitVTable service_vtable = {
2863 .object_size = sizeof(Service),
2864 .exec_context_offset = offsetof(Service, exec_context),
2865 .cgroup_context_offset = offsetof(Service, cgroup_context),
2866 .kill_context_offset = offsetof(Service, kill_context),
2867 .exec_runtime_offset = offsetof(Service, exec_runtime),
2873 .private_section = "Service",
2875 .init = service_init,
2876 .done = service_done,
2877 .load = service_load,
2879 .coldplug = service_coldplug,
2881 .dump = service_dump,
2883 .start = service_start,
2884 .stop = service_stop,
2885 .reload = service_reload,
2887 .can_reload = service_can_reload,
2889 .kill = service_kill,
2891 .serialize = service_serialize,
2892 .deserialize_item = service_deserialize_item,
2894 .active_state = service_active_state,
2895 .sub_state_to_string = service_sub_state_to_string,
2897 .check_gc = service_check_gc,
2898 .check_snapshot = service_check_snapshot,
2900 .sigchld_event = service_sigchld_event,
2902 .reset_failed = service_reset_failed,
2904 .notify_cgroup_empty = service_notify_cgroup_empty_event,
2905 .notify_message = service_notify_message,
2907 .bus_name_owner_change = service_bus_name_owner_change,
2909 .bus_interface = "org.freedesktop.systemd1.Service",
2910 .bus_vtable = bus_service_vtable,
2911 .bus_set_property = bus_service_set_property,
2912 .bus_commit_properties = bus_service_commit_properties,
2914 .get_timeout = service_get_timeout,
2915 .can_transient = true,
2917 .status_message_formats = {
2918 .starting_stopping = {
2919 [0] = "Starting %s...",
2920 [1] = "Stopping %s...",
2922 .finished_start_job = {
2923 [JOB_DONE] = "Started %s.",
2924 [JOB_FAILED] = "Failed to start %s.",
2925 [JOB_DEPENDENCY] = "Dependency failed for %s.",
2926 [JOB_TIMEOUT] = "Timed out starting %s.",
2928 .finished_stop_job = {
2929 [JOB_DONE] = "Stopped %s.",
2930 [JOB_FAILED] = "Stopped (with error) %s.",
2931 [JOB_TIMEOUT] = "Timed out stopping %s.",