Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flushing channel data when pty was allocated #85

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions common-channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ static int writechannel(struct Channel* channel, int fd, circbuffer *cbuf,
const unsigned char *moredata, unsigned int *morelen);
static void send_msg_channel_window_adjust(const struct Channel *channel,
unsigned int incr);
static void send_msg_channel_data(struct Channel *channel, int isextended);
static int send_msg_channel_data(struct Channel *channel, int isextended);
static int flush_msg_channel_data(struct Channel *channel, int isextended);
static void send_msg_channel_eof(struct Channel *channel);
static void send_msg_channel_close(struct Channel *channel);
static void remove_channel(struct Channel *channel);
Expand Down Expand Up @@ -276,6 +277,7 @@ static unsigned int write_pending(const struct Channel * channel) {
/* EOF/close handling */
static void check_close(struct Channel *channel) {
int close_allowed = 0;
int flush_done = 0;

TRACE2(("check_close: writefd %d, readfd %d, errfd %d, sent_close %d, recv_close %d",
channel->writefd, channel->readfd,
Expand Down Expand Up @@ -311,28 +313,34 @@ static void check_close(struct Channel *channel) {
return;
}

if ((channel->recv_eof && !write_pending(channel))
/* have a server "session" and child has exited */
|| (channel->type->check_close && close_allowed)) {
close_chan_fd(channel, channel->writefd, SHUT_WR);
}

/* Special handling for flushing read data after an exit. We
read regardless of whether the select FD was set,
and if there isn't data available, the channel will get closed. */
if (channel->flushing) {
flush_done = 1;
TRACE(("might send data, flushing"))
if (channel->readfd >= 0 && channel->transwindow > 0) {
if (channel->readfd >= 0) {
TRACE(("send data readfd"))
send_msg_channel_data(channel, 0);
if (!flush_msg_channel_data(channel, 0)) {
flush_done = 0;
}
}
if (ERRFD_IS_READ(channel) && channel->errfd >= 0
&& channel->transwindow > 0) {
if (ERRFD_IS_READ(channel) && channel->errfd >= 0) {
TRACE(("send data errfd"))
send_msg_channel_data(channel, 1);
if (!flush_msg_channel_data(channel, 1)) {
flush_done = 0;
}
}
}

if ((channel->recv_eof && !write_pending(channel))
/* have a server "session" and child has exited */
|| (channel->type->check_close && close_allowed &&
(channel->readfd == channel->writefd &&
(!channel->flushing || flush_done)))) {
close_chan_fd(channel, channel->writefd, SHUT_WR);
}

/* If we're not going to send any more data, send EOF */
if (!channel->sent_eof
&& channel->readfd == FD_CLOSED
Expand Down Expand Up @@ -705,7 +713,7 @@ void recv_msg_channel_request() {
* chan is the remote channel, isextended is 0 if it is normal data, 1
* if it is extended data. if it is extended, then the type is in
* exttype */
static void send_msg_channel_data(struct Channel *channel, int isextended) {
static int send_msg_channel_data(struct Channel *channel, int isextended) {

int len;
size_t maxlen, size_pos;
Expand All @@ -732,7 +740,7 @@ static void send_msg_channel_data(struct Channel *channel, int isextended) {
TRACE(("maxlen %zd", maxlen))
if (maxlen == 0) {
TRACE(("leave send_msg_channel_data: no window"))
return;
return 0;
}

buf_putbyte(ses.writepayload,
Expand All @@ -759,15 +767,15 @@ static void send_msg_channel_data(struct Channel *channel, int isextended) {
buf_setlen(ses.writepayload, 0);
TRACE(("leave send_msg_channel_data: len %d read err %d or EOF for fd %d",
len, errno, fd))
return;
return len;
}

if (channel->read_mangler) {
channel->read_mangler(channel, buf_getwriteptr(ses.writepayload, len), &len);
if (len == 0) {
buf_setpos(ses.writepayload, 0);
buf_setlen(ses.writepayload, 0);
return;
return 0;
}
}

Expand All @@ -781,14 +789,18 @@ static void send_msg_channel_data(struct Channel *channel, int isextended) {

encrypt_packet();

/* If we receive less data than we requested when flushing, we've
reached the equivalent of EOF */
if (channel->flushing && len < (ssize_t)maxlen)
{
TRACE(("closing from channel, flushing out."))
close_chan_fd(channel, fd, SHUT_RD);
}
TRACE(("leave send_msg_channel_data"))
return len;
}

static int flush_msg_channel_data(struct Channel *channel, int isextended) {
do {
if (channel->transwindow <= 0) {
// window full
return 0;
}
} while(send_msg_channel_data(channel, isextended) > 0);
return 1;
}

/* We receive channel data */
Expand Down