From c4291ff47addada77baf93b8e59cef9e653fe07f Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 16 Apr 2022 21:19:15 -0500 Subject: [PATCH] Revert "Don't crash if the log file can't be opened" We don't have a stdout to _dup2() into when run as a service. This reverts commit 16ddcb82d94dd99a8bee8887434acd528411773c. --- GSv6Fwd/GSv6Fwd.cpp | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/GSv6Fwd/GSv6Fwd.cpp b/GSv6Fwd/GSv6Fwd.cpp index 3648338..5da4d1d 100644 --- a/GSv6Fwd/GSv6Fwd.cpp +++ b/GSv6Fwd/GSv6Fwd.cpp @@ -6,8 +6,6 @@ #include #include #include -#include -#include #pragma comment(lib, "ws2_32") #include @@ -847,27 +845,15 @@ void ResetLogFile(bool standaloneExe) ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-old.log", oldLogFilePath, sizeof(oldLogFilePath)); ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-current.log", currentLogFilePath, sizeof(currentLogFilePath)); + // Close the existing stdout handle. This is important because otherwise + // it may still be open as stdout when we try to MoveFileEx below. + fclose(stdout); + // Rotate the current to the old log file MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING); - // Open the new log file - HANDLE newLogHandle = CreateFileA(currentLogFilePath, - GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_DELETE, - nullptr, - CREATE_ALWAYS, - FILE_ATTRIBUTE_NORMAL, - nullptr); - if (newLogHandle == INVALID_HANDLE_VALUE) { - return; - } - - // Convert the HANDLE to an fd (transfers ownership too) - int fd = _open_osfhandle((intptr_t)newLogHandle, _O_WRONLY); - // Redirect stdout to this new file - _dup2(fd, _fileno(stdout)); - _close(fd); + freopen(currentLogFilePath, "w", stdout); } // Print a log header