if (!pidref_is_set(&s->control_pid))
return;
- unit_unwatch_pid(UNIT(s), s->control_pid.pid);
+ unit_unwatch_pidref(UNIT(s), &s->control_pid);
pidref_done(&s->control_pid);
}
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
}
-static int swap_arm_timer(Swap *s, usec_t usec) {
- int r;
-
+static int swap_arm_timer(Swap *s, bool relative, usec_t usec) {
assert(s);
- if (s->timer_event_source) {
- r = sd_event_source_set_time(s->timer_event_source, usec);
- if (r < 0)
- return r;
-
- return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
- }
-
- if (usec == USEC_INFINITY)
- return 0;
-
- r = sd_event_add_time(
- UNIT(s)->manager->event,
- &s->timer_event_source,
- CLOCK_MONOTONIC,
- usec, 0,
- swap_dispatch_timer, s);
- if (r < 0)
- return r;
-
- (void) sd_event_source_set_description(s->timer_event_source, "swap-timer");
-
- return 0;
+ return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, swap_dispatch_timer);
}
static SwapParameters* swap_get_parameters(Swap *s) {
delete = true;
r = unit_new_for_name(m, sizeof(Swap), e, &u);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(u, r, "Failed to load swap unit: %m");
goto fail;
+ }
SWAP(u)->what = strdup(what);
if (!SWAP(u)->what) {
- r = -ENOMEM;
+ r = log_oom();
goto fail;
}
if (!p->what) {
p->what = strdup(what_proc_swaps);
if (!p->what) {
- r = -ENOMEM;
+ r = log_oom();
goto fail;
}
}
return 0;
fail:
- log_unit_warning_errno(u, r, "Failed to load swap unit: %m");
-
if (delete)
unit_free(u);
pid_is_unwaited(s->control_pid.pid) &&
SWAP_STATE_WITH_PROCESS(new_state)) {
- r = unit_watch_pid(UNIT(s), s->control_pid.pid, /* exclusive= */ false);
+ r = unit_watch_pidref(UNIT(s), &s->control_pid, /* exclusive= */ false);
if (r < 0)
return r;
- r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
+ r = swap_arm_timer(s, /* relative= */ false, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
if (r < 0)
return r;
}
if (r < 0)
return r;
- r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
+ r = swap_arm_timer(s, /* relative= */ true, s->timeout_usec);
if (r < 0)
return r;
if (r < 0)
return r;
- r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true);
+ r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true);
if (r < 0)
return r;
/* main_pid= */ NULL,
&s->control_pid,
/* main_pid_alien= */ false);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
goto fail;
+ }
if (r > 0) {
- r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
- if (r < 0)
+ r = swap_arm_timer(s, /* relative= */ true, s->timeout_usec);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m");
goto fail;
+ }
swap_set_state(s, state);
} else if (state == SWAP_DEACTIVATING_SIGTERM && s->kill_context.send_sigkill)
return;
fail:
- log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
}
else
r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority);
if (r < 0) {
- r = -ENOMEM;
+ r = log_oom();
goto fail;
}
}
}
r = exec_command_set(s->control_command, "/sbin/swapon", "--fixpgsz", NULL);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to initialize swapon command line: %m");
goto fail;
+ }
if (s->parameters_fragment.options || opts) {
r = exec_command_append(s->control_command, "-o",
opts ?: s->parameters_fragment.options, NULL);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to prepare swapon command line: %m");
goto fail;
+ }
}
r = exec_command_append(s->control_command, s->what, NULL);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to prepare swapon command line: %m");
goto fail;
+ }
swap_unwatch_control_pid(s);
r = swap_spawn(s, s->control_command, &s->control_pid);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'swapon' task: %m");
goto fail;
+ }
swap_set_state(s, SWAP_ACTIVATING);
return;
fail:
- log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapon' task: %m");
swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
}
"/sbin/swapoff",
s->what,
NULL);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to prepare swapoff command line: %m");
goto fail;
+ }
swap_unwatch_control_pid(s);
r = swap_spawn(s, s->control_command, &s->control_pid);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'swapoff' task: %m");
goto fail;
+ }
swap_set_state(s, SWAP_DEACTIVATING);
-
return;
fail:
- log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapoff' task: %m");
swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
}
if (k == EOF)
break;
- log_warning("Failed to parse /proc/swaps:%u.", i);
+ log_warning("Failed to parse /proc/swaps:%u, skipping.", i);
continue;
}
r = swap_load_proc_swaps(m, true);
if (r < 0) {
- log_error_errno(r, "Failed to reread /proc/swaps: %m");
-
/* Reset flags, just in case, for late calls */
LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
Swap *swap = SWAP(u);
s->control_command = NULL;
s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
- r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec));
- if (r < 0)
+ r = swap_arm_timer(s, /* relative= */ true, s->exec_context.timeout_clean_usec);
+ if (r < 0) {
+ log_unit_warning_errno(u, r, "Failed to install timer: %m");
goto fail;
+ }
r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
- if (r < 0)
+ if (r < 0) {
+ log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m");
goto fail;
+ }
swap_set_state(s, SWAP_CLEANING);
return 0;
fail:
- log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m");
s->clean_result = SWAP_FAILURE_RESOURCES;
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
return r;