OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "update_engine/utils.h" | 5 #include "update_engine/utils.h" |
6 | 6 |
7 #include <sys/mount.h> | 7 #include <sys/mount.h> |
8 #include <sys/resource.h> | 8 #include <sys/resource.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <dirent.h> | 11 #include <dirent.h> |
12 #include <errno.h> | 12 #include <errno.h> |
13 #include <fcntl.h> | 13 #include <fcntl.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include <string.h> | 16 #include <string.h> |
17 #include <unistd.h> | 17 #include <unistd.h> |
18 | 18 |
19 #include <algorithm> | 19 #include <algorithm> |
20 | 20 |
21 #include "base/file_path.h" | 21 #include <base/file_path.h> |
22 #include "base/file_util.h" | 22 #include <base/file_util.h> |
23 #include "base/rand_util.h" | 23 #include <base/rand_util.h> |
24 #include "base/string_util.h" | 24 #include <base/string_util.h> |
25 #include "base/logging.h" | 25 #include <base/logging.h> |
| 26 #include <rootdev/rootdev.h> |
| 27 |
26 #include "update_engine/file_writer.h" | 28 #include "update_engine/file_writer.h" |
27 #include "update_engine/omaha_request_params.h" | 29 #include "update_engine/omaha_request_params.h" |
28 #include "update_engine/subprocess.h" | 30 #include "update_engine/subprocess.h" |
29 | 31 |
30 using std::min; | 32 using std::min; |
31 using std::string; | 33 using std::string; |
32 using std::vector; | 34 using std::vector; |
33 | 35 |
34 namespace chromeos_update_engine { | 36 namespace chromeos_update_engine { |
35 | 37 |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 return false; | 358 return false; |
357 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); | 359 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); |
358 } | 360 } |
359 | 361 |
360 bool StringHasPrefix(const std::string& str, const std::string& prefix) { | 362 bool StringHasPrefix(const std::string& str, const std::string& prefix) { |
361 if (prefix.size() > str.size()) | 363 if (prefix.size() > str.size()) |
362 return false; | 364 return false; |
363 return 0 == str.compare(0, prefix.size(), prefix); | 365 return 0 == str.compare(0, prefix.size(), prefix); |
364 } | 366 } |
365 | 367 |
366 const string BootDevice() { | 368 const std::string BootDevice() { |
367 string proc_cmdline; | 369 char boot_path[PATH_MAX]; |
368 if (!ReadFileToString("/proc/cmdline", &proc_cmdline)) | 370 // Resolve the boot device path fully, including dereferencing |
369 return ""; | 371 // through dm-verity. |
370 // look for "root=" in the command line | 372 int ret = rootdev(boot_path, sizeof(boot_path), true, false); |
371 string::size_type pos = 0; | 373 |
372 if (!StringHasPrefix(proc_cmdline, "root=")) { | 374 if (ret < 0) { |
373 pos = proc_cmdline.find(" root=") + 1; | 375 LOG(ERROR) << "rootdev failed to find the root device"; |
374 } | |
375 if (pos == string::npos) { | |
376 // can't find root= | |
377 return ""; | 376 return ""; |
378 } | 377 } |
379 // at this point, pos is the point in the string where "root=" starts | 378 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node"; |
380 string ret; | 379 |
381 pos += strlen("root="); // advance to the device name itself | 380 // This local variable is used to construct the return string and is not |
382 while (pos < proc_cmdline.size()) { | 381 // passed around after use. |
383 char c = proc_cmdline[pos]; | 382 return boot_path; |
384 if (c == ' ') | |
385 break; | |
386 ret += c; | |
387 pos++; | |
388 } | |
389 return ret; | |
390 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems | |
391 } | 383 } |
392 | 384 |
393 const string BootKernelDevice(const std::string& boot_device) { | 385 const string BootKernelDevice(const std::string& boot_device) { |
394 // Currntly this assumes the last digit of the boot device is | 386 // Currntly this assumes the last digit of the boot device is |
395 // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to | 387 // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to |
396 // get the kernel device. | 388 // get the kernel device. |
397 string ret = boot_device; | 389 string ret = boot_device; |
398 if (ret.empty()) | 390 if (ret.empty()) |
399 return ret; | 391 return ret; |
400 char last_char = ret[ret.size() - 1]; | 392 char last_char = ret[ret.size() - 1]; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 int min = value - range / 2; | 454 int min = value - range / 2; |
463 int max = value + range - range / 2; | 455 int max = value + range - range / 2; |
464 return base::RandInt(min, max); | 456 return base::RandInt(min, max); |
465 } | 457 } |
466 | 458 |
467 const char* const kStatefulPartition = "/mnt/stateful_partition"; | 459 const char* const kStatefulPartition = "/mnt/stateful_partition"; |
468 | 460 |
469 } // namespace utils | 461 } // namespace utils |
470 | 462 |
471 } // namespace chromeos_update_engine | 463 } // namespace chromeos_update_engine |
OLD | NEW |