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 #define COLDBOOT_DONE "/dev/.coldboot_done"
29 
30 const std::string kAndroidDtDir("/proc/device-tree/firmware/android/");
31 
32 using namespace std::chrono_literals;
33 
34 int create_socket(const char *name, int type, mode_t perm,
35                   uid_t uid, gid_t gid, const char *socketcon);
36 
37 bool read_file(const std::string& path, std::string* content);
38 bool write_file(const std::string& path, const std::string& content);
39 
40 // A std::chrono clock based on CLOCK_BOOTTIME.
41 class boot_clock {
42  public:
43   typedef std::chrono::nanoseconds duration;
44   typedef std::chrono::time_point<boot_clock, duration> time_point;
45   static constexpr bool is_steady = true;
46 
47   static time_point now();
48 };
49 
50 class Timer {
51  public:
Timer()52   Timer() : start_(boot_clock::now()) {
53   }
54 
duration_s()55   double duration_s() const {
56     typedef std::chrono::duration<double> double_duration;
57     return std::chrono::duration_cast<double_duration>(boot_clock::now() - start_).count();
58   }
59 
duration_ms()60   int64_t duration_ms() const {
61     return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_).count();
62   }
63 
64  private:
65   boot_clock::time_point start_;
66 };
67 
68 std::ostream& operator<<(std::ostream& os, const Timer& t);
69 
70 unsigned int decode_uid(const char *s);
71 
72 int mkdir_recursive(const char *pathname, mode_t mode);
73 void sanitize(char *p);
74 int wait_for_file(const char *filename, std::chrono::nanoseconds timeout);
75 void import_kernel_cmdline(bool in_qemu,
76                            const std::function<void(const std::string&, const std::string&, bool)>&);
77 int make_dir(const char *path, mode_t mode);
78 int restorecon(const char *pathname, int flags = 0);
79 std::string bytes_to_hex(const uint8_t *bytes, size_t bytes_len);
80 bool is_dir(const char* pathname);
81 bool expand_props(const std::string& src, std::string* dst);
82 
83 void panic() __attribute__((__noreturn__));
84 
85 // Reads or compares the content of device tree file under kAndroidDtDir directory.
86 bool read_android_dt_file(const std::string& sub_path, std::string* dt_content);
87 bool is_android_dt_value_expected(const std::string& sub_path, const std::string& expected_content);
88 
89 #endif
90