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 | |
| 17 | #ifndef _INIT_SUBCONTEXT_H |
| 18 | #define _INIT_SUBCONTEXT_H |
| 19 | |
| 20 | #include <signal.h> |
| 21 | |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <android-base/unique_fd.h> |
| 26 | |
| 27 | #include "builtins.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 | |
| 33 | extern const std::string kInitContext; |
| 34 | extern const std::string kVendorContext; |
| 35 | |
| 36 | class Subcontext { |
| 37 | public: |
| 38 | Subcontext(std::string path_prefix, std::string context) |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 39 | : path_prefix_(std::move(path_prefix)), context_(std::move(context)), pid_(0) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 40 | Fork(); |
| 41 | } |
| 42 | |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame^] | 43 | Result<Success> Execute(const std::vector<std::string>& args); |
| 44 | Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 45 | void Restart(); |
| 46 | |
| 47 | const std::string& path_prefix() const { return path_prefix_; } |
| 48 | const std::string& context() const { return context_; } |
| 49 | pid_t pid() const { return pid_; } |
| 50 | |
| 51 | private: |
| 52 | void Fork(); |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame^] | 53 | Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 54 | |
| 55 | std::string path_prefix_; |
| 56 | std::string context_; |
| 57 | pid_t pid_; |
| 58 | android::base::unique_fd socket_; |
| 59 | }; |
| 60 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 61 | int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map); |
| 62 | std::vector<Subcontext>* InitializeSubcontexts(); |
| 63 | bool SubcontextChildReap(pid_t pid); |
| 64 | |
| 65 | } // namespace init |
| 66 | } // namespace android |
| 67 | |
| 68 | #endif |