aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/daemons
diff options
context:
space:
mode:
Diffstat (limited to 'brillo/daemons')
-rw-r--r--brillo/daemons/daemon.cc35
-rw-r--r--brillo/daemons/daemon.h20
2 files changed, 54 insertions, 1 deletions
diff --git a/brillo/daemons/daemon.cc b/brillo/daemons/daemon.cc
index 7c7e996..1b3d6d2 100644
--- a/brillo/daemons/daemon.cc
+++ b/brillo/daemons/daemon.cc
@@ -5,9 +5,9 @@
#include <brillo/daemons/daemon.h>
#include <sysexits.h>
+#include <time.h>
#include <base/bind.h>
-#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/logging.h>
#include <base/run_loop.h>
@@ -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,4 +96,30 @@ 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) {
+ base::DeleteFile(previous_log_symlink, false);
+ base::Move(latest_log_symlink, previous_log_symlink);
+ if (!base::CreateSymbolicLink(log_file.BaseName(), latest_log_symlink)) {
+ PLOG(ERROR) << "Unable to create symbolic link from "
+ << latest_log_symlink.value() << " to " << log_file.value();
+ }
+}
+
+std::string GetTimeAsLogString(const base::Time& time) {
+ time_t utime = time.ToTimeT();
+ struct tm tm;
+ CHECK_EQ(localtime_r(&utime, &tm), &tm);
+ char str[16];
+ CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15UL);
+ return std::string(str);
+}
+
} // namespace brillo
diff --git a/brillo/daemons/daemon.h b/brillo/daemons/daemon.h
index 8a11f0f..a16e04a 100644
--- a/brillo/daemons/daemon.h
+++ b/brillo/daemons/daemon.h
@@ -8,8 +8,10 @@
#include <string>
#include <base/at_exit.h>
+#include <base/files/file_path.h>
#include <base/macros.h>
#include <base/message_loop/message_loop.h>
+#include <base/time/time.h>
#include <brillo/asynchronous_signal_handler.h>
#include <brillo/brillo_export.h>
#include <brillo/message_loops/base_message_loop.h>
@@ -66,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
@@ -95,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_;
@@ -109,6 +118,17 @@ class BRILLO_EXPORT Daemon : public AsynchronousSignalHandlerInterface {
DISALLOW_COPY_AND_ASSIGN(Daemon);
};
+// Moves |latest_log_symlink| to |previous_log_symlink| and creates a relative
+// symlink at |latest_log_symlink| pointing to |log_file|.
+// |latest_log_symlink| does not need to exist.
+BRILLO_EXPORT void UpdateLogSymlinks(const base::FilePath& latest_log_symlink,
+ const base::FilePath& previous_log_symlink,
+ const base::FilePath& log_file);
+
+// Formats the current time with "%Y%m%d-%H%M%S".
+// Commonly used for naming log files.
+BRILLO_EXPORT std::string GetTimeAsLogString(const base::Time& time);
+
} // namespace brillo
#endif // LIBBRILLO_BRILLO_DAEMONS_DAEMON_H_