1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 19:12:48 -05:00

Refactor the LogContext type.

LogContext is now the owner of the logevent() function that back ends
and so forth are constantly calling. Previously, logevent was owned by
the Frontend, which would store the message into its list for the GUI
Event Log dialog (or print it to standard error, or whatever) and then
pass it _back_ to LogContext to write to the currently open log file.
Now it's the other way round: LogContext gets the message from the
back end first, writes it to its log file if it feels so inclined, and
communicates it back to the front end.

This means that lots of parts of the back end system no longer need to
have a pointer to a full-on Frontend; the only thing they needed it
for was logging, so now they just have a LogContext (which many of
them had to have anyway, e.g. for logging SSH packets or session
traffic).

LogContext itself also doesn't get a full Frontend pointer any more:
it now talks back to the front end via a little vtable of its own
called LogPolicy, which contains the method that passes Event Log
entries through, the old askappend() function that decides whether to
truncate a pre-existing log file, and an emergency function for
printing an especially prominent message if the log file can't be
created. One minor nice effect of this is that console and GUI apps
can implement that last function subtly differently, so that Unix
console apps can write it with a plain \n instead of the \r\n
(harmless but inelegant) that the old centralised implementation
generated.

One other consequence of this is that the LogContext has to be
provided to backend_init() so that it's available to backends from the
instant of creation, rather than being provided via a separate API
call a couple of function calls later, because backends have typically
started doing things that need logging (like making network
connections) before the call to backend_provide_logctx. Fortunately,
there's no case in the whole code base where we don't already have
logctx by the time we make a backend (so I don't actually remember why
I ever delayed providing one). So that shortens the backend API by one
function, which is always nice.

While I'm tidying up, I've also moved the printf-style logeventf() and
the handy logevent_and_free() into logging.c, instead of having copies
of them scattered around other places. This has also let me remove
some stub functions from a couple of outlying applications like
Pageant. Finally, I've removed the pointless "_tag" at the end of
LogContext's official struct name.
This commit is contained in:
Simon Tatham
2018-10-10 19:26:18 +01:00
parent 1b67ec2963
commit ad0c502cef
47 changed files with 363 additions and 440 deletions

View File

@ -13,8 +13,6 @@
int console_batch_mode = FALSE;
static void *console_logctx = NULL;
/*
* Clean up and exit.
*/
@ -247,8 +245,9 @@ int askhk(Frontend *frontend, const char *algname, const char *betteralgs,
* Ask whether to wipe a session log file before writing to it.
* Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
*/
int askappend(Frontend *frontend, Filename *filename,
void (*callback)(void *ctx, int result), void *ctx)
static int console_askappend(LogPolicy *lp, Filename *filename,
void (*callback)(void *ctx, int result),
void *ctx)
{
HANDLE hin;
DWORD savemode, i;
@ -335,18 +334,20 @@ void pgp_fingerprints(void)
" " PGP_PREV_MASTER_KEY_FP "\n", stdout);
}
void console_provide_logctx(LogContext *logctx)
static void console_logging_error(LogPolicy *lp, const char *string)
{
console_logctx = logctx;
/* Ordinary Event Log entries are displayed in the same way as
* logging errors, but only in verbose mode */
fprintf(stderr, "%s\n", string);
fflush(stderr);
}
void logevent(Frontend *frontend, const char *string)
static void console_eventlog(LogPolicy *lp, const char *string)
{
if (flags & FLAG_VERBOSE) {
fprintf(stderr, "%s\n", string);
fflush(stderr);
}
log_eventlog(console_logctx, string);
/* Ordinary Event Log entries are displayed in the same way as
* logging errors, but only in verbose mode */
if (flags & FLAG_VERBOSE)
console_logging_error(lp, string);
}
static void console_data_untrusted(HANDLE hout, const char *data, int len)
@ -486,3 +487,10 @@ void frontend_keypress(Frontend *frontend)
*/
return;
}
static const LogPolicyVtable default_logpolicy_vt = {
console_eventlog,
console_askappend,
console_logging_error,
};
LogPolicy default_logpolicy[1] = {{ &default_logpolicy_vt }};

View File

