Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 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 | |
Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 17 | #pragma once |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 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" |
Tom Cherry | 618d310 | 2018-01-19 14:25:48 -0800 | [diff] [blame] | 27 | #include "result.h" |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 28 | #include "system/core/init/subcontext.pb.h" |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace init { |
| 32 | |
Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 33 | static constexpr const char kInitContext[] = "u:r:init:s0"; |
| 34 | static constexpr const char kVendorContext[] = "u:r:vendor_init:s0"; |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 35 | static constexpr const char kTestContext[] = "test-test-test"; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 36 | |
| 37 | class Subcontext { |
| 38 | public: |
Chan Wang | 5996d60 | 2024-11-14 13:25:37 +0000 | [diff] [blame] | 39 | Subcontext(std::vector<std::string> path_prefixes, std::vector<std::string> partitions, |
| 40 | std::string_view context, bool host = false) |
Bart Van Assche | 462ea55 | 2022-10-26 09:31:35 -0700 | [diff] [blame] | 41 | : path_prefixes_(std::move(path_prefixes)), |
Chan Wang | 5996d60 | 2024-11-14 13:25:37 +0000 | [diff] [blame] | 42 | partitions_(std::move(partitions)), |
Bart Van Assche | 462ea55 | 2022-10-26 09:31:35 -0700 | [diff] [blame] | 43 | context_(context.begin(), context.end()), |
| 44 | pid_(0) { |
Daniel Norman | f597fa5 | 2020-11-09 17:28:24 -0800 | [diff] [blame] | 45 | if (!host) { |
| 46 | Fork(); |
| 47 | } |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 50 | Result<void> Execute(const std::vector<std::string>& args); |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 51 | Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 52 | void Restart(); |
Jooyung Han | 38e8e74 | 2022-05-10 05:35:35 +0900 | [diff] [blame] | 53 | bool PathMatchesSubcontext(const std::string& path) const; |
Chan Wang | 5996d60 | 2024-11-14 13:25:37 +0000 | [diff] [blame] | 54 | bool PartitionMatchesSubcontext(const std::string& partition) const; |
Jooyung Han | 38e8e74 | 2022-05-10 05:35:35 +0900 | [diff] [blame] | 55 | void SetApexList(std::vector<std::string>&& apex_list); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 56 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 57 | const std::string& context() const { return context_; } |
| 58 | pid_t pid() const { return pid_; } |
| 59 | |
| 60 | private: |
| 61 | void Fork(); |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 62 | Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 63 | |
Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 64 | std::vector<std::string> path_prefixes_; |
Chan Wang | 5996d60 | 2024-11-14 13:25:37 +0000 | [diff] [blame] | 65 | std::vector<std::string> partitions_; |
Jooyung Han | 38e8e74 | 2022-05-10 05:35:35 +0900 | [diff] [blame] | 66 | std::vector<std::string> apex_list_; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 67 | std::string context_; |
| 68 | pid_t pid_; |
| 69 | android::base::unique_fd socket_; |
| 70 | }; |
| 71 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 72 | int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map); |
Tom Cherry | e3e77d3 | 2020-04-28 13:55:19 -0700 | [diff] [blame] | 73 | void InitializeSubcontext(); |
Daniel Norman | f597fa5 | 2020-11-09 17:28:24 -0800 | [diff] [blame] | 74 | void InitializeHostSubcontext(std::vector<std::string> vendor_prefixes); |
Tom Cherry | e3e77d3 | 2020-04-28 13:55:19 -0700 | [diff] [blame] | 75 | Subcontext* GetSubcontext(); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 76 | bool SubcontextChildReap(pid_t pid); |
Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 77 | void SubcontextTerminate(); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 78 | |
| 79 | } // namespace init |
| 80 | } // namespace android |