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