#include <dirent.h>
#include <unistd.h>
#include <sys/reboot.h>
+#include <linux/reboot.h>
+#include <sys/syscall.h>
#include "manager.h"
#include "unit.h"
s->socket_fd = -1;
s->guess_main_pid = true;
- exec_context_init(&s->exec_context);
- kill_context_init(&s->kill_context);
- cgroup_context_init(&s->cgroup_context);
-
- RATELIMIT_INIT(s->start_limit,
- u->manager->default_start_limit_interval,
- u->manager->default_start_limit_burst);
+ RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
}
if (s->socket_fd < 0)
return;
- close_nointr_nofail(s->socket_fd);
- s->socket_fd = -1;
+ s->socket_fd = safe_close(s->socket_fd);
}
static void service_connection_unref(Service *s) {
r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
} else {
- r = sd_event_add_monotonic(UNIT(s)->manager->event, s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, service_dispatch_watchdog, s, &s->watchdog_event_source);
+ r = sd_event_add_time(
+ UNIT(s)->manager->event,
+ &s->watchdog_event_source,
+ CLOCK_MONOTONIC,
+ s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
+ service_dispatch_watchdog, s);
if (r < 0) {
log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
return;
free(s->status_text);
s->status_text = NULL;
- cgroup_context_done(&s->cgroup_context);
- exec_context_done(&s->exec_context);
+ free(s->reboot_arg);
+ s->reboot_arg = NULL;
+
s->exec_runtime = exec_runtime_unref(s->exec_runtime);
exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
s->control_command = NULL;
return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
}
- return sd_event_add_monotonic(UNIT(s)->manager->event, now(CLOCK_MONOTONIC) + usec, 0, service_dispatch_timer, s, &s->timer_event_source);
+ return sd_event_add_time(
+ UNIT(s)->manager->event,
+ &s->timer_event_source,
+ CLOCK_MONOTONIC,
+ now(CLOCK_MONOTONIC) + usec, 0,
+ service_dispatch_timer, s);
}
#ifdef HAVE_SYSV_COMPAT
static char *sysv_translate_name(const char *name) {
char *r;
- r = new(char, strlen(name) + sizeof(".service"));
+ r = new(char, strlen(name) + strlen(".service") + 1);
if (!r)
return NULL;
static const char * const table[] = {
/* LSB defined facilities */
"local_fs", NULL,
- "network", SPECIAL_NETWORK_TARGET,
+ "network", SPECIAL_NETWORK_ONLINE_TARGET,
"named", SPECIAL_NSS_LOOKUP_TARGET,
"portmap", SPECIAL_RPCBIND_TARGET,
"remote_fs", SPECIAL_REMOTE_FS_TARGET,
state = NORMAL;
- if (sscanf(t+10, "%15s %i %*i",
- runlevels,
- &start_priority) != 2) {
-
- log_warning_unit(u->id,
- "[%s:%u] Failed to parse chkconfig line. Ignoring.",
- path, line);
+ if (sscanf(t+10, "%15s %i %*i", runlevels, &start_priority) != 2) {
+ log_warning_unit(u->id, "[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
continue;
}
* symlink farms is preferred over the
* data from the LSB header. */
if (start_priority < 0 || start_priority > 99)
- log_warning_unit(u->id,
- "[%s:%u] Start priority out of range. Ignoring.",
- path, line);
+ log_warning_unit(u->id, "[%s:%u] Start priority out of range. Ignoring.", path, line);
else
- s->sysv_start_priority = start_priority;
+ log_debug_unit(u->id, "[%s:%u] Ignoring start priority set in the chkconfig file.", path, line);
char_array_0(runlevels);
k = delete_chars(runlevels, WHITESPACE "-");
-
if (k[0]) {
char *d;
- if (!(d = strdup(k))) {
+ d = strdup(k);
+ if (!d) {
r = -ENOMEM;
goto finish;
}
FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
char *n, *m;
+ bool is_before;
if (!(n = strndup(w, z))) {
r = -ENOMEM;
if (r == 0)
continue;
- r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
+ is_before = startswith_no_case(t, "X-Start-Before:");
+
+ if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before)
+ /* the network-online target is special, as it needs to be actively pulled in */
+ r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, m, NULL, true);
+ else
+ r = unit_add_dependency_by_name(u, is_before ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
if (r < 0)
log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
u->description = d;
}
- /* The priority that has been set in /etc/rcN.d/ hierarchies
- * takes precedence over what is stored as default in the LSB
- * header */
+ /* Initialize the start priority from what has been set in the
+ * /etc/rcN.d/ hierarchies if we load the unit file as SysV
+ * init script. */
if (s->sysv_start_priority_from_rcnd >= 0)
s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
service_fix_output(s);
+ r = unit_patch_contexts(u);
+ if (r < 0)
+ return r;
+
r = unit_add_exec_dependencies(u, &s->exec_context);
if (r < 0)
return r;
- r = unit_add_default_slice(u);
+ r = unit_add_default_slice(u, &s->cgroup_context);
if (r < 0)
return r;
return r;
#endif
- if (s->bus_name) {
- r = unit_watch_bus_name(u, s->bus_name);
- if (r < 0)
- return r;
- }
-
if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
s->notify_access = NOTIFY_MAIN;
if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
s->notify_access = NOTIFY_MAIN;
- if (UNIT(s)->default_dependencies) {
- r = service_add_default_dependencies(s);
+ if (s->bus_name) {
+ r = unit_watch_bus_name(u, s->bus_name);
if (r < 0)
return r;
}
- r = unit_exec_context_defaults(u, &s->exec_context);
- if (r < 0)
- return r;
+ if (u->default_dependencies) {
+ r = service_add_default_dependencies(s);
+ if (r < 0)
+ return r;
+ }
}
return service_verify(s);
return r;
}
- if (kill(pid, 0) < 0 && errno != EPERM) {
+ if (!pid_is_alive(pid)) {
if (may_warn)
- log_info_unit(UNIT(s)->id,
- "PID "PID_FMT" read from file %s does not exist.",
- pid, s->pid_file);
+ log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
+
return -ESRCH;
}
log_warning_unit(UNIT(s)->id,
"Failed to watch PID "PID_FMT" from service %s",
pid, UNIT(s)->id);
- return r;
-
- return 0;
+ return r;
}
static void service_set_state(Service *s, ServiceState state) {
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
}
+ if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
+ unit_unwatch_all_pids(UNIT(s));
+
if (!IN_SET(state,
SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
SERVICE_RUNNING, SERVICE_RELOAD,
return r;
}
- if (pid_valid(s->main_pid) &&
+ if (pid_is_unwaited(s->main_pid) &&
((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
IN_SET(s->deserialized_state,
SERVICE_START, SERVICE_START_POST,
return r;
}
- if (pid_valid(s->control_pid) &&
+ if (pid_is_unwaited(s->control_pid) &&
IN_SET(s->deserialized_state,
SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
SERVICE_RELOAD,
return r;
}
+ if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
+ unit_watch_all_pids(UNIT(s));
+
if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
service_start_watchdog(s);
UNIT(s)->manager->confirm_spawn,
UNIT(s)->manager->cgroup_supported,
path,
+ manager_get_runtime_prefix(UNIT(s)->manager),
UNIT(s)->id,
s->watchdog_usec,
s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
/* If it's an alien child let's check if it is still
* alive ... */
if (s->main_pid_alien && s->main_pid > 0)
- return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
+ return pid_is_alive(s->main_pid);
/* .. otherwise assume we'll get a SIGCHLD for it,
* which we really should wait for to collect exit
return !r;
}
+static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none);
+
static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
int r;
assert(s);
service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
+ if (s->result != SERVICE_SUCCESS)
+ service_execute_action(s, s->failure_action, "failed", false);
+
if (allow_restart &&
!s->forbid_restart &&
(s->restart == SERVICE_RESTART_ALWAYS ||
s->forbid_restart = false;
- /* we want fresh tmpdirs in case service is started again immediately */
+ /* We want fresh tmpdirs in case service is started again immediately */
exec_runtime_destroy(s->exec_runtime);
s->exec_runtime = exec_runtime_unref(s->exec_runtime);
+ /* Also, remove the runtime directory in */
+ exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
+
/* Try to delete the pid file. At this point it will be
* out-of-date, and some software might be confused by it, so
* let's remove it. */
s->result = f;
service_unwatch_control_pid(s);
+ unit_watch_all_pids(UNIT(s));
s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
if (s->control_command) {
if (f != SERVICE_SUCCESS)
s->result = f;
+ unit_watch_all_pids(UNIT(s));
+
r = unit_kill_context(
UNIT(s),
&s->kill_context,
s->result = f;
service_unwatch_control_pid(s);
+ unit_watch_all_pids(UNIT(s));
s->control_command = s->exec_command[SERVICE_EXEC_STOP];
if (s->control_command) {
service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
}
-static int service_start_limit_test(Service *s) {
+static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) {
assert(s);
- if (ratelimit_test(&s->start_limit))
- return 0;
+ if (action == SERVICE_FAILURE_ACTION_REBOOT ||
+ action == SERVICE_FAILURE_ACTION_REBOOT_FORCE)
+ update_reboot_param_file(s->reboot_arg);
- switch (s->start_limit_action) {
+ switch (action) {
- case SERVICE_START_LIMIT_NONE:
- log_warning_unit(UNIT(s)->id,
- "%s start request repeated too quickly, refusing to start.",
- UNIT(s)->id);
+ case SERVICE_FAILURE_ACTION_NONE:
+ if (log_action_none)
+ log_warning_unit(UNIT(s)->id,
+ "%s %s, refusing to start.", UNIT(s)->id, reason);
break;
- case SERVICE_START_LIMIT_REBOOT: {
+ case SERVICE_FAILURE_ACTION_REBOOT: {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
log_warning_unit(UNIT(s)->id,
- "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
+ "%s %s, rebooting.", UNIT(s)->id, reason);
r = manager_add_job_by_name(UNIT(s)->manager, JOB_START,
SPECIAL_REBOOT_TARGET, JOB_REPLACE,
break;
}
- case SERVICE_START_LIMIT_REBOOT_FORCE:
+ case SERVICE_FAILURE_ACTION_REBOOT_FORCE:
log_warning_unit(UNIT(s)->id,
- "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
+ "%s %s, forcibly rebooting.", UNIT(s)->id, reason);
UNIT(s)->manager->exit_code = MANAGER_REBOOT;
break;
- case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
+ case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE:
log_warning_unit(UNIT(s)->id,
- "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
+ "%s %s, rebooting immediately.", UNIT(s)->id, reason);
sync();
+ if (s->reboot_arg) {
+ log_info("Rebooting with argument '%s'.", s->reboot_arg);
+ syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
+ LINUX_REBOOT_CMD_RESTART2, s->reboot_arg);
+ }
+
+ log_info("Rebooting.");
reboot(RB_AUTOBOOT);
break;
default:
log_error_unit(UNIT(s)->id,
- "start limit action=%i", s->start_limit_action);
- assert_not_reached("Unknown StartLimitAction.");
+ "failure action=%i", action);
+ assert_not_reached("Unknown FailureAction.");
}
return -ECANCELED;
}
+static int service_start_limit_test(Service *s) {
+ assert(s);
+
+ if (ratelimit_test(&s->start_limit))
+ return 0;
+
+ return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true);
+}
+
static int service_start(Unit *u) {
Service *s = SERVICE(u);
int r;
log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
else {
- if (s->socket_fd >= 0)
- close_nointr_nofail(s->socket_fd);
+ safe_close(s->socket_fd);
s->socket_fd = fdset_remove(fds, fd);
}
} else if (streq(key, "main-exec-status-pid")) {
return 0;
}
+static void service_notify_cgroup_empty_event(Unit *u) {
+ Service *s = SERVICE(u);
+
+ assert(u);
+
+ log_debug_unit(u->id, "%s: cgroup is empty", u->id);
+
+ 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:
+ case SERVICE_START_POST:
+ /* If we were hoping for the daemon to write its PID file,
+ * we can give up now. */
+ if (s->pid_file_pathspec) {
+ log_warning_unit(u->id,
+ "%s never wrote its PID file. Failing.", UNIT(s)->id);
+ service_unwatch_pid_file(s);
+ if (s->state == SERVICE_START)
+ service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
+ else
+ service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
+ }
+ break;
+
+ case SERVICE_RUNNING:
+ /* service_enter_running() will figure out what to do */
+ service_enter_running(s, SERVICE_SUCCESS);
+ break;
+
+ case SERVICE_STOP_SIGTERM:
+ case SERVICE_STOP_SIGKILL:
+
+ if (main_pid_good(s) <= 0 && !control_pid_good(s))
+ service_enter_stop_post(s, SERVICE_SUCCESS);
+
+ break;
+
+ case SERVICE_STOP_POST:
+ case SERVICE_FINAL_SIGTERM:
+ case SERVICE_FINAL_SIGKILL:
+ if (main_pid_good(s) <= 0 && !control_pid_good(s))
+ service_enter_dead(s, SERVICE_SUCCESS, true);
+
+ break;
+
+ default:
+ ;
+ }
+}
+
static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
Service *s = SERVICE(u);
ServiceResult f;
/* Notify clients about changed exit status */
unit_add_to_dbus_queue(u);
+
+ /* We got one SIGCHLD for the service, let's watch all
+ * processes that are now running of the service, and watch
+ * that. Among the PIDs we then watch will be children
+ * reassigned to us, which hopefully allows us to identify
+ * when all children are gone */
+ unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
+ unit_watch_all_pids(u);
+
+ /* If the PID set is empty now, then let's finish this off */
+ if (set_isempty(u->pids))
+ service_notify_cgroup_empty_event(u);
}
static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
case SERVICE_AUTO_RESTART:
log_info_unit(UNIT(s)->id,
- "%s holdoff time over, scheduling restart.", UNIT(s)->id);
+ s->restart_usec > 0 ?
+ "%s holdoff time over, scheduling restart." :
+ "%s has no holdoff time, scheduling restart.",
+ UNIT(s)->id);
service_enter_restart(s);
break;
return 0;
}
-static void service_notify_cgroup_empty_event(Unit *u) {
- Service *s = SERVICE(u);
-
- assert(u);
-
- log_debug_unit(u->id, "%s: cgroup is empty", u->id);
-
- 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:
- case SERVICE_START_POST:
- /* If we were hoping for the daemon to write its PID file,
- * we can give up now. */
- if (s->pid_file_pathspec) {
- log_warning_unit(u->id,
- "%s never wrote its PID file. Failing.", UNIT(s)->id);
- service_unwatch_pid_file(s);
- if (s->state == SERVICE_START)
- service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
- else
- service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
- }
- break;
-
- case SERVICE_RUNNING:
- /* service_enter_running() will figure out what to do */
- service_enter_running(s, SERVICE_SUCCESS);
- break;
-
- case SERVICE_STOP_SIGTERM:
- case SERVICE_STOP_SIGKILL:
-
- if (main_pid_good(s) <= 0 && !control_pid_good(s))
- service_enter_stop_post(s, SERVICE_SUCCESS);
-
- break;
-
- case SERVICE_STOP_POST:
- case SERVICE_FINAL_SIGTERM:
- case SERVICE_FINAL_SIGKILL:
- if (main_pid_good(s) <= 0 && !control_pid_good(s))
- service_enter_dead(s, SERVICE_SUCCESS, true);
-
- break;
-
- default:
- ;
- }
-}
-
static void service_notify_message(Unit *u, pid_t pid, char **tags) {
Service *s = SERVICE(u);
const char *e;
return;
}
- if (s->notify_access == NOTIFY_MAIN && s->main_pid != 0 && pid != s->main_pid) {
- log_warning_unit(u->id,
- "%s: Got notification message from PID "PID_FMT", but reception only permitted for PID "PID_FMT,
- u->id, pid, s->main_pid);
+ if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
+
+ if (s->main_pid != 0)
+ 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);
+ else
+ 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);
return;
}
free(t);
}
- /* Interpet WATCHDOG= */
+ /* Interpret WATCHDOG= */
if (strv_find(tags, "WATCHDOG=1")) {
log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
service_reset_watchdog(s);
DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
-static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
- [SERVICE_START_LIMIT_NONE] = "none",
- [SERVICE_START_LIMIT_REBOOT] = "reboot",
- [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
- [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
+static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = {
+ [SERVICE_FAILURE_ACTION_NONE] = "none",
+ [SERVICE_FAILURE_ACTION_REBOOT] = "reboot",
+ [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
+ [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
};
-DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
+DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);
const UnitVTable service_vtable = {
.object_size = sizeof(Service),