Commit 0371ba40 authored by Niels Möller's avatar Niels Möller
Browse files

* src/server_session.c (spawn_process): Deleted old code.

(shell_request_handler): Likewise.
(exec_request_handler): Likewise.
(do_spawn_subsystem): Likewise.

Rev: src/server_session.c:1.82
parent 460bca66
......@@ -502,238 +502,6 @@ spawn_process(struct server_session *session,
return 1;
}
#if 0
/* Returns -1 on failure, 0 for child and +1 for parent */
static int
spawn_process(struct server_session *session,
struct lsh_user *user,
struct address_info *peer)
{
struct lsh_process *child;
int in[2];
int out[2];
int err[2];
/* Pipe used for syncronization. */
int sync[2];
if (session->process)
/* Already spawned a shell or command */
return -1;
/* {in|out|err}[0] is for reading,
* {in|out|err}[1] for writing. */
if (!lsh_make_pipe(sync))
{
werror("spawn_process: Failed to create syncronization pipe.\n");
return -1;
}
if (session->pty && !make_pty(session->pty, in, out, err))
{
KILL_RESOURCE(&session->pty->super);
KILL(session->pty);
session->pty = NULL;
}
if (!session->pty && !make_pipes(in, out, err))
return -1;
if (USER_FORK(user, &child,
make_exit_shell(session),
peer, session->pty ? session->pty->tty_name : NULL))
{
if (child)
{ /* Parent */
char dummy;
int res;
struct ssh_channel *channel = &session->super;
trace("spawn_process: Parent process\n");
session->process = child;
/* Close the child's fd:s */
close(in[0]);
close(out[1]);
close(err[1]);
close(sync[1]);
/* On Solaris, reading the master side of the pty before the
* child has opened the slave side of it results in EINVAL.
* We can't have that, so we'll wait until the child has
* opened the tty, after which it should close its end of
* the syncronization pipe, and our read will return 0.
*
* We need the syncronizatino only if we're actually using a
* pty, but for simplicity, we do it every time. */
do
res = read(sync[0], &dummy, 1);
while (res < 0 && errno == EINTR);
close(sync[0]);
{
/* Exception handlers */
struct exception_handler *io_exception_handler
= make_channel_io_exception_handler(channel,
"lshd: Child stdio: ",
&default_exception_handler,
HANDLER_CONTEXT);
/* Close callback for stderr and stdout */
struct lsh_callback *read_close_callback
= make_channel_read_close_callback(channel);
session->in
= io_write(make_lsh_fd(in[1], "child stdin",
io_exception_handler),
SSH_MAX_PACKET, NULL);
/* Flow control */
session->in->write_buffer->report = &session->super.super;
/* FIXME: Should we really use the same exception handler,
* which will close the channel on read errors, or is it
* better to just send EOF on read errors? */
session->out
= io_read(make_lsh_fd(out[0], "child stdout",
io_exception_handler),
make_channel_read_data(channel),
read_close_callback);
session->err
= ( (err[0] != -1)
? io_read(make_lsh_fd(err[0], "child stderr",
io_exception_handler),
make_channel_read_stderr(channel),
read_close_callback)
: NULL);
}
channel->receive = do_receive;
channel->send_adjust = do_send_adjust;
channel->eof = do_eof;
/* Make sure that the process and it's stdio is
* cleaned up if the channel or connection dies. */
remember_resource
(channel->resources, &child->super);
/* FIXME: How to do this properly if in and out may use the
* same fd? */
remember_resource
(channel->resources, &session->in->super);
remember_resource
(channel->resources, &session->out->super);
if (session->err)
remember_resource
(channel->resources, &session->err->super);
/* Don't close channel immediately at EOF, as we want to
* get a chance to send exit-status or exit-signal. */
session->super.flags &= ~CHANNEL_CLOSE_AT_EOF;
return 1;
}
else
{ /* Child */
int tty = -1;
trace("spawn_process: Child process\n");
assert(getuid() == user->uid);
#if 0
/* Debug timing problems */
if (sleep(5))
{
trace("server_session.c: sleep interrupted\n");
sleep(5);
}
#endif
if (!USER_CHDIR_HOME(user))
{
werror("Could not change to home (or root) directory!\n");
_exit(EXIT_FAILURE);
}
#if WITH_PTY_SUPPORT
if (session->pty)
{
debug("lshd: server.c: Opening slave tty...\n");
if ( (tty = pty_open_slave(session->pty)) < 0)
{
debug("lshd: server.c: "
"Opening slave tty... Failed!\n");
werror("lshd: Can't open controlling tty for child!\n");
_exit(EXIT_FAILURE);
}
else
debug("lshd: server.c: Opening slave tty... Ok.\n");
}
#endif /* WITH_PTY_SUPPORT */
/* Now any tty processing is done, so notify our parent by
* closing the syncronization pipe. */
close(sync[0]); close(sync[1]);
/* Close all descriptors but those used for communicationg
* with parent. We rely on the close-on-exec flag for all
* other fd:s. */
if (dup2(in[0] >= 0 ? in[0] : tty, STDIN_FILENO) < 0)
{
werror("Can't dup stdin!\n");
_exit(EXIT_FAILURE);
}
if (dup2(out[1] >= 0 ? out[1] : tty, STDOUT_FILENO) < 0)
{
werror("Can't dup stdout!\n");
_exit(EXIT_FAILURE);
}
if (!dup_error_stream())
{
werror("server_session: Failed to dup old stderr. Bye.\n");
set_error_ignore();
}
if (dup2(err[1] >= 0 ? err[1] : tty, STDERR_FILENO) < 0)
{
werror("Can't dup stderr!\n");
_exit(EXIT_FAILURE);
}
/* Unconditionally close all the fd:s, no matter if some
* of them are -1. */
close(in[0]);
close(in[1]);
close(out[0]);
close(out[1]);
close(err[0]);
close(err[1]);
close(tty);
return 0;
}
}
/* fork failed */
/* Close all fd:s */
close(err[0]);
close(err[1]);
close(out[0]);
close(out[1]);
close(in[0]);
close(in[1]);
return -1;
}
#endif
static void
init_spawn_info(struct spawn_info *info, struct server_session *session,
unsigned argc, const char **argv,
......@@ -798,65 +566,6 @@ DEFINE_CHANNEL_REQUEST(shell_request_handler)
fail:
EXCEPTION_RAISE(e, &shell_request_failed);
}
#if 0
switch (spawn_process(session, channel->connection->user,
channel->connection->peer))
{
case 1: /* Parent */
/* NOTE: The return value is not used. */
COMMAND_RETURN(c, channel);
channel_start_receive(channel, session->initial_window);
return;
case 0:
{ /* Child */
#define MAX_ENV 1
/* No args, and the USER_EXEC method fills in argv[0]. */
const char *argv[] = { NULL, NULL };
struct env_value env[MAX_ENV];
int env_length = 0;
debug("do_spawn_shell: Child process\n");
assert(getuid() == channel->connection->user->uid);
if (session->term)
{
env[env_length].name ="TERM";
env[env_length].value = session->term;
env_length++;
}
assert(env_length <= MAX_ENV);
#undef MAX_ENV
#if 1
USER_EXEC(channel->connection->user, 1, argv, env_length, env);
/* exec failed! */
verbose("server_session: exec failed (errno = %i): %z\n",
errno, STRERROR(errno));
_exit(EXIT_FAILURE);
#else
# define GREETING "Hello world!\n"
if (write(STDOUT_FILENO, GREETING, strlen(GREETING)) < 0)
_exit(errno);
kill(getuid(), SIGSTOP);
if (write(STDOUT_FILENO, shell, strlen(shell)) < 0)
_exit(125);
_exit(126);
# undef GREETING
#endif
}
case -1:
/* fork failed */
break;
default:
fatal("Internal error!");
}
fail:
EXCEPTION_RAISE(e, &shell_request_failed);
#endif
}
DEFINE_CHANNEL_REQUEST(exec_request_handler)
......@@ -909,67 +618,6 @@ DEFINE_CHANNEL_REQUEST(exec_request_handler)
EXCEPTION_RAISE(e, &exec_request_failed);
lsh_string_free(s);
#if 0
switch (spawn_process(session, channel->connection->user,
channel->connection->peer))
{
case 1: /* Parent */
lsh_string_free(command_line);
/* NOTE: The return value is not used. */
COMMAND_RETURN(c, channel);
channel_start_receive(channel, session->initial_window);
return;
case 0:
{ /* Child */
#define MAX_ENV 1
struct env_value env[MAX_ENV];
int env_length = 0;
/* No args, and the USER_EXEC method fills in argv[0]. */
/* NOTE: I'd like to use an array initializer, but that's
* not ANSI-C, and at least HPUX' compiler can't handle
* it. */
const char *argv[4];
argv[0] = NULL;
argv[1] = "-c";
argv[2] = lsh_get_cstring(command_line);
argv[3] = NULL;
debug("do_spawn_shell: Child process\n");
assert(getuid() == channel->connection->user->uid);
assert(argv[2]);
/* FIXME: Set SSH_TTY, SSH_CLIENT and SSH_ORIGINAL_COMMAND */
if (session->term)
{
env[env_length].name ="TERM";
env[env_length].value = session->term;
env_length++;
}
assert(env_length <= MAX_ENV);
#undef MAX_ENV
USER_EXEC(channel->connection->user, 0, argv, env_length, env);
/* exec failed! */
verbose("server_session: exec failed (errno = %i): %z\n",
errno, STRERROR(errno));
_exit(EXIT_FAILURE);
}
case -1:
/* fork failed */
lsh_string_free(command_line);
EXCEPTION_RAISE(e, &exec_request_failed);
break;
default:
fatal("Internal error!");
}
#endif
}
}
......@@ -1060,38 +708,7 @@ do_spawn_subsystem(struct channel_request *s,
{
COMMAND_RETURN(c, channel);
return;
}
#if 0
switch (spawn_process(session, channel->connection->user,
channel->connection->peer))
{
case 1: /* Parent */
/* NOTE: The return value is not used. */
COMMAND_RETURN(c, channel);
channel_start_receive(channel, session->initial_window);
return;
case 0: /* Child */
{
/* No args, and the USER_EXEC method fills in argv[0]. */
const char *argv[] = { NULL, NULL };
debug("do_spawn_subsystem: Child process\n");
USER_EXEC(channel->connection->user, 1, argv, 0, NULL);
werror("server_session: subsystem exec failed (errno = %i): %z\n",
errno, STRERROR(errno));
_exit(EXIT_FAILURE);
}
case -1: /* Error */
break;
default:
fatal("Internal error!");
}
#endif
}
}
EXCEPTION_RAISE(e, &subsystem_request_failed);
}
......
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