From: Joel Rosdahl Date: Thu, 20 Mar 2025 18:49:06 +0000 (+0100) Subject: fix: Reset signal mask and defaults when executing compiler X-Git-Tag: v4.11.2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5222ec6745ddde739ed34ed6b492f8b837a9baee;p=thirdparty%2Fccache.git fix: Reset signal mask and defaults when executing compiler Fixes regression in 1eb0aa5b9bcf74bd2ca6f161e406da64ccd349af where posix_spawn was introduced to replace fork. Fixes #1580. (cherry picked from commit 5239d7712e515a4fa374584bf3a505be66bd8123) --- diff --git a/src/ccache/execute.cpp b/src/ccache/execute.cpp index 80e28a72..f3e1ad74 100644 --- a/src/ccache/execute.cpp +++ b/src/ccache/execute.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2002 Andrew Tridgell -// Copyright (C) 2011-2024 Joel Rosdahl and other contributors +// Copyright (C) 2011-2025 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -53,6 +53,10 @@ # include #endif +#ifndef _WIN32 +# include // NOLINT: sigaddset et al are defined in signal.h +#endif + namespace fs = util::filesystem; #ifdef _WIN32 @@ -312,13 +316,30 @@ execute(Context& ctx, CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, *err, STDERR_FILENO); CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, *err); + posix_spawnattr_t attr; + CHECK_LIB_CALL(posix_spawnattr_init, &attr); + CHECK_LIB_CALL(posix_spawnattr_setflags, + &attr, + POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK); + + sigset_t sigmask; + CHECK_LIB_CALL(sigemptyset, &sigmask); + CHECK_LIB_CALL(posix_spawnattr_setsigmask, &attr, &sigmask); + + sigset_t sigdefault; + CHECK_LIB_CALL(sigemptyset, &sigdefault); + for (int signum : SignalHandler::get_handled_signals()) { + CHECK_LIB_CALL(sigaddset, &sigdefault, signum); + } + CHECK_LIB_CALL(posix_spawnattr_setsigdefault, &attr, &sigdefault); + int result; { SignalHandlerBlocker signal_handler_blocker; pid_t pid; extern char** environ; result = posix_spawn( - &pid, argv[0], &fa, nullptr, const_cast(argv), environ); + &pid, argv[0], &fa, &attr, const_cast(argv), environ); if (result == 0) { ctx.compiler_pid = pid; } diff --git a/src/ccache/signalhandler.cpp b/src/ccache/signalhandler.cpp index f10c459a..3b437e3e 100644 --- a/src/ccache/signalhandler.cpp +++ b/src/ccache/signalhandler.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2024 Joel Rosdahl and other contributors +// Copyright (C) 2020-2025 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -31,7 +31,7 @@ namespace { SignalHandler* g_the_signal_handler = nullptr; sigset_t g_fatal_signal_set; -const int k_handled_signals[] = { +const std::vector k_handled_signals = { SIGINT, SIGTERM, #ifdef SIGHUP @@ -135,6 +135,12 @@ SignalHandler::unblock_signals() sigprocmask(SIG_SETMASK, &empty, nullptr); } +const std::vector& +SignalHandler::get_handled_signals() +{ + return k_handled_signals; +} + SignalHandlerBlocker::SignalHandlerBlocker() { SignalHandler::block_signals(); diff --git a/src/ccache/signalhandler.hpp b/src/ccache/signalhandler.hpp index b75e4667..45a56776 100644 --- a/src/ccache/signalhandler.hpp +++ b/src/ccache/signalhandler.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2023 Joel Rosdahl and other contributors +// Copyright (C) 2020-2025 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -18,6 +18,8 @@ #pragma once +#include + class Context; class SignalHandler @@ -30,6 +32,8 @@ public: static void block_signals(); static void unblock_signals(); + static const std::vector& get_handled_signals(); + private: #ifndef _WIN32 Context& m_ctx;