1 /*
2  * Copyright © 2018 Intel 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  */
17 #pragma once
18 
19 #define ALLOW_DEPRECATED_API 1
20 // make it possible to ONLY include api.hpp during the transition
21 #ifdef ALLOW_DEPRECATED_API
22 #include <ipmid/api.h>
23 #endif
24 
25 #include <boost/asio/io_context.hpp>
26 #include <ipmid/api-types.hpp>
27 #include <ipmid/filter.hpp>
28 #include <ipmid/handler.hpp>
29 #include <ipmid/message/types.hpp>
30 #include <sdbusplus/asio/connection.hpp>
31 #include <sdbusplus/asio/object_server.hpp>
32 
33 // any client can interact with the main asio context
34 std::shared_ptr<boost::asio::io_context> getIoContext();
35 
36 // any client can interact with the main sdbus
37 std::shared_ptr<sdbusplus::asio::connection> getSdBus();
38 
39 /**
40  * @brief post some work to the async exection queue
41  *
42  * The IPMI daemon runs an async exection queue; this allows any function to
43  * pass in work to be executed in that context
44  *
45  * @tparam WorkFn - a function of type void(void)
46  * @param work - the callback function to be executed
47  */
48 template <typename WorkFn>
post_work(WorkFn work)49 static inline void post_work(WorkFn work)
50 {
51     boost::asio::post(*getIoContext(), std::forward<WorkFn>(work));
52 }
53 
54 enum class SignalResponse : int
55 {
56     breakExecution,
57     continueExecution,
58 };
59 
60 /**
61  * @brief add a signal handler
62  *
63  * This registers a handler to be called asynchronously via the execution
64  * queue when the specified signal is received.
65  *
66  * Priority allows a signal handler to specify what order in the handler
67  * chain it gets called. Lower priority numbers will cause the handler to
68  * be executed later in the chain, while the highest priority numbers will cause
69  * the handler to be executed first.
70  *
71  * In order to facilitate a chain of handlers, each handler in the chain will be
72  * able to return breakExecution or continueExecution. Returning breakExecution
73  * will break the chain and no further handlers will execute for that signal.
74  * Returning continueExecution will allow lower-priority handlers to execute.
75  *
76  * By default, the main asio execution loop will register a low priority
77  * (prioOpenBmcBase) handler for SIGINT and SIGTERM to cause the process to stop
78  * on either of those signals. To prevent one of those signals from causing the
79  * process to stop, simply register a higher priority handler that returns
80  * breakExecution.
81  *
82  * @param int - priority of handler
83  * @param int - signal number to wait for
84  * @param handler - the callback function to be executed
85  */
86 void registerSignalHandler(int priority, int signalNumber,
87                            const std::function<SignalResponse(int)>& handler);
88