summaryrefslogtreecommitdiffstats
path: root/src/machine
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-12-18 05:35:54 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-12-18 12:28:28 +0100
commit64b504bde3f9e29fa66cbfb4bb3c0608e94fba77 (patch)
tree52fcda0ce416490dc09ef96980f217007514d1a3 /src/machine
parentptyfwd: always flush buffer and disconnect before exit (diff)
downloadsystemd-64b504bde3f9e29fa66cbfb4bb3c0608e94fba77.tar.xz
systemd-64b504bde3f9e29fa66cbfb4bb3c0608e94fba77.zip
machinectl: explicitly assign PTY forwarder to sd_bus_slot
No functional change, just refactoring.
Diffstat (limited to 'src/machine')
-rw-r--r--src/machine/machinectl.c43
1 files changed, 20 insertions, 23 deletions
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 97a9129ccd..39d3f3aed8 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -1188,34 +1188,29 @@ static int bind_mount(int argc, char *argv[], void *userdata) {
}
static int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
- PTYForward ** forward = (PTYForward**) userdata;
+ PTYForward *forward = ASSERT_PTR(userdata);
int r;
assert(m);
- assert(forward);
- if (*forward) {
- /* If the forwarder is already initialized, tell it to
- * exit on the next vhangup(), so that we still flush
- * out what might be queued and exit then. */
-
- r = pty_forward_set_ignore_vhangup(*forward, false);
- if (r >= 0)
- return 0;
+ /* Tell the forwarder to exit on the next vhangup(), so that we still flush out what might be queued
+ * and exit then. */
+ r = pty_forward_set_ignore_vhangup(forward, false);
+ if (r < 0) {
+ /* On error, quit immediately. */
log_error_errno(r, "Failed to set ignore_vhangup flag: %m");
+ (void) sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE);
}
- /* On error, or when the forwarder is not initialized yet, quit immediately */
- sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE);
return 0;
}
-static int process_forward(sd_event *event, PTYForward **forward, int master, PTYForwardFlags flags, const char *name) {
- bool machine_died;
+static int process_forward(sd_event *event, sd_bus_slot *machine_removed_slot, int master, PTYForwardFlags flags, const char *name) {
int r;
assert(event);
+ assert(machine_removed_slot);
assert(master >= 0);
assert(name);
@@ -1230,17 +1225,21 @@ static int process_forward(sd_event *event, PTYForward **forward, int master, PT
if (r < 0)
return log_error_errno(r, "Failed to enable SIGINT/SITERM handling: %m");
- r = pty_forward_new(event, master, flags, forward);
+ _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
+ r = pty_forward_new(event, master, flags, &forward);
if (r < 0)
return log_error_errno(r, "Failed to create PTY forwarder: %m");
+ /* No userdata should not set previously. */
+ assert_se(!sd_bus_slot_set_userdata(machine_removed_slot, forward));
+
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
- machine_died =
+ bool machine_died =
(flags & PTY_FORWARD_IGNORE_VHANGUP) &&
- pty_forward_get_ignore_vhangup(*forward) == 0;
+ pty_forward_get_ignore_vhangup(forward) == 0;
if (!arg_quiet) {
if (machine_died)
@@ -1292,7 +1291,6 @@ static int parse_machine_uid(const char *spec, const char **machine, char **uid)
static int login_machine(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
_cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
int master = -1, r;
@@ -1326,7 +1324,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
"member='MachineRemoved',"
"arg0='", machine, "'");
- r = sd_bus_add_match_async(bus, &slot, match, on_machine_removed, NULL, &forward);
+ r = sd_bus_add_match_async(bus, &slot, match, on_machine_removed, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to request machine removal match: %m");
@@ -1338,13 +1336,12 @@ static int login_machine(int argc, char *argv[], void *userdata) {
if (r < 0)
return bus_log_parse_error(r);
- return process_forward(event, &forward, master, PTY_FORWARD_IGNORE_VHANGUP, machine);
+ return process_forward(event, slot, master, PTY_FORWARD_IGNORE_VHANGUP, machine);
}
static int shell_machine(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
_cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
int master = -1, r;
@@ -1388,7 +1385,7 @@ static int shell_machine(int argc, char *argv[], void *userdata) {
"member='MachineRemoved',"
"arg0='", machine, "'");
- r = sd_bus_add_match_async(bus, &slot, match, on_machine_removed, NULL, &forward);
+ r = sd_bus_add_match_async(bus, &slot, match, on_machine_removed, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to request machine removal match: %m");
@@ -1418,7 +1415,7 @@ static int shell_machine(int argc, char *argv[], void *userdata) {
if (r < 0)
return bus_log_parse_error(r);
- return process_forward(event, &forward, master, 0, machine);
+ return process_forward(event, slot, master, /* flags = */ 0, machine);
}
static int normalize_nspawn_filename(const char *name, char **ret_file) {