summaryrefslogtreecommitdiff
path: root/src/system.hpp
diff options
context:
space:
mode:
authorKrzysztof Grobelny <krzysztof.grobelny@intel.com>2020-07-03 13:35:09 +0300
committerKarol Wachowski <karol.wachowski@intel.com>2020-07-17 09:48:46 +0300
commitd113e4284674d112aff0744fe734581bd3fc4abf (patch)
tree727b644c30a050f39d5fdd21452f40d70cf1df1a /src/system.hpp
parent1d453d987d5ece338aad08cee315fbacf179e692 (diff)
downloadvirtual-media-d113e4284674d112aff0744fe734581bd3fc4abf.tar.xz
Fixing multiple problems with state machine in virtual media
- Previously machine did not handle AnyEvent correctly, implementation in BaseState was always run - Changing from ActiveState to ReadyState was bugged, previously only one of event SubprocessStopped or UdevNotification caused state change when it is required to wait for both - Introduced longer timer when waiting for ReadyState during Eject and ActiveState during Inject, because ndbkit can timeout during Eject and it is required to complete before next inject can success. - Added event notification when process is terminated - Added resourcess classes to handle deletion and notifications Signed-off-by: Krzysztof Grobelny <krzysztof.grobelny@intel.com> Signed-off-by: Karol Wachowski <karol.wachowski@intel.com> Change-Id: Ie914e650c2f15bd73cdc87582ea77a94997a3472 Signed-off-by: Karol Wachowski <karol.wachowski@intel.com>
Diffstat (limited to 'src/system.hpp')
-rw-r--r--src/system.hpp107
1 files changed, 55 insertions, 52 deletions
diff --git a/src/system.hpp b/src/system.hpp
index a6dba84..a29b640 100644
--- a/src/system.hpp
+++ b/src/system.hpp
@@ -348,7 +348,7 @@ class DeviceMonitor
class Process : public std::enable_shared_from_this<Process>
{
public:
- Process(boost::asio::io_context& ioc, const std::string& name,
+ Process(boost::asio::io_context& ioc, std::string_view name,
const std::string& app, const NBDDevice& dev) :
ioc(ioc),
pipe(ioc), name(name), app(app), dev(dev)
@@ -372,70 +372,72 @@ class Process : public std::enable_shared_from_this<Process>
return false;
}
- boost::asio::spawn(
- ioc, [this, self = shared_from_this(),
- onExit{std::move(onExit)}](boost::asio::yield_context yield) {
- boost::system::error_code bec;
- std::string line;
- boost::asio::dynamic_string_buffer buffer{line};
- LogMsg(Logger::Info,
- "[Process]: Start reading console from nbd-client");
- while (1)
+ boost::asio::spawn(ioc, [this, self = shared_from_this(),
+ onExit = std::move(onExit)](
+ boost::asio::yield_context yield) {
+ boost::system::error_code bec;
+ std::string line;
+ boost::asio::dynamic_string_buffer buffer{line};
+ LogMsg(Logger::Info,
+ "[Process]: Start reading console from nbd-client");
+ while (1)
+ {
+ auto x = boost::asio::async_read_until(pipe, std::move(buffer),
+ '\n', yield[bec]);
+ auto lineBegin = line.begin();
+ while (lineBegin != line.end())
{
- auto x = boost::asio::async_read_until(
- pipe, std::move(buffer), '\n', yield[bec]);
- auto lineBegin = line.begin();
- while (lineBegin != line.end())
- {
- auto lineEnd = find(lineBegin, line.end(), '\n');
- LogMsg(Logger::Debug, "[Process]: (", name, ") ",
- std::string(lineBegin, lineEnd));
- if (lineEnd == line.end())
- {
- break;
- }
- lineBegin = lineEnd + 1;
- }
-
- buffer.consume(x);
- if (bec)
+ auto lineEnd = find(lineBegin, line.end(), '\n');
+ LogMsg(Logger::Debug, "[Process]: (", name, ") ",
+ std::string(lineBegin, lineEnd));
+ if (lineEnd == line.end())
{
- LogMsg(Logger::Debug, "[Process]: (", name,
- ") Loop Error: ", bec);
break;
}
+ lineBegin = lineEnd + 1;
}
- LogMsg(Logger::Info, "[Process]: Exiting from COUT Loop");
- // The process shall be dead, or almost here, give it a chance
- LogMsg(Logger::Debug,
- "[Process]: Waiting process to finish normally");
- boost::asio::steady_timer timer(ioc);
- int32_t waitCnt = 20;
- while (child.running() && waitCnt > 0)
- {
- boost::system::error_code ignored_ec;
- timer.expires_from_now(std::chrono::milliseconds(100));
- timer.async_wait(yield[ignored_ec]);
- waitCnt--;
- }
- if (child.running())
+
+ buffer.consume(x);
+ if (bec)
{
- child.terminate();
+ LogMsg(Logger::Debug, "[Process]: (", name,
+ ") Loop Error: ", bec);
+ break;
}
+ }
+ LogMsg(Logger::Info, "[Process]: Exiting from COUT Loop");
+ // The process shall be dead, or almost here, give it a chance
+ LogMsg(Logger::Debug,
+ "[Process]: Waiting process to finish normally");
+ boost::asio::steady_timer timer(ioc);
+ int32_t waitCnt = 20;
+ while (child.running() && waitCnt > 0)
+ {
+ boost::system::error_code ignored_ec;
+ timer.expires_from_now(std::chrono::milliseconds(100));
+ timer.async_wait(yield[ignored_ec]);
+ waitCnt--;
+ }
+ if (child.running())
+ {
+ child.terminate();
+ }
- child.wait();
- LogMsg(Logger::Info, "[Process]: running: ", child.running(),
- " EC: ", child.exit_code(),
- " Native: ", child.native_exit_code());
+ child.wait();
+ LogMsg(Logger::Info, "[Process]: running: ", child.running(),
+ " EC: ", child.exit_code(),
+ " Native: ", child.native_exit_code());
- onExit(child.exit_code(), dev.isReady());
- });
+ onExit(child.exit_code(), dev.isReady());
+ });
return true;
}
- void stop()
+ template <class OnTerminateCb>
+ void stop(OnTerminateCb&& onTerminate)
{
- boost::asio::spawn(ioc, [this, self = shared_from_this()](
+ boost::asio::spawn(ioc, [this, self = shared_from_this(),
+ onTerminate = std::move(onTerminate)](
boost::asio::yield_context yield) {
// The Good
dev.disconnect();
@@ -455,6 +457,7 @@ class Process : public std::enable_shared_from_this<Process>
LogMsg(Logger::Info, "[Process] Terminate if process doesnt "
"want to exit nicely");
child.terminate();
+ onTerminate();
}
});
}