diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 6ebd2469..3ad01e95 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -23,6 +23,15 @@ namespace bp = boost::process; namespace fs = boost::filesystem; +using namespace std::literals::string_literals; + +/** + * If this environment variable is set to `1`, then we won't enable the watchdog + * timer. This is only necessary when running the Wine process under a different + * namespace than the host. + */ +constexpr char disable_watchdog_timer_env_var[] = "YABRIDGE_NO_WATCHDOG"; + fs::path get_temporary_directory() { bp::environment env = boost::this_process::environment(); if (!env["XDG_RUNTIME_DIR"].empty()) { @@ -57,6 +66,13 @@ std::optional get_rttime_limit() noexcept { } } +bool is_watchdog_timer_disabled() { + // This is safe because we're not storing the pointer anywhere and the + // environment doesn't get modified anywhere + // NOLINTNEXTLINE(concurrency-mt-unsafe) + return getenv(disable_watchdog_timer_env_var) == "1"s; +} + bool pid_running(pid_t pid) { // With regular individually hosted plugins we can simply check whether the // process is still running, however Boost.Process does not allow you to do diff --git a/src/common/utils.h b/src/common/utils.h index 43b2da9f..e28d18e5 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -117,6 +117,16 @@ bool set_realtime_priority(bool sched_fifo, int priority = 5) noexcept; */ std::optional get_rttime_limit() noexcept; +/** + * Returns `true` if `YABRIDGE_NO_WATCHDOG` is set to `1`. In that case we will + * not check if the Wine plugin host process successfully started, and we'll + * also don't check if the native plugin host is still alive. Disabling the + * watchdog timers can cause plugins hang during scanning and dangling Wine + * processes to be left running, so this should only ever be used when running + * the Wine plugin host under a separate namespace. + */ +bool is_watchdog_timer_disabled(); + /** * Check whether a process with the given PID is still active (and not a * zombie). diff --git a/src/wine-host/utils.cpp b/src/wine-host/utils.cpp index c2958eb8..c6889da8 100644 --- a/src/wine-host/utils.cpp +++ b/src/wine-host/utils.cpp @@ -18,21 +18,10 @@ #include -#include - #include "bridges/common.h" -namespace bp = boost::process; - using namespace std::literals::chrono_literals; -/** - * If this environment variable is set to `1`, then we won't enable the watchdog - * timer. This is only necessary when running the Wine process under a different - * namespace than the host. - */ -constexpr char disable_watchdog_timer_env_var[] = "YABRIDGE_NO_WATCHDOG"; - uint32_t WINAPI win32_thread_trampoline(fu2::unique_function* entry_point) { (*entry_point)(); @@ -94,12 +83,10 @@ MainContext::MainContext() events_timer(context), watchdog_context(), watchdog_timer(watchdog_context) { - bp::environment env = boost::this_process::environment(); - // NOTE: We allow disabling the watchdog timer to allow the Wine process to // be run from a separate namespace. This is not something you'd // normally want to enable. - if (env[disable_watchdog_timer_env_var].to_string() == "1") { + if (is_watchdog_timer_disabled()) { std::cerr << "WARNING: Watchdog timer disabled. Not protecting" << std::endl; std::cerr << " against dangling processes." << std::endl;