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