diff common-session.c @ 1511:5916af64acd4 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Sat, 17 Feb 2018 19:29:51 +0800
parents 6b89eb92f872 0c16b4ccbd54
children bb8eaa26bc93
line wrap: on
line diff
--- a/common-session.c	Tue Jan 23 23:27:40 2018 +0800
+++ b/common-session.c	Sat Feb 17 19:29:51 2018 +0800
@@ -43,13 +43,6 @@
 
 struct sshsession ses; /* GLOBAL */
 
-/* need to know if the session struct has been initialised, this way isn't the
- * cleanest, but works OK */
-int sessinitdone = 0; /* GLOBAL */
-
-/* this is set when we get SIGINT or SIGTERM, the handler is in main.c */
-int exitflag = 0; /* GLOBAL */
-
 /* called only at the start of a session, set up initial state */
 void common_session_init(int sock_in, int sock_out) {
 	time_t now;
@@ -86,13 +79,13 @@
 	if (!fuzz.fuzzing)
 #endif
 	{
-		if (pipe(ses.signal_pipe) < 0) {
-			dropbear_exit("Signal pipe failed");
-		}
-		setnonblocking(ses.signal_pipe[0]);
-		setnonblocking(ses.signal_pipe[1]);
-		ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[0]);
-		ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[1]);
+	if (pipe(ses.signal_pipe) < 0) {
+		dropbear_exit("Signal pipe failed");
+	}
+	setnonblocking(ses.signal_pipe[0]);
+	setnonblocking(ses.signal_pipe[1]);
+	ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[0]);
+	ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[1]);
 	}
 	
 	ses.writepayload = buf_new(TRANS_MAX_PAYLOAD_LEN);
@@ -169,9 +162,8 @@
 		if (!fuzz.fuzzing) 
 #endif
 		{
-			FD_SET(ses.signal_pipe[0], &readfd);
+		FD_SET(ses.signal_pipe[0], &readfd);
 		}
-		ses.channel_signal_pending = 0;
 
 		/* set up for channels which can be read/written */
 		setchannelfds(&readfd, &writefd, writequeue_has_space);
@@ -199,7 +191,7 @@
 
 		val = select(ses.maxfd+1, &readfd, &writefd, NULL, &timeout);
 
-		if (exitflag) {
+		if (ses.exitflag) {
 			dropbear_exit("Terminated by signal");
 		}
 		
@@ -219,6 +211,7 @@
 		/* We'll just empty out the pipe if required. We don't do
 		any thing with the data, since the pipe's purpose is purely to
 		wake up the select() above. */
+		ses.channel_signal_pending = 0;
 		if (FD_ISSET(ses.signal_pipe[0], &readfd)) {
 			char x;
 			TRACE(("signal pipe set"))
@@ -253,6 +246,10 @@
 
 		handle_connect_fds(&writefd);
 
+		/* loop handler prior to channelio, in case the server loophandler closes
+		channels on process exit */
+		loophandler();
+
 		/* process pipes etc for the channels, ses.dataallowed == 0
 		 * during rekeying ) */
 		channelio(&readfd, &writefd);
@@ -264,11 +261,6 @@
 			}
 		}
 
-
-		if (loophandler) {
-			loophandler();
-		}
-
 	} /* for(;;) */
 	
 	/* Not reached */
@@ -289,8 +281,8 @@
 	TRACE(("enter session_cleanup"))
 	
 	/* we can't cleanup if we don't know the session state */
-	if (!sessinitdone) {
-		TRACE(("leave session_cleanup: !sessinitdone"))
+	if (!ses.init_done) {
+		TRACE(("leave session_cleanup: !ses.init_done"))
 		return;
 	}