fix running daemon in foreground for debugging

This commit is contained in:
walker0643 2018-01-30 14:18:14 -05:00
parent 5af4b13611
commit 8bdd530d7d
4 changed files with 68 additions and 37 deletions

View File

@ -192,13 +192,13 @@ DaemonApp::run(int argc, char** argv)
} }
void void
DaemonApp::mainLoop(bool logToFile) DaemonApp::mainLoop(bool daemonized)
{ {
try try
{ {
DAEMON_RUNNING(true); DAEMON_RUNNING(true);
if (logToFile) { if (daemonized) {
m_fileLogOutputter = new FileLogOutputter(logFilename().c_str()); m_fileLogOutputter = new FileLogOutputter(logFilename().c_str());
CLOG->insert(m_fileLogOutputter); CLOG->insert(m_fileLogOutputter);
} }
@ -215,7 +215,7 @@ DaemonApp::mainLoop(bool logToFile)
CLOG->insert(m_ipcLogOutputter); CLOG->insert(m_ipcLogOutputter);
#if SYSAPI_WIN32 #if SYSAPI_WIN32
m_watchdog = new MSWindowsWatchdog(false, *m_ipcServer, *m_ipcLogOutputter); m_watchdog = new MSWindowsWatchdog(daemonized, false, *m_ipcServer, *m_ipcLogOutputter);
m_watchdog->setFileLogOutputter(m_fileLogOutputter); m_watchdog->setFileLogOutputter(m_fileLogOutputter);
#endif #endif
@ -339,20 +339,21 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
} }
#if SYSAPI_WIN32 #if SYSAPI_WIN32
String logFilename; // eg. no log-to-file while running in foreground
if (argBase->m_logFile != NULL) { if (m_fileLogOutputter != nullptr) {
logFilename = String(argBase->m_logFile); String logFilename;
ARCH->setting("LogFilename", logFilename); if (argBase->m_logFile != NULL) {
m_watchdog->setFileLogOutputter(m_fileLogOutputter); logFilename = String(argBase->m_logFile);
command = ArgParser::assembleCommand(argsArray, "--log", 1); ARCH->setting("LogFilename", logFilename);
LOG((CLOG_DEBUG "removed log file argument and filename %s from command ", logFilename.c_str())); m_watchdog->setFileLogOutputter(m_fileLogOutputter);
LOG((CLOG_DEBUG "new command, elevate=%d command=%s", cm->elevate(), command.c_str())); command = ArgParser::assembleCommand(argsArray, "--log", 1);
LOG((CLOG_DEBUG "removed log file argument and filename %s from command ", logFilename.c_str()));
LOG((CLOG_DEBUG "new command, elevate=%d command=%s", cm->elevate(), command.c_str()));
} else {
m_watchdog->setFileLogOutputter(NULL);
}
m_fileLogOutputter->setLogFilename(logFilename.c_str());
} }
else {
m_watchdog->setFileLogOutputter(NULL);
}
m_fileLogOutputter->setLogFilename(logFilename.c_str());
#endif #endif
} }
else { else {
@ -392,8 +393,8 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
LOG((CLOG_DEBUG "ipc hello, type=%s", type.c_str())); LOG((CLOG_DEBUG "ipc hello, type=%s", type.c_str()));
#if SYSAPI_WIN32 #if SYSAPI_WIN32
String watchdogStatus = m_watchdog->isProcessActive() ? "ok" : "error"; const char * serverstatus = m_watchdog->isProcessActive() ? "active" : "not active";
LOG((CLOG_INFO "watchdog status: %s", watchdogStatus.c_str())); LOG((CLOG_INFO "server status: %s", serverstatus));
#endif #endif
m_ipcLogOutputter->notifyBuffer(); m_ipcLogOutputter->notifyBuffer();

View File

@ -37,7 +37,7 @@ public:
DaemonApp(); DaemonApp();
virtual ~DaemonApp(); virtual ~DaemonApp();
int run(int argc, char** argv); int run(int argc, char** argv);
void mainLoop(bool logToFile); void mainLoop(bool daemonized);
private: private:
void daemonize(); void daemonize();

View File

@ -47,6 +47,7 @@ typedef VOID (WINAPI *SendSas)(BOOL asUser);
const char g_activeDesktop[] = {"activeDesktop:"}; const char g_activeDesktop[] = {"activeDesktop:"};
MSWindowsWatchdog::MSWindowsWatchdog( MSWindowsWatchdog::MSWindowsWatchdog(
bool daemonized,
bool autoDetectCommand, bool autoDetectCommand,
IpcServer& ipcServer, IpcServer& ipcServer,
IpcLogOutputter& ipcLogOutputter) : IpcLogOutputter& ipcLogOutputter) :
@ -63,7 +64,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_ready(false),
m_daemonized(daemonized)
{ {
m_mutex = ARCH->newMutex(); m_mutex = ARCH->newMutex();
m_condVar = ARCH->newCondVar(); m_condVar = ARCH->newCondVar();
@ -286,23 +288,28 @@ MSWindowsWatchdog::startProcess()
m_session.updateActiveSession(); m_session.updateActiveSession();
SECURITY_ATTRIBUTES sa; BOOL createRet;
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); if (!m_daemonized) {
createRet = doStartProcessAsSelf(m_command);
} else {
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
getActiveDesktop(&sa); getActiveDesktop(&sa);
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); 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;
// patch by Jack Zhou and Henry Tung // patch by Jack Zhou and Henry Tung
// set UIAccess to fix Windows 8 GUI interaction // set UIAccess to fix Windows 8 GUI interaction
// http://symless.com/spit/issues/details/3338/#c70 // http://symless.com/spit/issues/details/3338/#c70
DWORD uiAccess = 1; DWORD uiAccess = 1;
SetTokenInformation(userToken, TokenUIAccess, &uiAccess, sizeof(DWORD)); SetTokenInformation(userToken, TokenUIAccess, &uiAccess, sizeof(DWORD));
BOOL createRet = doStartProcess(m_command, userToken, &sa); createRet = doStartProcessAsUser(m_command, userToken, &sa);
}
if (!createRet) { if (!createRet) {
LOG((CLOG_ERR "could not launch")); LOG((CLOG_ERR "could not launch"));
@ -329,7 +336,27 @@ MSWindowsWatchdog::startProcess()
} }
BOOL BOOL
MSWindowsWatchdog::doStartProcess(String& command, HANDLE userToken, LPSECURITY_ATTRIBUTES sa) MSWindowsWatchdog::doStartProcessAsSelf(String& command)
{
DWORD creationFlags =
NORMAL_PRIORITY_CLASS |
CREATE_NO_WINDOW |
CREATE_UNICODE_ENVIRONMENT;
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;
LOG((CLOG_INFO "starting new process as self"));
return CreateProcess(NULL, LPSTR(command.c_str()), NULL, NULL, FALSE, creationFlags, NULL, NULL, &si, &m_processInfo);
}
BOOL
MSWindowsWatchdog::doStartProcessAsUser(String& command, HANDLE userToken, LPSECURITY_ATTRIBUTES sa)
{ {
// clear, as we're reusing process info struct // clear, as we're reusing process info struct
ZeroMemory(&m_processInfo, sizeof(PROCESS_INFORMATION)); ZeroMemory(&m_processInfo, sizeof(PROCESS_INFORMATION));
@ -355,7 +382,7 @@ MSWindowsWatchdog::doStartProcess(String& command, HANDLE userToken, LPSECURITY_
CREATE_UNICODE_ENVIRONMENT; CREATE_UNICODE_ENVIRONMENT;
// re-launch in current active user session // re-launch in current active user session
LOG((CLOG_INFO "starting new process")); LOG((CLOG_INFO "starting new process as privileged user"));
BOOL createRet = CreateProcessAsUser( BOOL createRet = CreateProcessAsUser(
userToken, NULL, LPSTR(command.c_str()), userToken, NULL, LPSTR(command.c_str()),
sa, NULL, TRUE, creationFlags, sa, NULL, TRUE, creationFlags,
@ -542,7 +569,7 @@ MSWindowsWatchdog::getActiveDesktop(LPSECURITY_ATTRIBUTES security)
HANDLE userToken = getUserToken(security); HANDLE userToken = getUserToken(security);
m_elevateProcess = elevateProcess; m_elevateProcess = elevateProcess;
BOOL createRet = doStartProcess(syntoolCommand, userToken, security); BOOL createRet = doStartProcessAsUser(syntoolCommand, userToken, security);
if (!createRet) { if (!createRet) {
DWORD rc = GetLastError(); DWORD rc = GetLastError();

View File

@ -35,6 +35,7 @@ class FileLogOutputter;
class MSWindowsWatchdog { class MSWindowsWatchdog {
public: public:
MSWindowsWatchdog( MSWindowsWatchdog(
bool daemonized,
bool autoDetectCommand, bool autoDetectCommand,
IpcServer& ipcServer, IpcServer& ipcServer,
IpcLogOutputter& ipcLogOutputter); IpcLogOutputter& ipcLogOutputter);
@ -55,7 +56,8 @@ private:
HANDLE duplicateProcessToken(HANDLE process, LPSECURITY_ATTRIBUTES security); HANDLE duplicateProcessToken(HANDLE process, LPSECURITY_ATTRIBUTES security);
HANDLE getUserToken(LPSECURITY_ATTRIBUTES security); HANDLE getUserToken(LPSECURITY_ATTRIBUTES security);
void startProcess(); void startProcess();
BOOL doStartProcess(String& command, HANDLE userToken, LPSECURITY_ATTRIBUTES sa); BOOL doStartProcessAsUser(String& command, HANDLE userToken, LPSECURITY_ATTRIBUTES sa);
BOOL doStartProcessAsSelf(String& command);
void sendSas(); void sendSas();
void getActiveDesktop(LPSECURITY_ATTRIBUTES security); void getActiveDesktop(LPSECURITY_ATTRIBUTES security);
void testOutput(String buffer); void testOutput(String buffer);
@ -81,6 +83,7 @@ private:
ArchMutex m_mutex; ArchMutex m_mutex;
ArchCond m_condVar; ArchCond m_condVar;
bool m_ready; bool m_ready;
bool m_daemonized;
}; };
//! Relauncher error //! Relauncher error