| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium 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 "update_engine/omaha_request_prep_action.h" | 5 #include "update_engine/omaha_request_params.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 6 #include <sys/utsname.h> | 9 #include <sys/utsname.h> |
| 7 #include <errno.h> | 10 |
| 8 #include <map> | 11 #include <map> |
| 9 #include <string> | 12 #include <string> |
| 13 |
| 10 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 11 #include "update_engine/simple_key_value_store.h" | 15 #include "update_engine/simple_key_value_store.h" |
| 12 #include "update_engine/utils.h" | 16 #include "update_engine/utils.h" |
| 13 | 17 |
| 14 using std::map; | 18 using std::map; |
| 15 using std::string; | 19 using std::string; |
| 16 | 20 |
| 17 // This gathers local system information and prepares info used by the | |
| 18 // update check action. | |
| 19 | |
| 20 namespace { | 21 namespace { |
| 21 const string OmahaIdPath() { | 22 const string OmahaIdPath() { |
| 22 return string(chromeos_update_engine::utils::kStatefulPartition) + | 23 return string(chromeos_update_engine::utils::kStatefulPartition) + |
| 23 "/etc/omaha_id"; | 24 "/etc/omaha_id"; |
| 24 } | 25 } |
| 25 } // namespace {} | 26 } // namespace {} |
| 26 | 27 |
| 27 namespace chromeos_update_engine { | 28 namespace chromeos_update_engine { |
| 28 | 29 |
| 29 void OmahaRequestPrepAction::PerformAction() { | 30 bool OmahaRequestDeviceParams::Init() { |
| 30 // TODO(adlr): honor force_full_update_ | 31 TEST_AND_RETURN_FALSE(GetMachineId(&machine_id)); |
| 31 ScopedActionCompleter completer(processor_, this); | 32 user_id = machine_id; |
| 32 string machine_id; | 33 os_platform = OmahaRequestParams::kOsPlatform; |
| 33 TEST_AND_RETURN(GetMachineId(&machine_id)); | 34 os_version = OmahaRequestParams::kOsVersion; |
| 34 const string version(GetLsbValue("CHROMEOS_RELEASE_VERSION", "")); | 35 app_version = GetLsbValue("CHROMEOS_RELEASE_VERSION", ""); |
| 35 const string sp(version + "_" + GetMachineType()); | 36 os_sp = app_version + "_" + GetMachineType(); |
| 36 const string track(GetLsbValue("CHROMEOS_RELEASE_TRACK", "")); | 37 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", ""); |
| 37 const string update_url(GetLsbValue("CHROMEOS_AUSERVER", | 38 app_id = OmahaRequestParams::kAppId; |
| 38 OmahaRequestParams::kUpdateUrl)); | 39 app_lang = "en-US"; |
| 39 const string board(GetLsbValue("CHROMEOS_RELEASE_BOARD", "")); | 40 app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", ""); |
| 40 | 41 update_url = GetLsbValue("CHROMEOS_AUSERVER", |
| 41 OmahaRequestParams out(machine_id, // machine_id | 42 OmahaRequestParams::kUpdateUrl); |
| 42 machine_id, // user_id (use machine_id) | 43 return true; |
| 43 OmahaRequestParams::kOsPlatform, | |
| 44 OmahaRequestParams::kOsVersion, | |
| 45 sp, // e.g. 0.2.3.3_i686 | |
| 46 board, // e.g. x86-generic | |
| 47 OmahaRequestParams::kAppId, | |
| 48 version, // app version (from lsb-release) | |
| 49 "en-US", // lang | |
| 50 track, // track | |
| 51 update_url); | |
| 52 | |
| 53 CHECK(HasOutputPipe()); | |
| 54 SetOutputObject(out); | |
| 55 completer.set_success(true); | |
| 56 } | 44 } |
| 57 | 45 |
| 58 namespace { | 46 namespace { |
| 59 const size_t kGuidDataByteLength = 128 / 8; | 47 const size_t kGuidDataByteLength = 128 / 8; |
| 60 const string::size_type kGuidStringLength = 38; | 48 const string::size_type kGuidStringLength = 38; |
| 61 // Formats 16 bytes (128 bits) of data as a GUID: | 49 // Formats 16 bytes (128 bits) of data as a GUID: |
| 62 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit | 50 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit |
| 63 string GuidFromData(const unsigned char data[kGuidDataByteLength]) { | 51 string GuidFromData(const unsigned char data[kGuidDataByteLength]) { |
| 64 return StringPrintf( | 52 return StringPrintf( |
| 65 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}", | 53 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}", |
| 66 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], | 54 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], |
| 67 data[8], data[9], data[10], data[11], data[12], data[13], data[14], | 55 data[8], data[9], data[10], data[11], data[12], data[13], data[14], |
| 68 data[15]); | 56 data[15]); |
| 69 } | 57 } |
| 70 } | 58 } |
| 71 | 59 |
| 72 // Returns true on success. | 60 // Returns true on success. |
| 73 bool OmahaRequestPrepAction::GetMachineId(std::string* out_id) const { | 61 bool OmahaRequestDeviceParams::GetMachineId(std::string* out_id) const { |
| 74 // See if we have an existing Machine ID | 62 // Checks if we have an existing Machine ID. |
| 75 const string omaha_id_path = root_ + OmahaIdPath(); | 63 const string omaha_id_path = root_ + OmahaIdPath(); |
| 76 | 64 |
| 77 if (utils::ReadFileToString(omaha_id_path, out_id) && | 65 if (utils::ReadFileToString(omaha_id_path, out_id) && |
| 78 out_id->size() == kGuidStringLength) { | 66 out_id->size() == kGuidStringLength) { |
| 79 return true; | 67 return true; |
| 80 } | 68 } |
| 81 | 69 |
| 82 // Create a new ID | 70 // Creates a new ID. |
| 83 int rand_fd = open("/dev/urandom", O_RDONLY, 0); | 71 int rand_fd = open("/dev/urandom", O_RDONLY, 0); |
| 84 TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0); | 72 TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0); |
| 85 ScopedFdCloser rand_fd_closer(&rand_fd); | 73 ScopedFdCloser rand_fd_closer(&rand_fd); |
| 86 unsigned char buf[kGuidDataByteLength]; | 74 unsigned char buf[kGuidDataByteLength]; |
| 87 size_t bytes_read = 0; | 75 size_t bytes_read = 0; |
| 88 while (bytes_read < sizeof(buf)) { | 76 while (bytes_read < sizeof(buf)) { |
| 89 ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read); | 77 ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read); |
| 90 TEST_AND_RETURN_FALSE_ERRNO(rc > 0); | 78 TEST_AND_RETURN_FALSE_ERRNO(rc > 0); |
| 91 bytes_read += rc; | 79 bytes_read += rc; |
| 92 } | 80 } |
| 93 string guid = GuidFromData(buf); | 81 string guid = GuidFromData(buf); |
| 94 TEST_AND_RETURN_FALSE( | 82 TEST_AND_RETURN_FALSE( |
| 95 utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size())); | 83 utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size())); |
| 96 *out_id = guid; | 84 *out_id = guid; |
| 97 return true; | 85 return true; |
| 98 } | 86 } |
| 99 | 87 |
| 100 string OmahaRequestPrepAction::GetLsbValue( | 88 string OmahaRequestDeviceParams::GetLsbValue( |
| 101 const string& key, const string& default_value) const { | 89 const string& key, const string& default_value) const { |
| 102 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", | 90 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", |
| 103 "/etc/lsb-release"}; | 91 "/etc/lsb-release"}; |
| 104 for (unsigned int i = 0; i < arraysize(files); i++) { | 92 for (unsigned int i = 0; i < arraysize(files); ++i) { |
| 105 // TODO(adlr): make sure files checked are owned as root (and all | 93 // TODO(adlr): make sure files checked are owned as root (and all |
| 106 // their parents are recursively, too). | 94 // their parents are recursively, too). |
| 107 string file_data; | 95 string file_data; |
| 108 if (!utils::ReadFileToString(root_ + files[i], &file_data)) | 96 if (!utils::ReadFileToString(root_ + files[i], &file_data)) |
| 109 continue; | 97 continue; |
| 110 | 98 |
| 111 map<string, string> data = simple_key_value_store::ParseString(file_data); | 99 map<string, string> data = simple_key_value_store::ParseString(file_data); |
| 112 if (utils::MapContainsKey(data, key)) | 100 if (utils::MapContainsKey(data, key)) |
| 113 return data[key]; | 101 return data[key]; |
| 114 } | 102 } |
| 115 // not found | 103 // not found |
| 116 return default_value; | 104 return default_value; |
| 117 } | 105 } |
| 118 | 106 |
| 119 string OmahaRequestPrepAction::GetMachineType() const { | 107 string OmahaRequestDeviceParams::GetMachineType() const { |
| 120 struct utsname buf; | 108 struct utsname buf; |
| 121 string ret; | 109 string ret; |
| 122 if (uname(&buf) == 0) | 110 if (uname(&buf) == 0) |
| 123 ret = buf.machine; | 111 ret = buf.machine; |
| 124 return ret; | 112 return ret; |
| 125 } | 113 } |
| 126 | 114 |
| 127 } // namespace chromeos_update_engine | 115 } // namespace chromeos_update_engine |
| OLD | NEW |