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 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 30 | std::unique_ptr<TriggerableActionInterface> |
Patrick Venture | cf066ac | 2019-08-06 09:03:47 -0700 | [diff] [blame] | 31 | SystemdWithStatusFile::CreateSystemdWithStatusFile( |
| 32 | sdbusplus::bus::bus&& bus, const std::string& path, |
| 33 | const std::string& service, const std::string& mode) |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 34 | { |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 35 | return std::make_unique<SystemdWithStatusFile>(std::move(bus), path, |
| 36 | service, mode); |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 39 | bool SystemdWithStatusFile::trigger() |
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); |
Patrick Venture | ee614ec | 2019-08-05 12:08:44 -0700 | [diff] [blame] | 48 | method.append(mode); |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 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 | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 65 | void SystemdWithStatusFile::abort() |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 66 | { |
| 67 | /* TODO: Implement this. */ |
| 68 | } |
| 69 | |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 70 | ActionStatus SystemdWithStatusFile::status() |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 71 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 72 | ActionStatus result = ActionStatus::unknown; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 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 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 86 | result = ActionStatus::running; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 87 | } |
| 88 | else if (status == "success") |
| 89 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 90 | result = ActionStatus::success; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 91 | } |
| 92 | else if (status == "failed") |
| 93 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 94 | result = ActionStatus::failed; |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | return result; |
| 99 | } |
| 100 | |
Patrick Venture | 29af1e3 | 2019-08-05 13:42:28 -0700 | [diff] [blame] | 101 | const std::string SystemdWithStatusFile::getMode() const |
Patrick Venture | ee614ec | 2019-08-05 12:08:44 -0700 | [diff] [blame] | 102 | { |
| 103 | return mode; |
| 104 | } |
| 105 | |
Patrick Venture | e0216d2 | 2019-08-21 10:17:39 -0700 | [diff] [blame^] | 106 | std::unique_ptr<TriggerableActionInterface> |
| 107 | SystemdNoFile::CreateSystemdNoFile(sdbusplus::bus::bus&& bus, |
| 108 | const std::string& service, |
| 109 | const std::string& mode) |
| 110 | { |
| 111 | return std::make_unique<SystemdNoFile>(std::move(bus), service, mode); |
| 112 | } |
| 113 | |
| 114 | bool SystemdNoFile::trigger() |
| 115 | { |
| 116 | static constexpr auto systemdService = "org.freedesktop.systemd1"; |
| 117 | static constexpr auto systemdRoot = "/org/freedesktop/systemd1"; |
| 118 | static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
| 119 | |
| 120 | auto method = bus.new_method_call(systemdService, systemdRoot, |
| 121 | systemdInterface, "StartUnit"); |
| 122 | method.append(triggerService); |
| 123 | method.append(mode); |
| 124 | |
| 125 | try |
| 126 | { |
| 127 | bus.call_noreply(method); |
| 128 | state = ActionStatus::running; |
| 129 | return true; |
| 130 | } |
| 131 | catch (const sdbusplus::exception::SdBusError& ex) |
| 132 | { |
| 133 | /* TODO: Once logging supports unit-tests, add a log message to test |
| 134 | * this failure. |
| 135 | */ |
| 136 | state = ActionStatus::failed; |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void SystemdNoFile::abort() |
| 142 | { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | ActionStatus SystemdNoFile::status() |
| 147 | { |
| 148 | return state; |
| 149 | } |
| 150 | |
| 151 | const std::string SystemdNoFile::getMode() const |
| 152 | { |
| 153 | return mode; |
| 154 | } |
| 155 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 156 | } // namespace ipmi_flash |