feat(daemon): Print warning when not elevated

This commit is contained in:
Nick Bolton
2025-03-04 14:41:46 +00:00
parent cd0aa6496a
commit 3683db0db9
3 changed files with 37 additions and 0 deletions

View File

@ -68,6 +68,13 @@ int main(int argc, char **argv)
LOG_DEBUG("log level: %s", logLevel.c_str());
}
#if SYSAPI_WIN32
// Show warning if not running as admin as daemon will behave differently.
if (!ArchMiscWindows::isProcessElevated()) {
LOG_WARN("not running as admin, some features may not work");
}
#endif
switch (initResult) {
using enum DaemonApp::InitResult;

View File

@ -604,3 +604,29 @@ void ArchMiscWindows::guardRuntimeVersion() // NOSONAR - `noreturn` is not avail
exit(1);
}
}
bool ArchMiscWindows::isProcessElevated()
{
LOG_DEBUG("checking if process is elevated");
HANDLE hToken = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
throw XArch(new XArchEvalWindows());
}
TOKEN_ELEVATION elevation;
try {
DWORD dwSize = sizeof(TOKEN_ELEVATION);
if (!GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &dwSize)) {
throw XArch(new XArchEvalWindows());
}
} catch (...) {
CloseHandle(hToken);
throw;
}
const auto isElevated = elevation.TokenIsElevated;
LOG_DEBUG("process is %s", isElevated ? "elevated" : "not elevated");
return isElevated;
}

View File

@ -169,8 +169,12 @@ public:
//! Saves the window instance for later use.
static void setInstanceWin32(HINSTANCE instance);
//! Get the name of the active input desktop.
static std::string getActiveDesktopName();
//! Returns true if the process is running with elevated privileges (i.e. as admin).
static bool isProcessElevated();
private:
//! Open and return a registry key, closing the parent key
static HKEY openKey(HKEY parent, const TCHAR *child, bool create);