• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #pragma once
17 
18 #include <atomic>
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "common/libs/transport/channel_sharedfd.h"
27 #include "common/libs/utils/result.h"
28 #include "common/libs/utils/subprocess.h"
29 #include "host/libs/config/command_source.h"
30 
31 namespace cuttlefish {
32 
33 struct MonitorEntry {
34   std::unique_ptr<Command> cmd;
35   std::unique_ptr<Subprocess> proc;
36   bool is_critical;
37 
MonitorEntryMonitorEntry38   MonitorEntry(Command command, bool is_critical)
39       : cmd(new Command(std::move(command))), is_critical(is_critical) {}
40 };
41 
42 // Launches and keeps track of subprocesses, decides response if they
43 // unexpectedly exit
44 class ProcessMonitor {
45  public:
46   class Properties {
47    public:
48     Properties& RestartSubprocesses(bool) &;
49     Properties& AddCommand(MonitorCommand) &;
50     Properties& StraceCommands(std::set<std::string>) &;
51     Properties& StraceLogDir(std::string) &;
52 
53    private:
54     bool restart_subprocesses_;
55     std::vector<MonitorEntry> entries_;
56     std::set<std::string> strace_commands_;
57     std::string strace_log_dir_;
58 
59     friend class ProcessMonitor;
60   };
61   /*
62    * secure_env_fd is to send suspend/resume commands to secure_env.
63    */
64   ProcessMonitor(Properties&&, const SharedFD& secure_env_fd);
65 
66   // Start all processes given by AddCommand.
67   Result<void> StartAndMonitorProcesses();
68   // Stops all monitored subprocesses.
69   Result<void> StopMonitoredProcesses();
70   // Suspend all host subprocesses
71   Result<void> SuspendMonitoredProcesses();
72   // Resume all host subprocesses
73   Result<void> ResumeMonitoredProcesses();
74 
75  private:
76   Result<void> StartSubprocesses(Properties& properties);
77   Result<void> MonitorRoutine();
78   Result<void> ReadMonitorSocketLoop(std::atomic_bool&);
79   /*
80    * The child run_cvd process suspends the host processes
81    */
82   Result<void> SuspendHostProcessesImpl();
83   /*
84    * The child run_cvd process resumes the host processes
85    */
86   Result<void> ResumeHostProcessesImpl();
87 
88   Properties properties_;
89   const SharedFD channel_to_secure_env_;
90   pid_t monitor_;
91   std::optional<transport::SharedFdChannel> parent_channel_;
92   std::optional<transport::SharedFdChannel> child_channel_;
93 
94   /*
95    * The lock that should be acquired when multiple threads
96    * access to properties_. Currently, used by the child
97    * run_cvd process that runs MonitorRoutine()
98    */
99   std::mutex properties_mutex_;
100 };
101 
102 }  // namespace cuttlefish
103