HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/service.c
Git blob: 5c097c139d401c81662897faf4f6e6779f5302c7
Lines 4001–5000 of 6,768
r = service_refresh_on_reload_to_strv(s->refreshed_mask, &l);
if (r < 0)
return log_oom();
t = strv_join(l, " ");
if (!t)
return log_oom();
(void) serialize_item(f, "refreshed-mask", t);
}
return 0;
}
int service_deserialize_exec_command(
Unit *u,
const char *key,
const char *value) {
Service *s = ASSERT_PTR(SERVICE(u));
ExecCommand *command = NULL;
ServiceExecCommand id = _SERVICE_EXEC_COMMAND_INVALID;
_cleanup_free_ char *path = NULL;
_cleanup_strv_free_ char **argv = NULL;
unsigned idx = 0, i;
bool control, found = false, last = false;
int r;
enum {
STATE_EXEC_COMMAND_TYPE,
STATE_EXEC_COMMAND_INDEX,
STATE_EXEC_COMMAND_PATH,
STATE_EXEC_COMMAND_ARGS,
_STATE_EXEC_COMMAND_MAX,
_STATE_EXEC_COMMAND_INVALID = -EINVAL,
} state;
assert(key);
assert(value);
control = streq(key, "control-command");
state = STATE_EXEC_COMMAND_TYPE;
for (;;) {
_cleanup_free_ char *arg = NULL;
r = extract_first_word(&value, &arg, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE);
if (r < 0)
return r;
if (r == 0)
break;
switch (state) {
case STATE_EXEC_COMMAND_TYPE:
id = service_exec_command_from_string(arg);
if (id < 0)
return id;
state = STATE_EXEC_COMMAND_INDEX;
break;
case STATE_EXEC_COMMAND_INDEX:
/* ExecCommand index 1234 is serialized as either '1234' or '+1234'. The second form
* is used to mark the last command in a sequence. We warn if the deserialized command
* doesn't match what we have loaded from the unit, but we don't need to warn if
* that is the last command. */
r = safe_atou(arg, &idx);
if (r < 0)
return r;
last = arg[0] == '+';
state = STATE_EXEC_COMMAND_PATH;
break;
case STATE_EXEC_COMMAND_PATH:
path = TAKE_PTR(arg);
state = STATE_EXEC_COMMAND_ARGS;
break;
case STATE_EXEC_COMMAND_ARGS:
r = strv_extend(&argv, arg);
if (r < 0)
return r;
break;
default:
assert_not_reached();
}
}
if (state != STATE_EXEC_COMMAND_ARGS)
return -EINVAL;
if (strv_isempty(argv))
return -EINVAL; /* At least argv[0] must be always present. */
/* Let's check whether exec command on given offset matches data that we just deserialized */
for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) {
if (i != idx)
continue;
found = strv_equal(argv, command->argv) && streq(command->path, path);
break;
}
if (!found) {
/* Command at the index we serialized is different, let's look for command that exactly
* matches but is on different index. If there is no such command we will not resume execution. */
for (command = s->exec_command[id]; command; command = command->command_next)
if (strv_equal(command->argv, argv) && streq(command->path, path))
break;
}
if (command && control) {
s->control_command = command;
s->control_command_id = id;
} else if (command)
s->main_command = command;
else if (last)
log_unit_debug(u, "Current command vanished from the unit file.");
else
log_unit_warning(u, "Current command vanished from the unit file, execution of the command list won't be resumed.");
return 0;
}
static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
Service *s = ASSERT_PTR(SERVICE(u));
int r;
assert(key);
assert(value);
assert(fds);
if (streq(key, "state")) {
ServiceState state;
state = service_state_from_string(value);
if (state < 0)
log_unit_debug_errno(u, state, "Failed to parse state value: %s", value);
else
s->deserialized_state = state;
} else if (streq(key, "result")) {
ServiceResult f;
f = service_result_from_string(value);
if (f < 0)
log_unit_debug_errno(u, f, "Failed to parse result value: %s", value);
else if (f != SERVICE_SUCCESS)
s->result = f;
} else if (streq(key, "reload-result")) {
ServiceResult f;
f = service_result_from_string(value);
if (f < 0)
log_unit_debug_errno(u, f, "Failed to parse reload result value: %s", value);
else if (f != SERVICE_SUCCESS)
s->reload_result = f;
} else if (streq(key, "live-mount-result")) {
ServiceResult f;
f = service_result_from_string(value);
if (f < 0)
log_unit_debug_errno(u, f, "Failed to parse live mount result value: %s", value);
else if (f != SERVICE_SUCCESS)
s->live_mount_result = f;
} else if (streq(key, "control-pid")) {
if (!pidref_is_set(&s->control_pid))
(void) deserialize_pidref(fds, value, &s->control_pid);
} else if (streq(key, "main-pid")) {
PidRef pidref;
if (!pidref_is_set(&s->main_pid) && deserialize_pidref(fds, value, &pidref) >= 0)
(void) service_set_main_pidref(s, pidref, /* start_timestamp= */ NULL);
} else if (streq(key, "main-pid-known")) {
r = parse_boolean(value);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to parse main-pid-known value: %s", value);
else
s->main_pid_known = r;
} else if (streq(key, "bus-name-good")) {
r = parse_boolean(value);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to parse bus-name-good value: %s", value);
else
s->bus_name_good = r;
} else if (streq(key, "accept-socket")) {
Unit *socket;
if (unit_name_to_type(value) != UNIT_SOCKET) {
log_unit_debug(u, "Deserialized accept-socket is not a socket unit, ignoring: %s", value);
return 0;
}
r = manager_load_unit(u->manager, value, NULL, NULL, &socket);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to load accept-socket unit '%s': %m", value);
else {
unit_ref_set(&s->accept_socket, u, socket);
ASSERT_PTR(SOCKET(socket))->n_connections++;
}
} else if (streq(key, "socket-fd")) {
asynchronous_close(s->socket_fd);
s->socket_fd = deserialize_fd(fds, value);
} else if (streq(key, "fd-store-fd")) {
_cleanup_free_ char *fdv = NULL, *fdn = NULL, *fdp = NULL, *fdi = NULL;
_cleanup_close_ int fd = -EBADF;
int do_poll;
uint64_t index = 0;
r = extract_many_words(&value, " ", EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE, &fdv, &fdn, &fdp, &fdi);
if (r < 2 || r > 4) {
log_unit_debug(u, "Failed to deserialize fd-store-fd, ignoring: %s", value);
return 0;
}
fd = deserialize_fd(fds, fdv);
if (fd < 0)
return 0;
do_poll = r >= 3 ? parse_boolean(fdp) : true;
if (do_poll < 0) {
log_unit_debug_errno(u, do_poll,
"Failed to deserialize fd-store-fd do_poll, ignoring: %s", fdp);
return 0;
}
if (r == 4 && safe_atou64(fdi, &index) < 0) {
log_unit_debug(u, "Failed to parse fd-store-fd index '%s', ignoring.", fdi);
index = 0;
}
/* If the unit file is currently absent (e.g. after switch-root, before the unit file is
* available in the new root), the synthesized service has n_fd_store_max=0 and
* preserve_mode=NO, which would reject the fd. Grow the limit by one per fd so it matches
* exactly what was handed back, and force EXEC_PRESERVE_YES, so the fd survives until
* either a daemon-reload picks up the unit file or the service is explicitly stopped.
* Same logic as in luo_dispatch_fd(). */
if (u->load_state == UNIT_NOT_FOUND) {
s->fd_store_preserve_mode = EXEC_PRESERVE_YES;
s->n_fd_store_max++;
}
/* Don't propagate upstream during deserialization: the upstream supervisor (if any)
* already has these fds from when they were originally pushed. */
r = service_add_fd_store(s, TAKE_FD(fd), fdn, do_poll, /* propagate_upstream= */ false);
if (r <= 0 && u->load_state == UNIT_NOT_FOUND)
/* The fd was not actually stored, roll back the limit bump. */
s->n_fd_store_max--;
if (r < 0) {
log_unit_debug_errno(u, r,
"Failed to store deserialized fd '%s', ignoring: %m", fdn);
return 0;
}
/* If preservation is enabled then this fd was previously propagated upstream when it
* was first pushed. Restore the index so future removals can be forwarded upstream
* and the JSON mapping memfd can be regenerated. */
if (r > 0 && s->fd_store && index > 0)
s->fd_store->index = index;
} else if (streq(key, "extra-fd")) {
_cleanup_free_ char *fdv = NULL, *fdn = NULL;
_cleanup_close_ int fd = -EBADF;
r = extract_many_words(&value, " ", EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE, &fdv, &fdn);
if (r != 2) {
log_unit_debug(u, "Failed to deserialize extra-fd, ignoring: %s", value);
return 0;
}
fd = deserialize_fd(fds, fdv);
if (fd < 0)
return 0;
if (!GREEDY_REALLOC(s->extra_fds, s->n_extra_fds + 1)) {
log_oom_debug();
return 0;
}
s->extra_fds[s->n_extra_fds++] = (ServiceExtraFD) {
.fd = TAKE_FD(fd),
.fdname = TAKE_PTR(fdn),
};
} else if (streq(key, "main-exec-status-pid")) {
pid_t pid;
if (parse_pid(value, &pid) < 0)
log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value);
else
s->main_exec_status.pid = pid;
} else if (streq(key, "main-exec-status-code")) {
int i;
if (safe_atoi(value, &i) < 0)
log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value);
else
s->main_exec_status.code = i;
} else if (streq(key, "main-exec-status-status")) {
int i;
if (safe_atoi(value, &i) < 0)
log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value);
else
s->main_exec_status.status = i;
} else if (streq(key, "main-exec-status-start"))
(void) deserialize_dual_timestamp(value, &s->main_exec_status.start_timestamp);
else if (streq(key, "main-exec-status-exit"))
(void) deserialize_dual_timestamp(value, &s->main_exec_status.exit_timestamp);
else if (streq(key, "main-exec-status-handoff"))
(void) deserialize_dual_timestamp(value, &s->main_exec_status.handoff_timestamp);
else if (STR_IN_SET(key, "main-command", "control-command")) {
r = service_deserialize_exec_command(u, key, value);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to parse serialized command \"%s\": %m", value);
} else if (streq(key, "notify-access-override")) {
NotifyAccess notify_access;
notify_access = notify_access_from_string(value);
if (notify_access < 0)
log_unit_debug(u, "Failed to parse notify-access-override value: %s", value);
else
s->notify_access_override = notify_access;
} else if (streq(key, "notify-state")) {
NotifyState notify_state;
notify_state = notify_state_from_string(value);
if (notify_state < 0)
log_unit_debug(u, "Failed to parse notify-state value: %s", value);
else
s->notify_state = notify_state;
} else if (streq(key, "n-restarts")) {
r = safe_atou(value, &s->n_restarts);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value);
} else if (streq(key, "restart-randomized-delay-chosen-usec")) {
(void) deserialize_usec(value, &s->restart_randomized_delay_chosen_usec);
} else if (streq(key, "forbid-restart")) {
r = parse_boolean(value);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to parse forbid-restart value: %s", value);
else
s->forbid_restart = r;
} else if (streq(key, "stdin-fd")) {
asynchronous_close(s->stdin_fd);
s->stdin_fd = deserialize_fd(fds, value);
if (s->stdin_fd >= 0)
s->exec_context.stdio_as_fds = true;
} else if (streq(key, "stdout-fd")) {
asynchronous_close(s->stdout_fd);
s->stdout_fd = deserialize_fd(fds, value);
if (s->stdout_fd >= 0)
s->exec_context.stdio_as_fds = true;
} else if (streq(key, "stderr-fd")) {
asynchronous_close(s->stderr_fd);
s->stderr_fd = deserialize_fd(fds, value);
if (s->stderr_fd >= 0)
s->exec_context.stdio_as_fds = true;
} else if (streq(key, "root-directory-fd")) {
asynchronous_close(s->root_directory_fd);
s->root_directory_fd = deserialize_fd(fds, value);
if (s->root_directory_fd >= 0)
s->exec_context.root_directory_as_fd = true;
} else if (streq(key, "exec-fd")) {
_cleanup_close_ int fd = -EBADF;
fd = deserialize_fd(fds, value);
if (fd >= 0) {
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source);
if (service_allocate_exec_fd_event_source(s, fd, &s->exec_fd_event_source) >= 0)
TAKE_FD(fd);
}
} else if (streq(key, "status-text")) {
char *t;
ssize_t l;
l = cunescape(value, 0, &t);
if (l < 0)
log_unit_debug_errno(u, l, "Failed to unescape status text '%s': %m", value);
else
free_and_replace(s->status_text, t);
} else if (streq(key, "status-errno")) {
int i;
if (safe_atoi(value, &i) < 0)
log_unit_debug(u, "Failed to parse status-errno value: %s", value);
else
s->status_errno = i;
} else if (streq(key, "status-bus-error")) {
if (free_and_strdup(&s->status_bus_error, value) < 0)
log_oom_debug();
} else if (streq(key, "status-varlink-error")) {
if (free_and_strdup(&s->status_varlink_error, value) < 0)
log_oom_debug();
} else if (streq(key, "watchdog-timestamp"))
(void) deserialize_dual_timestamp(value, &s->watchdog_timestamp);
else if (streq(key, "watchdog-original-usec"))
(void) deserialize_usec(value, &s->watchdog_original_usec);
else if (streq(key, "watchdog-override-usec")) {
if (deserialize_usec(value, &s->watchdog_override_usec) >= 0)
s->watchdog_override_enable = true;
} else if (streq(key, "reload-begin-usec"))
(void) deserialize_usec(value, &s->reload_begin_usec);
else if (streq(key, "revalidate-runtime-begin-usec"))
(void) deserialize_usec(value, &s->revalidate_runtime_begin_usec);
else if (streq(key, "refreshed-mask")) {
r = service_refresh_on_reload_from_string_many(value, &s->refreshed_mask);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to parse refreshed-mask value: %s", value);
} else
log_unit_debug(u, "Unknown serialization key: %s", key);
return 0;
}
static UnitActiveState service_active_state(Unit *u) {
Service *s = ASSERT_PTR(SERVICE(u));
const UnitActiveState *table;
table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
return table[s->state];
}
static const char *service_sub_state_to_string(Unit *u) {
assert(u);
return service_state_to_string(SERVICE(u)->state);
}
static bool service_may_gc(Unit *u) {
Service *s = ASSERT_PTR(SERVICE(u));
/* Never clean up services that still have a process around, even if the service is formally dead. Note that
* unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they
* have moved outside of the cgroup. */
if (main_pid_good(s) > 0 ||
control_pid_good(s) > 0)
return false;
/* Only allow collection of actually dead services, i.e. not those that are in the transitionary
* SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART states, and not those
* that still have resources pinned (fd store with FileDescriptorStorePreserve=yes) in case they are
* started again later despite not having any reverse dependency. */
if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED))
return false;
return true;
}
static int service_retry_pid_file(Service *s) {
int r;
assert(s);
assert(s->pid_file);
assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST));
r = service_load_pid_file(s, false);
if (r < 0)
return r;
service_unwatch_pid_file(s);
service_enter_running(s, SERVICE_SUCCESS);
return 0;
}
static int service_watch_pid_file(Service *s) {
int r;
assert(s);
log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path);
r = path_spec_watch(s->pid_file_pathspec, service_dispatch_inotify_io);
if (r < 0) {
log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path);
service_unwatch_pid_file(s);
return r;
}
/* the pidfile might have appeared just before we set the watch */
log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path);
service_retry_pid_file(s);
return 0;
}
static int service_demand_pid_file(Service *s) {
_cleanup_free_ PathSpec *ps = NULL;
assert(s);
assert(s->pid_file);
assert(!s->pid_file_pathspec);
ps = new(PathSpec, 1);
if (!ps)
return -ENOMEM;
*ps = (PathSpec) {
.unit = UNIT(s),
.path = strdup(s->pid_file),
/* PATH_CHANGED would not be enough. There are daemons (sendmail) that keep their PID file
* open all the time. */
.type = PATH_MODIFIED,
.inotify_fd = -EBADF,
};
if (!ps->path)
return -ENOMEM;
path_simplify(ps->path);
s->pid_file_pathspec = TAKE_PTR(ps);
return service_watch_pid_file(s);
}
static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
PathSpec *p = ASSERT_PTR(userdata);
Service *s = ASSERT_PTR(SERVICE(p->unit));
assert(fd >= 0);
assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST));
assert(s->pid_file_pathspec);
assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
log_unit_debug(UNIT(s), "inotify event");
if (path_spec_fd_event(p, events) < 0)
goto fail;
if (service_retry_pid_file(s) == 0)
return 0;
if (service_watch_pid_file(s) < 0)
goto fail;
return 0;
fail:
service_unwatch_pid_file(s);
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
return 0;
}
static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
Service *s = ASSERT_PTR(SERVICE(userdata));
log_unit_debug(UNIT(s), "got exec-fd event");
/* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve()
* successfully for it. We implement this through a pipe() towards the child, which the kernel
* automatically closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on
* the pipe in the parent. We need to be careful however, as there are other reasons that we might
* cause the child's side of the pipe to be closed (for example, a simple exit()). To deal with that
* we'll ignore EOFs on the pipe unless the child signalled us first that it is about to call the
* execve(). It does so by sending us a simple non-zero byte via the pipe. We also provide the child
* with a way to inform us in case execve() failed: if it sends a zero byte we'll ignore POLLHUP on
* the fd again. */
for (;;) {
uint8_t x;
ssize_t n;
n = read(fd, &x, sizeof(x));
if (n < 0) {
if (errno == EAGAIN) /* O_NONBLOCK in effect → everything queued has now been processed. */
return 0;
return log_unit_error_errno(UNIT(s), errno, "Failed to read from exec_fd: %m");
}
if (n == 0) { /* EOF → the event we are waiting for in case of Type=exec */
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source);
if (s->exec_fd_hot) { /* Did the child tell us to expect EOF now? */
log_unit_debug(UNIT(s), "Got EOF on exec-fd");
s->exec_fd_hot = false;
/* Nice! This is what we have been waiting for. Transition to next state. */
if (s->type == SERVICE_EXEC && s->state == SERVICE_START)
service_enter_start_post(s);
} else
log_unit_debug(UNIT(s), "Got EOF on exec-fd while it was disabled, ignoring.");
return 0;
}
/* A byte was read → this turns on/off the exec fd logic */
assert(n == sizeof(x));
s->exec_fd_hot = x;
}
}
static void service_notify_cgroup_empty_event(Unit *u) {
Service *s = ASSERT_PTR(SERVICE(u));
log_unit_debug(u, "Control group is empty.");
switch (s->state) {
/* Waiting for SIGCHLD is usually more interesting, because it includes return
* codes/signals. Which is why we ignore the cgroup events for most cases, except when we
* don't know pid which to expect the SIGCHLD for. */
case SERVICE_START:
if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) &&
main_pid_good(s) == 0 &&
control_pid_good(s) == 0) {
/* No chance of getting a ready notification anymore */
service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL);
break;
}
if (s->exit_type == SERVICE_EXIT_CGROUP && main_pid_good(s) <= 0) {
service_enter_stop_post(s, SERVICE_SUCCESS);
break;
}
_fallthrough_;
case SERVICE_START_POST:
if (s->pid_file_pathspec &&
main_pid_good(s) == 0 &&
control_pid_good(s) == 0) {
/* Give up hoping for the daemon to write its PID file */
log_unit_warning(u, "Daemon never wrote its PID file. Failing.");
service_unwatch_pid_file(s);
if (s->state == SERVICE_START)
service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL);
else
service_enter_stop(s, SERVICE_FAILURE_PROTOCOL);
}
break;
case SERVICE_RUNNING:
case SERVICE_RUNNING_REVALIDATING:
/* service_enter_running() will figure out what to do */
service_enter_running(s, SERVICE_SUCCESS);
break;
case SERVICE_STOP_WATCHDOG:
case SERVICE_STOP_SIGTERM:
case SERVICE_STOP_SIGKILL:
if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0)
service_enter_stop_post(s, SERVICE_SUCCESS);
break;
case SERVICE_STOP_POST:
case SERVICE_FINAL_WATCHDOG:
case SERVICE_FINAL_SIGTERM:
case SERVICE_FINAL_SIGKILL:
if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0)
service_enter_dead(s, SERVICE_SUCCESS, true);
break;
/* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
* up the cgroup earlier and should do it now. */
case SERVICE_AUTO_RESTART:
case SERVICE_AUTO_RESTART_QUEUED:
unit_prune_cgroup(u);
break;
default:
;
}
}
static void service_notify_cgroup_oom_event(Unit *u, bool managed_oom) {
Service *s = ASSERT_PTR(SERVICE(u));
if (managed_oom)
log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd.");
else
log_unit_debug(u, "Process of control group was killed by the OOM killer.");
if (s->oom_policy == OOM_CONTINUE)
return;
switch (s->state) {
case SERVICE_CONDITION:
case SERVICE_START_PRE:
case SERVICE_START:
case SERVICE_START_POST:
case SERVICE_STOP:
if (s->oom_policy == OOM_STOP)
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_OOM_KILL);
else if (s->oom_policy == OOM_KILL)
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL);
break;
case SERVICE_EXITED:
case SERVICE_RUNNING:
case SERVICE_RUNNING_REVALIDATING:
if (s->oom_policy == OOM_STOP)
service_enter_stop(s, SERVICE_FAILURE_OOM_KILL);
else if (s->oom_policy == OOM_KILL)
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL);
break;
case SERVICE_STOP_WATCHDOG:
case SERVICE_STOP_SIGTERM:
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL);
break;
case SERVICE_STOP_SIGKILL:
case SERVICE_FINAL_SIGKILL:
if (s->result == SERVICE_SUCCESS)
s->result = SERVICE_FAILURE_OOM_KILL;
break;
case SERVICE_STOP_POST:
case SERVICE_FINAL_SIGTERM:
service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_OOM_KILL);
break;
default:
;
}
}
static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
Service *s = ASSERT_PTR(SERVICE(u));
bool notify_dbus = true;
ServiceResult f;
ExitClean clean_mode;
int r;
assert(pid >= 0);
/* Oneshot services and non-SERVICE_EXEC_START commands should not be
* considered daemons as they are typically not long running. */
if (s->type == SERVICE_ONESHOT || (s->control_pid.pid == pid && s->control_command_id != SERVICE_EXEC_START))
clean_mode = EXIT_CLEAN_COMMAND;
else
clean_mode = EXIT_CLEAN_DAEMON;
/* Our own helper processes are not subject to SuccessExitStatus= as they're opaque to users */
if (is_clean_exit(code, status, clean_mode, s->control_pid.pid == pid && s->control_command_id < 0 ? NULL : &s->success_status))
f = SERVICE_SUCCESS;
else if (code == CLD_EXITED)
f = SERVICE_FAILURE_EXIT_CODE;
else if (code == CLD_KILLED)
f = SERVICE_FAILURE_SIGNAL;
else if (code == CLD_DUMPED)
f = SERVICE_FAILURE_CORE_DUMP;
else
assert_not_reached();
if (s->main_pid.pid == pid) {
/* Clean up the exec_fd event source. We want to do this here, not later in
* service_set_state(), because service_enter_stop_post() calls service_spawn().
* The source owns its end of the pipe, so this will close that too. */
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source);
/* Forking services may occasionally move to a new PID.
* As long as they update the PID file before exiting the old
* PID, they're fine. */
if (service_load_pid_file(s, false) > 0)
return;
pidref_done(&s->main_pid);
exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
if (s->main_command) {
/* If this is not a forking service than the
* main process got started and hence we copy
* the exit status so that it is recorded both
* as main and as control process exit
* status */
s->main_command->exec_status = s->main_exec_status;
if (s->main_command->flags & EXEC_COMMAND_IGNORE_FAILURE)
f = SERVICE_SUCCESS;
} else if (s->exec_command[SERVICE_EXEC_START]) {
/* If this is a forked process, then we should
* ignore the return value if this was
* configured for the starter process */
if (s->exec_command[SERVICE_EXEC_START]->flags & EXEC_COMMAND_IGNORE_FAILURE)
f = SERVICE_SUCCESS;
}
unit_log_process_exit(
u,
"Main process",
service_exec_command_to_string(SERVICE_EXEC_START),
f == SERVICE_SUCCESS,
code, status);
if (s->result == SERVICE_SUCCESS)
s->result = f;
if (s->main_command &&
s->main_command->command_next &&
s->type == SERVICE_ONESHOT &&
f == SERVICE_SUCCESS) {
/* There is another command to execute, so let's do that. */
log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state));
service_run_next_main(s);
} else {
s->main_command = NULL;
/* Services with ExitType=cgroup do not act on main PID exiting, unless the cgroup is
* already empty */
if (s->exit_type == SERVICE_EXIT_MAIN || cgroup_good(s) <= 0) {
/* The service exited, so the service is officially gone. */
switch (s->state) {
case SERVICE_START_POST:
case SERVICE_REFRESH_EXTENSIONS:
case SERVICE_REFRESH_CREDENTIALS:
case SERVICE_RELOAD:
case SERVICE_RELOAD_SIGNAL:
case SERVICE_RELOAD_NOTIFY:
case SERVICE_RELOAD_POST:
case SERVICE_MOUNTING:
/* If neither main nor control processes are running then the current
* state can never exit cleanly, hence immediately terminate the
* service. */
if (control_pid_good(s) <= 0)
service_enter_stop(s, f);
/* Otherwise need to wait until the operation is done. */
break;
case SERVICE_STOP:
/* Need to wait until the operation is done. */
break;
case SERVICE_START:
if (s->type == SERVICE_ONESHOT) {
/* This was our main goal, so let's go on */
if (f == SERVICE_SUCCESS)
service_enter_start_post(s);
else
service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
break;
} else if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD)) {
/* Only enter running through a notification, so that the
* SERVICE_START state signifies that no ready notification
* has been received */
if (f != SERVICE_SUCCESS)
service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
else if (!s->remain_after_exit || service_get_notify_access(s) == NOTIFY_MAIN)
/* The service has never been and will never be active */
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL);
break;
}
_fallthrough_;
case SERVICE_RUNNING:
case SERVICE_RUNNING_REVALIDATING:
service_enter_running(s, f);
break;
case SERVICE_STOP_WATCHDOG:
case SERVICE_STOP_SIGTERM:
case SERVICE_STOP_SIGKILL:
if (control_pid_good(s) <= 0)
service_enter_stop_post(s, f);
/* If there is still a control process, wait for that first */
break;
case SERVICE_STOP_POST:
if (control_pid_good(s) <= 0)
service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
break;
case SERVICE_FINAL_WATCHDOG:
case SERVICE_FINAL_SIGTERM:
case SERVICE_FINAL_SIGKILL:
if (control_pid_good(s) <= 0)
service_enter_dead(s, f, true);
break;
default:
assert_not_reached();
}
} else if (s->exit_type == SERVICE_EXIT_CGROUP && s->state == SERVICE_START &&
!IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD, SERVICE_DBUS))
/* If a main process exits very quickly, this function might be executed
* before service_dispatch_exec_io(). Since this function disabled IO events
* to monitor the main process above, we need to update the state here too.
* Let's consider the process is successfully launched and exited, but
* only when we're not expecting a readiness notification or dbus name. */
service_enter_start_post(s);
}
} else if (s->control_pid.pid == pid) {
const char *kind;
bool success;
pidref_done(&s->control_pid);
if (s->control_command) {
exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE)
f = SERVICE_SUCCESS;
}
/* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */
if (s->state == SERVICE_CONDITION) {
if (f == SERVICE_FAILURE_EXIT_CODE && status < 255) {
UNIT(s)->condition_result = false;
f = SERVICE_SKIP_CONDITION;
success = true;
} else if (f == SERVICE_SUCCESS) {
UNIT(s)->condition_result = true;
success = true;
} else
success = false;
kind = "Condition check process";
} else {
kind = "Control process";
success = f == SERVICE_SUCCESS;
}
unit_log_process_exit(
u,
kind,
service_exec_command_to_string(s->control_command_id),
success,
code, status);
if (!IN_SET(s->state, SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, SERVICE_RELOAD, SERVICE_RELOAD_POST, SERVICE_MOUNTING) &&
s->result == SERVICE_SUCCESS)
s->result = f;
if (s->control_command &&
s->control_command->command_next &&
f == SERVICE_SUCCESS) {
/* There is another command to execute, so let's do that. */
log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state));
service_run_next_control(s);
} else {
/* No further commands for this step, so let's figure out what to do next */
s->control_command = NULL;
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state));
switch (s->state) {
case SERVICE_CONDITION:
if (f == SERVICE_SUCCESS)