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