comparison 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
comparison
equal deleted inserted replaced
1207:81063d78fe69 1209:ab7d9c12caa7
34 34
35 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN; 35 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN;
36 static void cli_dropbear_log(int priority, const char* format, va_list param); 36 static void cli_dropbear_log(int priority, const char* format, va_list param);
37 37
38 #ifdef ENABLE_CLI_PROXYCMD 38 #ifdef ENABLE_CLI_PROXYCMD
39 static void cli_proxy_cmd(int *sock_in, int *sock_out); 39 static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out);
40 static void killchild(int signo);
40 #endif 41 #endif
41 42
42 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI) 43 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
43 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI) 44 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
44 int cli_main(int argc, char ** argv) { 45 int cli_main(int argc, char ** argv) {
64 65
65 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { 66 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
66 dropbear_exit("signal() error"); 67 dropbear_exit("signal() error");
67 } 68 }
68 69
70 pid_t proxy_cmd_pid = 0;
69 #ifdef ENABLE_CLI_PROXYCMD 71 #ifdef ENABLE_CLI_PROXYCMD
70 if (cli_opts.proxycmd) { 72 if (cli_opts.proxycmd) {
71 cli_proxy_cmd(&sock_in, &sock_out); 73 cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
72 m_free(cli_opts.proxycmd); 74 m_free(cli_opts.proxycmd);
75 if (signal(SIGINT, killchild) == SIG_ERR ||
76 signal(SIGTERM, killchild) == SIG_ERR ||
77 signal(SIGHUP, killchild) == SIG_ERR) {
78 dropbear_exit("signal() error");
79 }
73 } else 80 } else
74 #endif 81 #endif
75 { 82 {
76 progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport, cli_connected, &ses); 83 progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport, cli_connected, &ses);
77 sock_in = sock_out = -1; 84 sock_in = sock_out = -1;
78 } 85 }
79 86
80 cli_session(sock_in, sock_out, progress); 87 cli_session(sock_in, sock_out, progress, proxy_cmd_pid);
81 88
82 /* not reached */ 89 /* not reached */
83 return -1; 90 return -1;
84 } 91 }
85 #endif /* DBMULTI stuff */ 92 #endif /* DBMULTI stuff */
130 run_shell_command(cmd, ses.maxfd, usershell); 137 run_shell_command(cmd, ses.maxfd, usershell);
131 dropbear_exit("Failed to run '%s'\n", cmd); 138 dropbear_exit("Failed to run '%s'\n", cmd);
132 } 139 }
133 140
134 #ifdef ENABLE_CLI_PROXYCMD 141 #ifdef ENABLE_CLI_PROXYCMD
135 static void cli_proxy_cmd(int *sock_in, int *sock_out) { 142 static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
136 int ret; 143 int ret;
137 144
138 fill_passwd(cli_opts.own_user); 145 fill_passwd(cli_opts.own_user);
139 146
140 ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd, 147 ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd,
141 sock_out, sock_in, NULL, NULL); 148 sock_out, sock_in, NULL, pid_out);
142 if (ret == DROPBEAR_FAILURE) { 149 if (ret == DROPBEAR_FAILURE) {
143 dropbear_exit("Failed running proxy command"); 150 dropbear_exit("Failed running proxy command");
144 *sock_in = *sock_out = -1; 151 *sock_in = *sock_out = -1;
145 } 152 }
146 } 153 }
154
155 static void killchild(int signo)
156 {
157 kill_proxy_command();
158 if (signo)
159 _exit(1);
160 exit(1);
161 }
147 #endif /* ENABLE_CLI_PROXYCMD */ 162 #endif /* ENABLE_CLI_PROXYCMD */