Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 1 | // Copyright (c) 2009 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" |
| 6 | #include <sys/mount.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <dirent.h> |
| 10 | #include <errno.h> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 11 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <unistd.h> |
| 16 | #include <algorithm> |
| 17 | #include "chromeos/obsolete_logging.h" |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 18 | #include "update_engine/file_writer.h" |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame^] | 19 | #include "update_engine/omaha_request_params.h" |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 20 | #include "update_engine/subprocess.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | |
| 22 | using std::min; |
| 23 | using std::string; |
| 24 | using std::vector; |
| 25 | |
| 26 | namespace chromeos_update_engine { |
| 27 | |
| 28 | namespace utils { |
| 29 | |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame^] | 30 | bool IsOfficialBuild() { |
| 31 | OmahaRequestDeviceParams params; |
| 32 | if (!params.Init("", "")) { |
| 33 | return true; |
| 34 | } |
| 35 | return params.app_track != "buildbot-build" && |
| 36 | params.app_track != "developer-build"; |
| 37 | } |
| 38 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 39 | bool WriteFile(const char* path, const char* data, int data_len) { |
| 40 | DirectFileWriter writer; |
| 41 | TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, |
| 42 | O_WRONLY | O_CREAT | O_TRUNC, |
| 43 | 0666)); |
| 44 | ScopedFileWriterCloser closer(&writer); |
| 45 | TEST_AND_RETURN_FALSE_ERRNO(data_len == writer.Write(data, data_len)); |
| 46 | return true; |
| 47 | } |
| 48 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 49 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 50 | const char* c_buf = static_cast<const char*>(buf); |
| 51 | ssize_t bytes_written = 0; |
| 52 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 53 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 54 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 55 | bytes_written += rc; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 60 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 61 | const char* c_buf = static_cast<const char*>(buf); |
| 62 | ssize_t bytes_written = 0; |
| 63 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 64 | ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, |
| 65 | offset + bytes_written); |
| 66 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 67 | bytes_written += rc; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 73 | ssize_t* out_bytes_read) { |
| 74 | char* c_buf = static_cast<char*>(buf); |
| 75 | ssize_t bytes_read = 0; |
| 76 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 77 | ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 78 | offset + bytes_read); |
| 79 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 80 | if (rc == 0) { |
| 81 | break; |
| 82 | } |
| 83 | bytes_read += rc; |
| 84 | } |
| 85 | *out_bytes_read = bytes_read; |
| 86 | return true; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 87 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 88 | } |
| 89 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 90 | bool ReadFile(const std::string& path, std::vector<char>* out) { |
| 91 | CHECK(out); |
| 92 | FILE* fp = fopen(path.c_str(), "r"); |
| 93 | if (!fp) |
| 94 | return false; |
| 95 | const size_t kChunkSize = 1024; |
| 96 | size_t read_size; |
| 97 | do { |
| 98 | char buf[kChunkSize]; |
| 99 | read_size = fread(buf, 1, kChunkSize, fp); |
| 100 | if (read_size == 0) |
| 101 | break; |
| 102 | out->insert(out->end(), buf, buf + read_size); |
| 103 | } while (read_size == kChunkSize); |
| 104 | bool success = !ferror(fp); |
| 105 | TEST_AND_RETURN_FALSE_ERRNO(fclose(fp) == 0); |
| 106 | return success; |
| 107 | } |
| 108 | |
| 109 | bool ReadFileToString(const std::string& path, std::string* out) { |
| 110 | vector<char> data; |
| 111 | bool success = ReadFile(path, &data); |
| 112 | if (!success) { |
| 113 | return false; |
| 114 | } |
| 115 | (*out) = string(&data[0], data.size()); |
| 116 | return true; |
| 117 | } |
| 118 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 119 | off_t FileSize(const string& path) { |
| 120 | struct stat stbuf; |
| 121 | int rc = stat(path.c_str(), &stbuf); |
| 122 | CHECK_EQ(rc, 0); |
| 123 | if (rc < 0) |
| 124 | return rc; |
| 125 | return stbuf.st_size; |
| 126 | } |
| 127 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 128 | void HexDumpArray(const unsigned char* const arr, const size_t length) { |
| 129 | const unsigned char* const char_arr = |
| 130 | reinterpret_cast<const unsigned char* const>(arr); |
| 131 | LOG(INFO) << "Logging array of length: " << length; |
| 132 | const unsigned int bytes_per_line = 16; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 133 | for (uint32_t i = 0; i < length; i += bytes_per_line) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 134 | const unsigned int bytes_remaining = length - i; |
| 135 | const unsigned int bytes_per_this_line = min(bytes_per_line, |
| 136 | bytes_remaining); |
| 137 | char header[100]; |
| 138 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); |
| 139 | TEST_AND_RETURN(r == 13); |
| 140 | string line = header; |
| 141 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { |
| 142 | char buf[20]; |
| 143 | unsigned char c = char_arr[i + j]; |
| 144 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); |
| 145 | TEST_AND_RETURN(r == 3); |
| 146 | line += buf; |
| 147 | } |
| 148 | LOG(INFO) << line; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | namespace { |
| 153 | class ScopedDirCloser { |
| 154 | public: |
| 155 | explicit ScopedDirCloser(DIR** dir) : dir_(dir) {} |
| 156 | ~ScopedDirCloser() { |
| 157 | if (dir_ && *dir_) { |
| 158 | int r = closedir(*dir_); |
| 159 | TEST_AND_RETURN_ERRNO(r == 0); |
| 160 | *dir_ = NULL; |
| 161 | dir_ = NULL; |
| 162 | } |
| 163 | } |
| 164 | private: |
| 165 | DIR** dir_; |
| 166 | }; |
| 167 | } // namespace {} |
| 168 | |
| 169 | bool RecursiveUnlinkDir(const std::string& path) { |
| 170 | struct stat stbuf; |
| 171 | int r = lstat(path.c_str(), &stbuf); |
| 172 | TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT)); |
| 173 | if ((r < 0) && (errno == ENOENT)) |
| 174 | // path request is missing. that's fine. |
| 175 | return true; |
| 176 | if (!S_ISDIR(stbuf.st_mode)) { |
| 177 | TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) || |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 178 | (errno == ENOENT)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 179 | // success or path disappeared before we could unlink. |
| 180 | return true; |
| 181 | } |
| 182 | { |
| 183 | // We have a dir, unlink all children, then delete dir |
| 184 | DIR *dir = opendir(path.c_str()); |
| 185 | TEST_AND_RETURN_FALSE_ERRNO(dir); |
| 186 | ScopedDirCloser dir_closer(&dir); |
| 187 | struct dirent dir_entry; |
| 188 | struct dirent *dir_entry_p; |
| 189 | int err = 0; |
| 190 | while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) { |
| 191 | if (dir_entry_p == NULL) { |
| 192 | // end of stream reached |
| 193 | break; |
| 194 | } |
| 195 | // Skip . and .. |
| 196 | if (!strcmp(dir_entry_p->d_name, ".") || |
| 197 | !strcmp(dir_entry_p->d_name, "..")) |
| 198 | continue; |
| 199 | TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" + |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 200 | dir_entry_p->d_name)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 201 | } |
| 202 | TEST_AND_RETURN_FALSE(err == 0); |
| 203 | } |
| 204 | // unlink dir |
| 205 | TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT)); |
| 206 | return true; |
| 207 | } |
| 208 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 209 | string RootDevice(const string& partition_device) { |
| 210 | CHECK(!partition_device.empty()); |
| 211 | string::const_iterator it = --partition_device.end(); |
| 212 | for (; it >= partition_device.begin(); --it) { |
| 213 | if (!isdigit(*it)) |
| 214 | break; |
| 215 | } |
| 216 | // Some devices contain a p before the partitions. For example: |
| 217 | // /dev/mmc0p4 should be shortened to /dev/mmc0. |
| 218 | if (*it == 'p') |
| 219 | --it; |
| 220 | return string(partition_device.begin(), it + 1); |
| 221 | } |
| 222 | |
| 223 | string PartitionNumber(const string& partition_device) { |
| 224 | CHECK(!partition_device.empty()); |
| 225 | string::const_iterator it = --partition_device.end(); |
| 226 | for (; it >= partition_device.begin(); --it) { |
| 227 | if (!isdigit(*it)) |
| 228 | break; |
| 229 | } |
| 230 | return string(it + 1, partition_device.end()); |
| 231 | } |
| 232 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 233 | std::string ErrnoNumberAsString(int err) { |
| 234 | char buf[100]; |
| 235 | buf[0] = '\0'; |
| 236 | return strerror_r(err, buf, sizeof(buf)); |
| 237 | } |
| 238 | |
| 239 | std::string NormalizePath(const std::string& path, bool strip_trailing_slash) { |
| 240 | string ret; |
| 241 | bool last_insert_was_slash = false; |
| 242 | for (string::const_iterator it = path.begin(); it != path.end(); ++it) { |
| 243 | if (*it == '/') { |
| 244 | if (last_insert_was_slash) |
| 245 | continue; |
| 246 | last_insert_was_slash = true; |
| 247 | } else { |
| 248 | last_insert_was_slash = false; |
| 249 | } |
| 250 | ret.push_back(*it); |
| 251 | } |
| 252 | if (strip_trailing_slash && last_insert_was_slash) { |
| 253 | string::size_type last_non_slash = ret.find_last_not_of('/'); |
| 254 | if (last_non_slash != string::npos) { |
| 255 | ret.resize(last_non_slash + 1); |
| 256 | } else { |
| 257 | ret = ""; |
| 258 | } |
| 259 | } |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | bool FileExists(const char* path) { |
| 264 | struct stat stbuf; |
| 265 | return 0 == lstat(path, &stbuf); |
| 266 | } |
| 267 | |
| 268 | std::string TempFilename(string path) { |
| 269 | static const string suffix("XXXXXX"); |
| 270 | CHECK(StringHasSuffix(path, suffix)); |
| 271 | do { |
| 272 | string new_suffix; |
| 273 | for (unsigned int i = 0; i < suffix.size(); i++) { |
| 274 | int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9] |
| 275 | if (r < 26) |
| 276 | new_suffix.append(1, 'a' + r); |
| 277 | else if (r < (26 * 2)) |
| 278 | new_suffix.append(1, 'A' + r - 26); |
| 279 | else |
| 280 | new_suffix.append(1, '0' + r - (26 * 2)); |
| 281 | } |
| 282 | CHECK_EQ(new_suffix.size(), suffix.size()); |
| 283 | path.resize(path.size() - new_suffix.size()); |
| 284 | path.append(new_suffix); |
| 285 | } while (FileExists(path.c_str())); |
| 286 | return path; |
| 287 | } |
| 288 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 289 | bool MakeTempFile(const std::string& filename_template, |
| 290 | std::string* filename, |
| 291 | int* fd) { |
| 292 | DCHECK(filename || fd); |
| 293 | vector<char> buf(filename_template.size() + 1); |
| 294 | memcpy(&buf[0], filename_template.data(), filename_template.size()); |
| 295 | buf[filename_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 296 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 297 | int mkstemp_fd = mkstemp(&buf[0]); |
| 298 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 299 | if (filename) { |
| 300 | *filename = &buf[0]; |
| 301 | } |
| 302 | if (fd) { |
| 303 | *fd = mkstemp_fd; |
| 304 | } else { |
| 305 | close(mkstemp_fd); |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 310 | bool MakeTempDirectory(const std::string& dirname_template, |
| 311 | std::string* dirname) { |
| 312 | DCHECK(dirname); |
| 313 | vector<char> buf(dirname_template.size() + 1); |
| 314 | memcpy(&buf[0], dirname_template.data(), dirname_template.size()); |
| 315 | buf[dirname_template.size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 316 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 317 | char* return_code = mkdtemp(&buf[0]); |
| 318 | TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL); |
| 319 | *dirname = &buf[0]; |
| 320 | return true; |
| 321 | } |
| 322 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 323 | bool StringHasSuffix(const std::string& str, const std::string& suffix) { |
| 324 | if (suffix.size() > str.size()) |
| 325 | return false; |
| 326 | return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); |
| 327 | } |
| 328 | |
| 329 | bool StringHasPrefix(const std::string& str, const std::string& prefix) { |
| 330 | if (prefix.size() > str.size()) |
| 331 | return false; |
| 332 | return 0 == str.compare(0, prefix.size(), prefix); |
| 333 | } |
| 334 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 335 | const string BootDevice() { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 336 | string proc_cmdline; |
| 337 | if (!ReadFileToString("/proc/cmdline", &proc_cmdline)) |
| 338 | return ""; |
| 339 | // look for "root=" in the command line |
| 340 | string::size_type pos = 0; |
| 341 | if (!StringHasPrefix(proc_cmdline, "root=")) { |
| 342 | pos = proc_cmdline.find(" root=") + 1; |
| 343 | } |
| 344 | if (pos == string::npos) { |
| 345 | // can't find root= |
| 346 | return ""; |
| 347 | } |
| 348 | // at this point, pos is the point in the string where "root=" starts |
| 349 | string ret; |
| 350 | pos += strlen("root="); // advance to the device name itself |
| 351 | while (pos < proc_cmdline.size()) { |
| 352 | char c = proc_cmdline[pos]; |
| 353 | if (c == ' ') |
| 354 | break; |
| 355 | ret += c; |
| 356 | pos++; |
| 357 | } |
| 358 | return ret; |
| 359 | // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems |
| 360 | } |
| 361 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 362 | const string BootKernelDevice(const std::string& boot_device) { |
| 363 | // Currntly this assumes the last digit of the boot device is |
| 364 | // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to |
| 365 | // get the kernel device. |
| 366 | string ret = boot_device; |
| 367 | if (ret.empty()) |
| 368 | return ret; |
| 369 | char last_char = ret[ret.size() - 1]; |
| 370 | if (last_char == '3' || last_char == '5' || last_char == '7') { |
| 371 | ret[ret.size() - 1] = last_char - 1; |
| 372 | return ret; |
| 373 | } |
| 374 | return ""; |
| 375 | } |
| 376 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 377 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 378 | const string& mountpoint, |
| 379 | unsigned long mountflags) { |
| 380 | 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] | 381 | if (rc < 0) { |
| 382 | string msg = ErrnoNumberAsString(errno); |
| 383 | LOG(ERROR) << "Unable to mount destination device: " << msg << ". " |
| 384 | << device << " on " << mountpoint; |
| 385 | return false; |
| 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | bool UnmountFilesystem(const string& mountpoint) { |
| 391 | TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0); |
| 392 | return true; |
| 393 | } |
| 394 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 395 | bool GetBootloader(BootLoader* out_bootloader) { |
| 396 | // For now, hardcode to syslinux. |
| 397 | *out_bootloader = BootLoader_SYSLINUX; |
| 398 | return true; |
| 399 | } |
| 400 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 401 | const char* GetGErrorMessage(const GError* error) { |
| 402 | if (!error) |
| 403 | return "Unknown error."; |
| 404 | return error->message; |
| 405 | } |
| 406 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 407 | bool Reboot() { |
| 408 | vector<string> command; |
| 409 | command.push_back("/sbin/shutdown"); |
| 410 | command.push_back("-r"); |
| 411 | command.push_back("now"); |
| 412 | int rc = 0; |
| 413 | Subprocess::SynchronousExec(command, &rc); |
| 414 | TEST_AND_RETURN_FALSE(rc == 0); |
| 415 | return true; |
| 416 | } |
| 417 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 418 | const char* const kStatefulPartition = "/mnt/stateful_partition"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 419 | |
| 420 | } // namespace utils |
| 421 | |
| 422 | } // namespace chromeos_update_engine |