blob: bcdda0d044474dc543611f7fa61b796a4c247f42 [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
21#include <fstream>
22#include <memory>
23#include <sdbusplus/bus.hpp>
24#include <string>
25#include <vector>
26
Patrick Venture1d5a31c2019-05-20 11:38:22 -070027namespace ipmi_flash
Patrick Venture3ecb3502019-05-17 11:03:51 -070028{
29
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -080030static constexpr auto systemdService = "org.freedesktop.systemd1";
31static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
32static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
33
34bool SystemdNoFile::trigger()
35{
36 if (job)
37 {
38 std::fprintf(stderr, "Job alreading running %s: %s\n",
39 triggerService.c_str(), job->c_str());
40 return false;
41 }
42
43 try
44 {
45 jobMonitor.emplace(
46 bus,
47 "type='signal',"
48 "sender='org.freedesktop.systemd1',"
49 "path='/org/freedesktop/systemd1',"
50 "interface='org.freedesktop.systemd1.Manager',"
51 "member='JobRemoved',",
52 [&](sdbusplus::message::message& m) { this->match(m); });
53
54 auto method = bus.new_method_call(systemdService, systemdRoot,
55 systemdInterface, "StartUnit");
56 method.append(triggerService);
57 method.append(mode);
58
59 sdbusplus::message::object_path obj_path;
60 bus.call(method).read(obj_path);
61 job = std::move(obj_path);
62 std::fprintf(stderr, "Triggered %s mode %s: %s\n",
63 triggerService.c_str(), mode.c_str(), job->c_str());
64 currentStatus = ActionStatus::running;
65 return true;
66 }
67 catch (const std::exception& e)
68 {
69 job = std::nullopt;
70 jobMonitor = std::nullopt;
71 currentStatus = ActionStatus::failed;
72 std::fprintf(stderr, "Failed to trigger %s mode %s: %s\n",
73 triggerService.c_str(), mode.c_str(), e.what());
74 return false;
75 }
76}
77
78void SystemdNoFile::abort()
79{
80 if (!job)
81 {
82 std::fprintf(stderr, "No running job %s\n", triggerService.c_str());
83 return;
84 }
85
86 // Cancel the job
87 auto cancel_req = bus.new_method_call(systemdService, job->c_str(),
88 systemdInterface, "Cancel");
89 try
90 {
91 bus.call_noreply(cancel_req);
92 std::fprintf(stderr, "Canceled %s: %s\n", triggerService.c_str(),
93 job->c_str());
94 }
95 catch (const sdbusplus::exception::SdBusError& ex)
96 {
97 std::fprintf(stderr, "Failed to cancel job %s %s: %s\n",
98 triggerService.c_str(), job->c_str(), ex.what());
99 }
100}
101
102ActionStatus SystemdNoFile::status()
103{
104 return currentStatus;
105}
106
107const std::string& SystemdNoFile::getMode() const
108{
109 return mode;
110}
111
112void SystemdNoFile::match(sdbusplus::message::message& m)
113{
114 if (!job)
115 {
116 std::fprintf(stderr, "No running job %s\n", triggerService.c_str());
117 return;
118 }
119
120 uint32_t job_id;
121 sdbusplus::message::object_path job_path;
122 std::string unit;
123 std::string result;
124 try
125 {
126 m.read(job_id, job_path, unit, result);
127 }
128 catch (const sdbusplus::exception::SdBusError& e)
129 {
130 std::fprintf(stderr, "Bad JobRemoved signal %s: %s\n",
131 triggerService.c_str(), e.what());
132 return;
133 }
134
135 if (*job != job_path.str)
136 {
137 return;
138 }
139
140 std::fprintf(stderr, "Job Finished %s %s: %s\n", triggerService.c_str(),
141 job->c_str(), result.c_str());
142 jobMonitor = std::nullopt;
143 job = std::nullopt;
144 currentStatus =
145 result == "done" ? ActionStatus::success : ActionStatus::failed;
146}
147
148std::unique_ptr<TriggerableActionInterface>
149 SystemdNoFile::CreateSystemdNoFile(sdbusplus::bus::bus&& bus,
150 const std::string& service,
151 const std::string& mode)
152{
153 return std::make_unique<SystemdNoFile>(std::move(bus), service, mode);
154}
155
Patrick Venture1d66fe62019-06-03 14:57:27 -0700156std::unique_ptr<TriggerableActionInterface>
Patrick Venturecf066ac2019-08-06 09:03:47 -0700157 SystemdWithStatusFile::CreateSystemdWithStatusFile(
158 sdbusplus::bus::bus&& bus, const std::string& path,
159 const std::string& service, const std::string& mode)
Patrick Venture3ecb3502019-05-17 11:03:51 -0700160{
Patrick Venture29af1e32019-08-05 13:42:28 -0700161 return std::make_unique<SystemdWithStatusFile>(std::move(bus), path,
162 service, mode);
Patrick Venture3ecb3502019-05-17 11:03:51 -0700163}
164
Patrick Venture29af1e32019-08-05 13:42:28 -0700165ActionStatus SystemdWithStatusFile::status()
Patrick Venture3ecb3502019-05-17 11:03:51 -0700166{
William A. Kennington IIIb22ebbf2019-11-19 17:03:48 -0800167 // Assume a status based on job execution if there is no file
168 ActionStatus result = SystemdNoFile::status() == ActionStatus::running
169 ? ActionStatus::running
170 : ActionStatus::failed;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700171
172 std::ifstream ifs;
173 ifs.open(checkPath);
174 if (ifs.good())
175 {
176 /*
177 * Check for the contents of the file, accepting:
178 * running, success, or failed.
179 */
180 std::string status;
181 ifs >> status;
182 if (status == "running")
183 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700184 result = ActionStatus::running;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700185 }
186 else if (status == "success")
187 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700188 result = ActionStatus::success;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700189 }
190 else if (status == "failed")
191 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700192 result = ActionStatus::failed;
Patrick Venture3ecb3502019-05-17 11:03:51 -0700193 }
194 }
195
196 return result;
197}
198
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700199} // namespace ipmi_flash