blob: bcaad2980a1203c44c35f1aec86d6b948cffcba7 [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 Cherrycb0f9bb2017-09-12 15:58:47 -070035
36class Subcontext {
37 public:
Tom Cherry14c24722019-09-18 13:47:19 -070038 Subcontext(std::vector<std::string> path_prefixes, std::string context)
39 : path_prefixes_(std::move(path_prefixes)), context_(std::move(context)), pid_(0) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070040 Fork();
41 }
42
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070043 Result<void> Execute(const std::vector<std::string>& args);
Tom Cherryc49719f2018-01-10 11:04:34 -080044 Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070045 void Restart();
Tom Cherry14c24722019-09-18 13:47:19 -070046 bool PathMatchesSubcontext(const std::string& path);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070047
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070048 const std::string& context() const { return context_; }
49 pid_t pid() const { return pid_; }
50
51 private:
52 void Fork();
Tom Cherryc49719f2018-01-10 11:04:34 -080053 Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070054
Tom Cherry14c24722019-09-18 13:47:19 -070055 std::vector<std::string> path_prefixes_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070056 std::string context_;
57 pid_t pid_;
58 android::base::unique_fd socket_;
59};
60
Tom Cherryd52a5b32019-07-22 16:05:36 -070061int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map);
Tom Cherry14c24722019-09-18 13:47:19 -070062std::unique_ptr<Subcontext> InitializeSubcontext();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070063bool SubcontextChildReap(pid_t pid);
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -070064void SubcontextTerminate();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070065
66} // namespace init
67} // namespace android