Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 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 | */ |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 16 | #include "config.h" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 17 | |
| 18 | #include "softoff.hpp" |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 19 | #include "timer.hpp" |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 20 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 21 | #include <systemd/sd-event.h> |
| 22 | |
| 23 | #include <phosphor-logging/elog-errors.hpp> |
| 24 | #include <phosphor-logging/elog.hpp> |
| 25 | #include <xyz/openbmc_project/State/Host/error.hpp> |
| 26 | |
Andrew Geissler | 83527ef | 2017-05-18 16:54:29 -0500 | [diff] [blame] | 27 | // Return -1 on any errors to ensure we follow the calling targets OnFailure= |
| 28 | // path |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 29 | int main(int argc, char** argv) |
| 30 | { |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame] | 31 | using namespace phosphor::logging; |
| 32 | |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 33 | // systemd event handler |
| 34 | sd_event* events = nullptr; |
| 35 | |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 36 | // Get a handle to system dbus. |
| 37 | auto bus = sdbusplus::bus::new_default(); |
| 38 | |
| 39 | // Add systemd object manager. |
| 40 | sdbusplus::server::manager::manager(bus, SOFTOFF_OBJPATH); |
| 41 | |
Andrew Geissler | 83527ef | 2017-05-18 16:54:29 -0500 | [diff] [blame] | 42 | // sd_event object |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 43 | auto r = sd_event_default(&events); |
| 44 | if (r < 0) |
| 45 | { |
| 46 | log<level::ERR>("Failure to create sd_event handler", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 47 | entry("ERRNO=0x%X", -r)); |
Andrew Geissler | 83527ef | 2017-05-18 16:54:29 -0500 | [diff] [blame] | 48 | return -1; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Attach the bus to sd_event to service user requests |
| 52 | bus.attach_event(events, SD_EVENT_PRIORITY_NORMAL); |
| 53 | |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame] | 54 | // Claim the bus. Delaying it until sending SMS_ATN may result |
| 55 | // in a race condition between this available and IPMI trying to send |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 56 | // message as a response to ack from host. |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 57 | bus.request_name(SOFTOFF_BUSNAME); |
| 58 | |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 59 | // Create the SoftPowerOff object. |
| 60 | phosphor::ipmi::SoftPowerOff powerObj(bus, events, SOFTOFF_OBJPATH); |
| 61 | |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame] | 62 | // Wait for client requests until this application has processed |
| 63 | // at least one successful SoftPowerOff or we timed out |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 64 | while (!powerObj.isCompleted() && !powerObj.isTimerExpired()) |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 65 | { |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 66 | // -1 denotes wait for ever |
| 67 | r = sd_event_run(events, (uint64_t)-1); |
| 68 | if (r < 0) |
| 69 | { |
| 70 | log<level::ERR>("Failure in processing request", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 71 | entry("ERRNO=0x%X", -r)); |
Andrew Geissler | 83527ef | 2017-05-18 16:54:29 -0500 | [diff] [blame] | 72 | return -1; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 73 | } |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 74 | } |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 75 | |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 76 | // Log an error if we timed out after getting Ack for SMS_ATN and before |
| 77 | // getting the Host Shutdown response |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 78 | if (powerObj.isTimerExpired() && |
| 79 | (powerObj.responseReceived() == |
| 80 | phosphor::ipmi::Base::SoftPowerOff::HostResponse::SoftOffReceived)) |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 81 | { |
Marri Devender Rao | 5cc0593 | 2017-04-15 06:15:19 -0500 | [diff] [blame] | 82 | using error = |
| 83 | sdbusplus::xyz::openbmc_project::State::Host::Error::SoftOffTimeout; |
| 84 | using errorMetadata = xyz::openbmc_project::State::Host::SoftOffTimeout; |
| 85 | report<error>(prev_entry<errorMetadata::TIMEOUT_IN_MSEC>()); |
Andrew Geissler | 83527ef | 2017-05-18 16:54:29 -0500 | [diff] [blame] | 86 | return -1; |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 87 | } |
| 88 | |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 89 | // Cleanup the event handler |
| 90 | events = sd_event_unref(events); |
| 91 | |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 92 | return 0; |
| 93 | } |