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_DAEMONS_DAEMON_H_
6 #define LIBBRILLO_BRILLO_DAEMONS_DAEMON_H_
7 
8 #include <string>
9 
10 #include <base/at_exit.h>
11 #include <base/macros.h>
12 #include <base/message_loop/message_loop.h>
13 #include <brillo/asynchronous_signal_handler.h>
14 #include <brillo/brillo_export.h>
15 #include <brillo/message_loops/base_message_loop.h>
16 
17 struct signalfd_siginfo;
18 
19 namespace brillo {
20 
21 // Daemon is a simple base class for system daemons. It provides a lot
22 // of useful facilities such as a message loop, handling of SIGTERM, SIGINT, and
23 // SIGHUP system signals.
24 // You can use this class directly to implement your daemon or you can
25 // specialize it by creating your own class and deriving it from
26 // brillo::Daemon. Override some of the virtual methods provide to fine-tune
27 // its behavior to suit your daemon's needs.
28 class BRILLO_EXPORT Daemon : public AsynchronousSignalHandlerInterface {
29  public:
30   Daemon();
31   virtual ~Daemon();
32 
33   // Performs proper initialization of the daemon and runs the message loop.
34   // Blocks until the daemon is finished. The return value is the error
35   // code that should be returned from daemon's main(). Returns EX_OK (0) on
36   // success.
37   virtual int Run();
38 
39   // Can be used by call-backs to trigger shut-down of a running message loop.
40   // Calls QuiteWithExitCode(EX_OK);
41   // WARNING: This method (as well as QuitWithExitCode) can only be called when
42   // the message loop is running (that is, during Daemon::Run() call). Calling
43   // these methods before (e.g. during OnInit()) or after (e.g in OnShutdown())
44   // will lead to abnormal process termination.
45   void Quit();
46 
47   // |exit_code| is the status code to be returned when the daemon process
48   // quits. See the warning for Quit() above regarding the allowed scope for
49   // this method.
50   void QuitWithExitCode(int exit_code);
51 
52   // AsynchronousSignalHandlerInterface overrides.
53   // Register/unregister custom signal handlers for the daemon. The semantics
54   // are identical to AsynchronousSignalHandler::RegisterHandler and
55   // AsynchronousSignalHandler::UnregisterHandler, except that handlers for
56   // SIGTERM, SIGINT, and SIGHUP cannot be modified.
57   void RegisterHandler(
58       int signal, const
59       AsynchronousSignalHandlerInterface::SignalHandler& callback) override;
60   void UnregisterHandler(int signal) override;
61 
62  protected:
63   // Overload to provide your own initialization code that should happen just
64   // before running the message loop. Return EX_OK (0) on success or any other
65   // non-zero error codes. If an error is returned, the message loop execution
66   // is aborted and Daemon::Run() exits early.
67   // When overloading, make sure you call the base implementation of OnInit().
68   virtual int OnInit();
69   // Called when the message loops exits and before Daemon::Run() returns.
70   // Overload to clean up the data that was set up during OnInit().
71   // |return_code| contains the current error code that will be returned from
72   // Run(). You can override this value with your own error code if needed.
73   // When overloading, make sure you call the base implementation of
74   // OnShutdown().
75   virtual void OnShutdown(int* exit_code);
76   // Called when the SIGHUP signal is received. In response to this call, your
77   // daemon could reset/reload the configuration and re-initialize its state
78   // as if the process has been reloaded.
79   // Return true if the signal was processed successfully and the daemon
80   // reset its configuration. Returning false will force the daemon to
81   // quit (and subsequently relaunched by an upstart job, if one is configured).
82   // The default implementation just returns false (unhandled), which terminates
83   // the daemon, so do not call the base implementation of OnRestart() from
84   // your overload.
85   virtual bool OnRestart();
86 
87   // Returns a delegate to Quit() method in the base::RunLoop instance.
QuitClosure()88   base::Closure QuitClosure() const {
89     return brillo_message_loop_.QuitClosure();
90   }
91 
92  private:
93   // Called when SIGTERM/SIGINT signals are received.
94   bool Shutdown(const signalfd_siginfo& info);
95   // Called when SIGHUP signal is received.
96   bool Restart(const signalfd_siginfo& info);
97 
98   // |at_exit_manager_| must be first to make sure it is initialized before
99   // other members, especially the |message_loop_|.
100   base::AtExitManager at_exit_manager_;
101   // The main message loop for the daemon.
102   base::MessageLoopForIO message_loop_;
103   // The brillo wrapper for the main message loop.
104   BaseMessageLoop brillo_message_loop_{&message_loop_};
105   // A helper to dispatch signal handlers asynchronously, so that the main
106   // system signal handler returns as soon as possible.
107   AsynchronousSignalHandler async_signal_handler_;
108   // Process exit code specified in QuitWithExitCode() method call.
109   int exit_code_;
110 
111   DISALLOW_COPY_AND_ASSIGN(Daemon);
112 };
113 
114 }  // namespace brillo
115 
116 #endif  // LIBBRILLO_BRILLO_DAEMONS_DAEMON_H_
117