blob: cb4138e69f3880af184e1f1a4960c58b37ddebf1 [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:
Daniel Normanf597fa52020-11-09 17:28:24 -080039 Subcontext(std::vector<std::string> path_prefixes, std::string context, bool host = false)
Tom Cherry14c24722019-09-18 13:47:19 -070040 : path_prefixes_(std::move(path_prefixes)), context_(std::move(context)), pid_(0) {
Daniel Normanf597fa52020-11-09 17:28:24 -080041 if (!host) {
42 Fork();
43 }
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070044 }
45
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070046 Result<void> Execute(const std::vector<std::string>& args);
Tom Cherryc49719f2018-01-10 11:04:34 -080047 Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070048 void Restart();
Tom Cherry14c24722019-09-18 13:47:19 -070049 bool PathMatchesSubcontext(const std::string& path);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070050
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070051 const std::string& context() const { return context_; }
52 pid_t pid() const { return pid_; }
53
54 private:
55 void Fork();
Tom Cherryc49719f2018-01-10 11:04:34 -080056 Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070057
Tom Cherry14c24722019-09-18 13:47:19 -070058 std::vector<std::string> path_prefixes_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070059 std::string context_;
60 pid_t pid_;
61 android::base::unique_fd socket_;
62};
63
Tom Cherryd52a5b32019-07-22 16:05:36 -070064int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map);
Tom Cherrye3e77d32020-04-28 13:55:19 -070065void InitializeSubcontext();
Daniel Normanf597fa52020-11-09 17:28:24 -080066void InitializeHostSubcontext(std::vector<std::string> vendor_prefixes);
Tom Cherrye3e77d32020-04-28 13:55:19 -070067Subcontext* GetSubcontext();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070068bool SubcontextChildReap(pid_t pid);
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -070069void SubcontextTerminate();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070070
71} // namespace init
72} // namespace android