From b028e88fede97f349df48fbf6a936818104aeaf1 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Mon, 2 Oct 2017 18:13:45 +0100 Subject: [PATCH] #6162 Add UID arg so service can change process user --- src/lib/core/App.cpp | 26 +++++++++++++++++++++----- src/lib/core/ArgParser.cpp | 3 +++ src/lib/core/ArgsBase.cpp | 1 + src/lib/core/ArgsBase.h | 1 + 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/lib/core/App.cpp b/src/lib/core/App.cpp index 9d8771e7..06d0cd12 100644 --- a/src/lib/core/App.cpp +++ b/src/lib/core/App.cpp @@ -33,21 +33,22 @@ #include "ipc/Ipc.h" #include "base/EventQueue.h" +#include +#include + #if SYSAPI_WIN32 #include "arch/win32/ArchMiscWindows.h" #include "base/IEventQueue.h" #include "base/TMethodJob.h" #endif -#include -#include - #if WINAPI_CARBON #include +#include "platform/OSXDragSimulator.h" #endif -#if defined(__APPLE__) -#include "platform/OSXDragSimulator.h" +#if WINAPI_XWINDOWS +#include #endif App* App::s_instance = nullptr; @@ -172,6 +173,21 @@ App::initApp(int argc, const char** argv) { // parse command line parseArgs(argc, argv); + +#if WINAPI_XWINDOWS + // for use on linux, tell the core process what user id it should run as. + // this is a simple way to allow the core process to talk to X. this avoids + // the "WARNING: primary screen unavailable: unable to open screen" error. + // a better way would be to use xauth cookie and dbus to get access to X. + if (!argsBase().m_runAsUid != -1) { + if (setuid(argsBase().m_runAsUid) == 0) { + LOG((CLOG_DEBUG "process uid was set to: %d", argsBase().m_runAsUid)); + } + else { + LOG((CLOG_WARN "failed to set process uid to: %d", argsBase().m_runAsUid)); + } + } +#endif ARCH->setProfileDirectory(argsBase().m_profileDirectory); ARCH->setPluginDirectory(argsBase().m_pluginDirectory); diff --git a/src/lib/core/ArgParser.cpp b/src/lib/core/ArgParser.cpp index 531554ad..284de5b3 100644 --- a/src/lib/core/ArgParser.cpp +++ b/src/lib/core/ArgParser.cpp @@ -299,6 +299,9 @@ ArgParser::parseGenericArgs(int argc, const char* const* argv, int& i) else if (isArg(i, argc, argv, NULL, "--plugin-dir", 1)) { argsBase().m_pluginDirectory = argv[++i]; } + else if (isArg(i, argc, argv, NULL, "--run-as-uid", 1)) { + argsBase().m_runAsUid = std::stoi(argv[++i]); + } else { // option not supported here return false; diff --git a/src/lib/core/ArgsBase.cpp b/src/lib/core/ArgsBase.cpp index 4e9c449c..b78a2e06 100644 --- a/src/lib/core/ArgsBase.cpp +++ b/src/lib/core/ArgsBase.cpp @@ -29,6 +29,7 @@ m_daemon(true), // backward compatibility for unix (daemon by default) #endif #if WINAPI_XWINDOWS m_disableXInitThreads(false), +m_runAsUid(-1), #endif m_backend(false), m_restartable(true), diff --git a/src/lib/core/ArgsBase.h b/src/lib/core/ArgsBase.h index 9ce6e98c..41e94157 100644 --- a/src/lib/core/ArgsBase.h +++ b/src/lib/core/ArgsBase.h @@ -44,6 +44,7 @@ public: #endif #if WINAPI_XWINDOWS bool m_disableXInitThreads; + int m_runAsUid; #endif bool m_shouldExit; String m_synergyAddress;