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 | |
| 21 | #include <fstream> |
| 22 | #include <memory> |
| 23 | #include <sdbusplus/bus.hpp> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 27 | namespace ipmi_flash |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 28 | { |
| 29 | |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 30 | static constexpr auto systemdService = "org.freedesktop.systemd1"; |
| 31 | static constexpr auto systemdRoot = "/org/freedesktop/systemd1"; |
| 32 | static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
| 33 | |
| 34 | bool 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 | |
| 78 | void 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 | |
| 102 | ActionStatus SystemdNoFile::status() |
| 103 | { |
| 104 | return currentStatus; |
| 105 | } |
| 106 | |
| 107 | const std::string& SystemdNoFile::getMode() const |
| 108 | { |
| 109 | return mode; |
| 110 | } |
| 111 | |
| 112 | void 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 | |
| 148 | std::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 Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 156 | std::unique_ptr<TriggerableActionInterface> |
Patrick Venture | cf066ac | 2019-08-06 09:03:47 -0700 | [diff] [blame] | 157 | SystemdWithStatusFile::CreateSystemdWithStatusFile( |
| 158 | sdbusplus::bus::bus&& bus, const std::string& path, |
| 159 | const std::string& service, const std::string& mode) |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 160 | { |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 161 | return std::make_unique<SystemdWithStatusFile>(std::move(bus), path, |
| 162 | service, mode); |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 165 | ActionStatus SystemdWithStatusFile::status() |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 166 | { |
William A. Kennington III | b22ebbf | 2019-11-19 17:03:48 -0800 | [diff] [blame] | 167 | // 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 Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 171 | |
| 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 Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 184 | result = ActionStatus::running; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 185 | } |
| 186 | else if (status == "success") |
| 187 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 188 | result = ActionStatus::success; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 189 | } |
| 190 | else if (status == "failed") |
| 191 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 192 | result = ActionStatus::failed; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | return result; |
| 197 | } |
| 198 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 199 | } // namespace ipmi_flash |