util.h revision 060b74baad7c366cb6c74042bf307f1548a8331f
1/* 2 * Copyright (C) 2010 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 _INIT_UTIL_H_ 18#define _INIT_UTIL_H_ 19 20#include <sys/stat.h> 21#include <sys/types.h> 22 23#include <chrono> 24#include <functional> 25#include <ostream> 26#include <string> 27 28#include <android-base/chrono_utils.h> 29 30#define COLDBOOT_DONE "/dev/.coldboot_done" 31 32using android::base::boot_clock; 33using namespace std::chrono_literals; 34 35int create_socket(const char *name, int type, mode_t perm, 36 uid_t uid, gid_t gid, const char *socketcon); 37 38bool read_file(const std::string& path, std::string* content); 39bool write_file(const std::string& path, const std::string& content); 40 41class Timer { 42 public: 43 Timer() : start_(boot_clock::now()) {} 44 45 double duration_s() const { 46 typedef std::chrono::duration<double> double_duration; 47 return std::chrono::duration_cast<double_duration>(boot_clock::now() - start_).count(); 48 } 49 50 int64_t duration_ms() const { 51 return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_) 52 .count(); 53 } 54 55 private: 56 android::base::boot_clock::time_point start_; 57}; 58 59std::ostream& operator<<(std::ostream& os, const Timer& t); 60 61unsigned int decode_uid(const char *s); 62 63int mkdir_recursive(const std::string& pathname, mode_t mode); 64int wait_for_file(const char *filename, std::chrono::nanoseconds timeout); 65void import_kernel_cmdline(bool in_qemu, 66 const std::function<void(const std::string&, const std::string&, bool)>&); 67int make_dir(const char *path, mode_t mode); 68int restorecon(const char *pathname, int flags = 0); 69std::string bytes_to_hex(const uint8_t *bytes, size_t bytes_len); 70bool is_dir(const char* pathname); 71bool expand_props(const std::string& src, std::string* dst); 72 73void panic() __attribute__((__noreturn__)); 74 75#endif 76