Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/utils.h" |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 6 | |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 7 | #include <attr/xattr.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 8 | #include <sys/mount.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 9 | #include <sys/resource.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 12 | #include <sys/wait.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include <dirent.h> |
| 14 | #include <errno.h> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 15 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <unistd.h> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 20 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 22 | #include <vector> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 23 | |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 24 | #include <base/file_path.h> |
| 25 | #include <base/file_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 26 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 27 | #include <base/posix/eintr_wrapper.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 28 | #include <base/rand_util.h> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 29 | #include <base/string_number_conversions.h> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 30 | #include <base/string_split.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 31 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 32 | #include <base/stringprintf.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 33 | #include <glib.h> |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 34 | #include <google/protobuf/stubs/common.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 35 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 36 | #include "update_engine/constants.h" |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 37 | #include "update_engine/file_writer.h" |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 38 | #include "update_engine/omaha_request_params.h" |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 39 | #include "update_engine/subprocess.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 40 | #include "update_engine/system_state.h" |
| 41 | #include "update_engine/update_attempter.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 42 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 43 | using base::Time; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 44 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 | using std::min; |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 46 | using std::pair; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 47 | using std::string; |
| 48 | using std::vector; |
| 49 | |
| 50 | namespace chromeos_update_engine { |
| 51 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 52 | namespace { |
| 53 | |
| 54 | // The following constants control how UnmountFilesystem should retry if |
| 55 | // umount() fails with an errno EBUSY, i.e. retry 5 times over the course of |
| 56 | // one second. |
| 57 | const int kUnmountMaxNumOfRetries = 5; |
| 58 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 59 | } // namespace |
| 60 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 61 | namespace utils { |
| 62 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 63 | // Cgroup container is created in update-engine's upstart script located at |
| 64 | // /etc/init/update-engine.conf. |
| 65 | static const char kCGroupDir[] = "/sys/fs/cgroup/cpu/update-engine"; |
| 66 | |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 67 | string ParseECVersion(string input_line) { |
| 68 | TrimWhitespaceASCII(input_line, TRIM_ALL, &input_line); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 69 | |
| 70 | // At this point we want to conver the format key=value pair from mosys to |
| 71 | // a vector of key value pairs. |
| 72 | vector<pair<string, string> > kv_pairs; |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 73 | if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 74 | for (vector<pair<string, string> >::iterator it = kv_pairs.begin(); |
| 75 | it != kv_pairs.end(); ++it) { |
| 76 | // Finally match against the fw_verion which may have quotes. |
| 77 | if (it->first == "fw_version") { |
| 78 | string output; |
| 79 | // Trim any quotes. |
| 80 | TrimString(it->second, "\"", &output); |
| 81 | return output; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | LOG(ERROR) << "Unable to parse fwid from ec info."; |
| 86 | return ""; |
| 87 | } |
| 88 | |
| 89 | |
J. Richard Barnette | 3084293 | 2013-10-28 15:04:23 -0700 | [diff] [blame] | 90 | const string KernelDeviceOfBootDevice(const std::string& boot_device) { |
| 91 | if (boot_device.empty()) |
| 92 | return boot_device; |
| 93 | |
| 94 | string ubiblock_prefix("/dev/ubiblock"); |
| 95 | string ret; |
| 96 | char partition_num; |
| 97 | if (StringHasPrefix(boot_device, ubiblock_prefix)) { |
| 98 | // eg: /dev/ubiblock3_0 becomes /dev/mtdblock2 |
| 99 | ret = "/dev/mtdblock"; |
| 100 | partition_num = boot_device[ubiblock_prefix.size()]; |
| 101 | } else { |
| 102 | // eg: /dev/sda3 becomes /dev/sda2 |
| 103 | // eg: /dev/mmcblk0p3 becomes /dev/mmcblk0p2 |
| 104 | ret = boot_device.substr(0, boot_device.size() - 1); |
| 105 | partition_num = boot_device[boot_device.size() - 1]; |
| 106 | } |
| 107 | |
| 108 | // Currently this assumes the partition number of the boot device is |
| 109 | // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to |
| 110 | // get the kernel device. |
| 111 | if (partition_num == '3' || partition_num == '5' || partition_num == '7') { |
| 112 | ret.append(1, partition_num - 1); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | return ""; |
| 117 | } |
| 118 | |
| 119 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 120 | bool WriteFile(const char* path, const char* data, int data_len) { |
| 121 | DirectFileWriter writer; |
| 122 | TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, |
| 123 | O_WRONLY | O_CREAT | O_TRUNC, |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 124 | 0600)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 125 | ScopedFileWriterCloser closer(&writer); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 126 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(data, data_len)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 130 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 131 | const char* c_buf = static_cast<const char*>(buf); |
| 132 | ssize_t bytes_written = 0; |
| 133 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 134 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 135 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 136 | bytes_written += rc; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 141 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 142 | const char* c_buf = static_cast<const char*>(buf); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 143 | size_t bytes_written = 0; |
| 144 | int num_attempts = 0; |
| 145 | while (bytes_written < count) { |
| 146 | num_attempts++; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 147 | ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, |
| 148 | offset + bytes_written); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 149 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. |
| 150 | if (rc < 0) { |
| 151 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts |
| 152 | << " bytes_written=" << bytes_written |
| 153 | << " count=" << count << " offset=" << offset; |
| 154 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 155 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 156 | bytes_written += rc; |
| 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 162 | ssize_t* out_bytes_read) { |
| 163 | char* c_buf = static_cast<char*>(buf); |
| 164 | ssize_t bytes_read = 0; |
| 165 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 166 | ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 167 | offset + bytes_read); |
| 168 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 169 | if (rc == 0) { |
| 170 | break; |
| 171 | } |
| 172 | bytes_read += rc; |
| 173 | } |
| 174 | *out_bytes_read = bytes_read; |
| 175 | return true; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 176 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 179 | // Append |nbytes| of content from |buf| to the vector pointed to by either |
| 180 | // |vec_p| or |str_p|. |
| 181 | static void AppendBytes(const char* buf, size_t nbytes, |
| 182 | std::vector<char>* vec_p) { |
| 183 | CHECK(buf); |
| 184 | CHECK(vec_p); |
| 185 | vec_p->insert(vec_p->end(), buf, buf + nbytes); |
| 186 | } |
| 187 | static void AppendBytes(const char* buf, size_t nbytes, |
| 188 | std::string* str_p) { |
| 189 | CHECK(buf); |
| 190 | CHECK(str_p); |
| 191 | str_p->append(buf, nbytes); |
| 192 | } |
| 193 | |
| 194 | // Reads from an open file |fp|, appending the read content to the container |
| 195 | // 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] | 196 | // file's content, false otherwise. If |size| is not -1, reads up to |size| |
| 197 | // bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 198 | template <class T> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 199 | static bool Read(FILE* fp, off_t size, T* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 200 | CHECK(fp); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 201 | CHECK(size == -1 || size >= 0); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 202 | char buf[1024]; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 203 | while (size == -1 || size > 0) { |
| 204 | off_t bytes_to_read = sizeof(buf); |
| 205 | if (size > 0 && bytes_to_read > size) { |
| 206 | bytes_to_read = size; |
| 207 | } |
| 208 | size_t nbytes = fread(buf, 1, bytes_to_read, fp); |
| 209 | if (!nbytes) { |
| 210 | break; |
| 211 | } |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 212 | AppendBytes(buf, nbytes, out_p); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 213 | if (size != -1) { |
| 214 | CHECK(size >= static_cast<off_t>(nbytes)); |
| 215 | size -= nbytes; |
| 216 | } |
| 217 | } |
| 218 | if (ferror(fp)) { |
| 219 | return false; |
| 220 | } |
| 221 | return size == 0 || feof(fp); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 224 | // Opens a file |path| for reading and appends its the contents to a container |
| 225 | // |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end |
| 226 | // 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] | 227 | template <class T> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 228 | static bool ReadFileChunkAndAppend(const std::string& path, |
| 229 | off_t offset, |
| 230 | off_t size, |
| 231 | T* out_p) { |
| 232 | CHECK_GE(offset, 0); |
| 233 | CHECK(size == -1 || size >= 0); |
| 234 | file_util::ScopedFILE fp(fopen(path.c_str(), "r")); |
| 235 | if (!fp.get()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 236 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 237 | if (offset) { |
| 238 | // Return success without appending any data if a chunk beyond the end of |
| 239 | // the file is requested. |
| 240 | if (offset >= FileSize(path)) { |
| 241 | return true; |
| 242 | } |
| 243 | TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0); |
| 244 | } |
| 245 | return Read(fp.get(), size, out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 248 | // Invokes a pipe |cmd|, then uses |append_func| to append its stdout to a |
| 249 | // container |out_p|. |
| 250 | template <class T> |
| 251 | static bool ReadPipeAndAppend(const std::string& cmd, T* out_p) { |
| 252 | FILE* fp = popen(cmd.c_str(), "r"); |
| 253 | if (!fp) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 254 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 255 | bool success = Read(fp, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 256 | return (success && pclose(fp) >= 0); |
| 257 | } |
| 258 | |
| 259 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 260 | bool ReadFile(const string& path, vector<char>* out_p) { |
| 261 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 264 | bool ReadFile(const string& path, string* out_p) { |
| 265 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 268 | bool ReadFileChunk(const string& path, off_t offset, off_t size, |
| 269 | vector<char>* out_p) { |
| 270 | return ReadFileChunkAndAppend(path, offset, size, out_p); |
| 271 | } |
| 272 | |
| 273 | bool ReadPipe(const string& cmd, vector<char>* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 274 | return ReadPipeAndAppend(cmd, out_p); |
| 275 | } |
| 276 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 277 | bool ReadPipe(const string& cmd, string* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 278 | return ReadPipeAndAppend(cmd, out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 281 | off_t FileSize(const string& path) { |
| 282 | struct stat stbuf; |
| 283 | int rc = stat(path.c_str(), &stbuf); |
| 284 | CHECK_EQ(rc, 0); |
| 285 | if (rc < 0) |
| 286 | return rc; |
| 287 | return stbuf.st_size; |
| 288 | } |
| 289 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 290 | void HexDumpArray(const unsigned char* const arr, const size_t length) { |
| 291 | const unsigned char* const char_arr = |
| 292 | reinterpret_cast<const unsigned char* const>(arr); |
| 293 | LOG(INFO) << "Logging array of length: " << length; |
| 294 | const unsigned int bytes_per_line = 16; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 295 | for (uint32_t i = 0; i < length; i += bytes_per_line) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 296 | const unsigned int bytes_remaining = length - i; |
| 297 | const unsigned int bytes_per_this_line = min(bytes_per_line, |
| 298 | bytes_remaining); |
| 299 | char header[100]; |
| 300 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); |
| 301 | TEST_AND_RETURN(r == 13); |
| 302 | string line = header; |
| 303 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { |
| 304 | char buf[20]; |
| 305 | unsigned char c = char_arr[i + j]; |
| 306 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); |
| 307 | TEST_AND_RETURN(r == 3); |
| 308 | line += buf; |
| 309 | } |
| 310 | LOG(INFO) << line; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | namespace { |
| 315 | class ScopedDirCloser { |
| 316 | public: |
| 317 | explicit ScopedDirCloser(DIR** dir) : dir_(dir) {} |
| 318 | ~ScopedDirCloser() { |
| 319 | if (dir_ && *dir_) { |
| 320 | int r = closedir(*dir_); |
| 321 | TEST_AND_RETURN_ERRNO(r == 0); |
| 322 | *dir_ = NULL; |
| 323 | dir_ = NULL; |
| 324 | } |
| 325 | } |
| 326 | private: |
| 327 | DIR** dir_; |
| 328 | }; |
| 329 | } // namespace {} |
| 330 | |
| 331 | bool RecursiveUnlinkDir(const std::string& path) { |
| 332 | struct stat stbuf; |
| 333 | int r = lstat(path.c_str(), &stbuf); |
| 334 | TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT)); |
| 335 | if ((r < 0) && (errno == ENOENT)) |
| 336 | // path request is missing. that's fine. |
| 337 | return true; |
| 338 | if (!S_ISDIR(stbuf.st_mode)) { |
| 339 | TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) || |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 340 | (errno == ENOENT)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 341 | // success or path disappeared before we could unlink. |
| 342 | return true; |
| 343 | } |
| 344 | { |
| 345 | // We have a dir, unlink all children, then delete dir |
| 346 | DIR *dir = opendir(path.c_str()); |
| 347 | TEST_AND_RETURN_FALSE_ERRNO(dir); |
| 348 | ScopedDirCloser dir_closer(&dir); |
| 349 | struct dirent dir_entry; |
| 350 | struct dirent *dir_entry_p; |
| 351 | int err = 0; |
| 352 | while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) { |
| 353 | if (dir_entry_p == NULL) { |
| 354 | // end of stream reached |
| 355 | break; |
| 356 | } |
| 357 | // Skip . and .. |
| 358 | if (!strcmp(dir_entry_p->d_name, ".") || |
| 359 | !strcmp(dir_entry_p->d_name, "..")) |
| 360 | continue; |
| 361 | TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" + |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 362 | dir_entry_p->d_name)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 363 | } |
| 364 | TEST_AND_RETURN_FALSE(err == 0); |
| 365 | } |
| 366 | // unlink dir |
| 367 | TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT)); |
| 368 | return true; |
| 369 | } |
| 370 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 371 | string RootDevice(const string& partition_device) { |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 372 | FilePath device_path(partition_device); |
| 373 | if (device_path.DirName().value() != "/dev") { |
| 374 | return ""; |
| 375 | } |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 376 | string::const_iterator it = --partition_device.end(); |
| 377 | for (; it >= partition_device.begin(); --it) { |
| 378 | if (!isdigit(*it)) |
| 379 | break; |
| 380 | } |
| 381 | // Some devices contain a p before the partitions. For example: |
| 382 | // /dev/mmc0p4 should be shortened to /dev/mmc0. |
| 383 | if (*it == 'p') |
| 384 | --it; |
| 385 | return string(partition_device.begin(), it + 1); |
| 386 | } |
| 387 | |
| 388 | string PartitionNumber(const string& partition_device) { |
| 389 | CHECK(!partition_device.empty()); |
| 390 | string::const_iterator it = --partition_device.end(); |
| 391 | for (; it >= partition_device.begin(); --it) { |
| 392 | if (!isdigit(*it)) |
| 393 | break; |
| 394 | } |
| 395 | return string(it + 1, partition_device.end()); |
| 396 | } |
| 397 | |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 398 | string SysfsBlockDevice(const string& device) { |
| 399 | FilePath device_path(device); |
| 400 | if (device_path.DirName().value() != "/dev") { |
| 401 | return ""; |
| 402 | } |
| 403 | return FilePath("/sys/block").Append(device_path.BaseName()).value(); |
| 404 | } |
| 405 | |
| 406 | bool IsRemovableDevice(const std::string& device) { |
| 407 | string sysfs_block = SysfsBlockDevice(device); |
| 408 | string removable; |
| 409 | if (sysfs_block.empty() || |
| 410 | !file_util::ReadFileToString(FilePath(sysfs_block).Append("removable"), |
| 411 | &removable)) { |
| 412 | return false; |
| 413 | } |
| 414 | TrimWhitespaceASCII(removable, TRIM_ALL, &removable); |
| 415 | return removable == "1"; |
| 416 | } |
| 417 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 418 | std::string ErrnoNumberAsString(int err) { |
| 419 | char buf[100]; |
| 420 | buf[0] = '\0'; |
| 421 | return strerror_r(err, buf, sizeof(buf)); |
| 422 | } |
| 423 | |
| 424 | std::string NormalizePath(const std::string& path, bool strip_trailing_slash) { |
| 425 | string ret; |
| 426 | bool last_insert_was_slash = false; |
| 427 | for (string::const_iterator it = path.begin(); it != path.end(); ++it) { |
| 428 | if (*it == '/') { |
| 429 | if (last_insert_was_slash) |
| 430 | continue; |
| 431 | last_insert_was_slash = true; |
| 432 | } else { |
| 433 | last_insert_was_slash = false; |
| 434 | } |
| 435 | ret.push_back(*it); |
| 436 | } |
| 437 | if (strip_trailing_slash && last_insert_was_slash) { |
| 438 | string::size_type last_non_slash = ret.find_last_not_of('/'); |
| 439 | if (last_non_slash != string::npos) { |
| 440 | ret.resize(last_non_slash + 1); |
| 441 | } else { |
| 442 | ret = ""; |
| 443 | } |
| 444 | } |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | bool FileExists(const char* path) { |
| 449 | struct stat stbuf; |
| 450 | return 0 == lstat(path, &stbuf); |
| 451 | } |
| 452 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 453 | bool IsSymlink(const char* path) { |
| 454 | struct stat stbuf; |
| 455 | return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0; |
| 456 | } |
| 457 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 458 | std::string TempFilename(string path) { |
| 459 | static const string suffix("XXXXXX"); |
| 460 | CHECK(StringHasSuffix(path, suffix)); |
| 461 | do { |
| 462 | string new_suffix; |
| 463 | for (unsigned int i = 0; i < suffix.size(); i++) { |
| 464 | int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9] |
| 465 | if (r < 26) |
| 466 | new_suffix.append(1, 'a' + r); |
| 467 | else if (r < (26 * 2)) |
| 468 | new_suffix.append(1, 'A' + r - 26); |
| 469 | else |
| 470 | new_suffix.append(1, '0' + r - (26 * 2)); |
| 471 | } |
| 472 | CHECK_EQ(new_suffix.size(), suffix.size()); |
| 473 | path.resize(path.size() - new_suffix.size()); |
| 474 | path.append(new_suffix); |
| 475 | } while (FileExists(path.c_str())); |
| 476 | return path; |
| 477 | } |
| 478 | |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 479 | // If |path| is absolute, or explicit relative to the current working directory, |
| 480 | // leaves it as is. Otherwise, if TMPDIR is defined in the environment and is |
| 481 | // non-empty, prepends it to |path|. Otherwise, prepends /tmp. Returns the |
| 482 | // resulting path. |
| 483 | static const string PrependTmpdir(const string& path) { |
| 484 | if (path[0] == '/' || StartsWithASCII(path, "./", true) || |
| 485 | StartsWithASCII(path, "../", true)) |
| 486 | return path; |
| 487 | |
| 488 | const char *tmpdir = getenv("TMPDIR"); |
| 489 | const string prefix = (tmpdir && *tmpdir ? tmpdir : "/tmp"); |
| 490 | return prefix + "/" + path; |
| 491 | } |
| 492 | |
| 493 | bool MakeTempFile(const std::string& base_filename_template, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 494 | std::string* filename, |
| 495 | int* fd) { |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 496 | const string filename_template = PrependTmpdir(base_filename_template); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 497 | DCHECK(filename || fd); |
| 498 | vector<char> buf(filename_template.size() + 1); |
| 499 | memcpy(&buf[0], filename_template.data(), filename_template.size()); |
| 500 | buf[filename_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 501 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 502 | int mkstemp_fd = mkstemp(&buf[0]); |
| 503 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 504 | if (filename) { |
| 505 | *filename = &buf[0]; |
| 506 | } |
| 507 | if (fd) { |
| 508 | *fd = mkstemp_fd; |
| 509 | } else { |
| 510 | close(mkstemp_fd); |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 515 | bool MakeTempDirectory(const std::string& base_dirname_template, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 516 | std::string* dirname) { |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 517 | const string dirname_template = PrependTmpdir(base_dirname_template); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 518 | DCHECK(dirname); |
| 519 | vector<char> buf(dirname_template.size() + 1); |
| 520 | memcpy(&buf[0], dirname_template.data(), dirname_template.size()); |
| 521 | buf[dirname_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 522 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 523 | char* return_code = mkdtemp(&buf[0]); |
| 524 | TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL); |
| 525 | *dirname = &buf[0]; |
| 526 | return true; |
| 527 | } |
| 528 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 529 | bool StringHasSuffix(const std::string& str, const std::string& suffix) { |
| 530 | if (suffix.size() > str.size()) |
| 531 | return false; |
| 532 | return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); |
| 533 | } |
| 534 | |
| 535 | bool StringHasPrefix(const std::string& str, const std::string& prefix) { |
| 536 | if (prefix.size() > str.size()) |
| 537 | return false; |
| 538 | return 0 == str.compare(0, prefix.size(), prefix); |
| 539 | } |
| 540 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 541 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 542 | const string& mountpoint, |
| 543 | unsigned long mountflags) { |
| 544 | int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", mountflags, NULL); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 545 | if (rc < 0) { |
| 546 | string msg = ErrnoNumberAsString(errno); |
| 547 | LOG(ERROR) << "Unable to mount destination device: " << msg << ". " |
| 548 | << device << " on " << mountpoint; |
| 549 | return false; |
| 550 | } |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | bool UnmountFilesystem(const string& mountpoint) { |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 555 | for (int num_retries = 0; ; ++num_retries) { |
| 556 | if (umount(mountpoint.c_str()) == 0) |
| 557 | break; |
| 558 | |
| 559 | TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY && |
| 560 | num_retries < kUnmountMaxNumOfRetries); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 561 | g_usleep(kUnmountRetryIntervalInMicroseconds); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 562 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 563 | return true; |
| 564 | } |
| 565 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 566 | bool GetFilesystemSize(const std::string& device, |
| 567 | int* out_block_count, |
| 568 | int* out_block_size) { |
| 569 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY)); |
| 570 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 571 | ScopedFdCloser fd_closer(&fd); |
| 572 | return GetFilesystemSizeFromFD(fd, out_block_count, out_block_size); |
| 573 | } |
| 574 | |
| 575 | bool GetFilesystemSizeFromFD(int fd, |
| 576 | int* out_block_count, |
| 577 | int* out_block_size) { |
| 578 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 579 | |
| 580 | // Determine the ext3 filesystem size by directly reading the block count and |
| 581 | // block size information from the superblock. See include/linux/ext3_fs.h for |
| 582 | // more details on the structure. |
| 583 | ssize_t kBufferSize = 16 * sizeof(uint32_t); |
| 584 | char buffer[kBufferSize]; |
| 585 | const int kSuperblockOffset = 1024; |
| 586 | if (HANDLE_EINTR(pread(fd, buffer, kBufferSize, kSuperblockOffset)) != |
| 587 | kBufferSize) { |
| 588 | PLOG(ERROR) << "Unable to determine file system size:"; |
| 589 | return false; |
| 590 | } |
| 591 | uint32_t block_count; // ext3_fs.h: ext3_super_block.s_blocks_count |
| 592 | uint32_t log_block_size; // ext3_fs.h: ext3_super_block.s_log_block_size |
| 593 | uint16_t magic; // ext3_fs.h: ext3_super_block.s_magic |
| 594 | memcpy(&block_count, &buffer[1 * sizeof(int32_t)], sizeof(block_count)); |
| 595 | memcpy(&log_block_size, &buffer[6 * sizeof(int32_t)], sizeof(log_block_size)); |
| 596 | memcpy(&magic, &buffer[14 * sizeof(int32_t)], sizeof(magic)); |
| 597 | block_count = le32toh(block_count); |
| 598 | const int kExt3MinBlockLogSize = 10; // ext3_fs.h: EXT3_MIN_BLOCK_LOG_SIZE |
| 599 | log_block_size = le32toh(log_block_size) + kExt3MinBlockLogSize; |
| 600 | magic = le16toh(magic); |
| 601 | |
| 602 | // Sanity check the parameters. |
| 603 | const uint16_t kExt3SuperMagic = 0xef53; // ext3_fs.h: EXT3_SUPER_MAGIC |
| 604 | TEST_AND_RETURN_FALSE(magic == kExt3SuperMagic); |
| 605 | const int kExt3MinBlockSize = 1024; // ext3_fs.h: EXT3_MIN_BLOCK_SIZE |
| 606 | const int kExt3MaxBlockSize = 4096; // ext3_fs.h: EXT3_MAX_BLOCK_SIZE |
| 607 | int block_size = 1 << log_block_size; |
| 608 | TEST_AND_RETURN_FALSE(block_size >= kExt3MinBlockSize && |
| 609 | block_size <= kExt3MaxBlockSize); |
| 610 | TEST_AND_RETURN_FALSE(block_count > 0); |
| 611 | |
| 612 | if (out_block_count) { |
| 613 | *out_block_count = block_count; |
| 614 | } |
| 615 | if (out_block_size) { |
| 616 | *out_block_size = block_size; |
| 617 | } |
| 618 | return true; |
| 619 | } |
| 620 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 621 | bool GetBootloader(BootLoader* out_bootloader) { |
| 622 | // For now, hardcode to syslinux. |
| 623 | *out_bootloader = BootLoader_SYSLINUX; |
| 624 | return true; |
| 625 | } |
| 626 | |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 627 | string GetAndFreeGError(GError** error) { |
| 628 | if (!*error) { |
| 629 | return "Unknown GLib error."; |
| 630 | } |
| 631 | string message = |
| 632 | base::StringPrintf("GError(%d): %s", |
| 633 | (*error)->code, |
| 634 | (*error)->message ? (*error)->message : "(unknown)"); |
| 635 | g_error_free(*error); |
| 636 | *error = NULL; |
| 637 | return message; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 640 | bool Reboot() { |
| 641 | vector<string> command; |
| 642 | command.push_back("/sbin/shutdown"); |
| 643 | command.push_back("-r"); |
| 644 | command.push_back("now"); |
| 645 | int rc = 0; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 646 | Subprocess::SynchronousExec(command, &rc, NULL); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 647 | TEST_AND_RETURN_FALSE(rc == 0); |
| 648 | return true; |
| 649 | } |
| 650 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 651 | namespace { |
| 652 | // Do the actual trigger. We do it as a main-loop callback to (try to) get a |
| 653 | // consistent stack trace. |
| 654 | gboolean TriggerCrashReporterUpload(void* unused) { |
| 655 | pid_t pid = fork(); |
| 656 | CHECK(pid >= 0) << "fork failed"; // fork() failed. Something is very wrong. |
| 657 | if (pid == 0) { |
| 658 | // We are the child. Crash. |
| 659 | abort(); // never returns |
| 660 | } |
| 661 | // We are the parent. Wait for child to terminate. |
| 662 | pid_t result = waitpid(pid, NULL, 0); |
| 663 | LOG_IF(ERROR, result < 0) << "waitpid() failed"; |
| 664 | return FALSE; // Don't call this callback again |
| 665 | } |
| 666 | } // namespace {} |
| 667 | |
| 668 | void ScheduleCrashReporterUpload() { |
| 669 | g_idle_add(&TriggerCrashReporterUpload, NULL); |
| 670 | } |
| 671 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 672 | bool SetCpuShares(CpuShares shares) { |
| 673 | string string_shares = base::IntToString(static_cast<int>(shares)); |
| 674 | string cpu_shares_file = string(utils::kCGroupDir) + "/cpu.shares"; |
| 675 | LOG(INFO) << "Setting cgroup cpu shares to " << string_shares; |
| 676 | if(utils::WriteFile(cpu_shares_file.c_str(), string_shares.c_str(), |
| 677 | string_shares.size())){ |
| 678 | return true; |
| 679 | } else { |
| 680 | LOG(ERROR) << "Failed to change cgroup cpu shares to "<< string_shares |
| 681 | << " using " << cpu_shares_file; |
| 682 | return false; |
| 683 | } |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 686 | int CompareCpuShares(CpuShares shares_lhs, |
| 687 | CpuShares shares_rhs) { |
| 688 | return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 691 | int FuzzInt(int value, unsigned int range) { |
| 692 | int min = value - range / 2; |
| 693 | int max = value + range - range / 2; |
| 694 | return base::RandInt(min, max); |
| 695 | } |
| 696 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 697 | gboolean GlibRunClosure(gpointer data) { |
| 698 | google::protobuf::Closure* callback = |
| 699 | reinterpret_cast<google::protobuf::Closure*>(data); |
| 700 | callback->Run(); |
| 701 | return FALSE; |
| 702 | } |
| 703 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 704 | string FormatSecs(unsigned secs) { |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 705 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 708 | string FormatTimeDelta(TimeDelta delta) { |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 709 | // Canonicalize into days, hours, minutes, seconds and microseconds. |
| 710 | unsigned days = delta.InDays(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 711 | delta -= TimeDelta::FromDays(days); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 712 | unsigned hours = delta.InHours(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 713 | delta -= TimeDelta::FromHours(hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 714 | unsigned mins = delta.InMinutes(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 715 | delta -= TimeDelta::FromMinutes(mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 716 | unsigned secs = delta.InSeconds(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 717 | delta -= TimeDelta::FromSeconds(secs); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 718 | unsigned usecs = delta.InMicroseconds(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 719 | |
| 720 | // Construct and return string. |
| 721 | string str; |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 722 | if (days) |
| 723 | base::StringAppendF(&str, "%ud", days); |
| 724 | if (days || hours) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 725 | base::StringAppendF(&str, "%uh", hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 726 | if (days || hours || mins) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 727 | base::StringAppendF(&str, "%um", mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 728 | base::StringAppendF(&str, "%u", secs); |
| 729 | if (usecs) { |
| 730 | int width = 6; |
| 731 | while ((usecs / 10) * 10 == usecs) { |
| 732 | usecs /= 10; |
| 733 | width--; |
| 734 | } |
| 735 | base::StringAppendF(&str, ".%0*u", width, usecs); |
| 736 | } |
| 737 | base::StringAppendF(&str, "s"); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 738 | return str; |
| 739 | } |
| 740 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 741 | string ToString(const Time utc_time) { |
| 742 | Time::Exploded exp_time; |
| 743 | utc_time.UTCExplode(&exp_time); |
| 744 | return StringPrintf("%d/%d/%d %d:%02d:%02d GMT", |
| 745 | exp_time.month, |
| 746 | exp_time.day_of_month, |
| 747 | exp_time.year, |
| 748 | exp_time.hour, |
| 749 | exp_time.minute, |
| 750 | exp_time.second); |
| 751 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 752 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 753 | string ToString(bool b) { |
| 754 | return (b ? "true" : "false"); |
| 755 | } |
| 756 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 757 | string ToString(DownloadSource source) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 758 | switch (source) { |
| 759 | case kDownloadSourceHttpsServer: return "HttpsServer"; |
| 760 | case kDownloadSourceHttpServer: return "HttpServer"; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 761 | case kDownloadSourceHttpPeer: return "HttpPeer"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 762 | case kNumDownloadSources: return "Unknown"; |
| 763 | // Don't add a default case to let the compiler warn about newly added |
| 764 | // download sources which should be added here. |
| 765 | } |
| 766 | |
| 767 | return "Unknown"; |
| 768 | } |
| 769 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 770 | string ToString(PayloadType payload_type) { |
| 771 | switch (payload_type) { |
| 772 | case kPayloadTypeDelta: return "Delta"; |
| 773 | case kPayloadTypeFull: return "Full"; |
| 774 | case kPayloadTypeForcedFull: return "ForcedFull"; |
| 775 | case kNumPayloadTypes: return "Unknown"; |
| 776 | // Don't add a default case to let the compiler warn about newly added |
| 777 | // payload types which should be added here. |
| 778 | } |
| 779 | |
| 780 | return "Unknown"; |
| 781 | } |
| 782 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 783 | ErrorCode GetBaseErrorCode(ErrorCode code) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 784 | // Ignore the higher order bits in the code by applying the mask as |
| 785 | // we want the enumerations to be in the small contiguous range |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 786 | // with values less than kErrorCodeUmaReportedMax. |
| 787 | ErrorCode base_code = static_cast<ErrorCode>(code & ~kErrorCodeSpecialFlags); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 788 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 789 | // Make additional adjustments required for UMA and error classification. |
| 790 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix |
| 791 | // chromium-os:34369. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 792 | if (base_code >= kErrorCodeOmahaRequestHTTPResponseBase) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 793 | // 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] | 794 | // errors into this one bucket for UMA and error classification purposes. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 795 | LOG(INFO) << "Converting error code " << base_code |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 796 | << " to kErrorCodeOmahaErrorInHTTPResponse"; |
| 797 | base_code = kErrorCodeOmahaErrorInHTTPResponse; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 800 | return base_code; |
| 801 | } |
| 802 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 803 | // Returns a printable version of the various flags denoted in the higher order |
| 804 | // bits of the given code. Returns an empty string if none of those bits are |
| 805 | // set. |
| 806 | string GetFlagNames(uint32_t code) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 807 | uint32_t flags = code & kErrorCodeSpecialFlags; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 808 | string flag_names; |
| 809 | string separator = ""; |
| 810 | for(size_t i = 0; i < sizeof(flags) * 8; i++) { |
| 811 | uint32_t flag = flags & (1 << i); |
| 812 | if (flag) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 813 | flag_names += separator + CodeToString(static_cast<ErrorCode>(flag)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 814 | separator = ", "; |
| 815 | } |
| 816 | } |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 817 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 818 | return flag_names; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 819 | } |
| 820 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 821 | void SendErrorCodeToUma(SystemState* system_state, ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 822 | if (!system_state) |
| 823 | return; |
| 824 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 825 | ErrorCode uma_error_code = GetBaseErrorCode(code); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 826 | |
| 827 | // If the code doesn't have flags computed already, compute them now based on |
| 828 | // the state of the current update attempt. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 829 | uint32_t flags = code & kErrorCodeSpecialFlags; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 830 | if (!flags) |
| 831 | flags = system_state->update_attempter()->GetErrorCodeFlags(); |
| 832 | |
| 833 | // Determine the UMA bucket depending on the flags. But, ignore the resumed |
| 834 | // flag, as it's perfectly normal for production devices to resume their |
| 835 | // downloads and so we want to record those cases also in NormalErrorCodes |
| 836 | // bucket. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 837 | string metric = (flags & ~kErrorCodeResumedFlag) ? |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 838 | "Installer.DevModeErrorCodes" : "Installer.NormalErrorCodes"; |
| 839 | |
| 840 | LOG(INFO) << "Sending error code " << uma_error_code |
| 841 | << " (" << CodeToString(uma_error_code) << ")" |
| 842 | << " to UMA metric: " << metric |
| 843 | << ". Flags = " << (flags ? GetFlagNames(flags) : "None"); |
| 844 | |
| 845 | system_state->metrics_lib()->SendEnumToUMA(metric, |
| 846 | uma_error_code, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 847 | kErrorCodeUmaReportedMax); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 848 | } |
| 849 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 850 | string CodeToString(ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 851 | // If the given code has both parts (i.e. the error code part and the flags |
| 852 | // part) then strip off the flags part since the switch statement below |
| 853 | // has case statements only for the base error code or a single flag but |
| 854 | // doesn't support any combinations of those. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 855 | if ((code & kErrorCodeSpecialFlags) && (code & ~kErrorCodeSpecialFlags)) |
| 856 | code = static_cast<ErrorCode>(code & ~kErrorCodeSpecialFlags); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 857 | switch (code) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 858 | case kErrorCodeSuccess: return "kErrorCodeSuccess"; |
| 859 | case kErrorCodeError: return "kErrorCodeError"; |
| 860 | case kErrorCodeOmahaRequestError: return "kErrorCodeOmahaRequestError"; |
| 861 | case kErrorCodeOmahaResponseHandlerError: |
| 862 | return "kErrorCodeOmahaResponseHandlerError"; |
| 863 | case kErrorCodeFilesystemCopierError: |
| 864 | return "kErrorCodeFilesystemCopierError"; |
| 865 | case kErrorCodePostinstallRunnerError: |
| 866 | return "kErrorCodePostinstallRunnerError"; |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 867 | case kErrorCodePayloadMismatchedType: |
| 868 | return "kErrorCodePayloadMismatchedType"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 869 | case kErrorCodeInstallDeviceOpenError: |
| 870 | return "kErrorCodeInstallDeviceOpenError"; |
| 871 | case kErrorCodeKernelDeviceOpenError: |
| 872 | return "kErrorCodeKernelDeviceOpenError"; |
| 873 | case kErrorCodeDownloadTransferError: |
| 874 | return "kErrorCodeDownloadTransferError"; |
| 875 | case kErrorCodePayloadHashMismatchError: |
| 876 | return "kErrorCodePayloadHashMismatchError"; |
| 877 | case kErrorCodePayloadSizeMismatchError: |
| 878 | return "kErrorCodePayloadSizeMismatchError"; |
| 879 | case kErrorCodeDownloadPayloadVerificationError: |
| 880 | return "kErrorCodeDownloadPayloadVerificationError"; |
| 881 | case kErrorCodeDownloadNewPartitionInfoError: |
| 882 | return "kErrorCodeDownloadNewPartitionInfoError"; |
| 883 | case kErrorCodeDownloadWriteError: |
| 884 | return "kErrorCodeDownloadWriteError"; |
| 885 | case kErrorCodeNewRootfsVerificationError: |
| 886 | return "kErrorCodeNewRootfsVerificationError"; |
| 887 | case kErrorCodeNewKernelVerificationError: |
| 888 | return "kErrorCodeNewKernelVerificationError"; |
| 889 | case kErrorCodeSignedDeltaPayloadExpectedError: |
| 890 | return "kErrorCodeSignedDeltaPayloadExpectedError"; |
| 891 | case kErrorCodeDownloadPayloadPubKeyVerificationError: |
| 892 | return "kErrorCodeDownloadPayloadPubKeyVerificationError"; |
| 893 | case kErrorCodePostinstallBootedFromFirmwareB: |
| 894 | return "kErrorCodePostinstallBootedFromFirmwareB"; |
| 895 | case kErrorCodeDownloadStateInitializationError: |
| 896 | return "kErrorCodeDownloadStateInitializationError"; |
| 897 | case kErrorCodeDownloadInvalidMetadataMagicString: |
| 898 | return "kErrorCodeDownloadInvalidMetadataMagicString"; |
| 899 | case kErrorCodeDownloadSignatureMissingInManifest: |
| 900 | return "kErrorCodeDownloadSignatureMissingInManifest"; |
| 901 | case kErrorCodeDownloadManifestParseError: |
| 902 | return "kErrorCodeDownloadManifestParseError"; |
| 903 | case kErrorCodeDownloadMetadataSignatureError: |
| 904 | return "kErrorCodeDownloadMetadataSignatureError"; |
| 905 | case kErrorCodeDownloadMetadataSignatureVerificationError: |
| 906 | return "kErrorCodeDownloadMetadataSignatureVerificationError"; |
| 907 | case kErrorCodeDownloadMetadataSignatureMismatch: |
| 908 | return "kErrorCodeDownloadMetadataSignatureMismatch"; |
| 909 | case kErrorCodeDownloadOperationHashVerificationError: |
| 910 | return "kErrorCodeDownloadOperationHashVerificationError"; |
| 911 | case kErrorCodeDownloadOperationExecutionError: |
| 912 | return "kErrorCodeDownloadOperationExecutionError"; |
| 913 | case kErrorCodeDownloadOperationHashMismatch: |
| 914 | return "kErrorCodeDownloadOperationHashMismatch"; |
| 915 | case kErrorCodeOmahaRequestEmptyResponseError: |
| 916 | return "kErrorCodeOmahaRequestEmptyResponseError"; |
| 917 | case kErrorCodeOmahaRequestXMLParseError: |
| 918 | return "kErrorCodeOmahaRequestXMLParseError"; |
| 919 | case kErrorCodeDownloadInvalidMetadataSize: |
| 920 | return "kErrorCodeDownloadInvalidMetadataSize"; |
| 921 | case kErrorCodeDownloadInvalidMetadataSignature: |
| 922 | return "kErrorCodeDownloadInvalidMetadataSignature"; |
| 923 | case kErrorCodeOmahaResponseInvalid: |
| 924 | return "kErrorCodeOmahaResponseInvalid"; |
| 925 | case kErrorCodeOmahaUpdateIgnoredPerPolicy: |
| 926 | return "kErrorCodeOmahaUpdateIgnoredPerPolicy"; |
| 927 | case kErrorCodeOmahaUpdateDeferredPerPolicy: |
| 928 | return "kErrorCodeOmahaUpdateDeferredPerPolicy"; |
| 929 | case kErrorCodeOmahaErrorInHTTPResponse: |
| 930 | return "kErrorCodeOmahaErrorInHTTPResponse"; |
| 931 | case kErrorCodeDownloadOperationHashMissingError: |
| 932 | return "kErrorCodeDownloadOperationHashMissingError"; |
| 933 | case kErrorCodeDownloadMetadataSignatureMissingError: |
| 934 | return "kErrorCodeDownloadMetadataSignatureMissingError"; |
| 935 | case kErrorCodeOmahaUpdateDeferredForBackoff: |
| 936 | return "kErrorCodeOmahaUpdateDeferredForBackoff"; |
| 937 | case kErrorCodePostinstallPowerwashError: |
| 938 | return "kErrorCodePostinstallPowerwashError"; |
| 939 | case kErrorCodeUpdateCanceledByChannelChange: |
| 940 | return "kErrorCodeUpdateCanceledByChannelChange"; |
| 941 | case kErrorCodeUmaReportedMax: |
| 942 | return "kErrorCodeUmaReportedMax"; |
| 943 | case kErrorCodeOmahaRequestHTTPResponseBase: |
| 944 | return "kErrorCodeOmahaRequestHTTPResponseBase"; |
| 945 | case kErrorCodeResumedFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 946 | return "Resumed"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 947 | case kErrorCodeDevModeFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 948 | return "DevMode"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 949 | case kErrorCodeTestImageFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 950 | return "TestImage"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 951 | case kErrorCodeTestOmahaUrlFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 952 | return "TestOmahaUrl"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 953 | case kErrorCodeSpecialFlags: |
| 954 | return "kErrorCodeSpecialFlags"; |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 955 | case kErrorCodePostinstallFirmwareRONotUpdatable: |
| 956 | return "kErrorCodePostinstallFirmwareRONotUpdatable"; |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 957 | case kErrorCodeUnsupportedMajorPayloadVersion: |
| 958 | return "kErrorCodeUnsupportedMajorPayloadVersion"; |
| 959 | case kErrorCodeUnsupportedMinorPayloadVersion: |
| 960 | return "kErrorCodeUnsupportedMinorPayloadVersion"; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 961 | // Don't add a default case to let the compiler warn about newly added |
| 962 | // error codes which should be added here. |
| 963 | } |
| 964 | |
| 965 | return "Unknown error: " + base::UintToString(static_cast<unsigned>(code)); |
| 966 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 967 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 968 | bool CreatePowerwashMarkerFile(const char* file_path) { |
| 969 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
| 970 | bool result = utils::WriteFile(marker_file, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 971 | kPowerwashCommand, |
| 972 | strlen(kPowerwashCommand)); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 973 | if (result) { |
| 974 | LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot"; |
| 975 | } else { |
| 976 | PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file; |
| 977 | } |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 978 | |
| 979 | return result; |
| 980 | } |
| 981 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 982 | bool DeletePowerwashMarkerFile(const char* file_path) { |
| 983 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
| 984 | const FilePath kPowerwashMarkerPath(marker_file); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 985 | bool result = file_util::Delete(kPowerwashMarkerPath, false); |
| 986 | |
| 987 | if (result) |
| 988 | LOG(INFO) << "Successfully deleted the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 989 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 990 | else |
| 991 | PLOG(ERROR) << "Could not delete the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 992 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 993 | |
| 994 | return result; |
| 995 | } |
| 996 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 997 | bool GetInstallDev(const std::string& boot_dev, std::string* install_dev) { |
| 998 | TEST_AND_RETURN_FALSE(StringHasPrefix(boot_dev, "/dev/")); |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame] | 999 | string::iterator it; |
| 1000 | string ubiblock_prefix("/dev/ubiblock"); |
| 1001 | |
| 1002 | install_dev->assign(boot_dev); |
| 1003 | |
| 1004 | if(StringHasPrefix(boot_dev, ubiblock_prefix)) { |
| 1005 | // UBI-based device |
| 1006 | it = install_dev->begin() + ubiblock_prefix.length(); |
| 1007 | } else { |
| 1008 | // non-UBI device |
| 1009 | it = install_dev->end() - 1; // last character in string |
| 1010 | } |
| 1011 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 1012 | // Right now, we just switch '3' and '5' partition numbers. |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame] | 1013 | TEST_AND_RETURN_FALSE(*it == '3' || *it == '5'); |
| 1014 | *it = (*it == '3' ? '5' : '3'); |
| 1015 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 1016 | return true; |
| 1017 | } |
| 1018 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1019 | Time TimeFromStructTimespec(struct timespec *ts) { |
| 1020 | int64 us = static_cast<int64>(ts->tv_sec) * Time::kMicrosecondsPerSecond + |
| 1021 | static_cast<int64>(ts->tv_nsec) / Time::kNanosecondsPerMicrosecond; |
| 1022 | return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us); |
| 1023 | } |
| 1024 | |
| 1025 | gchar** StringVectorToGStrv(const vector<string> &vector) { |
| 1026 | GPtrArray *p = g_ptr_array_new(); |
| 1027 | for (std::vector<string>::const_iterator i = vector.begin(); |
| 1028 | i != vector.end(); ++i) { |
| 1029 | g_ptr_array_add(p, g_strdup(i->c_str())); |
| 1030 | } |
| 1031 | g_ptr_array_add(p, NULL); |
| 1032 | return reinterpret_cast<gchar**>(g_ptr_array_free(p, FALSE)); |
| 1033 | } |
| 1034 | |
| 1035 | string StringVectorToString(const vector<string> &vector) { |
| 1036 | string str = "["; |
| 1037 | for (std::vector<string>::const_iterator i = vector.begin(); |
| 1038 | i != vector.end(); ++i) { |
| 1039 | if (i != vector.begin()) |
| 1040 | str += ", "; |
| 1041 | str += '"'; |
| 1042 | str += *i; |
| 1043 | str += '"'; |
| 1044 | } |
| 1045 | str += "]"; |
| 1046 | return str; |
| 1047 | } |
| 1048 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1049 | string CalculateP2PFileId(const string& payload_hash, size_t payload_size) { |
| 1050 | string encoded_hash; |
| 1051 | OmahaHashCalculator::Base64Encode(payload_hash.c_str(), |
| 1052 | payload_hash.size(), |
| 1053 | &encoded_hash); |
| 1054 | return StringPrintf("cros_update_size_%zu_hash_%s", |
| 1055 | payload_size, |
| 1056 | encoded_hash.c_str()); |
| 1057 | } |
| 1058 | |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 1059 | bool IsXAttrSupported(const base::FilePath& dir_path) { |
| 1060 | char *path = strdup(dir_path.Append("xattr_test_XXXXXX").value().c_str()); |
| 1061 | |
| 1062 | int fd = mkstemp(path); |
| 1063 | if (fd == -1) { |
| 1064 | PLOG(ERROR) << "Error creating temporary file in " << dir_path.value(); |
| 1065 | free(path); |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | if (unlink(path) != 0) { |
| 1070 | PLOG(ERROR) << "Error unlinking temporary file " << path; |
| 1071 | close(fd); |
| 1072 | free(path); |
| 1073 | return false; |
| 1074 | } |
| 1075 | |
| 1076 | int xattr_res = fsetxattr(fd, "user.xattr-test", "value", strlen("value"), 0); |
| 1077 | if (xattr_res != 0) { |
| 1078 | if (errno == ENOTSUP) { |
| 1079 | // Leave it to call-sites to warn about non-support. |
| 1080 | } else { |
| 1081 | PLOG(ERROR) << "Error setting xattr on " << path; |
| 1082 | } |
| 1083 | } |
| 1084 | close(fd); |
| 1085 | free(path); |
| 1086 | return xattr_res == 0; |
| 1087 | } |
| 1088 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1089 | bool DecodeAndStoreBase64String(const std::string& base64_encoded, |
| 1090 | base::FilePath *out_path) { |
| 1091 | vector<char> contents; |
| 1092 | |
| 1093 | out_path->clear(); |
| 1094 | |
| 1095 | if (base64_encoded.size() == 0) { |
| 1096 | LOG(ERROR) << "Can't decode empty string."; |
| 1097 | return false; |
| 1098 | } |
| 1099 | |
| 1100 | if (!OmahaHashCalculator::Base64Decode(base64_encoded, &contents) || |
| 1101 | contents.size() == 0) { |
| 1102 | LOG(ERROR) << "Error decoding base64."; |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | FILE *file = file_util::CreateAndOpenTemporaryFile(out_path); |
| 1107 | if (file == NULL) { |
| 1108 | LOG(ERROR) << "Error creating temporary file."; |
| 1109 | return false; |
| 1110 | } |
| 1111 | |
| 1112 | if (fwrite(&contents[0], 1, contents.size(), file) != contents.size()) { |
| 1113 | PLOG(ERROR) << "Error writing to temporary file."; |
| 1114 | if (fclose(file) != 0) |
| 1115 | PLOG(ERROR) << "Error closing temporary file."; |
| 1116 | if (unlink(out_path->value().c_str()) != 0) |
| 1117 | PLOG(ERROR) << "Error unlinking temporary file."; |
| 1118 | out_path->clear(); |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
| 1122 | if (fclose(file) != 0) { |
| 1123 | PLOG(ERROR) << "Error closing temporary file."; |
| 1124 | out_path->clear(); |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
| 1128 | return true; |
| 1129 | } |
| 1130 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1131 | bool ConvertToOmahaInstallDate(base::Time time, int *out_num_days) { |
| 1132 | time_t unix_time = time.ToTimeT(); |
| 1133 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". |
| 1134 | const time_t kOmahaEpoch = 1167638400; |
| 1135 | const int64_t kNumSecondsPerWeek = 7*24*3600; |
| 1136 | const int64_t kNumDaysPerWeek = 7; |
| 1137 | |
| 1138 | time_t omaha_time = unix_time - kOmahaEpoch; |
| 1139 | |
| 1140 | if (omaha_time < 0) |
| 1141 | return false; |
| 1142 | |
| 1143 | // Note, as per the comment in utils.h we are deliberately not |
| 1144 | // handling DST correctly. |
| 1145 | |
| 1146 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; |
| 1147 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; |
| 1148 | |
| 1149 | return true; |
| 1150 | } |
| 1151 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1152 | } // namespace utils |
| 1153 | |
| 1154 | } // namespace chromeos_update_engine |