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
13namespace 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.
18class BRILLO_EXPORT AsynchronousSignalHandlerInterface {
19 public:
20  virtual ~AsynchronousSignalHandlerInterface() = default;
21
22  // The callback called when a signal is received.
23  using SignalHandler = base::Callback<bool(const struct signalfd_siginfo&)>;
24
25  // Register a new handler for the given |signal|, replacing any previously
26  // registered handler. |callback| will be called on the thread the
27  // |AsynchronousSignalHandlerInterface| implementation is bound to when a
28  // signal is received. The received |signalfd_siginfo| will be passed to
29  // |callback|. |callback| must returns |true| if the signal handler must be
30  // unregistered, and |false| otherwise. Due to an implementation detail, you
31  // cannot set any sigaction flags you might be accustomed to using. This might
32  // matter if you hoped to use SA_NOCLDSTOP to avoid getting a SIGCHLD when a
33  // child process receives a SIGSTOP.
34  virtual void RegisterHandler(int signal, const SignalHandler& callback) = 0;
35
36  // Unregister a previously registered handler for the given |signal|.
37  virtual void UnregisterHandler(int signal) = 0;
38};
39
40}  // namespace brillo
41
42#endif  // LIBBRILLO_BRILLO_ASYNCHRONOUS_SIGNAL_HANDLER_INTERFACE_H_
43