82 lines
2.0 KiB
C++
82 lines
2.0 KiB
C++
/*
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "common/libs/utils/result.h"
|
|
#include "common/libs/utils/subprocess.h"
|
|
|
|
namespace cuttlefish {
|
|
|
|
struct MonitorEntry {
|
|
std::unique_ptr<Command> cmd;
|
|
std::unique_ptr<Subprocess> proc;
|
|
};
|
|
|
|
// Keeps track of launched subprocesses, restarts them if they unexpectedly exit
|
|
class ProcessMonitor {
|
|
public:
|
|
class Properties {
|
|
public:
|
|
Properties& RestartSubprocesses(bool) &;
|
|
Properties RestartSubprocesses(bool) &&;
|
|
|
|
Properties& AddCommand(Command) &;
|
|
Properties AddCommand(Command) &&;
|
|
|
|
template <typename T>
|
|
Properties& AddCommands(T commands) & {
|
|
for (auto& command : commands) {
|
|
AddCommand(std::move(command));
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
template <typename T>
|
|
Properties AddCommands(T commands) && {
|
|
for (auto& command : commands) {
|
|
AddCommand(std::move(command));
|
|
}
|
|
return std::move(*this);
|
|
}
|
|
|
|
private:
|
|
bool restart_subprocesses_;
|
|
std::vector<MonitorEntry> entries_;
|
|
|
|
friend class ProcessMonitor;
|
|
};
|
|
ProcessMonitor(Properties&&);
|
|
|
|
// Start all processes given by AddCommand.
|
|
Result<void> StartAndMonitorProcesses();
|
|
// Stops all monitored subprocesses.
|
|
Result<void> StopMonitoredProcesses();
|
|
|
|
private:
|
|
Result<void> MonitorRoutine();
|
|
|
|
Properties properties_;
|
|
pid_t monitor_;
|
|
SharedFD monitor_socket_;
|
|
};
|
|
|
|
} // namespace cuttlefish
|