Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 1 | /* |
| 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 Venture | cf066ac | 2019-08-06 09:03:47 -0700 | [diff] [blame] | 17 | #include "general_systemd.hpp" |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 18 | |
| 19 | #include "status.hpp" |
| 20 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 21 | #include <sdbusplus/bus.hpp> |
| 22 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 23 | #include <fstream> |
| 24 | #include <memory> |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 28 | namespace ipmi_flash |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 29 | { |
| 30 | |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 31 | static constexpr auto systemdService = "org.freedesktop.systemd1"; |
| 32 | static constexpr auto systemdRoot = "/org/freedesktop/systemd1"; |
| 33 | static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
William A. Kennington III | 01593f9 | 2020-04-11 23:21:11 -0700 | [diff] [blame] | 34 | static constexpr auto jobInterface = "org.freedesktop.systemd1.Job"; |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 35 | |
| 36 | bool 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 | { |
Patrick Williams | 40fbb0c | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 47 | jobMonitor.emplace(bus, |
| 48 | "type='signal'," |
| 49 | "sender='org.freedesktop.systemd1'," |
| 50 | "path='/org/freedesktop/systemd1'," |
| 51 | "interface='org.freedesktop.systemd1.Manager'," |
| 52 | "member='JobRemoved',", |
| 53 | [&](sdbusplus::message_t& m) { this->match(m); }); |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 54 | |
| 55 | auto method = bus.new_method_call(systemdService, systemdRoot, |
| 56 | systemdInterface, "StartUnit"); |
| 57 | method.append(triggerService); |
| 58 | method.append(mode); |
| 59 | |
| 60 | sdbusplus::message::object_path obj_path; |
| 61 | bus.call(method).read(obj_path); |
| 62 | job = std::move(obj_path); |
| 63 | std::fprintf(stderr, "Triggered %s mode %s: %s\n", |
| 64 | triggerService.c_str(), mode.c_str(), job->c_str()); |
| 65 | currentStatus = ActionStatus::running; |
| 66 | return true; |
| 67 | } |
| 68 | catch (const std::exception& e) |
| 69 | { |
| 70 | job = std::nullopt; |
| 71 | jobMonitor = std::nullopt; |
| 72 | currentStatus = ActionStatus::failed; |
| 73 | std::fprintf(stderr, "Failed to trigger %s mode %s: %s\n", |
| 74 | triggerService.c_str(), mode.c_str(), e.what()); |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void SystemdNoFile::abort() |
| 80 | { |
| 81 | if (!job) |
| 82 | { |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | // Cancel the job |
| 87 | auto cancel_req = bus.new_method_call(systemdService, job->c_str(), |
William A. Kennington III | 01593f9 | 2020-04-11 23:21:11 -0700 | [diff] [blame] | 88 | jobInterface, "Cancel"); |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 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 | } |
Patrick Williams | 40fbb0c | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 95 | catch (const sdbusplus::exception_t& ex) |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 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 | |
| 102 | ActionStatus SystemdNoFile::status() |
| 103 | { |
| 104 | return currentStatus; |
| 105 | } |
| 106 | |
| 107 | const std::string& SystemdNoFile::getMode() const |
| 108 | { |
| 109 | return mode; |
| 110 | } |
| 111 | |
Patrick Williams | 40fbb0c | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 112 | void SystemdNoFile::match(sdbusplus::message_t& m) |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 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 | } |
Patrick Williams | 40fbb0c | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 128 | catch (const sdbusplus::exception_t& e) |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 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; |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 144 | currentStatus = |
| 145 | result == "done" ? ActionStatus::success : ActionStatus::failed; |
William A. Kennington III | 4175b4c | 2020-12-23 22:45:18 -0800 | [diff] [blame] | 146 | |
| 147 | if (cb) |
| 148 | { |
| 149 | cb(*this); |
| 150 | } |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Patrick Williams | 40fbb0c | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 153 | std::unique_ptr<TriggerableActionInterface> SystemdNoFile::CreateSystemdNoFile( |
| 154 | sdbusplus::bus_t&& bus, const std::string& service, const std::string& mode) |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 155 | { |
| 156 | return std::make_unique<SystemdNoFile>(std::move(bus), service, mode); |
| 157 | } |
| 158 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 159 | std::unique_ptr<TriggerableActionInterface> |
Patrick Venture | cf066ac | 2019-08-06 09:03:47 -0700 | [diff] [blame] | 160 | SystemdWithStatusFile::CreateSystemdWithStatusFile( |
Patrick Williams | 40fbb0c | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 161 | sdbusplus::bus_t&& bus, const std::string& path, |
Patrick Venture | cf066ac | 2019-08-06 09:03:47 -0700 | [diff] [blame] | 162 | const std::string& service, const std::string& mode) |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 163 | { |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 164 | return std::make_unique<SystemdWithStatusFile>(std::move(bus), path, |
| 165 | service, mode); |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 166 | } |
| 167 | |
William A. Kennington III | 93f3c55 | 2020-04-07 18:23:53 -0700 | [diff] [blame] | 168 | bool SystemdWithStatusFile::trigger() |
| 169 | { |
| 170 | if (SystemdNoFile::status() != ActionStatus::running) |
| 171 | { |
| 172 | try |
| 173 | { |
| 174 | std::ofstream ofs; |
| 175 | ofs.open(checkPath); |
| 176 | ofs << "unknown"; |
| 177 | } |
| 178 | catch (const std::exception& e) |
| 179 | { |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | return SystemdNoFile::trigger(); |
| 184 | } |
| 185 | |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 186 | ActionStatus SystemdWithStatusFile::status() |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 187 | { |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 188 | // Assume a status based on job execution if there is no file |
| 189 | ActionStatus result = SystemdNoFile::status() == ActionStatus::running |
| 190 | ? ActionStatus::running |
| 191 | : ActionStatus::failed; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 192 | |
| 193 | std::ifstream ifs; |
| 194 | ifs.open(checkPath); |
| 195 | if (ifs.good()) |
| 196 | { |
| 197 | /* |
| 198 | * Check for the contents of the file, accepting: |
| 199 | * running, success, or failed. |
| 200 | */ |
| 201 | std::string status; |
| 202 | ifs >> status; |
| 203 | if (status == "running") |
| 204 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 205 | result = ActionStatus::running; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 206 | } |
| 207 | else if (status == "success") |
| 208 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 209 | result = ActionStatus::success; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 210 | } |
| 211 | else if (status == "failed") |
| 212 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 213 | result = ActionStatus::failed; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 214 | } |
William A. Kennington III | 93f3c55 | 2020-04-07 18:23:53 -0700 | [diff] [blame] | 215 | else |
| 216 | { |
| 217 | result = ActionStatus::unknown; |
| 218 | } |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return result; |
| 222 | } |
| 223 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 224 | } // namespace ipmi_flash |