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