/***
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 <errno.h>
#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "sd-event.h"
#include "alloc-util.h"
#include "fd-util.h"
#include "log.h"
#include "macro.h"
#include "ptyfwd.h"
#include "time-util.h"
struct PTYForward {
int master;
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;
}
assert(f);
if (f->flags & PTY_FORWARD_IGNORE_VHANGUP)
return true;
return true;
return false;
}
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->read_from_master = true;
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,
PTYForward **ret) {
int r;
if (!f)
return -ENOMEM;
if (event)
else {
r = sd_event_default(&f->event);
if (r < 0)
return r;
}
if (!(flags & PTY_FORWARD_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 (!(flags & PTY_FORWARD_READ_ONLY)) {
f->saved_stdin = true;
}
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->flags & PTY_FORWARD_IGNORE_VHANGUP) == b)
return 0;
if (b)
f->flags |= PTY_FORWARD_IGNORE_VHANGUP;
else
f->flags &= ~PTY_FORWARD_IGNORE_VHANGUP;
if (!ignore_vhangup(f)) {
/* 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;
}
assert(f);
return !!(f->flags & PTY_FORWARD_IGNORE_VHANGUP);
}