blob: 69b965bd5be1b2dbf25912bc0ae89f764f260e5c [file] [log] [blame]
Patrick Venture3ecb3502019-05-17 11:03:51 -07001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Patrick Venturecf066ac2019-08-06 09:03:47 -070017#include "general_systemd.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -070018
19#include "status.hpp"
20
Patrick Venture9b37b092020-05-28 20:58:57 -070021#include <sdbusplus/bus.hpp>
22
Patrick Venture3ecb3502019-05-17 11:03:51 -070023#include <fstream>
24#include <memory>
Patrick Venture3ecb3502019-05-17 11:03:51 -070025#include <string>
26#include <vector>
27
Patrick Venture1d5a31c2019-05-20 11:38:22 -070028namespace ipmi_flash
Patrick Venture3ecb3502019-05-17 11:03:51 -070029{
30
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -080031static constexpr auto systemdService = "org.freedesktop.systemd1";
32static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
33static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
William A. Kennington III01593f92020-04-11 23:21:11 -070034static constexpr auto jobInterface = "org.freedesktop.systemd1.Job";
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -080035
36bool SystemdNoFile::trigger()
37{
38 if (job)
39 {
40 std::fprintf(stderr, "Job alreading running %s: %s\n",
41 triggerService.c_str(), job->c_str());
42 return false;
43 }
44
45 try
46 {
47 jobMonitor.emplace(
48 bus,
49 "type='signal',"
50 "sender='org.freedesktop.systemd1',"
51 "path='/org/freedesktop/systemd1',"
52 "interface='org.freedesktop.systemd1.Manager',"
53 "member='JobRemoved',",
54 [&](sdbusplus::message::message& m) { this->match(m); });
55
56 auto method = bus.new_method_call(systemdService, systemdRoot,
57 systemdInterface, "StartUnit");
58 method.append(triggerService);
59 method.append(mode);
60
61 sdbusplus::message::object_path obj_path;
62 bus.call(method).read(obj_path);
63 job = std::move(obj_path);
64 std::fprintf(stderr, "Triggered %s mode %s: %s\n",
65 triggerService.c_str(), mode.c_str(), job->c_str());
66 currentStatus = ActionStatus::running;
67 return true;
68 }
69 catch (const std::exception& e)
70 {
71 job = std::nullopt;
72 jobMonitor = std::nullopt;
73 currentStatus = ActionStatus::failed;
74 std::fprintf(stderr, "Failed to trigger %s mode %s: %s\n",
75 triggerService.c_str(), mode.c_str(), e.what());
76 return false;
77 }
78}
79
80void SystemdNoFile::abort()
81{
82 if (!job)
83 {
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -080084 return;
85 }
86
87 // Cancel the job
88 auto cancel_req = bus.new_method_call(systemdService, job->c_str(),
William A. Kennington III01593f92020-04-11 23:21:11 -070089 jobInterface, "Cancel");
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -080090 try
91 {
92 bus.call_noreply(cancel_req);
93 std::fprintf(stderr, "Canceled %s: %s\n", triggerService.c_str(),
94 job->c_str());
95 }
Patrick Williams1c0fe132021-09-02 09:39:27 -050096 catch (const sdbusplus::exception::exception& ex)
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -080097 {
98 std::fprintf(stderr, "Failed to cancel job %s %s: %s\n",
99 triggerService.c_str(), job->c_str(), ex.what());
100 }
101}
102
103ActionStatus SystemdNoFile::status()
104{
105 return currentStatus;
106}
107
108const std::string& SystemdNoFile::getMode() const
109{
110 return mode;
111}
112
113void SystemdNoFile::match(sdbusplus::message::message& m)
114{
115 if (!job)
116 {
117 std::fprintf(stderr, "No running job %s\n", triggerService.c_str());
118 return;
119 }
120
121 uint32_t job_id;
122 sdbusplus::message::object_path job_path;
123 std::string unit;
124 std::string result;
125 try
126 {
127 m.read(job_id, job_path, unit, result);
128 }
Patrick Williams1c0fe132021-09-02 09:39:27 -0500129 catch (const sdbusplus::exception::exception& e)
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -0800130 {
131 std::fprintf(stderr, "Bad JobRemoved signal %s: %s\n",
132 triggerService.c_str(), e.what());
133 return;
134 }
135
136 if (*job != job_path.str)
137 {
138 return;
139 }
140
141 std::fprintf(stderr, "Job Finished %s %s: %s\n", triggerService.c_str(),
142 job->c_str(), result.c_str());
143 jobMonitor = std::nullopt;
144 job = std::nullopt;
145 currentStatus =
146 result == "done" ? ActionStatus::success : ActionStatus::failed;
William A. Kennington III4175b4c2020-12-23 22:45:18 -0800147
148 if (cb)
149 {
150 cb(*this);
151 }
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -0800152}
153
154std::unique_ptr<TriggerableActionInterface>
155 SystemdNoFile::CreateSystemdNoFile(sdbusplus::bus::bus&& bus,
156 const std::string& service,
157 const std::string& mode)
158{
159 return std::make_unique<SystemdNoFile>(std::move(bus), service, mode);
160}
161
Patrick Venture1d66fe62019-06-03 14:57:27 -0700162std::unique_ptr<TriggerableActionInterface>
Patrick Venturecf066ac2019-08-06 09:03:47 -0700163 SystemdWithStatusFile::CreateSystemdWithStatusFile(
164 sdbusplus::bus::bus&& bus, const std::string& path,
165 const std::string& service, const std::string& mode)
Patrick Venture3ecb3502019-05-17 11:03:51 -0700166{
Patrick Venture29af1e32019-08-05 13:42:28 -0700167 return std::make_unique<SystemdWithStatusFile>(std::move(bus), path,
168 service, mode);
Patrick Venture3ecb3502019-05-17 11:03:51 -0700169}
170
William A. Kennington III93f3c552020-04-07 18:23:53 -0700171bool SystemdWithStatusFile::trigger()
172{
173 if (SystemdNoFile::status() != ActionStatus::running)
174 {
175 try
176 {
177 std::ofstream ofs;
178 ofs.open(checkPath);
179 ofs << "unknown";
180 }
181 catch (const std::exception& e)
182 {
183 return false;
184 }
185 }
186 return SystemdNoFile::trigger();
187}
188
Patrick Venture29af1e32019-08-05 13:42:28 -0700189ActionStatus SystemdWithStatusFile::status()
Patrick Venture3ecb3502019-05-17 11:03:51 -0700190{
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -0800191 // Assume a status based on job execution if there is no file
192 ActionStatus result = SystemdNoFile::status() == ActionStatus::running
193 ? ActionStatus::running
194 : ActionStatus::failed;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700195
196 std::ifstream ifs;
197 ifs.open(checkPath);
198 if (ifs.good())
199 {
200 /*
201 * Check for the contents of the file, accepting:
202 * running, success, or failed.
203 */
204 std::string status;
205 ifs >> status;
206 if (status == "running")
207 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700208 result = ActionStatus::running;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700209 }
210 else if (status == "success")
211 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700212 result = ActionStatus::success;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700213 }
214 else if (status == "failed")
215 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700216 result = ActionStatus::failed;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700217 }
William A. Kennington III93f3c552020-04-07 18:23:53 -0700218 else
219 {
220 result = ActionStatus::unknown;
221 }
Patrick Venture3ecb3502019-05-17 11:03:51 -0700222 }
223
224 return result;
225}
226
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700227} // namespace ipmi_flash