xref: /openbmc/phosphor-ipmi-flash/bmc/general_systemd.cpp (revision cf066ace76d6f99b73a7c55793d6f475c058321a)
1*cf066aceSPatrick Venture /*
2*cf066aceSPatrick Venture  * Copyright 2019 Google Inc.
3*cf066aceSPatrick Venture  *
4*cf066aceSPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5*cf066aceSPatrick Venture  * you may not use this file except in compliance with the License.
6*cf066aceSPatrick Venture  * You may obtain a copy of the License at
7*cf066aceSPatrick Venture  *
8*cf066aceSPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9*cf066aceSPatrick Venture  *
10*cf066aceSPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11*cf066aceSPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12*cf066aceSPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*cf066aceSPatrick Venture  * See the License for the specific language governing permissions and
14*cf066aceSPatrick Venture  * limitations under the License.
15*cf066aceSPatrick Venture  */
16*cf066aceSPatrick Venture 
17*cf066aceSPatrick Venture #include "general_systemd.hpp"
18*cf066aceSPatrick Venture 
19*cf066aceSPatrick Venture #include "status.hpp"
20*cf066aceSPatrick Venture 
21*cf066aceSPatrick Venture #include <fstream>
22*cf066aceSPatrick Venture #include <memory>
23*cf066aceSPatrick Venture #include <sdbusplus/bus.hpp>
24*cf066aceSPatrick Venture #include <string>
25*cf066aceSPatrick Venture #include <vector>
26*cf066aceSPatrick Venture 
27*cf066aceSPatrick Venture namespace ipmi_flash
28*cf066aceSPatrick Venture {
29*cf066aceSPatrick Venture 
30*cf066aceSPatrick Venture std::unique_ptr<TriggerableActionInterface>
31*cf066aceSPatrick Venture     SystemdWithStatusFile::CreateSystemdWithStatusFile(
32*cf066aceSPatrick Venture         sdbusplus::bus::bus&& bus, const std::string& path,
33*cf066aceSPatrick Venture         const std::string& service, const std::string& mode)
34*cf066aceSPatrick Venture {
35*cf066aceSPatrick Venture     return std::make_unique<SystemdWithStatusFile>(std::move(bus), path,
36*cf066aceSPatrick Venture                                                    service, mode);
37*cf066aceSPatrick Venture }
38*cf066aceSPatrick Venture 
39*cf066aceSPatrick Venture bool SystemdWithStatusFile::trigger()
40*cf066aceSPatrick Venture {
41*cf066aceSPatrick Venture     static constexpr auto systemdService = "org.freedesktop.systemd1";
42*cf066aceSPatrick Venture     static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
43*cf066aceSPatrick Venture     static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
44*cf066aceSPatrick Venture 
45*cf066aceSPatrick Venture     auto method = bus.new_method_call(systemdService, systemdRoot,
46*cf066aceSPatrick Venture                                       systemdInterface, "StartUnit");
47*cf066aceSPatrick Venture     method.append(triggerService);
48*cf066aceSPatrick Venture     method.append(mode);
49*cf066aceSPatrick Venture 
50*cf066aceSPatrick Venture     try
51*cf066aceSPatrick Venture     {
52*cf066aceSPatrick Venture         bus.call_noreply(method);
53*cf066aceSPatrick Venture     }
54*cf066aceSPatrick Venture     catch (const sdbusplus::exception::SdBusError& ex)
55*cf066aceSPatrick Venture     {
56*cf066aceSPatrick Venture         /* TODO: Once logging supports unit-tests, add a log message to test
57*cf066aceSPatrick Venture          * this failure.
58*cf066aceSPatrick Venture          */
59*cf066aceSPatrick Venture         return false;
60*cf066aceSPatrick Venture     }
61*cf066aceSPatrick Venture 
62*cf066aceSPatrick Venture     return true;
63*cf066aceSPatrick Venture }
64*cf066aceSPatrick Venture 
65*cf066aceSPatrick Venture void SystemdWithStatusFile::abort()
66*cf066aceSPatrick Venture {
67*cf066aceSPatrick Venture     /* TODO: Implement this. */
68*cf066aceSPatrick Venture }
69*cf066aceSPatrick Venture 
70*cf066aceSPatrick Venture ActionStatus SystemdWithStatusFile::status()
71*cf066aceSPatrick Venture {
72*cf066aceSPatrick Venture     ActionStatus result = ActionStatus::unknown;
73*cf066aceSPatrick Venture 
74*cf066aceSPatrick Venture     std::ifstream ifs;
75*cf066aceSPatrick Venture     ifs.open(checkPath);
76*cf066aceSPatrick Venture     if (ifs.good())
77*cf066aceSPatrick Venture     {
78*cf066aceSPatrick Venture         /*
79*cf066aceSPatrick Venture          * Check for the contents of the file, accepting:
80*cf066aceSPatrick Venture          * running, success, or failed.
81*cf066aceSPatrick Venture          */
82*cf066aceSPatrick Venture         std::string status;
83*cf066aceSPatrick Venture         ifs >> status;
84*cf066aceSPatrick Venture         if (status == "running")
85*cf066aceSPatrick Venture         {
86*cf066aceSPatrick Venture             result = ActionStatus::running;
87*cf066aceSPatrick Venture         }
88*cf066aceSPatrick Venture         else if (status == "success")
89*cf066aceSPatrick Venture         {
90*cf066aceSPatrick Venture             result = ActionStatus::success;
91*cf066aceSPatrick Venture         }
92*cf066aceSPatrick Venture         else if (status == "failed")
93*cf066aceSPatrick Venture         {
94*cf066aceSPatrick Venture             result = ActionStatus::failed;
95*cf066aceSPatrick Venture         }
96*cf066aceSPatrick Venture     }
97*cf066aceSPatrick Venture 
98*cf066aceSPatrick Venture     return result;
99*cf066aceSPatrick Venture }
100*cf066aceSPatrick Venture 
101*cf066aceSPatrick Venture const std::string SystemdWithStatusFile::getMode() const
102*cf066aceSPatrick Venture {
103*cf066aceSPatrick Venture     return mode;
104*cf066aceSPatrick Venture }
105*cf066aceSPatrick Venture 
106*cf066aceSPatrick Venture } // namespace ipmi_flash
107