#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_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, service_dispatch_watchdog, s);
+ 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, &s->timer_event_source, now(CLOCK_MONOTONIC) + usec, 0, service_dispatch_timer, s);
+ 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;
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;
}
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);
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) {
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,
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. */
if (ratelimit_test(&s->start_limit))
return 0;
+ if (s->start_limit_action == SERVICE_START_LIMIT_REBOOT ||
+ s->start_limit_action == SERVICE_START_LIMIT_REBOOT_FORCE)
+ update_reboot_param_file(s->reboot_arg);
+
switch (s->start_limit_action) {
case SERVICE_START_LIMIT_NONE:
log_warning_unit(UNIT(s)->id,
"%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
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;
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")) {
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);