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 private:
78 Result<void> ReadMonitorSocketLoop(std::atomic_bool&);
79 /*
80 * The child run_cvd process suspends the host processes
81 */
83 /*
84 * The child run_cvd process resumes the host processes
85 */
87
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 */
100};
101
102} // namespace cuttlefish
Definition: expected.h:86
Definition: subprocess.h:139
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:327
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:333
Properties & RestartSubprocesses(bool) &
Definition: process_monitor.cc:321
Properties & StraceLogDir(std::string) &
Definition: process_monitor.cc:339
Definition: process_monitor.h:44
std::mutex properties_mutex_
Definition: process_monitor.h:99
Properties properties_
Definition: process_monitor.h:88
std::optional< transport::SharedFdChannel > parent_channel_
Definition: process_monitor.h:91
Result< void > StopMonitoredProcesses()
Definition: process_monitor.cc:351
std::optional< transport::SharedFdChannel > child_channel_
Definition: process_monitor.h:92
pid_t monitor_
Definition: process_monitor.h:90
Result< void > StartSubprocesses(Properties &properties)
Definition: process_monitor.cc:257
Result< void > SuspendMonitoredProcesses()
Definition: process_monitor.cc:371
const SharedFD channel_to_secure_env_
Definition: process_monitor.h:89
Result< void > MonitorRoutine()
Definition: process_monitor.cc:417
Result< void > ResumeMonitoredProcesses()
Definition: process_monitor.cc:383
Result< void > SuspendHostProcessesImpl()
Definition: process_monitor.cc:303
Result< void > ReadMonitorSocketLoop(std::atomic_bool &)
Definition: process_monitor.cc:278
ProcessMonitor(Properties &&, const SharedFD &secure_env_fd)
Definition: process_monitor.cc:345
Result< void > StartAndMonitorProcesses()
Definition: process_monitor.cc:395
Result< void > ResumeHostProcessesImpl()
Definition: process_monitor.cc:312
Definition: shared_fd.h:129
Definition: alloc_utils.cpp:23
Definition: logging.h:464
Definition: command_source.h:30
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