ptyfwd.c revision dc75168823540076b354135f6e2de7a9a978fbca
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010-2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <limits.h>
#include <termios.h>
#include "util.h"
#include "ptyfwd.h"
struct PTYForward {
int master;
struct termios saved_stdin_attr;
struct termios saved_stdout_attr;
bool read_only:1;
bool saved_stdin:1;
bool saved_stdout:1;
bool stdin_readable:1;
bool stdin_hangup:1;
bool stdout_writable:1;
bool stdout_hangup:1;
bool master_readable:1;
bool master_writable:1;
bool master_hangup:1;
/* Continue reading after hangup? */
bool ignore_vhangup:1;
bool last_char_set:1;
char last_char;
unsigned escape_counter;
};
const char *p;
assert(f);
assert(n > 0);
/* Check for ^] */
if (*p == 0x1D) {
f->escape_timestamp = nw;
f->escape_counter = 1;
} else {
(f->escape_counter)++;
if (f->escape_counter >= 3)
return true;
}
} else {
f->escape_timestamp = 0;
f->escape_counter = 0;
}
}
return false;
}
static int shovel(PTYForward *f) {
ssize_t k;
assert(f);
while ((f->stdin_readable && f->in_buffer_full <= 0) ||
(f->master_writable && f->in_buffer_full > 0) ||
(f->master_readable && f->out_buffer_full <= 0) ||
(f->stdout_writable && f->out_buffer_full > 0)) {
if (k < 0) {
f->stdin_readable = false;
f->stdin_readable = false;
f->stdin_hangup = true;
} else {
}
} else if (k == 0) {
/* EOF on stdin */
f->stdin_readable = false;
f->stdin_hangup = true;
} else {
/* Check if ^] has been
* pressed three times within
* one second. If we get this
* we quite immediately. */
f->in_buffer_full += (size_t) k;
}
}
if (f->master_writable && f->in_buffer_full > 0) {
if (k < 0) {
f->master_writable = false;
f->master_writable = f->master_readable = false;
f->master_hangup = true;
} else {
}
} else {
f->in_buffer_full -= k;
}
}
if (k < 0) {
/* 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. */
f->master_readable = false;
f->master_readable = f->master_writable = false;
f->master_hangup = true;
} else {
}
} else
f->out_buffer_full += (size_t) k;
}
if (f->stdout_writable && f->out_buffer_full > 0) {
if (k < 0) {
f->stdout_writable = false;
f->stdout_writable = false;
f->stdout_hangup = true;
} else {
}
} else {
if (k > 0) {
f->last_char_set = true;
}
f->out_buffer_full -= k;
}
}
}
/* Exit the loop if any side hung up and if there's
* nothing more to write or nothing we could write. */
if ((f->out_buffer_full <= 0 || f->stdout_hangup) &&
(f->in_buffer_full <= 0 || f->master_hangup))
}
return 0;
}
PTYForward *f = userdata;
assert(f);
assert(e);
assert(e == f->master_event_source);
f->master_readable = true;
f->master_writable = true;
return shovel(f);
}
PTYForward *f = userdata;
assert(f);
assert(e);
assert(e == f->stdin_event_source);
f->stdin_readable = true;
return shovel(f);
}
PTYForward *f = userdata;
assert(f);
assert(e);
assert(e == f->stdout_event_source);
f->stdout_writable = true;
return shovel(f);
}
static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *si, void *userdata) {
PTYForward *f = userdata;
assert(f);
assert(e);
assert(e == f->sigwinch_event_source);
/* The window size changed, let's forward that. */
return 0;
}
int pty_forward_new(
int master,
bool ignore_vhangup,
bool read_only,
PTYForward **ret) {
int r;
if (!f)
return -ENOMEM;
f->ignore_vhangup = ignore_vhangup;
if (event)
else {
r = sd_event_default(&f->event);
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(master, true);
if (r < 0)
return r;
if (!read_only) {
struct termios raw_stdin_attr;
f->saved_stdin = true;
}
struct termios raw_stdout_attr;
f->saved_stdout = true;
}
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)
/* stdout without epoll support. Likely redirected to regular file. */
f->stdout_writable = true;
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;
if (r < 0)
return r;
*ret = f;
f = NULL;
return 0;
}
if (f) {
sd_event_unref(f->event);
if (f->saved_stdout)
if (f->saved_stdin)
free(f);
}
* unconditionally reset it */
fd_nonblock(STDIN_FILENO, false);
fd_nonblock(STDOUT_FILENO, false);
return NULL;
}
assert(f);
if (!f->last_char_set)
return -ENXIO;
return 0;
}
int r;
assert(f);
if (f->ignore_vhangup == ignore_vhangup)
return 0;
f->ignore_vhangup = ignore_vhangup;
if (!f->ignore_vhangup) {
/* We shall now react to vhangup()s? Let's check
* immediately if we might be in one */
f->master_readable = true;
r = shovel(f);
if (r < 0)
return r;
}
return 0;
}
int pty_forward_get_ignore_vhangup(PTYForward *f) {
assert(f);
return f->ignore_vhangup;
}