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  */
16 #include "config.h"
17 
18 #include "softoff.hpp"
19 
20 #include <phosphor-logging/elog-errors.hpp>
21 #include <phosphor-logging/elog.hpp>
22 #include <sdeventplus/event.hpp>
23 #include <sdeventplus/exception.hpp>
24 #include <xyz/openbmc_project/State/Host/error.hpp>
25 
26 // Return -1 on any errors to ensure we follow the calling targets OnFailure=
27 // path
28 int main(int argc, char** argv)
29 {
30     using namespace phosphor::logging;
31 
32     // Get a handle to system dbus.
33     auto bus = sdbusplus::bus::new_default();
34 
35     // Add systemd object manager.
36     sdbusplus::server::manager::manager(bus, SOFTOFF_OBJPATH);
37 
38     // Get default event loop
39     auto event = sdeventplus::Event::get_default();
40 
41     // Attach the bus to sd_event to service user requests
42     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
43 
44     // Claim the bus. Delaying it until sending SMS_ATN may result
45     // in a race condition between this available and IPMI trying to send
46     // message as a response to ack from host.
47     bus.request_name(SOFTOFF_BUSNAME);
48 
49     // Create the SoftPowerOff object.
50     phosphor::ipmi::SoftPowerOff powerObj(bus, event.get(), SOFTOFF_OBJPATH);
51 
52     // Wait for client requests until this application has processed
53     // at least one successful SoftPowerOff or we timed out
54     while (!powerObj.isCompleted() && !powerObj.isTimerExpired())
55     {
56         try
57         {
58             event.run(std::nullopt);
59         }
60         catch (const sdeventplus::SdEventError& e)
61         {
62             log<level::ERR>("Failure in processing request",
63                             entry("ERROR=%s", e.what()));
64             return 1;
65         }
66     }
67 
68     // Log an error if we timed out after getting Ack for SMS_ATN and before
69     // getting the Host Shutdown response
70     if (powerObj.isTimerExpired() &&
71         (powerObj.responseReceived() ==
72          phosphor::ipmi::Base::SoftPowerOff::HostResponse::SoftOffReceived))
73     {
74         using error =
75             sdbusplus::xyz::openbmc_project::State::Host::Error::SoftOffTimeout;
76         using errorMetadata = xyz::openbmc_project::State::Host::SoftOffTimeout;
77         report<error>(prev_entry<errorMetadata::TIMEOUT_IN_MSEC>());
78         return -1;
79     }
80 
81     return 0;
82 }
83