blob: 6006eb85e736f63933380e613f26ebad509b75a6 [file] [log] [blame]
Darin Petkov296889c2010-07-23 16:20:54 -07001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/utils.h"
Darin Petkovf74eb652010-08-04 12:08:38 -07006
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <sys/mount.h>
Darin Petkovc6c135c2010-08-11 13:36:18 -07008#include <sys/resource.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <sys/stat.h>
10#include <sys/types.h>
11#include <dirent.h>
12#include <errno.h>
Andrew de los Reyes970bb282009-12-09 16:34:04 -080013#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
Darin Petkovf74eb652010-08-04 12:08:38 -070018
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <algorithm>
Darin Petkovf74eb652010-08-04 12:08:38 -070020
Will Drewry8f71da82010-08-30 14:07:11 -050021#include <base/file_path.h>
22#include <base/file_util.h>
23#include <base/rand_util.h>
24#include <base/string_util.h>
25#include <base/logging.h>
26#include <rootdev/rootdev.h>
27
Andrew de los Reyes970bb282009-12-09 16:34:04 -080028#include "update_engine/file_writer.h"
Darin Petkov33d30642010-08-04 10:18:57 -070029#include "update_engine/omaha_request_params.h"
Darin Petkov296889c2010-07-23 16:20:54 -070030#include "update_engine/subprocess.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000031
32using std::min;
33using std::string;
34using std::vector;
35
36namespace chromeos_update_engine {
37
38namespace utils {
39
Darin Petkov2a0e6332010-09-24 14:43:41 -070040static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
41
Darin Petkov33d30642010-08-04 10:18:57 -070042bool IsOfficialBuild() {
43 OmahaRequestDeviceParams params;
44 if (!params.Init("", "")) {
45 return true;
46 }
47 return params.app_track != "buildbot-build" &&
48 params.app_track != "developer-build";
49}
50
Darin Petkov2a0e6332010-09-24 14:43:41 -070051bool IsOOBEComplete() {
52 return file_util::PathExists(FilePath(kOOBECompletedMarker));
53}
54
Andrew de los Reyes970bb282009-12-09 16:34:04 -080055bool WriteFile(const char* path, const char* data, int data_len) {
56 DirectFileWriter writer;
57 TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path,
58 O_WRONLY | O_CREAT | O_TRUNC,
Chris Masone4dc2ada2010-09-23 12:43:03 -070059 0600));
Andrew de los Reyes970bb282009-12-09 16:34:04 -080060 ScopedFileWriterCloser closer(&writer);
61 TEST_AND_RETURN_FALSE_ERRNO(data_len == writer.Write(data, data_len));
62 return true;
63}
64
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070065bool WriteAll(int fd, const void* buf, size_t count) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070066 const char* c_buf = static_cast<const char*>(buf);
67 ssize_t bytes_written = 0;
68 while (bytes_written < static_cast<ssize_t>(count)) {
69 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written);
70 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
71 bytes_written += rc;
72 }
73 return true;
74}
75
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070076bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) {
77 const char* c_buf = static_cast<const char*>(buf);
78 ssize_t bytes_written = 0;
79 while (bytes_written < static_cast<ssize_t>(count)) {
80 ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written,
81 offset + bytes_written);
82 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
83 bytes_written += rc;
84 }
85 return true;
86}
87
88bool PReadAll(int fd, void* buf, size_t count, off_t offset,
89 ssize_t* out_bytes_read) {
90 char* c_buf = static_cast<char*>(buf);
91 ssize_t bytes_read = 0;
92 while (bytes_read < static_cast<ssize_t>(count)) {
93 ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read,
94 offset + bytes_read);
95 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
96 if (rc == 0) {
97 break;
98 }
99 bytes_read += rc;
100 }
101 *out_bytes_read = bytes_read;
102 return true;
Darin Petkov296889c2010-07-23 16:20:54 -0700103
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700104}
105
adlr@google.com3defe6a2009-12-04 20:57:17 +0000106bool ReadFile(const std::string& path, std::vector<char>* out) {
107 CHECK(out);
108 FILE* fp = fopen(path.c_str(), "r");
109 if (!fp)
110 return false;
111 const size_t kChunkSize = 1024;
112 size_t read_size;
113 do {
114 char buf[kChunkSize];
115 read_size = fread(buf, 1, kChunkSize, fp);
116 if (read_size == 0)
117 break;
118 out->insert(out->end(), buf, buf + read_size);
119 } while (read_size == kChunkSize);
120 bool success = !ferror(fp);
121 TEST_AND_RETURN_FALSE_ERRNO(fclose(fp) == 0);
122 return success;
123}
124
125bool ReadFileToString(const std::string& path, std::string* out) {
126 vector<char> data;
127 bool success = ReadFile(path, &data);
128 if (!success) {
129 return false;
130 }
131 (*out) = string(&data[0], data.size());
132 return true;
133}
134
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700135off_t FileSize(const string& path) {
136 struct stat stbuf;
137 int rc = stat(path.c_str(), &stbuf);
138 CHECK_EQ(rc, 0);
139 if (rc < 0)
140 return rc;
141 return stbuf.st_size;
142}
143
adlr@google.com3defe6a2009-12-04 20:57:17 +0000144void HexDumpArray(const unsigned char* const arr, const size_t length) {
145 const unsigned char* const char_arr =
146 reinterpret_cast<const unsigned char* const>(arr);
147 LOG(INFO) << "Logging array of length: " << length;
148 const unsigned int bytes_per_line = 16;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700149 for (uint32_t i = 0; i < length; i += bytes_per_line) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000150 const unsigned int bytes_remaining = length - i;
151 const unsigned int bytes_per_this_line = min(bytes_per_line,
152 bytes_remaining);
153 char header[100];
154 int r = snprintf(header, sizeof(header), "0x%08x : ", i);
155 TEST_AND_RETURN(r == 13);
156 string line = header;
157 for (unsigned int j = 0; j < bytes_per_this_line; j++) {
158 char buf[20];
159 unsigned char c = char_arr[i + j];
160 r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c));
161 TEST_AND_RETURN(r == 3);
162 line += buf;
163 }
164 LOG(INFO) << line;
165 }
166}
167
168namespace {
169class ScopedDirCloser {
170 public:
171 explicit ScopedDirCloser(DIR** dir) : dir_(dir) {}
172 ~ScopedDirCloser() {
173 if (dir_ && *dir_) {
174 int r = closedir(*dir_);
175 TEST_AND_RETURN_ERRNO(r == 0);
176 *dir_ = NULL;
177 dir_ = NULL;
178 }
179 }
180 private:
181 DIR** dir_;
182};
183} // namespace {}
184
185bool RecursiveUnlinkDir(const std::string& path) {
186 struct stat stbuf;
187 int r = lstat(path.c_str(), &stbuf);
188 TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT));
189 if ((r < 0) && (errno == ENOENT))
190 // path request is missing. that's fine.
191 return true;
192 if (!S_ISDIR(stbuf.st_mode)) {
193 TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) ||
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700194 (errno == ENOENT));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000195 // success or path disappeared before we could unlink.
196 return true;
197 }
198 {
199 // We have a dir, unlink all children, then delete dir
200 DIR *dir = opendir(path.c_str());
201 TEST_AND_RETURN_FALSE_ERRNO(dir);
202 ScopedDirCloser dir_closer(&dir);
203 struct dirent dir_entry;
204 struct dirent *dir_entry_p;
205 int err = 0;
206 while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) {
207 if (dir_entry_p == NULL) {
208 // end of stream reached
209 break;
210 }
211 // Skip . and ..
212 if (!strcmp(dir_entry_p->d_name, ".") ||
213 !strcmp(dir_entry_p->d_name, ".."))
214 continue;
215 TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" +
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700216 dir_entry_p->d_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000217 }
218 TEST_AND_RETURN_FALSE(err == 0);
219 }
220 // unlink dir
221 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT));
222 return true;
223}
224
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700225string RootDevice(const string& partition_device) {
Darin Petkovf74eb652010-08-04 12:08:38 -0700226 FilePath device_path(partition_device);
227 if (device_path.DirName().value() != "/dev") {
228 return "";
229 }
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700230 string::const_iterator it = --partition_device.end();
231 for (; it >= partition_device.begin(); --it) {
232 if (!isdigit(*it))
233 break;
234 }
235 // Some devices contain a p before the partitions. For example:
236 // /dev/mmc0p4 should be shortened to /dev/mmc0.
237 if (*it == 'p')
238 --it;
239 return string(partition_device.begin(), it + 1);
240}
241
242string PartitionNumber(const string& partition_device) {
243 CHECK(!partition_device.empty());
244 string::const_iterator it = --partition_device.end();
245 for (; it >= partition_device.begin(); --it) {
246 if (!isdigit(*it))
247 break;
248 }
249 return string(it + 1, partition_device.end());
250}
251
Darin Petkovf74eb652010-08-04 12:08:38 -0700252string SysfsBlockDevice(const string& device) {
253 FilePath device_path(device);
254 if (device_path.DirName().value() != "/dev") {
255 return "";
256 }
257 return FilePath("/sys/block").Append(device_path.BaseName()).value();
258}
259
260bool IsRemovableDevice(const std::string& device) {
261 string sysfs_block = SysfsBlockDevice(device);
262 string removable;
263 if (sysfs_block.empty() ||
264 !file_util::ReadFileToString(FilePath(sysfs_block).Append("removable"),
265 &removable)) {
266 return false;
267 }
268 TrimWhitespaceASCII(removable, TRIM_ALL, &removable);
269 return removable == "1";
270}
271
adlr@google.com3defe6a2009-12-04 20:57:17 +0000272std::string ErrnoNumberAsString(int err) {
273 char buf[100];
274 buf[0] = '\0';
275 return strerror_r(err, buf, sizeof(buf));
276}
277
278std::string NormalizePath(const std::string& path, bool strip_trailing_slash) {
279 string ret;
280 bool last_insert_was_slash = false;
281 for (string::const_iterator it = path.begin(); it != path.end(); ++it) {
282 if (*it == '/') {
283 if (last_insert_was_slash)
284 continue;
285 last_insert_was_slash = true;
286 } else {
287 last_insert_was_slash = false;
288 }
289 ret.push_back(*it);
290 }
291 if (strip_trailing_slash && last_insert_was_slash) {
292 string::size_type last_non_slash = ret.find_last_not_of('/');
293 if (last_non_slash != string::npos) {
294 ret.resize(last_non_slash + 1);
295 } else {
296 ret = "";
297 }
298 }
299 return ret;
300}
301
302bool FileExists(const char* path) {
303 struct stat stbuf;
304 return 0 == lstat(path, &stbuf);
305}
306
307std::string TempFilename(string path) {
308 static const string suffix("XXXXXX");
309 CHECK(StringHasSuffix(path, suffix));
310 do {
311 string new_suffix;
312 for (unsigned int i = 0; i < suffix.size(); i++) {
313 int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9]
314 if (r < 26)
315 new_suffix.append(1, 'a' + r);
316 else if (r < (26 * 2))
317 new_suffix.append(1, 'A' + r - 26);
318 else
319 new_suffix.append(1, '0' + r - (26 * 2));
320 }
321 CHECK_EQ(new_suffix.size(), suffix.size());
322 path.resize(path.size() - new_suffix.size());
323 path.append(new_suffix);
324 } while (FileExists(path.c_str()));
325 return path;
326}
327
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700328bool MakeTempFile(const std::string& filename_template,
329 std::string* filename,
330 int* fd) {
331 DCHECK(filename || fd);
332 vector<char> buf(filename_template.size() + 1);
333 memcpy(&buf[0], filename_template.data(), filename_template.size());
334 buf[filename_template.size()] = '\0';
Darin Petkov296889c2010-07-23 16:20:54 -0700335
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700336 int mkstemp_fd = mkstemp(&buf[0]);
337 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
338 if (filename) {
339 *filename = &buf[0];
340 }
341 if (fd) {
342 *fd = mkstemp_fd;
343 } else {
344 close(mkstemp_fd);
345 }
346 return true;
347}
348
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700349bool MakeTempDirectory(const std::string& dirname_template,
350 std::string* dirname) {
351 DCHECK(dirname);
352 vector<char> buf(dirname_template.size() + 1);
353 memcpy(&buf[0], dirname_template.data(), dirname_template.size());
354 buf[dirname_template.size()] = '\0';
Darin Petkov296889c2010-07-23 16:20:54 -0700355
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700356 char* return_code = mkdtemp(&buf[0]);
357 TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL);
358 *dirname = &buf[0];
359 return true;
360}
361
adlr@google.com3defe6a2009-12-04 20:57:17 +0000362bool StringHasSuffix(const std::string& str, const std::string& suffix) {
363 if (suffix.size() > str.size())
364 return false;
365 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
366}
367
368bool StringHasPrefix(const std::string& str, const std::string& prefix) {
369 if (prefix.size() > str.size())
370 return false;
371 return 0 == str.compare(0, prefix.size(), prefix);
372}
373
Will Drewry8f71da82010-08-30 14:07:11 -0500374const std::string BootDevice() {
375 char boot_path[PATH_MAX];
376 // Resolve the boot device path fully, including dereferencing
377 // through dm-verity.
378 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
379
380 if (ret < 0) {
381 LOG(ERROR) << "rootdev failed to find the root device";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000382 return "";
383 }
Will Drewry8f71da82010-08-30 14:07:11 -0500384 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
385
386 // This local variable is used to construct the return string and is not
387 // passed around after use.
388 return boot_path;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000389}
390
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700391const string BootKernelDevice(const std::string& boot_device) {
392 // Currntly this assumes the last digit of the boot device is
393 // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to
394 // get the kernel device.
395 string ret = boot_device;
396 if (ret.empty())
397 return ret;
398 char last_char = ret[ret.size() - 1];
399 if (last_char == '3' || last_char == '5' || last_char == '7') {
400 ret[ret.size() - 1] = last_char - 1;
401 return ret;
402 }
403 return "";
404}
405
adlr@google.com3defe6a2009-12-04 20:57:17 +0000406bool MountFilesystem(const string& device,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700407 const string& mountpoint,
408 unsigned long mountflags) {
409 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", mountflags, NULL);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000410 if (rc < 0) {
411 string msg = ErrnoNumberAsString(errno);
412 LOG(ERROR) << "Unable to mount destination device: " << msg << ". "
413 << device << " on " << mountpoint;
414 return false;
415 }
416 return true;
417}
418
419bool UnmountFilesystem(const string& mountpoint) {
420 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0);
421 return true;
422}
423
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700424bool GetBootloader(BootLoader* out_bootloader) {
425 // For now, hardcode to syslinux.
426 *out_bootloader = BootLoader_SYSLINUX;
427 return true;
428}
429
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700430const char* GetGErrorMessage(const GError* error) {
431 if (!error)
432 return "Unknown error.";
433 return error->message;
434}
435
Darin Petkov296889c2010-07-23 16:20:54 -0700436bool Reboot() {
437 vector<string> command;
438 command.push_back("/sbin/shutdown");
439 command.push_back("-r");
440 command.push_back("now");
441 int rc = 0;
442 Subprocess::SynchronousExec(command, &rc);
443 TEST_AND_RETURN_FALSE(rc == 0);
444 return true;
445}
446
Darin Petkovc6c135c2010-08-11 13:36:18 -0700447bool SetProcessPriority(ProcessPriority priority) {
448 int prio = static_cast<int>(priority);
449 LOG(INFO) << "Setting process priority to " << prio;
450 TEST_AND_RETURN_FALSE(setpriority(PRIO_PROCESS, 0, prio) == 0);
451 return true;
452}
453
454int ComparePriorities(ProcessPriority priority_lhs,
455 ProcessPriority priority_rhs) {
456 return static_cast<int>(priority_rhs) - static_cast<int>(priority_lhs);
457}
458
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700459int FuzzInt(int value, unsigned int range) {
460 int min = value - range / 2;
461 int max = value + range - range / 2;
462 return base::RandInt(min, max);
463}
464
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800465const char* const kStatefulPartition = "/mnt/stateful_partition";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000466
467} // namespace utils
468
469} // namespace chromeos_update_engine