forkoff()
Nicolas Williams
Nicolas.Williams at ubsw.com
Tue Feb 5 07:00:19 EST 2002
Please review the function below, forkoff(), meant to be used in
clientloop.c instead of daemon() and the code in process_escapes().
The intention is to make ~D ( like ~& but also detach) possible and to
make it possible for ssh -f (or ssh -f -f - see other thread on this)
to detach, not just forkoff().
I also intend to use the same detach technique in a feature patch for
the hang-on-exit behaviour that's been hashed to death. I.e., I might
like to add an option to control ssh's behaviour when the remote session
exits, the behaviours being: wait for the channel to be closed [current,
default], close all channels and exit, background, detach and force
exit.
To make detaching and some of the hang-on-exit options work correctly I
need a way to force a channel closed and to do it in way that is
correct wrt to the SSHv2 protocol. The forkoff() function below does
that, I think, but it would be nice to have confirmation.
Comments?
Nico
static
void
forkoff(int stop_listening, int detach)
{
pid_t pid;
int fd;
Channel *c;
/* Restore tty modes. */
leave_raw_mode();
/* Stop listening for new connections. */
if (stop_listening)
channel_stop_listening();
fprintf(stderr, "Forking off into the background - %s",
stop_listening ? "no longer listening" : "still listening");
/* Fork into background. */
pid = fork();
if (pid < 0) {
error("fork: %.100s", strerror(errno));
return;
}
if (pid != 0) { /* This is the parent. */
/* The parent just exits. */
exit(0);
}
c = channel_lookup(session_ident);
if (c == NULL)
error("couldn't lookup session channel");
/* The child continues serving connections. */
/* fake EOF on stdin */
if (compat20) {
buffer_append(&stdin_buffer, "\004", 1);
} else if (!stdin_eof) {
/*
* Sending SSH_CMSG_EOF alone does not always appear
* to be enough. So we try to send an EOF character
* first.
*/
packet_start(SSH_CMSG_STDIN_DATA);
packet_put_string("\004", 1);
packet_send();
/* Close stdin. */
stdin_eof = 1;
if (buffer_len(&stdin_buffer) == 0) {
packet_start(SSH_CMSG_EOF);
packet_send();
}
}
if (detach) {
/*
* There should be a chan_wont_read()/chan_wont_write()
* API, differing only in the debug messages used.
*/
chan_read_failed(c);
chan_write_failed(c);
channel_close_fds(c);
fd = open(_PATH_DEVNULL, O_RDWR, 0);
if (fd < 0)
return;
(void) dup2(fd, STDIN_FILENO);
(void) dup2(fd, STDOUT_FILENO);
(void) dup2(fd, STDERR_FILENO);
if (fd > 2)
(void) close(fd);
(void) setsid();
}
}
--
-DISCLAIMER: an automatically appended disclaimer may follow. By posting-
-to a public e-mail mailing list I hereby grant permission to distribute-
-and copy this message.-
Visit our website at http://www.ubswarburg.com
This message contains confidential information and is intended only
for the individual named. If you are not the named addressee you
should not disseminate, distribute or copy this e-mail. Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.
E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses. The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission. If
verification is required please request a hard-copy version. This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.
More information about the openssh-unix-dev
mailing list