Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/utils.h" |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 18 | |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <dirent.h> |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 22 | #include <elf.h> |
Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 23 | #include <endian.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | #include <errno.h> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 25 | #include <ext2fs/ext2fs.h> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 26 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 30 | #include <sys/mount.h> |
| 31 | #include <sys/resource.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/wait.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 36 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 38 | #include <utility> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 39 | #include <vector> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 40 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 41 | #include <base/callback.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 42 | #include <base/files/file_path.h> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 43 | #include <base/files/file_util.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 44 | #include <base/files/scoped_file.h> |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 45 | #include <base/format_macros.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 46 | #include <base/location.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 47 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 48 | #include <base/posix/eintr_wrapper.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 49 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 50 | #include <base/strings/string_number_conversions.h> |
| 51 | #include <base/strings/string_split.h> |
| 52 | #include <base/strings/string_util.h> |
| 53 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 54 | #include <brillo/data_encoding.h> |
| 55 | #include <brillo/message_loops/message_loop.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 56 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 57 | #include "update_engine/common/clock_interface.h" |
| 58 | #include "update_engine/common/constants.h" |
| 59 | #include "update_engine/common/prefs_interface.h" |
| 60 | #include "update_engine/common/subprocess.h" |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 61 | #include "update_engine/omaha_request_params.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 62 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 63 | #include "update_engine/payload_consumer/file_writer.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 64 | #include "update_engine/update_attempter.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 66 | using base::Time; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 67 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | using std::min; |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 69 | using std::pair; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 70 | using std::string; |
| 71 | using std::vector; |
| 72 | |
| 73 | namespace chromeos_update_engine { |
| 74 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 75 | namespace { |
| 76 | |
| 77 | // The following constants control how UnmountFilesystem should retry if |
| 78 | // umount() fails with an errno EBUSY, i.e. retry 5 times over the course of |
| 79 | // one second. |
| 80 | const int kUnmountMaxNumOfRetries = 5; |
| 81 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 82 | |
| 83 | // Number of bytes to read from a file to attempt to detect its contents. Used |
| 84 | // in GetFileFormat. |
| 85 | const int kGetFileFormatMaxHeaderSize = 32; |
| 86 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 87 | // The path to the kernel's boot_id. |
| 88 | const char kBootIdPath[] = "/proc/sys/kernel/random/boot_id"; |
| 89 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 90 | // Return true if |disk_name| is an MTD or a UBI device. Note that this test is |
| 91 | // simply based on the name of the device. |
| 92 | bool IsMtdDeviceName(const string& disk_name) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 93 | return base::StartsWithASCII(disk_name, "/dev/ubi", true) || |
| 94 | base::StartsWithASCII(disk_name, "/dev/mtd", true); |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Return the device name for the corresponding partition on a NAND device. |
| 98 | // WARNING: This function returns device names that are not mountable. |
| 99 | string MakeNandPartitionName(int partition_num) { |
| 100 | switch (partition_num) { |
| 101 | case 2: |
| 102 | case 4: |
| 103 | case 6: { |
| 104 | return base::StringPrintf("/dev/mtd%d", partition_num); |
| 105 | } |
| 106 | default: { |
| 107 | return base::StringPrintf("/dev/ubi%d_0", partition_num); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Return the device name for the corresponding partition on a NAND device that |
| 113 | // may be mountable (but may not be writable). |
| 114 | string MakeNandPartitionNameForMount(int partition_num) { |
| 115 | switch (partition_num) { |
| 116 | case 2: |
| 117 | case 4: |
| 118 | case 6: { |
| 119 | return base::StringPrintf("/dev/mtd%d", partition_num); |
| 120 | } |
| 121 | case 3: |
| 122 | case 5: |
| 123 | case 7: { |
| 124 | return base::StringPrintf("/dev/ubiblock%d_0", partition_num); |
| 125 | } |
| 126 | default: { |
| 127 | return base::StringPrintf("/dev/ubi%d_0", partition_num); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 132 | // If |path| is absolute, or explicit relative to the current working directory, |
| 133 | // leaves it as is. Otherwise, uses the system's temp directory, as defined by |
| 134 | // base::GetTempDir() and prepends it to |path|. On success stores the full |
| 135 | // temporary path in |template_path| and returns true. |
| 136 | bool GetTempName(const string& path, base::FilePath* template_path) { |
| 137 | if (path[0] == '/' || base::StartsWithASCII(path, "./", true) || |
| 138 | base::StartsWithASCII(path, "../", true)) { |
| 139 | *template_path = base::FilePath(path); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | base::FilePath temp_dir; |
| 144 | TEST_AND_RETURN_FALSE(base::GetTempDir(&temp_dir)); |
| 145 | *template_path = temp_dir.Append(path); |
| 146 | return true; |
| 147 | } |
| 148 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 149 | } // namespace |
| 150 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 151 | namespace utils { |
| 152 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 153 | // Cgroup container is created in update-engine's upstart script located at |
| 154 | // /etc/init/update-engine.conf. |
| 155 | static const char kCGroupDir[] = "/sys/fs/cgroup/cpu/update-engine"; |
| 156 | |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 157 | string ParseECVersion(string input_line) { |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 158 | base::TrimWhitespaceASCII(input_line, base::TRIM_ALL, &input_line); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 159 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 160 | // 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] | 161 | // a vector of key value pairs. |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 162 | vector<pair<string, string>> kv_pairs; |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 163 | if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) { |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 164 | for (const pair<string, string>& kv_pair : kv_pairs) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 165 | // Finally match against the fw_verion which may have quotes. |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 166 | if (kv_pair.first == "fw_version") { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 167 | string output; |
| 168 | // Trim any quotes. |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 169 | base::TrimString(kv_pair.second, "\"", &output); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 170 | return output; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | LOG(ERROR) << "Unable to parse fwid from ec info."; |
| 175 | return ""; |
| 176 | } |
| 177 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 178 | 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] | 179 | DirectFileWriter writer; |
| 180 | TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, |
| 181 | O_WRONLY | O_CREAT | O_TRUNC, |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 182 | 0600)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 183 | ScopedFileWriterCloser closer(&writer); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 184 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(data, data_len)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 185 | return true; |
| 186 | } |
| 187 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 188 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 189 | const char* c_buf = static_cast<const char*>(buf); |
| 190 | ssize_t bytes_written = 0; |
| 191 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 192 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 193 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 194 | bytes_written += rc; |
| 195 | } |
| 196 | return true; |
| 197 | } |
| 198 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 199 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 200 | const char* c_buf = static_cast<const char*>(buf); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 201 | size_t bytes_written = 0; |
| 202 | int num_attempts = 0; |
| 203 | while (bytes_written < count) { |
| 204 | num_attempts++; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 205 | ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, |
| 206 | offset + bytes_written); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 207 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. |
| 208 | if (rc < 0) { |
| 209 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts |
| 210 | << " bytes_written=" << bytes_written |
| 211 | << " count=" << count << " offset=" << offset; |
| 212 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 213 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 214 | bytes_written += rc; |
| 215 | } |
| 216 | return true; |
| 217 | } |
| 218 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 219 | bool WriteAll(FileDescriptorPtr fd, const void* buf, size_t count) { |
| 220 | const char* c_buf = static_cast<const char*>(buf); |
| 221 | ssize_t bytes_written = 0; |
| 222 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 223 | ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written); |
| 224 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 225 | bytes_written += rc; |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | bool PWriteAll(FileDescriptorPtr fd, |
| 231 | const void* buf, |
| 232 | size_t count, |
| 233 | off_t offset) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 234 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 235 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 236 | return WriteAll(fd, buf, count); |
| 237 | } |
| 238 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 239 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 240 | ssize_t* out_bytes_read) { |
| 241 | char* c_buf = static_cast<char*>(buf); |
| 242 | ssize_t bytes_read = 0; |
| 243 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 244 | ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 245 | offset + bytes_read); |
| 246 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 247 | if (rc == 0) { |
| 248 | break; |
| 249 | } |
| 250 | bytes_read += rc; |
| 251 | } |
| 252 | *out_bytes_read = bytes_read; |
| 253 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 256 | bool PReadAll(FileDescriptorPtr fd, void* buf, size_t count, off_t offset, |
| 257 | ssize_t* out_bytes_read) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 258 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 259 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 260 | char* c_buf = static_cast<char*>(buf); |
| 261 | ssize_t bytes_read = 0; |
| 262 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 263 | ssize_t rc = fd->Read(c_buf + bytes_read, count - bytes_read); |
| 264 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 265 | if (rc == 0) { |
| 266 | break; |
| 267 | } |
| 268 | bytes_read += rc; |
| 269 | } |
| 270 | *out_bytes_read = bytes_read; |
| 271 | return true; |
| 272 | } |
| 273 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 274 | // Append |nbytes| of content from |buf| to the vector pointed to by either |
| 275 | // |vec_p| or |str_p|. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 276 | static void AppendBytes(const uint8_t* buf, size_t nbytes, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 277 | brillo::Blob* vec_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 278 | CHECK(buf); |
| 279 | CHECK(vec_p); |
| 280 | vec_p->insert(vec_p->end(), buf, buf + nbytes); |
| 281 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 282 | static void AppendBytes(const uint8_t* buf, size_t nbytes, |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 283 | string* str_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 284 | CHECK(buf); |
| 285 | CHECK(str_p); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 286 | str_p->append(buf, buf + nbytes); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Reads from an open file |fp|, appending the read content to the container |
| 290 | // 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] | 291 | // file's content, false otherwise. If |size| is not -1, reads up to |size| |
| 292 | // bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 293 | template <class T> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 294 | static bool Read(FILE* fp, off_t size, T* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 295 | CHECK(fp); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 296 | CHECK(size == -1 || size >= 0); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 297 | uint8_t buf[1024]; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 298 | while (size == -1 || size > 0) { |
| 299 | off_t bytes_to_read = sizeof(buf); |
| 300 | if (size > 0 && bytes_to_read > size) { |
| 301 | bytes_to_read = size; |
| 302 | } |
| 303 | size_t nbytes = fread(buf, 1, bytes_to_read, fp); |
| 304 | if (!nbytes) { |
| 305 | break; |
| 306 | } |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 307 | AppendBytes(buf, nbytes, out_p); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 308 | if (size != -1) { |
| 309 | CHECK(size >= static_cast<off_t>(nbytes)); |
| 310 | size -= nbytes; |
| 311 | } |
| 312 | } |
| 313 | if (ferror(fp)) { |
| 314 | return false; |
| 315 | } |
| 316 | return size == 0 || feof(fp); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 319 | // Opens a file |path| for reading and appends its the contents to a container |
| 320 | // |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end |
| 321 | // 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] | 322 | template <class T> |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 323 | static bool ReadFileChunkAndAppend(const string& path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 324 | off_t offset, |
| 325 | off_t size, |
| 326 | T* out_p) { |
| 327 | CHECK_GE(offset, 0); |
| 328 | CHECK(size == -1 || size >= 0); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 329 | base::ScopedFILE fp(fopen(path.c_str(), "r")); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 330 | if (!fp.get()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 331 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 332 | if (offset) { |
| 333 | // Return success without appending any data if a chunk beyond the end of |
| 334 | // the file is requested. |
| 335 | if (offset >= FileSize(path)) { |
| 336 | return true; |
| 337 | } |
| 338 | TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0); |
| 339 | } |
| 340 | return Read(fp.get(), size, out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 343 | // TODO(deymo): This is only used in unittest, but requires the private |
| 344 | // Read<string>() defined here. Expose Read<string>() or move to base/ version. |
| 345 | bool ReadPipe(const string& cmd, string* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 346 | FILE* fp = popen(cmd.c_str(), "r"); |
| 347 | if (!fp) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 348 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 349 | bool success = Read(fp, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 350 | return (success && pclose(fp) >= 0); |
| 351 | } |
| 352 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 353 | bool ReadFile(const string& path, brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 354 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 357 | bool ReadFile(const string& path, string* out_p) { |
| 358 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 361 | bool ReadFileChunk(const string& path, off_t offset, off_t size, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 362 | brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 363 | return ReadFileChunkAndAppend(path, offset, size, out_p); |
| 364 | } |
| 365 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 366 | off_t BlockDevSize(int fd) { |
| 367 | uint64_t dev_size; |
| 368 | int rc = ioctl(fd, BLKGETSIZE64, &dev_size); |
| 369 | if (rc == -1) { |
| 370 | dev_size = -1; |
| 371 | PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd; |
| 372 | } |
| 373 | return dev_size; |
| 374 | } |
| 375 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 376 | off_t FileSize(int fd) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 377 | struct stat stbuf; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 378 | int rc = fstat(fd, &stbuf); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 379 | CHECK_EQ(rc, 0); |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 380 | if (rc < 0) { |
| 381 | PLOG(ERROR) << "Error stat-ing " << fd; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 382 | return rc; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 383 | } |
| 384 | if (S_ISREG(stbuf.st_mode)) |
| 385 | return stbuf.st_size; |
| 386 | if (S_ISBLK(stbuf.st_mode)) |
| 387 | return BlockDevSize(fd); |
| 388 | LOG(ERROR) << "Couldn't determine the type of " << fd; |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | off_t FileSize(const string& path) { |
| 393 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 394 | if (fd == -1) { |
| 395 | PLOG(ERROR) << "Error opening " << path; |
| 396 | return fd; |
| 397 | } |
| 398 | off_t size = FileSize(fd); |
| 399 | if (size == -1) |
| 400 | PLOG(ERROR) << "Error getting file size of " << path; |
| 401 | close(fd); |
| 402 | return size; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 405 | void HexDumpArray(const uint8_t* const arr, const size_t length) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 406 | LOG(INFO) << "Logging array of length: " << length; |
| 407 | const unsigned int bytes_per_line = 16; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 408 | for (uint32_t i = 0; i < length; i += bytes_per_line) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 409 | const unsigned int bytes_remaining = length - i; |
| 410 | const unsigned int bytes_per_this_line = min(bytes_per_line, |
| 411 | bytes_remaining); |
| 412 | char header[100]; |
| 413 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); |
| 414 | TEST_AND_RETURN(r == 13); |
| 415 | string line = header; |
| 416 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { |
| 417 | char buf[20]; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 418 | uint8_t c = arr[i + j]; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 419 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); |
| 420 | TEST_AND_RETURN(r == 3); |
| 421 | line += buf; |
| 422 | } |
| 423 | LOG(INFO) << line; |
| 424 | } |
| 425 | } |
| 426 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 427 | bool SplitPartitionName(const string& partition_name, |
| 428 | string* out_disk_name, |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 429 | int* out_partition_num) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 430 | if (!base::StartsWithASCII(partition_name, "/dev/", true)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 431 | LOG(ERROR) << "Invalid partition device name: " << partition_name; |
| 432 | return false; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 433 | } |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 434 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 435 | size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789"); |
| 436 | if (last_nondigit_pos == string::npos || |
| 437 | (last_nondigit_pos + 1) == partition_name.size()) { |
| 438 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 442 | size_t partition_name_len = string::npos; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 443 | if (partition_name[last_nondigit_pos] == '_') { |
| 444 | // NAND block devices have weird naming which could be something |
| 445 | // like "/dev/ubiblock2_0". We discard "_0" in such a case. |
| 446 | size_t prev_nondigit_pos = |
| 447 | partition_name.find_last_not_of("0123456789", last_nondigit_pos - 1); |
| 448 | if (prev_nondigit_pos == string::npos || |
| 449 | (prev_nondigit_pos + 1) == last_nondigit_pos) { |
| 450 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | partition_name_len = last_nondigit_pos - prev_nondigit_pos; |
| 455 | last_nondigit_pos = prev_nondigit_pos; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | if (out_disk_name) { |
| 459 | // Special case for MMC devices which have the following naming scheme: |
| 460 | // mmcblk0p2 |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 461 | size_t disk_name_len = last_nondigit_pos; |
| 462 | if (partition_name[last_nondigit_pos] != 'p' || |
| 463 | last_nondigit_pos == 0 || |
| 464 | !isdigit(partition_name[last_nondigit_pos - 1])) { |
| 465 | disk_name_len++; |
| 466 | } |
| 467 | *out_disk_name = partition_name.substr(0, disk_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | if (out_partition_num) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 471 | string partition_str = partition_name.substr(last_nondigit_pos + 1, |
| 472 | partition_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 473 | *out_partition_num = atoi(partition_str.c_str()); |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 478 | string MakePartitionName(const string& disk_name, int partition_num) { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 479 | if (partition_num < 1) { |
| 480 | LOG(ERROR) << "Invalid partition number: " << partition_num; |
| 481 | return string(); |
| 482 | } |
| 483 | |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 484 | if (!base::StartsWithASCII(disk_name, "/dev/", true)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 485 | LOG(ERROR) << "Invalid disk name: " << disk_name; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 486 | return string(); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 489 | if (IsMtdDeviceName(disk_name)) { |
| 490 | // Special case for UBI block devices. |
| 491 | // 1. ubiblock is not writable, we need to use plain "ubi". |
| 492 | // 2. There is a "_0" suffix. |
| 493 | return MakeNandPartitionName(partition_num); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 496 | string partition_name = disk_name; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 497 | if (isdigit(partition_name.back())) { |
| 498 | // Special case for devices with names ending with a digit. |
| 499 | // Add "p" to separate the disk name from partition number, |
| 500 | // e.g. "/dev/loop0p2" |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 501 | partition_name += 'p'; |
| 502 | } |
| 503 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 504 | partition_name += std::to_string(partition_num); |
| 505 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 506 | return partition_name; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 509 | string MakePartitionNameForMount(const string& part_name) { |
| 510 | if (IsMtdDeviceName(part_name)) { |
| 511 | int partition_num; |
| 512 | if (!SplitPartitionName(part_name, nullptr, &partition_num)) { |
| 513 | return ""; |
| 514 | } |
| 515 | return MakeNandPartitionNameForMount(partition_num); |
| 516 | } |
| 517 | return part_name; |
| 518 | } |
| 519 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 520 | string ErrnoNumberAsString(int err) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 521 | char buf[100]; |
| 522 | buf[0] = '\0'; |
| 523 | return strerror_r(err, buf, sizeof(buf)); |
| 524 | } |
| 525 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 526 | bool FileExists(const char* path) { |
| 527 | struct stat stbuf; |
| 528 | return 0 == lstat(path, &stbuf); |
| 529 | } |
| 530 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 531 | bool IsSymlink(const char* path) { |
| 532 | struct stat stbuf; |
| 533 | return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0; |
| 534 | } |
| 535 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 536 | bool TryAttachingUbiVolume(int volume_num, int timeout) { |
| 537 | const string volume_path = base::StringPrintf("/dev/ubi%d_0", volume_num); |
| 538 | if (FileExists(volume_path.c_str())) { |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | int exit_code; |
| 543 | vector<string> cmd = { |
| 544 | "ubiattach", |
| 545 | "-m", |
| 546 | base::StringPrintf("%d", volume_num), |
| 547 | "-d", |
| 548 | base::StringPrintf("%d", volume_num) |
| 549 | }; |
| 550 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); |
| 551 | TEST_AND_RETURN_FALSE(exit_code == 0); |
| 552 | |
| 553 | cmd = { |
| 554 | "ubiblock", |
| 555 | "--create", |
| 556 | volume_path |
| 557 | }; |
| 558 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); |
| 559 | TEST_AND_RETURN_FALSE(exit_code == 0); |
| 560 | |
| 561 | while (timeout > 0 && !FileExists(volume_path.c_str())) { |
| 562 | sleep(1); |
| 563 | timeout--; |
| 564 | } |
| 565 | |
| 566 | return FileExists(volume_path.c_str()); |
| 567 | } |
| 568 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 569 | bool MakeTempFile(const string& base_filename_template, |
| 570 | string* filename, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 571 | int* fd) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 572 | base::FilePath filename_template; |
| 573 | TEST_AND_RETURN_FALSE( |
| 574 | GetTempName(base_filename_template, &filename_template)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 575 | DCHECK(filename || fd); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 576 | vector<char> buf(filename_template.value().size() + 1); |
| 577 | memcpy(buf.data(), filename_template.value().data(), |
| 578 | filename_template.value().size()); |
| 579 | buf[filename_template.value().size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 580 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 581 | int mkstemp_fd = mkstemp(buf.data()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 582 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 583 | if (filename) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 584 | *filename = buf.data(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 585 | } |
| 586 | if (fd) { |
| 587 | *fd = mkstemp_fd; |
| 588 | } else { |
| 589 | close(mkstemp_fd); |
| 590 | } |
| 591 | return true; |
| 592 | } |
| 593 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 594 | bool MakeTempDirectory(const string& base_dirname_template, |
| 595 | string* dirname) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 596 | base::FilePath dirname_template; |
| 597 | TEST_AND_RETURN_FALSE(GetTempName(base_dirname_template, &dirname_template)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 598 | DCHECK(dirname); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 599 | vector<char> buf(dirname_template.value().size() + 1); |
| 600 | memcpy(buf.data(), dirname_template.value().data(), |
| 601 | dirname_template.value().size()); |
| 602 | buf[dirname_template.value().size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 603 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 604 | char* return_code = mkdtemp(buf.data()); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 605 | TEST_AND_RETURN_FALSE_ERRNO(return_code != nullptr); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 606 | *dirname = buf.data(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 607 | return true; |
| 608 | } |
| 609 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 610 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 611 | const string& mountpoint, |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 612 | unsigned long mountflags) { // NOLINT(runtime/int) |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 613 | // TODO(sosa): Remove "ext3" once crbug.com/208022 is resolved. |
Alex Deymo | 5fc5e10 | 2015-09-18 10:36:21 -0700 | [diff] [blame] | 614 | const vector<const char*> fstypes{"ext2", "ext3", "ext4", "squashfs"}; |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 615 | for (const char* fstype : fstypes) { |
| 616 | int rc = mount(device.c_str(), mountpoint.c_str(), fstype, mountflags, |
| 617 | nullptr); |
| 618 | if (rc == 0) |
| 619 | return true; |
| 620 | |
| 621 | PLOG(WARNING) << "Unable to mount destination device " << device |
| 622 | << " on " << mountpoint << " as " << fstype; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 623 | } |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 624 | LOG(ERROR) << "Unable to mount " << device << " with any supported type"; |
| 625 | return false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | bool UnmountFilesystem(const string& mountpoint) { |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 629 | for (int num_retries = 0; ; ++num_retries) { |
| 630 | if (umount(mountpoint.c_str()) == 0) |
| 631 | break; |
| 632 | |
| 633 | TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY && |
| 634 | num_retries < kUnmountMaxNumOfRetries); |
Alex Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 635 | usleep(kUnmountRetryIntervalInMicroseconds); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 636 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 637 | return true; |
| 638 | } |
| 639 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 640 | bool GetFilesystemSize(const string& device, |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 641 | int* out_block_count, |
| 642 | int* out_block_size) { |
| 643 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY)); |
Alex Deymo | e7141c6 | 2014-10-17 14:03:01 -0700 | [diff] [blame] | 644 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 645 | ScopedFdCloser fd_closer(&fd); |
| 646 | return GetFilesystemSizeFromFD(fd, out_block_count, out_block_size); |
| 647 | } |
| 648 | |
| 649 | bool GetFilesystemSizeFromFD(int fd, |
| 650 | int* out_block_count, |
| 651 | int* out_block_size) { |
| 652 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 653 | |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 654 | // Determine the filesystem size by directly reading the block count and |
| 655 | // block size information from the superblock. Supported FS are ext3 and |
| 656 | // squashfs. |
| 657 | |
| 658 | // Read from the fd only once and detect in memory. The first 2 KiB is enough |
| 659 | // to read the ext2 superblock (located at offset 1024) and the squashfs |
| 660 | // superblock (located at offset 0). |
| 661 | const ssize_t kBufferSize = 2048; |
| 662 | |
| 663 | uint8_t buffer[kBufferSize]; |
| 664 | if (HANDLE_EINTR(pread(fd, buffer, kBufferSize, 0)) != kBufferSize) { |
| 665 | PLOG(ERROR) << "Unable to read the file system header:"; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 666 | return false; |
| 667 | } |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 668 | |
| 669 | if (GetSquashfs4Size(buffer, kBufferSize, out_block_count, out_block_size)) |
| 670 | return true; |
| 671 | if (GetExt3Size(buffer, kBufferSize, out_block_count, out_block_size)) |
| 672 | return true; |
| 673 | |
| 674 | LOG(ERROR) << "Unable to determine file system type."; |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | bool GetExt3Size(const uint8_t* buffer, size_t buffer_size, |
| 679 | int* out_block_count, |
| 680 | int* out_block_size) { |
| 681 | // See include/linux/ext2_fs.h for more details on the structure. We obtain |
| 682 | // ext2 constants from ext2fs/ext2fs.h header but we don't link with the |
| 683 | // library. |
| 684 | if (buffer_size < SUPERBLOCK_OFFSET + SUPERBLOCK_SIZE) |
| 685 | return false; |
| 686 | |
| 687 | const uint8_t* superblock = buffer + SUPERBLOCK_OFFSET; |
| 688 | |
| 689 | // ext3_fs.h: ext3_super_block.s_blocks_count |
| 690 | uint32_t block_count = |
| 691 | *reinterpret_cast<const uint32_t*>(superblock + 1 * sizeof(int32_t)); |
| 692 | |
| 693 | // ext3_fs.h: ext3_super_block.s_log_block_size |
| 694 | uint32_t log_block_size = |
| 695 | *reinterpret_cast<const uint32_t*>(superblock + 6 * sizeof(int32_t)); |
| 696 | |
| 697 | // ext3_fs.h: ext3_super_block.s_magic |
| 698 | uint16_t magic = |
| 699 | *reinterpret_cast<const uint16_t*>(superblock + 14 * sizeof(int32_t)); |
| 700 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 701 | block_count = le32toh(block_count); |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 702 | log_block_size = le32toh(log_block_size) + EXT2_MIN_BLOCK_LOG_SIZE; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 703 | magic = le16toh(magic); |
| 704 | |
| 705 | // Sanity check the parameters. |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 706 | TEST_AND_RETURN_FALSE(magic == EXT2_SUPER_MAGIC); |
| 707 | TEST_AND_RETURN_FALSE(log_block_size >= EXT2_MIN_BLOCK_LOG_SIZE && |
| 708 | log_block_size <= EXT2_MAX_BLOCK_LOG_SIZE); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 709 | TEST_AND_RETURN_FALSE(block_count > 0); |
| 710 | |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 711 | if (out_block_count) |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 712 | *out_block_count = block_count; |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 713 | if (out_block_size) |
| 714 | *out_block_size = 1 << log_block_size; |
| 715 | return true; |
| 716 | } |
| 717 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 718 | bool GetSquashfs4Size(const uint8_t* buffer, size_t buffer_size, |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 719 | int* out_block_count, |
| 720 | int* out_block_size) { |
| 721 | // See fs/squashfs/squashfs_fs.h for format details. We only support |
| 722 | // Squashfs 4.x little endian. |
| 723 | |
| 724 | // sizeof(struct squashfs_super_block) |
| 725 | const size_t kSquashfsSuperBlockSize = 96; |
| 726 | if (buffer_size < kSquashfsSuperBlockSize) |
| 727 | return false; |
| 728 | |
| 729 | // Check magic, squashfs_fs.h: SQUASHFS_MAGIC |
| 730 | if (memcmp(buffer, "hsqs", 4) != 0) |
| 731 | return false; // Only little endian is supported. |
| 732 | |
| 733 | // squashfs_fs.h: struct squashfs_super_block.s_major |
| 734 | uint16_t s_major = *reinterpret_cast<const uint16_t*>( |
| 735 | buffer + 5 * sizeof(uint32_t) + 4 * sizeof(uint16_t)); |
| 736 | |
| 737 | if (s_major != 4) { |
| 738 | LOG(ERROR) << "Found unsupported squashfs major version " << s_major; |
| 739 | return false; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 740 | } |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 741 | |
| 742 | // squashfs_fs.h: struct squashfs_super_block.bytes_used |
| 743 | uint64_t bytes_used = *reinterpret_cast<const int64_t*>( |
| 744 | buffer + 5 * sizeof(uint32_t) + 6 * sizeof(uint16_t) + sizeof(uint64_t)); |
| 745 | |
| 746 | const int block_size = 4096; |
| 747 | |
| 748 | // The squashfs' bytes_used doesn't need to be aligned with the block boundary |
| 749 | // so we round up to the nearest blocksize. |
| 750 | if (out_block_count) |
| 751 | *out_block_count = (bytes_used + block_size - 1) / block_size; |
| 752 | if (out_block_size) |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 753 | *out_block_size = block_size; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 754 | return true; |
| 755 | } |
| 756 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 757 | bool IsExtFilesystem(const string& device) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 758 | brillo::Blob header; |
Alex Deymo | de942f3 | 2015-03-13 11:57:15 -0700 | [diff] [blame] | 759 | // The first 2 KiB is enough to read the ext2 superblock (located at offset |
| 760 | // 1024). |
| 761 | if (!ReadFileChunk(device, 0, 2048, &header)) |
| 762 | return false; |
| 763 | return GetExt3Size(header.data(), header.size(), nullptr, nullptr); |
| 764 | } |
| 765 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 766 | bool IsSquashfsFilesystem(const string& device) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 767 | brillo::Blob header; |
Alex Deymo | de942f3 | 2015-03-13 11:57:15 -0700 | [diff] [blame] | 768 | // The first 96 is enough to read the squashfs superblock. |
| 769 | const ssize_t kSquashfsSuperBlockSize = 96; |
| 770 | if (!ReadFileChunk(device, 0, kSquashfsSuperBlockSize, &header)) |
| 771 | return false; |
| 772 | return GetSquashfs4Size(header.data(), header.size(), nullptr, nullptr); |
| 773 | } |
| 774 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 775 | // Tries to parse the header of an ELF file to obtain a human-readable |
| 776 | // description of it on the |output| string. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 777 | static bool GetFileFormatELF(const uint8_t* buffer, size_t size, |
| 778 | string* output) { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 779 | // 0x00: EI_MAG - ELF magic header, 4 bytes. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 780 | if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 781 | return false; |
| 782 | *output = "ELF"; |
| 783 | |
| 784 | // 0x04: EI_CLASS, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 785 | if (size < EI_CLASS + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 786 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 787 | switch (buffer[EI_CLASS]) { |
| 788 | case ELFCLASS32: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 789 | *output += " 32-bit"; |
| 790 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 791 | case ELFCLASS64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 792 | *output += " 64-bit"; |
| 793 | break; |
| 794 | default: |
| 795 | *output += " ?-bit"; |
| 796 | } |
| 797 | |
| 798 | // 0x05: EI_DATA, endianness, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 799 | if (size < EI_DATA + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 800 | return true; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 801 | uint8_t ei_data = buffer[EI_DATA]; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 802 | switch (ei_data) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 803 | case ELFDATA2LSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 804 | *output += " little-endian"; |
| 805 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 806 | case ELFDATA2MSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 807 | *output += " big-endian"; |
| 808 | break; |
| 809 | default: |
| 810 | *output += " ?-endian"; |
| 811 | // Don't parse anything after the 0x10 offset if endianness is unknown. |
| 812 | return true; |
| 813 | } |
| 814 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 815 | const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer); |
| 816 | // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12) |
| 817 | // and size is the same for both 32 and 64 bits. |
| 818 | if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine)) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 819 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 820 | uint16_t e_machine; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 821 | // Fix endianess regardless of the host endianess. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 822 | if (ei_data == ELFDATA2LSB) |
| 823 | e_machine = le16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 824 | else |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 825 | e_machine = be16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 826 | |
| 827 | switch (e_machine) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 828 | case EM_386: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 829 | *output += " x86"; |
| 830 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 831 | case EM_MIPS: |
| 832 | *output += " mips"; |
| 833 | break; |
| 834 | case EM_ARM: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 835 | *output += " arm"; |
| 836 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 837 | case EM_X86_64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 838 | *output += " x86-64"; |
| 839 | break; |
| 840 | default: |
| 841 | *output += " unknown-arch"; |
| 842 | } |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | string GetFileFormat(const string& path) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 847 | brillo::Blob buffer; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 848 | if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer)) |
| 849 | return "File not found."; |
| 850 | |
| 851 | string result; |
| 852 | if (GetFileFormatELF(buffer.data(), buffer.size(), &result)) |
| 853 | return result; |
| 854 | |
| 855 | return "data"; |
| 856 | } |
| 857 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 858 | namespace { |
| 859 | // Do the actual trigger. We do it as a main-loop callback to (try to) get a |
| 860 | // consistent stack trace. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 861 | void TriggerCrashReporterUpload() { |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 862 | pid_t pid = fork(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 863 | 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] | 864 | if (pid == 0) { |
| 865 | // We are the child. Crash. |
| 866 | abort(); // never returns |
| 867 | } |
| 868 | // We are the parent. Wait for child to terminate. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 869 | pid_t result = waitpid(pid, nullptr, 0); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 870 | LOG_IF(ERROR, result < 0) << "waitpid() failed"; |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 871 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 872 | } // namespace |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 873 | |
| 874 | void ScheduleCrashReporterUpload() { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 875 | brillo::MessageLoop::current()->PostTask( |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 876 | FROM_HERE, |
| 877 | base::Bind(&TriggerCrashReporterUpload)); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 878 | } |
| 879 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 880 | bool SetCpuShares(CpuShares shares) { |
| 881 | string string_shares = base::IntToString(static_cast<int>(shares)); |
| 882 | string cpu_shares_file = string(utils::kCGroupDir) + "/cpu.shares"; |
| 883 | LOG(INFO) << "Setting cgroup cpu shares to " << string_shares; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 884 | if (utils::WriteFile(cpu_shares_file.c_str(), string_shares.c_str(), |
| 885 | string_shares.size())) { |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 886 | return true; |
| 887 | } else { |
| 888 | LOG(ERROR) << "Failed to change cgroup cpu shares to "<< string_shares |
| 889 | << " using " << cpu_shares_file; |
| 890 | return false; |
| 891 | } |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 894 | int FuzzInt(int value, unsigned int range) { |
| 895 | int min = value - range / 2; |
| 896 | int max = value + range - range / 2; |
| 897 | return base::RandInt(min, max); |
| 898 | } |
| 899 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 900 | string FormatSecs(unsigned secs) { |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 901 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 904 | string FormatTimeDelta(TimeDelta delta) { |
David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 905 | string str; |
| 906 | |
| 907 | // Handle negative durations by prefixing with a minus. |
| 908 | if (delta.ToInternalValue() < 0) { |
| 909 | delta *= -1; |
| 910 | str = "-"; |
| 911 | } |
| 912 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 913 | // Canonicalize into days, hours, minutes, seconds and microseconds. |
| 914 | unsigned days = delta.InDays(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 915 | delta -= TimeDelta::FromDays(days); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 916 | unsigned hours = delta.InHours(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 917 | delta -= TimeDelta::FromHours(hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 918 | unsigned mins = delta.InMinutes(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 919 | delta -= TimeDelta::FromMinutes(mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 920 | unsigned secs = delta.InSeconds(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 921 | delta -= TimeDelta::FromSeconds(secs); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 922 | unsigned usecs = delta.InMicroseconds(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 923 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 924 | if (days) |
| 925 | base::StringAppendF(&str, "%ud", days); |
| 926 | if (days || hours) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 927 | base::StringAppendF(&str, "%uh", hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 928 | if (days || hours || mins) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 929 | base::StringAppendF(&str, "%um", mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 930 | base::StringAppendF(&str, "%u", secs); |
| 931 | if (usecs) { |
| 932 | int width = 6; |
| 933 | while ((usecs / 10) * 10 == usecs) { |
| 934 | usecs /= 10; |
| 935 | width--; |
| 936 | } |
| 937 | base::StringAppendF(&str, ".%0*u", width, usecs); |
| 938 | } |
| 939 | base::StringAppendF(&str, "s"); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 940 | return str; |
| 941 | } |
| 942 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 943 | string ToString(const Time utc_time) { |
| 944 | Time::Exploded exp_time; |
| 945 | utc_time.UTCExplode(&exp_time); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 946 | return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT", |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 947 | exp_time.month, |
| 948 | exp_time.day_of_month, |
| 949 | exp_time.year, |
| 950 | exp_time.hour, |
| 951 | exp_time.minute, |
| 952 | exp_time.second); |
| 953 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 954 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 955 | string ToString(bool b) { |
| 956 | return (b ? "true" : "false"); |
| 957 | } |
| 958 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 959 | string ToString(DownloadSource source) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 960 | switch (source) { |
| 961 | case kDownloadSourceHttpsServer: return "HttpsServer"; |
| 962 | case kDownloadSourceHttpServer: return "HttpServer"; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 963 | case kDownloadSourceHttpPeer: return "HttpPeer"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 964 | case kNumDownloadSources: return "Unknown"; |
| 965 | // Don't add a default case to let the compiler warn about newly added |
| 966 | // download sources which should be added here. |
| 967 | } |
| 968 | |
| 969 | return "Unknown"; |
| 970 | } |
| 971 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 972 | string ToString(PayloadType payload_type) { |
| 973 | switch (payload_type) { |
| 974 | case kPayloadTypeDelta: return "Delta"; |
| 975 | case kPayloadTypeFull: return "Full"; |
| 976 | case kPayloadTypeForcedFull: return "ForcedFull"; |
| 977 | case kNumPayloadTypes: return "Unknown"; |
| 978 | // Don't add a default case to let the compiler warn about newly added |
| 979 | // payload types which should be added here. |
| 980 | } |
| 981 | |
| 982 | return "Unknown"; |
| 983 | } |
| 984 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 985 | ErrorCode GetBaseErrorCode(ErrorCode code) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 986 | // Ignore the higher order bits in the code by applying the mask as |
| 987 | // we want the enumerations to be in the small contiguous range |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 988 | // with values less than ErrorCode::kUmaReportedMax. |
| 989 | ErrorCode base_code = static_cast<ErrorCode>( |
| 990 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 991 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 992 | // Make additional adjustments required for UMA and error classification. |
| 993 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix |
| 994 | // chromium-os:34369. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 995 | if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 996 | // 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] | 997 | // errors into this one bucket for UMA and error classification purposes. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 998 | LOG(INFO) << "Converting error code " << base_code |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 999 | << " to ErrorCode::kOmahaErrorInHTTPResponse"; |
| 1000 | base_code = ErrorCode::kOmahaErrorInHTTPResponse; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1003 | return base_code; |
| 1004 | } |
| 1005 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1006 | string CodeToString(ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1007 | // If the given code has both parts (i.e. the error code part and the flags |
| 1008 | // part) then strip off the flags part since the switch statement below |
| 1009 | // has case statements only for the base error code or a single flag but |
| 1010 | // doesn't support any combinations of those. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1011 | if ((static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags)) && |
| 1012 | (static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags))) |
| 1013 | code = static_cast<ErrorCode>( |
| 1014 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1015 | switch (code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1016 | case ErrorCode::kSuccess: return "ErrorCode::kSuccess"; |
| 1017 | case ErrorCode::kError: return "ErrorCode::kError"; |
| 1018 | case ErrorCode::kOmahaRequestError: return "ErrorCode::kOmahaRequestError"; |
| 1019 | case ErrorCode::kOmahaResponseHandlerError: |
| 1020 | return "ErrorCode::kOmahaResponseHandlerError"; |
| 1021 | case ErrorCode::kFilesystemCopierError: |
| 1022 | return "ErrorCode::kFilesystemCopierError"; |
| 1023 | case ErrorCode::kPostinstallRunnerError: |
| 1024 | return "ErrorCode::kPostinstallRunnerError"; |
| 1025 | case ErrorCode::kPayloadMismatchedType: |
| 1026 | return "ErrorCode::kPayloadMismatchedType"; |
| 1027 | case ErrorCode::kInstallDeviceOpenError: |
| 1028 | return "ErrorCode::kInstallDeviceOpenError"; |
| 1029 | case ErrorCode::kKernelDeviceOpenError: |
| 1030 | return "ErrorCode::kKernelDeviceOpenError"; |
| 1031 | case ErrorCode::kDownloadTransferError: |
| 1032 | return "ErrorCode::kDownloadTransferError"; |
| 1033 | case ErrorCode::kPayloadHashMismatchError: |
| 1034 | return "ErrorCode::kPayloadHashMismatchError"; |
| 1035 | case ErrorCode::kPayloadSizeMismatchError: |
| 1036 | return "ErrorCode::kPayloadSizeMismatchError"; |
| 1037 | case ErrorCode::kDownloadPayloadVerificationError: |
| 1038 | return "ErrorCode::kDownloadPayloadVerificationError"; |
| 1039 | case ErrorCode::kDownloadNewPartitionInfoError: |
| 1040 | return "ErrorCode::kDownloadNewPartitionInfoError"; |
| 1041 | case ErrorCode::kDownloadWriteError: |
| 1042 | return "ErrorCode::kDownloadWriteError"; |
| 1043 | case ErrorCode::kNewRootfsVerificationError: |
| 1044 | return "ErrorCode::kNewRootfsVerificationError"; |
| 1045 | case ErrorCode::kNewKernelVerificationError: |
| 1046 | return "ErrorCode::kNewKernelVerificationError"; |
| 1047 | case ErrorCode::kSignedDeltaPayloadExpectedError: |
| 1048 | return "ErrorCode::kSignedDeltaPayloadExpectedError"; |
| 1049 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: |
| 1050 | return "ErrorCode::kDownloadPayloadPubKeyVerificationError"; |
| 1051 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 1052 | return "ErrorCode::kPostinstallBootedFromFirmwareB"; |
| 1053 | case ErrorCode::kDownloadStateInitializationError: |
| 1054 | return "ErrorCode::kDownloadStateInitializationError"; |
| 1055 | case ErrorCode::kDownloadInvalidMetadataMagicString: |
| 1056 | return "ErrorCode::kDownloadInvalidMetadataMagicString"; |
| 1057 | case ErrorCode::kDownloadSignatureMissingInManifest: |
| 1058 | return "ErrorCode::kDownloadSignatureMissingInManifest"; |
| 1059 | case ErrorCode::kDownloadManifestParseError: |
| 1060 | return "ErrorCode::kDownloadManifestParseError"; |
| 1061 | case ErrorCode::kDownloadMetadataSignatureError: |
| 1062 | return "ErrorCode::kDownloadMetadataSignatureError"; |
| 1063 | case ErrorCode::kDownloadMetadataSignatureVerificationError: |
| 1064 | return "ErrorCode::kDownloadMetadataSignatureVerificationError"; |
| 1065 | case ErrorCode::kDownloadMetadataSignatureMismatch: |
| 1066 | return "ErrorCode::kDownloadMetadataSignatureMismatch"; |
| 1067 | case ErrorCode::kDownloadOperationHashVerificationError: |
| 1068 | return "ErrorCode::kDownloadOperationHashVerificationError"; |
| 1069 | case ErrorCode::kDownloadOperationExecutionError: |
| 1070 | return "ErrorCode::kDownloadOperationExecutionError"; |
| 1071 | case ErrorCode::kDownloadOperationHashMismatch: |
| 1072 | return "ErrorCode::kDownloadOperationHashMismatch"; |
| 1073 | case ErrorCode::kOmahaRequestEmptyResponseError: |
| 1074 | return "ErrorCode::kOmahaRequestEmptyResponseError"; |
| 1075 | case ErrorCode::kOmahaRequestXMLParseError: |
| 1076 | return "ErrorCode::kOmahaRequestXMLParseError"; |
| 1077 | case ErrorCode::kDownloadInvalidMetadataSize: |
| 1078 | return "ErrorCode::kDownloadInvalidMetadataSize"; |
| 1079 | case ErrorCode::kDownloadInvalidMetadataSignature: |
| 1080 | return "ErrorCode::kDownloadInvalidMetadataSignature"; |
| 1081 | case ErrorCode::kOmahaResponseInvalid: |
| 1082 | return "ErrorCode::kOmahaResponseInvalid"; |
| 1083 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 1084 | return "ErrorCode::kOmahaUpdateIgnoredPerPolicy"; |
| 1085 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 1086 | return "ErrorCode::kOmahaUpdateDeferredPerPolicy"; |
| 1087 | case ErrorCode::kOmahaErrorInHTTPResponse: |
| 1088 | return "ErrorCode::kOmahaErrorInHTTPResponse"; |
| 1089 | case ErrorCode::kDownloadOperationHashMissingError: |
| 1090 | return "ErrorCode::kDownloadOperationHashMissingError"; |
| 1091 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 1092 | return "ErrorCode::kDownloadMetadataSignatureMissingError"; |
| 1093 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 1094 | return "ErrorCode::kOmahaUpdateDeferredForBackoff"; |
| 1095 | case ErrorCode::kPostinstallPowerwashError: |
| 1096 | return "ErrorCode::kPostinstallPowerwashError"; |
| 1097 | case ErrorCode::kUpdateCanceledByChannelChange: |
| 1098 | return "ErrorCode::kUpdateCanceledByChannelChange"; |
| 1099 | case ErrorCode::kUmaReportedMax: |
| 1100 | return "ErrorCode::kUmaReportedMax"; |
| 1101 | case ErrorCode::kOmahaRequestHTTPResponseBase: |
| 1102 | return "ErrorCode::kOmahaRequestHTTPResponseBase"; |
| 1103 | case ErrorCode::kResumedFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1104 | return "Resumed"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1105 | case ErrorCode::kDevModeFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1106 | return "DevMode"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1107 | case ErrorCode::kTestImageFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1108 | return "TestImage"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1109 | case ErrorCode::kTestOmahaUrlFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1110 | return "TestOmahaUrl"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1111 | case ErrorCode::kSpecialFlags: |
| 1112 | return "ErrorCode::kSpecialFlags"; |
| 1113 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
| 1114 | return "ErrorCode::kPostinstallFirmwareRONotUpdatable"; |
| 1115 | case ErrorCode::kUnsupportedMajorPayloadVersion: |
| 1116 | return "ErrorCode::kUnsupportedMajorPayloadVersion"; |
| 1117 | case ErrorCode::kUnsupportedMinorPayloadVersion: |
| 1118 | return "ErrorCode::kUnsupportedMinorPayloadVersion"; |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1119 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
| 1120 | return "ErrorCode::kOmahaRequestXMLHasEntityDecl"; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1121 | case ErrorCode::kFilesystemVerifierError: |
| 1122 | return "ErrorCode::kFilesystemVerifierError"; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1123 | // Don't add a default case to let the compiler warn about newly added |
| 1124 | // error codes which should be added here. |
| 1125 | } |
| 1126 | |
| 1127 | return "Unknown error: " + base::UintToString(static_cast<unsigned>(code)); |
| 1128 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1129 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1130 | bool CreatePowerwashMarkerFile(const char* file_path) { |
| 1131 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
| 1132 | bool result = utils::WriteFile(marker_file, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1133 | kPowerwashCommand, |
| 1134 | strlen(kPowerwashCommand)); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1135 | if (result) { |
| 1136 | LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot"; |
| 1137 | } else { |
| 1138 | PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file; |
| 1139 | } |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1140 | |
| 1141 | return result; |
| 1142 | } |
| 1143 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1144 | bool DeletePowerwashMarkerFile(const char* file_path) { |
| 1145 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1146 | const base::FilePath kPowerwashMarkerPath(marker_file); |
| 1147 | bool result = base::DeleteFile(kPowerwashMarkerPath, false); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1148 | |
| 1149 | if (result) |
| 1150 | LOG(INFO) << "Successfully deleted the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1151 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1152 | else |
| 1153 | PLOG(ERROR) << "Could not delete the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1154 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1155 | |
| 1156 | return result; |
| 1157 | } |
| 1158 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1159 | Time TimeFromStructTimespec(struct timespec *ts) { |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1160 | int64_t us = static_cast<int64_t>(ts->tv_sec) * Time::kMicrosecondsPerSecond + |
| 1161 | static_cast<int64_t>(ts->tv_nsec) / Time::kNanosecondsPerMicrosecond; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1162 | return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us); |
| 1163 | } |
| 1164 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1165 | string StringVectorToString(const vector<string> &vec_str) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1166 | string str = "["; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1167 | for (vector<string>::const_iterator i = vec_str.begin(); |
| 1168 | i != vec_str.end(); ++i) { |
| 1169 | if (i != vec_str.begin()) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1170 | str += ", "; |
| 1171 | str += '"'; |
| 1172 | str += *i; |
| 1173 | str += '"'; |
| 1174 | } |
| 1175 | str += "]"; |
| 1176 | return str; |
| 1177 | } |
| 1178 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1179 | string CalculateP2PFileId(const string& payload_hash, size_t payload_size) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1180 | string encoded_hash = brillo::data_encoding::Base64Encode(payload_hash); |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 1181 | return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s", |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1182 | payload_size, |
| 1183 | encoded_hash.c_str()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1186 | bool DecodeAndStoreBase64String(const string& base64_encoded, |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1187 | base::FilePath *out_path) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1188 | brillo::Blob contents; |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1189 | |
| 1190 | out_path->clear(); |
| 1191 | |
| 1192 | if (base64_encoded.size() == 0) { |
| 1193 | LOG(ERROR) << "Can't decode empty string."; |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1197 | if (!brillo::data_encoding::Base64Decode(base64_encoded, &contents) || |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1198 | contents.size() == 0) { |
| 1199 | LOG(ERROR) << "Error decoding base64."; |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1203 | FILE *file = base::CreateAndOpenTemporaryFile(out_path); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1204 | if (file == nullptr) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1205 | LOG(ERROR) << "Error creating temporary file."; |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1209 | if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1210 | PLOG(ERROR) << "Error writing to temporary file."; |
| 1211 | if (fclose(file) != 0) |
| 1212 | PLOG(ERROR) << "Error closing temporary file."; |
| 1213 | if (unlink(out_path->value().c_str()) != 0) |
| 1214 | PLOG(ERROR) << "Error unlinking temporary file."; |
| 1215 | out_path->clear(); |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
| 1219 | if (fclose(file) != 0) { |
| 1220 | PLOG(ERROR) << "Error closing temporary file."; |
| 1221 | out_path->clear(); |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | return true; |
| 1226 | } |
| 1227 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1228 | bool ConvertToOmahaInstallDate(Time time, int *out_num_days) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1229 | time_t unix_time = time.ToTimeT(); |
| 1230 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". |
| 1231 | const time_t kOmahaEpoch = 1167638400; |
| 1232 | const int64_t kNumSecondsPerWeek = 7*24*3600; |
| 1233 | const int64_t kNumDaysPerWeek = 7; |
| 1234 | |
| 1235 | time_t omaha_time = unix_time - kOmahaEpoch; |
| 1236 | |
| 1237 | if (omaha_time < 0) |
| 1238 | return false; |
| 1239 | |
| 1240 | // Note, as per the comment in utils.h we are deliberately not |
| 1241 | // handling DST correctly. |
| 1242 | |
| 1243 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; |
| 1244 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; |
| 1245 | |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1249 | bool GetMinorVersion(const brillo::KeyValueStore& store, |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1250 | uint32_t* minor_version) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1251 | string result; |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1252 | if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) { |
Allie Wood | 425aa97 | 2015-02-17 14:47:17 -0800 | [diff] [blame] | 1253 | if (!base::StringToUint(result, minor_version)) { |
| 1254 | LOG(ERROR) << "StringToUint failed when parsing delta minor version."; |
| 1255 | return false; |
| 1256 | } |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 1257 | return true; |
| 1258 | } |
| 1259 | return false; |
| 1260 | } |
| 1261 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1262 | bool ReadExtents(const string& path, const vector<Extent>& extents, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1263 | brillo::Blob* out_data, ssize_t out_data_size, |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1264 | size_t block_size) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1265 | brillo::Blob data(out_data_size); |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1266 | ssize_t bytes_read = 0; |
| 1267 | int fd = open(path.c_str(), O_RDONLY); |
| 1268 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 1269 | ScopedFdCloser fd_closer(&fd); |
| 1270 | |
Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 1271 | for (const Extent& extent : extents) { |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1272 | ssize_t bytes_read_this_iteration = 0; |
| 1273 | ssize_t bytes = extent.num_blocks() * block_size; |
| 1274 | TEST_AND_RETURN_FALSE(bytes_read + bytes <= out_data_size); |
| 1275 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
| 1276 | &data[bytes_read], |
| 1277 | bytes, |
| 1278 | extent.start_block() * block_size, |
| 1279 | &bytes_read_this_iteration)); |
| 1280 | TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes); |
| 1281 | bytes_read += bytes_read_this_iteration; |
| 1282 | } |
| 1283 | TEST_AND_RETURN_FALSE(out_data_size == bytes_read); |
| 1284 | *out_data = data; |
| 1285 | return true; |
| 1286 | } |
| 1287 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 1288 | bool GetBootId(string* boot_id) { |
| 1289 | TEST_AND_RETURN_FALSE( |
| 1290 | base::ReadFileToString(base::FilePath(kBootIdPath), boot_id)); |
| 1291 | base::TrimWhitespaceASCII(*boot_id, base::TRIM_TRAILING, boot_id); |
| 1292 | return true; |
| 1293 | } |
| 1294 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1295 | } // namespace utils |
| 1296 | |
| 1297 | } // namespace chromeos_update_engine |