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 // 16 17 #ifndef UPDATE_ENGINE_COMMON_UTILS_H_ 18 #define UPDATE_ENGINE_COMMON_UTILS_H_ 19 20 #include <errno.h> 21 #include <time.h> 22 #include <unistd.h> 23 24 #include <algorithm> 25 #include <limits> 26 #include <map> 27 #include <memory> 28 #include <set> 29 #include <string> 30 #include <vector> 31 32 #include <base/files/file_path.h> 33 #include <base/posix/eintr_wrapper.h> 34 #include <base/time/time.h> 35 #include <brillo/key_value_store.h> 36 #include <brillo/secure_blob.h> 37 38 #include "update_engine/common/action.h" 39 #include "update_engine/common/action_processor.h" 40 #include "update_engine/common/constants.h" 41 #include "update_engine/payload_consumer/file_descriptor.h" 42 #include "update_engine/update_metadata.pb.h" 43 44 namespace chromeos_update_engine { 45 46 namespace utils { 47 48 // Formats |vec_str| as a string of the form ["<elem1>", "<elem2>"]. 49 // Does no escaping, only use this for presentation in error messages. 50 std::string StringVectorToString(const std::vector<std::string>& vec_str); 51 52 // Calculates the p2p file id from payload hash and size 53 std::string CalculateP2PFileId(const brillo::Blob& payload_hash, 54 size_t payload_size); 55 56 // Parse the firmware version from one line of output from the 57 // "mosys" command. 58 std::string ParseECVersion(std::string input_line); 59 60 // Writes the data passed to path. The file at path will be overwritten if it 61 // exists. Returns true on success, false otherwise. 62 bool WriteFile(const char* path, const void* data, size_t data_len); 63 64 // Calls write() or pwrite() repeatedly until all count bytes at buf are 65 // written to fd or an error occurs. Returns true on success. 66 bool WriteAll(int fd, const void* buf, size_t count); 67 bool PWriteAll(int fd, const void* buf, size_t count, off_t offset); 68 69 bool WriteAll(const FileDescriptorPtr& fd, const void* buf, size_t count); 70 bool PWriteAll(const FileDescriptorPtr& fd, 71 const void* buf, 72 size_t count, 73 off_t offset); 74 75 // Calls read() repeatedly until |count| bytes are read or EOF or EWOULDBLOCK 76 // is reached. Returns whether all read() calls succeeded (including EWOULDBLOCK 77 // as a success case), sets |eof| to whether the eof was reached and sets 78 // |out_bytes_read| to the actual number of bytes read regardless of the return 79 // value. 80 bool ReadAll( 81 int fd, void* buf, size_t count, size_t* out_bytes_read, bool* eof); 82 83 // Calls pread() repeatedly until count bytes are read, or EOF is reached. 84 // Returns number of bytes read in *bytes_read. Returns true on success. 85 bool PReadAll( 86 int fd, void* buf, size_t count, off_t offset, ssize_t* out_bytes_read); 87 88 bool PReadAll(const FileDescriptorPtr& fd, 89 void* buf, 90 size_t count, 91 off_t offset, 92 ssize_t* out_bytes_read); 93 94 // Opens |path| for reading and appends its entire content to the container 95 // pointed to by |out_p|. Returns true upon successfully reading all of the 96 // file's content, false otherwise, in which case the state of the output 97 // container is unknown. ReadFileChunk starts reading the file from |offset|; if 98 // |size| is not -1, only up to |size| bytes are read in. 99 bool ReadFile(const std::string& path, brillo::Blob* out_p); 100 bool ReadFile(const std::string& path, std::string* out_p); 101 bool ReadFileChunk(const std::string& path, 102 off_t offset, 103 off_t size, 104 brillo::Blob* out_p); 105 106 // Invokes |cmd| in a pipe and appends its stdout to the container pointed to by 107 // |out_p|. Returns true upon successfully reading all of the output, false 108 // otherwise, in which case the state of the output container is unknown. 109 bool ReadPipe(const std::string& cmd, std::string* out_p); 110 111 // Returns the size of the block device at the file descriptor fd. If an error 112 // occurs, -1 is returned. 113 off_t BlockDevSize(int fd); 114 115 // Returns the size of the file at path, or the file descriptor fd. If the file 116 // is actually a block device, this function will automatically call 117 // BlockDevSize. If the file doesn't exist or some error occurrs, -1 is 118 // returned. 119 off_t FileSize(const std::string& path); 120 off_t FileSize(int fd); 121 122 std::string ErrnoNumberAsString(int err); 123 124 // Returns true if the file exists for sure. Returns false if it doesn't exist, 125 // or an error occurs. 126 bool FileExists(const char* path); 127 128 // Returns true if |path| exists and is a symbolic link. 129 bool IsSymlink(const char* path); 130 131 // Try attaching UBI |volume_num|. If there is any error executing required 132 // commands to attach the volume, this function returns false. This function 133 // only returns true if "/dev/ubi%d_0" becomes available in |timeout| seconds. 134 bool TryAttachingUbiVolume(int volume_num, int timeout); 135 136 // If |base_filename_template| is neither absolute (starts with "/") nor 137 // explicitly relative to the current working directory (starts with "./" or 138 // "../"), then it is prepended the system's temporary directory. On success, 139 // stores the name of the new temporary file in |filename|. If |fd| is 140 // non-null, the file descriptor returned by mkstemp is written to it and 141 // kept open; otherwise, it is closed. The template must end with "XXXXXX". 142 // Returns true on success. 143 bool MakeTempFile(const std::string& base_filename_template, 144 std::string* filename, 145 int* fd); 146 147 // Splits the partition device name into the block device name and partition 148 // number. For example, "/dev/sda3" will be split into {"/dev/sda", 3} and 149 // "/dev/mmcblk0p2" into {"/dev/mmcblk0", 2} 150 // Returns false when malformed device name is passed in. 151 // If both output parameters are omitted (null), can be used 152 // just to test the validity of the device name. Note that the function 153 // simply checks if the device name looks like a valid device, no other 154 // checks are performed (i.e. it doesn't check if the device actually exists). 155 bool SplitPartitionName(const std::string& partition_name, 156 std::string* out_disk_name, 157 int* out_partition_num); 158 159 // Builds a partition device name from the block device name and partition 160 // number. For example: 161 // {"/dev/sda", 1} => "/dev/sda1" 162 // {"/dev/mmcblk2", 12} => "/dev/mmcblk2p12" 163 // Returns empty string when invalid parameters are passed in 164 std::string MakePartitionName(const std::string& disk_name, int partition_num); 165 166 // Similar to "MakePartitionName" but returns a name that is suitable for 167 // mounting. On NAND system we can write to "/dev/ubiX_0", which is what 168 // MakePartitionName returns, but we cannot mount that device. To mount, we 169 // have to use "/dev/ubiblockX_0" for rootfs. Stateful and OEM partitions are 170 // mountable with "/dev/ubiX_0". The input is a partition device such as 171 // /dev/sda3. Return empty string on error. 172 std::string MakePartitionNameForMount(const std::string& part_name); 173 174 // Set the read-only attribute on the block device |device| to the value passed 175 // in |read_only|. Return whether the operation succeeded. 176 bool SetBlockDeviceReadOnly(const std::string& device, bool read_only); 177 178 // Synchronously mount or unmount a filesystem. Return true on success. 179 // When mounting, it will attempt to mount the device as the passed filesystem 180 // type |type|, with the passed |flags| options. If |type| is empty, "ext2", 181 // "ext3", "ext4" and "squashfs" will be tried. 182 bool MountFilesystem(const std::string& device, 183 const std::string& mountpoint, 184 unsigned long flags, // NOLINT(runtime/int) 185 const std::string& type, 186 const std::string& fs_mount_options); 187 bool UnmountFilesystem(const std::string& mountpoint); 188 189 // Return whether the passed |mountpoint| path is a directory where a filesystem 190 // is mounted. Due to detection mechanism limitations, when used on directories 191 // where another part of the tree was bind mounted returns true only if bind 192 // mounted on top of a different filesystem (not inside the same filesystem). 193 bool IsMountpoint(const std::string& mountpoint); 194 195 // Returns a human-readable string with the file format based on magic constants 196 // on the header of the file. 197 std::string GetFileFormat(const std::string& path); 198 199 // Returns the string representation of the given UTC time. 200 // such as "11/14/2011 14:05:30 GMT". 201 std::string ToString(const base::Time utc_time); 202 203 // Returns true or false depending on the value of b. 204 std::string ToString(bool b); 205 206 // Returns a string representation of the given enum. 207 std::string ToString(DownloadSource source); 208 209 // Returns a string representation of the given enum. 210 std::string ToString(PayloadType payload_type); 211 212 // Fuzzes an integer |value| randomly in the range: 213 // [value - range / 2, value + range - range / 2] 214 int FuzzInt(int value, unsigned int range); 215 216 // Log a string in hex to LOG(INFO). Useful for debugging. 217 void HexDumpArray(const uint8_t* const arr, const size_t length); HexDumpString(const std::string & str)218 inline void HexDumpString(const std::string& str) { 219 HexDumpArray(reinterpret_cast<const uint8_t*>(str.data()), str.size()); 220 } HexDumpVector(const brillo::Blob & vect)221 inline void HexDumpVector(const brillo::Blob& vect) { 222 HexDumpArray(vect.data(), vect.size()); 223 } 224 225 template <typename T> VectorIndexOf(const std::vector<T> & vect,const T & value,typename std::vector<T>::size_type * out_index)226 bool VectorIndexOf(const std::vector<T>& vect, 227 const T& value, 228 typename std::vector<T>::size_type* out_index) { 229 typename std::vector<T>::const_iterator it = 230 std::find(vect.begin(), vect.end(), value); 231 if (it == vect.end()) { 232 return false; 233 } else { 234 *out_index = it - vect.begin(); 235 return true; 236 } 237 } 238 239 // Return the total number of blocks in the passed |extents| collection. 240 template <class T> BlocksInExtents(const T & extents)241 uint64_t BlocksInExtents(const T& extents) { 242 uint64_t sum = 0; 243 for (const auto& ext : extents) { 244 sum += ext.num_blocks(); 245 } 246 return sum; 247 } 248 249 // Converts seconds into human readable notation including days, hours, minutes 250 // and seconds. For example, 185 will yield 3m5s, 4300 will yield 1h11m40s, and 251 // 360000 will yield 4d4h0m0s. Zero padding not applied. Seconds are always 252 // shown in the result. 253 std::string FormatSecs(unsigned secs); 254 255 // Converts a TimeDelta into human readable notation including days, hours, 256 // minutes, seconds and fractions of a second down to microsecond granularity, 257 // as necessary; for example, an output of 5d2h0m15.053s means that the input 258 // time was precise to the milliseconds only. Zero padding not applied, except 259 // for fractions. Seconds are always shown, but fractions thereof are only shown 260 // when applicable. If |delta| is negative, the output will have a leading '-' 261 // followed by the absolute duration. 262 std::string FormatTimeDelta(base::TimeDelta delta); 263 264 // This method transforms the given error code to be suitable for UMA and 265 // for error classification purposes by removing the higher order bits and 266 // aggregating error codes beyond the enum range, etc. This method is 267 // idempotent, i.e. if called with a value previously returned by this method, 268 // it'll return the same value again. 269 ErrorCode GetBaseErrorCode(ErrorCode code); 270 271 // Converts |time| to an Omaha InstallDate which is defined as "the 272 // number of PST8PDT calendar weeks since Jan 1st 2007 0:00 PST, times 273 // seven" with PST8PDT defined as "Pacific Time" (e.g. UTC-07:00 if 274 // daylight savings is observed and UTC-08:00 otherwise.) 275 // 276 // If the passed in |time| variable is before Monday January 1st 2007 277 // 0:00 PST, False is returned and the value returned in 278 // |out_num_days| is undefined. Otherwise the number of PST8PDT 279 // calendar weeks since that date times seven is returned in 280 // |out_num_days| and the function returns True. 281 // 282 // (NOTE: This function does not currently take daylight savings time 283 // into account so the result may up to one hour off. This is because 284 // the glibc date and timezone routines depend on the TZ environment 285 // variable and changing environment variables is not thread-safe. 286 bool ConvertToOmahaInstallDate(base::Time time, int* out_num_days); 287 288 // Look for the minor version value in the passed |store| and set 289 // |minor_version| to that value. Return whether the value was found and valid. 290 bool GetMinorVersion(const brillo::KeyValueStore& store, 291 uint32_t* minor_version); 292 293 // This function reads the specified data in |extents| into |out_data|. The 294 // extents are read from the file at |path|. |out_data_size| is the size of 295 // |out_data|. Returns false if the number of bytes to read given in 296 // |extents| does not equal |out_data_size|. 297 bool ReadExtents(const std::string& path, 298 const std::vector<Extent>& extents, 299 brillo::Blob* out_data, 300 ssize_t out_data_size, 301 size_t block_size); 302 303 // Read the current boot identifier and store it in |boot_id|. This identifier 304 // is constants during the same boot of the kernel and is regenerated after 305 // reboot. Returns whether it succeeded getting the boot_id. 306 bool GetBootId(std::string* boot_id); 307 308 // This function gets the file path of the file pointed to by FileDiscriptor. 309 std::string GetFilePath(int fd); 310 311 // Divide |x| by |y| and round up to the nearest integer. DivRoundUp(uint64_t x,uint64_t y)312 constexpr uint64_t DivRoundUp(uint64_t x, uint64_t y) { 313 return (x + y - 1) / y; 314 } 315 316 // Round |x| up to be a multiple of |y|. RoundUp(uint64_t x,uint64_t y)317 constexpr uint64_t RoundUp(uint64_t x, uint64_t y) { 318 return DivRoundUp(x, y) * y; 319 } 320 321 // Returns the integer value of the first section of |version|. E.g. for 322 // "10575.39." returns 10575. Returns 0 if |version| is empty, returns -1 if 323 // first section of |version| is invalid (e.g. not a number). 324 int VersionPrefix(const std::string& version); 325 326 // Parses a string in the form high.low, where high and low are 16 bit unsigned 327 // integers. If there is more than 1 dot, or if either of the two parts are 328 // not valid 16 bit unsigned numbers, then 0xffff is returned for both. 329 void ParseRollbackKeyVersion(const std::string& raw_version, 330 uint16_t* high_version, 331 uint16_t* low_version); 332 333 // Return a string representation of |utime| for log file names. 334 std::string GetTimeAsString(time_t utime); 335 336 } // namespace utils 337 338 // Utility class to close a file descriptor 339 class ScopedFdCloser { 340 public: ScopedFdCloser(int * fd)341 explicit ScopedFdCloser(int* fd) : fd_(fd) {} ~ScopedFdCloser()342 ~ScopedFdCloser() { 343 if (should_close_ && fd_ && (*fd_ >= 0) && !IGNORE_EINTR(close(*fd_))) 344 *fd_ = -1; 345 } set_should_close(bool should_close)346 void set_should_close(bool should_close) { should_close_ = should_close; } 347 348 private: 349 int* fd_; 350 bool should_close_ = true; 351 DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser); 352 }; 353 354 // Utility class to delete a file when it goes out of scope. 355 class ScopedPathUnlinker { 356 public: ScopedPathUnlinker(const std::string & path)357 explicit ScopedPathUnlinker(const std::string& path) 358 : path_(path), should_remove_(true) {} ~ScopedPathUnlinker()359 ~ScopedPathUnlinker() { 360 if (should_remove_ && unlink(path_.c_str()) < 0) { 361 PLOG(ERROR) << "Unable to unlink path " << path_; 362 } 363 } set_should_remove(bool should_remove)364 void set_should_remove(bool should_remove) { should_remove_ = should_remove; } 365 366 private: 367 const std::string path_; 368 bool should_remove_; 369 DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); 370 }; 371 372 // A little object to call ActionComplete on the ActionProcessor when 373 // it's destructed. 374 class ScopedActionCompleter { 375 public: ScopedActionCompleter(ActionProcessor * processor,AbstractAction * action)376 explicit ScopedActionCompleter(ActionProcessor* processor, 377 AbstractAction* action) 378 : processor_(processor), 379 action_(action), 380 code_(ErrorCode::kError), 381 should_complete_(true) { 382 CHECK(processor_); 383 } ~ScopedActionCompleter()384 ~ScopedActionCompleter() { 385 if (should_complete_) 386 processor_->ActionComplete(action_, code_); 387 } set_code(ErrorCode code)388 void set_code(ErrorCode code) { code_ = code; } set_should_complete(bool should_complete)389 void set_should_complete(bool should_complete) { 390 should_complete_ = should_complete; 391 } get_code()392 ErrorCode get_code() const { return code_; } 393 394 private: 395 ActionProcessor* processor_; 396 AbstractAction* action_; 397 ErrorCode code_; 398 bool should_complete_; 399 DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter); 400 }; 401 402 } // namespace chromeos_update_engine 403 404 #define TEST_AND_RETURN_FALSE_ERRNO(_x) \ 405 do { \ 406 bool _success = static_cast<bool>(_x); \ 407 if (!_success) { \ 408 std::string _msg = \ 409 chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ 410 LOG(ERROR) << #_x " failed: " << _msg; \ 411 return false; \ 412 } \ 413 } while (0) 414 415 #define TEST_AND_RETURN_FALSE(_x) \ 416 do { \ 417 bool _success = static_cast<bool>(_x); \ 418 if (!_success) { \ 419 LOG(ERROR) << #_x " failed."; \ 420 return false; \ 421 } \ 422 } while (0) 423 424 #define TEST_AND_RETURN_ERRNO(_x) \ 425 do { \ 426 bool _success = static_cast<bool>(_x); \ 427 if (!_success) { \ 428 std::string _msg = \ 429 chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ 430 LOG(ERROR) << #_x " failed: " << _msg; \ 431 return; \ 432 } \ 433 } while (0) 434 435 #define TEST_AND_RETURN(_x) \ 436 do { \ 437 bool _success = static_cast<bool>(_x); \ 438 if (!_success) { \ 439 LOG(ERROR) << #_x " failed."; \ 440 return; \ 441 } \ 442 } while (0) 443 444 #define TEST_AND_RETURN_FALSE_ERRCODE(_x) \ 445 do { \ 446 errcode_t _error = (_x); \ 447 if (_error) { \ 448 errno = _error; \ 449 LOG(ERROR) << #_x " failed: " << _error; \ 450 return false; \ 451 } \ 452 } while (0) 453 454 #endif // UPDATE_ENGINE_COMMON_UTILS_H_ 455