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> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 25 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> |
| 30 | #include <sys/resource.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
Yifan Hong | c80de2d | 2020-02-25 17:13:53 -0800 | [diff] [blame] | 33 | #include <time.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 35 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 37 | #include <utility> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 38 | #include <vector> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 39 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 40 | #include <base/callback.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 41 | #include <base/files/file_path.h> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 42 | #include <base/files/file_util.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 43 | #include <base/files/scoped_file.h> |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 44 | #include <base/format_macros.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 45 | #include <base/location.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 46 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 47 | #include <base/posix/eintr_wrapper.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 48 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 49 | #include <base/strings/string_number_conversions.h> |
| 50 | #include <base/strings/string_split.h> |
| 51 | #include <base/strings/string_util.h> |
| 52 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 53 | #include <brillo/data_encoding.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 54 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 55 | #include "update_engine/common/clock_interface.h" |
| 56 | #include "update_engine/common/constants.h" |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 57 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 58 | #include "update_engine/common/prefs_interface.h" |
| 59 | #include "update_engine/common/subprocess.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 60 | #include "update_engine/payload_consumer/file_descriptor.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 61 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 62 | using base::Time; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 63 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 64 | using std::min; |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 65 | using std::numeric_limits; |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 66 | using std::pair; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | using std::string; |
| 68 | using std::vector; |
| 69 | |
| 70 | namespace chromeos_update_engine { |
| 71 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 72 | namespace { |
| 73 | |
| 74 | // The following constants control how UnmountFilesystem should retry if |
| 75 | // umount() fails with an errno EBUSY, i.e. retry 5 times over the course of |
| 76 | // one second. |
| 77 | const int kUnmountMaxNumOfRetries = 5; |
| 78 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 79 | |
| 80 | // Number of bytes to read from a file to attempt to detect its contents. Used |
| 81 | // in GetFileFormat. |
| 82 | const int kGetFileFormatMaxHeaderSize = 32; |
| 83 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 84 | // The path to the kernel's boot_id. |
| 85 | const char kBootIdPath[] = "/proc/sys/kernel/random/boot_id"; |
| 86 | |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 87 | // If |path| is absolute, or explicit relative to the current working directory, |
| 88 | // leaves it as is. Otherwise, uses the system's temp directory, as defined by |
| 89 | // base::GetTempDir() and prepends it to |path|. On success stores the full |
| 90 | // temporary path in |template_path| and returns true. |
| 91 | bool GetTempName(const string& path, base::FilePath* template_path) { |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 92 | if (path[0] == '/' || |
| 93 | base::StartsWith(path, "./", base::CompareCase::SENSITIVE) || |
| 94 | base::StartsWith(path, "../", base::CompareCase::SENSITIVE)) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 95 | *template_path = base::FilePath(path); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | base::FilePath temp_dir; |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 100 | #ifdef __ANDROID__ |
Sen Jiang | c2b3766 | 2019-01-09 16:51:18 -0800 | [diff] [blame] | 101 | temp_dir = base::FilePath(constants::kNonVolatileDirectory).Append("tmp"); |
Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 102 | #else |
Sen Jiang | c2b3766 | 2019-01-09 16:51:18 -0800 | [diff] [blame] | 103 | TEST_AND_RETURN_FALSE(base::GetTempDir(&temp_dir)); |
Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 104 | #endif // __ANDROID__ |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 105 | if (!base::PathExists(temp_dir)) |
| 106 | TEST_AND_RETURN_FALSE(base::CreateDirectory(temp_dir)); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 107 | *template_path = temp_dir.Append(path); |
| 108 | return true; |
| 109 | } |
| 110 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 111 | } // namespace |
| 112 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 113 | namespace utils { |
| 114 | |
Alex Deymo | 335516c | 2016-11-28 13:37:06 -0800 | [diff] [blame] | 115 | bool WriteFile(const char* path, const void* data, size_t data_len) { |
| 116 | int fd = HANDLE_EINTR(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600)); |
| 117 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 118 | ScopedFdCloser fd_closer(&fd); |
| 119 | return WriteAll(fd, data, data_len); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 122 | bool ReadAll( |
| 123 | int fd, void* buf, size_t count, size_t* out_bytes_read, bool* eof) { |
| 124 | char* c_buf = static_cast<char*>(buf); |
| 125 | size_t bytes_read = 0; |
| 126 | *eof = false; |
| 127 | while (bytes_read < count) { |
| 128 | ssize_t rc = HANDLE_EINTR(read(fd, c_buf + bytes_read, count - bytes_read)); |
| 129 | if (rc < 0) { |
| 130 | // EAGAIN and EWOULDBLOCK are normal return values when there's no more |
| 131 | // input and we are in non-blocking mode. |
| 132 | if (errno != EWOULDBLOCK && errno != EAGAIN) { |
| 133 | PLOG(ERROR) << "Error reading fd " << fd; |
| 134 | *out_bytes_read = bytes_read; |
| 135 | return false; |
| 136 | } |
| 137 | break; |
| 138 | } else if (rc == 0) { |
| 139 | // A value of 0 means that we reached EOF and there is nothing else to |
| 140 | // read from this fd. |
| 141 | *eof = true; |
| 142 | break; |
| 143 | } else { |
| 144 | bytes_read += rc; |
| 145 | } |
| 146 | } |
| 147 | *out_bytes_read = bytes_read; |
| 148 | return true; |
| 149 | } |
| 150 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 151 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 152 | const char* c_buf = static_cast<const char*>(buf); |
| 153 | ssize_t bytes_written = 0; |
| 154 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 155 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 156 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 157 | bytes_written += rc; |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 162 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 163 | const char* c_buf = static_cast<const char*>(buf); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 164 | size_t bytes_written = 0; |
| 165 | int num_attempts = 0; |
| 166 | while (bytes_written < count) { |
| 167 | num_attempts++; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 168 | ssize_t rc = pwrite(fd, |
| 169 | c_buf + bytes_written, |
| 170 | count - bytes_written, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 171 | offset + bytes_written); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 172 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. |
| 173 | if (rc < 0) { |
| 174 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 175 | << " bytes_written=" << bytes_written << " count=" << count |
| 176 | << " offset=" << offset; |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 177 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 178 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 179 | bytes_written += rc; |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 184 | bool WriteAll(const FileDescriptorPtr& fd, const void* buf, size_t count) { |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 185 | const char* c_buf = static_cast<const char*>(buf); |
| 186 | ssize_t bytes_written = 0; |
| 187 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 188 | ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written); |
| 189 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 190 | bytes_written += rc; |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 195 | bool PWriteAll(const FileDescriptorPtr& fd, |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 196 | const void* buf, |
| 197 | size_t count, |
| 198 | off_t offset) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 199 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 200 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 201 | return WriteAll(fd, buf, count); |
| 202 | } |
| 203 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 204 | bool PReadAll( |
| 205 | int fd, void* buf, size_t count, off_t offset, ssize_t* out_bytes_read) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 206 | char* c_buf = static_cast<char*>(buf); |
| 207 | ssize_t bytes_read = 0; |
| 208 | while (bytes_read < static_cast<ssize_t>(count)) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 209 | ssize_t rc = |
| 210 | pread(fd, c_buf + bytes_read, count - bytes_read, offset + bytes_read); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 211 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 212 | if (rc == 0) { |
| 213 | break; |
| 214 | } |
| 215 | bytes_read += rc; |
| 216 | } |
| 217 | *out_bytes_read = bytes_read; |
| 218 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 221 | bool PReadAll(const FileDescriptorPtr& fd, |
| 222 | void* buf, |
| 223 | size_t count, |
| 224 | off_t offset, |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 225 | ssize_t* out_bytes_read) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 226 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 227 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 228 | char* c_buf = static_cast<char*>(buf); |
| 229 | ssize_t bytes_read = 0; |
| 230 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 231 | ssize_t rc = fd->Read(c_buf + bytes_read, count - bytes_read); |
| 232 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 233 | if (rc == 0) { |
| 234 | break; |
| 235 | } |
| 236 | bytes_read += rc; |
| 237 | } |
| 238 | *out_bytes_read = bytes_read; |
| 239 | return true; |
| 240 | } |
| 241 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 242 | // Append |nbytes| of content from |buf| to the vector pointed to by either |
| 243 | // |vec_p| or |str_p|. |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 244 | static void AppendBytes(const uint8_t* buf, |
| 245 | size_t nbytes, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 246 | brillo::Blob* vec_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 247 | CHECK(buf); |
| 248 | CHECK(vec_p); |
| 249 | vec_p->insert(vec_p->end(), buf, buf + nbytes); |
| 250 | } |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 251 | static void AppendBytes(const uint8_t* buf, size_t nbytes, string* str_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 252 | CHECK(buf); |
| 253 | CHECK(str_p); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 254 | str_p->append(buf, buf + nbytes); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Reads from an open file |fp|, appending the read content to the container |
| 258 | // 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] | 259 | // file's content, false otherwise. If |size| is not -1, reads up to |size| |
| 260 | // bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 261 | template <class T> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 262 | static bool Read(FILE* fp, off_t size, T* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 263 | CHECK(fp); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 264 | CHECK(size == -1 || size >= 0); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 265 | uint8_t buf[1024]; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 266 | while (size == -1 || size > 0) { |
| 267 | off_t bytes_to_read = sizeof(buf); |
| 268 | if (size > 0 && bytes_to_read > size) { |
| 269 | bytes_to_read = size; |
| 270 | } |
| 271 | size_t nbytes = fread(buf, 1, bytes_to_read, fp); |
| 272 | if (!nbytes) { |
| 273 | break; |
| 274 | } |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 275 | AppendBytes(buf, nbytes, out_p); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 276 | if (size != -1) { |
| 277 | CHECK(size >= static_cast<off_t>(nbytes)); |
| 278 | size -= nbytes; |
| 279 | } |
| 280 | } |
| 281 | if (ferror(fp)) { |
| 282 | return false; |
| 283 | } |
| 284 | return size == 0 || feof(fp); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 287 | // Opens a file |path| for reading and appends its the contents to a container |
| 288 | // |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end |
| 289 | // 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] | 290 | template <class T> |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 291 | static bool ReadFileChunkAndAppend(const string& path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 292 | off_t offset, |
| 293 | off_t size, |
| 294 | T* out_p) { |
| 295 | CHECK_GE(offset, 0); |
| 296 | CHECK(size == -1 || size >= 0); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 297 | base::ScopedFILE fp(fopen(path.c_str(), "r")); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 298 | if (!fp.get()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 299 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 300 | if (offset) { |
| 301 | // Return success without appending any data if a chunk beyond the end of |
| 302 | // the file is requested. |
| 303 | if (offset >= FileSize(path)) { |
| 304 | return true; |
| 305 | } |
| 306 | TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0); |
| 307 | } |
| 308 | return Read(fp.get(), size, out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 311 | // TODO(deymo): This is only used in unittest, but requires the private |
| 312 | // Read<string>() defined here. Expose Read<string>() or move to base/ version. |
| 313 | bool ReadPipe(const string& cmd, string* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 314 | FILE* fp = popen(cmd.c_str(), "r"); |
| 315 | if (!fp) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 316 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 317 | bool success = Read(fp, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 318 | return (success && pclose(fp) >= 0); |
| 319 | } |
| 320 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 321 | bool ReadFile(const string& path, brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 322 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 325 | bool ReadFile(const string& path, string* out_p) { |
| 326 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 329 | bool ReadFileChunk(const string& path, |
| 330 | off_t offset, |
| 331 | off_t size, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 332 | brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 333 | return ReadFileChunkAndAppend(path, offset, size, out_p); |
| 334 | } |
| 335 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 336 | off_t BlockDevSize(int fd) { |
| 337 | uint64_t dev_size; |
| 338 | int rc = ioctl(fd, BLKGETSIZE64, &dev_size); |
| 339 | if (rc == -1) { |
| 340 | dev_size = -1; |
| 341 | PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd; |
| 342 | } |
| 343 | return dev_size; |
| 344 | } |
| 345 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 346 | off_t FileSize(int fd) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 347 | struct stat stbuf; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 348 | int rc = fstat(fd, &stbuf); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 349 | CHECK_EQ(rc, 0); |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 350 | if (rc < 0) { |
| 351 | PLOG(ERROR) << "Error stat-ing " << fd; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 352 | return rc; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 353 | } |
| 354 | if (S_ISREG(stbuf.st_mode)) |
| 355 | return stbuf.st_size; |
| 356 | if (S_ISBLK(stbuf.st_mode)) |
| 357 | return BlockDevSize(fd); |
| 358 | LOG(ERROR) << "Couldn't determine the type of " << fd; |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | off_t FileSize(const string& path) { |
| 363 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 364 | if (fd == -1) { |
| 365 | PLOG(ERROR) << "Error opening " << path; |
| 366 | return fd; |
| 367 | } |
| 368 | off_t size = FileSize(fd); |
| 369 | if (size == -1) |
| 370 | PLOG(ERROR) << "Error getting file size of " << path; |
| 371 | close(fd); |
| 372 | return size; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 375 | void HexDumpArray(const uint8_t* const arr, const size_t length) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 376 | LOG(INFO) << "Logging array of length: " << length; |
| 377 | const unsigned int bytes_per_line = 16; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 378 | for (uint32_t i = 0; i < length; i += bytes_per_line) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 379 | const unsigned int bytes_remaining = length - i; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 380 | const unsigned int bytes_per_this_line = |
| 381 | min(bytes_per_line, bytes_remaining); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 382 | char header[100]; |
| 383 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); |
| 384 | TEST_AND_RETURN(r == 13); |
| 385 | string line = header; |
| 386 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { |
| 387 | char buf[20]; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 388 | uint8_t c = arr[i + j]; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 389 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); |
| 390 | TEST_AND_RETURN(r == 3); |
| 391 | line += buf; |
| 392 | } |
| 393 | LOG(INFO) << line; |
| 394 | } |
| 395 | } |
| 396 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 397 | bool SplitPartitionName(const string& partition_name, |
| 398 | string* out_disk_name, |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 399 | int* out_partition_num) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 400 | if (!base::StartsWith( |
| 401 | partition_name, "/dev/", base::CompareCase::SENSITIVE)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 402 | LOG(ERROR) << "Invalid partition device name: " << partition_name; |
| 403 | return false; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 404 | } |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 405 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 406 | size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789"); |
| 407 | if (last_nondigit_pos == string::npos || |
| 408 | (last_nondigit_pos + 1) == partition_name.size()) { |
| 409 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 410 | return false; |
| 411 | } |
| 412 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 413 | if (out_disk_name) { |
| 414 | // Special case for MMC devices which have the following naming scheme: |
| 415 | // mmcblk0p2 |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 416 | size_t disk_name_len = last_nondigit_pos; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 417 | if (partition_name[last_nondigit_pos] != 'p' || last_nondigit_pos == 0 || |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 418 | !isdigit(partition_name[last_nondigit_pos - 1])) { |
| 419 | disk_name_len++; |
| 420 | } |
| 421 | *out_disk_name = partition_name.substr(0, disk_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (out_partition_num) { |
Brian Norris | 7b428f5 | 2019-06-26 10:03:35 -0700 | [diff] [blame] | 425 | string partition_str = partition_name.substr(last_nondigit_pos + 1); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 426 | *out_partition_num = atoi(partition_str.c_str()); |
| 427 | } |
| 428 | return true; |
| 429 | } |
| 430 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 431 | string MakePartitionName(const string& disk_name, int partition_num) { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 432 | if (partition_num < 1) { |
| 433 | LOG(ERROR) << "Invalid partition number: " << partition_num; |
| 434 | return string(); |
| 435 | } |
| 436 | |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 437 | if (!base::StartsWith(disk_name, "/dev/", base::CompareCase::SENSITIVE)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 438 | LOG(ERROR) << "Invalid disk name: " << disk_name; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 439 | return string(); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 442 | string partition_name = disk_name; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 443 | if (isdigit(partition_name.back())) { |
| 444 | // Special case for devices with names ending with a digit. |
| 445 | // Add "p" to separate the disk name from partition number, |
| 446 | // e.g. "/dev/loop0p2" |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 447 | partition_name += 'p'; |
| 448 | } |
| 449 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 450 | partition_name += std::to_string(partition_num); |
| 451 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 452 | return partition_name; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 455 | string ErrnoNumberAsString(int err) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 456 | char buf[100]; |
| 457 | buf[0] = '\0'; |
| 458 | return strerror_r(err, buf, sizeof(buf)); |
| 459 | } |
| 460 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 461 | bool FileExists(const char* path) { |
| 462 | struct stat stbuf; |
| 463 | return 0 == lstat(path, &stbuf); |
| 464 | } |
| 465 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 466 | bool IsSymlink(const char* path) { |
| 467 | struct stat stbuf; |
| 468 | return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0; |
| 469 | } |
| 470 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 471 | bool MakeTempFile(const string& base_filename_template, |
| 472 | string* filename, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 473 | int* fd) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 474 | base::FilePath filename_template; |
| 475 | TEST_AND_RETURN_FALSE( |
| 476 | GetTempName(base_filename_template, &filename_template)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 477 | DCHECK(filename || fd); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 478 | vector<char> buf(filename_template.value().size() + 1); |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 479 | memcpy(buf.data(), |
| 480 | filename_template.value().data(), |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 481 | filename_template.value().size()); |
| 482 | buf[filename_template.value().size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 483 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 484 | int mkstemp_fd = mkstemp(buf.data()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 485 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 486 | if (filename) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 487 | *filename = buf.data(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 488 | } |
| 489 | if (fd) { |
| 490 | *fd = mkstemp_fd; |
| 491 | } else { |
| 492 | close(mkstemp_fd); |
| 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 497 | bool SetBlockDeviceReadOnly(const string& device, bool read_only) { |
| 498 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY | O_CLOEXEC)); |
| 499 | if (fd < 0) { |
| 500 | PLOG(ERROR) << "Opening block device " << device; |
| 501 | return false; |
| 502 | } |
| 503 | ScopedFdCloser fd_closer(&fd); |
| 504 | // We take no action if not needed. |
| 505 | int read_only_flag; |
| 506 | int expected_flag = read_only ? 1 : 0; |
| 507 | int rc = ioctl(fd, BLKROGET, &read_only_flag); |
| 508 | // In case of failure reading the setting we will try to set it anyway. |
| 509 | if (rc == 0 && read_only_flag == expected_flag) |
| 510 | return true; |
| 511 | |
| 512 | rc = ioctl(fd, BLKROSET, &expected_flag); |
| 513 | if (rc != 0) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 514 | PLOG(ERROR) << "Marking block device " << device |
| 515 | << " as read_only=" << expected_flag; |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | return true; |
| 519 | } |
| 520 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 521 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 522 | const string& mountpoint, |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 523 | unsigned long mountflags, // NOLINT(runtime/int) |
Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 524 | const string& type, |
| 525 | const string& fs_mount_options) { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 526 | vector<const char*> fstypes; |
| 527 | if (type.empty()) { |
| 528 | fstypes = {"ext2", "ext3", "ext4", "squashfs"}; |
| 529 | } else { |
| 530 | fstypes = {type.c_str()}; |
| 531 | } |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 532 | for (const char* fstype : fstypes) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 533 | int rc = mount(device.c_str(), |
| 534 | mountpoint.c_str(), |
| 535 | fstype, |
| 536 | mountflags, |
Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 537 | fs_mount_options.c_str()); |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 538 | if (rc == 0) |
| 539 | return true; |
| 540 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 541 | PLOG(WARNING) << "Unable to mount destination device " << device << " on " |
| 542 | << mountpoint << " as " << fstype; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 543 | } |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 544 | if (!type.empty()) { |
| 545 | LOG(ERROR) << "Unable to mount " << device << " with any supported type"; |
| 546 | } |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 547 | return false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | bool UnmountFilesystem(const string& mountpoint) { |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 551 | int num_retries = 1; |
| 552 | for (;; ++num_retries) { |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 553 | if (umount(mountpoint.c_str()) == 0) |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 554 | return true; |
| 555 | if (errno != EBUSY || num_retries >= kUnmountMaxNumOfRetries) |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 556 | break; |
Alex Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 557 | usleep(kUnmountRetryIntervalInMicroseconds); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 558 | } |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 559 | if (errno == EINVAL) { |
| 560 | LOG(INFO) << "Not a mountpoint: " << mountpoint; |
| 561 | return false; |
| 562 | } |
| 563 | PLOG(WARNING) << "Error unmounting " << mountpoint << " after " << num_retries |
| 564 | << " attempts. Lazy unmounting instead, error was"; |
| 565 | if (umount2(mountpoint.c_str(), MNT_DETACH) != 0) { |
| 566 | PLOG(ERROR) << "Lazy unmount failed"; |
| 567 | return false; |
| 568 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 569 | return true; |
| 570 | } |
| 571 | |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 572 | bool IsMountpoint(const std::string& mountpoint) { |
| 573 | struct stat stdir, stparent; |
| 574 | |
| 575 | // Check whether the passed mountpoint is a directory and the /.. is in the |
| 576 | // same device or not. If mountpoint/.. is in a different device it means that |
| 577 | // there is a filesystem mounted there. If it is not, but they both point to |
| 578 | // the same inode it basically is the special case of /.. pointing to /. This |
| 579 | // test doesn't play well with bind mount but that's out of the scope of what |
| 580 | // we want to detect here. |
| 581 | if (lstat(mountpoint.c_str(), &stdir) != 0) { |
| 582 | PLOG(ERROR) << "Error stat'ing " << mountpoint; |
| 583 | return false; |
| 584 | } |
| 585 | if (!S_ISDIR(stdir.st_mode)) |
| 586 | return false; |
| 587 | |
| 588 | base::FilePath parent(mountpoint); |
| 589 | parent = parent.Append(".."); |
| 590 | if (lstat(parent.value().c_str(), &stparent) != 0) { |
| 591 | PLOG(ERROR) << "Error stat'ing " << parent.value(); |
| 592 | return false; |
| 593 | } |
| 594 | return S_ISDIR(stparent.st_mode) && |
| 595 | (stparent.st_dev != stdir.st_dev || stparent.st_ino == stdir.st_ino); |
| 596 | } |
| 597 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 598 | // Tries to parse the header of an ELF file to obtain a human-readable |
| 599 | // description of it on the |output| string. |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 600 | static bool GetFileFormatELF(const uint8_t* buffer, |
| 601 | size_t size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 602 | string* output) { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 603 | // 0x00: EI_MAG - ELF magic header, 4 bytes. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 604 | if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 605 | return false; |
| 606 | *output = "ELF"; |
| 607 | |
| 608 | // 0x04: EI_CLASS, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 609 | if (size < EI_CLASS + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 610 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 611 | switch (buffer[EI_CLASS]) { |
| 612 | case ELFCLASS32: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 613 | *output += " 32-bit"; |
| 614 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 615 | case ELFCLASS64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 616 | *output += " 64-bit"; |
| 617 | break; |
| 618 | default: |
| 619 | *output += " ?-bit"; |
| 620 | } |
| 621 | |
| 622 | // 0x05: EI_DATA, endianness, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 623 | if (size < EI_DATA + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 624 | return true; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 625 | uint8_t ei_data = buffer[EI_DATA]; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 626 | switch (ei_data) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 627 | case ELFDATA2LSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 628 | *output += " little-endian"; |
| 629 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 630 | case ELFDATA2MSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 631 | *output += " big-endian"; |
| 632 | break; |
| 633 | default: |
| 634 | *output += " ?-endian"; |
| 635 | // Don't parse anything after the 0x10 offset if endianness is unknown. |
| 636 | return true; |
| 637 | } |
| 638 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 639 | const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer); |
| 640 | // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12) |
| 641 | // and size is the same for both 32 and 64 bits. |
| 642 | if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine)) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 643 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 644 | uint16_t e_machine; |
Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 645 | // Fix endianness regardless of the host endianness. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 646 | if (ei_data == ELFDATA2LSB) |
| 647 | e_machine = le16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 648 | else |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 649 | e_machine = be16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 650 | |
| 651 | switch (e_machine) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 652 | case EM_386: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 653 | *output += " x86"; |
| 654 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 655 | case EM_MIPS: |
| 656 | *output += " mips"; |
| 657 | break; |
| 658 | case EM_ARM: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 659 | *output += " arm"; |
| 660 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 661 | case EM_X86_64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 662 | *output += " x86-64"; |
| 663 | break; |
| 664 | default: |
| 665 | *output += " unknown-arch"; |
| 666 | } |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | string GetFileFormat(const string& path) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 671 | brillo::Blob buffer; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 672 | if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer)) |
| 673 | return "File not found."; |
| 674 | |
| 675 | string result; |
| 676 | if (GetFileFormatELF(buffer.data(), buffer.size(), &result)) |
| 677 | return result; |
| 678 | |
| 679 | return "data"; |
| 680 | } |
| 681 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 682 | int FuzzInt(int value, unsigned int range) { |
| 683 | int min = value - range / 2; |
| 684 | int max = value + range - range / 2; |
| 685 | return base::RandInt(min, max); |
| 686 | } |
| 687 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 688 | string FormatSecs(unsigned secs) { |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 689 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 692 | string FormatTimeDelta(TimeDelta delta) { |
David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 693 | string str; |
| 694 | |
| 695 | // Handle negative durations by prefixing with a minus. |
| 696 | if (delta.ToInternalValue() < 0) { |
| 697 | delta *= -1; |
| 698 | str = "-"; |
| 699 | } |
| 700 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 701 | // Canonicalize into days, hours, minutes, seconds and microseconds. |
| 702 | unsigned days = delta.InDays(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 703 | delta -= TimeDelta::FromDays(days); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 704 | unsigned hours = delta.InHours(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 705 | delta -= TimeDelta::FromHours(hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 706 | unsigned mins = delta.InMinutes(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 707 | delta -= TimeDelta::FromMinutes(mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 708 | unsigned secs = delta.InSeconds(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 709 | delta -= TimeDelta::FromSeconds(secs); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 710 | unsigned usecs = delta.InMicroseconds(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 711 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 712 | if (days) |
| 713 | base::StringAppendF(&str, "%ud", days); |
| 714 | if (days || hours) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 715 | base::StringAppendF(&str, "%uh", hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 716 | if (days || hours || mins) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 717 | base::StringAppendF(&str, "%um", mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 718 | base::StringAppendF(&str, "%u", secs); |
| 719 | if (usecs) { |
| 720 | int width = 6; |
| 721 | while ((usecs / 10) * 10 == usecs) { |
| 722 | usecs /= 10; |
| 723 | width--; |
| 724 | } |
| 725 | base::StringAppendF(&str, ".%0*u", width, usecs); |
| 726 | } |
| 727 | base::StringAppendF(&str, "s"); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 728 | return str; |
| 729 | } |
| 730 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 731 | string ToString(const Time utc_time) { |
| 732 | Time::Exploded exp_time; |
| 733 | utc_time.UTCExplode(&exp_time); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 734 | return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT", |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 735 | exp_time.month, |
| 736 | exp_time.day_of_month, |
| 737 | exp_time.year, |
| 738 | exp_time.hour, |
| 739 | exp_time.minute, |
| 740 | exp_time.second); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 741 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 742 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 743 | string ToString(bool b) { |
| 744 | return (b ? "true" : "false"); |
| 745 | } |
| 746 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 747 | string ToString(DownloadSource source) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 748 | switch (source) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 749 | case kDownloadSourceHttpsServer: |
| 750 | return "HttpsServer"; |
| 751 | case kDownloadSourceHttpServer: |
| 752 | return "HttpServer"; |
| 753 | case kDownloadSourceHttpPeer: |
| 754 | return "HttpPeer"; |
| 755 | case kNumDownloadSources: |
| 756 | return "Unknown"; |
| 757 | // Don't add a default case to let the compiler warn about newly added |
| 758 | // download sources which should be added here. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | return "Unknown"; |
| 762 | } |
| 763 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 764 | string ToString(PayloadType payload_type) { |
| 765 | switch (payload_type) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 766 | case kPayloadTypeDelta: |
| 767 | return "Delta"; |
| 768 | case kPayloadTypeFull: |
| 769 | return "Full"; |
| 770 | case kPayloadTypeForcedFull: |
| 771 | return "ForcedFull"; |
| 772 | case kNumPayloadTypes: |
| 773 | return "Unknown"; |
| 774 | // Don't add a default case to let the compiler warn about newly added |
| 775 | // payload types which should be added here. |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | return "Unknown"; |
| 779 | } |
| 780 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 781 | ErrorCode GetBaseErrorCode(ErrorCode code) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 782 | // Ignore the higher order bits in the code by applying the mask as |
| 783 | // we want the enumerations to be in the small contiguous range |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 784 | // with values less than ErrorCode::kUmaReportedMax. |
| 785 | ErrorCode base_code = static_cast<ErrorCode>( |
| 786 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 787 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 788 | // Make additional adjustments required for UMA and error classification. |
| 789 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix |
| 790 | // chromium-os:34369. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 791 | if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 792 | // 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] | 793 | // errors into this one bucket for UMA and error classification purposes. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 794 | LOG(INFO) << "Converting error code " << base_code |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 795 | << " to ErrorCode::kOmahaErrorInHTTPResponse"; |
| 796 | base_code = ErrorCode::kOmahaErrorInHTTPResponse; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 799 | return base_code; |
| 800 | } |
| 801 | |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 802 | string StringVectorToString(const vector<string>& vec_str) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 803 | string str = "["; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 804 | for (vector<string>::const_iterator i = vec_str.begin(); i != vec_str.end(); |
| 805 | ++i) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 806 | if (i != vec_str.begin()) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 807 | str += ", "; |
| 808 | str += '"'; |
| 809 | str += *i; |
| 810 | str += '"'; |
| 811 | } |
| 812 | str += "]"; |
| 813 | return str; |
| 814 | } |
| 815 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 816 | // The P2P file id should be the same for devices running new version and old |
| 817 | // version so that they can share it with each other. The hash in the response |
| 818 | // was base64 encoded, but now that we switched to use "hash_sha256" field which |
| 819 | // is hex encoded, we have to convert them back to base64 for P2P. However, the |
| 820 | // base64 encoded hash was base64 encoded here again historically for some |
| 821 | // reason, so we keep the same behavior here. |
| 822 | string CalculateP2PFileId(const brillo::Blob& payload_hash, |
| 823 | size_t payload_size) { |
| 824 | string encoded_hash = brillo::data_encoding::Base64Encode( |
| 825 | brillo::data_encoding::Base64Encode(payload_hash)); |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 826 | return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s", |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 827 | payload_size, |
| 828 | encoded_hash.c_str()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 831 | bool ConvertToOmahaInstallDate(Time time, int* out_num_days) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 832 | time_t unix_time = time.ToTimeT(); |
| 833 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". |
| 834 | const time_t kOmahaEpoch = 1167638400; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 835 | const int64_t kNumSecondsPerWeek = 7 * 24 * 3600; |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 836 | const int64_t kNumDaysPerWeek = 7; |
| 837 | |
| 838 | time_t omaha_time = unix_time - kOmahaEpoch; |
| 839 | |
| 840 | if (omaha_time < 0) |
| 841 | return false; |
| 842 | |
| 843 | // Note, as per the comment in utils.h we are deliberately not |
| 844 | // handling DST correctly. |
| 845 | |
| 846 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; |
| 847 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; |
| 848 | |
| 849 | return true; |
| 850 | } |
| 851 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 852 | bool GetMinorVersion(const brillo::KeyValueStore& store, |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 853 | uint32_t* minor_version) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 854 | string result; |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 855 | if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) { |
Allie Wood | 425aa97 | 2015-02-17 14:47:17 -0800 | [diff] [blame] | 856 | if (!base::StringToUint(result, minor_version)) { |
| 857 | LOG(ERROR) << "StringToUint failed when parsing delta minor version."; |
| 858 | return false; |
| 859 | } |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 860 | return true; |
| 861 | } |
| 862 | return false; |
| 863 | } |
| 864 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 865 | bool ReadExtents(const string& path, |
| 866 | const vector<Extent>& extents, |
| 867 | brillo::Blob* out_data, |
| 868 | ssize_t out_data_size, |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 869 | size_t block_size) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 870 | brillo::Blob data(out_data_size); |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 871 | ssize_t bytes_read = 0; |
| 872 | int fd = open(path.c_str(), O_RDONLY); |
| 873 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 874 | ScopedFdCloser fd_closer(&fd); |
| 875 | |
Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 876 | for (const Extent& extent : extents) { |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 877 | ssize_t bytes_read_this_iteration = 0; |
| 878 | ssize_t bytes = extent.num_blocks() * block_size; |
| 879 | TEST_AND_RETURN_FALSE(bytes_read + bytes <= out_data_size); |
| 880 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
| 881 | &data[bytes_read], |
| 882 | bytes, |
| 883 | extent.start_block() * block_size, |
| 884 | &bytes_read_this_iteration)); |
| 885 | TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes); |
| 886 | bytes_read += bytes_read_this_iteration; |
| 887 | } |
| 888 | TEST_AND_RETURN_FALSE(out_data_size == bytes_read); |
| 889 | *out_data = data; |
| 890 | return true; |
| 891 | } |
| 892 | |
Matt Ziegelbaum | 91ba9be | 2020-06-10 16:56:40 -0400 | [diff] [blame] | 893 | bool GetVpdValue(string key, string* result) { |
| 894 | int exit_code = 0; |
| 895 | string value, error; |
| 896 | vector<string> cmd = {"vpd_get_value", key}; |
| 897 | if (!chromeos_update_engine::Subprocess::SynchronousExec( |
| 898 | cmd, &exit_code, &value, &error) || |
| 899 | exit_code) { |
| 900 | LOG(ERROR) << "Failed to get vpd key for " << value |
| 901 | << " with exit code: " << exit_code << " and error: " << error; |
| 902 | return false; |
| 903 | } else if (!error.empty()) { |
| 904 | LOG(INFO) << "vpd_get_value succeeded but with following errors: " << error; |
| 905 | } |
| 906 | |
| 907 | base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value); |
| 908 | *result = value; |
| 909 | return true; |
| 910 | } |
| 911 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 912 | bool GetBootId(string* boot_id) { |
| 913 | TEST_AND_RETURN_FALSE( |
| 914 | base::ReadFileToString(base::FilePath(kBootIdPath), boot_id)); |
| 915 | base::TrimWhitespaceASCII(*boot_id, base::TRIM_TRAILING, boot_id); |
| 916 | return true; |
| 917 | } |
| 918 | |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 919 | int VersionPrefix(const std::string& version) { |
| 920 | if (version.empty()) { |
| 921 | return 0; |
| 922 | } |
| 923 | vector<string> tokens = base::SplitString( |
| 924 | version, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 925 | int value; |
| 926 | if (tokens.empty() || !base::StringToInt(tokens[0], &value)) |
| 927 | return -1; // Target version is invalid. |
| 928 | return value; |
| 929 | } |
| 930 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 931 | void ParseRollbackKeyVersion(const string& raw_version, |
| 932 | uint16_t* high_version, |
| 933 | uint16_t* low_version) { |
| 934 | DCHECK(high_version); |
| 935 | DCHECK(low_version); |
| 936 | *high_version = numeric_limits<uint16_t>::max(); |
| 937 | *low_version = numeric_limits<uint16_t>::max(); |
| 938 | |
| 939 | vector<string> parts = base::SplitString( |
| 940 | raw_version, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 941 | if (parts.size() != 2) { |
| 942 | // The version string must have exactly one period. |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | int high; |
| 947 | int low; |
| 948 | if (!(base::StringToInt(parts[0], &high) && |
| 949 | base::StringToInt(parts[1], &low))) { |
| 950 | // Both parts of the version could not be parsed correctly. |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | if (high >= 0 && high < numeric_limits<uint16_t>::max() && low >= 0 && |
| 955 | low < numeric_limits<uint16_t>::max()) { |
| 956 | *high_version = static_cast<uint16_t>(high); |
| 957 | *low_version = static_cast<uint16_t>(low); |
| 958 | } |
| 959 | } |
| 960 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 961 | string GetFilePath(int fd) { |
| 962 | base::FilePath proc("/proc/self/fd/" + std::to_string(fd)); |
| 963 | base::FilePath file_name; |
| 964 | |
| 965 | if (!base::ReadSymbolicLink(proc, &file_name)) { |
| 966 | return "not found"; |
| 967 | } |
| 968 | return file_name.value(); |
| 969 | } |
| 970 | |
Yifan Hong | c80de2d | 2020-02-25 17:13:53 -0800 | [diff] [blame] | 971 | string GetTimeAsString(time_t utime) { |
| 972 | struct tm tm; |
| 973 | CHECK_EQ(localtime_r(&utime, &tm), &tm); |
| 974 | char str[16]; |
| 975 | CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u); |
| 976 | return str; |
| 977 | } |
| 978 | |
Jae Hoon Kim | 694eeb0 | 2020-06-01 14:24:08 -0700 | [diff] [blame] | 979 | string GetExclusionName(const string& str_to_convert) { |
| 980 | return base::NumberToString(base::StringPieceHash()(str_to_convert)); |
| 981 | } |
| 982 | |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 983 | static bool ParseTimestamp(const std::string& str, int64_t* out) { |
| 984 | if (!base::StringToInt64(str, out)) { |
| 985 | LOG(WARNING) << "Invalid timestamp: " << str; |
| 986 | return false; |
| 987 | } |
| 988 | return true; |
| 989 | } |
| 990 | |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 991 | ErrorCode IsTimestampNewer(const std::string& old_version, |
| 992 | const std::string& new_version) { |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 993 | if (old_version.empty() || new_version.empty()) { |
| 994 | LOG(WARNING) |
| 995 | << "One of old/new timestamp is empty, permit update anyway. Old: " |
| 996 | << old_version << " New: " << new_version; |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 997 | return ErrorCode::kSuccess; |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 998 | } |
| 999 | int64_t old_ver = 0; |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1000 | if (!ParseTimestamp(old_version, &old_ver)) { |
| 1001 | return ErrorCode::kError; |
| 1002 | } |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1003 | int64_t new_ver = 0; |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1004 | if (!ParseTimestamp(new_version, &new_ver)) { |
| 1005 | return ErrorCode::kDownloadManifestParseError; |
| 1006 | } |
| 1007 | if (old_ver > new_ver) { |
| 1008 | return ErrorCode::kPayloadTimestampError; |
| 1009 | } |
| 1010 | return ErrorCode::kSuccess; |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1013 | } // namespace utils |
| 1014 | |
| 1015 | } // namespace chromeos_update_engine |