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;
38
40 : cmd(new Command(std::move(command))),
43
45 : cmd(new Command(std::move(command))),
49};
50
51// Launches and keeps track of subprocesses, decides response if they
52// unexpectedly exit
54 public:
55 class Properties {
56 public:
59 Properties& StraceCommands(std::set<std::string>) &;
60 Properties& StraceLogDir(std::string) &;
61
62 private:
64 std::vector<MonitorEntry> entries_;
65 std::set<std::string> strace_commands_;
66 std::string strace_log_dir_;
67
68 friend class ProcessMonitor;
69 };
70 /*
71 * secure_env_fd is to send suspend/resume commands to secure_env.
72 */
73 ProcessMonitor(Properties&&, const SharedFD& secure_env_fd);
74
75 // Start all processes given by AddCommand.
77 // Stops all monitored subprocesses.
79 // Suspend all host subprocesses
81 // Resume all host subprocesses
83
84 /* Reads on this SharedFD will block while subprocesses are running, */
85 SharedFD status() { return status_; };
86
87 private:
88 Result<void> StartSubprocesses(Properties& properties);
90 Result<void> ReadMonitorSocketLoop(std::atomic_bool&);
91 /*
92 * The child run_cvd process suspends the host processes
93 */
95 /*
96 * The child run_cvd process resumes the host processes
97 */
99
103 pid_t monitor_;
104 std::optional<transport::SharedFdChannel> parent_channel_;
105 std::optional<transport::SharedFdChannel> child_channel_;
107
108 /*
109 * The lock that should be acquired when multiple threads
110 * access to properties_. Currently, used by the child
111 * run_cvd process that runs MonitorRoutine()
112 */
114};
115
116} // namespace cuttlefish
Definition: command.h:38
Definition: process_monitor.h:55
bool restart_subprocesses_
Definition: process_monitor.h:63
std::set< std::string > strace_commands_
Definition: process_monitor.h:65
Properties & AddCommand(MonitorCommand) &
Definition: process_monitor.cc:361
std::vector< MonitorEntry > entries_
Definition: process_monitor.h:64
std::string strace_log_dir_
Definition: process_monitor.h:66
Properties & StraceCommands(std::set< std::string >) &
Definition: process_monitor.cc:367
Properties & RestartSubprocesses(bool) &
Definition: process_monitor.cc:355
Properties & StraceLogDir(std::string) &
Definition: process_monitor.cc:373
Definition: process_monitor.h:53
std::mutex properties_mutex_
Definition: process_monitor.h:113
Properties properties_
Definition: process_monitor.h:100
std::optional< transport::SharedFdChannel > parent_channel_
Definition: process_monitor.h:104
Result< void > StopMonitoredProcesses()
Definition: process_monitor.cc:385
std::optional< transport::SharedFdChannel > child_channel_
Definition: process_monitor.h:105
pid_t monitor_
Definition: process_monitor.h:103
SharedFD status_
Definition: process_monitor.h:101
Result< void > StartSubprocesses(Properties &properties)
Definition: process_monitor.cc:282
Result< void > SuspendMonitoredProcesses()
Definition: process_monitor.cc:416
const SharedFD channel_to_secure_env_
Definition: process_monitor.h:102
Result< void > MonitorRoutine()
Definition: process_monitor.cc:469
Result< void > ResumeMonitoredProcesses()
Definition: process_monitor.cc:428
Result< void > SuspendHostProcessesImpl()
Definition: process_monitor.cc:337
SharedFD child_sock_
Definition: process_monitor.h:106
Result< void > ReadMonitorSocketLoop(std::atomic_bool &)
Definition: process_monitor.cc:304
ProcessMonitor(Properties &&, const SharedFD &secure_env_fd)
Definition: process_monitor.cc:379
Result< void > StartAndMonitorProcesses()
Definition: process_monitor.cc:440
Result< void > ResumeHostProcessesImpl()
Definition: process_monitor.cc:346
SharedFD status()
Definition: process_monitor.h:85
Definition: shared_fd.h:125
Definition: alloc_driver.h:21
ProcessCategory
Definition: command_source.h:31
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
Definition: command_source.h:52
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
ProcessCategory category
Definition: process_monitor.h:37
MonitorEntry(Command command, bool is_critical)
Definition: process_monitor.h:44
MonitorEntry(Command command, ProcessCategory category)
Definition: process_monitor.h:39