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