blob: 52e9c9efa1e22c4c30651d071009ab8a2420d903 [file] [log] [blame]
Chris Masoned903c3b2011-05-12 15:35:46 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08005#include <string>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08006#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -07007
Darin Petkov1023a602010-08-30 13:47:51 -07008#include <base/at_exit.h>
9#include <base/command_line.h>
Darin Petkov30291ed2010-11-12 10:23:06 -080010#include <base/file_util.h>
Darin Petkov1023a602010-08-30 13:47:51 -070011#include <base/logging.h>
12#include <base/string_util.h>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080013#include <gflags/gflags.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000014#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070015#include <metrics/metrics_library.h>
Chris Masone4dc2ada2010-09-23 12:43:03 -070016#include <sys/types.h>
17#include <sys/stat.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070018
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070019#include "update_engine/dbus_constants.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080020#include "update_engine/dbus_interface.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/dbus_service.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070022#include "update_engine/prefs.h"
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070023#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070024#include "update_engine/terminator.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070025#include "update_engine/update_attempter.h"
Darin Petkov1023a602010-08-30 13:47:51 -070026#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070027
28extern "C" {
29#include "update_engine/update_engine.dbusserver.h"
30}
31
32DEFINE_bool(logtostderr, false,
33 "Write logs to stderr instead of to a file in log_dir.");
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070034DEFINE_bool(foreground, false,
35 "Don't daemon()ize; run in foreground.");
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080036
37using std::string;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080038using std::vector;
39
40namespace chromeos_update_engine {
41
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070042gboolean UpdateBootFlags(void* arg) {
Darin Petkov61635a92011-05-18 16:20:36 -070043 reinterpret_cast<UpdateAttempter*>(arg)->UpdateBootFlags();
44 return FALSE; // Don't call this callback again
45}
46
47gboolean BroadcastStatus(void* arg) {
48 reinterpret_cast<UpdateAttempter*>(arg)->BroadcastStatus();
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070049 return FALSE; // Don't call this callback again
50}
51
Andrew de los Reyes73520672010-05-10 13:44:58 -070052namespace {
53
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070054void SetupDbusService(UpdateEngineService* service) {
55 DBusGConnection *bus;
56 DBusGProxy *proxy;
57 GError *error = NULL;
58
59 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
60 if (!bus) {
61 LOG(FATAL) << "Failed to get bus";
62 }
63 proxy = dbus_g_proxy_new_for_name(bus,
64 DBUS_SERVICE_DBUS,
65 DBUS_PATH_DBUS,
66 DBUS_INTERFACE_DBUS);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070067 guint32 request_name_ret;
68 if (!org_freedesktop_DBus_request_name(proxy,
69 kUpdateEngineServiceName,
70 0,
71 &request_name_ret,
72 &error)) {
73 LOG(FATAL) << "Failed to get name: " << error->message;
74 }
75 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
76 g_warning("Got result code %u from requesting name", request_name_ret);
77 g_error_free(error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070078 LOG(FATAL) << "Got result code " << request_name_ret
79 << " from requesting name, but expected "
80 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
81 }
82 dbus_g_connection_register_g_object(bus,
83 "/org/chromium/UpdateEngine",
84 G_OBJECT(service));
85}
86
Darin Petkov30291ed2010-11-12 10:23:06 -080087void SetupLogSymlink(const string& symlink_path, const string& log_path) {
88 // TODO(petkov): To ensure a smooth transition between non-timestamped and
89 // timestamped logs, move an existing log to start the first timestamped
90 // one. This code can go away once all clients are switched to this version or
91 // we stop caring about the old-style logs.
92 if (utils::FileExists(symlink_path.c_str()) &&
93 !utils::IsSymlink(symlink_path.c_str())) {
94 file_util::ReplaceFile(FilePath(symlink_path), FilePath(log_path));
95 }
96 file_util::Delete(FilePath(symlink_path), true);
97 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
98 PLOG(ERROR) << "Unable to create symlink " << symlink_path
99 << " pointing at " << log_path;
100 }
101}
102
103string GetTimeAsString(time_t utime) {
104 struct tm tm;
105 CHECK(localtime_r(&utime, &tm) == &tm);
106 char str[16];
107 CHECK(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm) == 15);
108 return str;
109}
110
111string SetupLogFile(const string& kLogsRoot) {
112 const string kLogSymlink = kLogsRoot + "/update_engine.log";
113 const string kLogsDir = kLogsRoot + "/update_engine";
114 const string kLogPath =
115 StringPrintf("%s/update_engine.%s",
116 kLogsDir.c_str(),
117 GetTimeAsString(::time(NULL)).c_str());
118 mkdir(kLogsDir.c_str(), 0755);
119 SetupLogSymlink(kLogSymlink, kLogPath);
120 return kLogSymlink;
121}
122
123void SetupLogging() {
124 // Log to stderr initially.
125 logging::InitLogging(NULL,
126 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
127 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700128 logging::APPEND_TO_OLD_LOG_FILE,
129 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800130 if (FLAGS_logtostderr) {
131 return;
132 }
133 const string log_file = SetupLogFile("/var/log");
134 logging::InitLogging(log_file.c_str(),
135 logging::LOG_ONLY_TO_FILE,
136 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700137 logging::APPEND_TO_OLD_LOG_FILE,
138 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800139}
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800140
Andrew de los Reyes73520672010-05-10 13:44:58 -0700141} // namespace {}
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800142} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143
rspangler@google.com49fdf182009-10-10 00:57:34 +0000144int main(int argc, char** argv) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700145 ::g_type_init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000146 g_thread_init(NULL);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700147 dbus_g_thread_init();
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700148 base::AtExitManager exit_manager; // Required for base/rand_util.h.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700149 chromeos_update_engine::Terminator::Init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000150 chromeos_update_engine::Subprocess::Init();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800151 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700152 CommandLine::Init(argc, argv);
Darin Petkov30291ed2010-11-12 10:23:06 -0800153 chromeos_update_engine::SetupLogging();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700154 if (!FLAGS_foreground)
155 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
156
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800157 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700158
Chris Masone4dc2ada2010-09-23 12:43:03 -0700159 // Ensure that all written files have safe permissions.
160 // This is a mask, so we _block_ execute for the owner, and ALL
161 // permissions for other users.
162 // Done _after_ log file creation.
163 umask(S_IXUSR | S_IRWXG | S_IRWXO);
164
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800165 // Create the single GMainLoop
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700166 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800167
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700168 chromeos_update_engine::Prefs prefs;
169 LOG_IF(ERROR, !prefs.Init(FilePath("/var/lib/update_engine/prefs")))
170 << "Failed to initialize preferences.";
171
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700172 MetricsLibrary metrics_lib;
173 metrics_lib.Init();
174
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700175 // Create the update attempter:
Andrew de los Reyes45168102010-11-22 11:13:50 -0800176 chromeos_update_engine::ConcreteDbusGlib dbus;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700177 chromeos_update_engine::UpdateAttempter update_attempter(&prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800178 &metrics_lib,
179 &dbus);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800180
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700181 // Create the dbus service object:
182 dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE,
183 &dbus_glib_update_engine_service_object_info);
184 UpdateEngineService* service =
185 UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
186 service->update_attempter_ = &update_attempter;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700187 update_attempter.set_dbus_service(service);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188 chromeos_update_engine::SetupDbusService(service);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800189
Darin Petkov1023a602010-08-30 13:47:51 -0700190 // Schedule periodic update checks.
191 chromeos_update_engine::UpdateCheckScheduler scheduler(&update_attempter);
192 scheduler.Run();
Andrew de los Reyes73520672010-05-10 13:44:58 -0700193
Darin Petkoveee26ee2010-10-22 11:05:46 -0700194 // Update boot flags after 45 seconds.
195 g_timeout_add_seconds(45,
196 &chromeos_update_engine::UpdateBootFlags,
197 &update_attempter);
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -0700198
Darin Petkov61635a92011-05-18 16:20:36 -0700199 // Broadcast the update engine status on startup to ensure consistent system
200 // state on crashes.
201 g_idle_add(&chromeos_update_engine::BroadcastStatus, &update_attempter);
202
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700203 // Run the main loop until exit time:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800204 g_main_loop_run(loop);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700205
206 // Cleanup:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800207 g_main_loop_unref(loop);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700208 update_attempter.set_dbus_service(NULL);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700209 g_object_unref(G_OBJECT(service));
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800210
211 LOG(INFO) << "Chrome OS Update Engine terminating";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212 return 0;
213}