1 /* 2 * Copyright (C) 2017 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 <signal.h> 20 21 #include <string> 22 #include <vector> 23 24 #include <android-base/unique_fd.h> 25 26 #include "builtins.h" 27 #include "result.h" 28 #include "system/core/init/subcontext.pb.h" 29 30 namespace android { 31 namespace init { 32 33 static constexpr const char kInitContext[] = "u:r:init:s0"; 34 static constexpr const char kVendorContext[] = "u:r:vendor_init:s0"; 35 static constexpr const char kTestContext[] = "test-test-test"; 36 37 class Subcontext { 38 public: 39 Subcontext(std::vector<std::string> path_prefixes, std::string_view context, bool host = false) path_prefixes_(std::move (path_prefixes))40 : path_prefixes_(std::move(path_prefixes)), 41 context_(context.begin(), context.end()), 42 pid_(0) { 43 if (!host) { 44 Fork(); 45 } 46 } 47 48 Result<void> Execute(const std::vector<std::string>& args); 49 Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args); 50 void Restart(); 51 bool PathMatchesSubcontext(const std::string& path) const; 52 void SetApexList(std::vector<std::string>&& apex_list); 53 context()54 const std::string& context() const { return context_; } pid()55 pid_t pid() const { return pid_; } 56 57 private: 58 void Fork(); 59 Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command); 60 61 std::vector<std::string> path_prefixes_; 62 std::vector<std::string> apex_list_; 63 std::string context_; 64 pid_t pid_; 65 android::base::unique_fd socket_; 66 }; 67 68 int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map); 69 void InitializeSubcontext(); 70 void InitializeHostSubcontext(std::vector<std::string> vendor_prefixes); 71 Subcontext* GetSubcontext(); 72 bool SubcontextChildReap(pid_t pid); 73 void SubcontextTerminate(); 74 75 } // namespace init 76 } // namespace android 77