1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-03-22 14:39:24 -05:00

Remove FLAG_STDERR completely.

Originally, it controlled whether ssh.c should send terminal messages
(such as login and password prompts) to terminal.c or to stderr. But
we've had the from_backend() abstraction for ages now, which even has
an existing flag to indicate that the data is stderr rather than
stdout data; applications which set FLAG_STDERR are precisely those
that link against uxcons or wincons, so from_backend will do the
expected thing anyway with data sent to it with that flag set. So
there's no reason ssh.c can't just unconditionally pass everything
through that, and remove the special case.

FLAG_STDERR was also used by winproxy and uxproxy to decide whether to
capture standard error from a local proxy command, or whether to let
the proxy command send its diagnostics directly to the usual standard
error. On reflection, I think it's better to unconditionally capture
the proxy's stderr, for three reasons. Firstly, it means proxy
diagnostics are prefixed with 'proxy:' so that you can tell them apart
from any other stderr spew (which used to be particularly confusing if
both the main application and the proxy command were instances of
Plink); secondly, proxy diagnostics are now reliably copied to packet
log files along with all the other Event Log entries, even by
command-line tools; and thirdly, this means the option to suppress
proxy command diagnostics after the main session starts will actually
_work_ in the command-line tools, which it previously couldn't.

A more minor structure change is that copying of Event Log messages to
stderr in verbose mode is now done by wincons/uxcons, instead of
centrally in logging.c (since logging.c can now no longer check
FLAG_STDERR to decide whether to do it). The total amount of code to
do this is considerably smaller than the defensive-sounding comment in
logevent.c explaining why I did it the other way instead :-)
This commit is contained in:
Simon Tatham 2018-09-21 16:15:49 +01:00
parent 361efee621
commit e230751853
11 changed files with 31 additions and 77 deletions

View File

