blob: 23c4a241c98f5623ce68700399ceb8815d6acc99 [file] [log] [blame]
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001/*
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 Cherry14c24722019-09-18 13:47:19 -070017#pragma once
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070018
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 Cherry618d3102018-01-19 14:25:48 -080027#include "result.h"
Tom Cherryc49719f2018-01-10 11:04:34 -080028#include "system/core/init/subcontext.pb.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070029
30namespace android {
31namespace init {
32
Tom Cherry14c24722019-09-18 13:47:19 -070033static constexpr const char kInitContext[] = "u:r:init:s0";
34static constexpr const char kVendorContext[] = "u:r:vendor_init:s0";
Tom Cherry1c005f32019-11-20 15:51:36 -080035static constexpr const char kTestContext[] = "test-test-test";
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070036
37class Subcontext {
38 public:
Chan Wang5996d602024-11-14 13:25:37 +000039 Subcontext(std::vector<std::string> path_prefixes, std::vector<std::string> partitions,
40 std::string_view context, bool host = false)
Bart Van Assche462ea552022-10-26 09:31:35 -070041 : path_prefixes_(std::move(path_prefixes)),
Chan Wang5996d602024-11-14 13:25:37 +000042 partitions_(std::move(partitions)),
Bart Van Assche462ea552022-10-26 09:31:35 -070043 context_(context.begin(), context.end()),
44 pid_(0) {
Daniel Normanf597fa52020-11-09 17:28:24 -080045 if (!host) {
46 Fork();
47 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070048 }
49
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070050 Result<void> Execute(const std::vector<std::string>& args);
Tom Cherryc49719f2018-01-10 11:04:34 -080051 Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070052 void Restart();
Jooyung Han38e8e742022-05-10 05:35:35 +090053 bool PathMatchesSubcontext(const std::string& path) const;
Chan Wang5996d602024-11-14 13:25:37 +000054 bool PartitionMatchesSubcontext(const std::string& partition) const;
Jooyung Han38e8e742022-05-10 05:35:35 +090055 void SetApexList(std::vector<std::string>&& apex_list);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070056
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070057 const std::string& context() const { return context_; }
58 pid_t pid() const { return pid_; }
59
60 private:
61 void Fork();
Tom Cherryc49719f2018-01-10 11:04:34 -080062 Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070063
Tom Cherry14c24722019-09-18 13:47:19 -070064 std::vector<std::string> path_prefixes_;
Chan Wang5996d602024-11-14 13:25:37 +000065 std::vector<std::string> partitions_;
Jooyung Han38e8e742022-05-10 05:35:35 +090066 std::vector<std::string> apex_list_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070067 std::string context_;
68 pid_t pid_;
69 android::base::unique_fd socket_;
70};
71
Tom Cherryd52a5b32019-07-22 16:05:36 -070072int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map);
Tom Cherrye3e77d32020-04-28 13:55:19 -070073void InitializeSubcontext();
Daniel Normanf597fa52020-11-09 17:28:24 -080074void InitializeHostSubcontext(std::vector<std::string> vendor_prefixes);
Tom Cherrye3e77d32020-04-28 13:55:19 -070075Subcontext* GetSubcontext();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070076bool SubcontextChildReap(pid_t pid);
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -070077void SubcontextTerminate();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070078
79} // namespace init
80} // namespace android