Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/utils.h" |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 18 | |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <dirent.h> |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 22 | #include <elf.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 23 | #include <errno.h> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 24 | #include <ext2fs/ext2fs.h> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 25 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> |
| 30 | #include <sys/resource.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 35 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 37 | #include <utility> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 38 | #include <vector> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 39 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 40 | #include <base/callback.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 41 | #include <base/files/file_path.h> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 42 | #include <base/files/file_util.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 43 | #include <base/files/scoped_file.h> |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 44 | #include <base/format_macros.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 45 | #include <base/location.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 46 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 47 | #include <base/posix/eintr_wrapper.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 48 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 49 | #include <base/strings/string_number_conversions.h> |
| 50 | #include <base/strings/string_split.h> |
| 51 | #include <base/strings/string_util.h> |
| 52 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 53 | #include <chromeos/data_encoding.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 54 | #include <chromeos/message_loops/message_loop.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 55 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 56 | #include "update_engine/clock_interface.h" |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 57 | #include "update_engine/constants.h" |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 58 | #include "update_engine/file_descriptor.h" |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 59 | #include "update_engine/file_writer.h" |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 60 | #include "update_engine/omaha_request_params.h" |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 61 | #include "update_engine/prefs_interface.h" |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 62 | #include "update_engine/subprocess.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 63 | #include "update_engine/system_state.h" |
| 64 | #include "update_engine/update_attempter.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 66 | using base::Time; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 67 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | using std::min; |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 69 | using std::pair; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 70 | using std::string; |
| 71 | using std::vector; |
| 72 | |
| 73 | namespace chromeos_update_engine { |
| 74 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 75 | namespace { |
| 76 | |
| 77 | // The following constants control how UnmountFilesystem should retry if |
| 78 | // umount() fails with an errno EBUSY, i.e. retry 5 times over the course of |
| 79 | // one second. |
| 80 | const int kUnmountMaxNumOfRetries = 5; |
| 81 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 82 | |
| 83 | // Number of bytes to read from a file to attempt to detect its contents. Used |
| 84 | // in GetFileFormat. |
| 85 | const int kGetFileFormatMaxHeaderSize = 32; |
| 86 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 87 | // Return true if |disk_name| is an MTD or a UBI device. Note that this test is |
| 88 | // simply based on the name of the device. |
| 89 | bool IsMtdDeviceName(const string& disk_name) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 90 | return base::StartsWithASCII(disk_name, "/dev/ubi", true) || |
| 91 | base::StartsWithASCII(disk_name, "/dev/mtd", true); |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Return the device name for the corresponding partition on a NAND device. |
| 95 | // WARNING: This function returns device names that are not mountable. |
| 96 | string MakeNandPartitionName(int partition_num) { |
| 97 | switch (partition_num) { |
| 98 | case 2: |
| 99 | case 4: |
| 100 | case 6: { |
| 101 | return base::StringPrintf("/dev/mtd%d", partition_num); |
| 102 | } |
| 103 | default: { |
| 104 | return base::StringPrintf("/dev/ubi%d_0", partition_num); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Return the device name for the corresponding partition on a NAND device that |
| 110 | // may be mountable (but may not be writable). |
| 111 | string MakeNandPartitionNameForMount(int partition_num) { |
| 112 | switch (partition_num) { |
| 113 | case 2: |
| 114 | case 4: |
| 115 | case 6: { |
| 116 | return base::StringPrintf("/dev/mtd%d", partition_num); |
| 117 | } |
| 118 | case 3: |
| 119 | case 5: |
| 120 | case 7: { |
| 121 | return base::StringPrintf("/dev/ubiblock%d_0", partition_num); |
| 122 | } |
| 123 | default: { |
| 124 | return base::StringPrintf("/dev/ubi%d_0", partition_num); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 129 | } // namespace |
| 130 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 131 | namespace utils { |
| 132 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 133 | // Cgroup container is created in update-engine's upstart script located at |
| 134 | // /etc/init/update-engine.conf. |
| 135 | static const char kCGroupDir[] = "/sys/fs/cgroup/cpu/update-engine"; |
| 136 | |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 137 | string ParseECVersion(string input_line) { |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 138 | base::TrimWhitespaceASCII(input_line, base::TRIM_ALL, &input_line); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 139 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 140 | // At this point we want to convert the format key=value pair from mosys to |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 141 | // a vector of key value pairs. |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 142 | vector<pair<string, string>> kv_pairs; |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 143 | if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) { |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 144 | for (const pair<string, string>& kv_pair : kv_pairs) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 145 | // Finally match against the fw_verion which may have quotes. |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 146 | if (kv_pair.first == "fw_version") { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 147 | string output; |
| 148 | // Trim any quotes. |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 149 | base::TrimString(kv_pair.second, "\"", &output); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 150 | return output; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | LOG(ERROR) << "Unable to parse fwid from ec info."; |
| 155 | return ""; |
| 156 | } |
| 157 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 158 | bool WriteFile(const char* path, const void* data, int data_len) { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 159 | DirectFileWriter writer; |
| 160 | TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, |
| 161 | O_WRONLY | O_CREAT | O_TRUNC, |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 162 | 0600)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 163 | ScopedFileWriterCloser closer(&writer); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 164 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(data, data_len)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 168 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 169 | const char* c_buf = static_cast<const char*>(buf); |
| 170 | ssize_t bytes_written = 0; |
| 171 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 172 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 173 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 174 | bytes_written += rc; |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 179 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 180 | const char* c_buf = static_cast<const char*>(buf); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 181 | size_t bytes_written = 0; |
| 182 | int num_attempts = 0; |
| 183 | while (bytes_written < count) { |
| 184 | num_attempts++; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 185 | ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, |
| 186 | offset + bytes_written); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 187 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. |
| 188 | if (rc < 0) { |
| 189 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts |
| 190 | << " bytes_written=" << bytes_written |
| 191 | << " count=" << count << " offset=" << offset; |
| 192 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 193 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 194 | bytes_written += rc; |
| 195 | } |
| 196 | return true; |
| 197 | } |
| 198 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 199 | bool WriteAll(FileDescriptorPtr fd, const void* buf, size_t count) { |
| 200 | const char* c_buf = static_cast<const char*>(buf); |
| 201 | ssize_t bytes_written = 0; |
| 202 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 203 | ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written); |
| 204 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 205 | bytes_written += rc; |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | bool PWriteAll(FileDescriptorPtr fd, |
| 211 | const void* buf, |
| 212 | size_t count, |
| 213 | off_t offset) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 214 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 215 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 216 | return WriteAll(fd, buf, count); |
| 217 | } |
| 218 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 219 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 220 | ssize_t* out_bytes_read) { |
| 221 | char* c_buf = static_cast<char*>(buf); |
| 222 | ssize_t bytes_read = 0; |
| 223 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 224 | ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 225 | offset + bytes_read); |
| 226 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 227 | if (rc == 0) { |
| 228 | break; |
| 229 | } |
| 230 | bytes_read += rc; |
| 231 | } |
| 232 | *out_bytes_read = bytes_read; |
| 233 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 236 | bool PReadAll(FileDescriptorPtr fd, void* buf, size_t count, off_t offset, |
| 237 | ssize_t* out_bytes_read) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 238 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 239 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 240 | char* c_buf = static_cast<char*>(buf); |
| 241 | ssize_t bytes_read = 0; |
| 242 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 243 | ssize_t rc = fd->Read(c_buf + bytes_read, count - bytes_read); |
| 244 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 245 | if (rc == 0) { |
| 246 | break; |
| 247 | } |
| 248 | bytes_read += rc; |
| 249 | } |
| 250 | *out_bytes_read = bytes_read; |
| 251 | return true; |
| 252 | } |
| 253 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 254 | // Append |nbytes| of content from |buf| to the vector pointed to by either |
| 255 | // |vec_p| or |str_p|. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 256 | static void AppendBytes(const uint8_t* buf, size_t nbytes, |
| 257 | chromeos::Blob* vec_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 258 | CHECK(buf); |
| 259 | CHECK(vec_p); |
| 260 | vec_p->insert(vec_p->end(), buf, buf + nbytes); |
| 261 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 262 | static void AppendBytes(const uint8_t* buf, size_t nbytes, |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 263 | string* str_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 264 | CHECK(buf); |
| 265 | CHECK(str_p); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 266 | str_p->append(buf, buf + nbytes); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Reads from an open file |fp|, appending the read content to the container |
| 270 | // pointer to by |out_p|. Returns true upon successful reading all of the |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 271 | // file's content, false otherwise. If |size| is not -1, reads up to |size| |
| 272 | // bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 273 | template <class T> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 274 | static bool Read(FILE* fp, off_t size, T* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 275 | CHECK(fp); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 276 | CHECK(size == -1 || size >= 0); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 277 | uint8_t buf[1024]; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 278 | while (size == -1 || size > 0) { |
| 279 | off_t bytes_to_read = sizeof(buf); |
| 280 | if (size > 0 && bytes_to_read > size) { |
| 281 | bytes_to_read = size; |
| 282 | } |
| 283 | size_t nbytes = fread(buf, 1, bytes_to_read, fp); |
| 284 | if (!nbytes) { |
| 285 | break; |
| 286 | } |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 287 | AppendBytes(buf, nbytes, out_p); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 288 | if (size != -1) { |
| 289 | CHECK(size >= static_cast<off_t>(nbytes)); |
| 290 | size -= nbytes; |
| 291 | } |
| 292 | } |
| 293 | if (ferror(fp)) { |
| 294 | return false; |
| 295 | } |
| 296 | return size == 0 || feof(fp); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 299 | // Opens a file |path| for reading and appends its the contents to a container |
| 300 | // |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end |
| 301 | // of the file, returns success. If |size| is not -1, reads up to |size| bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 302 | template <class T> |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 303 | static bool ReadFileChunkAndAppend(const string& path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 304 | off_t offset, |
| 305 | off_t size, |
| 306 | T* out_p) { |
| 307 | CHECK_GE(offset, 0); |
| 308 | CHECK(size == -1 || size >= 0); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 309 | base::ScopedFILE fp(fopen(path.c_str(), "r")); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 310 | if (!fp.get()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 311 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 312 | if (offset) { |
| 313 | // Return success without appending any data if a chunk beyond the end of |
| 314 | // the file is requested. |
| 315 | if (offset >= FileSize(path)) { |
| 316 | return true; |
| 317 | } |
| 318 | TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0); |
| 319 | } |
| 320 | return Read(fp.get(), size, out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 323 | // TODO(deymo): This is only used in unittest, but requires the private |
| 324 | // Read<string>() defined here. Expose Read<string>() or move to base/ version. |
| 325 | bool ReadPipe(const string& cmd, string* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 326 | FILE* fp = popen(cmd.c_str(), "r"); |
| 327 | if (!fp) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 328 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 329 | bool success = Read(fp, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 330 | return (success && pclose(fp) >= 0); |
| 331 | } |
| 332 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 333 | bool ReadFile(const string& path, chromeos::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 334 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 337 | bool ReadFile(const string& path, string* out_p) { |
| 338 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 341 | bool ReadFileChunk(const string& path, off_t offset, off_t size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 342 | chromeos::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 343 | return ReadFileChunkAndAppend(path, offset, size, out_p); |
| 344 | } |
| 345 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 346 | off_t BlockDevSize(int fd) { |
| 347 | uint64_t dev_size; |
| 348 | int rc = ioctl(fd, BLKGETSIZE64, &dev_size); |
| 349 | if (rc == -1) { |
| 350 | dev_size = -1; |
| 351 | PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd; |
| 352 | } |
| 353 | return dev_size; |
| 354 | } |
| 355 | |
| 356 | off_t BlockDevSize(const string& path) { |
| 357 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 358 | if (fd == -1) { |
| 359 | PLOG(ERROR) << "Error opening " << path; |
| 360 | return fd; |
| 361 | } |
| 362 | |
| 363 | off_t dev_size = BlockDevSize(fd); |
| 364 | if (dev_size == -1) |
| 365 | PLOG(ERROR) << "Error getting block device size on " << path; |
| 366 | |
| 367 | close(fd); |
| 368 | return dev_size; |
| 369 | } |
| 370 | |
| 371 | off_t FileSize(int fd) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 372 | struct stat stbuf; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 373 | int rc = fstat(fd, &stbuf); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 374 | CHECK_EQ(rc, 0); |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 375 | if (rc < 0) { |
| 376 | PLOG(ERROR) << "Error stat-ing " << fd; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 377 | return rc; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 378 | } |
| 379 | if (S_ISREG(stbuf.st_mode)) |
| 380 | return stbuf.st_size; |
| 381 | if (S_ISBLK(stbuf.st_mode)) |
| 382 | return BlockDevSize(fd); |
| 383 | LOG(ERROR) << "Couldn't determine the type of " << fd; |
| 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | off_t FileSize(const string& path) { |
| 388 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 389 | if (fd == -1) { |
| 390 | PLOG(ERROR) << "Error opening " << path; |
| 391 | return fd; |
| 392 | } |
| 393 | off_t size = FileSize(fd); |
| 394 | if (size == -1) |
| 395 | PLOG(ERROR) << "Error getting file size of " << path; |
| 396 | close(fd); |
| 397 | return size; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 400 | void HexDumpArray(const uint8_t* const arr, const size_t length) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 401 | LOG(INFO) << "Logging array of length: " << length; |
| 402 | const unsigned int bytes_per_line = 16; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 403 | for (uint32_t i = 0; i < length; i += bytes_per_line) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 404 | const unsigned int bytes_remaining = length - i; |
| 405 | const unsigned int bytes_per_this_line = min(bytes_per_line, |
| 406 | bytes_remaining); |
| 407 | char header[100]; |
| 408 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); |
| 409 | TEST_AND_RETURN(r == 13); |
| 410 | string line = header; |
| 411 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { |
| 412 | char buf[20]; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 413 | uint8_t c = arr[i + j]; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 414 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); |
| 415 | TEST_AND_RETURN(r == 3); |
| 416 | line += buf; |
| 417 | } |
| 418 | LOG(INFO) << line; |
| 419 | } |
| 420 | } |
| 421 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 422 | bool SplitPartitionName(const string& partition_name, |
| 423 | string* out_disk_name, |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 424 | int* out_partition_num) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 425 | if (!base::StartsWithASCII(partition_name, "/dev/", true)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 426 | LOG(ERROR) << "Invalid partition device name: " << partition_name; |
| 427 | return false; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 428 | } |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 429 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 430 | size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789"); |
| 431 | if (last_nondigit_pos == string::npos || |
| 432 | (last_nondigit_pos + 1) == partition_name.size()) { |
| 433 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 434 | return false; |
| 435 | } |
| 436 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 437 | size_t partition_name_len = string::npos; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 438 | if (partition_name[last_nondigit_pos] == '_') { |
| 439 | // NAND block devices have weird naming which could be something |
| 440 | // like "/dev/ubiblock2_0". We discard "_0" in such a case. |
| 441 | size_t prev_nondigit_pos = |
| 442 | partition_name.find_last_not_of("0123456789", last_nondigit_pos - 1); |
| 443 | if (prev_nondigit_pos == string::npos || |
| 444 | (prev_nondigit_pos + 1) == last_nondigit_pos) { |
| 445 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | partition_name_len = last_nondigit_pos - prev_nondigit_pos; |
| 450 | last_nondigit_pos = prev_nondigit_pos; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (out_disk_name) { |
| 454 | // Special case for MMC devices which have the following naming scheme: |
| 455 | // mmcblk0p2 |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 456 | size_t disk_name_len = last_nondigit_pos; |
| 457 | if (partition_name[last_nondigit_pos] != 'p' || |
| 458 | last_nondigit_pos == 0 || |
| 459 | !isdigit(partition_name[last_nondigit_pos - 1])) { |
| 460 | disk_name_len++; |
| 461 | } |
| 462 | *out_disk_name = partition_name.substr(0, disk_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if (out_partition_num) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 466 | string partition_str = partition_name.substr(last_nondigit_pos + 1, |
| 467 | partition_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 468 | *out_partition_num = atoi(partition_str.c_str()); |
| 469 | } |
| 470 | return true; |
| 471 | } |
| 472 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 473 | string MakePartitionName(const string& disk_name, int partition_num) { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 474 | if (partition_num < 1) { |
| 475 | LOG(ERROR) << "Invalid partition number: " << partition_num; |
| 476 | return string(); |
| 477 | } |
| 478 | |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 479 | if (!base::StartsWithASCII(disk_name, "/dev/", true)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 480 | LOG(ERROR) << "Invalid disk name: " << disk_name; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 481 | return string(); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 484 | if (IsMtdDeviceName(disk_name)) { |
| 485 | // Special case for UBI block devices. |
| 486 | // 1. ubiblock is not writable, we need to use plain "ubi". |
| 487 | // 2. There is a "_0" suffix. |
| 488 | return MakeNandPartitionName(partition_num); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 491 | string partition_name = disk_name; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 492 | if (isdigit(partition_name.back())) { |
| 493 | // Special case for devices with names ending with a digit. |
| 494 | // Add "p" to separate the disk name from partition number, |
| 495 | // e.g. "/dev/loop0p2" |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 496 | partition_name += 'p'; |
| 497 | } |
| 498 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 499 | partition_name += std::to_string(partition_num); |
| 500 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 501 | return partition_name; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 504 | string MakePartitionNameForMount(const string& part_name) { |
| 505 | if (IsMtdDeviceName(part_name)) { |
| 506 | int partition_num; |
| 507 | if (!SplitPartitionName(part_name, nullptr, &partition_num)) { |
| 508 | return ""; |
| 509 | } |
| 510 | return MakeNandPartitionNameForMount(partition_num); |
| 511 | } |
| 512 | return part_name; |
| 513 | } |
| 514 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 515 | string ErrnoNumberAsString(int err) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 516 | char buf[100]; |
| 517 | buf[0] = '\0'; |
| 518 | return strerror_r(err, buf, sizeof(buf)); |
| 519 | } |
| 520 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 521 | bool FileExists(const char* path) { |
| 522 | struct stat stbuf; |
| 523 | return 0 == lstat(path, &stbuf); |
| 524 | } |
| 525 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 526 | bool IsSymlink(const char* path) { |
| 527 | struct stat stbuf; |
| 528 | return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0; |
| 529 | } |
| 530 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 531 | bool TryAttachingUbiVolume(int volume_num, int timeout) { |
| 532 | const string volume_path = base::StringPrintf("/dev/ubi%d_0", volume_num); |
| 533 | if (FileExists(volume_path.c_str())) { |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | int exit_code; |
| 538 | vector<string> cmd = { |
| 539 | "ubiattach", |
| 540 | "-m", |
| 541 | base::StringPrintf("%d", volume_num), |
| 542 | "-d", |
| 543 | base::StringPrintf("%d", volume_num) |
| 544 | }; |
| 545 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); |
| 546 | TEST_AND_RETURN_FALSE(exit_code == 0); |
| 547 | |
| 548 | cmd = { |
| 549 | "ubiblock", |
| 550 | "--create", |
| 551 | volume_path |
| 552 | }; |
| 553 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); |
| 554 | TEST_AND_RETURN_FALSE(exit_code == 0); |
| 555 | |
| 556 | while (timeout > 0 && !FileExists(volume_path.c_str())) { |
| 557 | sleep(1); |
| 558 | timeout--; |
| 559 | } |
| 560 | |
| 561 | return FileExists(volume_path.c_str()); |
| 562 | } |
| 563 | |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 564 | // If |path| is absolute, or explicit relative to the current working directory, |
| 565 | // leaves it as is. Otherwise, if TMPDIR is defined in the environment and is |
| 566 | // non-empty, prepends it to |path|. Otherwise, prepends /tmp. Returns the |
| 567 | // resulting path. |
| 568 | static const string PrependTmpdir(const string& path) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 569 | if (path[0] == '/' || base::StartsWithASCII(path, "./", true) || |
| 570 | base::StartsWithASCII(path, "../", true)) |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 571 | return path; |
| 572 | |
| 573 | const char *tmpdir = getenv("TMPDIR"); |
| 574 | const string prefix = (tmpdir && *tmpdir ? tmpdir : "/tmp"); |
| 575 | return prefix + "/" + path; |
| 576 | } |
| 577 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 578 | bool MakeTempFile(const string& base_filename_template, |
| 579 | string* filename, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 580 | int* fd) { |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 581 | const string filename_template = PrependTmpdir(base_filename_template); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 582 | DCHECK(filename || fd); |
| 583 | vector<char> buf(filename_template.size() + 1); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 584 | memcpy(buf.data(), filename_template.data(), filename_template.size()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 585 | buf[filename_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 586 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 587 | int mkstemp_fd = mkstemp(buf.data()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 588 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 589 | if (filename) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 590 | *filename = buf.data(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 591 | } |
| 592 | if (fd) { |
| 593 | *fd = mkstemp_fd; |
| 594 | } else { |
| 595 | close(mkstemp_fd); |
| 596 | } |
| 597 | return true; |
| 598 | } |
| 599 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 600 | bool MakeTempDirectory(const string& base_dirname_template, |
| 601 | string* dirname) { |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 602 | const string dirname_template = PrependTmpdir(base_dirname_template); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 603 | DCHECK(dirname); |
| 604 | vector<char> buf(dirname_template.size() + 1); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 605 | memcpy(buf.data(), dirname_template.data(), dirname_template.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 606 | buf[dirname_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 607 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 608 | char* return_code = mkdtemp(buf.data()); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 609 | TEST_AND_RETURN_FALSE_ERRNO(return_code != nullptr); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 610 | *dirname = buf.data(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 611 | return true; |
| 612 | } |
| 613 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 614 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 615 | const string& mountpoint, |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 616 | unsigned long mountflags) { // NOLINT(runtime/int) |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 617 | // TODO(sosa): Remove "ext3" once crbug.com/208022 is resolved. |
| 618 | const vector<const char*> fstypes{"ext2", "ext3", "squashfs"}; |
| 619 | for (const char* fstype : fstypes) { |
| 620 | int rc = mount(device.c_str(), mountpoint.c_str(), fstype, mountflags, |
| 621 | nullptr); |
| 622 | if (rc == 0) |
| 623 | return true; |
| 624 | |
| 625 | PLOG(WARNING) << "Unable to mount destination device " << device |
| 626 | << " on " << mountpoint << " as " << fstype; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 627 | } |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 628 | LOG(ERROR) << "Unable to mount " << device << " with any supported type"; |
| 629 | return false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | bool UnmountFilesystem(const string& mountpoint) { |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 633 | for (int num_retries = 0; ; ++num_retries) { |
| 634 | if (umount(mountpoint.c_str()) == 0) |
| 635 | break; |
| 636 | |
| 637 | TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY && |
| 638 | num_retries < kUnmountMaxNumOfRetries); |
Alex Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 639 | usleep(kUnmountRetryIntervalInMicroseconds); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 640 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 644 | bool GetFilesystemSize(const string& device, |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 645 | int* out_block_count, |
| 646 | int* out_block_size) { |
| 647 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY)); |
Alex Deymo | e7141c6 | 2014-10-17 14:03:01 -0700 | [diff] [blame] | 648 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 649 | ScopedFdCloser fd_closer(&fd); |
| 650 | return GetFilesystemSizeFromFD(fd, out_block_count, out_block_size); |
| 651 | } |
| 652 | |
| 653 | bool GetFilesystemSizeFromFD(int fd, |
| 654 | int* out_block_count, |
| 655 | int* out_block_size) { |
| 656 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 657 | |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 658 | // Determine the filesystem size by directly reading the block count and |
| 659 | // block size information from the superblock. Supported FS are ext3 and |
| 660 | // squashfs. |
| 661 | |
| 662 | // Read from the fd only once and detect in memory. The first 2 KiB is enough |
| 663 | // to read the ext2 superblock (located at offset 1024) and the squashfs |
| 664 | // superblock (located at offset 0). |
| 665 | const ssize_t kBufferSize = 2048; |
| 666 | |
| 667 | uint8_t buffer[kBufferSize]; |
| 668 | if (HANDLE_EINTR(pread(fd, buffer, kBufferSize, 0)) != kBufferSize) { |
| 669 | PLOG(ERROR) << "Unable to read the file system header:"; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 670 | return false; |
| 671 | } |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 672 | |
| 673 | if (GetSquashfs4Size(buffer, kBufferSize, out_block_count, out_block_size)) |
| 674 | return true; |
| 675 | if (GetExt3Size(buffer, kBufferSize, out_block_count, out_block_size)) |
| 676 | return true; |
| 677 | |
| 678 | LOG(ERROR) << "Unable to determine file system type."; |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | bool GetExt3Size(const uint8_t* buffer, size_t buffer_size, |
| 683 | int* out_block_count, |
| 684 | int* out_block_size) { |
| 685 | // See include/linux/ext2_fs.h for more details on the structure. We obtain |
| 686 | // ext2 constants from ext2fs/ext2fs.h header but we don't link with the |
| 687 | // library. |
| 688 | if (buffer_size < SUPERBLOCK_OFFSET + SUPERBLOCK_SIZE) |
| 689 | return false; |
| 690 | |
| 691 | const uint8_t* superblock = buffer + SUPERBLOCK_OFFSET; |
| 692 | |
| 693 | // ext3_fs.h: ext3_super_block.s_blocks_count |
| 694 | uint32_t block_count = |
| 695 | *reinterpret_cast<const uint32_t*>(superblock + 1 * sizeof(int32_t)); |
| 696 | |
| 697 | // ext3_fs.h: ext3_super_block.s_log_block_size |
| 698 | uint32_t log_block_size = |
| 699 | *reinterpret_cast<const uint32_t*>(superblock + 6 * sizeof(int32_t)); |
| 700 | |
| 701 | // ext3_fs.h: ext3_super_block.s_magic |
| 702 | uint16_t magic = |
| 703 | *reinterpret_cast<const uint16_t*>(superblock + 14 * sizeof(int32_t)); |
| 704 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 705 | block_count = le32toh(block_count); |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 706 | log_block_size = le32toh(log_block_size) + EXT2_MIN_BLOCK_LOG_SIZE; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 707 | magic = le16toh(magic); |
| 708 | |
| 709 | // Sanity check the parameters. |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 710 | TEST_AND_RETURN_FALSE(magic == EXT2_SUPER_MAGIC); |
| 711 | TEST_AND_RETURN_FALSE(log_block_size >= EXT2_MIN_BLOCK_LOG_SIZE && |
| 712 | log_block_size <= EXT2_MAX_BLOCK_LOG_SIZE); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 713 | TEST_AND_RETURN_FALSE(block_count > 0); |
| 714 | |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 715 | if (out_block_count) |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 716 | *out_block_count = block_count; |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 717 | if (out_block_size) |
| 718 | *out_block_size = 1 << log_block_size; |
| 719 | return true; |
| 720 | } |
| 721 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 722 | bool GetSquashfs4Size(const uint8_t* buffer, size_t buffer_size, |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 723 | int* out_block_count, |
| 724 | int* out_block_size) { |
| 725 | // See fs/squashfs/squashfs_fs.h for format details. We only support |
| 726 | // Squashfs 4.x little endian. |
| 727 | |
| 728 | // sizeof(struct squashfs_super_block) |
| 729 | const size_t kSquashfsSuperBlockSize = 96; |
| 730 | if (buffer_size < kSquashfsSuperBlockSize) |
| 731 | return false; |
| 732 | |
| 733 | // Check magic, squashfs_fs.h: SQUASHFS_MAGIC |
| 734 | if (memcmp(buffer, "hsqs", 4) != 0) |
| 735 | return false; // Only little endian is supported. |
| 736 | |
| 737 | // squashfs_fs.h: struct squashfs_super_block.s_major |
| 738 | uint16_t s_major = *reinterpret_cast<const uint16_t*>( |
| 739 | buffer + 5 * sizeof(uint32_t) + 4 * sizeof(uint16_t)); |
| 740 | |
| 741 | if (s_major != 4) { |
| 742 | LOG(ERROR) << "Found unsupported squashfs major version " << s_major; |
| 743 | return false; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 744 | } |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 745 | |
| 746 | // squashfs_fs.h: struct squashfs_super_block.bytes_used |
| 747 | uint64_t bytes_used = *reinterpret_cast<const int64_t*>( |
| 748 | buffer + 5 * sizeof(uint32_t) + 6 * sizeof(uint16_t) + sizeof(uint64_t)); |
| 749 | |
| 750 | const int block_size = 4096; |
| 751 | |
| 752 | // The squashfs' bytes_used doesn't need to be aligned with the block boundary |
| 753 | // so we round up to the nearest blocksize. |
| 754 | if (out_block_count) |
| 755 | *out_block_count = (bytes_used + block_size - 1) / block_size; |
| 756 | if (out_block_size) |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 757 | *out_block_size = block_size; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 758 | return true; |
| 759 | } |
| 760 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 761 | bool IsExtFilesystem(const string& device) { |
Alex Deymo | de942f3 | 2015-03-13 11:57:15 -0700 | [diff] [blame] | 762 | chromeos::Blob header; |
| 763 | // The first 2 KiB is enough to read the ext2 superblock (located at offset |
| 764 | // 1024). |
| 765 | if (!ReadFileChunk(device, 0, 2048, &header)) |
| 766 | return false; |
| 767 | return GetExt3Size(header.data(), header.size(), nullptr, nullptr); |
| 768 | } |
| 769 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 770 | bool IsSquashfsFilesystem(const string& device) { |
Alex Deymo | de942f3 | 2015-03-13 11:57:15 -0700 | [diff] [blame] | 771 | chromeos::Blob header; |
| 772 | // The first 96 is enough to read the squashfs superblock. |
| 773 | const ssize_t kSquashfsSuperBlockSize = 96; |
| 774 | if (!ReadFileChunk(device, 0, kSquashfsSuperBlockSize, &header)) |
| 775 | return false; |
| 776 | return GetSquashfs4Size(header.data(), header.size(), nullptr, nullptr); |
| 777 | } |
| 778 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 779 | // Tries to parse the header of an ELF file to obtain a human-readable |
| 780 | // description of it on the |output| string. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 781 | static bool GetFileFormatELF(const uint8_t* buffer, size_t size, |
| 782 | string* output) { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 783 | // 0x00: EI_MAG - ELF magic header, 4 bytes. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 784 | if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 785 | return false; |
| 786 | *output = "ELF"; |
| 787 | |
| 788 | // 0x04: EI_CLASS, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 789 | if (size < EI_CLASS + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 790 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 791 | switch (buffer[EI_CLASS]) { |
| 792 | case ELFCLASS32: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 793 | *output += " 32-bit"; |
| 794 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 795 | case ELFCLASS64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 796 | *output += " 64-bit"; |
| 797 | break; |
| 798 | default: |
| 799 | *output += " ?-bit"; |
| 800 | } |
| 801 | |
| 802 | // 0x05: EI_DATA, endianness, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 803 | if (size < EI_DATA + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 804 | return true; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 805 | uint8_t ei_data = buffer[EI_DATA]; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 806 | switch (ei_data) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 807 | case ELFDATA2LSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 808 | *output += " little-endian"; |
| 809 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 810 | case ELFDATA2MSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 811 | *output += " big-endian"; |
| 812 | break; |
| 813 | default: |
| 814 | *output += " ?-endian"; |
| 815 | // Don't parse anything after the 0x10 offset if endianness is unknown. |
| 816 | return true; |
| 817 | } |
| 818 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 819 | const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer); |
| 820 | // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12) |
| 821 | // and size is the same for both 32 and 64 bits. |
| 822 | if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine)) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 823 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 824 | uint16_t e_machine; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 825 | // Fix endianess regardless of the host endianess. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 826 | if (ei_data == ELFDATA2LSB) |
| 827 | e_machine = le16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 828 | else |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 829 | e_machine = be16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 830 | |
| 831 | switch (e_machine) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 832 | case EM_386: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 833 | *output += " x86"; |
| 834 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 835 | case EM_MIPS: |
| 836 | *output += " mips"; |
| 837 | break; |
| 838 | case EM_ARM: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 839 | *output += " arm"; |
| 840 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 841 | case EM_X86_64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 842 | *output += " x86-64"; |
| 843 | break; |
| 844 | default: |
| 845 | *output += " unknown-arch"; |
| 846 | } |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | string GetFileFormat(const string& path) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 851 | chromeos::Blob buffer; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 852 | if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer)) |
| 853 | return "File not found."; |
| 854 | |
| 855 | string result; |
| 856 | if (GetFileFormatELF(buffer.data(), buffer.size(), &result)) |
| 857 | return result; |
| 858 | |
| 859 | return "data"; |
| 860 | } |
| 861 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 862 | namespace { |
| 863 | // Do the actual trigger. We do it as a main-loop callback to (try to) get a |
| 864 | // consistent stack trace. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 865 | void TriggerCrashReporterUpload() { |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 866 | pid_t pid = fork(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 867 | CHECK_GE(pid, 0) << "fork failed"; // fork() failed. Something is very wrong. |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 868 | if (pid == 0) { |
| 869 | // We are the child. Crash. |
| 870 | abort(); // never returns |
| 871 | } |
| 872 | // We are the parent. Wait for child to terminate. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 873 | pid_t result = waitpid(pid, nullptr, 0); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 874 | LOG_IF(ERROR, result < 0) << "waitpid() failed"; |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 875 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 876 | } // namespace |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 877 | |
| 878 | void ScheduleCrashReporterUpload() { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 879 | chromeos::MessageLoop::current()->PostTask( |
| 880 | FROM_HERE, |
| 881 | base::Bind(&TriggerCrashReporterUpload)); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 884 | bool SetCpuShares(CpuShares shares) { |
| 885 | string string_shares = base::IntToString(static_cast<int>(shares)); |
| 886 | string cpu_shares_file = string(utils::kCGroupDir) + "/cpu.shares"; |
| 887 | LOG(INFO) << "Setting cgroup cpu shares to " << string_shares; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 888 | if (utils::WriteFile(cpu_shares_file.c_str(), string_shares.c_str(), |
| 889 | string_shares.size())) { |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 890 | return true; |
| 891 | } else { |
| 892 | LOG(ERROR) << "Failed to change cgroup cpu shares to "<< string_shares |
| 893 | << " using " << cpu_shares_file; |
| 894 | return false; |
| 895 | } |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 898 | int FuzzInt(int value, unsigned int range) { |
| 899 | int min = value - range / 2; |
| 900 | int max = value + range - range / 2; |
| 901 | return base::RandInt(min, max); |
| 902 | } |
| 903 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 904 | string FormatSecs(unsigned secs) { |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 905 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 908 | string FormatTimeDelta(TimeDelta delta) { |
David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 909 | string str; |
| 910 | |
| 911 | // Handle negative durations by prefixing with a minus. |
| 912 | if (delta.ToInternalValue() < 0) { |
| 913 | delta *= -1; |
| 914 | str = "-"; |
| 915 | } |
| 916 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 917 | // Canonicalize into days, hours, minutes, seconds and microseconds. |
| 918 | unsigned days = delta.InDays(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 919 | delta -= TimeDelta::FromDays(days); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 920 | unsigned hours = delta.InHours(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 921 | delta -= TimeDelta::FromHours(hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 922 | unsigned mins = delta.InMinutes(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 923 | delta -= TimeDelta::FromMinutes(mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 924 | unsigned secs = delta.InSeconds(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 925 | delta -= TimeDelta::FromSeconds(secs); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 926 | unsigned usecs = delta.InMicroseconds(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 927 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 928 | if (days) |
| 929 | base::StringAppendF(&str, "%ud", days); |
| 930 | if (days || hours) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 931 | base::StringAppendF(&str, "%uh", hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 932 | if (days || hours || mins) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 933 | base::StringAppendF(&str, "%um", mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 934 | base::StringAppendF(&str, "%u", secs); |
| 935 | if (usecs) { |
| 936 | int width = 6; |
| 937 | while ((usecs / 10) * 10 == usecs) { |
| 938 | usecs /= 10; |
| 939 | width--; |
| 940 | } |
| 941 | base::StringAppendF(&str, ".%0*u", width, usecs); |
| 942 | } |
| 943 | base::StringAppendF(&str, "s"); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 944 | return str; |
| 945 | } |
| 946 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 947 | string ToString(const Time utc_time) { |
| 948 | Time::Exploded exp_time; |
| 949 | utc_time.UTCExplode(&exp_time); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 950 | return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT", |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 951 | exp_time.month, |
| 952 | exp_time.day_of_month, |
| 953 | exp_time.year, |
| 954 | exp_time.hour, |
| 955 | exp_time.minute, |
| 956 | exp_time.second); |
| 957 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 958 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 959 | string ToString(bool b) { |
| 960 | return (b ? "true" : "false"); |
| 961 | } |
| 962 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 963 | string ToString(DownloadSource source) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 964 | switch (source) { |
| 965 | case kDownloadSourceHttpsServer: return "HttpsServer"; |
| 966 | case kDownloadSourceHttpServer: return "HttpServer"; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 967 | case kDownloadSourceHttpPeer: return "HttpPeer"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 968 | case kNumDownloadSources: return "Unknown"; |
| 969 | // Don't add a default case to let the compiler warn about newly added |
| 970 | // download sources which should be added here. |
| 971 | } |
| 972 | |
| 973 | return "Unknown"; |
| 974 | } |
| 975 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 976 | string ToString(PayloadType payload_type) { |
| 977 | switch (payload_type) { |
| 978 | case kPayloadTypeDelta: return "Delta"; |
| 979 | case kPayloadTypeFull: return "Full"; |
| 980 | case kPayloadTypeForcedFull: return "ForcedFull"; |
| 981 | case kNumPayloadTypes: return "Unknown"; |
| 982 | // Don't add a default case to let the compiler warn about newly added |
| 983 | // payload types which should be added here. |
| 984 | } |
| 985 | |
| 986 | return "Unknown"; |
| 987 | } |
| 988 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 989 | ErrorCode GetBaseErrorCode(ErrorCode code) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 990 | // Ignore the higher order bits in the code by applying the mask as |
| 991 | // we want the enumerations to be in the small contiguous range |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 992 | // with values less than ErrorCode::kUmaReportedMax. |
| 993 | ErrorCode base_code = static_cast<ErrorCode>( |
| 994 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 995 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 996 | // Make additional adjustments required for UMA and error classification. |
| 997 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix |
| 998 | // chromium-os:34369. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 999 | if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1000 | // Since we want to keep the enums to a small value, aggregate all HTTP |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1001 | // errors into this one bucket for UMA and error classification purposes. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1002 | LOG(INFO) << "Converting error code " << base_code |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1003 | << " to ErrorCode::kOmahaErrorInHTTPResponse"; |
| 1004 | base_code = ErrorCode::kOmahaErrorInHTTPResponse; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1007 | return base_code; |
| 1008 | } |
| 1009 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1010 | metrics::AttemptResult GetAttemptResult(ErrorCode code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1011 | ErrorCode base_code = static_cast<ErrorCode>( |
| 1012 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1013 | |
| 1014 | switch (base_code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1015 | case ErrorCode::kSuccess: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1016 | return metrics::AttemptResult::kUpdateSucceeded; |
| 1017 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1018 | case ErrorCode::kDownloadTransferError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1019 | return metrics::AttemptResult::kPayloadDownloadError; |
| 1020 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1021 | case ErrorCode::kDownloadInvalidMetadataSize: |
| 1022 | case ErrorCode::kDownloadInvalidMetadataMagicString: |
| 1023 | case ErrorCode::kDownloadMetadataSignatureError: |
| 1024 | case ErrorCode::kDownloadMetadataSignatureVerificationError: |
| 1025 | case ErrorCode::kPayloadMismatchedType: |
| 1026 | case ErrorCode::kUnsupportedMajorPayloadVersion: |
| 1027 | case ErrorCode::kUnsupportedMinorPayloadVersion: |
| 1028 | case ErrorCode::kDownloadNewPartitionInfoError: |
| 1029 | case ErrorCode::kDownloadSignatureMissingInManifest: |
| 1030 | case ErrorCode::kDownloadManifestParseError: |
| 1031 | case ErrorCode::kDownloadOperationHashMissingError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1032 | return metrics::AttemptResult::kMetadataMalformed; |
| 1033 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1034 | case ErrorCode::kDownloadOperationHashMismatch: |
| 1035 | case ErrorCode::kDownloadOperationHashVerificationError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1036 | return metrics::AttemptResult::kOperationMalformed; |
| 1037 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1038 | case ErrorCode::kDownloadOperationExecutionError: |
| 1039 | case ErrorCode::kInstallDeviceOpenError: |
| 1040 | case ErrorCode::kKernelDeviceOpenError: |
| 1041 | case ErrorCode::kDownloadWriteError: |
| 1042 | case ErrorCode::kFilesystemCopierError: |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1043 | case ErrorCode::kFilesystemVerifierError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1044 | return metrics::AttemptResult::kOperationExecutionError; |
| 1045 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1046 | case ErrorCode::kDownloadMetadataSignatureMismatch: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1047 | return metrics::AttemptResult::kMetadataVerificationFailed; |
| 1048 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1049 | case ErrorCode::kPayloadSizeMismatchError: |
| 1050 | case ErrorCode::kPayloadHashMismatchError: |
| 1051 | case ErrorCode::kDownloadPayloadVerificationError: |
| 1052 | case ErrorCode::kSignedDeltaPayloadExpectedError: |
| 1053 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1054 | return metrics::AttemptResult::kPayloadVerificationFailed; |
| 1055 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1056 | case ErrorCode::kNewRootfsVerificationError: |
| 1057 | case ErrorCode::kNewKernelVerificationError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1058 | return metrics::AttemptResult::kVerificationFailed; |
| 1059 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1060 | case ErrorCode::kPostinstallRunnerError: |
| 1061 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 1062 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1063 | return metrics::AttemptResult::kPostInstallFailed; |
| 1064 | |
| 1065 | // We should never get these errors in the update-attempt stage so |
| 1066 | // return internal error if this happens. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1067 | case ErrorCode::kError: |
| 1068 | case ErrorCode::kOmahaRequestXMLParseError: |
| 1069 | case ErrorCode::kOmahaRequestError: |
| 1070 | case ErrorCode::kOmahaResponseHandlerError: |
| 1071 | case ErrorCode::kDownloadStateInitializationError: |
| 1072 | case ErrorCode::kOmahaRequestEmptyResponseError: |
| 1073 | case ErrorCode::kDownloadInvalidMetadataSignature: |
| 1074 | case ErrorCode::kOmahaResponseInvalid: |
| 1075 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 1076 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 1077 | case ErrorCode::kOmahaErrorInHTTPResponse: |
| 1078 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 1079 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 1080 | case ErrorCode::kPostinstallPowerwashError: |
| 1081 | case ErrorCode::kUpdateCanceledByChannelChange: |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1082 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1083 | return metrics::AttemptResult::kInternalError; |
| 1084 | |
| 1085 | // Special flags. These can't happen (we mask them out above) but |
| 1086 | // the compiler doesn't know that. Just break out so we can warn and |
| 1087 | // return |kInternalError|. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1088 | case ErrorCode::kUmaReportedMax: |
| 1089 | case ErrorCode::kOmahaRequestHTTPResponseBase: |
| 1090 | case ErrorCode::kDevModeFlag: |
| 1091 | case ErrorCode::kResumedFlag: |
| 1092 | case ErrorCode::kTestImageFlag: |
| 1093 | case ErrorCode::kTestOmahaUrlFlag: |
| 1094 | case ErrorCode::kSpecialFlags: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | LOG(ERROR) << "Unexpected error code " << base_code; |
| 1099 | return metrics::AttemptResult::kInternalError; |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1104 | ErrorCode base_code = static_cast<ErrorCode>( |
| 1105 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1106 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1107 | if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) { |
| 1108 | int http_status = |
| 1109 | static_cast<int>(base_code) - |
| 1110 | static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1111 | if (http_status >= 200 && http_status <= 599) { |
| 1112 | return static_cast<metrics::DownloadErrorCode>( |
| 1113 | static_cast<int>(metrics::DownloadErrorCode::kHttpStatus200) + |
| 1114 | http_status - 200); |
| 1115 | } else if (http_status == 0) { |
| 1116 | // The code is using HTTP Status 0 for "Unable to get http |
| 1117 | // response code." |
| 1118 | return metrics::DownloadErrorCode::kDownloadError; |
| 1119 | } |
| 1120 | LOG(WARNING) << "Unexpected HTTP status code " << http_status; |
| 1121 | return metrics::DownloadErrorCode::kHttpStatusOther; |
| 1122 | } |
| 1123 | |
| 1124 | switch (base_code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1125 | // Unfortunately, ErrorCode::kDownloadTransferError is returned for a wide |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1126 | // variety of errors (proxy errors, host not reachable, timeouts etc.). |
| 1127 | // |
| 1128 | // For now just map that to kDownloading. See http://crbug.com/355745 |
| 1129 | // for how we plan to add more detail in the future. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1130 | case ErrorCode::kDownloadTransferError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1131 | return metrics::DownloadErrorCode::kDownloadError; |
| 1132 | |
| 1133 | // All of these error codes are not related to downloading so break |
| 1134 | // out so we can warn and return InputMalformed. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1135 | case ErrorCode::kSuccess: |
| 1136 | case ErrorCode::kError: |
| 1137 | case ErrorCode::kOmahaRequestError: |
| 1138 | case ErrorCode::kOmahaResponseHandlerError: |
| 1139 | case ErrorCode::kFilesystemCopierError: |
| 1140 | case ErrorCode::kPostinstallRunnerError: |
| 1141 | case ErrorCode::kPayloadMismatchedType: |
| 1142 | case ErrorCode::kInstallDeviceOpenError: |
| 1143 | case ErrorCode::kKernelDeviceOpenError: |
| 1144 | case ErrorCode::kPayloadHashMismatchError: |
| 1145 | case ErrorCode::kPayloadSizeMismatchError: |
| 1146 | case ErrorCode::kDownloadPayloadVerificationError: |
| 1147 | case ErrorCode::kDownloadNewPartitionInfoError: |
| 1148 | case ErrorCode::kDownloadWriteError: |
| 1149 | case ErrorCode::kNewRootfsVerificationError: |
| 1150 | case ErrorCode::kNewKernelVerificationError: |
| 1151 | case ErrorCode::kSignedDeltaPayloadExpectedError: |
| 1152 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: |
| 1153 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 1154 | case ErrorCode::kDownloadStateInitializationError: |
| 1155 | case ErrorCode::kDownloadInvalidMetadataMagicString: |
| 1156 | case ErrorCode::kDownloadSignatureMissingInManifest: |
| 1157 | case ErrorCode::kDownloadManifestParseError: |
| 1158 | case ErrorCode::kDownloadMetadataSignatureError: |
| 1159 | case ErrorCode::kDownloadMetadataSignatureVerificationError: |
| 1160 | case ErrorCode::kDownloadMetadataSignatureMismatch: |
| 1161 | case ErrorCode::kDownloadOperationHashVerificationError: |
| 1162 | case ErrorCode::kDownloadOperationExecutionError: |
| 1163 | case ErrorCode::kDownloadOperationHashMismatch: |
| 1164 | case ErrorCode::kOmahaRequestEmptyResponseError: |
| 1165 | case ErrorCode::kOmahaRequestXMLParseError: |
| 1166 | case ErrorCode::kDownloadInvalidMetadataSize: |
| 1167 | case ErrorCode::kDownloadInvalidMetadataSignature: |
| 1168 | case ErrorCode::kOmahaResponseInvalid: |
| 1169 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 1170 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 1171 | case ErrorCode::kOmahaErrorInHTTPResponse: |
| 1172 | case ErrorCode::kDownloadOperationHashMissingError: |
| 1173 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 1174 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 1175 | case ErrorCode::kPostinstallPowerwashError: |
| 1176 | case ErrorCode::kUpdateCanceledByChannelChange: |
| 1177 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
| 1178 | case ErrorCode::kUnsupportedMajorPayloadVersion: |
| 1179 | case ErrorCode::kUnsupportedMinorPayloadVersion: |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1180 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1181 | case ErrorCode::kFilesystemVerifierError: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1182 | break; |
| 1183 | |
| 1184 | // Special flags. These can't happen (we mask them out above) but |
| 1185 | // the compiler doesn't know that. Just break out so we can warn and |
| 1186 | // return |kInputMalformed|. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1187 | case ErrorCode::kUmaReportedMax: |
| 1188 | case ErrorCode::kOmahaRequestHTTPResponseBase: |
| 1189 | case ErrorCode::kDevModeFlag: |
| 1190 | case ErrorCode::kResumedFlag: |
| 1191 | case ErrorCode::kTestImageFlag: |
| 1192 | case ErrorCode::kTestOmahaUrlFlag: |
| 1193 | case ErrorCode::kSpecialFlags: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1194 | LOG(ERROR) << "Unexpected error code " << base_code; |
| 1195 | break; |
| 1196 | } |
| 1197 | |
| 1198 | return metrics::DownloadErrorCode::kInputMalformed; |
| 1199 | } |
| 1200 | |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1201 | metrics::ConnectionType GetConnectionType( |
| 1202 | NetworkConnectionType type, |
| 1203 | NetworkTethering tethering) { |
| 1204 | switch (type) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 1205 | case NetworkConnectionType::kUnknown: |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1206 | return metrics::ConnectionType::kUnknown; |
| 1207 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 1208 | case NetworkConnectionType::kEthernet: |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1209 | if (tethering == NetworkTethering::kConfirmed) |
| 1210 | return metrics::ConnectionType::kTetheredEthernet; |
| 1211 | else |
| 1212 | return metrics::ConnectionType::kEthernet; |
| 1213 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 1214 | case NetworkConnectionType::kWifi: |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1215 | if (tethering == NetworkTethering::kConfirmed) |
| 1216 | return metrics::ConnectionType::kTetheredWifi; |
| 1217 | else |
| 1218 | return metrics::ConnectionType::kWifi; |
| 1219 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 1220 | case NetworkConnectionType::kWimax: |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1221 | return metrics::ConnectionType::kWimax; |
| 1222 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 1223 | case NetworkConnectionType::kBluetooth: |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1224 | return metrics::ConnectionType::kBluetooth; |
| 1225 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 1226 | case NetworkConnectionType::kCellular: |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1227 | return metrics::ConnectionType::kCellular; |
| 1228 | } |
| 1229 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 1230 | LOG(ERROR) << "Unexpected network connection type: type=" |
| 1231 | << static_cast<int>(type) |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1232 | << ", tethering=" << static_cast<int>(tethering); |
| 1233 | |
| 1234 | return metrics::ConnectionType::kUnknown; |
| 1235 | } |
| 1236 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1237 | // Returns a printable version of the various flags denoted in the higher order |
| 1238 | // bits of the given code. Returns an empty string if none of those bits are |
| 1239 | // set. |
| 1240 | string GetFlagNames(uint32_t code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1241 | uint32_t flags = (static_cast<uint32_t>(code) & |
| 1242 | static_cast<uint32_t>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1243 | string flag_names; |
| 1244 | string separator = ""; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1245 | for (size_t i = 0; i < sizeof(flags) * 8; i++) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1246 | uint32_t flag = flags & (1 << i); |
| 1247 | if (flag) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1248 | flag_names += separator + CodeToString(static_cast<ErrorCode>(flag)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1249 | separator = ", "; |
| 1250 | } |
| 1251 | } |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1252 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1253 | return flag_names; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1256 | void SendErrorCodeToUma(SystemState* system_state, ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1257 | if (!system_state) |
| 1258 | return; |
| 1259 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1260 | ErrorCode uma_error_code = GetBaseErrorCode(code); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1261 | |
| 1262 | // If the code doesn't have flags computed already, compute them now based on |
| 1263 | // the state of the current update attempt. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1264 | uint32_t flags = |
| 1265 | static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1266 | if (!flags) |
| 1267 | flags = system_state->update_attempter()->GetErrorCodeFlags(); |
| 1268 | |
| 1269 | // Determine the UMA bucket depending on the flags. But, ignore the resumed |
| 1270 | // flag, as it's perfectly normal for production devices to resume their |
| 1271 | // downloads and so we want to record those cases also in NormalErrorCodes |
| 1272 | // bucket. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1273 | string metric = |
| 1274 | flags & ~static_cast<uint32_t>(ErrorCode::kResumedFlag) ? |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1275 | "Installer.DevModeErrorCodes" : "Installer.NormalErrorCodes"; |
| 1276 | |
| 1277 | LOG(INFO) << "Sending error code " << uma_error_code |
| 1278 | << " (" << CodeToString(uma_error_code) << ")" |
| 1279 | << " to UMA metric: " << metric |
| 1280 | << ". Flags = " << (flags ? GetFlagNames(flags) : "None"); |
| 1281 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1282 | system_state->metrics_lib()->SendEnumToUMA( |
| 1283 | metric, static_cast<int>(uma_error_code), |
| 1284 | static_cast<int>(ErrorCode::kUmaReportedMax)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1287 | string CodeToString(ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1288 | // If the given code has both parts (i.e. the error code part and the flags |
| 1289 | // part) then strip off the flags part since the switch statement below |
| 1290 | // has case statements only for the base error code or a single flag but |
| 1291 | // doesn't support any combinations of those. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1292 | if ((static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags)) && |
| 1293 | (static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags))) |
| 1294 | code = static_cast<ErrorCode>( |
| 1295 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1296 | switch (code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1297 | case ErrorCode::kSuccess: return "ErrorCode::kSuccess"; |
| 1298 | case ErrorCode::kError: return "ErrorCode::kError"; |
| 1299 | case ErrorCode::kOmahaRequestError: return "ErrorCode::kOmahaRequestError"; |
| 1300 | case ErrorCode::kOmahaResponseHandlerError: |
| 1301 | return "ErrorCode::kOmahaResponseHandlerError"; |
| 1302 | case ErrorCode::kFilesystemCopierError: |
| 1303 | return "ErrorCode::kFilesystemCopierError"; |
| 1304 | case ErrorCode::kPostinstallRunnerError: |
| 1305 | return "ErrorCode::kPostinstallRunnerError"; |
| 1306 | case ErrorCode::kPayloadMismatchedType: |
| 1307 | return "ErrorCode::kPayloadMismatchedType"; |
| 1308 | case ErrorCode::kInstallDeviceOpenError: |
| 1309 | return "ErrorCode::kInstallDeviceOpenError"; |
| 1310 | case ErrorCode::kKernelDeviceOpenError: |
| 1311 | return "ErrorCode::kKernelDeviceOpenError"; |
| 1312 | case ErrorCode::kDownloadTransferError: |
| 1313 | return "ErrorCode::kDownloadTransferError"; |
| 1314 | case ErrorCode::kPayloadHashMismatchError: |
| 1315 | return "ErrorCode::kPayloadHashMismatchError"; |
| 1316 | case ErrorCode::kPayloadSizeMismatchError: |
| 1317 | return "ErrorCode::kPayloadSizeMismatchError"; |
| 1318 | case ErrorCode::kDownloadPayloadVerificationError: |
| 1319 | return "ErrorCode::kDownloadPayloadVerificationError"; |
| 1320 | case ErrorCode::kDownloadNewPartitionInfoError: |
| 1321 | return "ErrorCode::kDownloadNewPartitionInfoError"; |
| 1322 | case ErrorCode::kDownloadWriteError: |
| 1323 | return "ErrorCode::kDownloadWriteError"; |
| 1324 | case ErrorCode::kNewRootfsVerificationError: |
| 1325 | return "ErrorCode::kNewRootfsVerificationError"; |
| 1326 | case ErrorCode::kNewKernelVerificationError: |
| 1327 | return "ErrorCode::kNewKernelVerificationError"; |
| 1328 | case ErrorCode::kSignedDeltaPayloadExpectedError: |
| 1329 | return "ErrorCode::kSignedDeltaPayloadExpectedError"; |
| 1330 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: |
| 1331 | return "ErrorCode::kDownloadPayloadPubKeyVerificationError"; |
| 1332 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 1333 | return "ErrorCode::kPostinstallBootedFromFirmwareB"; |
| 1334 | case ErrorCode::kDownloadStateInitializationError: |
| 1335 | return "ErrorCode::kDownloadStateInitializationError"; |
| 1336 | case ErrorCode::kDownloadInvalidMetadataMagicString: |
| 1337 | return "ErrorCode::kDownloadInvalidMetadataMagicString"; |
| 1338 | case ErrorCode::kDownloadSignatureMissingInManifest: |
| 1339 | return "ErrorCode::kDownloadSignatureMissingInManifest"; |
| 1340 | case ErrorCode::kDownloadManifestParseError: |
| 1341 | return "ErrorCode::kDownloadManifestParseError"; |
| 1342 | case ErrorCode::kDownloadMetadataSignatureError: |
| 1343 | return "ErrorCode::kDownloadMetadataSignatureError"; |
| 1344 | case ErrorCode::kDownloadMetadataSignatureVerificationError: |
| 1345 | return "ErrorCode::kDownloadMetadataSignatureVerificationError"; |
| 1346 | case ErrorCode::kDownloadMetadataSignatureMismatch: |
| 1347 | return "ErrorCode::kDownloadMetadataSignatureMismatch"; |
| 1348 | case ErrorCode::kDownloadOperationHashVerificationError: |
| 1349 | return "ErrorCode::kDownloadOperationHashVerificationError"; |
| 1350 | case ErrorCode::kDownloadOperationExecutionError: |
| 1351 | return "ErrorCode::kDownloadOperationExecutionError"; |
| 1352 | case ErrorCode::kDownloadOperationHashMismatch: |
| 1353 | return "ErrorCode::kDownloadOperationHashMismatch"; |
| 1354 | case ErrorCode::kOmahaRequestEmptyResponseError: |
| 1355 | return "ErrorCode::kOmahaRequestEmptyResponseError"; |
| 1356 | case ErrorCode::kOmahaRequestXMLParseError: |
| 1357 | return "ErrorCode::kOmahaRequestXMLParseError"; |
| 1358 | case ErrorCode::kDownloadInvalidMetadataSize: |
| 1359 | return "ErrorCode::kDownloadInvalidMetadataSize"; |
| 1360 | case ErrorCode::kDownloadInvalidMetadataSignature: |
| 1361 | return "ErrorCode::kDownloadInvalidMetadataSignature"; |
| 1362 | case ErrorCode::kOmahaResponseInvalid: |
| 1363 | return "ErrorCode::kOmahaResponseInvalid"; |
| 1364 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 1365 | return "ErrorCode::kOmahaUpdateIgnoredPerPolicy"; |
| 1366 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 1367 | return "ErrorCode::kOmahaUpdateDeferredPerPolicy"; |
| 1368 | case ErrorCode::kOmahaErrorInHTTPResponse: |
| 1369 | return "ErrorCode::kOmahaErrorInHTTPResponse"; |
| 1370 | case ErrorCode::kDownloadOperationHashMissingError: |
| 1371 | return "ErrorCode::kDownloadOperationHashMissingError"; |
| 1372 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 1373 | return "ErrorCode::kDownloadMetadataSignatureMissingError"; |
| 1374 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 1375 | return "ErrorCode::kOmahaUpdateDeferredForBackoff"; |
| 1376 | case ErrorCode::kPostinstallPowerwashError: |
| 1377 | return "ErrorCode::kPostinstallPowerwashError"; |
| 1378 | case ErrorCode::kUpdateCanceledByChannelChange: |
| 1379 | return "ErrorCode::kUpdateCanceledByChannelChange"; |
| 1380 | case ErrorCode::kUmaReportedMax: |
| 1381 | return "ErrorCode::kUmaReportedMax"; |
| 1382 | case ErrorCode::kOmahaRequestHTTPResponseBase: |
| 1383 | return "ErrorCode::kOmahaRequestHTTPResponseBase"; |
| 1384 | case ErrorCode::kResumedFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1385 | return "Resumed"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1386 | case ErrorCode::kDevModeFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1387 | return "DevMode"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1388 | case ErrorCode::kTestImageFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1389 | return "TestImage"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1390 | case ErrorCode::kTestOmahaUrlFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1391 | return "TestOmahaUrl"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1392 | case ErrorCode::kSpecialFlags: |
| 1393 | return "ErrorCode::kSpecialFlags"; |
| 1394 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
| 1395 | return "ErrorCode::kPostinstallFirmwareRONotUpdatable"; |
| 1396 | case ErrorCode::kUnsupportedMajorPayloadVersion: |
| 1397 | return "ErrorCode::kUnsupportedMajorPayloadVersion"; |
| 1398 | case ErrorCode::kUnsupportedMinorPayloadVersion: |
| 1399 | return "ErrorCode::kUnsupportedMinorPayloadVersion"; |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1400 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
| 1401 | return "ErrorCode::kOmahaRequestXMLHasEntityDecl"; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1402 | case ErrorCode::kFilesystemVerifierError: |
| 1403 | return "ErrorCode::kFilesystemVerifierError"; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1404 | // Don't add a default case to let the compiler warn about newly added |
| 1405 | // error codes which should be added here. |
| 1406 | } |
| 1407 | |
| 1408 | return "Unknown error: " + base::UintToString(static_cast<unsigned>(code)); |
| 1409 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1410 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1411 | bool CreatePowerwashMarkerFile(const char* file_path) { |
| 1412 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
| 1413 | bool result = utils::WriteFile(marker_file, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1414 | kPowerwashCommand, |
| 1415 | strlen(kPowerwashCommand)); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1416 | if (result) { |
| 1417 | LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot"; |
| 1418 | } else { |
| 1419 | PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file; |
| 1420 | } |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1421 | |
| 1422 | return result; |
| 1423 | } |
| 1424 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1425 | bool DeletePowerwashMarkerFile(const char* file_path) { |
| 1426 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1427 | const base::FilePath kPowerwashMarkerPath(marker_file); |
| 1428 | bool result = base::DeleteFile(kPowerwashMarkerPath, false); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1429 | |
| 1430 | if (result) |
| 1431 | LOG(INFO) << "Successfully deleted the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1432 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1433 | else |
| 1434 | PLOG(ERROR) << "Could not delete the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1435 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1436 | |
| 1437 | return result; |
| 1438 | } |
| 1439 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1440 | Time TimeFromStructTimespec(struct timespec *ts) { |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1441 | int64_t us = static_cast<int64_t>(ts->tv_sec) * Time::kMicrosecondsPerSecond + |
| 1442 | static_cast<int64_t>(ts->tv_nsec) / Time::kNanosecondsPerMicrosecond; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1443 | return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us); |
| 1444 | } |
| 1445 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1446 | string StringVectorToString(const vector<string> &vec_str) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1447 | string str = "["; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1448 | for (vector<string>::const_iterator i = vec_str.begin(); |
| 1449 | i != vec_str.end(); ++i) { |
| 1450 | if (i != vec_str.begin()) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1451 | str += ", "; |
| 1452 | str += '"'; |
| 1453 | str += *i; |
| 1454 | str += '"'; |
| 1455 | } |
| 1456 | str += "]"; |
| 1457 | return str; |
| 1458 | } |
| 1459 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1460 | string CalculateP2PFileId(const string& payload_hash, size_t payload_size) { |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1461 | string encoded_hash = chromeos::data_encoding::Base64Encode(payload_hash); |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 1462 | return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s", |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1463 | payload_size, |
| 1464 | encoded_hash.c_str()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1467 | bool DecodeAndStoreBase64String(const string& base64_encoded, |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1468 | base::FilePath *out_path) { |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1469 | chromeos::Blob contents; |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1470 | |
| 1471 | out_path->clear(); |
| 1472 | |
| 1473 | if (base64_encoded.size() == 0) { |
| 1474 | LOG(ERROR) << "Can't decode empty string."; |
| 1475 | return false; |
| 1476 | } |
| 1477 | |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1478 | if (!chromeos::data_encoding::Base64Decode(base64_encoded, &contents) || |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1479 | contents.size() == 0) { |
| 1480 | LOG(ERROR) << "Error decoding base64."; |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1484 | FILE *file = base::CreateAndOpenTemporaryFile(out_path); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1485 | if (file == nullptr) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1486 | LOG(ERROR) << "Error creating temporary file."; |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1490 | if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1491 | PLOG(ERROR) << "Error writing to temporary file."; |
| 1492 | if (fclose(file) != 0) |
| 1493 | PLOG(ERROR) << "Error closing temporary file."; |
| 1494 | if (unlink(out_path->value().c_str()) != 0) |
| 1495 | PLOG(ERROR) << "Error unlinking temporary file."; |
| 1496 | out_path->clear(); |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | if (fclose(file) != 0) { |
| 1501 | PLOG(ERROR) << "Error closing temporary file."; |
| 1502 | out_path->clear(); |
| 1503 | return false; |
| 1504 | } |
| 1505 | |
| 1506 | return true; |
| 1507 | } |
| 1508 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1509 | bool ConvertToOmahaInstallDate(Time time, int *out_num_days) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1510 | time_t unix_time = time.ToTimeT(); |
| 1511 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". |
| 1512 | const time_t kOmahaEpoch = 1167638400; |
| 1513 | const int64_t kNumSecondsPerWeek = 7*24*3600; |
| 1514 | const int64_t kNumDaysPerWeek = 7; |
| 1515 | |
| 1516 | time_t omaha_time = unix_time - kOmahaEpoch; |
| 1517 | |
| 1518 | if (omaha_time < 0) |
| 1519 | return false; |
| 1520 | |
| 1521 | // Note, as per the comment in utils.h we are deliberately not |
| 1522 | // handling DST correctly. |
| 1523 | |
| 1524 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; |
| 1525 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; |
| 1526 | |
| 1527 | return true; |
| 1528 | } |
| 1529 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1530 | bool WallclockDurationHelper(SystemState* system_state, |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1531 | const string& state_variable_key, |
| 1532 | TimeDelta* out_duration) { |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1533 | bool ret = false; |
| 1534 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1535 | Time now = system_state->clock()->GetWallclockTime(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1536 | int64_t stored_value; |
| 1537 | if (system_state->prefs()->GetInt64(state_variable_key, &stored_value)) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1538 | Time stored_time = Time::FromInternalValue(stored_value); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1539 | if (stored_time > now) { |
| 1540 | LOG(ERROR) << "Stored time-stamp used for " << state_variable_key |
| 1541 | << " is in the future."; |
| 1542 | } else { |
| 1543 | *out_duration = now - stored_time; |
| 1544 | ret = true; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | if (!system_state->prefs()->SetInt64(state_variable_key, |
| 1549 | now.ToInternalValue())) { |
| 1550 | LOG(ERROR) << "Error storing time-stamp in " << state_variable_key; |
| 1551 | } |
| 1552 | |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | bool MonotonicDurationHelper(SystemState* system_state, |
| 1557 | int64_t* storage, |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1558 | TimeDelta* out_duration) { |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1559 | bool ret = false; |
| 1560 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1561 | Time now = system_state->clock()->GetMonotonicTime(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1562 | if (*storage != 0) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1563 | Time stored_time = Time::FromInternalValue(*storage); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1564 | *out_duration = now - stored_time; |
| 1565 | ret = true; |
| 1566 | } |
| 1567 | *storage = now.ToInternalValue(); |
| 1568 | |
| 1569 | return ret; |
| 1570 | } |
| 1571 | |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1572 | bool GetMinorVersion(const chromeos::KeyValueStore& store, |
| 1573 | uint32_t* minor_version) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1574 | string result; |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1575 | if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) { |
Allie Wood | 425aa97 | 2015-02-17 14:47:17 -0800 | [diff] [blame] | 1576 | if (!base::StringToUint(result, minor_version)) { |
| 1577 | LOG(ERROR) << "StringToUint failed when parsing delta minor version."; |
| 1578 | return false; |
| 1579 | } |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 1580 | return true; |
| 1581 | } |
| 1582 | return false; |
| 1583 | } |
| 1584 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1585 | bool ReadExtents(const string& path, const vector<Extent>& extents, |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1586 | chromeos::Blob* out_data, ssize_t out_data_size, |
| 1587 | size_t block_size) { |
| 1588 | chromeos::Blob data(out_data_size); |
| 1589 | ssize_t bytes_read = 0; |
| 1590 | int fd = open(path.c_str(), O_RDONLY); |
| 1591 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 1592 | ScopedFdCloser fd_closer(&fd); |
| 1593 | |
Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 1594 | for (const Extent& extent : extents) { |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1595 | ssize_t bytes_read_this_iteration = 0; |
| 1596 | ssize_t bytes = extent.num_blocks() * block_size; |
| 1597 | TEST_AND_RETURN_FALSE(bytes_read + bytes <= out_data_size); |
| 1598 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
| 1599 | &data[bytes_read], |
| 1600 | bytes, |
| 1601 | extent.start_block() * block_size, |
| 1602 | &bytes_read_this_iteration)); |
| 1603 | TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes); |
| 1604 | bytes_read += bytes_read_this_iteration; |
| 1605 | } |
| 1606 | TEST_AND_RETURN_FALSE(out_data_size == bytes_read); |
| 1607 | *out_data = data; |
| 1608 | return true; |
| 1609 | } |
| 1610 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1611 | } // namespace utils |
| 1612 | |
| 1613 | } // namespace chromeos_update_engine |