diff cli-main.c @ 1209:ab7d9c12caa7

Merge pull request #17 from annulen/kill_proxy_cmd Client: kill proxy command when exiting application.
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Dec 2015 21:40:32 +0800
parents fb58cf341951
children bf626d259eb1
line wrap: on
line diff
--- a/cli-main.c	Tue Dec 15 21:30:59 2015 +0800
+++ b/cli-main.c	Tue Dec 15 21:40:32 2015 +0800
@@ -36,7 +36,8 @@
 static void cli_dropbear_log(int priority, const char* format, va_list param);
 
 #ifdef ENABLE_CLI_PROXYCMD
-static void cli_proxy_cmd(int *sock_in, int *sock_out);
+static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out);
+static void killchild(int signo);
 #endif
 
 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
@@ -66,10 +67,16 @@
 		dropbear_exit("signal() error");
 	}
 
+	pid_t proxy_cmd_pid = 0;
 #ifdef ENABLE_CLI_PROXYCMD
 	if (cli_opts.proxycmd) {
-		cli_proxy_cmd(&sock_in, &sock_out);
+		cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
 		m_free(cli_opts.proxycmd);
+		if (signal(SIGINT, killchild) == SIG_ERR ||
+			signal(SIGTERM, killchild) == SIG_ERR ||
+			signal(SIGHUP, killchild) == SIG_ERR) {
+			dropbear_exit("signal() error");
+		}
 	} else
 #endif
 	{
@@ -77,7 +84,7 @@
 		sock_in = sock_out = -1;
 	}
 
-	cli_session(sock_in, sock_out, progress);
+	cli_session(sock_in, sock_out, progress, proxy_cmd_pid);
 
 	/* not reached */
 	return -1;
@@ -132,16 +139,24 @@
 }
 
 #ifdef ENABLE_CLI_PROXYCMD
-static void cli_proxy_cmd(int *sock_in, int *sock_out) {
+static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
 	int ret;
 
 	fill_passwd(cli_opts.own_user);
 
 	ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd,
-			sock_out, sock_in, NULL, NULL);
+			sock_out, sock_in, NULL, pid_out);
 	if (ret == DROPBEAR_FAILURE) {
 		dropbear_exit("Failed running proxy command");
 		*sock_in = *sock_out = -1;
 	}
 }
+
+static void killchild(int signo)
+{
+	kill_proxy_command();
+	if (signo)
+		_exit(1);
+	exit(1);
+}
 #endif /* ENABLE_CLI_PROXYCMD */