View | Details | Raw Unified | Return to bug 92 | Differences between
and this patch

Collapse All | Expand All

(-)3_0_2p1.1/ssh.c (-1 / +4 lines)
Lines 111-116 Link Here
111
 * background.
111
 * background.
112
 */
112
 */
113
int fork_after_authentication_flag = 0;
113
int fork_after_authentication_flag = 0;
114
int wait_for_port_open_before_fork = 0;
114
115
115
/*
116
/*
116
 * General data structure for command line options and options configurable
117
 * General data structure for command line options and options configurable
Lines 330-335 Link Here
330
			stdin_null_flag = 1;
331
			stdin_null_flag = 1;
331
			break;
332
			break;
332
		case 'f':
333
		case 'f':
334
			if (fork_after_authentication_flag)
335
				wait_for_port_open_before_fork = 1;
333
			fork_after_authentication_flag = 1;
336
			fork_after_authentication_flag = 1;
334
			stdin_null_flag = 1;
337
			stdin_null_flag = 1;
335
			break;
338
			break;
Lines 1164-1170 Link Here
1164
		id = ssh_session2_open();
1167
		id = ssh_session2_open();
1165
1168
1166
	/* If requested, let ssh continue in the background. */
1169
	/* If requested, let ssh continue in the background. */
1167
	if (fork_after_authentication_flag)
1170
	if (fork_after_authentication_flag && !wait_for_port_open_before_fork)
1168
		if (daemon(1, 1) < 0)
1171
		if (daemon(1, 1) < 0)
1169
			fatal("daemon() failed: %.200s", strerror(errno));
1172
			fatal("daemon() failed: %.200s", strerror(errno));
1170
1173
(-)3_0_2p1.1/clientloop.c (+79 lines)
Lines 95-100 Link Here
95
 */
95
 */
96
extern char *host;
96
extern char *host;
97
97
98
extern int fork_after_authentication_flag;
99
extern int wait_for_port_open_before_fork;
100
98
/*
101
/*
99
 * Flag to indicate that we have received a window change signal which has
102
 * Flag to indicate that we have received a window change signal which has
100
 * not yet been processed.  This will cause a message indicating the new
103
 * not yet been processed.  This will cause a message indicating the new
Lines 1007-1012 Link Here
1007
1010
1008
/*********/
1011
/*********/
1009
1012
1013
/*
1014
 * Detach the program (continue to serve connections,
1015
 * but put in background and no more new connections).
1016
 */
1017
static
1018
void
1019
forkoff(int stop_listening, int detach)
1020
{
1021
	pid_t pid;
1022
	int fd;
1023
	Channel *c;
1024
1025
	/* Restore tty modes. */
1026
	leave_raw_mode();
1027
1028
	/* Stop listening for new connections. */
1029
	if (stop_listening)
1030
		channel_stop_listening();
1031
1032
	fprintf(stderr, "Forking off into the background - %s",
1033
		stop_listening ? "no longer listening" : "still listening");
1034
1035
	/* Fork into background. */
1036
	pid = fork();
1037
	if (pid < 0) {
1038
		error("fork: %.100s", strerror(errno));
1039
		return;
1040
	}
1041
	if (pid != 0) {	/* This is the parent. */
1042
		/* The parent just exits. */
1043
		exit(0);
1044
	}
1045
1046
	c = channel_lookup(session_ident);
1047
	if (c == NULL)
1048
		error("couldn't lookup session channel");
1049
1050
	/* The child continues serving connections. */
1051
	/* fake EOF on stdin */
1052
	if (compat20) {
1053
		buffer_append(&stdin_buffer, "\004", 1);
1054
	} else if (!stdin_eof) {
1055
		/*
1056
		 * Sending SSH_CMSG_EOF alone does not always appear
1057
		 * to be enough.  So we try to send an EOF character
1058
		 * first.
1059
		 */
1060
		packet_start(SSH_CMSG_STDIN_DATA);
1061
		packet_put_string("\004", 1);
1062
		packet_send();
1063
		/* Close stdin. */
1064
		stdin_eof = 1;
1065
		if (buffer_len(&stdin_buffer) == 0) {
1066
			packet_start(SSH_CMSG_EOF);
1067
			packet_send();
1068
		}
1069
	}
1070
1071
	if (detach) {
1072
		chan_read_failed(c);
1073
		chan_write_failed(c);
1074
		channel_close_fds(c);
1075
		fd = open(_PATH_DEVNULL, O_RDWR, 0);
1076
		if (fd < 0)
1077
			return;
1078
		(void) dup2(fd, STDIN_FILENO);
1079
		(void) dup2(fd, STDOUT_FILENO);
1080
		(void) dup2(fd, STDERR_FILENO);
1081
		if (fd > 2)
1082
			(void) close(fd);
1083
		(void) setsid();
1084
	}
1085
}
1086
1010
static void
1087
static void
1011
client_input_stdout_data(int type, int plen, void *ctxt)
1088
client_input_stdout_data(int type, int plen, void *ctxt)
1012
{
1089
{
Lines 1187-1192 Link Here
1187
			packet_put_int(c->local_maxpacket);
1264
			packet_put_int(c->local_maxpacket);
1188
			packet_send();
1265
			packet_send();
1189
		}
1266
		}
1267
		if (fork_after_authentication_flag && wait_for_port_open_before_fork)
1268
			forkoff(0, 1);
1190
	} else {
1269
	} else {
1191
		debug("failure %s", ctype);
1270
		debug("failure %s", ctype);
1192
		packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1271
		packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
(-)3_0_2p1.1/channels.h (+1 lines)
Lines 153-158 Link Here
153
void	 channel_register_filter(int, channel_filter_fn *);
153
void	 channel_register_filter(int, channel_filter_fn *);
154
void	 channel_cancel_cleanup(int);
154
void	 channel_cancel_cleanup(int);
155
int	 channel_close_fd(int *);
155
int	 channel_close_fd(int *);
156
void	 channel_close_fds(Channel *);
156
157
157
/* protocol handler */
158
/* protocol handler */
158
159
(-)3_0_2p1.1/channels.c (-1 / +1 lines)
Lines 301-307 Link Here
301
301
302
/* Close all channel fd/socket. */
302
/* Close all channel fd/socket. */
303
303
304
static void
304
void
305
channel_close_fds(Channel *c)
305
channel_close_fds(Channel *c)
306
{
306
{
307
	debug3("channel_close_fds: channel %d: r %d w %d e %d",
307
	debug3("channel_close_fds: channel %d: r %d w %d e %d",

Return to bug 92