-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ebd948
commit 85c8130
Showing
15 changed files
with
693 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
** Copyright 2024 Centreon | ||
** | ||
** 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. | ||
** | ||
** For more information : [email protected] | ||
*/ | ||
#ifndef CENTREON_AGENT_CHECK_PROCESS_HH | ||
#define CENTREON_AGENT_CHECK_PROCESS_HH | ||
|
||
namespace com::centreon::common { | ||
|
||
namespace detail { | ||
// here to limit included files | ||
struct boost_process; | ||
} // namespace detail | ||
|
||
/** | ||
* @brief This class allow to exec a process asynchronously. | ||
* It's a base class. If you want to get stdin and stdout returned data, you | ||
* must inherit from this and override on_stdout_read and on_stderr_read | ||
* You can call start_process at any moment, if a process is already running, | ||
* it's killed | ||
* As we can start a process at any moment, all handlers take a caller in | ||
* parameter, if this caller is not equal to current _proc, we do nothing. | ||
* When handler like on_stdout_read are called, _protect is already locked | ||
*/ | ||
class process : public std::enable_shared_from_this<process> { | ||
std::shared_ptr<asio::io_context> _io_context; | ||
std::shared_ptr<spdlog::logger> _logger; | ||
|
||
std::string _exe_path; | ||
std::vector<std::string> _args; | ||
|
||
std::deque<std::shared_ptr<std::string>> _stdin_write_queue; | ||
bool _write_pending = false; | ||
|
||
// it would be better to user an unique_ptr but gcc complains because he | ||
// doesn't know boost_process size | ||
detail::boost_process* _proc = nullptr; | ||
|
||
int _exit_status = 0; | ||
|
||
std::mutex _protect; | ||
|
||
void stdin_write_no_lock(const std::shared_ptr<std::string>& data); | ||
void stdin_write(const std::shared_ptr<std::string>& data); | ||
|
||
void stdout_read(); | ||
void stderr_read(); | ||
|
||
protected: | ||
char _stdout_read_buffer[0x1000]; | ||
char _stderr_read_buffer[0x1000]; | ||
|
||
virtual void on_stdout_read(const boost::system::error_code& err, | ||
size_t nb_read); | ||
virtual void on_stderr_read(const boost::system::error_code& err, | ||
size_t nb_read); | ||
|
||
virtual void on_process_end(const boost::system::error_code& err, | ||
int raw_exit_status); | ||
|
||
virtual void on_stdin_write(const boost::system::error_code& err); | ||
|
||
public: | ||
template <typename string_iterator> | ||
process(const std::shared_ptr<boost::asio::io_context>& io_context, | ||
const std::shared_ptr<spdlog::logger>& logger, | ||
const std::string& exe_path, | ||
string_iterator arg_begin, | ||
string_iterator arg_end); | ||
|
||
template <typename args_container> | ||
process(const std::shared_ptr<boost::asio::io_context>& io_context, | ||
const std::shared_ptr<spdlog::logger>& logger, | ||
const std::string& exe_path, | ||
const args_container& args); | ||
|
||
template <typename string_type> | ||
process(const std::shared_ptr<boost::asio::io_context>& io_context, | ||
const std::shared_ptr<spdlog::logger>& logger, | ||
const std::string& exe_path, | ||
const std::initializer_list<string_type>& args); | ||
|
||
process(const std::shared_ptr<boost::asio::io_context>& io_context, | ||
const std::shared_ptr<spdlog::logger>& logger, | ||
const std::string& cmd_line); | ||
|
||
virtual ~process(); | ||
|
||
template <typename string_class> | ||
void write_to_stdin(const string_class& content); | ||
|
||
void start_process(); | ||
|
||
void kill(); | ||
|
||
int get_exit_status() const { return _exit_status; } | ||
}; | ||
|
||
template <typename string_iterator> | ||
process::process(const std::shared_ptr<asio::io_context>& io_context, | ||
const std::shared_ptr<spdlog::logger>& logger, | ||
const std::string& exe_path, | ||
string_iterator arg_begin, | ||
string_iterator arg_end) | ||
: _io_context(io_context), | ||
_logger(logger), | ||
_exe_path(exe_path), | ||
_args(arg_begin, arg_end) {} | ||
|
||
template <typename args_container> | ||
process::process(const std::shared_ptr<boost::asio::io_context>& io_context, | ||
const std::shared_ptr<spdlog::logger>& logger, | ||
const std::string& exe_path, | ||
const args_container& args) | ||
: _io_context(io_context), | ||
_logger(logger), | ||
_exe_path(exe_path), | ||
_args(args) {} | ||
|
||
template <typename string_type> | ||
process::process(const std::shared_ptr<boost::asio::io_context>& io_context, | ||
const std::shared_ptr<spdlog::logger>& logger, | ||
const std::string& exe_path, | ||
const std::initializer_list<string_type>& args) | ||
: _io_context(io_context), _logger(logger), _exe_path(exe_path) { | ||
_args.reserve(args.size()); | ||
for (const auto& str : args) { | ||
_args.emplace_back(str); | ||
} | ||
} | ||
|
||
template <typename string_class> | ||
void process::write_to_stdin(const string_class& content) { | ||
stdin_write(std::make_shared<std::string>(content)); | ||
} | ||
|
||
} // namespace com::centreon::common | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.