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 | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 16 | #include <systemd/sd-event.h> |
Vishwanatha Subbanna | 7cc9d71 | 2017-01-24 18:48:40 +0530 | [diff] [blame] | 17 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 18 | #include "softoff.hpp" |
| 19 | #include "config.h" |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 20 | #include "timer.hpp" |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 21 | |
| 22 | int main(int argc, char** argv) |
| 23 | { |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame^] | 24 | using namespace phosphor::logging; |
| 25 | |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 26 | // systemd event handler |
| 27 | sd_event* events = nullptr; |
| 28 | |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 29 | // Get a handle to system dbus. |
| 30 | auto bus = sdbusplus::bus::new_default(); |
| 31 | |
| 32 | // Add systemd object manager. |
| 33 | sdbusplus::server::manager::manager(bus, SOFTOFF_OBJPATH); |
| 34 | |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame^] | 35 | // sd_event object. StateManager wants that this applicatin return '0' |
| 36 | // always. |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 37 | auto r = sd_event_default(&events); |
| 38 | if (r < 0) |
| 39 | { |
| 40 | log<level::ERR>("Failure to create sd_event handler", |
| 41 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame^] | 42 | return 0; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Attach the bus to sd_event to service user requests |
| 46 | bus.attach_event(events, SD_EVENT_PRIORITY_NORMAL); |
| 47 | |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 48 | // Create the SoftPowerOff object. |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 49 | phosphor::ipmi::SoftPowerOff powerObj(bus, events, SOFTOFF_OBJPATH); |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 50 | |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame^] | 51 | // Claim the bus. Delaying it until sending SMS_ATN may result |
| 52 | // in a race condition between this available and IPMI trying to send |
| 53 | // message as a reponse to ack from host. |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 54 | bus.request_name(SOFTOFF_BUSNAME); |
| 55 | |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame^] | 56 | // Wait for client requests until this application has processed |
| 57 | // at least one successful SoftPowerOff or we timed out |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 58 | while(!powerObj.isCompleted() && !powerObj.isTimerExpired()) |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 59 | { |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 60 | // -1 denotes wait for ever |
| 61 | r = sd_event_run(events, (uint64_t)-1); |
| 62 | if (r < 0) |
| 63 | { |
| 64 | log<level::ERR>("Failure in processing request", |
| 65 | entry("ERROR=%s", strerror(-r))); |
| 66 | break; |
| 67 | } |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 68 | } |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 69 | |
| 70 | // Cleanup the event handler |
| 71 | events = sd_event_unref(events); |
| 72 | |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 73 | return 0; |
| 74 | } |