Commit 1307f1fc authored by Niels Möller's avatar Niels Möller
Browse files

* src/io.c: Deleted class io_backend. Register signal handlers and

files with gc_global() instead. Deleted all references to
io_backend in the rest of the source files. Doesn't quite work yet.

Rev: src/client.c:1.130
Rev: src/client.h:1.45
Rev: src/client_x11.c:1.12
Rev: src/gc.h:1.9
Rev: src/interact.h:1.9
Rev: src/io.c:1.148
Rev: src/io.h:1.80
Rev: src/io_commands.c:1.45
Rev: src/io_commands.h:1.20
Rev: src/lsh-decode-key.c:1.10
Rev: src/lsh-export-key.c:1.10
Rev: src/lsh-writekey.c:1.16
Rev: src/lsh.c:1.147
Rev: src/lsh.h:1.22
Rev: src/lsh_proxy.c:1.24
Rev: src/lshd.c:1.124
Rev: src/lshg.c:1.14
Rev: src/reaper.c:1.21
Rev: src/reaper.h:1.10
Rev: src/resource.h:1.12
Rev: src/server_session.c:1.68
Rev: src/server_session.h:1.16
Rev: src/server_userauth.h:1.19
Rev: src/sexp-conv.c:1.9
Rev: src/srp-gen.c:1.11
Rev: src/tcpforward_commands.c:1.36
Rev: src/tcpforward_commands.h:1.6
Rev: src/unix_interact.c:1.12
Rev: src/unix_user.c:1.41
parent 6b5cb05f
......@@ -382,15 +382,13 @@ make_client_session(struct client_options *options);
void
init_client_options(struct client_options *self,
struct io_backend *backend,
struct randomness_with_poll *random,
struct exception_handler *handler,
int *exit_code)
{
self->backend = backend;
self->random = random;
self->tty = make_unix_interact(backend);
self->tty = make_unix_interact();
self->escape = -1;
self->handler = handler;
......@@ -920,14 +918,11 @@ make_client_session(struct client_options *options)
options->stdin_file = options->stdout_file = options->stderr_file = NULL;
return make_client_session_channel
(io_read(make_lsh_fd(options->backend,
in, "client stdin", options->handler),
(io_read(make_lsh_fd(in, "client stdin", options->handler),
NULL, NULL),
io_write(make_lsh_fd(options->backend,
out, "client stdout", options->handler),
io_write(make_lsh_fd(out, "client stdout", options->handler),
BLOCK_SIZE, NULL),
io_write(make_lsh_fd(options->backend,
err, "client stderr", options->handler),
io_write(make_lsh_fd(err, "client stderr", options->handler),
BLOCK_SIZE, NULL),
escape,
WINDOW_SIZE,
......@@ -1063,7 +1058,7 @@ client_argp_parser(int key, char *arg, struct argp_state *state)
client_add_action(options,
make_install_fix_channel_open_handler
(ATOM_X11,
make_channel_open_x11(options->backend)));
make_channel_open_x11()));
/* Install suspend-handler */
suspend_install_handler();
......@@ -1104,8 +1099,7 @@ client_argp_parser(int key, char *arg, struct argp_state *state)
argp_error(state, "Invalid forward specification '%s'.", arg);
client_add_action(options, make_forward_local_port
(options->backend,
make_address_info((options->with_remote_peers
(make_address_info((options->with_remote_peers
? NULL
: ssh_format("%lz", "127.0.0.1")),
listen_port),
......@@ -1123,8 +1117,7 @@ client_argp_parser(int key, char *arg, struct argp_state *state)
argp_error(state, "Invalid forward specification '%s'.", arg);
client_add_action(options, make_forward_remote_port
(options->backend,
make_address_info((options->with_remote_peers
(make_address_info((options->with_remote_peers
? ssh_format("%lz", "0.0.0.0")
: ssh_format("%lz", "127.0.0.1")),
listen_port),
......
......@@ -103,7 +103,7 @@ make_pty_request(struct interact *tty);
struct channel_open *
make_channel_open_x11(struct io_backend *backend);
make_channel_open_x11(void);
struct command *
make_forward_x11(const char *display_string,
......@@ -116,8 +116,6 @@ make_client_x11_display(const char *display, struct lsh_string *fake);
(class
(name client_options)
(vars
(backend object io_backend)
;; Used only by lsh, NULL for lshg.
(random object randomness_with_poll)
......@@ -168,7 +166,6 @@ make_client_x11_display(const char *display, struct lsh_string *fake);
void
init_client_options(struct client_options *options,
struct io_backend *backend,
struct randomness_with_poll *random,
struct exception_handler *handler,
int *exit_code);
......
......@@ -423,6 +423,7 @@ make_exc_x11_connect_handler(struct exception_handler *parent,
return make_exception_handler(do_exc_x11_connect_handler, parent, context);
}
/* FIXME: Use a static object? */
static void
do_channel_open_x11(struct channel_open *s,
struct ssh_connection *connection,
......@@ -450,8 +451,7 @@ do_channel_open_x11(struct channel_open *s,
if (display)
{
struct lsh_fd *fd
= io_connect(self->backend,
display->address,
= io_connect(display->address,
display->address_length,
make_channel_open_x11_continuation(display,
c),
......@@ -480,12 +480,11 @@ do_channel_open_x11(struct channel_open *s,
}
struct channel_open *
make_channel_open_x11(struct io_backend *backend)
make_channel_open_x11(void)
{
NEW(channel_open_x11, self);
self->super.handler = do_channel_open_x11;
self->backend = backend;
return &self->super;
}
......
......@@ -31,8 +31,10 @@
void gc_register(struct lsh_object *o);
void gc_kill(struct lsh_object *o);
void gc(struct lsh_object *root);
void gc_maybe(struct lsh_object *root, int busy);
void gc_global(struct resource *o);
void gc(void);
void gc_maybe(int busy);
#if DEBUG_ALLOC
extern int gc_final_p;
......
......@@ -98,6 +98,6 @@ struct terminal_dimensions;
((i)->window_change_subscribe((i), (c)))
struct interact *
make_unix_interact(struct io_backend *backend);
make_unix_interact(void);
#endif /* LSH_INTERACT_H_INCLUDED */
......@@ -72,29 +72,53 @@
/* Glue to liboop */
/* Because of signal handlers, there can be only one oop object. */
oop_source_sys *the_oop = NULL;
static oop_source_sys *global_oop_sys = NULL;
static oop_source *source = NULL;
void
io_init(void)
{
assert(!the_oop);
the_oop = oop_sys_new();
if (!the_oop)
struct sigaction pipe;
memset(&pipe, 0, sizeof(pipe));
pipe.sa_handler = SIG_IGN;
sigemptyset(&pipe.sa_mask);
pipe.sa_flags = 0;
if (sigaction(SIGPIPE, &pipe, NULL) < 0)
fatal("Failed to ignore SIGPIPE.\n");
assert(!global_oop_sys);
global_oop_sys = oop_sys_new();
if (!global_oop_sys)
fatal("Failed to initialize liboop.\n");
source = oop_sys_source(global_oop_sys);
}
void
io_finish(void)
io_final(void)
{
/* There mustn't be any outstanding callbacks left. */
oop_sys_delete(the_oop);
the_oop = NULL;
oop_sys_delete(global_oop_sys);
global_oop_sys = NULL;
source = NULL;
}
void
io_run(void)
{
oop_sys_run(global_oop_sys);
/* FIXME: Check for OOP_ERROR */
trace("io_run: Exiting\n");
}
/* OOP Callbacks */
static void *
lsh_oop_signal_callback(oop_source *source UNUSED, int sig, void *data)
lsh_oop_signal_callback(oop_source *s UNUSED, int sig, void *data)
{
CAST(lsh_signal_handler, self, (struct lsh_object *) data);
......@@ -115,11 +139,7 @@ lsh_oop_register_signal(struct lsh_signal_handler *handler)
handler->signum, handler);
if (handler->super.alive)
{
oop_source *source = oop_sys_source(the_oop);
source->on_signal(source, handler->signum, lsh_oop_signal_callback, handler);
}
source->on_signal(source, handler->signum, lsh_oop_signal_callback, handler);
}
static void
......@@ -127,16 +147,13 @@ lsh_oop_cancel_signal(struct lsh_signal_handler *handler)
{
trace("lsh_oop_cancel_signal: signal: %i, handler: %t\n",
handler->signum, handler);
if (handler->super.alive)
{
oop_source *source = oop_sys_source(the_oop);
source->cancel_signal(source, handler->signum, lsh_oop_signal_callback, handler);
}
if (handler->super.alive)
source->cancel_signal(source, handler->signum, lsh_oop_signal_callback, handler);
}
static void *
lsh_oop_fd_read_callback(oop_source *source UNUSED, int fileno, oop_event event, void *data)
lsh_oop_fd_read_callback(oop_source *s UNUSED, int fileno, oop_event event, void *data)
{
CAST(lsh_fd, fd, (struct lsh_object *) data);
......@@ -160,8 +177,6 @@ lsh_oop_register_read_fd(struct lsh_fd *fd)
if (fd->super.alive && !fd->want_read)
{
oop_source *source = oop_sys_source(the_oop);
assert(fd->read);
source->on_fd(source, fd->fd, OOP_READ, lsh_oop_fd_read_callback, fd);
......@@ -177,15 +192,13 @@ lsh_oop_cancel_read_fd(struct lsh_fd *fd)
if (fd->super.alive)
{
oop_source *source = oop_sys_source(the_oop);
source->cancel_fd(source, fd->fd, OOP_READ);
fd->want_read = 0;
}
}
static void *
lsh_oop_fd_write_callback(oop_source *source UNUSED, int fileno, oop_event event, void *data)
lsh_oop_fd_write_callback(oop_source *s UNUSED, int fileno, oop_event event, void *data)
{
CAST(lsh_fd, fd, (struct lsh_object *) data);
......@@ -209,8 +222,6 @@ lsh_oop_register_write_fd(struct lsh_fd *fd)
if (fd->super.alive && !fd->want_write)
{
oop_source *source = oop_sys_source(the_oop);
assert(fd->write);
source->on_fd(source, fd->fd, OOP_WRITE, lsh_oop_fd_write_callback, fd);
......@@ -226,8 +237,6 @@ lsh_oop_cancel_write_fd(struct lsh_fd *fd)
if (fd->super.alive)
{
oop_source *source = oop_sys_source(the_oop);
source->cancel_fd(source, fd->fd, OOP_WRITE);
fd->want_write = 0;
}
......@@ -240,7 +249,6 @@ lsh_oop_cancel_write_fd(struct lsh_fd *fd)
(name lsh_signal_handler)
(super resource)
(vars
(next object lsh_signal_handler)
(signum . int)
(action object lsh_callback)))
*/
......@@ -258,7 +266,7 @@ lsh_oop_cancel_write_fd(struct lsh_fd *fd)
/* FIXME: With liboop, we should not need this object, we'd only need
* a resource_list owned by the gc. */
/* GABA:
/* ;; GABA:
(class
(name io_backend)
(super resource)
......@@ -550,24 +558,7 @@ int io_iter(struct io_backend *b)
}
#endif
void
io_run(struct io_backend *b)
{
struct sigaction pipe;
memset(&pipe, 0, sizeof(pipe));
pipe.sa_handler = SIG_IGN;
sigemptyset(&pipe.sa_mask);
pipe.sa_flags = 0;
if (sigaction(SIGPIPE, &pipe, NULL) < 0)
fatal("Failed to ignore SIGPIPE.\n");
oop_sys_run(the_oop);
trace("oop_sys_run returned!?\n");
}
#if 0
static void
do_kill_io_backend(struct resource *s)
{
......@@ -593,14 +584,14 @@ do_kill_io_backend(struct resource *s)
backend->super.alive = 0;
}
}
#endif
#if 0
struct io_backend *
make_io_backend(void)
{
NEW(io_backend, b);
assert(!the_oop);
io_init();
init_resource(&b->super, do_kill_io_backend);
......@@ -610,12 +601,7 @@ make_io_backend(void)
return b;
}
void
io_final(struct io_backend *b)
{
KILL_RESOURCE(&b->super);
}
#endif
static void
do_kill_signal_handler(struct resource *s)
......@@ -630,39 +616,34 @@ do_kill_signal_handler(struct resource *s)
}
struct resource *
io_signal_handler(struct io_backend *b,
int signum,
io_signal_handler(int signum,
struct lsh_callback *action)
{
NEW(lsh_signal_handler, handler);
init_resource(&handler->super, do_kill_signal_handler);
handler->next = b->signals;
handler->signum = signum;
handler->action = action;
b->signals = handler;
lsh_oop_register_signal(handler);
gc_global(&handler->super);
return &handler->super;
}
/* Delays not implemented. */
struct resource *
io_callout(struct io_backend *b,
UINT32 delay UNUSED,
io_callout(UINT32 delay UNUSED,
struct lsh_callback *action)
{
NEW(lsh_callout, callout);
init_resource(&callout->super, NULL);
NEW(lsh_callout, self);
init_resource(&self->super, NULL);
callout->next = b->callouts;
callout->action = action;
b->callouts = callout;
self->action = action;
return &callout->super;
gc_global(&self->super);
return &self->super;
}
/* Read-related callbacks */
......@@ -793,11 +774,13 @@ do_consuming_read(struct io_callback *c,
assert(fd->want_read);
#if 0
if (fd->hanged_up)
{
/* If hanged_up is set, pretend that read returned 0 */
goto eof;
}
#endif
if (!wanted)
{
......@@ -948,7 +931,6 @@ make_listen_value(struct lsh_fd *fd,
(name io_listen_callback)
(super io_callback)
(vars
(backend object io_backend)
(c object command_continuation)
(e object exception_handler)))
*/
......@@ -977,20 +959,17 @@ do_listen_callback(struct io_callback *s,
}
trace("io.c: accept on fd %i\n", conn);
COMMAND_RETURN(self->c,
make_listen_value(make_lsh_fd(self->backend,
conn, "accepted socket", self->e),
make_listen_value(make_lsh_fd(conn, "accepted socket", self->e),
sockaddr2info(addr_len,
(struct sockaddr *) &peer)));
}
struct io_callback *
make_listen_callback(struct io_backend *backend,
struct command_continuation *c,
make_listen_callback(struct command_continuation *c,
struct exception_handler *e)
{
NEW(io_listen_callback, self);
self->super.f = do_listen_callback;
self->backend = backend;
self->c = c;
self->e = e;
......@@ -1079,6 +1058,7 @@ do_exc_io_handler(struct exception_handler *self,
return;
}
#if 0
/* Initializes a file structure, and adds it to the backend's list. */
static void
init_file(struct io_backend *b, struct lsh_fd *f, int fd,
......@@ -1108,6 +1088,7 @@ init_file(struct io_backend *b, struct lsh_fd *f, int fd,
f->next = b->files;
b->files = f;
}
#endif
/* These functions are used by werror and friends */
......@@ -1533,22 +1514,38 @@ void io_init_fd(int fd)
}
struct lsh_fd *
make_lsh_fd(struct io_backend *b,
int fd, const char *label,
make_lsh_fd(int fd, const char *label,
struct exception_handler *e)
{
NEW(lsh_fd, f);
NEW(lsh_fd, self);
io_init_fd(fd);
init_file(b, f, fd, label, e);
return f;
init_resource(&self->super, do_kill_fd);
self->fd = fd;
self->label = label;
self->e = make_exception_handler(do_exc_io_handler, e, HANDLER_CONTEXT);
self->close_callback = NULL;
self->prepare = NULL;
self->want_read = 0;
self->read = NULL;
self->want_write = 0;
self->write = NULL;
self->write_close = NULL;
gc_global(&self->super);
return self;
}
/* Some code is taken from Thomas Bellman's tcputils. */
struct lsh_fd *
io_connect(struct io_backend *b,
struct sockaddr *remote,
io_connect(struct sockaddr *remote,
socklen_t remote_length,
struct command_continuation *c,
struct exception_handler *e)
......@@ -1582,7 +1579,7 @@ io_connect(struct io_backend *b,
return NULL;
}
fd = make_lsh_fd(b, s, "connecting socket", e);
fd = make_lsh_fd(s, "connecting socket", e);
fd->write = make_connect_callback(c);
lsh_oop_register_write_fd(fd);
......@@ -1591,8 +1588,7 @@ io_connect(struct io_backend *b,
}
struct lsh_fd *
io_listen(struct io_backend *b,
struct sockaddr *local,
io_listen(struct sockaddr *local,
socklen_t length,
struct io_callback *callback,
struct exception_handler *e)
......@@ -1624,7 +1620,7 @@ io_listen(struct io_backend *b,
return NULL;
}
fd = make_lsh_fd(b, s, "listening socket", e);
fd = make_lsh_fd(s, "listening socket", e);
fd->read = callback;
lsh_oop_register_read_fd(fd);
......@@ -1748,8 +1744,7 @@ safe_pushd(const char *directory,
struct lsh_fd *
io_listen_local(struct io_backend *b,
struct local_info *info,
io_listen_local(struct local_info *info,
struct io_callback *callback,
struct exception_handler *e)
{
......@@ -1802,7 +1797,7 @@ io_listen_local(struct io_backend *b,
old_umask = umask(0077);
/* Bind and listen */
fd = io_listen(b, (struct sockaddr *) local, local_length, callback, e);
fd = io_listen( (struct sockaddr *) local, local_length, callback, e);
/* Ok, now we restore umask and cwd */
umask(old_umask);
......@@ -1814,8 +1809,7 @@ io_listen_local(struct io_backend *b,
/* Requires DIRECTORY and NAME to be NUL-terminated */
struct lsh_fd *
io_connect_local(struct io_backend *b,
struct local_info *info,
io_connect_local(struct local_info *info,
struct command_continuation *c,
struct exception_handler *e)
{
......@@ -1847,7 +1841,7 @@ io_connect_local(struct io_backend *b,
if (old_cd < 0)
return NULL;
fd = io_connect(b, (struct sockaddr *) addr, addr_length, c, e);
fd = io_connect( (struct sockaddr *) addr, addr_length, c, e);
safe_popd(old_cd, cdir);
......@@ -1920,8 +1914,7 @@ io_write(struct lsh_fd *fd,
}
struct lsh_fd *
io_write_file(struct io_backend *backend,
const char *fname, int flags, int mode,
io_write_file(const char *fname, int flags, int mode,
UINT32 block_size,
struct lsh_callback *c,
struct exception_handler *e)
......@@ -1930,20 +1923,19 @@ io_write_file(struct io_backend *backend,
if (fd < 0)
return NULL;
return io_write(make_lsh_fd(backend, fd, "write-only file", e),
return io_write(make_lsh_fd(fd, "write-only file", e),
block_size, c);
}
struct lsh_fd *
io_read_file(struct io_backend *backend,
const char *fname,
io_read_file(const char *fname,
struct exception_handler *e)
{
int fd = open(fname, O_RDONLY);
if (fd < 0)
return NULL;
return make_lsh_fd(backend, fd, "read-only file", e);
return make_lsh_fd(fd, "read-only file", e);
}
void close_fd(struct lsh_fd *fd)
......
......@@ -44,10 +44,11 @@
#include "io.h.x"
#undef GABA_DECLARE
#if 0
/* Declare the class object, so that io_commands.c can refer to it for
* type-checking. */
extern struct lsh_class io_backend_class;
#endif
/* GABA:
(class
......@@ -81,9 +82,6 @@ extern struct lsh_class io_backend_class;
; For debugging purposes
(label . "const char *")
;; (backend object io_backend)
;; (next_closed object lsh_fd)
; Used for raising i/o-exceptions.
; Also passed on to readers of the consuming type,
; which seems kind of bogus.
......@@ -98,7 +96,7 @@ extern struct lsh_class io_backend_class;
; This flag is set by the backend if it detects that a
; connection is hanged up.
(hanged_up . int)
;;; (hanged_up . int)
(want_read . int)
; Called if poll indicates that data can be read.
......@@ -227,7 +225,9 @@ void
io_init(void);
void
io_finish(void);
io_final(void);
void io_run(void);
#if 0
void
......@@ -249,27 +249,12 @@ lsh_oop_register_write_fd(struct lsh_fd *fd);
void
lsh_oop_cancel_write_fd(struct lsh_fd *fd);
struct io_backend *
make_io_backend(void);