| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1 | /* | 
 | 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 | #include "subcontext.h" | 
 | 18 |  | 
 | 19 | #include <fcntl.h> | 
 | 20 | #include <poll.h> | 
 | 21 | #include <sys/socket.h> | 
 | 22 | #include <unistd.h> | 
 | 23 |  | 
 | 24 | #include <android-base/file.h> | 
 | 25 | #include <android-base/logging.h> | 
 | 26 | #include <android-base/strings.h> | 
 | 27 | #include <selinux/android.h> | 
 | 28 |  | 
 | 29 | #include "action.h" | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 30 | #include "util.h" | 
 | 31 |  | 
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 32 | #if defined(__ANDROID__) | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 33 | #include <android/api-level.h> | 
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 34 | #include "property_service.h" | 
| Vic Yang | 92c236e | 2019-05-28 15:58:35 -0700 | [diff] [blame] | 35 | #include "selabel.h" | 
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 36 | #include "selinux.h" | 
 | 37 | #else | 
 | 38 | #include "host_init_stubs.h" | 
 | 39 | #endif | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 40 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 41 | using android::base::GetExecutablePath; | 
 | 42 | using android::base::Join; | 
 | 43 | using android::base::Socketpair; | 
 | 44 | using android::base::Split; | 
 | 45 | using android::base::StartsWith; | 
 | 46 | using android::base::unique_fd; | 
 | 47 |  | 
 | 48 | namespace android { | 
 | 49 | namespace init { | 
 | 50 |  | 
| Tom Cherry | ac7428b | 2017-10-02 16:59:02 -0700 | [diff] [blame] | 51 | const std::string kInitContext = "u:r:init:s0"; | 
 | 52 | const std::string kVendorContext = "u:r:vendor_init:s0"; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 53 |  | 
| Tom Cherry | dc37586 | 2018-02-28 10:39:01 -0800 | [diff] [blame] | 54 | const char* const paths_and_secontexts[2][2] = { | 
 | 55 |     {"/vendor", kVendorContext.c_str()}, | 
 | 56 |     {"/odm", kVendorContext.c_str()}, | 
 | 57 | }; | 
 | 58 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 59 | namespace { | 
 | 60 |  | 
 | 61 | constexpr size_t kBufferSize = 4096; | 
 | 62 |  | 
 | 63 | Result<std::string> ReadMessage(int socket) { | 
 | 64 |     char buffer[kBufferSize] = {}; | 
 | 65 |     auto result = TEMP_FAILURE_RETRY(recv(socket, buffer, sizeof(buffer), 0)); | 
| Luis Hector Chavez | 7235359 | 2018-09-21 12:27:02 -0700 | [diff] [blame] | 66 |     if (result == 0) { | 
 | 67 |         return Error(); | 
 | 68 |     } else if (result < 0) { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 69 |         return ErrnoError(); | 
 | 70 |     } | 
 | 71 |     return std::string(buffer, result); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | template <typename T> | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 75 | Result<void> SendMessage(int socket, const T& message) { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 76 |     std::string message_string; | 
 | 77 |     if (!message.SerializeToString(&message_string)) { | 
 | 78 |         return Error() << "Unable to serialize message"; | 
 | 79 |     } | 
 | 80 |  | 
 | 81 |     if (message_string.size() > kBufferSize) { | 
 | 82 |         return Error() << "Serialized message too long to send"; | 
 | 83 |     } | 
 | 84 |  | 
 | 85 |     if (auto result = | 
 | 86 |             TEMP_FAILURE_RETRY(send(socket, message_string.c_str(), message_string.size(), 0)); | 
 | 87 |         result != static_cast<long>(message_string.size())) { | 
 | 88 |         return ErrnoError() << "send() failed to send message contents"; | 
 | 89 |     } | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 90 |     return {}; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 91 | } | 
 | 92 |  | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 93 | std::vector<std::pair<std::string, std::string>> properties_to_set; | 
 | 94 |  | 
 | 95 | uint32_t SubcontextPropertySet(const std::string& name, const std::string& value) { | 
 | 96 |     properties_to_set.emplace_back(name, value); | 
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 97 |     return 0; | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 98 | } | 
 | 99 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 100 | class SubcontextProcess { | 
 | 101 |   public: | 
 | 102 |     SubcontextProcess(const KeywordFunctionMap* function_map, std::string context, int init_fd) | 
 | 103 |         : function_map_(function_map), context_(std::move(context)), init_fd_(init_fd){}; | 
 | 104 |     void MainLoop(); | 
 | 105 |  | 
 | 106 |   private: | 
 | 107 |     void RunCommand(const SubcontextCommand::ExecuteCommand& execute_command, | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 108 |                     SubcontextReply* reply) const; | 
 | 109 |     void ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command, | 
 | 110 |                     SubcontextReply* reply) const; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 111 |  | 
 | 112 |     const KeywordFunctionMap* function_map_; | 
 | 113 |     const std::string context_; | 
 | 114 |     const int init_fd_; | 
 | 115 | }; | 
 | 116 |  | 
 | 117 | void SubcontextProcess::RunCommand(const SubcontextCommand::ExecuteCommand& execute_command, | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 118 |                                    SubcontextReply* reply) const { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 119 |     // Need to use ArraySplice instead of this code. | 
 | 120 |     auto args = std::vector<std::string>(); | 
 | 121 |     for (const auto& string : execute_command.args()) { | 
 | 122 |         args.emplace_back(string); | 
 | 123 |     } | 
 | 124 |  | 
 | 125 |     auto map_result = function_map_->FindFunction(args); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 126 |     Result<void> result; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 127 |     if (!map_result) { | 
 | 128 |         result = Error() << "Cannot find command: " << map_result.error(); | 
 | 129 |     } else { | 
 | 130 |         result = RunBuiltinFunction(map_result->second, args, context_); | 
 | 131 |     } | 
 | 132 |  | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 133 |     for (const auto& [name, value] : properties_to_set) { | 
 | 134 |         auto property = reply->add_properties_to_set(); | 
 | 135 |         property->set_name(name); | 
 | 136 |         property->set_value(value); | 
 | 137 |     } | 
 | 138 |  | 
 | 139 |     properties_to_set.clear(); | 
 | 140 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 141 |     if (result) { | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 142 |         reply->set_success(true); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 143 |     } else { | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 144 |         auto* failure = reply->mutable_failure(); | 
| Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 145 |         failure->set_error_string(result.error().message()); | 
 | 146 |         failure->set_error_errno(result.error().code()); | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 147 |     } | 
 | 148 | } | 
 | 149 |  | 
 | 150 | void SubcontextProcess::ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command, | 
 | 151 |                                    SubcontextReply* reply) const { | 
 | 152 |     for (const auto& arg : expand_args_command.args()) { | 
 | 153 |         auto expanded_prop = std::string{}; | 
 | 154 |         if (!expand_props(arg, &expanded_prop)) { | 
 | 155 |             auto* failure = reply->mutable_failure(); | 
 | 156 |             failure->set_error_string("Failed to expand '" + arg + "'"); | 
 | 157 |             failure->set_error_errno(0); | 
 | 158 |             return; | 
 | 159 |         } else { | 
 | 160 |             auto* expand_args_reply = reply->mutable_expand_args_reply(); | 
 | 161 |             expand_args_reply->add_expanded_args(expanded_prop); | 
 | 162 |         } | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 163 |     } | 
 | 164 | } | 
 | 165 |  | 
 | 166 | void SubcontextProcess::MainLoop() { | 
 | 167 |     pollfd ufd[1]; | 
 | 168 |     ufd[0].events = POLLIN; | 
 | 169 |     ufd[0].fd = init_fd_; | 
 | 170 |  | 
 | 171 |     while (true) { | 
 | 172 |         ufd[0].revents = 0; | 
 | 173 |         int nr = TEMP_FAILURE_RETRY(poll(ufd, arraysize(ufd), -1)); | 
 | 174 |         if (nr == 0) continue; | 
 | 175 |         if (nr < 0) { | 
 | 176 |             PLOG(FATAL) << "poll() of subcontext socket failed, continuing"; | 
 | 177 |         } | 
 | 178 |  | 
 | 179 |         auto init_message = ReadMessage(init_fd_); | 
 | 180 |         if (!init_message) { | 
| Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 181 |             if (init_message.error().code() == 0) { | 
| Luis Hector Chavez | 7235359 | 2018-09-21 12:27:02 -0700 | [diff] [blame] | 182 |                 // If the init file descriptor was closed, let's exit quietly. If | 
 | 183 |                 // this was accidental, init will restart us. If init died, this | 
 | 184 |                 // avoids calling abort(3) unnecessarily. | 
 | 185 |                 return; | 
 | 186 |             } | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 187 |             LOG(FATAL) << "Could not read message from init: " << init_message.error(); | 
 | 188 |         } | 
 | 189 |  | 
 | 190 |         auto subcontext_command = SubcontextCommand(); | 
 | 191 |         if (!subcontext_command.ParseFromString(*init_message)) { | 
 | 192 |             LOG(FATAL) << "Unable to parse message from init"; | 
 | 193 |         } | 
 | 194 |  | 
 | 195 |         auto reply = SubcontextReply(); | 
 | 196 |         switch (subcontext_command.command_case()) { | 
 | 197 |             case SubcontextCommand::kExecuteCommand: { | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 198 |                 RunCommand(subcontext_command.execute_command(), &reply); | 
 | 199 |                 break; | 
 | 200 |             } | 
 | 201 |             case SubcontextCommand::kExpandArgsCommand: { | 
 | 202 |                 ExpandArgs(subcontext_command.expand_args_command(), &reply); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 203 |                 break; | 
 | 204 |             } | 
 | 205 |             default: | 
 | 206 |                 LOG(FATAL) << "Unknown message type from init: " | 
 | 207 |                            << subcontext_command.command_case(); | 
 | 208 |         } | 
 | 209 |  | 
 | 210 |         if (auto result = SendMessage(init_fd_, reply); !result) { | 
 | 211 |             LOG(FATAL) << "Failed to send message to init: " << result.error(); | 
 | 212 |         } | 
 | 213 |     } | 
 | 214 | } | 
 | 215 |  | 
 | 216 | }  // namespace | 
 | 217 |  | 
 | 218 | int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map) { | 
 | 219 |     if (argc < 4) LOG(FATAL) << "Fewer than 4 args specified to subcontext (" << argc << ")"; | 
 | 220 |  | 
 | 221 |     auto context = std::string(argv[2]); | 
 | 222 |     auto init_fd = std::atoi(argv[3]); | 
 | 223 |  | 
| Tom Cherry | 0d1452e | 2017-10-19 14:39:35 -0700 | [diff] [blame] | 224 |     SelabelInitialize(); | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 225 |  | 
 | 226 |     property_set = SubcontextPropertySet; | 
 | 227 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 228 |     auto subcontext_process = SubcontextProcess(function_map, context, init_fd); | 
 | 229 |     subcontext_process.MainLoop(); | 
 | 230 |     return 0; | 
 | 231 | } | 
 | 232 |  | 
 | 233 | void Subcontext::Fork() { | 
 | 234 |     unique_fd subcontext_socket; | 
 | 235 |     if (!Socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, &socket_, &subcontext_socket)) { | 
 | 236 |         LOG(FATAL) << "Could not create socket pair to communicate to subcontext"; | 
 | 237 |         return; | 
 | 238 |     } | 
 | 239 |  | 
 | 240 |     auto result = fork(); | 
 | 241 |  | 
 | 242 |     if (result == -1) { | 
 | 243 |         LOG(FATAL) << "Could not fork subcontext"; | 
 | 244 |     } else if (result == 0) { | 
 | 245 |         socket_.reset(); | 
 | 246 |  | 
 | 247 |         // We explicitly do not use O_CLOEXEC here, such that we can reference this FD by number | 
 | 248 |         // in the subcontext process after we exec. | 
 | 249 |         int child_fd = dup(subcontext_socket); | 
 | 250 |         if (child_fd < 0) { | 
 | 251 |             PLOG(FATAL) << "Could not dup child_fd"; | 
 | 252 |         } | 
 | 253 |  | 
 | 254 |         if (setexeccon(context_.c_str()) < 0) { | 
 | 255 |             PLOG(FATAL) << "Could not set execcon for '" << context_ << "'"; | 
 | 256 |         } | 
 | 257 |  | 
 | 258 |         auto init_path = GetExecutablePath(); | 
 | 259 |         auto child_fd_string = std::to_string(child_fd); | 
 | 260 |         const char* args[] = {init_path.c_str(), "subcontext", context_.c_str(), | 
 | 261 |                               child_fd_string.c_str(), nullptr}; | 
 | 262 |         execv(init_path.data(), const_cast<char**>(args)); | 
 | 263 |  | 
 | 264 |         PLOG(FATAL) << "Could not execv subcontext init"; | 
 | 265 |     } else { | 
 | 266 |         subcontext_socket.reset(); | 
 | 267 |         pid_ = result; | 
 | 268 |         LOG(INFO) << "Forked subcontext for '" << context_ << "' with pid " << pid_; | 
 | 269 |     } | 
 | 270 | } | 
 | 271 |  | 
 | 272 | void Subcontext::Restart() { | 
 | 273 |     LOG(ERROR) << "Restarting subcontext '" << context_ << "'"; | 
 | 274 |     if (pid_) { | 
 | 275 |         kill(pid_, SIGKILL); | 
 | 276 |     } | 
 | 277 |     pid_ = 0; | 
 | 278 |     socket_.reset(); | 
 | 279 |     Fork(); | 
 | 280 | } | 
 | 281 |  | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 282 | Result<SubcontextReply> Subcontext::TransmitMessage(const SubcontextCommand& subcontext_command) { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 283 |     if (auto result = SendMessage(socket_, subcontext_command); !result) { | 
 | 284 |         Restart(); | 
 | 285 |         return ErrnoError() << "Failed to send message to subcontext"; | 
 | 286 |     } | 
 | 287 |  | 
 | 288 |     auto subcontext_message = ReadMessage(socket_); | 
 | 289 |     if (!subcontext_message) { | 
 | 290 |         Restart(); | 
 | 291 |         return Error() << "Failed to receive result from subcontext: " << subcontext_message.error(); | 
 | 292 |     } | 
 | 293 |  | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 294 |     auto subcontext_reply = SubcontextReply{}; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 295 |     if (!subcontext_reply.ParseFromString(*subcontext_message)) { | 
 | 296 |         Restart(); | 
 | 297 |         return Error() << "Unable to parse message from subcontext"; | 
 | 298 |     } | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 299 |     return subcontext_reply; | 
 | 300 | } | 
 | 301 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 302 | Result<void> Subcontext::Execute(const std::vector<std::string>& args) { | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 303 |     auto subcontext_command = SubcontextCommand(); | 
 | 304 |     std::copy( | 
 | 305 |         args.begin(), args.end(), | 
 | 306 |         RepeatedPtrFieldBackInserter(subcontext_command.mutable_execute_command()->mutable_args())); | 
 | 307 |  | 
 | 308 |     auto subcontext_reply = TransmitMessage(subcontext_command); | 
 | 309 |     if (!subcontext_reply) { | 
 | 310 |         return subcontext_reply.error(); | 
 | 311 |     } | 
 | 312 |  | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 313 |     for (const auto& property : subcontext_reply->properties_to_set()) { | 
 | 314 |         ucred cr = {.pid = pid_, .uid = 0, .gid = 0}; | 
| Tom Cherry | 69d47aa | 2018-03-01 11:00:57 -0800 | [diff] [blame] | 315 |         std::string error; | 
 | 316 |         if (HandlePropertySet(property.name(), property.value(), context_, cr, &error) != 0) { | 
 | 317 |             LOG(ERROR) << "Subcontext init could not set '" << property.name() << "' to '" | 
 | 318 |                        << property.value() << "': " << error; | 
 | 319 |         } | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 320 |     } | 
 | 321 |  | 
 | 322 |     if (subcontext_reply->reply_case() == SubcontextReply::kFailure) { | 
 | 323 |         auto& failure = subcontext_reply->failure(); | 
 | 324 |         return ResultError(failure.error_string(), failure.error_errno()); | 
 | 325 |     } | 
 | 326 |  | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 327 |     if (subcontext_reply->reply_case() != SubcontextReply::kSuccess) { | 
 | 328 |         return Error() << "Unexpected message type from subcontext: " | 
 | 329 |                        << subcontext_reply->reply_case(); | 
 | 330 |     } | 
 | 331 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 332 |     return {}; | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 333 | } | 
 | 334 |  | 
 | 335 | Result<std::vector<std::string>> Subcontext::ExpandArgs(const std::vector<std::string>& args) { | 
 | 336 |     auto subcontext_command = SubcontextCommand{}; | 
 | 337 |     std::copy(args.begin(), args.end(), | 
 | 338 |               RepeatedPtrFieldBackInserter( | 
 | 339 |                   subcontext_command.mutable_expand_args_command()->mutable_args())); | 
 | 340 |  | 
 | 341 |     auto subcontext_reply = TransmitMessage(subcontext_command); | 
 | 342 |     if (!subcontext_reply) { | 
 | 343 |         return subcontext_reply.error(); | 
 | 344 |     } | 
 | 345 |  | 
| Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 346 |     if (subcontext_reply->reply_case() == SubcontextReply::kFailure) { | 
 | 347 |         auto& failure = subcontext_reply->failure(); | 
 | 348 |         return ResultError(failure.error_string(), failure.error_errno()); | 
 | 349 |     } | 
 | 350 |  | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 351 |     if (subcontext_reply->reply_case() != SubcontextReply::kExpandArgsReply) { | 
 | 352 |         return Error() << "Unexpected message type from subcontext: " | 
 | 353 |                        << subcontext_reply->reply_case(); | 
 | 354 |     } | 
 | 355 |  | 
 | 356 |     auto& reply = subcontext_reply->expand_args_reply(); | 
 | 357 |     auto expanded_args = std::vector<std::string>{}; | 
 | 358 |     for (const auto& string : reply.expanded_args()) { | 
 | 359 |         expanded_args.emplace_back(string); | 
 | 360 |     } | 
 | 361 |     return expanded_args; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 362 | } | 
 | 363 |  | 
 | 364 | static std::vector<Subcontext> subcontexts; | 
| Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 365 | static bool shutting_down; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 366 |  | 
 | 367 | std::vector<Subcontext>* InitializeSubcontexts() { | 
| Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 368 |     if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) { | 
| Tom Cherry | a1dbeb8 | 2018-04-11 15:50:00 -0700 | [diff] [blame] | 369 |         for (const auto& [path_prefix, secontext] : paths_and_secontexts) { | 
 | 370 |             subcontexts.emplace_back(path_prefix, secontext); | 
 | 371 |         } | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 372 |     } | 
 | 373 |     return &subcontexts; | 
 | 374 | } | 
 | 375 |  | 
 | 376 | bool SubcontextChildReap(pid_t pid) { | 
 | 377 |     for (auto& subcontext : subcontexts) { | 
 | 378 |         if (subcontext.pid() == pid) { | 
| Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 379 |             if (!shutting_down) { | 
 | 380 |                 subcontext.Restart(); | 
 | 381 |             } | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 382 |             return true; | 
 | 383 |         } | 
 | 384 |     } | 
 | 385 |     return false; | 
 | 386 | } | 
 | 387 |  | 
| Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 388 | void SubcontextTerminate() { | 
 | 389 |     shutting_down = true; | 
 | 390 |     for (auto& subcontext : subcontexts) { | 
 | 391 |         kill(subcontext.pid(), SIGTERM); | 
 | 392 |     } | 
 | 393 | } | 
 | 394 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 395 | }  // namespace init | 
 | 396 | }  // namespace android |