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