Jagpal Singh Gill | 15dde86 | 2024-10-16 09:42:54 -0700 | [diff] [blame] | 1 | #include "SystemdInterface.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/lg2.hpp> |
| 4 | #include <sdbusplus/async.hpp> |
| 5 | #include <sdbusplus/message/native_types.hpp> |
| 6 | |
| 7 | #include <exception> |
| 8 | #include <string> |
| 9 | #include <variant> |
| 10 | |
| 11 | PHOSPHOR_LOG2_USING; |
| 12 | |
| 13 | namespace systemd |
| 14 | { |
| 15 | |
| 16 | auto SystemdInterface::startUnit(sdbusplus::async::context& ctx, |
| 17 | std::string sysdUnit) |
| 18 | -> sdbusplus::async::task<> |
| 19 | { |
| 20 | if (sysdUnit.empty()) |
| 21 | { |
| 22 | error("sysdUnit is empty"); |
| 23 | co_return; |
| 24 | } |
| 25 | |
| 26 | try |
| 27 | { |
| 28 | constexpr auto systemd = |
| 29 | sdbusplus::async::proxy() |
| 30 | .service("org.freedesktop.systemd1") |
| 31 | .path("/org/freedesktop/systemd1") |
| 32 | .interface("org.freedesktop.systemd1.Manager"); |
| 33 | |
| 34 | std::variant<sdbusplus::message::object_path> jobObjectPath = |
| 35 | co_await systemd |
| 36 | .call<std::variant<sdbusplus::message::object_path>>( |
| 37 | ctx, "StartUnit", sysdUnit, "replace"); |
| 38 | |
| 39 | debug("Started {UNIT} with {JOBID}", "UNIT", sysdUnit, "JOBID", |
| 40 | std::get<sdbusplus::message::object_path>(jobObjectPath)); |
| 41 | } |
| 42 | catch (const std::exception& e) |
| 43 | { |
| 44 | warning("Failed to start {UNIT}: {ERROR}", "UNIT", sysdUnit, "ERROR", |
| 45 | e); |
| 46 | co_return; |
| 47 | } |
| 48 | |
| 49 | co_return; |
| 50 | } |
| 51 | |
| 52 | } // namespace systemd |