2012-06-10 16:50:54 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2012 Bolton Software Ltd.
|
|
|
|
* Copyright (C) 2009 Chris Schoeneman
|
2012-06-10 16:50:54 +00:00
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "platform/MSWindowsWatchdog.h"
|
|
|
|
|
|
|
|
#include "ipc/IpcLogOutputter.h"
|
|
|
|
#include "ipc/IpcServer.h"
|
|
|
|
#include "ipc/IpcMessage.h"
|
|
|
|
#include "ipc/Ipc.h"
|
|
|
|
#include "synergy/App.h"
|
|
|
|
#include "synergy/ArgsBase.h"
|
|
|
|
#include "mt/Thread.h"
|
|
|
|
#include "arch/win32/ArchDaemonWindows.h"
|
|
|
|
#include "arch/win32/XArchWindows.h"
|
|
|
|
#include "arch/Arch.h"
|
|
|
|
#include "base/TMethodJob.h"
|
|
|
|
#include "base/Log.h"
|
|
|
|
#include "common/Version.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
2014-02-28 12:36:45 +00:00
|
|
|
#include <UserEnv.h>
|
2012-07-28 02:59:20 +00:00
|
|
|
#include <Shellapi.h>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
kOutputBufferSize = 4096
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef VOID (WINAPI *SendSas)(BOOL asUser);
|
|
|
|
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::CMSWindowsWatchdog(
|
2012-07-05 18:05:35 +00:00
|
|
|
bool autoDetectCommand,
|
|
|
|
CIpcServer& ipcServer,
|
|
|
|
CIpcLogOutputter& ipcLogOutputter) :
|
2012-06-10 16:50:54 +00:00
|
|
|
m_thread(NULL),
|
|
|
|
m_autoDetectCommand(autoDetectCommand),
|
2013-10-14 16:29:02 +00:00
|
|
|
m_monitoring(true),
|
2012-06-10 16:50:54 +00:00
|
|
|
m_commandChanged(false),
|
|
|
|
m_stdOutWrite(NULL),
|
2012-07-03 14:15:05 +00:00
|
|
|
m_stdOutRead(NULL),
|
2012-07-05 18:05:35 +00:00
|
|
|
m_ipcServer(ipcServer),
|
2012-07-28 02:59:20 +00:00
|
|
|
m_ipcLogOutputter(ipcLogOutputter),
|
2013-10-14 16:29:02 +00:00
|
|
|
m_elevateProcess(false),
|
2013-10-15 15:46:02 +00:00
|
|
|
m_processFailures(0),
|
|
|
|
m_processRunning(false)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::~CMSWindowsWatchdog()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::startAsync()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2013-10-14 17:15:03 +00:00
|
|
|
m_thread = new CThread(new TMethodJob<CMSWindowsWatchdog>(
|
|
|
|
this, &CMSWindowsWatchdog::mainLoop, nullptr));
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2013-10-14 17:15:03 +00:00
|
|
|
m_outputThread = new CThread(new TMethodJob<CMSWindowsWatchdog>(
|
|
|
|
this, &CMSWindowsWatchdog::outputLoop, nullptr));
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::stop()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2013-10-14 16:29:02 +00:00
|
|
|
m_monitoring = false;
|
2012-07-10 01:51:51 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
m_thread->wait(5);
|
2012-07-10 01:51:51 +00:00
|
|
|
delete m_thread;
|
|
|
|
|
|
|
|
m_outputThread->wait(5);
|
|
|
|
delete m_outputThread;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
2012-07-28 02:59:20 +00:00
|
|
|
HANDLE
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::duplicateProcessToken(HANDLE process, LPSECURITY_ATTRIBUTES security)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2012-07-28 02:59:20 +00:00
|
|
|
HANDLE sourceToken;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2012-07-28 02:59:20 +00:00
|
|
|
BOOL tokenRet = OpenProcessToken(
|
|
|
|
process,
|
|
|
|
TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS,
|
|
|
|
&sourceToken);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2012-07-28 02:59:20 +00:00
|
|
|
if (!tokenRet) {
|
2013-10-14 16:29:02 +00:00
|
|
|
LOG((CLOG_ERR "could not open token, process handle: %d", process));
|
|
|
|
throw XArch(new XArchEvalWindows());
|
2012-07-28 02:59:20 +00:00
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2012-08-03 17:03:24 +00:00
|
|
|
LOG((CLOG_DEBUG "got token %i, duplicating", sourceToken));
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2012-07-28 02:59:20 +00:00
|
|
|
HANDLE newToken;
|
|
|
|
BOOL duplicateRet = DuplicateTokenEx(
|
|
|
|
sourceToken, TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS, security,
|
|
|
|
SecurityImpersonation, TokenPrimary, &newToken);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2012-07-28 02:59:20 +00:00
|
|
|
if (!duplicateRet) {
|
2013-10-14 16:29:02 +00:00
|
|
|
LOG((CLOG_ERR "could not duplicate token %i", sourceToken));
|
|
|
|
throw XArch(new XArchEvalWindows());
|
2012-07-28 02:59:20 +00:00
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2012-07-28 02:59:20 +00:00
|
|
|
LOG((CLOG_DEBUG "duplicated, new token: %i", newToken));
|
|
|
|
return newToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::getUserToken(LPSECURITY_ATTRIBUTES security)
|
2012-07-28 02:59:20 +00:00
|
|
|
{
|
2012-07-28 13:34:35 +00:00
|
|
|
// always elevate if we are at the vista/7 login screen. we could also
|
|
|
|
// elevate for the uac dialog (consent.exe) but this would be pointless,
|
|
|
|
// since synergy would re-launch as non-elevated after the desk switch,
|
|
|
|
// and so would be unusable with the new elevated process taking focus.
|
2013-10-10 16:06:13 +00:00
|
|
|
if (m_elevateProcess || m_session.isProcessInSession("logonui.exe", NULL)) {
|
2012-07-28 13:34:35 +00:00
|
|
|
|
|
|
|
LOG((CLOG_DEBUG "getting elevated token, %s",
|
|
|
|
(m_elevateProcess ? "elevation required" : "at login screen")));
|
2013-10-14 16:29:02 +00:00
|
|
|
|
2012-07-28 02:59:20 +00:00
|
|
|
HANDLE process;
|
2013-10-14 16:29:02 +00:00
|
|
|
if (!m_session.isProcessInSession("winlogon.exe", &process)) {
|
|
|
|
throw XMSWindowsWatchdogError("cannot get user token without winlogon.exe");
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
2013-10-14 16:29:02 +00:00
|
|
|
|
|
|
|
return duplicateProcessToken(process, security);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-28 13:34:35 +00:00
|
|
|
LOG((CLOG_DEBUG "getting non-elevated token"));
|
2013-10-10 16:06:13 +00:00
|
|
|
return m_session.getUserToken(security);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::mainLoop(void*)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2012-07-10 11:54:20 +00:00
|
|
|
shutdownExistingProcesses();
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
SendSas sendSasFunc = NULL;
|
|
|
|
HINSTANCE sasLib = LoadLibrary("sas.dll");
|
|
|
|
if (sasLib) {
|
|
|
|
LOG((CLOG_DEBUG "found sas.dll"));
|
|
|
|
sendSasFunc = (SendSas)GetProcAddress(sasLib, "SendSAS");
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:41:12 +00:00
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
|
|
|
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
2012-06-10 16:50:54 +00:00
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
if (!CreatePipe(&m_stdOutRead, &m_stdOutWrite, &saAttr, 0)) {
|
|
|
|
throw XArch(new XArchEvalWindows());
|
|
|
|
}
|
|
|
|
|
2013-10-14 16:29:02 +00:00
|
|
|
ZeroMemory(&m_processInfo, sizeof(PROCESS_INFORMATION));
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2013-10-14 16:29:02 +00:00
|
|
|
while (m_monitoring) {
|
2013-10-15 15:46:02 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
if (m_processRunning && getCommand().empty()) {
|
|
|
|
LOG((CLOG_INFO "process started but command is empty, shutting down"));
|
2013-10-14 17:10:51 +00:00
|
|
|
shutdownExistingProcesses();
|
2013-10-15 15:46:02 +00:00
|
|
|
m_processRunning = false;
|
2012-07-28 02:59:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
if (m_processFailures != 0) {
|
|
|
|
// increasing backoff period, maximum of 10 seconds.
|
|
|
|
int timeout = (m_processFailures * 2) < 10 ? (m_processFailures * 2) : 10;
|
|
|
|
LOG((CLOG_INFO "backing off, wait=%ds, failures=%d", timeout, m_processFailures));
|
|
|
|
ARCH->sleep(timeout);
|
2013-10-14 16:29:02 +00:00
|
|
|
}
|
2013-10-15 15:46:02 +00:00
|
|
|
|
|
|
|
if (!getCommand().empty() && ((m_processFailures != 0) || m_session.hasChanged() || m_commandChanged)) {
|
|
|
|
startProcess();
|
2012-07-28 02:59:20 +00:00
|
|
|
}
|
2013-10-15 15:46:02 +00:00
|
|
|
|
|
|
|
if (m_processRunning && !isProcessActive()) {
|
|
|
|
|
|
|
|
m_processFailures++;
|
|
|
|
m_processRunning = false;
|
|
|
|
|
|
|
|
LOG((CLOG_WARN "detected application not running, pid=%d",
|
|
|
|
m_processInfo.dwProcessId));
|
2013-10-14 16:29:02 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
if (sendSasFunc != NULL) {
|
2012-07-28 02:59:20 +00:00
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
HANDLE sendSasEvent = CreateEvent(NULL, FALSE, FALSE, "Global\\SendSAS");
|
|
|
|
if (sendSasEvent != NULL) {
|
2012-07-28 02:59:20 +00:00
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
// use SendSAS event to wait for next session (timeout 1 second).
|
|
|
|
if (WaitForSingleObject(sendSasEvent, 1000) == WAIT_OBJECT_0) {
|
|
|
|
LOG((CLOG_DEBUG "calling SendSAS"));
|
|
|
|
sendSasFunc(FALSE);
|
|
|
|
}
|
2012-07-28 02:59:20 +00:00
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
CloseHandle(sendSasEvent);
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-28 02:59:20 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
// if the sas event failed, wait by sleeping.
|
|
|
|
ARCH->sleep(1);
|
|
|
|
|
|
|
|
}
|
2014-03-14 18:30:21 +00:00
|
|
|
catch (std::exception& e) {
|
|
|
|
LOG((CLOG_ERR "failed to launch, error: %s", e.what()));
|
2013-10-15 15:46:02 +00:00
|
|
|
m_processFailures++;
|
|
|
|
m_processRunning = false;
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-14 18:30:21 +00:00
|
|
|
catch (...) {
|
|
|
|
LOG((CLOG_ERR "failed to launch, unknown error."));
|
2013-10-15 15:46:02 +00:00
|
|
|
m_processFailures++;
|
|
|
|
m_processRunning = false;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
if (m_processRunning) {
|
2012-06-10 16:50:54 +00:00
|
|
|
LOG((CLOG_DEBUG "terminated running process on exit"));
|
2013-10-14 16:29:02 +00:00
|
|
|
shutdownProcess(m_processInfo.hProcess, m_processInfo.dwProcessId, 20);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
2012-07-10 01:51:51 +00:00
|
|
|
|
2014-02-07 18:04:25 +00:00
|
|
|
LOG((CLOG_DEBUG "watchdog main thread finished"));
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 16:29:02 +00:00
|
|
|
bool
|
2013-10-15 15:46:02 +00:00
|
|
|
CMSWindowsWatchdog::isProcessActive()
|
2013-10-14 16:29:02 +00:00
|
|
|
{
|
2013-10-15 15:46:02 +00:00
|
|
|
DWORD exitCode;
|
|
|
|
GetExitCodeProcess(m_processInfo.hProcess, &exitCode);
|
|
|
|
return exitCode == STILL_ACTIVE;
|
2013-10-14 16:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-15 15:46:02 +00:00
|
|
|
CMSWindowsWatchdog::startProcess()
|
2013-10-14 16:29:02 +00:00
|
|
|
{
|
2013-10-15 15:46:02 +00:00
|
|
|
if (m_command.empty()) {
|
|
|
|
throw XMSWindowsWatchdogError("cannot start process, command is empty");
|
|
|
|
}
|
|
|
|
|
2013-10-14 16:29:02 +00:00
|
|
|
m_commandChanged = false;
|
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
if (m_processRunning) {
|
2013-10-14 16:29:02 +00:00
|
|
|
LOG((CLOG_DEBUG "closing existing process to make way for new one"));
|
|
|
|
shutdownProcess(m_processInfo.hProcess, m_processInfo.dwProcessId, 20);
|
2013-10-15 15:46:02 +00:00
|
|
|
m_processRunning = false;
|
2013-10-14 16:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_session.updateActiveSession();
|
|
|
|
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
|
|
|
|
|
|
|
|
HANDLE userToken = getUserToken(&sa);
|
|
|
|
|
2013-10-30 13:49:40 +00:00
|
|
|
// patch by Jack Zhou and Henry Tung
|
|
|
|
// set UIAccess to fix Windows 8 GUI interaction
|
2014-05-08 07:33:35 +00:00
|
|
|
// http://synergy-project.org/spit/issues/details/3338/#c70
|
2013-10-30 13:49:40 +00:00
|
|
|
DWORD uiAccess = 1;
|
|
|
|
SetTokenInformation(userToken, TokenUIAccess, &uiAccess, sizeof(DWORD));
|
|
|
|
|
2013-10-14 16:29:02 +00:00
|
|
|
// clear, as we're reusing process info struct
|
|
|
|
ZeroMemory(&m_processInfo, sizeof(PROCESS_INFORMATION));
|
|
|
|
|
|
|
|
STARTUPINFO si;
|
|
|
|
ZeroMemory(&si, sizeof(STARTUPINFO));
|
|
|
|
si.cb = sizeof(STARTUPINFO);
|
|
|
|
si.lpDesktop = "winsta0\\Default"; // TODO: maybe this should be \winlogon if we have logonui.exe?
|
|
|
|
si.hStdError = m_stdOutWrite;
|
|
|
|
si.hStdOutput = m_stdOutWrite;
|
|
|
|
si.dwFlags |= STARTF_USESTDHANDLES;
|
|
|
|
|
|
|
|
LPVOID environment;
|
|
|
|
BOOL blockRet = CreateEnvironmentBlock(&environment, userToken, FALSE);
|
|
|
|
if (!blockRet) {
|
|
|
|
LOG((CLOG_ERR "could not create environment block"));
|
|
|
|
throw XArch(new XArchEvalWindows);
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD creationFlags =
|
|
|
|
NORMAL_PRIORITY_CLASS |
|
|
|
|
CREATE_NO_WINDOW |
|
|
|
|
CREATE_UNICODE_ENVIRONMENT;
|
|
|
|
|
|
|
|
// re-launch in current active user session
|
|
|
|
LOG((CLOG_INFO "starting new process"));
|
|
|
|
BOOL createRet = CreateProcessAsUser(
|
2013-10-15 15:46:02 +00:00
|
|
|
userToken, NULL, LPSTR(m_command.c_str()),
|
2013-10-14 16:29:02 +00:00
|
|
|
&sa, NULL, TRUE, creationFlags,
|
|
|
|
environment, NULL, &si, &m_processInfo);
|
|
|
|
|
|
|
|
DestroyEnvironmentBlock(environment);
|
|
|
|
CloseHandle(userToken);
|
|
|
|
|
|
|
|
if (!createRet) {
|
|
|
|
LOG((CLOG_ERR "could not launch"));
|
|
|
|
throw XArch(new XArchEvalWindows);
|
|
|
|
}
|
|
|
|
else {
|
2013-10-16 15:30:42 +00:00
|
|
|
// wait for program to fail.
|
|
|
|
ARCH->sleep(1);
|
|
|
|
if (!isProcessActive()) {
|
|
|
|
throw XMSWindowsWatchdogError("process immediately stopped");
|
|
|
|
}
|
|
|
|
|
2013-10-15 15:46:02 +00:00
|
|
|
m_processRunning = true;
|
|
|
|
m_processFailures = 0;
|
|
|
|
|
2013-10-14 16:29:02 +00:00
|
|
|
LOG((CLOG_DEBUG "started process, session=%i, command=%s",
|
2013-10-15 15:46:02 +00:00
|
|
|
m_session.getActiveSessionId(), m_command.c_str()));
|
2013-10-14 16:29:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
void
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::setCommand(const std::string& command, bool elevate)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
LOG((CLOG_INFO "service command updated"));
|
|
|
|
m_command = command;
|
2012-07-28 02:59:20 +00:00
|
|
|
m_elevateProcess = elevate;
|
2012-06-10 16:50:54 +00:00
|
|
|
m_commandChanged = true;
|
2014-02-07 18:04:25 +00:00
|
|
|
m_processFailures = 0;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::getCommand() const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
if (!m_autoDetectCommand) {
|
|
|
|
return m_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
// seems like a fairly convoluted way to get the process name
|
|
|
|
const char* launchName = CApp::instance().argsBase().m_pname;
|
|
|
|
std::string args = ARCH->commandLine();
|
|
|
|
|
|
|
|
// build up a full command line
|
|
|
|
std::stringstream cmdTemp;
|
2012-07-05 18:05:35 +00:00
|
|
|
cmdTemp << launchName << args;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
std::string cmd = cmdTemp.str();
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
std::string find = "--relaunch";
|
2013-10-14 16:29:02 +00:00
|
|
|
while ((i = cmd.find(find)) != std::string::npos) {
|
2012-06-10 16:50:54 +00:00
|
|
|
cmd.replace(i, find.length(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::outputLoop(void*)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2012-07-05 18:05:35 +00:00
|
|
|
// +1 char for \0
|
|
|
|
CHAR buffer[kOutputBufferSize + 1];
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2013-10-14 16:29:02 +00:00
|
|
|
while (m_monitoring) {
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
DWORD bytesRead;
|
|
|
|
BOOL success = ReadFile(m_stdOutRead, buffer, kOutputBufferSize, &bytesRead, NULL);
|
|
|
|
|
|
|
|
// assume the process has gone away? slow down
|
|
|
|
// the reads until another one turns up.
|
|
|
|
if (!success || bytesRead == 0) {
|
|
|
|
ARCH->sleep(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer[bytesRead] = '\0';
|
2012-07-08 16:27:28 +00:00
|
|
|
|
|
|
|
// send process output over IPC to GUI, and force it to be sent
|
|
|
|
// which bypasses the ipc logging anti-recursion mechanism.
|
|
|
|
m_ipcLogOutputter.write(kINFO, buffer, true);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::shutdownProcess(HANDLE handle, DWORD pid, int timeout)
|
2013-07-24 16:41:12 +00:00
|
|
|
{
|
|
|
|
DWORD exitCode;
|
|
|
|
GetExitCodeProcess(handle, &exitCode);
|
2013-10-14 16:29:02 +00:00
|
|
|
if (exitCode != STILL_ACTIVE) {
|
2013-07-24 16:41:12 +00:00
|
|
|
return;
|
2013-10-14 16:29:02 +00:00
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
|
|
|
|
CIpcShutdownMessage shutdown;
|
|
|
|
m_ipcServer.send(shutdown, kIpcClientNode);
|
|
|
|
|
|
|
|
// wait for process to exit gracefully.
|
|
|
|
double start = ARCH->time();
|
2013-10-14 16:29:02 +00:00
|
|
|
while (true) {
|
|
|
|
|
2013-07-24 16:41:12 +00:00
|
|
|
GetExitCodeProcess(handle, &exitCode);
|
|
|
|
if (exitCode != STILL_ACTIVE) {
|
|
|
|
// yay, we got a graceful shutdown. there should be no hook in use errors!
|
|
|
|
LOG((CLOG_INFO "process %d was shutdown gracefully", pid));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
double elapsed = (ARCH->time() - start);
|
|
|
|
if (elapsed > timeout) {
|
|
|
|
// if timeout reached, kill forcefully.
|
|
|
|
// calling TerminateProcess on synergy is very bad!
|
|
|
|
// it causes the hook DLL to stay loaded in some apps,
|
|
|
|
// making it impossible to start synergy again.
|
|
|
|
LOG((CLOG_WARN "shutdown timed out after %d secs, forcefully terminating", (int)elapsed));
|
|
|
|
TerminateProcess(handle, kExitSuccess);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ARCH->sleep(1);
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 11:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 17:15:03 +00:00
|
|
|
CMSWindowsWatchdog::shutdownExistingProcesses()
|
2012-07-10 11:54:20 +00:00
|
|
|
{
|
|
|
|
// first we need to take a snapshot of the running processes
|
|
|
|
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (snapshot == INVALID_HANDLE_VALUE) {
|
2013-10-14 16:29:02 +00:00
|
|
|
LOG((CLOG_ERR "could not get process snapshot"));
|
|
|
|
throw XArch(new XArchEvalWindows);
|
2012-07-10 11:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PROCESSENTRY32 entry;
|
|
|
|
entry.dwSize = sizeof(PROCESSENTRY32);
|
|
|
|
|
|
|
|
// get the first process, and if we can't do that then it's
|
|
|
|
// unlikely we can go any further
|
|
|
|
BOOL gotEntry = Process32First(snapshot, &entry);
|
|
|
|
if (!gotEntry) {
|
2013-10-14 16:29:02 +00:00
|
|
|
LOG((CLOG_ERR "could not get first process entry"));
|
|
|
|
throw XArch(new XArchEvalWindows);
|
2012-07-10 11:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now just iterate until we can find winlogon.exe pid
|
|
|
|
DWORD pid = 0;
|
2013-10-14 16:29:02 +00:00
|
|
|
while (gotEntry) {
|
2012-07-10 11:54:20 +00:00
|
|
|
|
|
|
|
// make sure we're not checking the system process
|
|
|
|
if (entry.th32ProcessID != 0) {
|
|
|
|
|
|
|
|
if (_stricmp(entry.szExeFile, "synergyc.exe") == 0 ||
|
|
|
|
_stricmp(entry.szExeFile, "synergys.exe") == 0) {
|
|
|
|
|
|
|
|
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
|
2012-07-13 17:08:00 +00:00
|
|
|
shutdownProcess(handle, entry.th32ProcessID, 10);
|
2012-07-10 11:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now move on to the next entry (if we're not at the end)
|
|
|
|
gotEntry = Process32Next(snapshot, &entry);
|
|
|
|
if (!gotEntry) {
|
|
|
|
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
if (err != ERROR_NO_MORE_FILES) {
|
|
|
|
|
|
|
|
// only worry about error if it's not the end of the snapshot
|
2013-10-14 16:29:02 +00:00
|
|
|
LOG((CLOG_ERR "could not get subsiquent process entry"));
|
|
|
|
throw XArch(new XArchEvalWindows);
|
2012-07-10 11:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(snapshot);
|
2013-10-15 15:46:02 +00:00
|
|
|
m_processRunning = false;
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|