blob: 5601b802235f2c2dd3af7bfc2344643613acd757 [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
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 Cherry618d3102018-01-19 14:25:48 -080028#include "result.h"
Tom Cherryc49719f2018-01-10 11:04:34 -080029#include "system/core/init/subcontext.pb.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070030
31namespace android {
32namespace init {
33
34extern const std::string kInitContext;
35extern const std::string kVendorContext;
36
37class Subcontext {
38 public:
39 Subcontext(std::string path_prefix, std::string context)
Tom Cherrye6d37cd2017-10-19 14:15:37 -070040 : path_prefix_(std::move(path_prefix)), context_(std::move(context)), pid_(0) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070041 Fork();
42 }
43
Tom Cherryc49719f2018-01-10 11:04:34 -080044 Result<Success> Execute(const std::vector<std::string>& args);
45 Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070046 void Restart();
47
48 const std::string& path_prefix() const { return path_prefix_; }
49 const std::string& context() const { return context_; }
50 pid_t pid() const { return pid_; }
51
52 private:
53 void Fork();
Tom Cherryc49719f2018-01-10 11:04:34 -080054 Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070055
56 std::string path_prefix_;
57 std::string context_;
58 pid_t pid_;
59 android::base::unique_fd socket_;
60};
61
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070062int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map);
63std::vector<Subcontext>* InitializeSubcontexts();
64bool SubcontextChildReap(pid_t pid);
65
66} // namespace init
67} // namespace android
68
69#endif