shared: introduce MachineRegistrationContext to track bus and registration state
Bundle scope, buses, and registration success booleans into a MachineRegistrationContext struct. This eliminates the reterr_registered_system and reterr_registered_user output parameters from register_machine_with_fallback_and_log() and the corresponding input parameters from unregister_machine_with_fallback_and_log(). The struct carries state from registration to unregistration so the caller no longer needs to manually thread individual booleans between the two calls. register_machine_with_fallback_and_log() goes from 7 to 3 parameters, unregister_machine_with_fallback_and_log() goes from 5 to 2. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
This commit is contained in:
committed by
Daan De Meyer
parent
2af171653f
commit
454d9c1be1
+9
-9
@@ -1754,7 +1754,7 @@ static int verify_arguments(void) {
|
||||
|
||||
if (!(arg_clone_ns_flags & CLONE_NEWPID) ||
|
||||
!(arg_clone_ns_flags & CLONE_NEWUTS)) {
|
||||
arg_register = false;
|
||||
arg_register = 0;
|
||||
if (arg_start_mode != START_PID1)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--boot cannot be used without namespacing.");
|
||||
}
|
||||
@@ -5782,7 +5782,11 @@ static int run_container(
|
||||
scope_allocated = true;
|
||||
}
|
||||
|
||||
bool registered_system = false, registered_runtime = false;
|
||||
MachineRegistrationContext machine_ctx = {
|
||||
.scope = arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? RUNTIME_SCOPE_SYSTEM : _RUNTIME_SCOPE_INVALID,
|
||||
.system_bus = system_bus,
|
||||
.user_bus = runtime_bus,
|
||||
};
|
||||
if (arg_register != 0) {
|
||||
const MachineRegistration reg = {
|
||||
.name = arg_machine,
|
||||
@@ -5795,13 +5799,9 @@ static int run_container(
|
||||
};
|
||||
|
||||
r = register_machine_with_fallback_and_log(
|
||||
arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? RUNTIME_SCOPE_SYSTEM : _RUNTIME_SCOPE_INVALID,
|
||||
system_bus,
|
||||
runtime_bus,
|
||||
&machine_ctx,
|
||||
®,
|
||||
/* graceful= */ arg_register < 0,
|
||||
®istered_system,
|
||||
®istered_runtime);
|
||||
/* graceful= */ arg_register < 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
@@ -6015,7 +6015,7 @@ static int run_container(
|
||||
r = wait_for_container(pid, &container_status);
|
||||
|
||||
/* Tell machined that we are gone. */
|
||||
(void) unregister_machine_with_fallback_and_log(system_bus, runtime_bus, arg_machine, registered_system, registered_runtime);
|
||||
unregister_machine_with_fallback_and_log(&machine_ctx, arg_machine);
|
||||
|
||||
if (r < 0)
|
||||
/* We failed to wait for the container, or the container exited abnormally. */
|
||||
|
||||
@@ -34,6 +34,7 @@ static int register_machine_dbus_ex(
|
||||
assert(reg->name);
|
||||
assert(reg->service);
|
||||
assert(reg->class);
|
||||
assert(error);
|
||||
|
||||
r = bus_message_new_method_call(bus, &m, bus_machine_mgr, "RegisterMachineEx");
|
||||
if (r < 0)
|
||||
@@ -211,85 +212,73 @@ static const char* machine_registration_scope_string(RuntimeScope scope, bool re
|
||||
}
|
||||
|
||||
int register_machine_with_fallback_and_log(
|
||||
RuntimeScope scope,
|
||||
sd_bus *system_bus,
|
||||
sd_bus *user_bus,
|
||||
MachineRegistrationContext *ctx,
|
||||
const MachineRegistration *reg,
|
||||
bool graceful,
|
||||
bool *reterr_registered_system,
|
||||
bool *reterr_registered_user) {
|
||||
bool graceful) {
|
||||
|
||||
bool registered_system = false, registered_user = false;
|
||||
int r = 0;
|
||||
|
||||
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, _RUNTIME_SCOPE_INVALID));
|
||||
assert(system_bus || !IN_SET(scope, RUNTIME_SCOPE_SYSTEM, _RUNTIME_SCOPE_INVALID));
|
||||
assert(user_bus || !IN_SET(scope, RUNTIME_SCOPE_USER, _RUNTIME_SCOPE_INVALID));
|
||||
assert(ctx);
|
||||
assert(IN_SET(ctx->scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, _RUNTIME_SCOPE_INVALID));
|
||||
assert(ctx->system_bus || !IN_SET(ctx->scope, RUNTIME_SCOPE_SYSTEM, _RUNTIME_SCOPE_INVALID));
|
||||
assert(ctx->user_bus || !IN_SET(ctx->scope, RUNTIME_SCOPE_USER, _RUNTIME_SCOPE_INVALID));
|
||||
assert(reg);
|
||||
assert(reg->name);
|
||||
assert(reg->service);
|
||||
assert(reg->class);
|
||||
assert(reterr_registered_system);
|
||||
assert(reterr_registered_user);
|
||||
|
||||
if (IN_SET(scope, RUNTIME_SCOPE_SYSTEM, _RUNTIME_SCOPE_INVALID)) {
|
||||
if (IN_SET(ctx->scope, RUNTIME_SCOPE_SYSTEM, _RUNTIME_SCOPE_INVALID)) {
|
||||
MachineRegistration system_reg = *reg;
|
||||
if (scope != RUNTIME_SCOPE_SYSTEM)
|
||||
if (ctx->scope != RUNTIME_SCOPE_SYSTEM)
|
||||
system_reg.allocate_unit = false;
|
||||
|
||||
int q = register_machine(system_bus, &system_reg, RUNTIME_SCOPE_SYSTEM);
|
||||
int q = register_machine(ctx->system_bus, &system_reg, RUNTIME_SCOPE_SYSTEM);
|
||||
if (q < 0)
|
||||
RET_GATHER(r, q);
|
||||
else
|
||||
registered_system = true;
|
||||
ctx->registered_system = true;
|
||||
}
|
||||
|
||||
if (IN_SET(scope, RUNTIME_SCOPE_USER, _RUNTIME_SCOPE_INVALID)) {
|
||||
int q = register_machine(user_bus, reg, RUNTIME_SCOPE_USER);
|
||||
if (IN_SET(ctx->scope, RUNTIME_SCOPE_USER, _RUNTIME_SCOPE_INVALID)) {
|
||||
int q = register_machine(ctx->user_bus, reg, RUNTIME_SCOPE_USER);
|
||||
if (q < 0)
|
||||
RET_GATHER(r, q);
|
||||
else
|
||||
registered_user = true;
|
||||
ctx->registered_user = true;
|
||||
}
|
||||
|
||||
if (r < 0) {
|
||||
if (graceful) {
|
||||
log_notice_errno(r, "Failed to register machine in %s context, ignoring: %m",
|
||||
machine_registration_scope_string(scope, registered_system, registered_user));
|
||||
machine_registration_scope_string(ctx->scope, ctx->registered_system, ctx->registered_user));
|
||||
r = 0;
|
||||
} else
|
||||
r = log_error_errno(r, "Failed to register machine in %s context: %m",
|
||||
machine_registration_scope_string(scope, registered_system, registered_user));
|
||||
machine_registration_scope_string(ctx->scope, ctx->registered_system, ctx->registered_user));
|
||||
}
|
||||
|
||||
if (reterr_registered_system)
|
||||
*reterr_registered_system = registered_system;
|
||||
if (reterr_registered_user)
|
||||
*reterr_registered_user = registered_user;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int unregister_machine_with_fallback_and_log(
|
||||
sd_bus *system_bus,
|
||||
sd_bus *user_bus,
|
||||
const char *machine_name,
|
||||
bool registered_system,
|
||||
bool registered_user) {
|
||||
void unregister_machine_with_fallback_and_log(
|
||||
const MachineRegistrationContext *ctx,
|
||||
const char *machine_name) {
|
||||
|
||||
int r = 0;
|
||||
bool failed_system = false, failed_user = false;
|
||||
|
||||
if (registered_system) {
|
||||
int q = unregister_machine(system_bus, machine_name, RUNTIME_SCOPE_SYSTEM);
|
||||
assert(ctx);
|
||||
|
||||
if (ctx->registered_system) {
|
||||
int q = unregister_machine(ctx->system_bus, machine_name, RUNTIME_SCOPE_SYSTEM);
|
||||
if (q < 0) {
|
||||
RET_GATHER(r, q);
|
||||
failed_system = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (registered_user) {
|
||||
int q = unregister_machine(user_bus, machine_name, RUNTIME_SCOPE_USER);
|
||||
if (ctx->registered_user) {
|
||||
int q = unregister_machine(ctx->user_bus, machine_name, RUNTIME_SCOPE_USER);
|
||||
if (q < 0) {
|
||||
RET_GATHER(r, q);
|
||||
failed_user = true;
|
||||
@@ -299,11 +288,9 @@ int unregister_machine_with_fallback_and_log(
|
||||
if (r < 0)
|
||||
log_notice_errno(r, "Failed to unregister machine in %s context, ignoring: %m",
|
||||
machine_registration_scope_string(
|
||||
registered_system && registered_user ? _RUNTIME_SCOPE_INVALID :
|
||||
registered_system ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER,
|
||||
ctx->registered_system && ctx->registered_user ? _RUNTIME_SCOPE_INVALID :
|
||||
ctx->registered_system ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER,
|
||||
!failed_system, !failed_user));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int unregister_machine(sd_bus *bus, const char *machine_name, RuntimeScope scope) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "sd-id128.h"
|
||||
|
||||
#include "runtime-scope.h"
|
||||
#include "shared-forward.h"
|
||||
|
||||
typedef struct MachineRegistration {
|
||||
@@ -19,23 +20,24 @@ typedef struct MachineRegistration {
|
||||
bool allocate_unit;
|
||||
} MachineRegistration;
|
||||
|
||||
typedef struct MachineRegistrationContext {
|
||||
RuntimeScope scope;
|
||||
sd_bus *system_bus;
|
||||
sd_bus *user_bus;
|
||||
bool registered_system;
|
||||
bool registered_user;
|
||||
} MachineRegistrationContext;
|
||||
|
||||
int register_machine(
|
||||
sd_bus *bus,
|
||||
const MachineRegistration *reg,
|
||||
RuntimeScope scope);
|
||||
int register_machine_with_fallback_and_log(
|
||||
RuntimeScope scope,
|
||||
sd_bus *system_bus,
|
||||
sd_bus *user_bus,
|
||||
MachineRegistrationContext *ctx,
|
||||
const MachineRegistration *reg,
|
||||
bool graceful,
|
||||
bool *reterr_registered_system,
|
||||
bool *reterr_registered_user);
|
||||
bool graceful);
|
||||
|
||||
int unregister_machine(sd_bus *bus, const char *machine_name, RuntimeScope scope);
|
||||
int unregister_machine_with_fallback_and_log(
|
||||
sd_bus *system_bus,
|
||||
sd_bus *user_bus,
|
||||
const char *machine_name,
|
||||
bool registered_system,
|
||||
bool registered_user);
|
||||
void unregister_machine_with_fallback_and_log(
|
||||
const MachineRegistrationContext *ctx,
|
||||
const char *machine_name);
|
||||
|
||||
@@ -3597,7 +3597,11 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
|
||||
return log_error_errno(r, "Failed to get our own unit: %m");
|
||||
}
|
||||
|
||||
bool registered_system = false, registered_runtime = false;
|
||||
MachineRegistrationContext machine_ctx = {
|
||||
.scope = arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? RUNTIME_SCOPE_SYSTEM : _RUNTIME_SCOPE_INVALID,
|
||||
.system_bus = system_bus,
|
||||
.user_bus = runtime_bus,
|
||||
};
|
||||
if (arg_register != 0) {
|
||||
char vm_address[STRLEN("vsock/") + DECIMAL_STR_MAX(unsigned)];
|
||||
xsprintf(vm_address, "vsock/%u", child_cid);
|
||||
@@ -3616,13 +3620,9 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
|
||||
};
|
||||
|
||||
r = register_machine_with_fallback_and_log(
|
||||
arg_runtime_scope == RUNTIME_SCOPE_USER ? _RUNTIME_SCOPE_INVALID : RUNTIME_SCOPE_SYSTEM,
|
||||
system_bus,
|
||||
runtime_bus,
|
||||
&machine_ctx,
|
||||
®,
|
||||
/* graceful= */ arg_register < 0,
|
||||
®istered_system,
|
||||
®istered_runtime);
|
||||
/* graceful= */ arg_register < 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
@@ -3727,7 +3727,7 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
|
||||
if (scope_allocated)
|
||||
terminate_scope(runtime_bus, arg_machine);
|
||||
|
||||
(void) unregister_machine_with_fallback_and_log(system_bus, runtime_bus, arg_machine, registered_system, registered_runtime);
|
||||
unregister_machine_with_fallback_and_log(&machine_ctx, arg_machine);
|
||||
|
||||
if (use_vsock) {
|
||||
if (exit_status == INT_MAX) {
|
||||
|
||||
Reference in New Issue
Block a user