diff --git a/scripts.c b/scripts.c index e8dae80..7f3b9f4 100644 --- a/scripts.c +++ b/scripts.c @@ -59,16 +59,31 @@ RunPreconnectScript(connection_t *c) if (_tstat(cmdline, &st) == -1) return; + // Create the filename of the logfile + TCHAR script_log_filename[MAX_PATH]; + _sntprintf_0(script_log_filename, _T("%s\\%s_pre.log"), o.log_dir, c->config_name); + + // Create the log file + SECURITY_ATTRIBUTES sa; + CLEAR(sa); + sa.nLength = sizeof(SECURITY_ATTRIBUTES); + sa.lpSecurityDescriptor = NULL; + sa.bInheritHandle = TRUE; + + HANDLE logfile_handle = CreateFile(script_log_filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, + &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + CLEAR(si); CLEAR(pi); /* fill in STARTUPINFO struct */ GetStartupInfo(&si); si.cb = sizeof(si); - si.dwFlags = 0; + si.dwFlags = STARTF_USESTDHANDLES; si.wShowWindow = SW_SHOWDEFAULT; si.hStdInput = NULL; - si.hStdOutput = NULL; + si.hStdOutput = logfile_handle; + si.hStdError = logfile_handle; /* Preconnect script is run too early for config env to be available * so we use the default process env here. @@ -92,6 +107,8 @@ RunPreconnectScript(connection_t *c) out: CloseHandle(pi.hThread); CloseHandle(pi.hProcess); + if (logfile_handle != NULL) + CloseHandle(logfile_handle); }