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
DaemonApp::mainLoop(bool logToFile)
DaemonApp::mainLoop(bool daemonized)
{
try
{
DAEMON_RUNNING(true);
if (logToFile) {
if (daemonized) {
m_fileLogOutputter = new FileLogOutputter(logFilename().c_str());
CLOG->insert(m_fileLogOutputter);
}
@ -215,7 +215,7 @@ DaemonApp::mainLoop(bool logToFile)
CLOG->insert(m_ipcLogOutputter);
#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);
#endif
@ -339,6 +339,8 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
}
#if SYSAPI_WIN32
// eg. no log-to-file while running in foreground
if (m_fileLogOutputter != nullptr) {
String logFilename;
if (argBase->m_logFile != NULL) {
logFilename = String(argBase->m_logFile);
@ -347,12 +349,11 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
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 {
} else {
m_watchdog->setFileLogOutputter(NULL);
}
m_fileLogOutputter->setLogFilename(logFilename.c_str());
}
#endif
}
else {
@ -392,8 +393,8 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
LOG((CLOG_DEBUG "ipc hello, type=%s", type.c_str()));
#if SYSAPI_WIN32
String watchdogStatus = m_watchdog->isProcessActive() ? "ok" : "error";
LOG((CLOG_INFO "watchdog status: %s", watchdogStatus.c_str()));
const char * serverstatus = m_watchdog->isProcessActive() ? "active" : "not active";
LOG((CLOG_INFO "server status: %s", serverstatus));
#endif
m_ipcLogOutputter->notifyBuffer();

View File

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

View File

@ -47,6 +47,7 @@ typedef VOID (WINAPI *SendSas)(BOOL asUser);
const char g_activeDesktop[] = {"activeDesktop:"};
MSWindowsWatchdog::MSWindowsWatchdog(
bool daemonized,
bool autoDetectCommand,
IpcServer& ipcServer,
IpcLogOutputter& ipcLogOutputter) :
@ -63,7 +64,8 @@ MSWindowsWatchdog::MSWindowsWatchdog(
m_processRunning(false),
m_fileLogOutputter(NULL),
m_autoElevated(false),
m_ready(false)
m_ready(false),
m_daemonized(daemonized)
{
m_mutex = ARCH->newMutex();
m_condVar = ARCH->newCondVar();
@ -286,6 +288,10 @@ MSWindowsWatchdog::startProcess()
m_session.updateActiveSession();
BOOL createRet;
if (!m_daemonized) {
createRet = doStartProcessAsSelf(m_command);
} else {
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
@ -302,7 +308,8 @@ MSWindowsWatchdog::startProcess()
DWORD uiAccess = 1;
SetTokenInformation(userToken, TokenUIAccess, &uiAccess, sizeof(DWORD));
BOOL createRet = doStartProcess(m_command, userToken, &sa);
createRet = doStartProcessAsUser(m_command, userToken, &sa);
}
if (!createRet) {
LOG((CLOG_ERR "could not launch"));
@ -329,7 +336,27 @@ MSWindowsWatchdog::startProcess()
}
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
ZeroMemory(&m_processInfo, sizeof(PROCESS_INFORMATION));
@ -355,7 +382,7 @@ MSWindowsWatchdog::doStartProcess(String& command, HANDLE userToken, LPSECURITY_
CREATE_UNICODE_ENVIRONMENT;
// 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(
userToken, NULL, LPSTR(command.c_str()),
sa, NULL, TRUE, creationFlags,
@ -542,7 +569,7 @@ MSWindowsWatchdog::getActiveDesktop(LPSECURITY_ATTRIBUTES security)
HANDLE userToken = getUserToken(security);
m_elevateProcess = elevateProcess;
BOOL createRet = doStartProcess(syntoolCommand, userToken, security);
BOOL createRet = doStartProcessAsUser(syntoolCommand, userToken, security);
if (!createRet) {
DWORD rc = GetLastError();

View File

@ -35,6 +35,7 @@ class FileLogOutputter;
class MSWindowsWatchdog {
public:
MSWindowsWatchdog(
bool daemonized,
bool autoDetectCommand,
IpcServer& ipcServer,
IpcLogOutputter& ipcLogOutputter);
@ -55,7 +56,8 @@ private:
HANDLE duplicateProcessToken(HANDLE process, LPSECURITY_ATTRIBUTES security);
HANDLE getUserToken(LPSECURITY_ATTRIBUTES security);
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 getActiveDesktop(LPSECURITY_ATTRIBUTES security);
void testOutput(String buffer);
@ -81,6 +83,7 @@ private:
ArchMutex m_mutex;
ArchCond m_condVar;
bool m_ready;
bool m_daemonized;
};
//! Relauncher error