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 #pragma once
18
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include <chrono>
23 #include <functional>
24 #include <string>
25 #include <vector>
26
27 #include <android-base/chrono_utils.h>
28
29 #include "fscrypt_init_extensions.h"
30 #include "result.h"
31
32 using android::base::boot_clock;
33
34 namespace android {
35 namespace init {
36
37 enum mount_mode {
38 MOUNT_MODE_DEFAULT = 0,
39 MOUNT_MODE_EARLY = 1,
40 MOUNT_MODE_LATE = 2,
41 };
42
43 static const char kColdBootDoneProp[] = "ro.cold_boot_done";
44
45 extern void (*trigger_shutdown)(const std::string& command);
46
47 Result<int> CreateSocket(const std::string& name, int type, bool passcred, bool should_listen,
48 mode_t perm, uid_t uid, gid_t gid, const std::string& socketcon);
49
50 Result<std::string> ReadFile(const std::string& path);
51 Result<void> WriteFile(const std::string& path, const std::string& content);
52
53 Result<uid_t> DecodeUid(const std::string& name);
54
55 bool mkdir_recursive(const std::string& pathname, mode_t mode);
56 int wait_for_file(const char* filename, std::chrono::nanoseconds timeout);
57 bool make_dir(const std::string& path, mode_t mode);
58 bool is_dir(const char* pathname);
59 Result<std::string> ExpandProps(const std::string& src);
60
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 Result<void> IsLegalPropertyValue(const std::string& name, const std::string& value);
67 std::string CleanDirPath(const std::string& path);
68
69 struct MkdirOptions {
70 std::string target;
71 mode_t mode;
72 uid_t uid;
73 gid_t gid;
74 FscryptAction fscrypt_action;
75 std::string ref_option;
76 };
77
78 Result<MkdirOptions> ParseMkdir(const std::vector<std::string>& args);
79
80 struct MountAllOptions {
81 std::vector<std::string> rc_paths;
82 std::string fstab_path;
83 mount_mode mode;
84 bool import_rc;
85 };
86
87 Result<MountAllOptions> ParseMountAll(const std::vector<std::string>& args);
88
89 Result<std::pair<int, std::vector<std::string>>> ParseRestorecon(
90 const std::vector<std::string>& args);
91
92 Result<std::string> ParseSwaponAll(const std::vector<std::string>& args);
93
94 Result<std::string> ParseUmountAll(const std::vector<std::string>& args);
95
96 void SetStdioToDevNull(char** argv);
97 void InitKernelLogging(char** argv);
98 bool IsRecoveryMode();
99
100 bool IsDefaultMountNamespaceReady();
101 void SetDefaultMountNamespaceReady();
102
IsMicrodroid()103 inline constexpr bool IsMicrodroid() {
104 #ifdef MICRODROID
105 return MICRODROID;
106 #else
107 return false;
108 #endif
109 }
110
111 bool Has32BitAbi();
112
113 std::string GetApexNameFromFileName(const std::string& path);
114
115 // Compare all files */path.#rc and */path.rc with the same path prefix.
116 // Keep the one with the highest # that doesn't exceed the system's SDK.
117 // (.rc == .0rc for ranking purposes)
118 std::vector<std::string> FilterVersionedConfigs(const std::vector<std::string>& configs,
119 int active_sdk);
120
121 // Forks, executes the provided program in the child, and waits for the completion in the parent.
122 // Child's stderr is captured and logged using LOG(ERROR).
123 bool ForkExecveAndWaitForCompletion(const char* filename, char* const argv[]);
124
125 } // namespace init
126 } // namespace android
127