| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // 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 | 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 "update_engine/omaha_request_prep_action.h" | 5 #include "update_engine/omaha_request_prep_action.h" |
| 6 #include <sys/utsname.h> | 6 #include <sys/utsname.h> |
| 7 #include <errno.h> |
| 7 #include <string> | 8 #include <string> |
| 9 #include "base/string_util.h" |
| 8 #include "update_engine/utils.h" | 10 #include "update_engine/utils.h" |
| 9 | 11 |
| 10 using std::string; | 12 using std::string; |
| 11 | 13 |
| 12 // This gathers local system information and prepares info used by the | 14 // This gathers local system information and prepares info used by the |
| 13 // update check action. | 15 // update check action. |
| 14 | 16 |
| 17 namespace { |
| 18 const string OmahaIdPath() { |
| 19 return chromeos_update_engine::utils::kStatefulPartition + "/etc/omaha_id"; |
| 20 } |
| 21 } // namespace {} |
| 22 |
| 15 namespace chromeos_update_engine { | 23 namespace chromeos_update_engine { |
| 16 | 24 |
| 17 void OmahaRequestPrepAction::PerformAction() { | 25 void OmahaRequestPrepAction::PerformAction() { |
| 18 // TODO(adlr): honor force_full_update_ | 26 // TODO(adlr): honor force_full_update_ |
| 19 const string machine_id(GetMachineId()); | 27 ScopedActionCompleter completer(processor_, this); |
| 28 string machine_id; |
| 29 TEST_AND_RETURN(GetMachineId(&machine_id)); |
| 20 const string version(GetLsbValue("GOOGLE_RELEASE")); | 30 const string version(GetLsbValue("GOOGLE_RELEASE")); |
| 21 const string sp(version + "_" + GetMachineType()); | 31 const string sp(version + "_" + GetMachineType()); |
| 22 const string track(GetLsbValue("GOOGLE_TRACK")); | 32 const string track(GetLsbValue("GOOGLE_TRACK")); |
| 23 | 33 |
| 24 UpdateCheckParams out(machine_id, // machine_id | 34 UpdateCheckParams out(machine_id, // machine_id |
| 25 machine_id, // user_id (use machine_id) | 35 machine_id, // user_id (use machine_id) |
| 26 UpdateCheckParams::kOsPlatform, | 36 UpdateCheckParams::kOsPlatform, |
| 27 UpdateCheckParams::kOsVersion, | 37 UpdateCheckParams::kOsVersion, |
| 28 sp, // e.g. 0.2.3.3_i686 | 38 sp, // e.g. 0.2.3.3_i686 |
| 29 UpdateCheckParams::kAppId, | 39 UpdateCheckParams::kAppId, |
| 30 version, // app version (from lsb-release) | 40 version, // app version (from lsb-release) |
| 31 "en-US", //lang | 41 "en-US", //lang |
| 32 track); // track | 42 track); // track |
| 33 | 43 |
| 34 CHECK(HasOutputPipe()); | 44 CHECK(HasOutputPipe()); |
| 35 SetOutputObject(out); | 45 SetOutputObject(out); |
| 36 processor_->ActionComplete(this, true); | 46 completer.set_success(true); |
| 37 } | 47 } |
| 38 | 48 |
| 39 std::string OmahaRequestPrepAction::GetMachineId() const { | 49 namespace { |
| 40 FILE* fp = popen("/sbin/ifconfig", "r"); | 50 const size_t kGuidDataByteLength = 128 / 8; |
| 41 if (!fp) | 51 const string::size_type kGuidStringLength = 38; |
| 42 return ""; | 52 // Formats 16 bytes (128 bits) of data as a GUID: |
| 43 string data; | 53 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit |
| 44 for (;;) { | 54 string GuidFromData(const unsigned char data[kGuidDataByteLength]) { |
| 45 char buffer[1000]; | 55 return StringPrintf( |
| 46 size_t r = fread(buffer, 1, sizeof(buffer), fp); | 56 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}", |
| 47 if (r <= 0) | 57 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], |
| 48 break; | 58 data[8], data[9], data[10], data[11], data[12], data[13], data[14], |
| 49 data.insert(data.end(), buffer, buffer + r); | 59 data[15]); |
| 60 } |
| 61 } |
| 62 |
| 63 // Returns true on success. |
| 64 bool OmahaRequestPrepAction::GetMachineId(std::string* out_id) const { |
| 65 // See if we have an existing Machine ID |
| 66 const string omaha_id_path = root_ + OmahaIdPath(); |
| 67 |
| 68 if (utils::ReadFileToString(omaha_id_path, out_id) && |
| 69 out_id->size() == kGuidStringLength) { |
| 70 return true; |
| 50 } | 71 } |
| 51 fclose(fp); | 72 |
| 52 // scan data for MAC address | 73 // Create a new ID |
| 53 string::size_type pos = data.find(" HWaddr "); | 74 int rand_fd = open("/dev/urandom", O_RDONLY, 0); |
| 54 if (pos == string::npos) | 75 TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0); |
| 55 return ""; | 76 ScopedFdCloser rand_fd_closer(&rand_fd); |
| 56 // 3 * 6 - 1 is the number of bytes of the hwaddr. | 77 unsigned char buf[kGuidDataByteLength]; |
| 57 return data.substr(pos + strlen(" HWaddr "), 3 * 6 - 1); | 78 size_t bytes_read = 0; |
| 79 while (bytes_read < sizeof(buf)) { |
| 80 ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read); |
| 81 TEST_AND_RETURN_FALSE_ERRNO(rc > 0); |
| 82 bytes_read += rc; |
| 83 } |
| 84 string guid = GuidFromData(buf); |
| 85 TEST_AND_RETURN_FALSE( |
| 86 utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size())); |
| 87 *out_id = guid; |
| 88 return true; |
| 58 } | 89 } |
| 59 | 90 |
| 60 std::string OmahaRequestPrepAction::GetLsbValue(const std::string& key) const { | 91 std::string OmahaRequestPrepAction::GetLsbValue(const std::string& key) const { |
| 61 string files[] = {utils::kStatefulPartition + "/etc/lsb-release", | 92 string files[] = {utils::kStatefulPartition + "/etc/lsb-release", |
| 62 "/etc/lsb-release"}; | 93 "/etc/lsb-release"}; |
| 63 for (unsigned int i = 0; i < arraysize(files); i++) { | 94 for (unsigned int i = 0; i < arraysize(files); i++) { |
| 64 string file_data; | 95 string file_data; |
| 65 if (!utils::ReadFileToString(root_ + files[i], &file_data)) | 96 if (!utils::ReadFileToString(root_ + files[i], &file_data)) |
| 66 continue; | 97 continue; |
| 67 string::size_type pos = 0; | 98 string::size_type pos = 0; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 84 | 115 |
| 85 std::string OmahaRequestPrepAction::GetMachineType() const { | 116 std::string OmahaRequestPrepAction::GetMachineType() const { |
| 86 struct utsname buf; | 117 struct utsname buf; |
| 87 string ret; | 118 string ret; |
| 88 if (uname(&buf) == 0) | 119 if (uname(&buf) == 0) |
| 89 ret = buf.machine; | 120 ret = buf.machine; |
| 90 return ret; | 121 return ret; |
| 91 } | 122 } |
| 92 | 123 |
| 93 } // namespace chromeos_update_engine | 124 } // namespace chromeos_update_engine |
| OLD | NEW |