1 #pragma once
2 
3 #include "status.hpp"
4 
5 #include <sdbusplus/bus.hpp>
6 #include <sdbusplus/bus/match.hpp>
7 
8 #include <memory>
9 #include <string>
10 
11 namespace ipmi_flash
12 {
13 
14 class SystemdNoFile : public TriggerableActionInterface
15 {
16   public:
17     static std::unique_ptr<TriggerableActionInterface>
18         CreateSystemdNoFile(sdbusplus::bus_t&& bus, const std::string& service,
19                             const std::string& mode);
20 
SystemdNoFile(sdbusplus::bus_t && bus,const std::string & service,const std::string & mode)21     SystemdNoFile(sdbusplus::bus_t&& bus, const std::string& service,
22                   const std::string& mode) :
23         bus(std::move(bus)), triggerService(service), mode(mode)
24     {}
25 
26     SystemdNoFile(const SystemdNoFile&) = delete;
27     SystemdNoFile& operator=(const SystemdNoFile&) = delete;
28     // sdbusplus match requires us to be pinned
29     SystemdNoFile(SystemdNoFile&&) = delete;
30     SystemdNoFile& operator=(SystemdNoFile&&) = delete;
31 
32     bool trigger() override;
33     void abort() override;
34     ActionStatus status() override;
35 
36     const std::string& getMode() const;
37 
38   private:
39     sdbusplus::bus_t bus;
40     const std::string triggerService;
41     const std::string mode;
42 
43     std::optional<sdbusplus::bus::match_t> jobMonitor;
44     std::optional<std::string> job;
45     ActionStatus currentStatus = ActionStatus::unknown;
46 
47     void match(sdbusplus::message_t& m);
48 };
49 
50 /**
51  * Representation of what is used for triggering an action with systemd and
52  * checking the result by reading a file.
53  */
54 class SystemdWithStatusFile : public SystemdNoFile
55 {
56   public:
57     /**
58      * Create a default SystemdWithStatusFile object that uses systemd to
59      * trigger the process.
60      *
61      * @param[in] bus - an sdbusplus handler for a bus to use.
62      * @param[in] path - the path to check for verification status.
63      * @param[in] service - the systemd service to start to trigger
64      * verification.
65      * @param[in] mode - the job-mode when starting the systemd Unit.
66      */
67     static std::unique_ptr<TriggerableActionInterface>
68         CreateSystemdWithStatusFile(
69             sdbusplus::bus_t&& bus, const std::string& path,
70             const std::string& service, const std::string& mode);
71 
SystemdWithStatusFile(sdbusplus::bus_t && bus,const std::string & path,const std::string & service,const std::string & mode)72     SystemdWithStatusFile(sdbusplus::bus_t&& bus, const std::string& path,
73                           const std::string& service, const std::string& mode) :
74         SystemdNoFile(std::move(bus), service, mode), checkPath(path)
75     {}
76 
77     bool trigger() override;
78     ActionStatus status() override;
79 
80   private:
81     const std::string checkPath;
82 };
83 
84 } // namespace ipmi_flash
85