1 // Copyright 2015 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_INTERFACE_H_
6 #define LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_INTERFACE_H_
7 
8 #include <sys/signalfd.h>
9 
10 #include <base/callback.h>
11 #include <brillo/brillo_export.h>
12 
13 namespace brillo {
14 
15 // Sets up signal handlers for registered signals, and converts signal receipt
16 // into a write on a pipe. Watches that pipe for data and, when some appears,
17 // execute the associated callback.
18 class BRILLO_EXPORT AsynchronousSignalHandlerInterface {
19  public:
20   virtual ~AsynchronousSignalHandlerInterface() = default;
21 
22   // The callback called when a signal is received.
23   using SignalHandler =
24       base::RepeatingCallback<bool(const struct signalfd_siginfo&)>;
25 
26   // Register a new handler for the given |signal|, replacing any previously
27   // registered handler. |callback| will be called on the thread the
28   // |AsynchronousSignalHandlerInterface| implementation is bound to when a
29   // signal is received. The received |signalfd_siginfo| will be passed to
30   // |callback|. |callback| must returns |true| if the signal handler must be
31   // unregistered, and |false| otherwise. Due to an implementation detail, you
32   // cannot set any sigaction flags you might be accustomed to using. This might
33   // matter if you hoped to use SA_NOCLDSTOP to avoid getting a SIGCHLD when a
34   // child process receives a SIGSTOP.
35   virtual void RegisterHandler(int signal, const SignalHandler& callback) = 0;
36 
37   // Unregister a previously registered handler for the given |signal|.
38   virtual void UnregisterHandler(int signal) = 0;
39 };
40 
41 }  // namespace brillo
42 
43 #endif  // LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_INTERFACE_H_
44