aboutsummaryrefslogtreecommitdiff
path: root/brillo/asynchronous_signal_handler.h
blob: 4b0edce08bfe20b8f736b1d02a3921404317f8e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_H_
#define LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_H_

#include <sys/signalfd.h>

#include <map>
#include <memory>

#include <base/callback.h>
#include <base/files/file_descriptor_watcher_posix.h>
#include <base/files/scoped_file.h>
#include <brillo/asynchronous_signal_handler_interface.h>
#include <brillo/brillo_export.h>

namespace brillo {
// Sets up signal handlers for registered signals, and converts signal receipt
// into a write on a pipe. Watches that pipe for data and, when some appears,
// execute the associated callback.
class BRILLO_EXPORT AsynchronousSignalHandler final :
    public AsynchronousSignalHandlerInterface {
 public:
  using AsynchronousSignalHandlerInterface::SignalHandler;

  AsynchronousSignalHandler();
  ~AsynchronousSignalHandler() override;

  AsynchronousSignalHandler(const AsynchronousSignalHandler&) = delete;
  AsynchronousSignalHandler&
  operator=(const AsynchronousSignalHandler&) = delete;

  // Initialize the handler.
  void Init();

  // AsynchronousSignalHandlerInterface overrides.
  void RegisterHandler(int signal, const SignalHandler& callback) override;
  void UnregisterHandler(int signal) override;

 private:
  // Called from the main loop when we can read from |descriptor_|, indicated
  // that a signal was processed.
  void OnReadable();

  // Updates the set of signals that this handler listens to.
  BRILLO_PRIVATE void UpdateSignals();

  // Map from signal to its registered callback.
  using Callbacks = std::map<int, SignalHandler>;
  Callbacks registered_callbacks_;

  // File descriptor for accepting signals indicated by |signal_mask_|.
  base::ScopedFD descriptor_;

  // Controller used to manage watching of signalling pipe.
  std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_;

  // A set of signals to be handled after the dispatcher is running.
  sigset_t signal_mask_;

  // A copy of the signal mask before the dispatcher starts, which will be
  // used to restore to the original state when the dispatcher stops.
  sigset_t saved_signal_mask_;
};

}  // namespace brillo

#endif  // LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_H_