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 | 26a1726 | 2019-05-20 11:03:35 -0700 | [diff] [blame] | 17 | #include "verify_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 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame^] | 30 | std::unique_ptr<TriggerableActionInterface> |
Patrick Venture | 26a1726 | 2019-05-20 11:03:35 -0700 | [diff] [blame] | 31 | SystemdVerification::CreateVerification(sdbusplus::bus::bus&& bus, |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 32 | const std::string& path, |
| 33 | const std::string& service) |
| 34 | { |
Patrick Venture | 26a1726 | 2019-05-20 11:03:35 -0700 | [diff] [blame] | 35 | return std::make_unique<SystemdVerification>(std::move(bus), path, service); |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame^] | 38 | bool SystemdVerification::trigger() |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 39 | { |
| 40 | static constexpr auto systemdService = "org.freedesktop.systemd1"; |
| 41 | static constexpr auto systemdRoot = "/org/freedesktop/systemd1"; |
| 42 | static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
| 43 | |
| 44 | auto method = bus.new_method_call(systemdService, systemdRoot, |
| 45 | systemdInterface, "StartUnit"); |
| 46 | method.append(triggerService); |
| 47 | method.append("replace"); |
| 48 | |
| 49 | try |
| 50 | { |
| 51 | bus.call_noreply(method); |
| 52 | } |
| 53 | catch (const sdbusplus::exception::SdBusError& ex) |
| 54 | { |
| 55 | /* TODO: Once logging supports unit-tests, add a log message to test |
| 56 | * this failure. |
| 57 | */ |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame^] | 64 | void SystemdVerification::abort() |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 65 | { |
| 66 | /* TODO: Implement this. */ |
| 67 | } |
| 68 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 69 | ActionStatus SystemdVerification::status() |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 70 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 71 | ActionStatus result = ActionStatus::unknown; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 72 | |
| 73 | std::ifstream ifs; |
| 74 | ifs.open(checkPath); |
| 75 | if (ifs.good()) |
| 76 | { |
| 77 | /* |
| 78 | * Check for the contents of the file, accepting: |
| 79 | * running, success, or failed. |
| 80 | */ |
| 81 | std::string status; |
| 82 | ifs >> status; |
| 83 | if (status == "running") |
| 84 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 85 | result = ActionStatus::running; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 86 | } |
| 87 | else if (status == "success") |
| 88 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 89 | result = ActionStatus::success; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 90 | } |
| 91 | else if (status == "failed") |
| 92 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 93 | result = ActionStatus::failed; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | return result; |
| 98 | } |
| 99 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 100 | } // namespace ipmi_flash |