Android-cuttlefish cvd tool
process_monitor.h
Go to the documentation of this file.
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
30
31namespace cuttlefish {
32
34 std::unique_ptr<Command> cmd;
35 std::unique_ptr<Subprocess> proc;
37
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
45 public:
46 class Properties {
47 public:
50 Properties& StraceCommands(std::set<std::string>) &;
51 Properties& StraceLogDir(std::string) &;
52
53 private:
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.
68 // Stops all monitored subprocesses.
70 // Suspend all host subprocesses
72 // Resume all host subprocesses
74
75 /* Reads on this SharedFD will block while subprocesses are running, */
76 SharedFD status() { return status_; };
77
78 private:
79 Result<void> StartSubprocesses(Properties& properties);
81 Result<void> ReadMonitorSocketLoop(std::atomic_bool&);
82 /*
83 * The child run_cvd process suspends the host processes
84 */
86 /*
87 * The child run_cvd process resumes the host processes
88 */
90
94 pid_t monitor_;
95 std::optional<transport::SharedFdChannel> parent_channel_;
96 std::optional<transport::SharedFdChannel> child_channel_;
98
99 /*
100 * The lock that should be acquired when multiple threads
101 * access to properties_. Currently, used by the child
102 * run_cvd process that runs MonitorRoutine()
103 */
105};
106
107} // namespace cuttlefish
Definition: command.h:38
Definition: process_monitor.h:46
bool restart_subprocesses_
Definition: process_monitor.h:54
std::set< std::string > strace_commands_
Definition: process_monitor.h:56
Properties & AddCommand(MonitorCommand) &
Definition: process_monitor.cc:335
std::vector< MonitorEntry > entries_
Definition: process_monitor.h:55
std::string strace_log_dir_
Definition: process_monitor.h:57
Properties & StraceCommands(std::set< std::string >) &
Definition: process_monitor.cc:341
Properties & RestartSubprocesses(bool) &
Definition: process_monitor.cc:329
Properties & StraceLogDir(std::string) &
Definition: process_monitor.cc:347
Definition: process_monitor.h:44
std::mutex properties_mutex_
Definition: process_monitor.h:104
Properties properties_
Definition: process_monitor.h:91
std::optional< transport::SharedFdChannel > parent_channel_
Definition: process_monitor.h:95
Result< void > StopMonitoredProcesses()
Definition: process_monitor.cc:359
std::optional< transport::SharedFdChannel > child_channel_
Definition: process_monitor.h:96
pid_t monitor_
Definition: process_monitor.h:94
SharedFD status_
Definition: process_monitor.h:92
Result< void > StartSubprocesses(Properties &properties)
Definition: process_monitor.cc:256
Result< void > SuspendMonitoredProcesses()
Definition: process_monitor.cc:379
const SharedFD channel_to_secure_env_
Definition: process_monitor.h:93
Result< void > MonitorRoutine()
Definition: process_monitor.cc:432
Result< void > ResumeMonitoredProcesses()
Definition: process_monitor.cc:391
Result< void > SuspendHostProcessesImpl()
Definition: process_monitor.cc:311
SharedFD child_sock_
Definition: process_monitor.h:97
Result< void > ReadMonitorSocketLoop(std::atomic_bool &)
Definition: process_monitor.cc:278
ProcessMonitor(Properties &&, const SharedFD &secure_env_fd)
Definition: process_monitor.cc:353
Result< void > StartAndMonitorProcesses()
Definition: process_monitor.cc:403
Result< void > ResumeHostProcessesImpl()
Definition: process_monitor.cc:320
SharedFD status()
Definition: process_monitor.h:76
Definition: shared_fd.h:124
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
Definition: logging.h:464
Definition: command_source.h:29
Definition: process_monitor.h:33
bool is_critical
Definition: process_monitor.h:36
std::unique_ptr< Command > cmd
Definition: process_monitor.h:34
std::unique_ptr< Subprocess > proc
Definition: process_monitor.h:35
MonitorEntry(Command command, bool is_critical)
Definition: process_monitor.h:38