Commit 0af9bf7a authored by Niels Möller's avatar Niels Möller
Browse files

* src/channel_io.c (channel_io_read): Return an enum

channel_io_status. Don't call channel_read_state_close on error;
leave that up to the caller.
(channel_io_start_read): Check that file->fd is valid.
(channel_io_write, channel_io_flush): Return an enum
channel_io_status. Don't call channel_write_state_close on error;
leave that up to the caller.
(channel_io_stop_write): Return CHANNEL_IO_EOF if all data has
been written.

* src/channel_io.h (enum channel_io_status): New enum.

Rev: src/channel_io.c:1.1.2.4
Rev: src/channel_io.h:1.1.2.2
parent df12eafb
......@@ -59,17 +59,9 @@ channel_read_state_close(struct channel_read_state *file)
file->fd = -1;
}
/* Returns amount of data read into the read buffer. It returns 0 if
one of the following happens:
* There's no more window space on the channel. Reading is stopped.
* The read returned EOF. Calls channel_eof if appropriate.
* There's a read error. The channel is closed. */
uint32_t
enum channel_io_status
channel_io_read(struct ssh_channel *channel,
struct channel_read_state *file)
struct channel_read_state *file, uint32_t *done)
{
uint32_t to_read;
int res;
......@@ -102,8 +94,8 @@ channel_io_read(struct ssh_channel *channel,
{
/* Out of window space, so stop reading. */
channel_io_stop_read(file);
return 0;
*done = 0;
return CHANNEL_IO_OK;
}
res = lsh_string_read(file->buffer, 0, file->fd, to_read);
......@@ -111,26 +103,29 @@ channel_io_read(struct ssh_channel *channel,
if (res < 0)
{
werror("reading on channel fd %d failed: %e.\n", file->fd, errno);
channel_read_state_close(file);
channel_close(channel);
return 0;
return CHANNEL_IO_ERROR;
}
else if (res == 0)
{
channel_read_state_close(file);
assert(channel->sources);
if (!--channel->sources)
channel_eof(channel);
*done = 0;
return CHANNEL_IO_EOF;
}
return res;
*done = res;
return CHANNEL_IO_OK;
}
void
channel_io_start_read(struct ssh_channel *channel,
struct channel_read_state *file, oop_call_fd *f)
{
if (!file->active)
if (file->fd >= 0 && !file->active)
{
file->active = 1;
global_oop_source->on_fd(global_oop_source, file->fd,
......@@ -172,7 +167,7 @@ channel_write_state_close(struct ssh_channel *channel,
channel_maybe_close(channel);
}
void
enum channel_io_status
channel_io_write(struct ssh_channel *channel,
struct channel_write_state *file,
oop_call_fd *f,
......@@ -186,20 +181,23 @@ channel_io_write(struct ssh_channel *channel,
channel_adjust_rec_window(channel, done);
if (file->state->length)
channel_io_start_write(channel, file, f);
{
channel_io_start_write(channel, file, f);
return CHANNEL_IO_OK;
}
else
channel_io_stop_write(channel, file);
return channel_io_stop_write(channel, file);
}
else
{
werror("write failed on channel write fd %d: %e.\n", file->fd, errno);
channel_write_state_close(channel, file);
channel_close(channel);
return CHANNEL_IO_ERROR;
}
}
void
enum channel_io_status
channel_io_flush(struct ssh_channel *channel,
struct channel_write_state *file)
{
......@@ -208,15 +206,16 @@ channel_io_flush(struct ssh_channel *channel,
{
channel_adjust_rec_window(channel, done);
if (!file->state->length)
channel_io_stop_write(channel, file);
return channel_io_stop_write(channel, file);
}
else if (errno != EWOULDBLOCK)
{
werror("Write failed on channel write fd %d: %e.\n", file->fd, errno);
channel_write_state_close(channel, file);
channel_close(channel);
return CHANNEL_IO_ERROR;
}
return CHANNEL_IO_OK;
}
void
......@@ -231,16 +230,15 @@ channel_io_start_write(struct ssh_channel *channel,
}
}
void
enum channel_io_status
channel_io_stop_write(struct ssh_channel *channel,
struct channel_write_state *file)
struct channel_write_state *file)
{
if (file->active)
{
file->active = 0;
global_oop_source->cancel_fd(global_oop_source, file->fd, OOP_WRITE);
}
if (channel->flags & CHANNEL_RECEIVED_EOF)
channel_write_state_close(channel, file);
}
return (channel->flags & CHANNEL_RECEIVED_EOF) ? CHANNEL_IO_EOF : CHANNEL_IO_OK;
}
......@@ -28,6 +28,14 @@
struct ssh_channel;
enum channel_io_status
{
/* Includes the case that we're out of window space. */
CHANNEL_IO_OK = 0,
CHANNEL_IO_EOF,
CHANNEL_IO_ERROR,
};
#define GABA_DECLARE
# include "channel_io.h.x"
#undef GABA_DECLARE
......@@ -48,9 +56,9 @@ init_channel_read_state(struct channel_read_state *self, int fd,
void
channel_read_state_close(struct channel_read_state *file);
uint32_t
enum channel_io_status
channel_io_read(struct ssh_channel *channel,
struct channel_read_state *file);
struct channel_read_state *file, uint32_t *done);
void
channel_io_start_read(struct ssh_channel *channel,
......@@ -76,13 +84,13 @@ void
channel_write_state_close(struct ssh_channel *channel,
struct channel_write_state *file);
void
enum channel_io_status
channel_io_write(struct ssh_channel *channel,
struct channel_write_state *file,
oop_call_fd *f,
uint32_t length, const uint8_t *data);
void
enum channel_io_status
channel_io_flush(struct ssh_channel *channel,
struct channel_write_state *file);
......@@ -90,7 +98,7 @@ void
channel_io_start_write(struct ssh_channel *channel,
struct channel_write_state *file, oop_call_fd *f);
void
enum channel_io_status
channel_io_stop_write(struct ssh_channel *channel,
struct channel_write_state *file);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment