summaryrefslogtreecommitdiff
path: root/src/state
diff options
context:
space:
mode:
authorPrzemyslaw Czarnowski <przemyslaw.hawrylewicz.czarnowski@intel.com>2022-03-15 00:20:54 +0300
committerGitHub <noreply@github.com>2022-03-15 00:20:54 +0300
commited2aceab6ee059a40d939ea21364bc18ec80d94b (patch)
tree654bbae1b29b9dcdc6e4ed0036dc5d70ad0544a5 /src/state
parent1fb7beae5e97aadf8471ae7b6e07f5c2e5f33c78 (diff)
downloadvirtual-media-ed2aceab6ee059a40d939ea21364bc18ec80d94b.tar.xz
Make mount/unmount dbus calls asynchronous
Change the default behavior of mount/umount dbus calls from blocking to unblocking ones. Once mount/unmount is triggered, appropriate action is running in the background moving handling of operation result to async event. At the end of processing dbus completion signal is sent to client with uint value of operation status (identical with errno code). Tested: Manual scheduling of mount and unmount operations with monitoring dbus communication of virtual-media service - matching api calls with completion signal. Signed-off-by: Przemyslaw Czarnowski <przemyslaw.hawrylewicz.czarnowski@intel.com>
Diffstat (limited to 'src/state')
-rw-r--r--src/state/activating_state.cpp3
-rw-r--r--src/state/active_state.hpp15
-rw-r--r--src/state/deactivating_state.hpp5
-rw-r--r--src/state/initial_state.hpp146
-rw-r--r--src/state/ready_state.hpp7
5 files changed, 49 insertions, 127 deletions
diff --git a/src/state/activating_state.cpp b/src/state/activating_state.cpp
index c22174f..7143545 100644
--- a/src/state/activating_state.cpp
+++ b/src/state/activating_state.cpp
@@ -54,7 +54,8 @@ std::unique_ptr<BasicState> ActivatingState::handleEvent([
[maybe_unused]] SubprocessStoppedEvent event)
{
LogMsg(Logger::Error, "Process ended prematurely");
- return std::make_unique<ReadyState>(machine);
+ return std::make_unique<ReadyState>(machine, std::errc::connection_refused,
+ "Process ended prematurely");
}
std::unique_ptr<BasicState> ActivatingState::activateProxyMode()
diff --git a/src/state/active_state.hpp b/src/state/active_state.hpp
index a204dc3..beb1da1 100644
--- a/src/state/active_state.hpp
+++ b/src/state/active_state.hpp
@@ -14,7 +14,10 @@ struct ActiveState : public BasicStateT<ActiveState>
std::unique_ptr<resource::Process> process,
std::unique_ptr<resource::Gadget> gadget) :
BasicStateT(machine),
- process(std::move(process)), gadget(std::move(gadget)){};
+ process(std::move(process)), gadget(std::move(gadget))
+ {
+ machine.notify();
+ };
virtual std::unique_ptr<BasicState> onEnter()
{
@@ -43,12 +46,9 @@ struct ActiveState : public BasicStateT<ActiveState>
Configuration::inactivityTimeout.count(),
"s) - Unmounting");
// unmount media & stop retriggering timer
- boost::asio::spawn(
- machine.getIoc(),
- [&machine = machine](
- [[maybe_unused]] boost::asio::yield_context yield) {
- machine.emitUnmountEvent();
- });
+ boost::asio::post(machine.getIoc(), [&machine = machine]() {
+ machine.emitUnmountEvent();
+ });
return;
}
else
@@ -80,6 +80,7 @@ struct ActiveState : public BasicStateT<ActiveState>
std::unique_ptr<BasicState> handleEvent([[maybe_unused]] UnmountEvent event)
{
+ machine.notificationStart();
return std::make_unique<DeactivatingState>(machine, std::move(process),
std::move(gadget));
}
diff --git a/src/state/deactivating_state.hpp b/src/state/deactivating_state.hpp
index 57f1072..472b6f1 100644
--- a/src/state/deactivating_state.hpp
+++ b/src/state/deactivating_state.hpp
@@ -64,14 +64,17 @@ struct DeactivatingState : public BasicStateT<DeactivatingState>
{
LogMsg(Logger::Info, machine.getName(),
" udev StateChange::removed");
+ return std::make_unique<ReadyState>(machine);
}
else
{
LogMsg(Logger::Error, machine.getName(), " udev StateChange::",
static_cast<std::underlying_type_t<StateChange>>(
udevStateChangeEvent->devState));
+ return std::make_unique<ReadyState>(
+ machine, std::errc::connection_refused,
+ "Not expected udev state");
}
- return std::make_unique<ReadyState>(machine);
}
return nullptr;
}
diff --git a/src/state/initial_state.hpp b/src/state/initial_state.hpp
index 324f456..f9255b0 100644
--- a/src/state/initial_state.hpp
+++ b/src/state/initial_state.hpp
@@ -1,7 +1,13 @@
#include "active_state.hpp"
#include "basic_state.hpp"
+#include "logger.hpp"
#include "ready_state.hpp"
+#include <sys/mount.h>
+
+#include <memory>
+#include <sdbusplus/asio/connection.hpp>
+#include <string>
#include <system_error>
struct InitialState : public BasicStateT<InitialState>
@@ -200,85 +206,18 @@ struct InitialState : public BasicStateT<InitialState>
auto iface = event.objServer->add_interface(path, name);
- const auto timerPeriod = std::chrono::milliseconds(100);
- const auto duration = std::chrono::seconds(
- machine.getConfig().timeout.value_or(
- Configuration::MountPoint::defaultTimeout) +
- 5);
- const auto waitCnt =
- std::chrono::duration_cast<std::chrono::milliseconds>(duration) /
- timerPeriod;
- LogMsg(Logger::Debug, "[App] waitCnt == ", waitCnt);
+ iface->register_signal<int32_t>("Completion");
+ machine.notificationInitialize(event.bus, path, name, "Completion");
// Common unmount
- iface->register_method(
- "Unmount", [&machine = machine, waitCnt,
- timerPeriod](boost::asio::yield_context yield) {
- LogMsg(Logger::Info, "[App]: Unmount called on ",
- machine.getName());
- machine.emitUnmountEvent();
-
- auto repeats = waitCnt;
- boost::asio::steady_timer timer(machine.getIoc());
- while (repeats > 0)
- {
- if (machine.getState().get_if<ReadyState>())
- {
- LogMsg(Logger::Debug, "[App] Unmount ok");
- return true;
- }
- boost::system::error_code ignored_ec;
- timer.expires_from_now(timerPeriod);
- timer.async_wait(yield[ignored_ec]);
- repeats--;
- }
- LogMsg(Logger::Error,
- "[App] timedout when waiting for ReadyState");
- throw sdbusplus::exception::SdBusError(EBUSY,
- "Resource is busy");
- return false;
- });
+ iface->register_method("Unmount", [&machine = machine]() {
+ LogMsg(Logger::Info, "[App]: Unmount called on ",
+ machine.getName());
- // Common mount
- const auto handleMount =
- [waitCnt, timerPeriod](
- boost::asio::yield_context yield,
- interfaces::MountPointStateMachine& machine,
- std::optional<interfaces::MountPointStateMachine::Target>
- target) {
- machine.emitMountEvent(std::move(target));
-
- auto repeats = waitCnt;
- boost::asio::steady_timer timer(machine.getIoc());
- while (repeats > 0)
- {
- if (auto s = machine.getState().get_if<ReadyState>())
- {
- if (s->error)
- {
- throw sdbusplus::exception::SdBusError(
- static_cast<int>(s->error->code),
- s->error->message.c_str());
- }
- LogMsg(Logger::Error, "[App] Mount failed");
- return false;
- }
- if (machine.getState().get_if<ActiveState>())
- {
- LogMsg(Logger::Info, "[App] Mount ok");
- return true;
- }
- boost::system::error_code ignored_ec;
- timer.expires_from_now(timerPeriod);
- timer.async_wait(yield[ignored_ec]);
- repeats--;
- }
- LogMsg(Logger::Error,
- "[App] timedout when waiting for ActiveState");
- throw sdbusplus::exception::SdBusError(EBUSY,
- "Resource is busy");
- return false;
- };
+ machine.emitUnmountEvent();
+
+ return true;
+ });
// Mount specialization
if (isLegacy)
@@ -287,9 +226,9 @@ struct InitialState : public BasicStateT<InitialState>
using optional_fd = std::variant<int, unix_fd>;
iface->register_method(
- "Mount", [&machine = machine, handleMount](
- boost::asio::yield_context yield,
- std::string imgUrl, bool rw, optional_fd fd) {
+ "Mount", [&machine = machine](boost::asio::yield_context yield,
+ std::string imgUrl, bool rw,
+ optional_fd fd) {
LogMsg(Logger::Info, "[App]: Mount called on ",
getObjectPath(machine), machine.getName());
@@ -333,48 +272,21 @@ struct InitialState : public BasicStateT<InitialState>
utils::secureCleanup(buf);
}
- try
- {
- auto ret =
- handleMount(yield, machine, std::move(target));
- if (machine.getTarget())
- {
- machine.getTarget()->credentials.reset();
- }
- LogMsg(Logger::Info, "[App]: mount completed ", ret);
- return ret;
- }
- catch (const std::exception& e)
- {
- LogMsg(Logger::Error, e.what());
- if (machine.getTarget())
- {
- machine.getTarget()->credentials.reset();
- }
- throw;
- return false;
- }
- catch (...)
- {
- if (machine.getTarget())
- {
- machine.getTarget()->credentials.reset();
- }
- throw;
- return false;
- }
+ machine.emitMountEvent(std::move(target));
+
+ return true;
});
}
- else
+ else // proxy
{
- iface->register_method(
- "Mount", [&machine = machine,
- handleMount](boost::asio::yield_context yield) {
- LogMsg(Logger::Info, "[App]: Mount called on ",
- getObjectPath(machine), machine.getName());
+ iface->register_method("Mount", [&machine = machine]() mutable {
+ LogMsg(Logger::Info, "[App]: Mount called on ",
+ getObjectPath(machine), machine.getName());
- return handleMount(yield, machine, std::nullopt);
- });
+ machine.emitMountEvent(std::nullopt);
+
+ return true;
+ });
}
iface->initialize();
diff --git a/src/state/ready_state.hpp b/src/state/ready_state.hpp
index bf1e160..e20e700 100644
--- a/src/state/ready_state.hpp
+++ b/src/state/ready_state.hpp
@@ -24,7 +24,10 @@ struct ReadyState : public BasicStateT<ReadyState>
};
ReadyState(interfaces::MountPointStateMachine& machine) :
- BasicStateT(machine){};
+ BasicStateT(machine)
+ {
+ machine.notify();
+ };
ReadyState(interfaces::MountPointStateMachine& machine, const std::errc& ec,
const std::string& message) :
@@ -33,6 +36,7 @@ struct ReadyState : public BasicStateT<ReadyState>
{
LogMsg(Logger::Error, machine.getName(),
" Errno = ", static_cast<int>(ec), " : ", message);
+ machine.notify(std::make_error_code(ec));
}
std::unique_ptr<BasicState> onEnter() override
@@ -47,6 +51,7 @@ struct ReadyState : public BasicStateT<ReadyState>
std::unique_ptr<BasicState> handleEvent(MountEvent event)
{
+ machine.notificationStart();
if (event.target)
{
machine.getTarget() = std::move(event.target);