MSWindowsWatchdog checks active desktop without external call to syntool
This commit is contained in:
parent
c16fd089f6
commit
451bd72b30
|
@ -44,7 +44,19 @@ enum {
|
||||||
|
|
||||||
typedef VOID (WINAPI *SendSas)(BOOL asUser);
|
typedef VOID (WINAPI *SendSas)(BOOL asUser);
|
||||||
|
|
||||||
const char g_activeDesktop[] = {"activeDesktop:"};
|
std::string activeDesktopName()
|
||||||
|
{
|
||||||
|
const std::size_t BufferLength = 1024;
|
||||||
|
std::string name;
|
||||||
|
HDESK desk = OpenInputDesktop(0, FALSE, GENERIC_READ);
|
||||||
|
if (desk != NULL) {
|
||||||
|
TCHAR buffer[BufferLength];
|
||||||
|
if (GetUserObjectInformation(desk, UOI_NAME, buffer, BufferLength - 1, NULL) == TRUE)
|
||||||
|
name = buffer;
|
||||||
|
CloseDesktop(desk);
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
MSWindowsWatchdog::MSWindowsWatchdog(
|
MSWindowsWatchdog::MSWindowsWatchdog(
|
||||||
bool daemonized,
|
bool daemonized,
|
||||||
|
@ -64,22 +76,8 @@ MSWindowsWatchdog::MSWindowsWatchdog(
|
||||||
m_processRunning(false),
|
m_processRunning(false),
|
||||||
m_fileLogOutputter(NULL),
|
m_fileLogOutputter(NULL),
|
||||||
m_autoElevated(false),
|
m_autoElevated(false),
|
||||||
m_ready(false),
|
|
||||||
m_daemonized(daemonized)
|
m_daemonized(daemonized)
|
||||||
{
|
{
|
||||||
m_mutex = ARCH->newMutex();
|
|
||||||
m_condVar = ARCH->newCondVar();
|
|
||||||
}
|
|
||||||
|
|
||||||
MSWindowsWatchdog::~MSWindowsWatchdog()
|
|
||||||
{
|
|
||||||
if (m_condVar != NULL) {
|
|
||||||
ARCH->closeCondVar(m_condVar);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_mutex != NULL) {
|
|
||||||
ARCH->closeMutex(m_mutex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -288,12 +286,9 @@ MSWindowsWatchdog::startProcess()
|
||||||
if (!m_daemonized) {
|
if (!m_daemonized) {
|
||||||
createRet = doStartProcessAsSelf(m_command);
|
createRet = doStartProcessAsSelf(m_command);
|
||||||
} else {
|
} else {
|
||||||
SECURITY_ATTRIBUTES sa;
|
m_autoElevated = activeDesktopName() != "Default";
|
||||||
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
|
|
||||||
|
|
||||||
getActiveDesktop(&sa);
|
SECURITY_ATTRIBUTES sa{ 0 };
|
||||||
|
|
||||||
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
|
|
||||||
HANDLE userToken = getUserToken(&sa);
|
HANDLE userToken = getUserToken(&sa);
|
||||||
m_elevateProcess = m_autoElevated ? m_autoElevated : m_elevateProcess;
|
m_elevateProcess = m_autoElevated ? m_autoElevated : m_elevateProcess;
|
||||||
m_autoElevated = false;
|
m_autoElevated = false;
|
||||||
|
@ -444,11 +439,7 @@ MSWindowsWatchdog::outputLoop(void*)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
buffer[bytesRead] = '\0';
|
buffer[bytesRead] = '\0';
|
||||||
|
|
||||||
testOutput(buffer);
|
|
||||||
|
|
||||||
m_ipcLogOutputter.write(kINFO, buffer);
|
m_ipcLogOutputter.write(kINFO, buffer);
|
||||||
|
|
||||||
if (m_fileLogOutputter != NULL) {
|
if (m_fileLogOutputter != NULL) {
|
||||||
m_fileLogOutputter->write(kINFO, buffer);
|
m_fileLogOutputter->write(kINFO, buffer);
|
||||||
}
|
}
|
||||||
|
@ -549,63 +540,3 @@ MSWindowsWatchdog::shutdownExistingProcesses()
|
||||||
CloseHandle(snapshot);
|
CloseHandle(snapshot);
|
||||||
m_processRunning = false;
|
m_processRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
MSWindowsWatchdog::getActiveDesktop(LPSECURITY_ATTRIBUTES security)
|
|
||||||
{
|
|
||||||
String installedDir = ARCH->getInstalledDirectory();
|
|
||||||
if (!installedDir.empty()) {
|
|
||||||
String syntoolCommand;
|
|
||||||
syntoolCommand.append("\"").append(installedDir).append("\\").append("syntool").append("\"");
|
|
||||||
syntoolCommand.append(" --get-active-desktop");
|
|
||||||
|
|
||||||
m_session.updateActiveSession();
|
|
||||||
bool elevateProcess = m_elevateProcess;
|
|
||||||
m_elevateProcess = true;
|
|
||||||
HANDLE userToken = getUserToken(security);
|
|
||||||
m_elevateProcess = elevateProcess;
|
|
||||||
|
|
||||||
BOOL createRet = doStartProcessAsUser(syntoolCommand, userToken, security);
|
|
||||||
|
|
||||||
if (!createRet) {
|
|
||||||
DWORD rc = GetLastError();
|
|
||||||
RevertToSelf();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
LOG((CLOG_DEBUG "launched syntool to check active desktop"));
|
|
||||||
}
|
|
||||||
|
|
||||||
ARCH->lockMutex(m_mutex);
|
|
||||||
int waitTime = 0;
|
|
||||||
while (!m_ready) {
|
|
||||||
if (waitTime >= MAXIMUM_WAIT_TIME) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ARCH->waitCondVar(m_condVar, m_mutex, 1.0);
|
|
||||||
waitTime++;
|
|
||||||
}
|
|
||||||
m_ready = false;
|
|
||||||
ARCH->unlockMutex(m_mutex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
MSWindowsWatchdog::testOutput(String buffer)
|
|
||||||
{
|
|
||||||
// HACK: check standard output seems hacky.
|
|
||||||
size_t i = buffer.find(g_activeDesktop);
|
|
||||||
if (i != String::npos) {
|
|
||||||
size_t s = sizeof(g_activeDesktop);
|
|
||||||
String defaultDesktop("Default");
|
|
||||||
String sub = buffer.substr(i + s - 1, defaultDesktop.size());
|
|
||||||
if (sub != defaultDesktop) {
|
|
||||||
m_autoElevated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
ARCH->lockMutex(m_mutex);
|
|
||||||
m_ready = true;
|
|
||||||
ARCH->broadcastCondVar(m_condVar);
|
|
||||||
ARCH->unlockMutex(m_mutex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -39,7 +39,6 @@ public:
|
||||||
bool autoDetectCommand,
|
bool autoDetectCommand,
|
||||||
IpcServer& ipcServer,
|
IpcServer& ipcServer,
|
||||||
IpcLogOutputter& ipcLogOutputter);
|
IpcLogOutputter& ipcLogOutputter);
|
||||||
virtual ~MSWindowsWatchdog();
|
|
||||||
|
|
||||||
void startAsync();
|
void startAsync();
|
||||||
std::string getCommand() const;
|
std::string getCommand() const;
|
||||||
|
@ -58,9 +57,6 @@ private:
|
||||||
void startProcess();
|
void startProcess();
|
||||||
BOOL doStartProcessAsUser(String& command, HANDLE userToken, LPSECURITY_ATTRIBUTES sa);
|
BOOL doStartProcessAsUser(String& command, HANDLE userToken, LPSECURITY_ATTRIBUTES sa);
|
||||||
BOOL doStartProcessAsSelf(String& command);
|
BOOL doStartProcessAsSelf(String& command);
|
||||||
void sendSas();
|
|
||||||
void getActiveDesktop(LPSECURITY_ATTRIBUTES security);
|
|
||||||
void testOutput(String buffer);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Thread* m_thread;
|
Thread* m_thread;
|
||||||
|
@ -80,9 +76,6 @@ private:
|
||||||
bool m_processRunning;
|
bool m_processRunning;
|
||||||
FileLogOutputter* m_fileLogOutputter;
|
FileLogOutputter* m_fileLogOutputter;
|
||||||
bool m_autoElevated;
|
bool m_autoElevated;
|
||||||
ArchMutex m_mutex;
|
|
||||||
ArchCond m_condVar;
|
|
||||||
bool m_ready;
|
|
||||||
bool m_daemonized;
|
bool m_daemonized;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue