diff options
author | Eric Caruso <ejcaruso@chromium.org> | 2018-03-12 18:00:45 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-03-13 19:00:17 -0700 |
commit | 0a5aff886985c2687ccd789a6def7e47c34cd1ac (patch) | |
tree | c1911592c4382bd1025ede8572e646d4aaa283fc | |
parent | 91b37e06780bea8aaa7e80299683460db1b64f55 (diff) | |
download | platform_external_libbrillo-0a5aff886985c2687ccd789a6def7e47c34cd1ac.tar.gz platform_external_libbrillo-0a5aff886985c2687ccd789a6def7e47c34cd1ac.tar.bz2 platform_external_libbrillo-0a5aff886985c2687ccd789a6def7e47c34cd1ac.zip |
libbrillo: provide Daemon method to do init on message loop
In OnInit the message loop may not be instantiated yet, so if
some initialization has to happen asynchronously it might have
any task runner to post tasks to. Allow subclasses of Daemon to
provide a method implementation with initialization that must
take place when the message loop has started and is the current
message loop.
BUG=b:74250103
TEST=with modemfwd
Change-Id: I4c9a35934681a24730b01b60e9bca5e853e104ef
Reviewed-on: https://chromium-review.googlesource.com/959524
Commit-Ready: Eric Caruso <ejcaruso@chromium.org>
Tested-by: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Dan Erat <derat@chromium.org>
-rw-r--r-- | brillo/daemons/daemon.cc | 13 | ||||
-rw-r--r-- | brillo/daemons/daemon.h | 7 |
2 files changed, 20 insertions, 0 deletions
diff --git a/brillo/daemons/daemon.cc b/brillo/daemons/daemon.cc index 2692f0b..1b3d6d2 100644 --- a/brillo/daemons/daemon.cc +++ b/brillo/daemons/daemon.cc @@ -26,6 +26,8 @@ int Daemon::Run() { if (exit_code != EX_OK) return exit_code; + message_loop_.PostTask( + base::Bind(&Daemon::OnEventLoopStartedTask, base::Unretained(this))); message_loop_.Run(); OnShutdown(&exit_code_); @@ -68,6 +70,11 @@ int Daemon::OnInit() { return EX_OK; } +int Daemon::OnEventLoopStarted() { + // Do nothing. + return EX_OK; +} + void Daemon::OnShutdown(int* /* exit_code */) { // Do nothing. } @@ -89,6 +96,12 @@ bool Daemon::Restart(const signalfd_siginfo& /* info */) { return true; // Unregister the signal handler. } +void Daemon::OnEventLoopStartedTask() { + int exit_code = OnEventLoopStarted(); + if (exit_code != EX_OK) + QuitWithExitCode(exit_code); +} + void UpdateLogSymlinks(const base::FilePath& latest_log_symlink, const base::FilePath& previous_log_symlink, const base::FilePath& log_file) { diff --git a/brillo/daemons/daemon.h b/brillo/daemons/daemon.h index 0295da7..a16e04a 100644 --- a/brillo/daemons/daemon.h +++ b/brillo/daemons/daemon.h @@ -68,6 +68,10 @@ class BRILLO_EXPORT Daemon : public AsynchronousSignalHandlerInterface { // is aborted and Daemon::Run() exits early. // When overloading, make sure you call the base implementation of OnInit(). virtual int OnInit(); + // Overload to provide initialization code that should be the first code to + // run on the message loop. Returning something other than EX_OK will cause + // the daemon to exit with that error code. + virtual int OnEventLoopStarted(); // Called when the message loops exits and before Daemon::Run() returns. // Overload to clean up the data that was set up during OnInit(). // |return_code| contains the current error code that will be returned from @@ -97,6 +101,9 @@ class BRILLO_EXPORT Daemon : public AsynchronousSignalHandlerInterface { // Called when SIGHUP signal is received. bool Restart(const signalfd_siginfo& info); + // Actual task posted first to the message loop. + void OnEventLoopStartedTask(); + // |at_exit_manager_| must be first to make sure it is initialized before // other members, especially the |message_loop_|. base::AtExitManager at_exit_manager_; |