X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fptyfwd.c;h=789f217efc88b4eb8fb8afd4dc7f59512247f5c0;hb=d896ac2d2fbce41a0b11a0618a685adeaf18b8fe;hp=db2445ceab20a3780aac2d18f627c4ad9d4538f1;hpb=0ec5543c4c0318552a4dcdd83210793347b93081;p=elogind.git diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c index db2445cea..789f217ef 100644 --- a/src/shared/ptyfwd.c +++ b/src/shared/ptyfwd.c @@ -20,7 +20,6 @@ ***/ #include -#include #include #include #include @@ -42,6 +41,8 @@ struct PTYForward { struct termios saved_stdin_attr; struct termios saved_stdout_attr; + bool read_only:1; + bool saved_stdin:1; bool saved_stdout:1; @@ -52,9 +53,9 @@ struct PTYForward { bool master_readable:1; bool master_writable:1; bool master_hangup:1; - bool master_suppressed_hangup:1; - bool repeat:1; + /* Continue reading after hangup? */ + bool ignore_vhangup:1; bool last_char_set:1; char last_char; @@ -171,22 +172,16 @@ 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) { - 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. */ + /* Note that EIO on the master device + * might be caused by vhangup() or + * temporary closing of everything on + * the other side, we treat it like + * EAGAIN here and try again, unless + * ignore_vhangup is off. */ + if (errno == EAGAIN || (errno == EIO && f->ignore_vhangup)) f->master_readable = false; - f->master_suppressed_hangup = true; - - } else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) { + else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) { f->master_readable = f->master_writable = false; f->master_hangup = true; @@ -250,8 +245,6 @@ 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; @@ -301,12 +294,18 @@ static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo * /* The window size changed, let's forward that. */ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0) - (void)ioctl(f->master, TIOCSWINSZ, &ws); + (void) ioctl(f->master, TIOCSWINSZ, &ws); return 0; } -int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) { +int pty_forward_new( + sd_event *event, + int master, + bool ignore_vhangup, + bool read_only, + PTYForward **ret) { + _cleanup_(pty_forward_freep) PTYForward *f = NULL; struct winsize ws; int r; @@ -315,7 +314,8 @@ int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) if (!f) return -ENOMEM; - f->repeat = repeat; + f->read_only = read_only; + f->ignore_vhangup = ignore_vhangup; if (event) f->event = sd_event_ref(event); @@ -325,13 +325,15 @@ int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) return r; } - r = fd_nonblock(STDIN_FILENO, true); - if (r < 0) - return r; + if (!read_only) { + r = fd_nonblock(STDIN_FILENO, true); + if (r < 0) + return r; - r = fd_nonblock(STDOUT_FILENO, true); - if (r < 0) - return r; + r = fd_nonblock(STDOUT_FILENO, true); + if (r < 0) + return r; + } r = fd_nonblock(master, true); if (r < 0) @@ -340,38 +342,36 @@ int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) f->master = master; if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0) - (void)ioctl(master, TIOCSWINSZ, &ws); + (void) ioctl(master, TIOCSWINSZ, &ws); - if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) { - struct termios raw_stdin_attr; + if (!read_only) { + if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) { + struct termios raw_stdin_attr; - f->saved_stdin = true; + f->saved_stdin = true; - raw_stdin_attr = f->saved_stdin_attr; - cfmakeraw(&raw_stdin_attr); - raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag; - tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr); - } - - if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) { - struct termios raw_stdout_attr; + raw_stdin_attr = f->saved_stdin_attr; + cfmakeraw(&raw_stdin_attr); + raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag; + tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr); + } - f->saved_stdout = true; + if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) { + struct termios raw_stdout_attr; - raw_stdout_attr = f->saved_stdout_attr; - cfmakeraw(&raw_stdout_attr); - raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag; - raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag; - tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr); - } + f->saved_stdout = true; - r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f); - if (r < 0) - return r; + raw_stdout_attr = f->saved_stdout_attr; + cfmakeraw(&raw_stdout_attr); + raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag; + raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag; + tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr); + } - r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f); - if (r < 0 && r != -EPERM) - return r; + r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f); + if (r < 0 && r != -EPERM) + return r; + } r = sd_event_add_io(f->event, &f->stdout_event_source, STDOUT_FILENO, EPOLLOUT|EPOLLET, on_stdout_event, f); if (r == -EPERM) @@ -380,7 +380,13 @@ int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) else if (r < 0) return r; + r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f); + if (r < 0) + return r; + r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f); + if (r < 0) + return r; *ret = f; f = NULL; @@ -423,23 +429,19 @@ int pty_forward_get_last_char(PTYForward *f, char *ch) { return 0; } -int pty_forward_set_repeat(PTYForward *f, bool repeat) { +int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup) { int r; assert(f); - if (f->repeat == repeat) + if (f->ignore_vhangup == ignore_vhangup) 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) { + f->ignore_vhangup = ignore_vhangup; + if (!f->ignore_vhangup) { - /* Let's try to read again from the master fd, and if - * it is this will now cause termination of the - * session. */ + /* We shall now react to vhangup()s? Let's check + * immediately if we might be in one */ f->master_readable = true; r = shovel(f); @@ -450,8 +452,8 @@ int pty_forward_set_repeat(PTYForward *f, bool repeat) { return 0; } -int pty_forward_get_repeat(PTYForward *f) { +int pty_forward_get_ignore_vhangup(PTYForward *f) { assert(f); - return f->repeat; + return f->ignore_vhangup; }