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 #include <selinux/label.h>
30 
31 #include "result.h"
32 
33 #define COLDBOOT_DONE "/dev/.coldboot_done"
34 
35 using android::base::boot_clock;
36 using namespace std::chrono_literals;
37 
38 namespace android {
39 namespace init {
40 
41 int CreateSocket(const char* name, int type, bool passcred, mode_t perm, uid_t uid, gid_t gid,
42                  const char* socketcon);
43 
44 Result<std::string> ReadFile(const std::string& path);
45 Result<Success> WriteFile(const std::string& path, const std::string& content);
46 
47 Result<uid_t> DecodeUid(const std::string& name);
48 
49 bool mkdir_recursive(const std::string& pathname, mode_t mode);
50 int wait_for_file(const char *filename, std::chrono::nanoseconds timeout);
51 void import_kernel_cmdline(bool in_qemu,
52                            const std::function<void(const std::string&, const std::string&, bool)>&);
53 bool make_dir(const std::string& path, mode_t mode);
54 std::string bytes_to_hex(const uint8_t *bytes, size_t bytes_len);
55 bool is_dir(const char* pathname);
56 bool expand_props(const std::string& src, std::string* dst);
57 
58 // Returns the platform's Android DT directory as specified in the kernel cmdline.
59 // If the platform does not configure a custom DT path, returns the standard one (based in procfs).
60 const std::string& get_android_dt_dir();
61 // Reads or compares the content of device tree file under the platform's Android DT directory.
62 bool read_android_dt_file(const std::string& sub_path, std::string* dt_content);
63 bool is_android_dt_value_expected(const std::string& sub_path, const std::string& expected_content);
64 
65 bool IsLegalPropertyName(const std::string& name);
66 
67 }  // namespace init
68 }  // namespace android
69 
70 #endif
71