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