@ -219,20 +219,11 @@ void logtraffic(LogContext *ctx, unsigned char c, int logmode)
} }
/* /*
* Log an Event Log entry. Used in SSH packet logging mode; this is * Log an Event Log entry. Used in SSH packet logging mode, to copy
* also as convenient a place as any to put the output of Event Log * the Event Log entries into the same log file as the packet data.
* entries to stderr when a command-line tool is in verbose mode.
* (In particular, this is a better place to put it than in the
* front ends, because it only has to be done once for all
* platforms. Platforms which don't have a meaningful stderr can
* just avoid defining FLAG_STDERR.
*/ */
void log_eventlog(LogContext *ctx, const char *event) void log_eventlog(LogContext *ctx, const char *event)
{ {
if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
fprintf(stderr, "%s\n", event);
fflush(stderr);
}
/* If we don't have a context yet (eg winnet.c init) then skip entirely */ /* If we don't have a context yet (eg winnet.c init) then skip entirely */
if (!ctx) if (!ctx)
return; return;

2
pscp.c
View File

@ -2283,7 +2283,7 @@ int psftp_main(int argc, char *argv[])
default_protocol = PROT_TELNET; default_protocol = PROT_TELNET;
flags = FLAG_STDERR flags = 0
#ifdef FLAG_SYNCAGENT #ifdef FLAG_SYNCAGENT
| FLAG_SYNCAGENT | FLAG_SYNCAGENT
#endif #endif

View File

@ -2872,7 +2872,7 @@ int psftp_main(int argc, char *argv[])
int modeflags = 0; int modeflags = 0;
char *batchfile = NULL; char *batchfile = NULL;
flags = FLAG_STDERR | FLAG_INTERACTIVE flags = FLAG_INTERACTIVE
#ifdef FLAG_SYNCAGENT #ifdef FLAG_SYNCAGENT
| FLAG_SYNCAGENT | FLAG_SYNCAGENT
#endif #endif

View File

@ -520,10 +520,6 @@ extern const char *const appname;
* *
* FLAG_VERBOSE is set when the user requests verbose details. * FLAG_VERBOSE is set when the user requests verbose details.
* *
* FLAG_STDERR is set in command-line applications (which have a
* functioning stderr that it makes sense to write to) and not in
* GUI applications (which don't).
*
* FLAG_INTERACTIVE is set when a full interactive shell session is * FLAG_INTERACTIVE is set when a full interactive shell session is
* being run, _either_ because no remote command has been provided * being run, _either_ because no remote command has been provided
* _or_ because the application is GUI and can't run non- * _or_ because the application is GUI and can't run non-
@ -538,8 +534,7 @@ extern const char *const appname;
* avoid collision. * avoid collision.
*/ */
#define FLAG_VERBOSE 0x0001 #define FLAG_VERBOSE 0x0001
#define FLAG_STDERR 0x0002 #define FLAG_INTERACTIVE 0x0002
#define FLAG_INTERACTIVE 0x0004
GLOBAL int flags; GLOBAL int flags;
/* /*

17
ssh.c
View File

@ -704,21 +704,9 @@ static int ssh_rportcmp_ssh2(void *av, void *bv)
return 0; return 0;
} }
static void c_write_stderr(int trusted, const void *vbuf, int len)
{
const char *buf = (const char *)vbuf;
int i;
for (i = 0; i < len; i++)
if (buf[i] != '\r' && (trusted || buf[i] == '\n' || (buf[i] & 0x60)))
fputc(buf[i], stderr);
}
static void c_write(Ssh ssh, const void *buf, int len) static void c_write(Ssh ssh, const void *buf, int len)
{ {
if (flags & FLAG_STDERR) from_backend(ssh->frontend, 1, buf, len);
c_write_stderr(1, buf, len);
else
from_backend(ssh->frontend, 1, buf, len);
} }
static void c_write_str(Ssh ssh, const char *buf) static void c_write_str(Ssh ssh, const char *buf)
@ -1866,8 +1854,7 @@ static void do_ssh1_login(void *vctx)
{ {
char *userlog = dupprintf("Sent username \"%s\"", ssh->username); char *userlog = dupprintf("Sent username \"%s\"", ssh->username);
logevent(userlog); logevent(userlog);
if (flags & FLAG_INTERACTIVE && if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
(!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) {
c_write_str(ssh, userlog); c_write_str(ssh, userlog);
c_write_str(ssh, "\r\n"); c_write_str(ssh, "\r\n");
} }

View File

@ -413,12 +413,14 @@ void console_provide_logctx(LogContext *logctx)
void logevent(Frontend *frontend, const char *string) void logevent(Frontend *frontend, const char *string)
{ {
struct termios cf; struct termios cf;
if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) if (flags & FLAG_VERBOSE) {
premsg(&cf); premsg(&cf);
fprintf(stderr, "%s\n", string);
fflush(stderr);
postmsg(&cf);
}
if (console_logctx) if (console_logctx)
log_eventlog(console_logctx, string); log_eventlog(console_logctx, string);
if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE))
postmsg(&cf);
} }
/* /*

View File

@ -610,7 +610,7 @@ int main(int argc, char **argv)
bufchain_init(&stderr_data); bufchain_init(&stderr_data);
outgoingeof = EOF_NO; outgoingeof = EOF_NO;
flags = FLAG_STDERR | FLAG_STDERR_TTY; flags = FLAG_STDERR_TTY;
cmdline_tooltype |= cmdline_tooltype |=
(TOOLTYPE_HOST_ARG | (TOOLTYPE_HOST_ARG |
TOOLTYPE_HOST_ARG_CAN_BE_SESSION | TOOLTYPE_HOST_ARG_CAN_BE_SESSION |

View File

@ -299,18 +299,6 @@ Socket platform_new_connection(SockAddr addr, const char *hostname,
if (proxytype == PROXY_CMD) { if (proxytype == PROXY_CMD) {
cmd = format_telnet_command(addr, port, conf); cmd = format_telnet_command(addr, port, conf);
if (flags & FLAG_STDERR) {
/* If we have a sensible stderr, the proxy command can
* send its own standard error there, so we won't
* interfere. */
cmd_err_pipe[0] = cmd_err_pipe[1] = -1;
} else {
/* If we don't have a sensible stderr, we should catch the
* proxy command's standard error to put in our event
* log. */
cmd_err_pipe[0] = cmd_err_pipe[1] = 0;
}
{ {
char *logmsg = dupprintf("Starting local proxy command: %s", cmd); char *logmsg = dupprintf("Starting local proxy command: %s", cmd);
plug_log(plug, 2, NULL, 0, logmsg, 0); plug_log(plug, 2, NULL, 0, logmsg, 0);
@ -323,15 +311,14 @@ Socket platform_new_connection(SockAddr addr, const char *hostname,
*/ */
if (pipe(to_cmd_pipe) < 0 || if (pipe(to_cmd_pipe) < 0 ||
pipe(from_cmd_pipe) < 0 || pipe(from_cmd_pipe) < 0 ||
(cmd_err_pipe[0] == 0 && pipe(cmd_err_pipe) < 0)) { pipe(cmd_err_pipe) < 0) {
ret->error = dupprintf("pipe: %s", strerror(errno)); ret->error = dupprintf("pipe: %s", strerror(errno));
sfree(cmd); sfree(cmd);
return &ret->sockvt; return &ret->sockvt;
} }
cloexec(to_cmd_pipe[1]); cloexec(to_cmd_pipe[1]);
cloexec(from_cmd_pipe[0]); cloexec(from_cmd_pipe[0]);
if (cmd_err_pipe[0] >= 0) cloexec(cmd_err_pipe[0]);
cloexec(cmd_err_pipe[0]);
pid = fork(); pid = fork();
@ -346,10 +333,7 @@ Socket platform_new_connection(SockAddr addr, const char *hostname,
dup2(from_cmd_pipe[1], 1); dup2(from_cmd_pipe[1], 1);
close(to_cmd_pipe[0]); close(to_cmd_pipe[0]);
close(from_cmd_pipe[1]); close(from_cmd_pipe[1]);
if (cmd_err_pipe[0] >= 0) { dup2(cmd_err_pipe[1], 2);
dup2(cmd_err_pipe[1], 2);
close(cmd_err_pipe[1]);
}
noncloexec(0); noncloexec(0);
noncloexec(1); noncloexec(1);
execl("/bin/sh", "sh", "-c", cmd, (void *)NULL); execl("/bin/sh", "sh", "-c", cmd, (void *)NULL);
@ -360,8 +344,7 @@ Socket platform_new_connection(SockAddr addr, const char *hostname,
close(to_cmd_pipe[0]); close(to_cmd_pipe[0]);
close(from_cmd_pipe[1]); close(from_cmd_pipe[1]);
if (cmd_err_pipe[0] >= 0) close(cmd_err_pipe[1]);
close(cmd_err_pipe[1]);
ret->to_cmd = to_cmd_pipe[1]; ret->to_cmd = to_cmd_pipe[1];
ret->from_cmd = from_cmd_pipe[0]; ret->from_cmd = from_cmd_pipe[0];

View File

@ -342,6 +342,10 @@ void console_provide_logctx(LogContext *logctx)
void logevent(Frontend *frontend, const char *string) void logevent(Frontend *frontend, const char *string)
{ {
if (flags & FLAG_VERBOSE) {
fprintf(stderr, "%s\n", string);
fflush(stderr);
}
log_eventlog(console_logctx, string); log_eventlog(console_logctx, string);
} }

View File

@ -302,7 +302,7 @@ int main(int argc, char **argv)
default_protocol = PROT_SSH; default_protocol = PROT_SSH;
default_port = 22; default_port = 22;
flags = FLAG_STDERR; flags = 0;
cmdline_tooltype |= cmdline_tooltype |=
(TOOLTYPE_HOST_ARG | (TOOLTYPE_HOST_ARG |
TOOLTYPE_HOST_ARG_CAN_BE_SESSION | TOOLTYPE_HOST_ARG_CAN_BE_SESSION |

View File

@ -66,23 +66,15 @@ Socket platform_new_connection(SockAddr addr, const char *hostname,
return ret; return ret;
} }
if (flags & FLAG_STDERR) { if (!CreatePipe(&us_from_cmd_err, &cmd_err_to_us, &sa, 0)) {
/* If we have a sensible stderr, the proxy command can send Socket ret = new_error_socket
* its own standard error there, so we won't interfere. */ ("Unable to create pipes for proxy command", plug);
us_from_cmd_err = cmd_err_to_us = NULL; sfree(cmd);
} else { CloseHandle(us_from_cmd);
/* If we don't have a sensible stderr, we should catch the CloseHandle(cmd_to_us);
* proxy command's standard error to put in our event log. */ CloseHandle(us_to_cmd);
if (!CreatePipe(&us_from_cmd_err, &cmd_err_to_us, &sa, 0)) { CloseHandle(cmd_from_us);
Socket ret = new_error_socket return ret;
("Unable to create pipes for proxy command", plug);
sfree(cmd);
CloseHandle(us_from_cmd);
CloseHandle(cmd_to_us);
CloseHandle(us_to_cmd);
CloseHandle(cmd_from_us);
return ret;
}
} }
SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0); SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);