systemctl: Implement --wait for kill command

TEST-26-SYSTEMCTL is racy as we call systemctl is-active immediately
after systemctl kill. Let's implement --wait for systemctl kill and
use it in TEST-26-SYSTEMCTL to avoid the race.
This commit is contained in:
Daan De Meyer
2024-05-01 09:40:32 +02:00
parent 0e7e84cbb1
commit 6be4dab095
5 changed files with 38 additions and 11 deletions
+24 -3
View File
@@ -2,11 +2,13 @@
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-wait-for-units.h"
#include "systemctl-kill.h"
#include "systemctl-util.h"
#include "systemctl.h"
int verb_kill(int argc, char *argv[], void *userdata) {
_cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL;
_cleanup_strv_free_ char **names = NULL;
const char *kill_whom;
sd_bus *bus;
@@ -16,6 +18,12 @@ int verb_kill(int argc, char *argv[], void *userdata) {
if (r < 0)
return r;
if (arg_wait) {
r = bus_wait_for_units_new(bus, &w);
if (r < 0)
return log_error_errno(r, "Failed to allocate unit watch context: %m");
}
polkit_agent_open_maybe();
kill_whom = arg_kill_whom ?: "all";
@@ -48,10 +56,23 @@ int verb_kill(int argc, char *argv[], void *userdata) {
NULL,
"ssi", *name, kill_whom, arg_signal);
if (q < 0) {
log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q));
if (r == 0)
r = q;
RET_GATHER(r, log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)));
continue;
}
if (w) {
q = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_FOR_INACTIVE|BUS_WAIT_NO_JOB, NULL, NULL);
if (q < 0)
RET_GATHER(r, log_error_errno(q, "Failed to watch unit %s: %m", *name));
}
}
if (w) {
q = bus_wait_for_units_run(w);
if (q < 0)
return log_error_errno(q, "Failed to wait for units: %m");
if (q == BUS_WAIT_FAILURE)
RET_GATHER(r, -EIO);
}
return r;