mirror of
https://github.com/systemd/systemd.git
synced 2025-01-11 09:18:07 +03:00
Merge pull request #34769 from yuwata/machine-by-name-or-pid
machine: lookup_machine_by_name_or_pid() may return 1 on error and it is already replied
This commit is contained in:
commit
35c279478f
@ -236,6 +236,8 @@ int lookup_machine_by_name_or_pid(sd_varlink *link, Manager *manager, const char
|
||||
assert(manager);
|
||||
assert(ret_machine);
|
||||
|
||||
/* This returns 0 on success, 1 on error and it is replied, and a negative errno otherwise. */
|
||||
|
||||
if (machine_name) {
|
||||
r = lookup_machine_by_name(link, manager, machine_name, &machine);
|
||||
if (r == -EINVAL)
|
||||
@ -340,7 +342,7 @@ int vl_method_kill(sd_varlink *link, sd_json_variant *parameters, sd_varlink_met
|
||||
r = lookup_machine_by_name_or_pid(link, manager, p.machine_name, p.pid, &machine);
|
||||
if (r == -ESRCH)
|
||||
return sd_varlink_error(link, "io.systemd.Machine.NoSuchMachine", NULL);
|
||||
if (r < 0)
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
if (isempty(p.swhom))
|
||||
|
@ -448,7 +448,7 @@ static int vl_method_list(sd_varlink *link, sd_json_variant *parameters, sd_varl
|
||||
r = lookup_machine_by_name_or_pid(link, m, p.machine_name, p.pid, &machine);
|
||||
if (r == -ESRCH)
|
||||
return sd_varlink_error(link, "io.systemd.Machine.NoSuchMachine", NULL);
|
||||
if (r < 0)
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
return list_machine_one(link, machine, /* more= */ false);
|
||||
@ -496,7 +496,7 @@ static int lookup_machine_and_call_method(sd_varlink *link, sd_json_variant *par
|
||||
r = lookup_machine_by_name_or_pid(link, manager, p.machine_name, p.pid, &machine);
|
||||
if (r == -ESRCH)
|
||||
return sd_varlink_error(link, "io.systemd.Machine.NoSuchMachine", NULL);
|
||||
if (r < 0)
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
return method(link, parameters, flags, machine);
|
||||
@ -561,7 +561,8 @@ static int vl_method_list_images(sd_varlink *link, sd_json_variant *parameters,
|
||||
struct params {
|
||||
const char *image_name;
|
||||
bool acquire_metadata;
|
||||
};
|
||||
} p = {};
|
||||
int r;
|
||||
|
||||
static const sd_json_dispatch_field dispatch_table[] = {
|
||||
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct params, image_name), 0 },
|
||||
@ -570,11 +571,6 @@ static int vl_method_list_images(sd_varlink *link, sd_json_variant *parameters,
|
||||
{}
|
||||
};
|
||||
|
||||
_cleanup_hashmap_free_ Hashmap *images = NULL;
|
||||
struct params p = {};
|
||||
Image *image;
|
||||
int r;
|
||||
|
||||
assert(link);
|
||||
assert(parameters);
|
||||
|
||||
@ -583,22 +579,24 @@ static int vl_method_list_images(sd_varlink *link, sd_json_variant *parameters,
|
||||
return r;
|
||||
|
||||
if (p.image_name) {
|
||||
_cleanup_(image_unrefp) Image *found = NULL;
|
||||
|
||||
if (!image_name_is_valid(p.image_name))
|
||||
return sd_varlink_error_invalid_parameter_name(link, "name");
|
||||
|
||||
r = image_find(IMAGE_MACHINE, p.image_name, /* root = */ NULL, &image);
|
||||
r = image_find(IMAGE_MACHINE, p.image_name, /* root = */ NULL, &found);
|
||||
if (r == -ENOENT)
|
||||
return sd_varlink_error(link, "io.systemd.MachineImage.NoSuchImage", NULL);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to find image: %m");
|
||||
|
||||
return list_image_one_and_maybe_read_metadata(link, image, /* more = */ false, p.acquire_metadata);
|
||||
return list_image_one_and_maybe_read_metadata(link, found, /* more = */ false, p.acquire_metadata);
|
||||
}
|
||||
|
||||
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE))
|
||||
return sd_varlink_error(link, SD_VARLINK_ERROR_EXPECTED_MORE, NULL);
|
||||
|
||||
images = hashmap_new(&image_hash_ops);
|
||||
_cleanup_hashmap_free_ Hashmap *images = hashmap_new(&image_hash_ops);
|
||||
if (!images)
|
||||
return -ENOMEM;
|
||||
|
||||
@ -606,7 +604,7 @@ static int vl_method_list_images(sd_varlink *link, sd_json_variant *parameters,
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to discover images: %m");
|
||||
|
||||
Image *previous = NULL;
|
||||
Image *image, *previous = NULL;
|
||||
HASHMAP_FOREACH(image, images) {
|
||||
if (previous) {
|
||||
r = list_image_one_and_maybe_read_metadata(link, previous, /* more = */ true, p.acquire_metadata);
|
||||
|
@ -12,7 +12,7 @@ export PAGER=
|
||||
at_exit() {
|
||||
set +e
|
||||
|
||||
machinectl status long-running >/dev/null && machinectl kill --signal=KILL long-running
|
||||
machinectl status long-running &>/dev/null && machinectl kill --signal=KILL long-running
|
||||
mountpoint -q /var/lib/machines && timeout 10 sh -c "until umount /var/lib/machines; do sleep .5; done"
|
||||
[[ -n "${NSPAWN_FRAGMENT:-}" ]] && rm -f "/etc/systemd/nspawn/$NSPAWN_FRAGMENT" "/var/lib/machines/$NSPAWN_FRAGMENT"
|
||||
rm -f /run/systemd/nspawn/*.nspawn
|
||||
@ -60,11 +60,13 @@ EOF
|
||||
|
||||
long_running_machine_start() {
|
||||
# shellcheck disable=SC2015
|
||||
machinectl status long-running >/dev/null && return 0 || true
|
||||
machinectl status long-running &>/dev/null && return 0 || true
|
||||
|
||||
# Ensure the service stopped.
|
||||
systemctl stop systemd-nspawn@long-running.service 2>/dev/null || :
|
||||
|
||||
rm -f /var/lib/machines/long-running/ready
|
||||
# sometime `machinectl start` returns 1 and then do a success
|
||||
machinectl start long-running || machinectl start long-running
|
||||
machinectl start long-running
|
||||
# !!!! DO NOT REMOVE THIS TEST
|
||||
# The test makes sure that the long-running's init script has enough time to start and registered signal traps
|
||||
timeout 30 bash -c "until test -e /var/lib/machines/long-running/ready; do sleep .5; done"
|
||||
@ -120,7 +122,7 @@ timeout 10 bash -c "until test -e /var/lib/machines/long-running/reboot; do slee
|
||||
rm -f /var/lib/machines/long-running/terminate
|
||||
machinectl terminate long-running
|
||||
timeout 10 bash -c "until test -e /var/lib/machines/long-running/terminate; do sleep .5; done"
|
||||
timeout 10 bash -c "while machinectl status long-running; do sleep .5; done"
|
||||
timeout 10 bash -c "while machinectl status long-running &>/dev/null; do sleep .5; done"
|
||||
# Restart container
|
||||
long_running_machine_start
|
||||
# Test for 'machinectl kill'
|
||||
@ -261,11 +263,11 @@ varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List
|
||||
|
||||
pid=$(varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":"long-running"}' | jq '.leader.pid')
|
||||
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":"long-running"}' >/tmp/expected
|
||||
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List "{\"pid\":$pid}" >/tmp/got
|
||||
diff -u /tmp/expected /tmp/got
|
||||
|
||||
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List "{\"name\":\"long-running\", \"pid\":$pid}"
|
||||
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List "{\"pid\":$pid}" | diff /tmp/expected -
|
||||
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List "{\"name\":\"long-running\", \"pid\":$pid}" | diff /tmp/expected -
|
||||
(! varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List "{\"name\":\"non-existent\", \"pid\":$pid}")
|
||||
(! varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":""}')
|
||||
(! varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{"name":"ah@??.hmm"}')
|
||||
|
||||
# test io.systemd.Machine.Kill
|
||||
# sending TRAP signal
|
||||
@ -273,10 +275,6 @@ rm -f /var/lib/machines/long-running/trap
|
||||
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.Kill '{"name":"long-running", "whom": "leader", "signal": 5}'
|
||||
timeout 30 bash -c "until test -e /var/lib/machines/long-running/trap; do sleep .5; done"
|
||||
|
||||
# sending KILL signal
|
||||
varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.Kill '{"name":"long-running", "signal": 9}'
|
||||
timeout 30 bash -c "while varlinkctl call /run/systemd/machine/io.systemd.Machine io.systemd.Machine.List '{\"name\":\"long-running\"}'; do sleep 0.5; done"
|
||||
|
||||
# test io.systemd.Machine.Terminate
|
||||
long_running_machine_start
|
||||
rm -f /var/lib/machines/long-running/terminate
|
||||
|
Loading…
Reference in New Issue
Block a user