Path: blob/main/contrib/kyua/utils/signals/misc.cpp
48199 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/signals/misc.hpp"2930#if defined(HAVE_CONFIG_H)31# include "config.h"32#endif3334extern "C" {35#include <signal.h>36}3738#include <cerrno>39#include <cstddef>4041#include "utils/format/macros.hpp"42#include "utils/logging/macros.hpp"43#include "utils/signals/exceptions.hpp"4445namespace signals = utils::signals;464748/// Number of the last valid signal.49const int utils::signals::last_signo = LAST_SIGNO;505152/// Resets a signal handler to its default behavior.53///54/// \param signo The number of the signal handler to reset.55///56/// \throw signals::system_error If there is a problem trying to reset the57/// signal handler to its default behavior.58void59signals::reset(const int signo)60{61struct ::sigaction sa;62sa.sa_handler = SIG_DFL;63sigemptyset(&sa.sa_mask);64sa.sa_flags = 0;6566if (::sigaction(signo, &sa, NULL) == -1) {67const int original_errno = errno;68throw system_error(F("Failed to reset signal %s") % signo,69original_errno);70}71}727374/// Resets all signals to their default handlers.75///76/// \return True if all signals could be reset properly; false otherwise.77bool78signals::reset_all(void)79{80bool ok = true;8182for (int signo = 1; signo <= signals::last_signo; ++signo) {83if (signo == SIGKILL || signo == SIGSTOP) {84// Don't attempt to reset immutable signals.85} else {86try {87signals::reset(signo);88} catch (const signals::error& e) {89#if defined(SIGTHR)90if (signo == SIGTHR) {91// If FreeBSD's libthr is loaded, it prevents us from92// modifying SIGTHR (at least in 11.0-CURRENT as of93// 2015-01-28). Skip failures for this signal if they94// happen to avoid this corner case.95continue;96}97#endif98LW(e.what());99ok = false;100}101}102}103104return ok;105}106107108