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