Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
| 7 | |
Andrew de los Reyes | 81ebcd8 | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 8 | #include <algorithm> |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 9 | #include <errno.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 12 | #include <unistd.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include <vector> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 14 | |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 15 | #include <base/posix/eintr_wrapper.h> |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 16 | #include <base/time.h> |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 17 | #include <ext2fs/ext2fs.h> |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 18 | #include <glib.h> |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 19 | #include "metrics/metrics_library.h" |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 20 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include "update_engine/action.h" |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 22 | #include "update_engine/constants.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 23 | #include "update_engine/action_processor.h" |
| 24 | |
| 25 | namespace chromeos_update_engine { |
| 26 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 27 | class SystemState; |
| 28 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 29 | namespace utils { |
| 30 | |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 31 | // Returns true if this is an official Chrome OS build, false otherwise. |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 32 | bool IsOfficialBuild(); |
| 33 | |
Darin Petkov | 44d98d9 | 2011-03-21 16:08:11 -0700 | [diff] [blame] | 34 | // Returns true if the boot mode is normal or if it's unable to determine the |
| 35 | // boot mode. Returns false if the boot mode is developer. |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 36 | bool IsNormalBootMode(); |
| 37 | |
Darin Petkov | f2065b4 | 2011-05-17 16:36:27 -0700 | [diff] [blame] | 38 | // Returns the HWID or an empty string on error. |
| 39 | std::string GetHardwareClass(); |
| 40 | |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame^] | 41 | // Returns the firmware version or an empty string if the system is not running |
| 42 | // chrome os firmware. |
| 43 | std::string GetFirmwareVersion(); |
| 44 | |
| 45 | // Returns the ec version or an empty string if the system is not running a |
| 46 | // custom chrome os ec. If input_line is not NULL, reads from this line, |
| 47 | // otherwise polls the system for the input line. input_line should contain |
| 48 | // fw_version=value. |
| 49 | std::string GetECVersion(const char* input_line); |
| 50 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 51 | // Writes the data passed to path. The file at path will be overwritten if it |
| 52 | // exists. Returns true on success, false otherwise. |
| 53 | bool WriteFile(const char* path, const char* data, int data_len); |
| 54 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 55 | // Calls write() or pwrite() repeatedly until all count bytes at buf are |
| 56 | // written to fd or an error occurs. Returns true on success. |
| 57 | bool WriteAll(int fd, const void* buf, size_t count); |
| 58 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset); |
| 59 | |
| 60 | // Calls pread() repeatedly until count bytes are read, or EOF is reached. |
| 61 | // Returns number of bytes read in *bytes_read. Returns true on success. |
| 62 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 63 | ssize_t* out_bytes_read); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 64 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 65 | // Opens |path| for reading and appends its entire content to the container |
| 66 | // pointed to by |out_p|. Returns true upon successfully reading all of the |
| 67 | // file's content, false otherwise, in which case the state of the output |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 68 | // container is unknown. ReadFileChunk starts reading the file from |offset|; if |
| 69 | // |size| is not -1, only up to |size| bytes are read in. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 70 | bool ReadFile(const std::string& path, std::vector<char>* out_p); |
| 71 | bool ReadFile(const std::string& path, std::string* out_p); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 72 | bool ReadFileChunk(const std::string& path, off_t offset, off_t size, |
| 73 | std::vector<char>* out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 74 | |
| 75 | // Invokes |cmd| in a pipe and appends its stdout to the container pointed to by |
| 76 | // |out_p|. Returns true upon successfully reading all of the output, false |
| 77 | // otherwise, in which case the state of the output container is unknown. |
| 78 | bool ReadPipe(const std::string& cmd, std::vector<char>* out_p); |
| 79 | bool ReadPipe(const std::string& cmd, std::string* out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 80 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 81 | // Returns the size of the file at path. If the file doesn't exist or some |
| 82 | // error occurrs, -1 is returned. |
| 83 | off_t FileSize(const std::string& path); |
| 84 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 85 | std::string ErrnoNumberAsString(int err); |
| 86 | |
| 87 | // Strips duplicate slashes, and optionally removes all trailing slashes. |
| 88 | // Does not compact /./ or /../. |
| 89 | std::string NormalizePath(const std::string& path, bool strip_trailing_slash); |
| 90 | |
| 91 | // Returns true if the file exists for sure. Returns false if it doesn't exist, |
| 92 | // or an error occurs. |
| 93 | bool FileExists(const char* path); |
| 94 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 95 | // Returns true if |path| exists and is a symbolic link. |
| 96 | bool IsSymlink(const char* path); |
| 97 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 98 | // The last 6 chars of path must be XXXXXX. They will be randomly changed |
| 99 | // and a non-existent path will be returned. Intentionally makes a copy |
| 100 | // of the string passed in. |
| 101 | // NEVER CALL THIS FUNCTION UNLESS YOU ARE SURE |
| 102 | // THAT YOUR PROCESS WILL BE THE ONLY THING WRITING FILES IN THIS DIRECTORY. |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 103 | std::string TempFilename(std::string path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 104 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 105 | // Calls mkstemp() with the template passed. Returns the filename in the |
| 106 | // out param filename. If fd is non-NULL, the file fd returned by mkstemp |
| 107 | // is not close()d and is returned in the out param 'fd'. However, if |
| 108 | // fd is NULL, the fd from mkstemp() will be closed. |
| 109 | // The last six chars of the template must be XXXXXX. |
| 110 | // Returns true on success. |
| 111 | bool MakeTempFile(const std::string& filename_template, |
| 112 | std::string* filename, |
| 113 | int* fd); |
| 114 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 115 | // Calls mkdtemp() with the template passed. Returns the generated dirname |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 116 | // in the dirname param. Returns TRUE on success. dirname must not be NULL. |
| 117 | bool MakeTempDirectory(const std::string& dirname_template, |
| 118 | std::string* dirname); |
| 119 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 120 | // Deletes a directory and all its contents synchronously. Returns true |
| 121 | // on success. This may be called with a regular file--it will just unlink it. |
| 122 | // This WILL cross filesystem boundaries. |
| 123 | bool RecursiveUnlinkDir(const std::string& path); |
| 124 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 125 | // Returns the root device for a partition. For example, |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 126 | // RootDevice("/dev/sda3") returns "/dev/sda". Returns an empty string |
| 127 | // if the input device is not of the "/dev/xyz" form. |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 128 | std::string RootDevice(const std::string& partition_device); |
| 129 | |
| 130 | // Returns the partition number, as a string, of partition_device. For example, |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 131 | // PartitionNumber("/dev/sda3") returns "3". |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 132 | std::string PartitionNumber(const std::string& partition_device); |
| 133 | |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 134 | // Returns the sysfs block device for a root block device. For |
| 135 | // example, SysfsBlockDevice("/dev/sda") returns |
| 136 | // "/sys/block/sda". Returns an empty string if the input device is |
| 137 | // not of the "/dev/xyz" form. |
| 138 | std::string SysfsBlockDevice(const std::string& device); |
| 139 | |
| 140 | // Returns true if the root |device| (e.g., "/dev/sdb") is known to be |
| 141 | // removable, false otherwise. |
| 142 | bool IsRemovableDevice(const std::string& device); |
| 143 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 144 | // Synchronously mount or unmount a filesystem. Return true on success. |
| 145 | // Mounts as ext3 with default options. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 146 | bool MountFilesystem(const std::string& device, const std::string& mountpoint, |
| 147 | unsigned long flags); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 148 | bool UnmountFilesystem(const std::string& mountpoint); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 149 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 150 | // Returns the block count and the block byte size of the ext3 file system on |
| 151 | // |device| (which may be a real device or a path to a filesystem image) or on |
| 152 | // an opened file descriptor |fd|. The actual file-system size is |block_count| |
| 153 | // * |block_size| bytes. Returns true on success, false otherwise. |
| 154 | bool GetFilesystemSize(const std::string& device, |
| 155 | int* out_block_count, |
| 156 | int* out_block_size); |
| 157 | bool GetFilesystemSizeFromFD(int fd, |
| 158 | int* out_block_count, |
| 159 | int* out_block_size); |
| 160 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 161 | // Returns the string representation of the given UTC time. |
| 162 | // such as "11/14/2011 14:05:30 GMT". |
| 163 | std::string ToString(const base::Time utc_time); |
| 164 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 165 | // Returns true or false depending on the value of b. |
| 166 | std::string ToString(bool b); |
| 167 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 168 | // Returns a string representation of the given enum. |
| 169 | std::string ToString(DownloadSource source); |
| 170 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 171 | enum BootLoader { |
| 172 | BootLoader_SYSLINUX = 0, |
| 173 | BootLoader_CHROME_FIRMWARE = 1 |
| 174 | }; |
| 175 | // Detects which bootloader this system uses and returns it via the out |
| 176 | // param. Returns true on success. |
| 177 | bool GetBootloader(BootLoader* out_bootloader); |
| 178 | |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 179 | // Returns the error message, if any, from a GError pointer. Frees the GError |
| 180 | // object and resets error to NULL. |
| 181 | std::string GetAndFreeGError(GError** error); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 182 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 183 | // Initiates a system reboot. Returns true on success, false otherwise. |
| 184 | bool Reboot(); |
| 185 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 186 | // Schedules a Main Loop callback to trigger the crash reporter to perform an |
| 187 | // upload as if this process had crashed. |
| 188 | void ScheduleCrashReporterUpload(); |
| 189 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 190 | // Fuzzes an integer |value| randomly in the range: |
| 191 | // [value - range / 2, value + range - range / 2] |
| 192 | int FuzzInt(int value, unsigned int range); |
| 193 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 194 | // Log a string in hex to LOG(INFO). Useful for debugging. |
| 195 | void HexDumpArray(const unsigned char* const arr, const size_t length); |
| 196 | inline void HexDumpString(const std::string& str) { |
| 197 | HexDumpArray(reinterpret_cast<const unsigned char*>(str.data()), str.size()); |
| 198 | } |
| 199 | inline void HexDumpVector(const std::vector<char>& vect) { |
| 200 | HexDumpArray(reinterpret_cast<const unsigned char*>(&vect[0]), vect.size()); |
| 201 | } |
| 202 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 203 | bool StringHasSuffix(const std::string& str, const std::string& suffix); |
| 204 | bool StringHasPrefix(const std::string& str, const std::string& prefix); |
| 205 | |
| 206 | template<typename KeyType, typename ValueType> |
| 207 | bool MapContainsKey(const std::map<KeyType, ValueType>& m, const KeyType& k) { |
| 208 | return m.find(k) != m.end(); |
| 209 | } |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 210 | template<typename KeyType> |
| 211 | bool SetContainsKey(const std::set<KeyType>& s, const KeyType& k) { |
| 212 | return s.find(k) != s.end(); |
| 213 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 214 | |
| 215 | template<typename ValueType> |
| 216 | std::set<ValueType> SetWithValue(const ValueType& value) { |
| 217 | std::set<ValueType> ret; |
| 218 | ret.insert(value); |
| 219 | return ret; |
| 220 | } |
| 221 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 222 | template<typename T> |
| 223 | bool VectorContainsValue(const std::vector<T>& vect, const T& value) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 224 | return std::find(vect.begin(), vect.end(), value) != vect.end(); |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 227 | template<typename T> |
| 228 | bool VectorIndexOf(const std::vector<T>& vect, const T& value, |
| 229 | typename std::vector<T>::size_type* out_index) { |
| 230 | typename std::vector<T>::const_iterator it = std::find(vect.begin(), |
| 231 | vect.end(), |
| 232 | value); |
| 233 | if (it == vect.end()) { |
| 234 | return false; |
| 235 | } else { |
| 236 | *out_index = it - vect.begin(); |
| 237 | return true; |
| 238 | } |
| 239 | } |
| 240 | |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 241 | template<typename ValueType> |
| 242 | void ApplyMap(std::vector<ValueType>* collection, |
| 243 | const std::map<ValueType, ValueType>& the_map) { |
| 244 | for (typename std::vector<ValueType>::iterator it = collection->begin(); |
| 245 | it != collection->end(); ++it) { |
| 246 | typename std::map<ValueType, ValueType>::const_iterator map_it = |
| 247 | the_map.find(*it); |
| 248 | if (map_it != the_map.end()) { |
| 249 | *it = map_it->second; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 254 | // Returns the currently booted device. "/dev/sda3", for example. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 255 | // This will not interpret LABEL= or UUID=. You'll need to use findfs |
| 256 | // or something with equivalent funcionality to interpret those. |
| 257 | const std::string BootDevice(); |
| 258 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 259 | // Returns the currently booted kernel device, "dev/sda2", for example. |
| 260 | // Client must pass in the boot device. The suggested calling convention |
| 261 | // is: BootKernelDevice(BootDevice()). |
| 262 | // This function works by doing string modification on boot_device. |
| 263 | // Returns empty string on failure. |
| 264 | const std::string BootKernelDevice(const std::string& boot_device); |
| 265 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 266 | // Cgroups cpu shares constants. 1024 is the default shares a standard process |
| 267 | // gets and 2 is the minimum value. We set High as a value that gives the |
| 268 | // update-engine 2x the cpu share of a standard process. |
| 269 | enum CpuShares { |
| 270 | kCpuSharesHigh = 2048, |
| 271 | kCpuSharesNormal = 1024, |
| 272 | kCpuSharesLow = 2, |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 275 | // Compares cpu shares and returns an integer that is less |
| 276 | // than, equal to or greater than 0 if |shares_lhs| is, |
| 277 | // respectively, lower than, same as or higher than |shares_rhs|. |
| 278 | int CompareCpuShares(CpuShares shares_lhs, |
| 279 | CpuShares shares_rhs); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 280 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 281 | // Sets the current process shares to |shares|. Returns true on |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 282 | // success, false otherwise. |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 283 | bool SetCpuShares(CpuShares shares); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 284 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 285 | // Assumes data points to a Closure. Runs it and returns FALSE; |
| 286 | gboolean GlibRunClosure(gpointer data); |
| 287 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 288 | // Converts seconds into human readable notation including days, hours, minutes |
| 289 | // and seconds. For example, 185 will yield 3m5s, 4300 will yield 1h11m40s, and |
| 290 | // 360000 will yield 4d4h0m0s. Zero padding not applied. Seconds are always |
| 291 | // shown in the result. |
| 292 | std::string FormatSecs(unsigned secs); |
| 293 | |
| 294 | // Converts a TimeDelta into human readable notation including days, hours, |
| 295 | // minutes, seconds and fractions of a second down to microsecond granularity, |
| 296 | // as necessary; for example, an output of 5d2h0m15.053s means that the input |
| 297 | // time was precise to the milliseconds only. Zero padding not applied, except |
| 298 | // for fractions. Seconds are always shown, but fractions thereof are only shown |
| 299 | // when applicable. |
| 300 | std::string FormatTimeDelta(base::TimeDelta delta); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 301 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 302 | // This method transforms the given error code to be suitable for UMA and |
| 303 | // for error classification purposes by removing the higher order bits and |
| 304 | // aggregating error codes beyond the enum range, etc. This method is |
| 305 | // idempotent, i.e. if called with a value previously returned by this method, |
| 306 | // it'll return the same value again. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 307 | ErrorCode GetBaseErrorCode(ErrorCode code); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 308 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 309 | // Sends the error code to UMA using the metrics interface object in the given |
| 310 | // system state. It also uses the system state to determine the right UMA |
| 311 | // bucket for the error code. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 312 | void SendErrorCodeToUma(SystemState* system_state, ErrorCode code); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 313 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 314 | // Returns a string representation of the ErrorCodes (either the base |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 315 | // error codes or the bit flags) for logging purposes. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 316 | std::string CodeToString(ErrorCode code); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 317 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 318 | // Creates the powerwash marker file with the appropriate commands in it. |
| 319 | // Returns true if successfully created. False otherwise. |
| 320 | bool CreatePowerwashMarkerFile(); |
| 321 | |
| 322 | // Deletes the marker file used to trigger Powerwash using clobber-state. |
| 323 | // Returns true if successfully deleted. False otherwise. |
| 324 | bool DeletePowerwashMarkerFile(); |
| 325 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 326 | } // namespace utils |
| 327 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 328 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 329 | // Class to unmount FS when object goes out of scope |
| 330 | class ScopedFilesystemUnmounter { |
| 331 | public: |
| 332 | explicit ScopedFilesystemUnmounter(const std::string& mountpoint) |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 333 | : mountpoint_(mountpoint), |
| 334 | should_unmount_(true) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 335 | ~ScopedFilesystemUnmounter() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 336 | if (should_unmount_) { |
| 337 | utils::UnmountFilesystem(mountpoint_); |
| 338 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 339 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 340 | void set_should_unmount(bool unmount) { should_unmount_ = unmount; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 341 | private: |
| 342 | const std::string mountpoint_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 343 | bool should_unmount_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 344 | DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | // Utility class to close a file descriptor |
| 348 | class ScopedFdCloser { |
| 349 | public: |
| 350 | explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 351 | ~ScopedFdCloser() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 352 | if (should_close_ && fd_ && (*fd_ >= 0)) { |
Gilad Arnold | f960911 | 2012-02-29 11:38:47 -0800 | [diff] [blame] | 353 | if (!close(*fd_)) |
| 354 | *fd_ = -1; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 357 | void set_should_close(bool should_close) { should_close_ = should_close; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 358 | private: |
| 359 | int* fd_; |
| 360 | bool should_close_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 361 | DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser); |
| 362 | }; |
| 363 | |
Gilad Arnold | f960911 | 2012-02-29 11:38:47 -0800 | [diff] [blame] | 364 | // An EINTR-immune file descriptor closer. |
| 365 | class ScopedEintrSafeFdCloser { |
| 366 | public: |
| 367 | explicit ScopedEintrSafeFdCloser(int* fd) : fd_(fd), should_close_(true) {} |
| 368 | ~ScopedEintrSafeFdCloser() { |
| 369 | if (should_close_ && fd_ && (*fd_ >= 0)) { |
| 370 | if (!HANDLE_EINTR(close(*fd_))) |
| 371 | *fd_ = -1; |
| 372 | } |
| 373 | } |
| 374 | void set_should_close(bool should_close) { should_close_ = should_close; } |
| 375 | private: |
| 376 | int* fd_; |
| 377 | bool should_close_; |
| 378 | DISALLOW_COPY_AND_ASSIGN(ScopedEintrSafeFdCloser); |
| 379 | }; |
| 380 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 381 | // Utility class to close a file system |
| 382 | class ScopedExt2fsCloser { |
| 383 | public: |
| 384 | explicit ScopedExt2fsCloser(ext2_filsys filsys) : filsys_(filsys) {} |
| 385 | ~ScopedExt2fsCloser() { ext2fs_close(filsys_); } |
| 386 | |
| 387 | private: |
| 388 | ext2_filsys filsys_; |
| 389 | DISALLOW_COPY_AND_ASSIGN(ScopedExt2fsCloser); |
| 390 | }; |
| 391 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 392 | // Utility class to delete a file when it goes out of scope. |
| 393 | class ScopedPathUnlinker { |
| 394 | public: |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 395 | explicit ScopedPathUnlinker(const std::string& path) |
| 396 | : path_(path), |
| 397 | should_remove_(true) {} |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 398 | ~ScopedPathUnlinker() { |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 399 | if (should_remove_ && unlink(path_.c_str()) < 0) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 400 | std::string err_message = strerror(errno); |
| 401 | LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message; |
| 402 | } |
| 403 | } |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 404 | void set_should_remove(bool should_remove) { should_remove_ = should_remove; } |
| 405 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 406 | private: |
| 407 | const std::string path_; |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 408 | bool should_remove_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 409 | DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); |
| 410 | }; |
| 411 | |
| 412 | // Utility class to delete an empty directory when it goes out of scope. |
| 413 | class ScopedDirRemover { |
| 414 | public: |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 415 | explicit ScopedDirRemover(const std::string& path) |
| 416 | : path_(path), |
| 417 | should_remove_(true) {} |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 418 | ~ScopedDirRemover() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 419 | if (should_remove_ && (rmdir(path_.c_str()) < 0)) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 420 | PLOG(ERROR) << "Unable to remove dir " << path_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | void set_should_remove(bool should_remove) { should_remove_ = should_remove; } |
| 424 | |
| 425 | protected: |
| 426 | const std::string path_; |
| 427 | |
| 428 | private: |
| 429 | bool should_remove_; |
| 430 | DISALLOW_COPY_AND_ASSIGN(ScopedDirRemover); |
| 431 | }; |
| 432 | |
| 433 | // Utility class to unmount a filesystem mounted on a temporary directory and |
| 434 | // delete the temporary directory when it goes out of scope. |
| 435 | class ScopedTempUnmounter : public ScopedDirRemover { |
| 436 | public: |
| 437 | explicit ScopedTempUnmounter(const std::string& path) : |
| 438 | ScopedDirRemover(path) {} |
| 439 | ~ScopedTempUnmounter() { |
| 440 | utils::UnmountFilesystem(path_); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 441 | } |
| 442 | private: |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 443 | DISALLOW_COPY_AND_ASSIGN(ScopedTempUnmounter); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | // A little object to call ActionComplete on the ActionProcessor when |
| 447 | // it's destructed. |
| 448 | class ScopedActionCompleter { |
| 449 | public: |
| 450 | explicit ScopedActionCompleter(ActionProcessor* processor, |
| 451 | AbstractAction* action) |
| 452 | : processor_(processor), |
| 453 | action_(action), |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 454 | code_(kErrorCodeError), |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 455 | should_complete_(true) {} |
| 456 | ~ScopedActionCompleter() { |
| 457 | if (should_complete_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 458 | processor_->ActionComplete(action_, code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 459 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 460 | void set_code(ErrorCode code) { code_ = code; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 461 | void set_should_complete(bool should_complete) { |
| 462 | should_complete_ = should_complete; |
| 463 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 464 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 465 | private: |
| 466 | ActionProcessor* processor_; |
| 467 | AbstractAction* action_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 468 | ErrorCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 469 | bool should_complete_; |
| 470 | DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter); |
| 471 | }; |
| 472 | |
| 473 | } // namespace chromeos_update_engine |
| 474 | |
| 475 | #define TEST_AND_RETURN_FALSE_ERRNO(_x) \ |
| 476 | do { \ |
| 477 | bool _success = (_x); \ |
| 478 | if (!_success) { \ |
| 479 | std::string _msg = \ |
| 480 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 481 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 482 | return false; \ |
| 483 | } \ |
| 484 | } while (0) |
| 485 | |
| 486 | #define TEST_AND_RETURN_FALSE(_x) \ |
| 487 | do { \ |
| 488 | bool _success = (_x); \ |
| 489 | if (!_success) { \ |
| 490 | LOG(ERROR) << #_x " failed."; \ |
| 491 | return false; \ |
| 492 | } \ |
| 493 | } while (0) |
| 494 | |
| 495 | #define TEST_AND_RETURN_ERRNO(_x) \ |
| 496 | do { \ |
| 497 | bool _success = (_x); \ |
| 498 | if (!_success) { \ |
| 499 | std::string _msg = \ |
| 500 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 501 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 502 | return; \ |
| 503 | } \ |
| 504 | } while (0) |
| 505 | |
| 506 | #define TEST_AND_RETURN(_x) \ |
| 507 | do { \ |
| 508 | bool _success = (_x); \ |
| 509 | if (!_success) { \ |
| 510 | LOG(ERROR) << #_x " failed."; \ |
| 511 | return; \ |
| 512 | } \ |
| 513 | } while (0) |
| 514 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 515 | #define TEST_AND_RETURN_FALSE_ERRCODE(_x) \ |
| 516 | do { \ |
| 517 | errcode_t _error = (_x); \ |
| 518 | if (_error) { \ |
| 519 | errno = _error; \ |
| 520 | LOG(ERROR) << #_x " failed: " << _error; \ |
| 521 | return false; \ |
| 522 | } \ |
| 523 | } while (0) |
| 524 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 525 | |
| 526 | |
| 527 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |