[host] windows: move log path to %ProgramData%\Looking Glass (host)

Instead of using %windir%\Temp, which is not accessible by default and
contains a lot of unrelated files, as the location for our log files,
this commit moves it to %ProgramData%\Looking Glass (host), which will
be a dedicated directory just for the LG host log files. This applies
to both the host application logs and the service logs.

Also, we now switched to using PathCombineA from shlwapi.dll instead
of using snprintf, which greatly simplifies the code. PathCombineA
guarantees that the path would not overflow a buffer of MAX_PATH.
This commit is contained in:
Quantum 2021-01-28 20:39:05 -05:00 committed by Geoffrey McRae
parent fb916cbac1
commit 1761ea2b9b
4 changed files with 39 additions and 11 deletions

View File

@ -32,6 +32,7 @@ target_link_libraries(platform_Windows
userenv
wtsapi32
psapi
shlwapi
)
target_include_directories(platform_Windows

View File

@ -23,6 +23,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <windows.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <fcntl.h>
#include "interface/platform.h"
@ -34,6 +35,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#define ID_MENU_SHOW_LOG 3000
#define ID_MENU_EXIT 3001
#define LOG_NAME "looking-glass-host.txt"
struct AppState
{
@ -44,6 +46,7 @@ struct AppState
char ** argv;
char executable[MAX_PATH + 1];
char systemLogDir[MAX_PATH];
HWND messageWnd;
NOTIFYICONDATA iconData;
UINT trayRestartMsg;
@ -166,6 +169,29 @@ static BOOL WINAPI CtrlHandler(DWORD dwCtrlType)
return FALSE;
}
const char *getSystemLogDirectory(void)
{
return app.systemLogDir;
}
static void populateSystemLogDirectory()
{
char programData[MAX_PATH];
if (GetEnvironmentVariableA("ProgramData", programData, sizeof(programData)) &&
PathIsDirectoryA(programData))
{
if (!PathCombineA(app.systemLogDir, programData, "Looking Glass (host)"))
goto fail;
if (!PathIsDirectoryA(app.systemLogDir) && !CreateDirectoryA(app.systemLogDir, NULL))
goto fail;
return;
}
fail:
strcpy(app.systemLogDir, "");
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// convert the command line to the standard argc and argv
@ -180,6 +206,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
LocalFree(wargv);
GetModuleFileName(NULL, app.executable, sizeof(app.executable));
populateSystemLogDirectory();
if (HandleService(app.argc, app.argv))
return LG_HOST_EXIT_FAILED;
@ -201,11 +229,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
int result = 0;
app.hInst = hInstance;
char tempPath[MAX_PATH+1];
GetTempPathA(sizeof(tempPath), tempPath);
int len = snprintf(NULL, 0, "%slooking-glass-host.txt", tempPath);
char * logFilePath = malloc(len + 1);
sprintf(logFilePath, "%slooking-glass-host.txt", tempPath);
char logFilePath[MAX_PATH];
if (!PathCombineA(logFilePath, app.systemLogDir, LOG_NAME))
strcpy(logFilePath, LOG_NAME);
struct Option options[] =
{
@ -220,7 +246,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
};
option_register(options);
free(logFilePath);
// setup a handler for ctrl+c
SetConsoleCtrlHandler(CtrlHandler, TRUE);

View File

@ -30,4 +30,5 @@ struct MSG_CALL_FUNCTION
LPARAM lParam;
};
const char *getSystemLogDirectory(void);
LRESULT sendAppMessage(UINT Msg, WPARAM wParam, LPARAM lParam);

View File

@ -20,6 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "interface/platform.h"
#include "common/ivshmem.h"
#include "service.h"
#include "platform.h"
#include <stdio.h>
#include <stdbool.h>
@ -29,6 +30,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <time.h>
#include <windows.h>
#include <shlwapi.h>
#include <winsvc.h>
#include <psapi.h>
#include <sddl.h>
@ -37,6 +39,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#define SVCNAME "Looking Glass (host)"
#define SVC_ERROR ((DWORD)0xC0020001L)
#define LOG_NAME "looking-glass-host-service.txt"
/*
* Windows 10 provides this API via kernel32.dll as well as advapi32.dll and
@ -112,11 +115,9 @@ static bool setupAPI(void)
static void setupLogging(void)
{
char tempPath[MAX_PATH+1];
GetTempPathA(sizeof(tempPath), tempPath);
int len = snprintf(NULL, 0, "%slooking-glass-host-service.txt", tempPath);
char * logFilePath = malloc(len + 1);
sprintf(logFilePath, "%slooking-glass-host-service.txt", tempPath);
char logFilePath[MAX_PATH];
if (!PathCombineA(logFilePath, getSystemLogDirectory(), LOG_NAME))
strcpy(logFilePath, LOG_NAME);
service.logFile = fopen(logFilePath, "a+");
setbuf(service.logFile, NULL);
doLog("Startup\n");