Home | History | Annotate | Download | only in brillo
      1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_H_
      6 #define LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_H_
      7 
      8 #include <signal.h>
      9 #include <sys/signalfd.h>
     10 
     11 #include <map>
     12 
     13 #include <base/callback.h>
     14 #include <base/compiler_specific.h>
     15 #include <base/macros.h>
     16 #include <base/message_loop/message_loop.h>
     17 #include <brillo/asynchronous_signal_handler_interface.h>
     18 #include <brillo/brillo_export.h>
     19 #include <brillo/message_loops/message_loop.h>
     20 
     21 namespace brillo {
     22 // Sets up signal handlers for registered signals, and converts signal receipt
     23 // into a write on a pipe. Watches that pipe for data and, when some appears,
     24 // execute the associated callback.
     25 class BRILLO_EXPORT AsynchronousSignalHandler final :
     26     public AsynchronousSignalHandlerInterface {
     27  public:
     28   AsynchronousSignalHandler();
     29   ~AsynchronousSignalHandler() override;
     30 
     31   using AsynchronousSignalHandlerInterface::SignalHandler;
     32 
     33   // Initialize the handler.
     34   void Init();
     35 
     36   // AsynchronousSignalHandlerInterface overrides.
     37   void RegisterHandler(int signal, const SignalHandler& callback) override;
     38   void UnregisterHandler(int signal) override;
     39 
     40  private:
     41   // Called from the main loop when we can read from |descriptor_|, indicated
     42   // that a signal was processed.
     43   void OnFileCanReadWithoutBlocking();
     44 
     45   // Controller used to manage watching of signalling pipe.
     46   MessageLoop::TaskId fd_watcher_task_{MessageLoop::kTaskIdNull};
     47 
     48   // The registered callbacks.
     49   typedef std::map<int, SignalHandler> Callbacks;
     50   Callbacks registered_callbacks_;
     51 
     52   // File descriptor for accepting signals indicated by |signal_mask_|.
     53   int descriptor_;
     54 
     55   // A set of signals to be handled after the dispatcher is running.
     56   sigset_t signal_mask_;
     57 
     58   // A copy of the signal mask before the dispatcher starts, which will be
     59   // used to restore to the original state when the dispatcher stops.
     60   sigset_t saved_signal_mask_;
     61 
     62   // Resets the given signal to its default behavior. Doesn't touch
     63   // |registered_callbacks_|.
     64   BRILLO_PRIVATE void ResetSignal(int signal);
     65 
     66   // Updates the set of signals that this handler listens to.
     67   BRILLO_PRIVATE void UpdateSignals();
     68 
     69   DISALLOW_COPY_AND_ASSIGN(AsynchronousSignalHandler);
     70 };
     71 
     72 }  // namespace brillo
     73 
     74 #endif  // LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_H_
     75