Don't redirect stdout for standalone exe invocation

This commit is contained in:
Cameron Gutman 2020-09-07 10:30:44 -07:00
parent 851eac0e72
commit f7de095417

View File

@ -833,25 +833,28 @@ void UpdatePcpPinholes()
free(addresses); free(addresses);
} }
void ResetLogFile() void ResetLogFile(bool standaloneExe)
{ {
char oldLogFilePath[MAX_PATH + 1];
char currentLogFilePath[MAX_PATH + 1];
char timeString[MAX_PATH + 1] = {}; char timeString[MAX_PATH + 1] = {};
SYSTEMTIME time; SYSTEMTIME time;
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-old.log", oldLogFilePath, sizeof(oldLogFilePath)); if (!standaloneExe) {
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-current.log", currentLogFilePath, sizeof(currentLogFilePath)); char oldLogFilePath[MAX_PATH + 1];
char currentLogFilePath[MAX_PATH + 1];
// Close the existing stdout handle. This is important because otherwise ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-old.log", oldLogFilePath, sizeof(oldLogFilePath));
// it may still be open as stdout when we try to MoveFileEx below. ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-current.log", currentLogFilePath, sizeof(currentLogFilePath));
fclose(stdout);
// Rotate the current to the old log file // Close the existing stdout handle. This is important because otherwise
MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING); // it may still be open as stdout when we try to MoveFileEx below.
fclose(stdout);
// Redirect stdout to this new file // Rotate the current to the old log file
freopen(currentLogFilePath, "w", stdout); MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING);
// Redirect stdout to this new file
freopen(currentLogFilePath, "w", stdout);
}
// Print a log header // Print a log header
printf("IPv6 Forwarder for GameStream v" VER_VERSION_STR "\n"); printf("IPv6 Forwarder for GameStream v" VER_VERSION_STR "\n");
@ -862,12 +865,12 @@ void ResetLogFile()
printf("The current UTC time is: %s\n", timeString); printf("The current UTC time is: %s\n", timeString);
} }
int Run(void) int Run(bool standaloneExe)
{ {
int err; int err;
WSADATA data; WSADATA data;
ResetLogFile(); ResetLogFile(standaloneExe);
HANDLE ifaceChangeEvent = CreateEvent(nullptr, true, false, nullptr); HANDLE ifaceChangeEvent = CreateEvent(nullptr, true, false, nullptr);
@ -912,7 +915,7 @@ int Run(void)
break; break;
} }
ResetLogFile(); ResetLogFile(standaloneExe);
} }
return 0; return 0;
@ -964,7 +967,7 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
SetServiceStatus(ServiceStatusHandle, &ServiceStatus); SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
// Start the relay // Start the relay
err = Run(); err = Run(false);
if (err != 0) { if (err != 0) {
ServiceStatus.dwCurrentState = SERVICE_STOPPED; ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = err; ServiceStatus.dwWin32ExitCode = err;
@ -982,7 +985,7 @@ static const SERVICE_TABLE_ENTRY ServiceTable[] = {
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
if (argc == 2 && !strcmp(argv[1], "exe")) { if (argc == 2 && !strcmp(argv[1], "exe")) {
Run(); Run(true);
return 0; return 0;
} }