Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
MSWindowsWatchdog.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2012 - 2025 Symless Ltd.
4 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
5 */
6
7#pragma once
8
11
12#define WIN32_LEAN_AND_MEAN
13#include <Windows.h>
14
15#include <memory>
16#include <mutex>
17#include <optional>
18#include <string>
19
20typedef VOID(WINAPI *SendSas)(BOOL asUser);
21
22class Thread;
24
29{
30 enum class ProcessState
31 {
32 Idle,
33 StartScheduled,
34 StartPending,
35 StopPending,
36 Running
37 };
38
39public:
40 explicit MSWindowsWatchdog(bool foreground, FileLogOutputter &fileLogOutputter);
41 ~MSWindowsWatchdog() = default;
42
46 void startAsync();
47
51 void setProcessConfig(const std::string_view &command, bool elevate);
52
56 void stop();
57
61 bool isProcessRunning();
62
63private:
67 void mainLoop(void *);
68
72 void outputLoop(void *);
73
83 HANDLE duplicateProcessToken(HANDLE process, LPSECURITY_ATTRIBUTES security);
84
92 HANDLE getUserToken(LPSECURITY_ATTRIBUTES security, bool elevatedToken);
93
97 void startProcess();
98
102 ProcessState handleStartError(const std::string_view &message = "");
103
107 void initOutputReadPipe();
108
112 void initSasFunc();
113
119 void sasLoop(void *);
120
124 static std::string processStateToString(ProcessState state);
125
129 static void shutdownExistingProcesses();
130
131private:
132 bool m_running = true;
133 std::unique_ptr<Thread> m_mainThread;
134 std::unique_ptr<Thread> m_outputThread;
135 std::unique_ptr<Thread> m_sasThread;
136 HANDLE m_outputWritePipe = nullptr;
137 HANDLE m_outputReadPipe = nullptr;
138 bool m_elevateProcess = false;
139 MSWindowsSession m_session;
140 int m_startFailures = 0;
141 FileLogOutputter &m_fileLogOutputter;
142 bool m_foreground = false;
143 std::string m_activeDesktop = "";
144 std::unique_ptr<deskflow::platform::MSWindowsProcess> m_process;
145 std::optional<double> m_nextStartTime = std::nullopt;
146 ProcessState m_processState = ProcessState::Idle;
147 std::string m_command = "";
148 SendSas m_sendSasFunc = nullptr;
149 std::mutex m_processStateMutex;
150};
static const WSAEVENT FAR BOOL
Definition ArchNetworkWinsock.cpp:54
VOID(WINAPI * SendSas)(BOOL asUser)
Definition MSWindowsWatchdog.h:20
Write log to file.
Definition LogOutputters.h:60
Definition MSWindowsSession.h:17
bool isProcessRunning()
Definition MSWindowsWatchdog.cpp:258
MSWindowsWatchdog(bool foreground, FileLogOutputter &fileLogOutputter)
Definition MSWindowsWatchdog.cpp:86
void startAsync()
Start threads for main loop and and output loop.
Definition MSWindowsWatchdog.cpp:94
void setProcessConfig(const std::string_view &command, bool elevate)
Set the command to run and whether to elevate the process.
Definition MSWindowsWatchdog.cpp:327
~MSWindowsWatchdog()=default
void stop()
Stop the main loop and output loop threads.
Definition MSWindowsWatchdog.cpp:101
Thread handle.
Definition Thread.h:33