summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-12-22 19:45:32 +0100
committerLennart Poettering <lennart@poettering.net>2014-12-23 03:26:24 +0100
commit9b15b7846d4de01bb5d9700a24077787e984e8ab (patch)
treee42326cf2ae7f03ca72b7f25e0ec738287cc1f46 /src/shared
parent91f4347ef7bde17418b365ed3a97a752fe65bd50 (diff)
run: add a new "-t" mode for invoking a binary on an allocated TTY
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/ptyfwd.c33
-rw-r--r--src/shared/ptyfwd.h4
2 files changed, 32 insertions, 5 deletions
diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c
index 085d374ed8..11356e2def 100644
--- a/src/shared/ptyfwd.c
+++ b/src/shared/ptyfwd.c
@@ -53,6 +53,11 @@ struct PTYForward {
bool master_writable:1;
bool master_hangup:1;
+ bool repeat:1;
+
+ bool last_char_set:1;
+ char last_char;
+
char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
size_t in_buffer_full, out_buffer_full;
@@ -169,11 +174,12 @@ static int shovel(PTYForward *f) {
* might be cause by vhangup() or
* temporary closing of everything on
* the other side, we treat it like
- * EAGAIN here and try again. */
+ * EAGAIN here and try again, unless
+ * repeat is off. */
- if (errno == EAGAIN || errno == EIO)
+ if (errno == EAGAIN || (errno == EIO && f->repeat))
f->master_readable = false;
- else if (errno == EPIPE || errno == ECONNRESET) {
+ else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
f->master_readable = f->master_writable = false;
f->master_hangup = true;
@@ -203,6 +209,12 @@ static int shovel(PTYForward *f) {
}
} else {
+
+ if (k > 0) {
+ f->last_char = f->out_buffer[k-1];
+ f->last_char_set = true;
+ }
+
assert(f->out_buffer_full >= (size_t) k);
memmove(f->out_buffer, f->out_buffer + k, f->out_buffer_full - k);
f->out_buffer_full -= k;
@@ -285,7 +297,7 @@ static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *
return 0;
}
-int pty_forward_new(sd_event *event, int master, PTYForward **ret) {
+int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) {
_cleanup_(pty_forward_freep) PTYForward *f = NULL;
struct winsize ws;
int r;
@@ -294,6 +306,8 @@ int pty_forward_new(sd_event *event, int master, PTYForward **ret) {
if (!f)
return -ENOMEM;
+ f->repeat = repeat;
+
if (event)
f->event = sd_event_ref(event);
else {
@@ -388,3 +402,14 @@ PTYForward *pty_forward_free(PTYForward *f) {
return NULL;
}
+
+int pty_forward_last_char(PTYForward *f, char *ch) {
+ assert(f);
+ assert(ch);
+
+ if (!f->last_char_set)
+ return -ENXIO;
+
+ *ch = f->last_char;
+ return 0;
+}
diff --git a/src/shared/ptyfwd.h b/src/shared/ptyfwd.h
index 5a612fd597..d7b658e181 100644
--- a/src/shared/ptyfwd.h
+++ b/src/shared/ptyfwd.h
@@ -28,7 +28,9 @@
typedef struct PTYForward PTYForward;
-int pty_forward_new(sd_event *event, int master, PTYForward **f);
+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);
+
DEFINE_TRIVIAL_CLEANUP_FUNC(PTYForward*, pty_forward_free);