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