summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-01-07 03:08:00 +0100
committerLennart Poettering <lennart@poettering.net>2015-01-07 03:08:00 +0100
commit0ec5543c4c0318552a4dcdd83210793347b93081 (patch)
tree9de7421842bd9d1e274ae5943cab1f11dcc7cc86
parentf7ad54a301e4ae8dceab54d3ab3934e56c1134ea (diff)
machinectl: make sure that "machinectl login" exits immediately when the machine it is connected to dies
-rw-r--r--src/machine/machinectl.c51
-rw-r--r--src/nspawn/nspawn.c2
-rw-r--r--src/run/run.c2
-rw-r--r--src/shared/ptyfwd.c60
-rw-r--r--src/shared/ptyfwd.h5
5 files changed, 104 insertions, 16 deletions
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 11f2273e4..7e995cab6 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -1230,16 +1230,42 @@ finish:
return r;
}
+static int on_machine_removed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
+ PTYForward ** forward = (PTYForward**) userdata;
+ int r;
+
+ assert(bus);
+ assert(m);
+ assert(forward);
+
+ if (*forward) {
+ /* If the forwarder is already initialized, tell it to
+ * exit on the next hangup */
+
+ r = pty_forward_set_repeat(*forward, false);
+ if (r >= 0)
+ return 0;
+
+ log_error_errno(r, "Failed to set repeat flag: %m");
+ }
+
+ /* On error, or when the forwarder is not initialized yet, quit immediately */
+ sd_event_exit(sd_bus_get_event(bus), EXIT_FAILURE);
+ return 0;
+}
+
static int login_machine(int argc, char *argv[], void *userdata) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ _cleanup_bus_slot_unref_ sd_bus_slot *slot = NULL;
_cleanup_(pty_forward_freep) PTYForward *forward = NULL;
_cleanup_event_unref_ sd_event *event = NULL;
int master = -1, r, ret = 0;
sd_bus *bus = userdata;
- const char *pty;
+ const char *pty, *match;
sigset_t mask;
char last_char = 0;
+ bool machine_died;
assert(bus);
@@ -1257,6 +1283,19 @@ static int login_machine(int argc, char *argv[], void *userdata) {
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
+ match = strappenda("type='signal',"
+ "sender='org.freedesktop.machine1',"
+ "path='/org/freedesktop/machine1',",
+ "interface='org.freedesktop.machine1.Manager',"
+ "member='MachineRemoved',"
+ "arg0='",
+ argv[1],
+ "'");
+
+ r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
+ if (r < 0)
+ return log_error_errno(r, "Failed to add machine removal match: %m");
+
r = sd_bus_message_new_method_call(bus,
&m,
"org.freedesktop.machine1",
@@ -1288,7 +1327,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
sigset_add_many(&mask, SIGWINCH, SIGTERM, SIGINT, -1);
assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
- log_info("Connected to container %s. Press ^] three times within 1s to exit session.", argv[1]);
+ log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", argv[1]);
sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
@@ -1301,14 +1340,18 @@ static int login_machine(int argc, char *argv[], void *userdata) {
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
- pty_forward_last_char(forward, &last_char);
+ pty_forward_get_last_char(forward, &last_char);
+ machine_died = pty_forward_get_repeat(forward) == 0;
forward = pty_forward_free(forward);
if (last_char != '\n')
fputc('\n', stdout);
- log_info("Connection to container %s terminated.", argv[1]);
+ if (machine_died)
+ log_info("Machine %s terminated.", argv[1]);
+ else
+ log_info("Connection to machine %s terminated.", argv[1]);
sd_event_get_exit_code(event, &ret);
return ret;
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 1ac0a7008..89bbf2b82 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -3668,7 +3668,7 @@ int main(int argc, char *argv[]) {
goto finish;
}
- pty_forward_last_char(forward, &last_char);
+ pty_forward_get_last_char(forward, &last_char);
forward = pty_forward_free(forward);
diff --git a/src/run/run.c b/src/run/run.c
index 22abb0ebb..d6d0b42d1 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -789,7 +789,7 @@ static int start_transient_service(
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
- pty_forward_last_char(forward, &last_char);
+ pty_forward_get_last_char(forward, &last_char);
forward = pty_forward_free(forward);
diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c
index 11356e2de..db2445cea 100644
--- a/src/shared/ptyfwd.c
+++ b/src/shared/ptyfwd.c
@@ -52,6 +52,7 @@ struct PTYForward {
bool master_readable:1;
bool master_writable:1;
bool master_hangup:1;
+ bool master_suppressed_hangup:1;
bool repeat:1;
@@ -170,16 +171,22 @@ static int shovel(PTYForward *f) {
k = read(f->master, f->out_buffer + f->out_buffer_full, LINE_MAX - f->out_buffer_full);
if (k < 0) {
- /* Note that EIO on the master device
- * might be cause by vhangup() or
- * temporary closing of everything on
- * the other side, we treat it like
- * EAGAIN here and try again, unless
- * repeat is off. */
+ if (errno == EAGAIN)
+ f->master_readable = false;
+
+ else if (errno == EIO && f->repeat) {
+
+ /* Note that EIO on the master device
+ * might be cause by vhangup() or
+ * temporary closing of everything on
+ * the other side, we treat it like
+ * EAGAIN here and try again, unless
+ * repeat is off. */
- if (errno == EAGAIN || (errno == EIO && f->repeat))
f->master_readable = false;
- else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
+ f->master_suppressed_hangup = true;
+
+ } else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
f->master_readable = f->master_writable = false;
f->master_hangup = true;
@@ -243,6 +250,8 @@ static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *u
assert(fd >= 0);
assert(fd == f->master);
+ f->master_suppressed_hangup = false;
+
if (revents & (EPOLLIN|EPOLLHUP))
f->master_readable = true;
@@ -403,7 +412,7 @@ PTYForward *pty_forward_free(PTYForward *f) {
return NULL;
}
-int pty_forward_last_char(PTYForward *f, char *ch) {
+int pty_forward_get_last_char(PTYForward *f, char *ch) {
assert(f);
assert(ch);
@@ -413,3 +422,36 @@ int pty_forward_last_char(PTYForward *f, char *ch) {
*ch = f->last_char;
return 0;
}
+
+int pty_forward_set_repeat(PTYForward *f, bool repeat) {
+ int r;
+
+ assert(f);
+
+ if (f->repeat == repeat)
+ return 0;
+
+ f->repeat = repeat;
+
+ /* Are we currently in a suppress hangup phase? If so, let's
+ * immediately terminate things */
+ if (!f->repeat && f->master_suppressed_hangup) {
+
+ /* Let's try to read again from the master fd, and if
+ * it is this will now cause termination of the
+ * session. */
+
+ f->master_readable = true;
+ r = shovel(f);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+int pty_forward_get_repeat(PTYForward *f) {
+ assert(f);
+
+ return f->repeat;
+}
diff --git a/src/shared/ptyfwd.h b/src/shared/ptyfwd.h
index d7b658e18..d557dee9d 100644
--- a/src/shared/ptyfwd.h
+++ b/src/shared/ptyfwd.h
@@ -31,6 +31,9 @@ typedef struct PTYForward PTYForward;
int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **f);
PTYForward *pty_forward_free(PTYForward *f);
-int pty_forward_last_char(PTYForward *f, char *ch);
+int pty_forward_get_last_char(PTYForward *f, char *ch);
+
+int pty_forward_set_repeat(PTYForward *f, bool repeat);
+int pty_forward_get_repeat(PTYForward *f);
DEFINE_TRIVIAL_CLEANUP_FUNC(PTYForward*, pty_forward_free);