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 void ImportKernelCmdline(const std::function<void(const std::string&, const std::string&)>&); 58 void ImportBootconfig(const std::function<void(const std::string&, const std::string&)>&); 59 bool make_dir(const std::string& path, mode_t mode); 60 bool is_dir(const char* pathname); 61 Result<std::string> ExpandProps(const std::string& src); 62 63 // Returns the platform's Android DT directory as specified in the kernel cmdline. 64 // If the platform does not configure a custom DT path, returns the standard one (based in procfs). 65 const std::string& get_android_dt_dir(); 66 // Reads or compares the content of device tree file under the platform's Android DT directory. 67 bool read_android_dt_file(const std::string& sub_path, std::string* dt_content); 68 bool is_android_dt_value_expected(const std::string& sub_path, const std::string& expected_content); 69 70 bool IsLegalPropertyName(const std::string& name); 71 Result<void> IsLegalPropertyValue(const std::string& name, const std::string& value); 72 std::string CleanDirPath(const std::string& path); 73 74 struct MkdirOptions { 75 std::string target; 76 mode_t mode; 77 uid_t uid; 78 gid_t gid; 79 FscryptAction fscrypt_action; 80 std::string ref_option; 81 }; 82 83 Result<MkdirOptions> ParseMkdir(const std::vector<std::string>& args); 84 85 struct MountAllOptions { 86 std::vector<std::string> rc_paths; 87 std::string fstab_path; 88 mount_mode mode; 89 bool import_rc; 90 }; 91 92 Result<MountAllOptions> ParseMountAll(const std::vector<std::string>& args); 93 94 Result<std::pair<int, std::vector<std::string>>> ParseRestorecon( 95 const std::vector<std::string>& args); 96 97 Result<std::string> ParseSwaponAll(const std::vector<std::string>& args); 98 99 Result<std::string> ParseUmountAll(const std::vector<std::string>& args); 100 101 void SetStdioToDevNull(char** argv); 102 void InitKernelLogging(char** argv); 103 bool IsRecoveryMode(); 104 105 bool IsDefaultMountNamespaceReady(); 106 void SetDefaultMountNamespaceReady(); 107 108 bool IsMicrodroid(); 109 bool Has32BitAbi(); 110 111 std::string GetApexNameFromFileName(const std::string& path); 112 113 // Compare all files */path.#rc and */path.rc with the same path prefix. 114 // Keep the one with the highest # that doesn't exceed the system's SDK. 115 // (.rc == .0rc for ranking purposes) 116 std::vector<std::string> FilterVersionedConfigs(const std::vector<std::string>& configs, 117 int active_sdk); 118 } // namespace init 119 } // namespace android 120