2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-02-10 20:27:49 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2025 Symless Ltd.
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
#include "deskflow/XDeskflow.h"
|
2025-02-13 17:37:58 +00:00
|
|
|
#include "platform/MSWindowsProcess.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "platform/MSWindowsSession.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
2025-02-13 17:37:58 +00:00
|
|
|
#include <memory>
|
2025-03-03 11:03:21 +00:00
|
|
|
#include <optional>
|
2025-02-13 17:37:58 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
class Thread;
|
|
|
|
|
class FileLogOutputter;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
class MSWindowsWatchdog
|
|
|
|
|
{
|
2025-03-03 11:03:21 +00:00
|
|
|
enum class ProcessState
|
|
|
|
|
{
|
|
|
|
|
Idle,
|
|
|
|
|
StartScheduled,
|
|
|
|
|
StartPending,
|
|
|
|
|
StopPending,
|
|
|
|
|
Running
|
|
|
|
|
};
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2025-03-03 11:15:08 +00:00
|
|
|
MSWindowsWatchdog(bool foreground);
|
2025-03-03 11:03:21 +00:00
|
|
|
~MSWindowsWatchdog() = default;
|
2013-10-10 16:06:13 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void startAsync();
|
2025-03-03 11:01:06 +00:00
|
|
|
void setProcessConfig(const std::string &command, bool elevate);
|
2024-07-02 19:07:06 +00:00
|
|
|
void stop();
|
2025-02-13 17:37:58 +00:00
|
|
|
bool isProcessRunning();
|
2024-07-02 19:07:06 +00:00
|
|
|
void setFileLogOutputter(FileLogOutputter *outputter);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
void mainLoop(void *);
|
|
|
|
|
void outputLoop(void *);
|
|
|
|
|
void shutdownExistingProcesses();
|
|
|
|
|
HANDLE duplicateProcessToken(HANDLE process, LPSECURITY_ATTRIBUTES security);
|
2025-02-13 17:37:58 +00:00
|
|
|
HANDLE getUserToken(LPSECURITY_ATTRIBUTES security, bool elevatedToken);
|
2024-07-02 19:07:06 +00:00
|
|
|
void startProcess();
|
|
|
|
|
void sendSas();
|
|
|
|
|
void setStartupInfo(STARTUPINFO &si);
|
2025-03-03 11:03:21 +00:00
|
|
|
void handleStartError(const std::string_view &message = "");
|
2025-02-13 17:37:58 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
/**
|
2025-02-13 17:37:58 +00:00
|
|
|
* @brief Re-run the process to get the active desktop name.
|
|
|
|
|
*
|
|
|
|
|
* It is necessary to run a utility process because the daemon runs in session 0, which does not
|
|
|
|
|
* have access to the active desktop, and so cannot query it's name.
|
|
|
|
|
*
|
|
|
|
|
* @return std::string The name of the active desktop.
|
2024-07-02 19:07:06 +00:00
|
|
|
*/
|
2025-02-13 17:37:58 +00:00
|
|
|
std::string runActiveDesktopUtility();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread *m_thread;
|
2025-03-03 11:03:21 +00:00
|
|
|
bool m_running;
|
2025-02-13 17:37:58 +00:00
|
|
|
HANDLE m_outputWritePipe;
|
|
|
|
|
HANDLE m_outputReadPipe;
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread *m_outputThread;
|
|
|
|
|
bool m_elevateProcess;
|
|
|
|
|
MSWindowsSession m_session;
|
2025-03-03 11:03:21 +00:00
|
|
|
int m_startFailures;
|
2024-07-02 19:07:06 +00:00
|
|
|
FileLogOutputter *m_fileLogOutputter;
|
|
|
|
|
bool m_foreground;
|
|
|
|
|
std::string m_activeDesktop;
|
2025-02-13 17:37:58 +00:00
|
|
|
std::unique_ptr<deskflow::platform::MSWindowsProcess> m_process;
|
2025-03-03 11:03:21 +00:00
|
|
|
std::optional<double> m_nextStartTime = std::nullopt;
|
|
|
|
|
ProcessState m_processState = ProcessState::Idle;
|
|
|
|
|
std::string m_command = "";
|
2013-10-14 16:29:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//! Relauncher error
|
|
|
|
|
/*!
|
|
|
|
|
An error occured in the process watchdog.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
class XMSWindowsWatchdogError : public XDeskflow
|
|
|
|
|
{
|
2013-10-14 16:29:02 +00:00
|
|
|
public:
|
2025-01-06 00:55:07 +00:00
|
|
|
XMSWindowsWatchdogError(const std::string &msg) : XDeskflow(msg)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
}
|
2013-10-14 16:29:02 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// XBase overrides
|
2025-01-06 00:55:07 +00:00
|
|
|
virtual std::string getWhat() const throw()
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
return what();
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|