comparison cli-main.c @ 1304:b66a483f3dcb

Improve exit message formatting
author Matt Johnston <matt@ucc.asn.au>
date Mon, 11 Jul 2016 23:09:33 +0800
parents f7d565054e5f
children 2c9dac2d6707
comparison
equal deleted inserted replaced
1303:eed9376a4ad6 1304:b66a483f3dcb
96 return -1; 96 return -1;
97 } 97 }
98 #endif /* DBMULTI stuff */ 98 #endif /* DBMULTI stuff */
99 99
100 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) { 100 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
101 char exitmsg[150];
102 char fullmsg[300];
101 103
102 char fmtbuf[300]; 104 /* Note that exit message must be rendered before session cleanup */
103 char exitmsg[500];
104 105
106 /* Render the formatted exit message */
107 vsnprintf(exitmsg, sizeof(exitmsg), format, param);
108
109 /* Add the prefix depending on session/auth state */
105 if (!sessinitdone) { 110 if (!sessinitdone) {
106 snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s", 111 snprintf(fullmsg, sizeof(fullmsg), "Exited: %s", exitmsg);
107 format);
108 } else { 112 } else {
109 snprintf(fmtbuf, sizeof(fmtbuf), 113 snprintf(fullmsg, sizeof(fullmsg),
110 "Connection to %s@%s:%s exited: %s", 114 "Connection to %s@%s:%s exited: %s",
111 cli_opts.username, cli_opts.remotehost, 115 cli_opts.username, cli_opts.remotehost,
112 cli_opts.remoteport, format); 116 cli_opts.remoteport, exitmsg);
113 } 117 }
114
115 /* Arguments to the exit printout may be unsafe to use after session_cleanup() */
116 vsnprintf(exitmsg, sizeof(exitmsg), fmtbuf, param);
117 118
118 /* Do the cleanup first, since then the terminal will be reset */ 119 /* Do the cleanup first, since then the terminal will be reset */
119 session_cleanup(); 120 session_cleanup();
120 /* Avoid printing onwards from terminal cruft */ 121 /* Avoid printing onwards from terminal cruft */
121 fprintf(stderr, "\n"); 122 fprintf(stderr, "\n");
122 123
123 dropbear_log(LOG_INFO, "%s", exitmsg);; 124 dropbear_log(LOG_INFO, "%s", fullmsg);
124 exit(exitcode); 125 exit(exitcode);
125 } 126 }
126 127
127 static void cli_dropbear_log(int priority, 128 static void cli_dropbear_log(int priority,
128 const char* format, va_list param) { 129 const char* format, va_list param) {