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, mode_t perm, uid_t uid,
48                          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 void ImportKernelCmdline(const std::function<void(const std::string&, const std::string&)>&);
58 bool make_dir(const std::string& path, mode_t mode);
59 bool is_dir(const char* pathname);
60 Result<std::string> ExpandProps(const std::string& src);
61 
62 // Returns the platform's Android DT directory as specified in the kernel cmdline.
63 // If the platform does not configure a custom DT path, returns the standard one (based in procfs).
64 const std::string& get_android_dt_dir();
65 // Reads or compares the content of device tree file under the platform's Android DT directory.
66 bool read_android_dt_file(const std::string& sub_path, std::string* dt_content);
67 bool is_android_dt_value_expected(const std::string& sub_path, const std::string& expected_content);
68 
69 bool IsLegalPropertyName(const std::string& name);
70 Result<void> IsLegalPropertyValue(const std::string& name, const std::string& value);
71 
72 struct MkdirOptions {
73     std::string target;
74     mode_t mode;
75     uid_t uid;
76     gid_t gid;
77     FscryptAction fscrypt_action;
78     std::string ref_option;
79 };
80 
81 Result<MkdirOptions> ParseMkdir(const std::vector<std::string>& args);
82 
83 struct MountAllOptions {
84     std::vector<std::string> rc_paths;
85     std::string fstab_path;
86     mount_mode mode;
87     bool import_rc;
88 };
89 
90 Result<MountAllOptions> ParseMountAll(const std::vector<std::string>& args);
91 
92 Result<std::pair<int, std::vector<std::string>>> ParseRestorecon(
93         const std::vector<std::string>& args);
94 
95 Result<std::string> ParseSwaponAll(const std::vector<std::string>& args);
96 
97 Result<std::string> ParseUmountAll(const std::vector<std::string>& args);
98 
99 void SetStdioToDevNull(char** argv);
100 void InitKernelLogging(char** argv);
101 void InitSecondStageLogging(char** argv);
102 void DumpShutdownDebugInformation();
103 bool IsRecoveryMode();
104 }  // namespace init
105 }  // namespace android
106