blob: ac77e08a83d381baea69ccf15736804900dba7d2 [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"
28
29namespace android {
30namespace init {
31
32extern const std::string kInitContext;
33extern const std::string kVendorContext;
34
35class Subcontext {
36 public:
37 Subcontext(std::string path_prefix, std::string context)
38 : path_prefix_(path_prefix), context_(std::move(context)) {
39 Fork();
40 }
41
42 Result<Success> Execute(const std::vector<std::string>& command);
43 void Restart();
44
45 const std::string& path_prefix() const { return path_prefix_; }
46 const std::string& context() const { return context_; }
47 pid_t pid() const { return pid_; }
48
49 private:
50 void Fork();
51
52 std::string path_prefix_;
53 std::string context_;
54 pid_t pid_;
55 android::base::unique_fd socket_;
56};
57
58// For testing, to kill the subcontext after the test has completed.
59class SubcontextKiller {
60 public:
61 SubcontextKiller(const Subcontext& subcontext) : subcontext_(subcontext) {}
62 ~SubcontextKiller() {
63 if (subcontext_.pid() > 0) {
64 kill(subcontext_.pid(), SIGTERM);
65 kill(subcontext_.pid(), SIGKILL);
66 }
67 }
68
69 private:
70 const Subcontext& subcontext_;
71};
72
73int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map);
74std::vector<Subcontext>* InitializeSubcontexts();
75bool SubcontextChildReap(pid_t pid);
76
77} // namespace init
78} // namespace android
79
80#endif