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