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