Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 8 | #include <errno.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 9 | |
Andrew de los Reyes | 81ebcd8 | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 10 | #include <algorithm> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <set> |
| 12 | #include <string> |
| 13 | #include <vector> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 14 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 15 | #include <glib.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 16 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 17 | #include "update_engine/action.h" |
| 18 | #include "update_engine/action_processor.h" |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | namespace utils { |
| 23 | |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 24 | // Returns true if this is an official Chrome OS build, false |
| 25 | // otherwise. Currently, this routine errs on the official build side |
| 26 | // -- if it doesn't recognize the update track as non-official, it |
| 27 | // assumes the build is official. |
| 28 | bool IsOfficialBuild(); |
| 29 | |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame^] | 30 | // Returns true if the OOBE process has been completed and EULA accepted, false |
| 31 | // otherwise. |
| 32 | bool IsOOBEComplete(); |
| 33 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 34 | // Writes the data passed to path. The file at path will be overwritten if it |
| 35 | // exists. Returns true on success, false otherwise. |
| 36 | bool WriteFile(const char* path, const char* data, int data_len); |
| 37 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 38 | // Calls write() or pwrite() repeatedly until all count bytes at buf are |
| 39 | // written to fd or an error occurs. Returns true on success. |
| 40 | bool WriteAll(int fd, const void* buf, size_t count); |
| 41 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset); |
| 42 | |
| 43 | // Calls pread() repeatedly until count bytes are read, or EOF is reached. |
| 44 | // Returns number of bytes read in *bytes_read. Returns true on success. |
| 45 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 46 | ssize_t* out_bytes_read); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 47 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 48 | // Returns the entire contents of the file at path. Returns true on success. |
| 49 | bool ReadFile(const std::string& path, std::vector<char>* out); |
| 50 | bool ReadFileToString(const std::string& path, std::string* out); |
| 51 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 52 | // Returns the size of the file at path. If the file doesn't exist or some |
| 53 | // error occurrs, -1 is returned. |
| 54 | off_t FileSize(const std::string& path); |
| 55 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 56 | std::string ErrnoNumberAsString(int err); |
| 57 | |
| 58 | // Strips duplicate slashes, and optionally removes all trailing slashes. |
| 59 | // Does not compact /./ or /../. |
| 60 | std::string NormalizePath(const std::string& path, bool strip_trailing_slash); |
| 61 | |
| 62 | // Returns true if the file exists for sure. Returns false if it doesn't exist, |
| 63 | // or an error occurs. |
| 64 | bool FileExists(const char* path); |
| 65 | |
| 66 | // The last 6 chars of path must be XXXXXX. They will be randomly changed |
| 67 | // and a non-existent path will be returned. Intentionally makes a copy |
| 68 | // of the string passed in. |
| 69 | // NEVER CALL THIS FUNCTION UNLESS YOU ARE SURE |
| 70 | // 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] | 71 | std::string TempFilename(std::string path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 72 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 73 | // Calls mkstemp() with the template passed. Returns the filename in the |
| 74 | // out param filename. If fd is non-NULL, the file fd returned by mkstemp |
| 75 | // is not close()d and is returned in the out param 'fd'. However, if |
| 76 | // fd is NULL, the fd from mkstemp() will be closed. |
| 77 | // The last six chars of the template must be XXXXXX. |
| 78 | // Returns true on success. |
| 79 | bool MakeTempFile(const std::string& filename_template, |
| 80 | std::string* filename, |
| 81 | int* fd); |
| 82 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 83 | // Calls mkdtemp() with the tempate passed. Returns the generated dirname |
| 84 | // in the dirname param. Returns TRUE on success. dirname must not be NULL. |
| 85 | bool MakeTempDirectory(const std::string& dirname_template, |
| 86 | std::string* dirname); |
| 87 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 88 | // Deletes a directory and all its contents synchronously. Returns true |
| 89 | // on success. This may be called with a regular file--it will just unlink it. |
| 90 | // This WILL cross filesystem boundaries. |
| 91 | bool RecursiveUnlinkDir(const std::string& path); |
| 92 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 93 | // Returns the root device for a partition. For example, |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 94 | // RootDevice("/dev/sda3") returns "/dev/sda". Returns an empty string |
| 95 | // 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] | 96 | std::string RootDevice(const std::string& partition_device); |
| 97 | |
| 98 | // Returns the partition number, as a string, of partition_device. For example, |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 99 | // PartitionNumber("/dev/sda3") returns "3". |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 100 | std::string PartitionNumber(const std::string& partition_device); |
| 101 | |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 102 | // Returns the sysfs block device for a root block device. For |
| 103 | // example, SysfsBlockDevice("/dev/sda") returns |
| 104 | // "/sys/block/sda". Returns an empty string if the input device is |
| 105 | // not of the "/dev/xyz" form. |
| 106 | std::string SysfsBlockDevice(const std::string& device); |
| 107 | |
| 108 | // Returns true if the root |device| (e.g., "/dev/sdb") is known to be |
| 109 | // removable, false otherwise. |
| 110 | bool IsRemovableDevice(const std::string& device); |
| 111 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 112 | // Synchronously mount or unmount a filesystem. Return true on success. |
| 113 | // Mounts as ext3 with default options. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 114 | bool MountFilesystem(const std::string& device, const std::string& mountpoint, |
| 115 | unsigned long flags); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 116 | bool UnmountFilesystem(const std::string& mountpoint); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 117 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 118 | enum BootLoader { |
| 119 | BootLoader_SYSLINUX = 0, |
| 120 | BootLoader_CHROME_FIRMWARE = 1 |
| 121 | }; |
| 122 | // Detects which bootloader this system uses and returns it via the out |
| 123 | // param. Returns true on success. |
| 124 | bool GetBootloader(BootLoader* out_bootloader); |
| 125 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 126 | // Returns the error message, if any, from a GError pointer. |
| 127 | const char* GetGErrorMessage(const GError* error); |
| 128 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 129 | // Initiates a system reboot. Returns true on success, false otherwise. |
| 130 | bool Reboot(); |
| 131 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 132 | // Fuzzes an integer |value| randomly in the range: |
| 133 | // [value - range / 2, value + range - range / 2] |
| 134 | int FuzzInt(int value, unsigned int range); |
| 135 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 136 | // Log a string in hex to LOG(INFO). Useful for debugging. |
| 137 | void HexDumpArray(const unsigned char* const arr, const size_t length); |
| 138 | inline void HexDumpString(const std::string& str) { |
| 139 | HexDumpArray(reinterpret_cast<const unsigned char*>(str.data()), str.size()); |
| 140 | } |
| 141 | inline void HexDumpVector(const std::vector<char>& vect) { |
| 142 | HexDumpArray(reinterpret_cast<const unsigned char*>(&vect[0]), vect.size()); |
| 143 | } |
| 144 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 145 | extern const char* const kStatefulPartition; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | |
| 147 | bool StringHasSuffix(const std::string& str, const std::string& suffix); |
| 148 | bool StringHasPrefix(const std::string& str, const std::string& prefix); |
| 149 | |
| 150 | template<typename KeyType, typename ValueType> |
| 151 | bool MapContainsKey(const std::map<KeyType, ValueType>& m, const KeyType& k) { |
| 152 | return m.find(k) != m.end(); |
| 153 | } |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 154 | template<typename KeyType> |
| 155 | bool SetContainsKey(const std::set<KeyType>& s, const KeyType& k) { |
| 156 | return s.find(k) != s.end(); |
| 157 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 158 | |
| 159 | template<typename ValueType> |
| 160 | std::set<ValueType> SetWithValue(const ValueType& value) { |
| 161 | std::set<ValueType> ret; |
| 162 | ret.insert(value); |
| 163 | return ret; |
| 164 | } |
| 165 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 166 | template<typename T> |
| 167 | bool VectorContainsValue(const std::vector<T>& vect, const T& value) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 168 | return std::find(vect.begin(), vect.end(), value) != vect.end(); |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 171 | template<typename T> |
| 172 | bool VectorIndexOf(const std::vector<T>& vect, const T& value, |
| 173 | typename std::vector<T>::size_type* out_index) { |
| 174 | typename std::vector<T>::const_iterator it = std::find(vect.begin(), |
| 175 | vect.end(), |
| 176 | value); |
| 177 | if (it == vect.end()) { |
| 178 | return false; |
| 179 | } else { |
| 180 | *out_index = it - vect.begin(); |
| 181 | return true; |
| 182 | } |
| 183 | } |
| 184 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 185 | // Returns the currently booted device. "/dev/sda3", for example. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 186 | // This will not interpret LABEL= or UUID=. You'll need to use findfs |
| 187 | // or something with equivalent funcionality to interpret those. |
| 188 | const std::string BootDevice(); |
| 189 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 190 | // Returns the currently booted kernel device, "dev/sda2", for example. |
| 191 | // Client must pass in the boot device. The suggested calling convention |
| 192 | // is: BootKernelDevice(BootDevice()). |
| 193 | // This function works by doing string modification on boot_device. |
| 194 | // Returns empty string on failure. |
| 195 | const std::string BootKernelDevice(const std::string& boot_device); |
| 196 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 197 | enum ProcessPriority { |
| 198 | kProcessPriorityHigh = -10, |
| 199 | kProcessPriorityNormal = 0, |
| 200 | kProcessPriorityLow = 10, |
| 201 | }; |
| 202 | |
| 203 | // Compares process priorities and returns an integer that is less |
| 204 | // than, equal to or greater than 0 if |priority_lhs| is, |
| 205 | // respectively, lower than, same as or higher than |priority_rhs|. |
| 206 | int ComparePriorities(ProcessPriority priority_lhs, |
| 207 | ProcessPriority priority_rhs); |
| 208 | |
| 209 | // Sets the current process priority to |priority|. Returns true on |
| 210 | // success, false otherwise. |
| 211 | bool SetProcessPriority(ProcessPriority priority); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 212 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 213 | } // namespace utils |
| 214 | |
| 215 | // Class to unmount FS when object goes out of scope |
| 216 | class ScopedFilesystemUnmounter { |
| 217 | public: |
| 218 | explicit ScopedFilesystemUnmounter(const std::string& mountpoint) |
| 219 | : mountpoint_(mountpoint) {} |
| 220 | ~ScopedFilesystemUnmounter() { |
| 221 | utils::UnmountFilesystem(mountpoint_); |
| 222 | } |
| 223 | private: |
| 224 | const std::string mountpoint_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 225 | DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | // Utility class to close a file descriptor |
| 229 | class ScopedFdCloser { |
| 230 | public: |
| 231 | explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {} |
| 232 | void set_should_close(bool should_close) { should_close_ = should_close; } |
| 233 | ~ScopedFdCloser() { |
| 234 | if (!should_close_) |
| 235 | return; |
| 236 | if (fd_ && (*fd_ >= 0)) { |
| 237 | close(*fd_); |
| 238 | *fd_ = -1; |
| 239 | } |
| 240 | } |
| 241 | private: |
| 242 | int* fd_; |
| 243 | bool should_close_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 244 | DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser); |
| 245 | }; |
| 246 | |
| 247 | // Utility class to delete a file when it goes out of scope. |
| 248 | class ScopedPathUnlinker { |
| 249 | public: |
| 250 | explicit ScopedPathUnlinker(const std::string& path) : path_(path) {} |
| 251 | ~ScopedPathUnlinker() { |
| 252 | if (unlink(path_.c_str()) < 0) { |
| 253 | std::string err_message = strerror(errno); |
| 254 | LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message; |
| 255 | } |
| 256 | } |
| 257 | private: |
| 258 | const std::string path_; |
| 259 | DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); |
| 260 | }; |
| 261 | |
| 262 | // Utility class to delete an empty directory when it goes out of scope. |
| 263 | class ScopedDirRemover { |
| 264 | public: |
| 265 | explicit ScopedDirRemover(const std::string& path) : path_(path) {} |
| 266 | ~ScopedDirRemover() { |
| 267 | if (rmdir(path_.c_str()) < 0) |
| 268 | PLOG(ERROR) << "Unable to remove dir " << path_; |
| 269 | } |
| 270 | private: |
| 271 | const std::string path_; |
| 272 | DISALLOW_COPY_AND_ASSIGN(ScopedDirRemover); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | // A little object to call ActionComplete on the ActionProcessor when |
| 276 | // it's destructed. |
| 277 | class ScopedActionCompleter { |
| 278 | public: |
| 279 | explicit ScopedActionCompleter(ActionProcessor* processor, |
| 280 | AbstractAction* action) |
| 281 | : processor_(processor), |
| 282 | action_(action), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 283 | code_(kActionCodeError), |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 284 | should_complete_(true) {} |
| 285 | ~ScopedActionCompleter() { |
| 286 | if (should_complete_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 287 | processor_->ActionComplete(action_, code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 288 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 289 | void set_code(ActionExitCode code) { code_ = code; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 290 | void set_should_complete(bool should_complete) { |
| 291 | should_complete_ = should_complete; |
| 292 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 293 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 294 | private: |
| 295 | ActionProcessor* processor_; |
| 296 | AbstractAction* action_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 297 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 298 | bool should_complete_; |
| 299 | DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter); |
| 300 | }; |
| 301 | |
| 302 | } // namespace chromeos_update_engine |
| 303 | |
| 304 | #define TEST_AND_RETURN_FALSE_ERRNO(_x) \ |
| 305 | do { \ |
| 306 | bool _success = (_x); \ |
| 307 | if (!_success) { \ |
| 308 | std::string _msg = \ |
| 309 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 310 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 311 | return false; \ |
| 312 | } \ |
| 313 | } while (0) |
| 314 | |
| 315 | #define TEST_AND_RETURN_FALSE(_x) \ |
| 316 | do { \ |
| 317 | bool _success = (_x); \ |
| 318 | if (!_success) { \ |
| 319 | LOG(ERROR) << #_x " failed."; \ |
| 320 | return false; \ |
| 321 | } \ |
| 322 | } while (0) |
| 323 | |
| 324 | #define TEST_AND_RETURN_ERRNO(_x) \ |
| 325 | do { \ |
| 326 | bool _success = (_x); \ |
| 327 | if (!_success) { \ |
| 328 | std::string _msg = \ |
| 329 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 330 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 331 | return; \ |
| 332 | } \ |
| 333 | } while (0) |
| 334 | |
| 335 | #define TEST_AND_RETURN(_x) \ |
| 336 | do { \ |
| 337 | bool _success = (_x); \ |
| 338 | if (!_success) { \ |
| 339 | LOG(ERROR) << #_x " failed."; \ |
| 340 | return; \ |
| 341 | } \ |
| 342 | } while (0) |
| 343 | |
| 344 | |
| 345 | |
| 346 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |