Nicolas Williams Nicolas.Williams at
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

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.



forkoff(int stop_listening, int detach)
	pid_t pid;
	int fd;
	Channel *c;

	/* Restore tty modes. */

	/* Stop listening for new connections. */
	if (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));
	if (pid != 0) {	/* This is the parent. */
		/* The parent just exits. */

	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_put_string("\004", 1);
		/* Close stdin. */
		stdin_eof = 1;
		if (buffer_len(&stdin_buffer) == 0) {

	if (detach) {
		 * There should be a chan_wont_read()/chan_wont_write()
		 * API, differing only in the debug messages used.
		fd = open(_PATH_DEVNULL, O_RDWR, 0);
		if (fd < 0)
		(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

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