subcontext.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef _INIT_SUBCONTEXT_H
  17. #define _INIT_SUBCONTEXT_H
  18. #include <signal.h>
  19. #include <string>
  20. #include <vector>
  21. #include <android-base/unique_fd.h>
  22. #include "builtins.h"
  23. #include "result.h"
  24. #include "system/core/init/subcontext.pb.h"
  25. namespace android {
  26. namespace init {
  27. extern const std::string kInitContext;
  28. extern const std::string kVendorContext;
  29. extern const char* const paths_and_secontexts[2][2];
  30. class Subcontext {
  31. public:
  32. Subcontext(std::string path_prefix, std::string context)
  33. : path_prefix_(std::move(path_prefix)), context_(std::move(context)), pid_(0) {
  34. Fork();
  35. }
  36. Result<Success> Execute(const std::vector<std::string>& args);
  37. Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args);
  38. void Restart();
  39. const std::string& path_prefix() const { return path_prefix_; }
  40. const std::string& context() const { return context_; }
  41. pid_t pid() const { return pid_; }
  42. private:
  43. void Fork();
  44. Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command);
  45. std::string path_prefix_;
  46. std::string context_;
  47. pid_t pid_;
  48. android::base::unique_fd socket_;
  49. };
  50. int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map);
  51. std::vector<Subcontext>* InitializeSubcontexts();
  52. bool SubcontextChildReap(pid_t pid);
  53. void SubcontextTerminate();
  54. } // namespace init
  55. } // namespace android
  56. #endif