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