@ -761,14 +761,12 @@ int do_reconfig(HWND hwnd, int protcfginfo)
return ret;
}
void logevent(Frontend *frontend, const char *string)
static void win_gui_eventlog(LogPolicy *lp, const char *string)
{
char timebuf[40];
char **location;
struct tm tm;
log_eventlog(logctx, string);
tm=ltime();
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
@ -798,6 +796,14 @@ void logevent(Frontend *frontend, const char *string)
}
}
static void win_gui_logging_error(LogPolicy *lp, const char *event)
{
/* Send 'can't open log file' errors to the terminal window.
* (Marked as stderr, although terminal.c won't care.) */
from_backend(NULL, 1, event, strlen(event));
from_backend(NULL, 1, "\r\n", 2);
}
void showeventlog(HWND hwnd)
{
if (!logbox) {
@ -953,8 +959,9 @@ int askhk(Frontend *frontend, const char *algname, const char *betteralgs,
* Ask whether to wipe a session log file before writing to it.
* Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
*/
int askappend(Frontend *frontend, Filename *filename,
void (*callback)(void *ctx, int result), void *ctx)
static int win_gui_askappend(LogPolicy *lp, Filename *filename,
void (*callback)(void *ctx, int result),
void *ctx)
{
static const char msgtemplate[] =
"The session log file \"%.*s\" already exists.\n"
@ -986,6 +993,13 @@ int askappend(Frontend *frontend, Filename *filename,
return 0;
}
static const LogPolicyVtable default_logpolicy_vt = {
win_gui_eventlog,
win_gui_askappend,
win_gui_logging_error,
};
LogPolicy default_logpolicy[1] = {{ &default_logpolicy_vt }};
/*
* Warn about the obsolescent key file format.
*

View File

@ -266,13 +266,12 @@ static void start_backend(void)
cleanup_exit(1);
}
error = backend_init(vt, NULL, &backend, conf,
error = backend_init(vt, NULL, &backend, logctx, conf,
conf_get_str(conf, CONF_host),
conf_get_int(conf, CONF_port),
&realhost,
conf_get_int(conf, CONF_tcp_nodelay),
conf_get_int(conf, CONF_tcp_keepalives));
backend_provide_logctx(backend, logctx);
if (error) {
char *str = dupprintf("%s Error", appname);
sprintf(msg, "Unable to open connection to\n"
@ -635,7 +634,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
*/
term = term_init(conf, &ucsdata, NULL);
setup_clipboards(term, conf);
logctx = log_init(NULL, conf);
logctx = log_init(default_logpolicy, conf);
term_provide_logctx(term, logctx);
term_size(term, conf_get_int(conf, CONF_height),
conf_get_int(conf, CONF_width),
@ -743,7 +742,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
}
if (restricted_acl) {
logevent(NULL, "Running with restricted process ACL");
lp_eventlog(default_logpolicy, "Running with restricted process ACL");
}
start_backend();
@ -2162,7 +2161,8 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
break;
case IDM_RESTART:
if (!backend) {
logevent(NULL, "----- Session restarted -----");
lp_eventlog(default_logpolicy,
"----- Session restarted -----");
term_pwron(term, FALSE);
start_backend();
}

View File

@ -220,13 +220,6 @@ int sk_startup(int hi, int lo)
return FALSE;
}
#ifdef NET_SETUP_DIAGNOSTICS
{
char buf[80];
sprintf(buf, "Using WinSock %d.%d", hi, lo);
logevent(NULL, buf);
}
#endif
return TRUE;
}
@ -252,9 +245,6 @@ void sk_init(void)
#ifndef NO_IPV6
/* Check if we have getaddrinfo in Winsock */
if (GetProcAddress(winsock_module, "getaddrinfo") != NULL) {
#ifdef NET_SETUP_DIAGNOSTICS
logevent(NULL, "Native WinSock IPv6 support detected");
#endif
GET_WINDOWS_FUNCTION(winsock_module, getaddrinfo);
GET_WINDOWS_FUNCTION(winsock_module, freeaddrinfo);
GET_WINDOWS_FUNCTION(winsock_module, getnameinfo);
@ -266,25 +256,15 @@ void sk_init(void)
/* Fall back to wship6.dll for Windows 2000 */
wship6_module = load_system32_dll("wship6.dll");
if (wship6_module) {
#ifdef NET_SETUP_DIAGNOSTICS
logevent(NULL, "WSH IPv6 support detected");
#endif
GET_WINDOWS_FUNCTION(wship6_module, getaddrinfo);
GET_WINDOWS_FUNCTION(wship6_module, freeaddrinfo);
GET_WINDOWS_FUNCTION(wship6_module, getnameinfo);
/* See comment above about type check */
GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, gai_strerror);
} else {
#ifdef NET_SETUP_DIAGNOSTICS
logevent(NULL, "No IPv6 support detected");
#endif
}
}
GET_WINDOWS_FUNCTION(winsock2_module, WSAAddressToStringA);
#else
#ifdef NET_SETUP_DIAGNOSTICS
logevent(NULL, "PuTTY was built without IPv6 support");
#endif
#endif
GET_WINDOWS_FUNCTION(winsock_module, WSAAsyncSelect);
@ -559,9 +539,6 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname,
*/
if (p_getaddrinfo) {
struct addrinfo hints;
#ifdef NET_SETUP_DIAGNOSTICS
logevent(NULL, "Using getaddrinfo() for resolving");
#endif
memset(&hints, 0, sizeof(hints));
hints.ai_family = hint_family;
hints.ai_flags = AI_CANONNAME;
@ -576,9 +553,6 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname,
} else
#endif
{
#ifdef NET_SETUP_DIAGNOSTICS
logevent(NULL, "Using gethostbyname() for resolving");
#endif
/*
* Otherwise use the IPv4-only gethostbyname...
* (NOTE: we don't use gethostbyname as a fallback!)
@ -796,7 +770,7 @@ static int ipv4_is_local_addr(struct in_addr addr)
&retbytes, NULL, NULL) == 0)
n_local_interfaces = retbytes / sizeof(INTERFACE_INFO);
else
logevent(NULL, "Unable to get list of local IP addresses");
n_local_interfaces = -1;
}
if (n_local_interfaces > 0) {
int i;

View File

@ -61,7 +61,6 @@ void nonfatal(const char *fmt, ...)
}
/* Stubs needed to link against misc.c */
void logevent(Frontend *frontend, const char *msg) { assert(0); }
void queue_idempotent_callback(IdempotentCallback *ic) { assert(0); }
/* ----------------------------------------------------------------------

View File

@ -114,7 +114,6 @@ static void unmungestr(char *in, char *out, int outlen)
}
/* Stubs needed to link against misc.c */
void logevent(Frontend *frontend, const char *msg) { assert(0); }
void queue_idempotent_callback(IdempotentCallback *ic) { assert(0); }
static int has_security;

View File

@ -446,8 +446,7 @@ int main(int argc, char **argv)
!conf_get_str_nthstrkey(conf, CONF_portfwd, 0))
conf_set_int(conf, CONF_ssh_simple, TRUE);
logctx = log_init(NULL, conf);
console_provide_logctx(logctx);
logctx = log_init(default_logpolicy, conf);
if (just_test_share_exists) {
if (!vt->test_for_upstream) {
@ -463,7 +462,7 @@ int main(int argc, char **argv)
}
if (restricted_acl) {
logevent(NULL, "Running with restricted process ACL");
lp_eventlog(default_logpolicy, "Running with restricted process ACL");
}
/*
@ -477,7 +476,7 @@ int main(int argc, char **argv)
int nodelay = conf_get_int(conf, CONF_tcp_nodelay) &&
(GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
error = backend_init(vt, NULL, &backend, conf,
error = backend_init(vt, NULL, &backend, logctx, conf,
conf_get_str(conf, CONF_host),
conf_get_int(conf, CONF_port),
&realhost, nodelay,
@ -486,7 +485,6 @@ int main(int argc, char **argv)
fprintf(stderr, "Unable to open connection:\n%s", error);
return 1;
}
backend_provide_logctx(backend, logctx);
sfree(realhost);
}

View File

@ -15,6 +15,7 @@ struct Serial {
HANDLE port;
struct handle *out, *in;
Frontend *frontend;
LogContext *logctx;
int bufsize;
long clearbreak_time;
int break_in_progress;
@ -61,7 +62,7 @@ static int serial_gotdata(struct handle *h, void *data, int len)
notify_remote_exit(serial->frontend);
logevent(serial->frontend, error_msg);
logevent(serial->logctx, error_msg);
connection_fatal(serial->frontend, "%s", error_msg);
@ -81,7 +82,7 @@ static void serial_sentdata(struct handle *h, int new_backlog)
notify_remote_exit(serial->frontend);
logevent(serial->frontend, error_msg);
logevent(serial->logctx, error_msg);
connection_fatal(serial->frontend, "%s", error_msg);
} else {
@ -101,7 +102,6 @@ static const char *serial_configure(Serial *serial, HANDLE serport, Conf *conf)
* device instead of a serial port.
*/
if (GetCommState(serport, &dcb)) {
char *msg;
const char *str;
/*
@ -124,14 +124,10 @@ static const char *serial_configure(Serial *serial, HANDLE serport, Conf *conf)
* Configurable parameters.
*/
dcb.BaudRate = conf_get_int(conf, CONF_serspeed);
msg = dupprintf("Configuring baud rate %lu", dcb.BaudRate);
logevent(serial->frontend, msg);
sfree(msg);
logeventf(serial->logctx, "Configuring baud rate %lu", dcb.BaudRate);
dcb.ByteSize = conf_get_int(conf, CONF_serdatabits);
msg = dupprintf("Configuring %u data bits", dcb.ByteSize);
logevent(serial->frontend, msg);
sfree(msg);
logeventf(serial->logctx, "Configuring %u data bits", dcb.ByteSize);
switch (conf_get_int(conf, CONF_serstopbits)) {
case 2: dcb.StopBits = ONESTOPBIT; str = "1"; break;
@ -139,9 +135,7 @@ static const char *serial_configure(Serial *serial, HANDLE serport, Conf *conf)
case 4: dcb.StopBits = TWOSTOPBITS; str = "2"; break;
default: return "Invalid number of stop bits (need 1, 1.5 or 2)";
}
msg = dupprintf("Configuring %s data bits", str);
logevent(serial->frontend, msg);
sfree(msg);
logeventf(serial->logctx, "Configuring %s data bits", str);
switch (conf_get_int(conf, CONF_serparity)) {
case SER_PAR_NONE: dcb.Parity = NOPARITY; str = "no"; break;
@ -150,9 +144,7 @@ static const char *serial_configure(Serial *serial, HANDLE serport, Conf *conf)
case SER_PAR_MARK: dcb.Parity = MARKPARITY; str = "mark"; break;
case SER_PAR_SPACE: dcb.Parity = SPACEPARITY; str = "space"; break;
}
msg = dupprintf("Configuring %s parity", str);
logevent(serial->frontend, msg);
sfree(msg);
logeventf(serial->logctx, "Configuring %s parity", str);
switch (conf_get_int(conf, CONF_serflow)) {
case SER_FLOW_NONE:
@ -173,9 +165,7 @@ static const char *serial_configure(Serial *serial, HANDLE serport, Conf *conf)
str = "DSR/DTR";
break;
}
msg = dupprintf("Configuring %s flow control", str);
logevent(serial->frontend, msg);
sfree(msg);
logeventf(serial->logctx, "Configuring %s flow control", str);
if (!SetCommState(serport, &dcb))
return "Unable to configure serial port";
@ -201,7 +191,8 @@ static const char *serial_configure(Serial *serial, HANDLE serport, Conf *conf)
* freed by the caller.
*/
static const char *serial_init(Frontend *frontend, Backend **backend_handle,
Conf *conf, const char *host, int port,
LogContext *logctx, Conf *conf,
const char *host, int port,
char **realhost, int nodelay, int keepalive)
{
Serial *serial;
@ -218,13 +209,10 @@ static const char *serial_init(Frontend *frontend, Backend **backend_handle,
*backend_handle = &serial->backend;
serial->frontend = frontend;
serial->logctx = logctx;
serline = conf_get_str(conf, CONF_serline);
{
char *msg = dupprintf("Opening serial device %s", serline);
logevent(serial->frontend, msg);
sfree(msg);
}
logeventf(serial->logctx, "Opening serial device %s", serline);
{
/*
@ -342,7 +330,7 @@ static void serbreak_timer(void *ctx, unsigned long now)
if (now == serial->clearbreak_time && serial->port) {
ClearCommBreak(serial->port);
serial->break_in_progress = FALSE;
logevent(serial->frontend, "Finished serial break");
logevent(serial->logctx, "Finished serial break");
}
}
@ -354,7 +342,7 @@ static void serial_special(Backend *be, SessionSpecialCode code, int arg)
Serial *serial = container_of(be, Serial, backend);
if (serial->port && code == SS_BRK) {
logevent(serial->frontend, "Starting serial break at user request");
logevent(serial->logctx, "Starting serial break at user request");
SetCommBreak(serial->port);
/*
* To send a serial break on Windows, we call SetCommBreak
@ -417,11 +405,6 @@ static void serial_provide_ldisc(Backend *be, Ldisc *ldisc)
/* This is a stub. */
}
static void serial_provide_logctx(Backend *be, LogContext *logctx)
{
/* This is a stub. */
}
static int serial_exitcode(Backend *be)
{
Serial *serial = container_of(be, Serial, backend);
@ -454,7 +437,6 @@ const struct BackendVtable serial_backend = {
serial_sendok,
serial_ldisc,
serial_provide_ldisc,
serial_provide_logctx,
serial_unthrottle,
serial_cfg_info,
NULL /* test_for_upstream */,

View File

@ -751,7 +751,7 @@ char *ssh_sftp_get_cmdline(const char *prompt, int no_fds_ok)
void platform_psftp_pre_conn_setup(void)
{
if (restricted_acl) {
logevent(NULL, "Running with restricted process ACL");
lp_eventlog(default_logpolicy, "Running with restricted process ACL");
}
}