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 <sys/mount.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 9 | #include <sys/resource.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 12 | #include <sys/wait.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include <dirent.h> |
| 14 | #include <errno.h> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 15 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 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> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 22 | #include <vector> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 23 | |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 24 | #include <base/file_util.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame^] | 25 | #include <base/files/file_path.h> |
| 26 | #include <base/files/scoped_file.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 27 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 28 | #include <base/posix/eintr_wrapper.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 29 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 30 | #include <base/strings/string_number_conversions.h> |
| 31 | #include <base/strings/string_split.h> |
| 32 | #include <base/strings/string_util.h> |
| 33 | #include <base/strings/stringprintf.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 34 | #include <glib.h> |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 35 | #include <google/protobuf/stubs/common.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 36 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 37 | #include "update_engine/clock_interface.h" |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 38 | #include "update_engine/constants.h" |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 39 | #include "update_engine/file_writer.h" |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 40 | #include "update_engine/omaha_request_params.h" |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 41 | #include "update_engine/prefs_interface.h" |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 42 | #include "update_engine/subprocess.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 43 | #include "update_engine/system_state.h" |
| 44 | #include "update_engine/update_attempter.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 46 | using base::Time; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 47 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 48 | using std::min; |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 49 | using std::pair; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 50 | using std::string; |
| 51 | using std::vector; |
| 52 | |
| 53 | namespace chromeos_update_engine { |
| 54 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 55 | namespace { |
| 56 | |
| 57 | // The following constants control how UnmountFilesystem should retry if |
| 58 | // umount() fails with an errno EBUSY, i.e. retry 5 times over the course of |
| 59 | // one second. |
| 60 | const int kUnmountMaxNumOfRetries = 5; |
| 61 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 62 | |
| 63 | // Number of bytes to read from a file to attempt to detect its contents. Used |
| 64 | // in GetFileFormat. |
| 65 | const int kGetFileFormatMaxHeaderSize = 32; |
| 66 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 67 | } // namespace |
| 68 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | namespace utils { |
| 70 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 71 | // Cgroup container is created in update-engine's upstart script located at |
| 72 | // /etc/init/update-engine.conf. |
| 73 | static const char kCGroupDir[] = "/sys/fs/cgroup/cpu/update-engine"; |
| 74 | |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 75 | string ParseECVersion(string input_line) { |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame^] | 76 | base::TrimWhitespaceASCII(input_line, base::TRIM_ALL, &input_line); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 77 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 78 | // 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] | 79 | // a vector of key value pairs. |
| 80 | vector<pair<string, string> > kv_pairs; |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 81 | if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 82 | for (vector<pair<string, string> >::iterator it = kv_pairs.begin(); |
| 83 | it != kv_pairs.end(); ++it) { |
| 84 | // Finally match against the fw_verion which may have quotes. |
| 85 | if (it->first == "fw_version") { |
| 86 | string output; |
| 87 | // Trim any quotes. |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 88 | base::TrimString(it->second, "\"", &output); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 89 | return output; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | LOG(ERROR) << "Unable to parse fwid from ec info."; |
| 94 | return ""; |
| 95 | } |
| 96 | |
| 97 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 98 | const string KernelDeviceOfBootDevice(const string& boot_device) { |
| 99 | string kernel_partition_name; |
J. Richard Barnette | 3084293 | 2013-10-28 15:04:23 -0700 | [diff] [blame] | 100 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 101 | string disk_name; |
| 102 | int partition_num; |
| 103 | if (SplitPartitionName(boot_device, &disk_name, &partition_num)) { |
| 104 | if (disk_name == "/dev/ubiblock") { |
| 105 | // Special case for NAND devices. |
| 106 | // eg: /dev/ubiblock3_0 becomes /dev/mtdblock2 |
| 107 | disk_name = "/dev/mtdblock"; |
| 108 | } |
| 109 | // Currently this assumes the partition number of the boot device is |
| 110 | // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to |
| 111 | // get the kernel device. |
| 112 | if (partition_num == 3 || partition_num == 5 || partition_num == 7) { |
| 113 | kernel_partition_name = MakePartitionName(disk_name, partition_num - 1); |
| 114 | } |
J. Richard Barnette | 3084293 | 2013-10-28 15:04:23 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 117 | return kernel_partition_name; |
J. Richard Barnette | 3084293 | 2013-10-28 15:04:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 121 | bool WriteFile(const char* path, const char* data, int data_len) { |
| 122 | DirectFileWriter writer; |
| 123 | TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, |
| 124 | O_WRONLY | O_CREAT | O_TRUNC, |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 125 | 0600)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 126 | ScopedFileWriterCloser closer(&writer); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 127 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(data, data_len)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 131 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 132 | const char* c_buf = static_cast<const char*>(buf); |
| 133 | ssize_t bytes_written = 0; |
| 134 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 135 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 136 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 137 | bytes_written += rc; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 142 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 143 | const char* c_buf = static_cast<const char*>(buf); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 144 | size_t bytes_written = 0; |
| 145 | int num_attempts = 0; |
| 146 | while (bytes_written < count) { |
| 147 | num_attempts++; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 148 | ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, |
| 149 | offset + bytes_written); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 150 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. |
| 151 | if (rc < 0) { |
| 152 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts |
| 153 | << " bytes_written=" << bytes_written |
| 154 | << " count=" << count << " offset=" << offset; |
| 155 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 156 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 157 | bytes_written += rc; |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 163 | ssize_t* out_bytes_read) { |
| 164 | char* c_buf = static_cast<char*>(buf); |
| 165 | ssize_t bytes_read = 0; |
| 166 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 167 | ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 168 | offset + bytes_read); |
| 169 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 170 | if (rc == 0) { |
| 171 | break; |
| 172 | } |
| 173 | bytes_read += rc; |
| 174 | } |
| 175 | *out_bytes_read = bytes_read; |
| 176 | return true; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 177 | |
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 | }; |
| 330 | } // namespace {} |
| 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 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 530 | std::string TempFilename(string path) { |
| 531 | static const string suffix("XXXXXX"); |
| 532 | CHECK(StringHasSuffix(path, suffix)); |
| 533 | do { |
| 534 | string new_suffix; |
| 535 | for (unsigned int i = 0; i < suffix.size(); i++) { |
| 536 | int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9] |
| 537 | if (r < 26) |
| 538 | new_suffix.append(1, 'a' + r); |
| 539 | else if (r < (26 * 2)) |
| 540 | new_suffix.append(1, 'A' + r - 26); |
| 541 | else |
| 542 | new_suffix.append(1, '0' + r - (26 * 2)); |
| 543 | } |
| 544 | CHECK_EQ(new_suffix.size(), suffix.size()); |
| 545 | path.resize(path.size() - new_suffix.size()); |
| 546 | path.append(new_suffix); |
| 547 | } while (FileExists(path.c_str())); |
| 548 | return path; |
| 549 | } |
| 550 | |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 551 | // If |path| is absolute, or explicit relative to the current working directory, |
| 552 | // leaves it as is. Otherwise, if TMPDIR is defined in the environment and is |
| 553 | // non-empty, prepends it to |path|. Otherwise, prepends /tmp. Returns the |
| 554 | // resulting path. |
| 555 | static const string PrependTmpdir(const string& path) { |
| 556 | if (path[0] == '/' || StartsWithASCII(path, "./", true) || |
| 557 | StartsWithASCII(path, "../", true)) |
| 558 | return path; |
| 559 | |
| 560 | const char *tmpdir = getenv("TMPDIR"); |
| 561 | const string prefix = (tmpdir && *tmpdir ? tmpdir : "/tmp"); |
| 562 | return prefix + "/" + path; |
| 563 | } |
| 564 | |
| 565 | bool MakeTempFile(const std::string& base_filename_template, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 566 | std::string* filename, |
| 567 | int* fd) { |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 568 | const string filename_template = PrependTmpdir(base_filename_template); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 569 | DCHECK(filename || fd); |
| 570 | vector<char> buf(filename_template.size() + 1); |
| 571 | memcpy(&buf[0], filename_template.data(), filename_template.size()); |
| 572 | buf[filename_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 573 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 574 | int mkstemp_fd = mkstemp(&buf[0]); |
| 575 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 576 | if (filename) { |
| 577 | *filename = &buf[0]; |
| 578 | } |
| 579 | if (fd) { |
| 580 | *fd = mkstemp_fd; |
| 581 | } else { |
| 582 | close(mkstemp_fd); |
| 583 | } |
| 584 | return true; |
| 585 | } |
| 586 | |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 587 | bool MakeTempDirectory(const std::string& base_dirname_template, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 588 | std::string* dirname) { |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 589 | const string dirname_template = PrependTmpdir(base_dirname_template); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 590 | DCHECK(dirname); |
| 591 | vector<char> buf(dirname_template.size() + 1); |
| 592 | memcpy(&buf[0], dirname_template.data(), dirname_template.size()); |
| 593 | buf[dirname_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 594 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 595 | char* return_code = mkdtemp(&buf[0]); |
| 596 | TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL); |
| 597 | *dirname = &buf[0]; |
| 598 | return true; |
| 599 | } |
| 600 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 601 | bool StringHasSuffix(const std::string& str, const std::string& suffix) { |
| 602 | if (suffix.size() > str.size()) |
| 603 | return false; |
| 604 | return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); |
| 605 | } |
| 606 | |
| 607 | bool StringHasPrefix(const std::string& str, const std::string& prefix) { |
| 608 | if (prefix.size() > str.size()) |
| 609 | return false; |
| 610 | return 0 == str.compare(0, prefix.size(), prefix); |
| 611 | } |
| 612 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 613 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 614 | const string& mountpoint, |
| 615 | unsigned long mountflags) { |
| 616 | 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] | 617 | if (rc < 0) { |
| 618 | string msg = ErrnoNumberAsString(errno); |
| 619 | LOG(ERROR) << "Unable to mount destination device: " << msg << ". " |
| 620 | << device << " on " << mountpoint; |
| 621 | return false; |
| 622 | } |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | bool UnmountFilesystem(const string& mountpoint) { |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 627 | for (int num_retries = 0; ; ++num_retries) { |
| 628 | if (umount(mountpoint.c_str()) == 0) |
| 629 | break; |
| 630 | |
| 631 | TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY && |
| 632 | num_retries < kUnmountMaxNumOfRetries); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 633 | g_usleep(kUnmountRetryIntervalInMicroseconds); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 634 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 635 | return true; |
| 636 | } |
| 637 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 638 | bool GetFilesystemSize(const std::string& device, |
| 639 | int* out_block_count, |
| 640 | int* out_block_size) { |
| 641 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY)); |
| 642 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 643 | ScopedFdCloser fd_closer(&fd); |
| 644 | return GetFilesystemSizeFromFD(fd, out_block_count, out_block_size); |
| 645 | } |
| 646 | |
| 647 | bool GetFilesystemSizeFromFD(int fd, |
| 648 | int* out_block_count, |
| 649 | int* out_block_size) { |
| 650 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 651 | |
| 652 | // Determine the ext3 filesystem size by directly reading the block count and |
| 653 | // block size information from the superblock. See include/linux/ext3_fs.h for |
| 654 | // more details on the structure. |
| 655 | ssize_t kBufferSize = 16 * sizeof(uint32_t); |
| 656 | char buffer[kBufferSize]; |
| 657 | const int kSuperblockOffset = 1024; |
| 658 | if (HANDLE_EINTR(pread(fd, buffer, kBufferSize, kSuperblockOffset)) != |
| 659 | kBufferSize) { |
| 660 | PLOG(ERROR) << "Unable to determine file system size:"; |
| 661 | return false; |
| 662 | } |
| 663 | uint32_t block_count; // ext3_fs.h: ext3_super_block.s_blocks_count |
| 664 | uint32_t log_block_size; // ext3_fs.h: ext3_super_block.s_log_block_size |
| 665 | uint16_t magic; // ext3_fs.h: ext3_super_block.s_magic |
| 666 | memcpy(&block_count, &buffer[1 * sizeof(int32_t)], sizeof(block_count)); |
| 667 | memcpy(&log_block_size, &buffer[6 * sizeof(int32_t)], sizeof(log_block_size)); |
| 668 | memcpy(&magic, &buffer[14 * sizeof(int32_t)], sizeof(magic)); |
| 669 | block_count = le32toh(block_count); |
| 670 | const int kExt3MinBlockLogSize = 10; // ext3_fs.h: EXT3_MIN_BLOCK_LOG_SIZE |
| 671 | log_block_size = le32toh(log_block_size) + kExt3MinBlockLogSize; |
| 672 | magic = le16toh(magic); |
| 673 | |
| 674 | // Sanity check the parameters. |
| 675 | const uint16_t kExt3SuperMagic = 0xef53; // ext3_fs.h: EXT3_SUPER_MAGIC |
| 676 | TEST_AND_RETURN_FALSE(magic == kExt3SuperMagic); |
| 677 | const int kExt3MinBlockSize = 1024; // ext3_fs.h: EXT3_MIN_BLOCK_SIZE |
| 678 | const int kExt3MaxBlockSize = 4096; // ext3_fs.h: EXT3_MAX_BLOCK_SIZE |
| 679 | int block_size = 1 << log_block_size; |
| 680 | TEST_AND_RETURN_FALSE(block_size >= kExt3MinBlockSize && |
| 681 | block_size <= kExt3MaxBlockSize); |
| 682 | TEST_AND_RETURN_FALSE(block_count > 0); |
| 683 | |
| 684 | if (out_block_count) { |
| 685 | *out_block_count = block_count; |
| 686 | } |
| 687 | if (out_block_size) { |
| 688 | *out_block_size = block_size; |
| 689 | } |
| 690 | return true; |
| 691 | } |
| 692 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 693 | // Tries to parse the header of an ELF file to obtain a human-readable |
| 694 | // description of it on the |output| string. |
| 695 | static bool GetFileFormatELF(const char* buffer, size_t size, string* output) { |
| 696 | // 0x00: EI_MAG - ELF magic header, 4 bytes. |
| 697 | if (size < 4 || memcmp(buffer, "\x7F""ELF", 4) != 0) |
| 698 | return false; |
| 699 | *output = "ELF"; |
| 700 | |
| 701 | // 0x04: EI_CLASS, 1 byte. |
| 702 | if (size < 0x04 + 1) |
| 703 | return true; |
| 704 | switch (buffer[4]) { |
| 705 | case 1: |
| 706 | *output += " 32-bit"; |
| 707 | break; |
| 708 | case 2: |
| 709 | *output += " 64-bit"; |
| 710 | break; |
| 711 | default: |
| 712 | *output += " ?-bit"; |
| 713 | } |
| 714 | |
| 715 | // 0x05: EI_DATA, endianness, 1 byte. |
| 716 | if (size < 0x05 + 1) |
| 717 | return true; |
| 718 | char ei_data = buffer[5]; |
| 719 | switch (ei_data) { |
| 720 | case 1: |
| 721 | *output += " little-endian"; |
| 722 | break; |
| 723 | case 2: |
| 724 | *output += " big-endian"; |
| 725 | break; |
| 726 | default: |
| 727 | *output += " ?-endian"; |
| 728 | // Don't parse anything after the 0x10 offset if endianness is unknown. |
| 729 | return true; |
| 730 | } |
| 731 | |
| 732 | // 0x12: e_machine, 2 byte endianness based on ei_data |
| 733 | if (size < 0x12 + 2) |
| 734 | return true; |
| 735 | uint16 e_machine = *reinterpret_cast<const uint16*>(buffer+0x12); |
| 736 | // Fix endianess regardless of the host endianess. |
| 737 | if (ei_data == 1) |
| 738 | e_machine = le16toh(e_machine); |
| 739 | else |
| 740 | e_machine = be16toh(e_machine); |
| 741 | |
| 742 | switch (e_machine) { |
| 743 | case 0x03: |
| 744 | *output += " x86"; |
| 745 | break; |
| 746 | case 0x28: |
| 747 | *output += " arm"; |
| 748 | break; |
| 749 | case 0x3E: |
| 750 | *output += " x86-64"; |
| 751 | break; |
| 752 | default: |
| 753 | *output += " unknown-arch"; |
| 754 | } |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | string GetFileFormat(const string& path) { |
| 759 | vector<char> buffer; |
| 760 | if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer)) |
| 761 | return "File not found."; |
| 762 | |
| 763 | string result; |
| 764 | if (GetFileFormatELF(buffer.data(), buffer.size(), &result)) |
| 765 | return result; |
| 766 | |
| 767 | return "data"; |
| 768 | } |
| 769 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 770 | bool GetBootloader(BootLoader* out_bootloader) { |
| 771 | // For now, hardcode to syslinux. |
| 772 | *out_bootloader = BootLoader_SYSLINUX; |
| 773 | return true; |
| 774 | } |
| 775 | |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 776 | string GetAndFreeGError(GError** error) { |
| 777 | if (!*error) { |
| 778 | return "Unknown GLib error."; |
| 779 | } |
| 780 | string message = |
| 781 | base::StringPrintf("GError(%d): %s", |
| 782 | (*error)->code, |
| 783 | (*error)->message ? (*error)->message : "(unknown)"); |
| 784 | g_error_free(*error); |
| 785 | *error = NULL; |
| 786 | return message; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 789 | bool Reboot() { |
| 790 | vector<string> command; |
| 791 | command.push_back("/sbin/shutdown"); |
| 792 | command.push_back("-r"); |
| 793 | command.push_back("now"); |
| 794 | int rc = 0; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 795 | Subprocess::SynchronousExec(command, &rc, NULL); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 796 | TEST_AND_RETURN_FALSE(rc == 0); |
| 797 | return true; |
| 798 | } |
| 799 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 800 | namespace { |
| 801 | // Do the actual trigger. We do it as a main-loop callback to (try to) get a |
| 802 | // consistent stack trace. |
| 803 | gboolean TriggerCrashReporterUpload(void* unused) { |
| 804 | pid_t pid = fork(); |
| 805 | CHECK(pid >= 0) << "fork failed"; // fork() failed. Something is very wrong. |
| 806 | if (pid == 0) { |
| 807 | // We are the child. Crash. |
| 808 | abort(); // never returns |
| 809 | } |
| 810 | // We are the parent. Wait for child to terminate. |
| 811 | pid_t result = waitpid(pid, NULL, 0); |
| 812 | LOG_IF(ERROR, result < 0) << "waitpid() failed"; |
| 813 | return FALSE; // Don't call this callback again |
| 814 | } |
| 815 | } // namespace {} |
| 816 | |
| 817 | void ScheduleCrashReporterUpload() { |
| 818 | g_idle_add(&TriggerCrashReporterUpload, NULL); |
| 819 | } |
| 820 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 821 | bool SetCpuShares(CpuShares shares) { |
| 822 | string string_shares = base::IntToString(static_cast<int>(shares)); |
| 823 | string cpu_shares_file = string(utils::kCGroupDir) + "/cpu.shares"; |
| 824 | LOG(INFO) << "Setting cgroup cpu shares to " << string_shares; |
| 825 | if(utils::WriteFile(cpu_shares_file.c_str(), string_shares.c_str(), |
| 826 | string_shares.size())){ |
| 827 | return true; |
| 828 | } else { |
| 829 | LOG(ERROR) << "Failed to change cgroup cpu shares to "<< string_shares |
| 830 | << " using " << cpu_shares_file; |
| 831 | return false; |
| 832 | } |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 835 | int CompareCpuShares(CpuShares shares_lhs, |
| 836 | CpuShares shares_rhs) { |
| 837 | return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 840 | int FuzzInt(int value, unsigned int range) { |
| 841 | int min = value - range / 2; |
| 842 | int max = value + range - range / 2; |
| 843 | return base::RandInt(min, max); |
| 844 | } |
| 845 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 846 | gboolean GlibRunClosure(gpointer data) { |
| 847 | google::protobuf::Closure* callback = |
| 848 | reinterpret_cast<google::protobuf::Closure*>(data); |
| 849 | callback->Run(); |
| 850 | return FALSE; |
| 851 | } |
| 852 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 853 | string FormatSecs(unsigned secs) { |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 854 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 857 | string FormatTimeDelta(TimeDelta delta) { |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 858 | // Canonicalize into days, hours, minutes, seconds and microseconds. |
| 859 | unsigned days = delta.InDays(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 860 | delta -= TimeDelta::FromDays(days); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 861 | unsigned hours = delta.InHours(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 862 | delta -= TimeDelta::FromHours(hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 863 | unsigned mins = delta.InMinutes(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 864 | delta -= TimeDelta::FromMinutes(mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 865 | unsigned secs = delta.InSeconds(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 866 | delta -= TimeDelta::FromSeconds(secs); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 867 | unsigned usecs = delta.InMicroseconds(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 868 | |
| 869 | // Construct and return string. |
| 870 | string str; |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 871 | if (days) |
| 872 | base::StringAppendF(&str, "%ud", days); |
| 873 | if (days || hours) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 874 | base::StringAppendF(&str, "%uh", hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 875 | if (days || hours || mins) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 876 | base::StringAppendF(&str, "%um", mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 877 | base::StringAppendF(&str, "%u", secs); |
| 878 | if (usecs) { |
| 879 | int width = 6; |
| 880 | while ((usecs / 10) * 10 == usecs) { |
| 881 | usecs /= 10; |
| 882 | width--; |
| 883 | } |
| 884 | base::StringAppendF(&str, ".%0*u", width, usecs); |
| 885 | } |
| 886 | base::StringAppendF(&str, "s"); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 887 | return str; |
| 888 | } |
| 889 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 890 | string ToString(const Time utc_time) { |
| 891 | Time::Exploded exp_time; |
| 892 | utc_time.UTCExplode(&exp_time); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 893 | return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT", |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 894 | exp_time.month, |
| 895 | exp_time.day_of_month, |
| 896 | exp_time.year, |
| 897 | exp_time.hour, |
| 898 | exp_time.minute, |
| 899 | exp_time.second); |
| 900 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 901 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 902 | string ToString(bool b) { |
| 903 | return (b ? "true" : "false"); |
| 904 | } |
| 905 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 906 | string ToString(DownloadSource source) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 907 | switch (source) { |
| 908 | case kDownloadSourceHttpsServer: return "HttpsServer"; |
| 909 | case kDownloadSourceHttpServer: return "HttpServer"; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 910 | case kDownloadSourceHttpPeer: return "HttpPeer"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 911 | case kNumDownloadSources: return "Unknown"; |
| 912 | // Don't add a default case to let the compiler warn about newly added |
| 913 | // download sources which should be added here. |
| 914 | } |
| 915 | |
| 916 | return "Unknown"; |
| 917 | } |
| 918 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 919 | string ToString(PayloadType payload_type) { |
| 920 | switch (payload_type) { |
| 921 | case kPayloadTypeDelta: return "Delta"; |
| 922 | case kPayloadTypeFull: return "Full"; |
| 923 | case kPayloadTypeForcedFull: return "ForcedFull"; |
| 924 | case kNumPayloadTypes: return "Unknown"; |
| 925 | // Don't add a default case to let the compiler warn about newly added |
| 926 | // payload types which should be added here. |
| 927 | } |
| 928 | |
| 929 | return "Unknown"; |
| 930 | } |
| 931 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 932 | ErrorCode GetBaseErrorCode(ErrorCode code) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 933 | // Ignore the higher order bits in the code by applying the mask as |
| 934 | // we want the enumerations to be in the small contiguous range |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 935 | // with values less than kErrorCodeUmaReportedMax. |
| 936 | ErrorCode base_code = static_cast<ErrorCode>(code & ~kErrorCodeSpecialFlags); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 937 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 938 | // Make additional adjustments required for UMA and error classification. |
| 939 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix |
| 940 | // chromium-os:34369. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 941 | if (base_code >= kErrorCodeOmahaRequestHTTPResponseBase) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 942 | // 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] | 943 | // errors into this one bucket for UMA and error classification purposes. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 944 | LOG(INFO) << "Converting error code " << base_code |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 945 | << " to kErrorCodeOmahaErrorInHTTPResponse"; |
| 946 | base_code = kErrorCodeOmahaErrorInHTTPResponse; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 949 | return base_code; |
| 950 | } |
| 951 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 952 | metrics::AttemptResult GetAttemptResult(ErrorCode code) { |
| 953 | ErrorCode base_code = static_cast<ErrorCode>(code & ~kErrorCodeSpecialFlags); |
| 954 | |
| 955 | switch (base_code) { |
| 956 | case kErrorCodeSuccess: |
| 957 | return metrics::AttemptResult::kUpdateSucceeded; |
| 958 | |
| 959 | case kErrorCodeDownloadTransferError: |
| 960 | return metrics::AttemptResult::kPayloadDownloadError; |
| 961 | |
| 962 | case kErrorCodeDownloadInvalidMetadataSize: |
| 963 | case kErrorCodeDownloadInvalidMetadataMagicString: |
| 964 | case kErrorCodeDownloadMetadataSignatureError: |
| 965 | case kErrorCodeDownloadMetadataSignatureVerificationError: |
| 966 | case kErrorCodePayloadMismatchedType: |
| 967 | case kErrorCodeUnsupportedMajorPayloadVersion: |
| 968 | case kErrorCodeUnsupportedMinorPayloadVersion: |
| 969 | case kErrorCodeDownloadNewPartitionInfoError: |
| 970 | case kErrorCodeDownloadSignatureMissingInManifest: |
| 971 | case kErrorCodeDownloadManifestParseError: |
| 972 | case kErrorCodeDownloadOperationHashMissingError: |
| 973 | return metrics::AttemptResult::kMetadataMalformed; |
| 974 | |
| 975 | case kErrorCodeDownloadOperationHashMismatch: |
| 976 | case kErrorCodeDownloadOperationHashVerificationError: |
| 977 | return metrics::AttemptResult::kOperationMalformed; |
| 978 | |
| 979 | case kErrorCodeDownloadOperationExecutionError: |
| 980 | case kErrorCodeInstallDeviceOpenError: |
| 981 | case kErrorCodeKernelDeviceOpenError: |
| 982 | case kErrorCodeDownloadWriteError: |
| 983 | case kErrorCodeFilesystemCopierError: |
| 984 | return metrics::AttemptResult::kOperationExecutionError; |
| 985 | |
| 986 | case kErrorCodeDownloadMetadataSignatureMismatch: |
| 987 | return metrics::AttemptResult::kMetadataVerificationFailed; |
| 988 | |
| 989 | case kErrorCodePayloadSizeMismatchError: |
| 990 | case kErrorCodePayloadHashMismatchError: |
| 991 | case kErrorCodeDownloadPayloadVerificationError: |
| 992 | case kErrorCodeSignedDeltaPayloadExpectedError: |
| 993 | case kErrorCodeDownloadPayloadPubKeyVerificationError: |
| 994 | return metrics::AttemptResult::kPayloadVerificationFailed; |
| 995 | |
| 996 | case kErrorCodeNewRootfsVerificationError: |
| 997 | case kErrorCodeNewKernelVerificationError: |
| 998 | return metrics::AttemptResult::kVerificationFailed; |
| 999 | |
| 1000 | case kErrorCodePostinstallRunnerError: |
| 1001 | case kErrorCodePostinstallBootedFromFirmwareB: |
| 1002 | case kErrorCodePostinstallFirmwareRONotUpdatable: |
| 1003 | return metrics::AttemptResult::kPostInstallFailed; |
| 1004 | |
| 1005 | // We should never get these errors in the update-attempt stage so |
| 1006 | // return internal error if this happens. |
| 1007 | case kErrorCodeError: |
| 1008 | case kErrorCodeOmahaRequestXMLParseError: |
| 1009 | case kErrorCodeOmahaRequestError: |
| 1010 | case kErrorCodeOmahaResponseHandlerError: |
| 1011 | case kErrorCodeDownloadStateInitializationError: |
| 1012 | case kErrorCodeOmahaRequestEmptyResponseError: |
| 1013 | case kErrorCodeDownloadInvalidMetadataSignature: |
| 1014 | case kErrorCodeOmahaResponseInvalid: |
| 1015 | case kErrorCodeOmahaUpdateIgnoredPerPolicy: |
| 1016 | case kErrorCodeOmahaUpdateDeferredPerPolicy: |
| 1017 | case kErrorCodeOmahaErrorInHTTPResponse: |
| 1018 | case kErrorCodeDownloadMetadataSignatureMissingError: |
| 1019 | case kErrorCodeOmahaUpdateDeferredForBackoff: |
| 1020 | case kErrorCodePostinstallPowerwashError: |
| 1021 | case kErrorCodeUpdateCanceledByChannelChange: |
| 1022 | return metrics::AttemptResult::kInternalError; |
| 1023 | |
| 1024 | // Special flags. These can't happen (we mask them out above) but |
| 1025 | // the compiler doesn't know that. Just break out so we can warn and |
| 1026 | // return |kInternalError|. |
| 1027 | case kErrorCodeUmaReportedMax: |
| 1028 | case kErrorCodeOmahaRequestHTTPResponseBase: |
| 1029 | case kErrorCodeDevModeFlag: |
| 1030 | case kErrorCodeResumedFlag: |
| 1031 | case kErrorCodeTestImageFlag: |
| 1032 | case kErrorCodeTestOmahaUrlFlag: |
| 1033 | case kErrorCodeSpecialFlags: |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | LOG(ERROR) << "Unexpected error code " << base_code; |
| 1038 | return metrics::AttemptResult::kInternalError; |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code) { |
| 1043 | ErrorCode base_code = static_cast<ErrorCode>(code & ~kErrorCodeSpecialFlags); |
| 1044 | |
| 1045 | if (base_code >= kErrorCodeOmahaRequestHTTPResponseBase) { |
| 1046 | int http_status = base_code - kErrorCodeOmahaRequestHTTPResponseBase; |
| 1047 | if (http_status >= 200 && http_status <= 599) { |
| 1048 | return static_cast<metrics::DownloadErrorCode>( |
| 1049 | static_cast<int>(metrics::DownloadErrorCode::kHttpStatus200) + |
| 1050 | http_status - 200); |
| 1051 | } else if (http_status == 0) { |
| 1052 | // The code is using HTTP Status 0 for "Unable to get http |
| 1053 | // response code." |
| 1054 | return metrics::DownloadErrorCode::kDownloadError; |
| 1055 | } |
| 1056 | LOG(WARNING) << "Unexpected HTTP status code " << http_status; |
| 1057 | return metrics::DownloadErrorCode::kHttpStatusOther; |
| 1058 | } |
| 1059 | |
| 1060 | switch (base_code) { |
| 1061 | // Unfortunately, kErrorCodeDownloadTransferError is returned for a wide |
| 1062 | // variety of errors (proxy errors, host not reachable, timeouts etc.). |
| 1063 | // |
| 1064 | // For now just map that to kDownloading. See http://crbug.com/355745 |
| 1065 | // for how we plan to add more detail in the future. |
| 1066 | case kErrorCodeDownloadTransferError: |
| 1067 | return metrics::DownloadErrorCode::kDownloadError; |
| 1068 | |
| 1069 | // All of these error codes are not related to downloading so break |
| 1070 | // out so we can warn and return InputMalformed. |
| 1071 | case kErrorCodeSuccess: |
| 1072 | case kErrorCodeError: |
| 1073 | case kErrorCodeOmahaRequestError: |
| 1074 | case kErrorCodeOmahaResponseHandlerError: |
| 1075 | case kErrorCodeFilesystemCopierError: |
| 1076 | case kErrorCodePostinstallRunnerError: |
| 1077 | case kErrorCodePayloadMismatchedType: |
| 1078 | case kErrorCodeInstallDeviceOpenError: |
| 1079 | case kErrorCodeKernelDeviceOpenError: |
| 1080 | case kErrorCodePayloadHashMismatchError: |
| 1081 | case kErrorCodePayloadSizeMismatchError: |
| 1082 | case kErrorCodeDownloadPayloadVerificationError: |
| 1083 | case kErrorCodeDownloadNewPartitionInfoError: |
| 1084 | case kErrorCodeDownloadWriteError: |
| 1085 | case kErrorCodeNewRootfsVerificationError: |
| 1086 | case kErrorCodeNewKernelVerificationError: |
| 1087 | case kErrorCodeSignedDeltaPayloadExpectedError: |
| 1088 | case kErrorCodeDownloadPayloadPubKeyVerificationError: |
| 1089 | case kErrorCodePostinstallBootedFromFirmwareB: |
| 1090 | case kErrorCodeDownloadStateInitializationError: |
| 1091 | case kErrorCodeDownloadInvalidMetadataMagicString: |
| 1092 | case kErrorCodeDownloadSignatureMissingInManifest: |
| 1093 | case kErrorCodeDownloadManifestParseError: |
| 1094 | case kErrorCodeDownloadMetadataSignatureError: |
| 1095 | case kErrorCodeDownloadMetadataSignatureVerificationError: |
| 1096 | case kErrorCodeDownloadMetadataSignatureMismatch: |
| 1097 | case kErrorCodeDownloadOperationHashVerificationError: |
| 1098 | case kErrorCodeDownloadOperationExecutionError: |
| 1099 | case kErrorCodeDownloadOperationHashMismatch: |
| 1100 | case kErrorCodeOmahaRequestEmptyResponseError: |
| 1101 | case kErrorCodeOmahaRequestXMLParseError: |
| 1102 | case kErrorCodeDownloadInvalidMetadataSize: |
| 1103 | case kErrorCodeDownloadInvalidMetadataSignature: |
| 1104 | case kErrorCodeOmahaResponseInvalid: |
| 1105 | case kErrorCodeOmahaUpdateIgnoredPerPolicy: |
| 1106 | case kErrorCodeOmahaUpdateDeferredPerPolicy: |
| 1107 | case kErrorCodeOmahaErrorInHTTPResponse: |
| 1108 | case kErrorCodeDownloadOperationHashMissingError: |
| 1109 | case kErrorCodeDownloadMetadataSignatureMissingError: |
| 1110 | case kErrorCodeOmahaUpdateDeferredForBackoff: |
| 1111 | case kErrorCodePostinstallPowerwashError: |
| 1112 | case kErrorCodeUpdateCanceledByChannelChange: |
| 1113 | case kErrorCodePostinstallFirmwareRONotUpdatable: |
| 1114 | case kErrorCodeUnsupportedMajorPayloadVersion: |
| 1115 | case kErrorCodeUnsupportedMinorPayloadVersion: |
| 1116 | break; |
| 1117 | |
| 1118 | // Special flags. These can't happen (we mask them out above) but |
| 1119 | // the compiler doesn't know that. Just break out so we can warn and |
| 1120 | // return |kInputMalformed|. |
| 1121 | case kErrorCodeUmaReportedMax: |
| 1122 | case kErrorCodeOmahaRequestHTTPResponseBase: |
| 1123 | case kErrorCodeDevModeFlag: |
| 1124 | case kErrorCodeResumedFlag: |
| 1125 | case kErrorCodeTestImageFlag: |
| 1126 | case kErrorCodeTestOmahaUrlFlag: |
| 1127 | case kErrorCodeSpecialFlags: |
| 1128 | LOG(ERROR) << "Unexpected error code " << base_code; |
| 1129 | break; |
| 1130 | } |
| 1131 | |
| 1132 | return metrics::DownloadErrorCode::kInputMalformed; |
| 1133 | } |
| 1134 | |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 1135 | metrics::ConnectionType GetConnectionType( |
| 1136 | NetworkConnectionType type, |
| 1137 | NetworkTethering tethering) { |
| 1138 | switch (type) { |
| 1139 | case kNetUnknown: |
| 1140 | return metrics::ConnectionType::kUnknown; |
| 1141 | |
| 1142 | case kNetEthernet: |
| 1143 | if (tethering == NetworkTethering::kConfirmed) |
| 1144 | return metrics::ConnectionType::kTetheredEthernet; |
| 1145 | else |
| 1146 | return metrics::ConnectionType::kEthernet; |
| 1147 | |
| 1148 | case kNetWifi: |
| 1149 | if (tethering == NetworkTethering::kConfirmed) |
| 1150 | return metrics::ConnectionType::kTetheredWifi; |
| 1151 | else |
| 1152 | return metrics::ConnectionType::kWifi; |
| 1153 | |
| 1154 | case kNetWimax: |
| 1155 | return metrics::ConnectionType::kWimax; |
| 1156 | |
| 1157 | case kNetBluetooth: |
| 1158 | return metrics::ConnectionType::kBluetooth; |
| 1159 | |
| 1160 | case kNetCellular: |
| 1161 | return metrics::ConnectionType::kCellular; |
| 1162 | } |
| 1163 | |
| 1164 | LOG(ERROR) << "Unexpected network connection type: type=" << type |
| 1165 | << ", tethering=" << static_cast<int>(tethering); |
| 1166 | |
| 1167 | return metrics::ConnectionType::kUnknown; |
| 1168 | } |
| 1169 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1170 | // Returns a printable version of the various flags denoted in the higher order |
| 1171 | // bits of the given code. Returns an empty string if none of those bits are |
| 1172 | // set. |
| 1173 | string GetFlagNames(uint32_t code) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1174 | uint32_t flags = code & kErrorCodeSpecialFlags; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1175 | string flag_names; |
| 1176 | string separator = ""; |
| 1177 | for(size_t i = 0; i < sizeof(flags) * 8; i++) { |
| 1178 | uint32_t flag = flags & (1 << i); |
| 1179 | if (flag) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1180 | flag_names += separator + CodeToString(static_cast<ErrorCode>(flag)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1181 | separator = ", "; |
| 1182 | } |
| 1183 | } |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1184 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1185 | return flag_names; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1188 | void SendErrorCodeToUma(SystemState* system_state, ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1189 | if (!system_state) |
| 1190 | return; |
| 1191 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1192 | ErrorCode uma_error_code = GetBaseErrorCode(code); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1193 | |
| 1194 | // If the code doesn't have flags computed already, compute them now based on |
| 1195 | // the state of the current update attempt. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1196 | uint32_t flags = code & kErrorCodeSpecialFlags; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1197 | if (!flags) |
| 1198 | flags = system_state->update_attempter()->GetErrorCodeFlags(); |
| 1199 | |
| 1200 | // Determine the UMA bucket depending on the flags. But, ignore the resumed |
| 1201 | // flag, as it's perfectly normal for production devices to resume their |
| 1202 | // downloads and so we want to record those cases also in NormalErrorCodes |
| 1203 | // bucket. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1204 | string metric = (flags & ~kErrorCodeResumedFlag) ? |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1205 | "Installer.DevModeErrorCodes" : "Installer.NormalErrorCodes"; |
| 1206 | |
| 1207 | LOG(INFO) << "Sending error code " << uma_error_code |
| 1208 | << " (" << CodeToString(uma_error_code) << ")" |
| 1209 | << " to UMA metric: " << metric |
| 1210 | << ". Flags = " << (flags ? GetFlagNames(flags) : "None"); |
| 1211 | |
| 1212 | system_state->metrics_lib()->SendEnumToUMA(metric, |
| 1213 | uma_error_code, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1214 | kErrorCodeUmaReportedMax); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1217 | string CodeToString(ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1218 | // If the given code has both parts (i.e. the error code part and the flags |
| 1219 | // part) then strip off the flags part since the switch statement below |
| 1220 | // has case statements only for the base error code or a single flag but |
| 1221 | // doesn't support any combinations of those. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1222 | if ((code & kErrorCodeSpecialFlags) && (code & ~kErrorCodeSpecialFlags)) |
| 1223 | code = static_cast<ErrorCode>(code & ~kErrorCodeSpecialFlags); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1224 | switch (code) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1225 | case kErrorCodeSuccess: return "kErrorCodeSuccess"; |
| 1226 | case kErrorCodeError: return "kErrorCodeError"; |
| 1227 | case kErrorCodeOmahaRequestError: return "kErrorCodeOmahaRequestError"; |
| 1228 | case kErrorCodeOmahaResponseHandlerError: |
| 1229 | return "kErrorCodeOmahaResponseHandlerError"; |
| 1230 | case kErrorCodeFilesystemCopierError: |
| 1231 | return "kErrorCodeFilesystemCopierError"; |
| 1232 | case kErrorCodePostinstallRunnerError: |
| 1233 | return "kErrorCodePostinstallRunnerError"; |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1234 | case kErrorCodePayloadMismatchedType: |
| 1235 | return "kErrorCodePayloadMismatchedType"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1236 | case kErrorCodeInstallDeviceOpenError: |
| 1237 | return "kErrorCodeInstallDeviceOpenError"; |
| 1238 | case kErrorCodeKernelDeviceOpenError: |
| 1239 | return "kErrorCodeKernelDeviceOpenError"; |
| 1240 | case kErrorCodeDownloadTransferError: |
| 1241 | return "kErrorCodeDownloadTransferError"; |
| 1242 | case kErrorCodePayloadHashMismatchError: |
| 1243 | return "kErrorCodePayloadHashMismatchError"; |
| 1244 | case kErrorCodePayloadSizeMismatchError: |
| 1245 | return "kErrorCodePayloadSizeMismatchError"; |
| 1246 | case kErrorCodeDownloadPayloadVerificationError: |
| 1247 | return "kErrorCodeDownloadPayloadVerificationError"; |
| 1248 | case kErrorCodeDownloadNewPartitionInfoError: |
| 1249 | return "kErrorCodeDownloadNewPartitionInfoError"; |
| 1250 | case kErrorCodeDownloadWriteError: |
| 1251 | return "kErrorCodeDownloadWriteError"; |
| 1252 | case kErrorCodeNewRootfsVerificationError: |
| 1253 | return "kErrorCodeNewRootfsVerificationError"; |
| 1254 | case kErrorCodeNewKernelVerificationError: |
| 1255 | return "kErrorCodeNewKernelVerificationError"; |
| 1256 | case kErrorCodeSignedDeltaPayloadExpectedError: |
| 1257 | return "kErrorCodeSignedDeltaPayloadExpectedError"; |
| 1258 | case kErrorCodeDownloadPayloadPubKeyVerificationError: |
| 1259 | return "kErrorCodeDownloadPayloadPubKeyVerificationError"; |
| 1260 | case kErrorCodePostinstallBootedFromFirmwareB: |
| 1261 | return "kErrorCodePostinstallBootedFromFirmwareB"; |
| 1262 | case kErrorCodeDownloadStateInitializationError: |
| 1263 | return "kErrorCodeDownloadStateInitializationError"; |
| 1264 | case kErrorCodeDownloadInvalidMetadataMagicString: |
| 1265 | return "kErrorCodeDownloadInvalidMetadataMagicString"; |
| 1266 | case kErrorCodeDownloadSignatureMissingInManifest: |
| 1267 | return "kErrorCodeDownloadSignatureMissingInManifest"; |
| 1268 | case kErrorCodeDownloadManifestParseError: |
| 1269 | return "kErrorCodeDownloadManifestParseError"; |
| 1270 | case kErrorCodeDownloadMetadataSignatureError: |
| 1271 | return "kErrorCodeDownloadMetadataSignatureError"; |
| 1272 | case kErrorCodeDownloadMetadataSignatureVerificationError: |
| 1273 | return "kErrorCodeDownloadMetadataSignatureVerificationError"; |
| 1274 | case kErrorCodeDownloadMetadataSignatureMismatch: |
| 1275 | return "kErrorCodeDownloadMetadataSignatureMismatch"; |
| 1276 | case kErrorCodeDownloadOperationHashVerificationError: |
| 1277 | return "kErrorCodeDownloadOperationHashVerificationError"; |
| 1278 | case kErrorCodeDownloadOperationExecutionError: |
| 1279 | return "kErrorCodeDownloadOperationExecutionError"; |
| 1280 | case kErrorCodeDownloadOperationHashMismatch: |
| 1281 | return "kErrorCodeDownloadOperationHashMismatch"; |
| 1282 | case kErrorCodeOmahaRequestEmptyResponseError: |
| 1283 | return "kErrorCodeOmahaRequestEmptyResponseError"; |
| 1284 | case kErrorCodeOmahaRequestXMLParseError: |
| 1285 | return "kErrorCodeOmahaRequestXMLParseError"; |
| 1286 | case kErrorCodeDownloadInvalidMetadataSize: |
| 1287 | return "kErrorCodeDownloadInvalidMetadataSize"; |
| 1288 | case kErrorCodeDownloadInvalidMetadataSignature: |
| 1289 | return "kErrorCodeDownloadInvalidMetadataSignature"; |
| 1290 | case kErrorCodeOmahaResponseInvalid: |
| 1291 | return "kErrorCodeOmahaResponseInvalid"; |
| 1292 | case kErrorCodeOmahaUpdateIgnoredPerPolicy: |
| 1293 | return "kErrorCodeOmahaUpdateIgnoredPerPolicy"; |
| 1294 | case kErrorCodeOmahaUpdateDeferredPerPolicy: |
| 1295 | return "kErrorCodeOmahaUpdateDeferredPerPolicy"; |
| 1296 | case kErrorCodeOmahaErrorInHTTPResponse: |
| 1297 | return "kErrorCodeOmahaErrorInHTTPResponse"; |
| 1298 | case kErrorCodeDownloadOperationHashMissingError: |
| 1299 | return "kErrorCodeDownloadOperationHashMissingError"; |
| 1300 | case kErrorCodeDownloadMetadataSignatureMissingError: |
| 1301 | return "kErrorCodeDownloadMetadataSignatureMissingError"; |
| 1302 | case kErrorCodeOmahaUpdateDeferredForBackoff: |
| 1303 | return "kErrorCodeOmahaUpdateDeferredForBackoff"; |
| 1304 | case kErrorCodePostinstallPowerwashError: |
| 1305 | return "kErrorCodePostinstallPowerwashError"; |
| 1306 | case kErrorCodeUpdateCanceledByChannelChange: |
| 1307 | return "kErrorCodeUpdateCanceledByChannelChange"; |
| 1308 | case kErrorCodeUmaReportedMax: |
| 1309 | return "kErrorCodeUmaReportedMax"; |
| 1310 | case kErrorCodeOmahaRequestHTTPResponseBase: |
| 1311 | return "kErrorCodeOmahaRequestHTTPResponseBase"; |
| 1312 | case kErrorCodeResumedFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1313 | return "Resumed"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1314 | case kErrorCodeDevModeFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1315 | return "DevMode"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1316 | case kErrorCodeTestImageFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1317 | return "TestImage"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1318 | case kErrorCodeTestOmahaUrlFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1319 | return "TestOmahaUrl"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1320 | case kErrorCodeSpecialFlags: |
| 1321 | return "kErrorCodeSpecialFlags"; |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 1322 | case kErrorCodePostinstallFirmwareRONotUpdatable: |
| 1323 | return "kErrorCodePostinstallFirmwareRONotUpdatable"; |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 1324 | case kErrorCodeUnsupportedMajorPayloadVersion: |
| 1325 | return "kErrorCodeUnsupportedMajorPayloadVersion"; |
| 1326 | case kErrorCodeUnsupportedMinorPayloadVersion: |
| 1327 | return "kErrorCodeUnsupportedMinorPayloadVersion"; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1328 | // Don't add a default case to let the compiler warn about newly added |
| 1329 | // error codes which should be added here. |
| 1330 | } |
| 1331 | |
| 1332 | return "Unknown error: " + base::UintToString(static_cast<unsigned>(code)); |
| 1333 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1334 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1335 | bool CreatePowerwashMarkerFile(const char* file_path) { |
| 1336 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
| 1337 | bool result = utils::WriteFile(marker_file, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1338 | kPowerwashCommand, |
| 1339 | strlen(kPowerwashCommand)); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1340 | if (result) { |
| 1341 | LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot"; |
| 1342 | } else { |
| 1343 | PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file; |
| 1344 | } |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1345 | |
| 1346 | return result; |
| 1347 | } |
| 1348 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1349 | bool DeletePowerwashMarkerFile(const char* file_path) { |
| 1350 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1351 | const base::FilePath kPowerwashMarkerPath(marker_file); |
| 1352 | bool result = base::DeleteFile(kPowerwashMarkerPath, false); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1353 | |
| 1354 | if (result) |
| 1355 | LOG(INFO) << "Successfully deleted the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1356 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1357 | else |
| 1358 | PLOG(ERROR) << "Could not delete the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1359 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1360 | |
| 1361 | return result; |
| 1362 | } |
| 1363 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 1364 | bool GetInstallDev(const std::string& boot_dev, std::string* install_dev) { |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 1365 | std::string disk_name; |
| 1366 | int partition_num; |
| 1367 | if (!SplitPartitionName(boot_dev, &disk_name, &partition_num)) |
| 1368 | return false; |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame] | 1369 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 1370 | // Right now, we just switch '3' and '5' partition numbers. |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 1371 | if (partition_num == 3) { |
| 1372 | partition_num = 5; |
| 1373 | } else if (partition_num == 5) { |
| 1374 | partition_num = 3; |
| 1375 | } else { |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
| 1379 | if (install_dev) |
| 1380 | *install_dev = MakePartitionName(disk_name, partition_num); |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame] | 1381 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 1382 | return true; |
| 1383 | } |
| 1384 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1385 | Time TimeFromStructTimespec(struct timespec *ts) { |
| 1386 | int64 us = static_cast<int64>(ts->tv_sec) * Time::kMicrosecondsPerSecond + |
| 1387 | static_cast<int64>(ts->tv_nsec) / Time::kNanosecondsPerMicrosecond; |
| 1388 | return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us); |
| 1389 | } |
| 1390 | |
| 1391 | gchar** StringVectorToGStrv(const vector<string> &vector) { |
| 1392 | GPtrArray *p = g_ptr_array_new(); |
| 1393 | for (std::vector<string>::const_iterator i = vector.begin(); |
| 1394 | i != vector.end(); ++i) { |
| 1395 | g_ptr_array_add(p, g_strdup(i->c_str())); |
| 1396 | } |
| 1397 | g_ptr_array_add(p, NULL); |
| 1398 | return reinterpret_cast<gchar**>(g_ptr_array_free(p, FALSE)); |
| 1399 | } |
| 1400 | |
| 1401 | string StringVectorToString(const vector<string> &vector) { |
| 1402 | string str = "["; |
| 1403 | for (std::vector<string>::const_iterator i = vector.begin(); |
| 1404 | i != vector.end(); ++i) { |
| 1405 | if (i != vector.begin()) |
| 1406 | str += ", "; |
| 1407 | str += '"'; |
| 1408 | str += *i; |
| 1409 | str += '"'; |
| 1410 | } |
| 1411 | str += "]"; |
| 1412 | return str; |
| 1413 | } |
| 1414 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1415 | string CalculateP2PFileId(const string& payload_hash, size_t payload_size) { |
| 1416 | string encoded_hash; |
| 1417 | OmahaHashCalculator::Base64Encode(payload_hash.c_str(), |
| 1418 | payload_hash.size(), |
| 1419 | &encoded_hash); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1420 | return base::StringPrintf("cros_update_size_%zu_hash_%s", |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1421 | payload_size, |
| 1422 | encoded_hash.c_str()); |
| 1423 | } |
| 1424 | |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 1425 | bool IsXAttrSupported(const base::FilePath& dir_path) { |
| 1426 | char *path = strdup(dir_path.Append("xattr_test_XXXXXX").value().c_str()); |
| 1427 | |
| 1428 | int fd = mkstemp(path); |
| 1429 | if (fd == -1) { |
| 1430 | PLOG(ERROR) << "Error creating temporary file in " << dir_path.value(); |
| 1431 | free(path); |
| 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | if (unlink(path) != 0) { |
| 1436 | PLOG(ERROR) << "Error unlinking temporary file " << path; |
| 1437 | close(fd); |
| 1438 | free(path); |
| 1439 | return false; |
| 1440 | } |
| 1441 | |
| 1442 | int xattr_res = fsetxattr(fd, "user.xattr-test", "value", strlen("value"), 0); |
| 1443 | if (xattr_res != 0) { |
| 1444 | if (errno == ENOTSUP) { |
| 1445 | // Leave it to call-sites to warn about non-support. |
| 1446 | } else { |
| 1447 | PLOG(ERROR) << "Error setting xattr on " << path; |
| 1448 | } |
| 1449 | } |
| 1450 | close(fd); |
| 1451 | free(path); |
| 1452 | return xattr_res == 0; |
| 1453 | } |
| 1454 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1455 | bool DecodeAndStoreBase64String(const std::string& base64_encoded, |
| 1456 | base::FilePath *out_path) { |
| 1457 | vector<char> contents; |
| 1458 | |
| 1459 | out_path->clear(); |
| 1460 | |
| 1461 | if (base64_encoded.size() == 0) { |
| 1462 | LOG(ERROR) << "Can't decode empty string."; |
| 1463 | return false; |
| 1464 | } |
| 1465 | |
| 1466 | if (!OmahaHashCalculator::Base64Decode(base64_encoded, &contents) || |
| 1467 | contents.size() == 0) { |
| 1468 | LOG(ERROR) << "Error decoding base64."; |
| 1469 | return false; |
| 1470 | } |
| 1471 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1472 | FILE *file = base::CreateAndOpenTemporaryFile(out_path); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1473 | if (file == NULL) { |
| 1474 | LOG(ERROR) << "Error creating temporary file."; |
| 1475 | return false; |
| 1476 | } |
| 1477 | |
| 1478 | if (fwrite(&contents[0], 1, contents.size(), file) != contents.size()) { |
| 1479 | PLOG(ERROR) << "Error writing to temporary file."; |
| 1480 | if (fclose(file) != 0) |
| 1481 | PLOG(ERROR) << "Error closing temporary file."; |
| 1482 | if (unlink(out_path->value().c_str()) != 0) |
| 1483 | PLOG(ERROR) << "Error unlinking temporary file."; |
| 1484 | out_path->clear(); |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
| 1488 | if (fclose(file) != 0) { |
| 1489 | PLOG(ERROR) << "Error closing temporary file."; |
| 1490 | out_path->clear(); |
| 1491 | return false; |
| 1492 | } |
| 1493 | |
| 1494 | return true; |
| 1495 | } |
| 1496 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1497 | bool ConvertToOmahaInstallDate(base::Time time, int *out_num_days) { |
| 1498 | time_t unix_time = time.ToTimeT(); |
| 1499 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". |
| 1500 | const time_t kOmahaEpoch = 1167638400; |
| 1501 | const int64_t kNumSecondsPerWeek = 7*24*3600; |
| 1502 | const int64_t kNumDaysPerWeek = 7; |
| 1503 | |
| 1504 | time_t omaha_time = unix_time - kOmahaEpoch; |
| 1505 | |
| 1506 | if (omaha_time < 0) |
| 1507 | return false; |
| 1508 | |
| 1509 | // Note, as per the comment in utils.h we are deliberately not |
| 1510 | // handling DST correctly. |
| 1511 | |
| 1512 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; |
| 1513 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; |
| 1514 | |
| 1515 | return true; |
| 1516 | } |
| 1517 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1518 | bool WallclockDurationHelper(SystemState* system_state, |
| 1519 | const std::string& state_variable_key, |
| 1520 | base::TimeDelta* out_duration) { |
| 1521 | bool ret = false; |
| 1522 | |
| 1523 | base::Time now = system_state->clock()->GetWallclockTime(); |
| 1524 | int64_t stored_value; |
| 1525 | if (system_state->prefs()->GetInt64(state_variable_key, &stored_value)) { |
| 1526 | base::Time stored_time = base::Time::FromInternalValue(stored_value); |
| 1527 | if (stored_time > now) { |
| 1528 | LOG(ERROR) << "Stored time-stamp used for " << state_variable_key |
| 1529 | << " is in the future."; |
| 1530 | } else { |
| 1531 | *out_duration = now - stored_time; |
| 1532 | ret = true; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | if (!system_state->prefs()->SetInt64(state_variable_key, |
| 1537 | now.ToInternalValue())) { |
| 1538 | LOG(ERROR) << "Error storing time-stamp in " << state_variable_key; |
| 1539 | } |
| 1540 | |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
| 1544 | bool MonotonicDurationHelper(SystemState* system_state, |
| 1545 | int64_t* storage, |
| 1546 | base::TimeDelta* out_duration) { |
| 1547 | bool ret = false; |
| 1548 | |
| 1549 | base::Time now = system_state->clock()->GetMonotonicTime(); |
| 1550 | if (*storage != 0) { |
| 1551 | base::Time stored_time = base::Time::FromInternalValue(*storage); |
| 1552 | *out_duration = now - stored_time; |
| 1553 | ret = true; |
| 1554 | } |
| 1555 | *storage = now.ToInternalValue(); |
| 1556 | |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1560 | } // namespace utils |
| 1561 | |
| 1562 | } // namespace chromeos_update_engine |