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