Skip to content

Commit

Permalink
[CMK-2375] - correct support for cmk-update-agent.exe
Browse files Browse the repository at this point in the history
- two modes of the start plugin: first is job(normal situation), second is for updater
- no killing at the end of cmk-update
- polling for the data instead of the for exit code for updater
- fixed long tests
- partly tested(not all)
- refactored AppRunner
- fixed corresponding logging

Change-Id: Ifcb8a221a92bacc932adbf9933bc590573b00b2f
  • Loading branch information
s-kipnis committed Jul 12, 2019
1 parent 1ace0c2 commit 032af83
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 127 deletions.
5 changes: 3 additions & 2 deletions agents/wnx/include/tools/_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <fstream>
#include <string>
#include <string_view>
#include <tuple>

#include "tools/_raii.h"
Expand Down Expand Up @@ -71,7 +72,7 @@ inline bool RunDetachedCommand(const std::string& Command) {
// returns process id
// used during auto update
inline uint32_t RunStdCommand(
const std::wstring& Command, // full command with arguments
std::wstring_view Command, // full command with arguments
bool Wait, // important flag! set false when you are sure
BOOL InheritHandle = FALSE, // not optimal, but default
HANDLE Stdio = 0, // when we want to catch output
Expand All @@ -92,7 +93,7 @@ inline uint32_t RunStdCommand(
memset(&pi, 0, sizeof(pi));

if (::CreateProcessW(nullptr, // stupid windows want null here
const_cast<wchar_t*>(Command.c_str()), // win32!
const_cast<wchar_t*>(Command.data()), // win32!
nullptr, // security attribute
nullptr, // thread attribute
InheritHandle, // handle inheritance
Expand Down
84 changes: 58 additions & 26 deletions agents/wnx/src/common/wtools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,79 @@

namespace wtools {

uint32_t AppRunner::goExec(std::wstring CommandLine, bool Wait,
bool InheritHandle, bool PipeOutput) noexcept {
void AppRunner::prepareResources(std::wstring_view command_line,
bool create_pipe) noexcept {
if (create_pipe) {
stdio_.create();
stderr_.create();
}

cmd_line_ = command_line;
job_handle_ = nullptr;
process_handle_ = nullptr;
}

void AppRunner::cleanResources() noexcept {
job_handle_ = nullptr;
process_handle_ = nullptr;
stdio_.shutdown();
stderr_.shutdown();
}

// returns PID or 0,
uint32_t AppRunner::goExecAsJob(std::wstring_view CommandLine) noexcept {
try {
if (process_id_) {
XLOG::l.bp("Attempt to reuse AppRunner");
return 0;
}
if (PipeOutput) {
stdio_.create();
stderr_.create();
}
cmd_line_ = CommandLine;
job_handle_ = nullptr;
process_handle_ = nullptr;

if (use_job_) {
auto [pid, jh, ph] = cma::tools::RunStdCommandAsJob(
CommandLine.c_str(), InheritHandle, stdio_.getWrite(),
stderr_.getWrite());
// store data to reuse
process_id_ = pid;
job_handle_ = jh;
process_handle_ = ph;

} else
process_id_ = cma::tools::RunStdCommand(
CommandLine.c_str(), Wait, InheritHandle, stdio_.getWrite(),
stderr_.getWrite());
prepareResources(CommandLine, true);

auto [pid, jh, ph] = cma::tools::RunStdCommandAsJob(
CommandLine.data(), true, stdio_.getWrite(), stderr_.getWrite());
// store data to reuse
process_id_ = pid;
job_handle_ = jh;
process_handle_ = ph;

// check and return on success
if (process_id_) return process_id_;

// failure s here
XLOG::l(XLOG_FLINE + " Failed RunStd: [{}]*", GetLastError());

job_handle_ = nullptr;
process_handle_ = nullptr;
stdio_.shutdown();
stderr_.shutdown();
cleanResources();

return 0;
} catch (const std::exception& e) {
XLOG::l.crit(XLOG_FLINE + " unexpected exception: '{}'", e.what());
}
return 0;
}

// returns process id
uint32_t AppRunner::goExecAsUpdater(std::wstring_view CommandLine) noexcept {
try {
if (process_id_) {
XLOG::l.bp("Attempt to reuse AppRunner");
return 0;
}
prepareResources(CommandLine, true);

process_id_ = cma::tools::RunStdCommand(
CommandLine, false, true, stdio_.getWrite(), stderr_.getWrite(),
CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS);

// check and return on success
if (process_id_) return process_id_;

// failure s here
XLOG::l(XLOG_FLINE + " Failed RunStd: [{}]*", GetLastError());

cleanResources();
return 0;

} catch (const std::exception& e) {
XLOG::l.crit(XLOG_FLINE + " unexpected exception: '{}'", e.what());
}
Expand Down
14 changes: 11 additions & 3 deletions agents/wnx/src/common/wtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,10 @@ class AppRunner {
}

// returns process id
uint32_t goExec(std::wstring CommandLine, bool Wait, bool InheritHandle,
bool PipeOutput) noexcept;
uint32_t goExecAsJob(std::wstring_view CommandLine) noexcept;

// returns process id
uint32_t goExecAsUpdater(std::wstring_view CommandLine) noexcept;

void kill(bool KillTreeToo) {
auto proc_id = process_id_.exchange(0);
Expand Down Expand Up @@ -327,7 +329,9 @@ class AppRunner {
}

private:
const bool use_job_ = true;
void prepareResources(std::wstring_view command_line,
bool create_pipe) noexcept;
void cleanResources() noexcept;
void setExitCode(uint32_t Code) { exit_code_ = Code; }
std::wstring cmd_line_;
std::atomic<uint32_t> process_id_;
Expand All @@ -339,6 +343,10 @@ class AppRunner {
// output
std::vector<char> data_;
uint32_t exit_code_;
#if defined(GTEST_INCLUDE_GTEST_GTEST_H_)
friend class Wtools;
FRIEND_TEST(Wtools, AppRunner);
#endif
};

class ServiceController {
Expand Down
2 changes: 2 additions & 0 deletions agents/wnx/src/engine/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,8 @@ struct Plugins : public Group {
void validateAndFix() {
if (cacheAge() >= kMinimumCacheAge) return;

if (!async_ && cacheAge() == 0) return;

XLOG::t(
"Plugin Entry '{}' has too low cache_age: [{}]. Setting at [{}]",
pattern_, cacheAge(), kMinimumCacheAge);
Expand Down

0 comments on commit 032af83

Please sign in to comment.