| 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 | 618d310 | 2018-01-19 14:25:48 -0800 | [diff] [blame] | 28 | #include "result.h" | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 29 | #include "system/core/init/subcontext.pb.h" | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 | namespace init { | 
|  | 33 |  | 
|  | 34 | extern const std::string kInitContext; | 
|  | 35 | extern const std::string kVendorContext; | 
| Tom Cherry | dc37586 | 2018-02-28 10:39:01 -0800 | [diff] [blame] | 36 | extern const char* const paths_and_secontexts[2][2]; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | class Subcontext { | 
|  | 39 | public: | 
|  | 40 | Subcontext(std::string path_prefix, std::string context) | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 41 | : path_prefix_(std::move(path_prefix)), context_(std::move(context)), pid_(0) { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 42 | Fork(); | 
|  | 43 | } | 
|  | 44 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 45 | Result<void> Execute(const std::vector<std::string>& args); | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 46 | Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 47 | void Restart(); | 
|  | 48 |  | 
|  | 49 | const std::string& path_prefix() const { return path_prefix_; } | 
|  | 50 | const std::string& context() const { return context_; } | 
|  | 51 | pid_t pid() const { return pid_; } | 
|  | 52 |  | 
|  | 53 | private: | 
|  | 54 | void Fork(); | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 55 | Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | std::string path_prefix_; | 
|  | 58 | std::string context_; | 
|  | 59 | pid_t pid_; | 
|  | 60 | android::base::unique_fd socket_; | 
|  | 61 | }; | 
|  | 62 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 63 | int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 64 | std::vector<Subcontext>* InitializeSubcontexts(); | 
|  | 65 | bool SubcontextChildReap(pid_t pid); | 
| Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 66 | void SubcontextTerminate(); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 67 |  | 
|  | 68 | }  // namespace init | 
|  | 69 | }  // namespace android | 
|  | 70 |  | 
|  | 71 | #endif |