OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 #include <tr1/memory> | |
7 #include <vector> | 6 #include <vector> |
8 | 7 |
9 #include <base/at_exit.h> | 8 #include <base/at_exit.h> |
10 #include <base/command_line.h> | 9 #include <base/command_line.h> |
10 #include <base/file_util.h> | |
11 #include <base/logging.h> | 11 #include <base/logging.h> |
12 #include <base/string_util.h> | 12 #include <base/string_util.h> |
13 #include <gflags/gflags.h> | 13 #include <gflags/gflags.h> |
14 #include <glib.h> | 14 #include <glib.h> |
15 #include <metrics/metrics_library.h> | 15 #include <metrics/metrics_library.h> |
16 #include <sys/types.h> | 16 #include <sys/types.h> |
17 #include <sys/stat.h> | 17 #include <sys/stat.h> |
18 | 18 |
19 #include "update_engine/dbus_constants.h" | 19 #include "update_engine/dbus_constants.h" |
20 #include "update_engine/dbus_service.h" | 20 #include "update_engine/dbus_service.h" |
21 #include "update_engine/prefs.h" | 21 #include "update_engine/prefs.h" |
22 #include "update_engine/subprocess.h" | 22 #include "update_engine/subprocess.h" |
23 #include "update_engine/terminator.h" | 23 #include "update_engine/terminator.h" |
24 #include "update_engine/update_attempter.h" | 24 #include "update_engine/update_attempter.h" |
25 #include "update_engine/update_check_scheduler.h" | 25 #include "update_engine/update_check_scheduler.h" |
26 | 26 |
27 extern "C" { | 27 extern "C" { |
28 #include "update_engine/update_engine.dbusserver.h" | 28 #include "update_engine/update_engine.dbusserver.h" |
29 } | 29 } |
30 | 30 |
31 DEFINE_bool(logtostderr, false, | 31 DEFINE_bool(logtostderr, false, |
32 "Write logs to stderr instead of to a file in log_dir."); | 32 "Write logs to stderr instead of to a file in log_dir."); |
33 DEFINE_bool(foreground, false, | 33 DEFINE_bool(foreground, false, |
34 "Don't daemon()ize; run in foreground."); | 34 "Don't daemon()ize; run in foreground."); |
35 | 35 |
36 using std::string; | 36 using std::string; |
37 using std::tr1::shared_ptr; | |
38 using std::vector; | 37 using std::vector; |
39 | 38 |
40 namespace chromeos_update_engine { | 39 namespace chromeos_update_engine { |
41 | 40 |
42 gboolean UpdateBootFlags(void* arg) { | 41 gboolean UpdateBootFlags(void* arg) { |
43 UpdateAttempter* attempter = reinterpret_cast<UpdateAttempter*>(arg); | 42 UpdateAttempter* attempter = reinterpret_cast<UpdateAttempter*>(arg); |
44 if (attempter->status() == UPDATE_STATUS_UPDATED_NEED_REBOOT) { | 43 if (attempter->status() == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
45 // Don't update the flags if there's an update that's just been applied and | 44 // Don't update the flags if there's an update that's just been applied and |
46 // we're waiting for reboot because we may end up reverting the update. | 45 // we're waiting for reboot because we may end up reverting the update. |
47 return FALSE; // Don't call this callback again. | 46 return FALSE; // Don't call this callback again. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 g_error_free(error); | 87 g_error_free(error); |
89 LOG(FATAL) << "Got result code " << request_name_ret | 88 LOG(FATAL) << "Got result code " << request_name_ret |
90 << " from requesting name, but expected " | 89 << " from requesting name, but expected " |
91 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; | 90 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; |
92 } | 91 } |
93 dbus_g_connection_register_g_object(bus, | 92 dbus_g_connection_register_g_object(bus, |
94 "/org/chromium/UpdateEngine", | 93 "/org/chromium/UpdateEngine", |
95 G_OBJECT(service)); | 94 G_OBJECT(service)); |
96 } | 95 } |
97 | 96 |
97 void SetUpLogSymlink(const string& symlink_path, const string& log_path) { | |
adlr
2010/11/11 17:23:28
all the other setup functions don't capitalize the
petkov
2010/11/12 18:14:44
copy/paste... fixed.
| |
98 // TODO(petkov): To ensure a smooth transition between non-timestamped and | |
99 // timestamped logs, move an existing log to start the first timestamped | |
100 // one. This code can go away once all clients are switched to this version or | |
101 // we stop caring about the old-style logs. | |
102 if (utils::FileExists(symlink_path.c_str()) && | |
103 !utils::IsSymlink(symlink_path.c_str())) { | |
104 file_util::ReplaceFile(FilePath(symlink_path), FilePath(log_path)); | |
105 } | |
106 file_util::Delete(FilePath(symlink_path), true); | |
107 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) { | |
108 PLOG(ERROR) << "Unable to create symlink " << symlink_path | |
109 << " pointing at " << log_path; | |
110 } | |
111 } | |
112 | |
113 string GetTimeAsString(time_t utime) { | |
114 struct tm tm; | |
115 CHECK(localtime_r(&utime, &tm) == &tm); | |
116 char str[16]; | |
117 CHECK(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm) == 15); | |
118 return str; | |
119 } | |
120 | |
121 string SetupLogFile(const string kLogsRoot) { | |
adlr
2010/11/11 17:23:28
const string&?
(yeah, here it doesn't matter, but
petkov
2010/11/12 18:14:44
Duh.. Done.
| |
122 const string kLogSymlink = kLogsRoot + "/update_engine.log"; | |
123 const string kLogsDir = kLogsRoot + "/update_engine"; | |
124 const string kLogPath = | |
125 StringPrintf("%s/update_engine.%s", | |
126 kLogsDir.c_str(), | |
127 GetTimeAsString(::time(NULL)).c_str()); | |
128 file_util::CreateDirectory(FilePath(kLogsDir)); | |
129 SetUpLogSymlink(kLogSymlink, kLogPath); | |
130 return kLogSymlink; | |
131 } | |
132 | |
133 void SetupLogging() { | |
134 // Log to stderr initially. | |
135 logging::InitLogging(NULL, | |
136 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | |
137 logging::DONT_LOCK_LOG_FILE, | |
138 logging::APPEND_TO_OLD_LOG_FILE); | |
139 if (FLAGS_logtostderr) { | |
140 return; | |
141 } | |
142 const string log_file = SetupLogFile("/var/log"); | |
143 logging::InitLogging(log_file.c_str(), | |
144 logging::LOG_ONLY_TO_FILE, | |
145 logging::DONT_LOCK_LOG_FILE, | |
146 logging::APPEND_TO_OLD_LOG_FILE); | |
147 } | |
98 } // namespace {} | 148 } // namespace {} |
99 | |
100 } // namespace chromeos_update_engine | 149 } // namespace chromeos_update_engine |
101 | 150 |
102 #include "update_engine/subprocess.h" | |
103 | |
104 int main(int argc, char** argv) { | 151 int main(int argc, char** argv) { |
105 ::g_type_init(); | 152 ::g_type_init(); |
106 g_thread_init(NULL); | 153 g_thread_init(NULL); |
107 dbus_g_thread_init(); | 154 dbus_g_thread_init(); |
108 base::AtExitManager exit_manager; // Required for base/rand_util.h. | 155 base::AtExitManager exit_manager; // Required for base/rand_util.h. |
109 chromeos_update_engine::Terminator::Init(); | 156 chromeos_update_engine::Terminator::Init(); |
110 chromeos_update_engine::Subprocess::Init(); | 157 chromeos_update_engine::Subprocess::Init(); |
111 google::ParseCommandLineFlags(&argc, &argv, true); | 158 google::ParseCommandLineFlags(&argc, &argv, true); |
112 CommandLine::Init(argc, argv); | 159 CommandLine::Init(argc, argv); |
113 logging::InitLogging("/var/log/update_engine.log", | 160 chromeos_update_engine::SetupLogging(); |
114 (FLAGS_logtostderr ? | |
115 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG : | |
116 logging::LOG_ONLY_TO_FILE), | |
117 logging::DONT_LOCK_LOG_FILE, | |
118 logging::APPEND_TO_OLD_LOG_FILE); | |
119 if (!FLAGS_foreground) | 161 if (!FLAGS_foreground) |
120 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; | 162 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
121 | 163 |
122 LOG(INFO) << "Chrome OS Update Engine starting"; | 164 LOG(INFO) << "Chrome OS Update Engine starting"; |
123 | 165 |
124 // Ensure that all written files have safe permissions. | 166 // Ensure that all written files have safe permissions. |
125 // This is a mask, so we _block_ execute for the owner, and ALL | 167 // This is a mask, so we _block_ execute for the owner, and ALL |
126 // permissions for other users. | 168 // permissions for other users. |
127 // Done _after_ log file creation. | 169 // Done _after_ log file creation. |
128 umask(S_IXUSR | S_IRWXG | S_IRWXO); | 170 umask(S_IXUSR | S_IRWXG | S_IRWXO); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 g_main_loop_run(loop); | 205 g_main_loop_run(loop); |
164 | 206 |
165 // Cleanup: | 207 // Cleanup: |
166 g_main_loop_unref(loop); | 208 g_main_loop_unref(loop); |
167 update_attempter.set_dbus_service(NULL); | 209 update_attempter.set_dbus_service(NULL); |
168 g_object_unref(G_OBJECT(service)); | 210 g_object_unref(G_OBJECT(service)); |
169 | 211 |
170 LOG(INFO) << "Chrome OS Update Engine terminating"; | 212 LOG(INFO) << "Chrome OS Update Engine terminating"; |
171 return 0; | 213 return 0; |
172 } | 214 } |
OLD | NEW |