~afrantzis/mir/reproduce-1444047

« back to all changes in this revision

Viewing changes to src/common/thread/signal_blocker.cpp

  • Committer: Tarmac
  • Author(s): Christopher James Halse Rogers, Christopher James Halse Rogers
  • Date: 2015-04-15 05:07:08 UTC
  • mfrom: (2465.4.4 master)
  • Revision ID: tarmac-20150415050708-bq08i0c0n00cn6km
Introduce trivial RAII helper for signal blocking.

Use it to close a tiny race in SimpleDispatchThread - there was a time between spawning the thread and blocking signals on it during which a signal could theoretically be caught.
.

Approved by PS Jenkins bot, Alexandros Frantzis, Cemil Azizoglu, Alan Griffiths, Kevin DuBois, Robert Carr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored By: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir/signal_blocker.h"
 
20
 
 
21
#include <system_error>
 
22
#include <signal.h>
 
23
#include <boost/exception/all.hpp>
 
24
 
 
25
mir::SignalBlocker::SignalBlocker()
 
26
{
 
27
    sigset_t all_signals;
 
28
    sigfillset(&all_signals);
 
29
 
 
30
    if (auto error = pthread_sigmask(SIG_BLOCK, &all_signals, &previous_set))
 
31
        BOOST_THROW_EXCEPTION((
 
32
                    std::system_error{error,
 
33
                                      std::system_category(),
 
34
                                      "Failed to block signals"}));
 
35
}
 
36
 
 
37
mir::SignalBlocker::~SignalBlocker() noexcept(false)
 
38
{
 
39
    if (auto error = pthread_sigmask(SIG_SETMASK, &previous_set, nullptr))
 
40
    {
 
41
        if (!std::uncaught_exception())
 
42
        {
 
43
            BOOST_THROW_EXCEPTION((std::system_error{error,
 
44
                                                     std::system_category(),
 
45
                                                     "Failed to restore signal mask"}));
 
46
        }
 
47
    }
 
48
}