xref: /openbmc/dbus-sensors/src/SystemdInterface.cpp (revision 7a28d492c8cf02a7c90cd34100c44e2378f3ca64)
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 
10 PHOSPHOR_LOG2_USING;
11 
12 namespace systemd
13 {
14 
startUnit(sdbusplus::async::context & ctx,std::string sysdUnit)15 auto SystemdInterface::startUnit(sdbusplus::async::context& ctx,
16                                  std::string sysdUnit)
17     -> sdbusplus::async::task<>
18 {
19     if (sysdUnit.empty())
20     {
21         error("sysdUnit is empty");
22         co_return;
23     }
24 
25     try
26     {
27         constexpr auto systemd =
28             sdbusplus::async::proxy()
29                 .service("org.freedesktop.systemd1")
30                 .path("/org/freedesktop/systemd1")
31                 .interface("org.freedesktop.systemd1.Manager");
32 
33         sdbusplus::message::object_path jobObjectPath =
34             co_await systemd.call<sdbusplus::message::object_path>(
35                 ctx, "StartUnit", sysdUnit, "replace");
36 
37         debug("Started {UNIT} with {JOBID}", "UNIT", sysdUnit, "JOBID",
38               jobObjectPath.str);
39     }
40     catch (const std::exception& e)
41     {
42         warning("Failed to start {UNIT}: {ERROR}", "UNIT", sysdUnit, "ERROR",
43                 e);
44         co_return;
45     }
46 
47     co_return;
48 }
49 
50 } // namespace systemd
51