blob: 9e144a0ebbfb57bbf694d99287a9b78edef5f3a1 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/omaha_request_prep_action.h"
6#include <sys/utsname.h>
Andrew de los Reyes970bb282009-12-09 16:34:04 -08007#include <errno.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00008#include <string>
Andrew de los Reyes970bb282009-12-09 16:34:04 -08009#include "base/string_util.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000010#include "update_engine/utils.h"
11
12using std::string;
13
14// This gathers local system information and prepares info used by the
15// update check action.
16
Andrew de los Reyes970bb282009-12-09 16:34:04 -080017namespace {
18const string OmahaIdPath() {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080019 return string(chromeos_update_engine::utils::kStatefulPartition) +
20 "/etc/omaha_id";
Andrew de los Reyes970bb282009-12-09 16:34:04 -080021}
22} // namespace {}
23
adlr@google.com3defe6a2009-12-04 20:57:17 +000024namespace chromeos_update_engine {
25
26void OmahaRequestPrepAction::PerformAction() {
27 // TODO(adlr): honor force_full_update_
Andrew de los Reyes970bb282009-12-09 16:34:04 -080028 ScopedActionCompleter completer(processor_, this);
29 string machine_id;
30 TEST_AND_RETURN(GetMachineId(&machine_id));
adlr@google.com3defe6a2009-12-04 20:57:17 +000031 const string version(GetLsbValue("GOOGLE_RELEASE"));
32 const string sp(version + "_" + GetMachineType());
33 const string track(GetLsbValue("GOOGLE_TRACK"));
34
35 UpdateCheckParams out(machine_id, // machine_id
36 machine_id, // user_id (use machine_id)
37 UpdateCheckParams::kOsPlatform,
38 UpdateCheckParams::kOsVersion,
39 sp, // e.g. 0.2.3.3_i686
40 UpdateCheckParams::kAppId,
41 version, // app version (from lsb-release)
42 "en-US", //lang
43 track); // track
44
45 CHECK(HasOutputPipe());
46 SetOutputObject(out);
Andrew de los Reyes970bb282009-12-09 16:34:04 -080047 completer.set_success(true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000048}
49
Andrew de los Reyes970bb282009-12-09 16:34:04 -080050namespace {
51const size_t kGuidDataByteLength = 128 / 8;
52const string::size_type kGuidStringLength = 38;
53// Formats 16 bytes (128 bits) of data as a GUID:
54// "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit
55string GuidFromData(const unsigned char data[kGuidDataByteLength]) {
56 return StringPrintf(
57 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
58 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
59 data[8], data[9], data[10], data[11], data[12], data[13], data[14],
60 data[15]);
61}
62}
63
64// Returns true on success.
65bool OmahaRequestPrepAction::GetMachineId(std::string* out_id) const {
66 // See if we have an existing Machine ID
67 const string omaha_id_path = root_ + OmahaIdPath();
68
69 if (utils::ReadFileToString(omaha_id_path, out_id) &&
70 out_id->size() == kGuidStringLength) {
71 return true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000072 }
Andrew de los Reyes970bb282009-12-09 16:34:04 -080073
74 // Create a new ID
75 int rand_fd = open("/dev/urandom", O_RDONLY, 0);
76 TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0);
77 ScopedFdCloser rand_fd_closer(&rand_fd);
78 unsigned char buf[kGuidDataByteLength];
79 size_t bytes_read = 0;
80 while (bytes_read < sizeof(buf)) {
81 ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read);
82 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
83 bytes_read += rc;
84 }
85 string guid = GuidFromData(buf);
86 TEST_AND_RETURN_FALSE(
87 utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size()));
88 *out_id = guid;
89 return true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000090}
91
92std::string OmahaRequestPrepAction::GetLsbValue(const std::string& key) const {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080093 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release",
adlr@google.com3defe6a2009-12-04 20:57:17 +000094 "/etc/lsb-release"};
95 for (unsigned int i = 0; i < arraysize(files); i++) {
96 string file_data;
97 if (!utils::ReadFileToString(root_ + files[i], &file_data))
98 continue;
99 string::size_type pos = 0;
100 if (!utils::StringHasPrefix(file_data, key + "=")) {
101 pos = file_data.find(string("\n") + key + "=");
102 if (pos != string::npos)
103 pos++; // advance past \n
104 }
105 if (pos == string::npos)
106 continue;
107 pos += key.size() + 1; // advance past the key and the '='
108 string::size_type endpos = file_data.find('\n', pos);
109 string::size_type length =
110 (endpos == string::npos ? string::npos : endpos - pos);
111 return file_data.substr(pos, length);
112 }
113 // not found
114 return "";
115}
116
117std::string OmahaRequestPrepAction::GetMachineType() const {
118 struct utsname buf;
119 string ret;
120 if (uname(&buf) == 0)
121 ret = buf.machine;
122 return ret;
123}
124
125} // namespace chromeos_update_engine