1 #ifndef DINIT_CONTROL_H
2 #define DINIT_CONTROL_H
6 #include <unordered_map>
14 #include "dinit-log.h"
15 #include "control-cmds.h"
16 #include "service-listener.h"
19 // Control connection for dinit
21 using namespace dasynq;
22 using eventloop_t = event_loop<null_mutex>;
25 class control_conn_watcher;
27 // forward-declaration of callback:
28 static rearm control_conn_cb(eventloop_t *loop, control_conn_watcher *watcher, int revents);
30 // Pointer to the control connection that is listening for rollback completion
31 extern control_conn_t * rollback_handler_conn;
33 extern int active_control_conns;
36 // (1 byte) packet type
37 // (N bytes) additional data (service name, etc)
38 // for LOADSERVICE/FINDSERVICE:
39 // (2 bytes) service name length
40 // (M bytes) service name (without nul terminator)
42 // Information packet:
43 // (1 byte) packet type, >= 100
44 // (1 byte) packet length (including all fields)
45 // N bytes: packet data (N = (length - 2))
50 class control_conn_watcher : public eventloop_t::bidi_fd_watcher_impl<control_conn_watcher>
52 inline rearm receive_event(eventloop_t &loop, int fd, int flags) noexcept;
54 eventloop_t * event_loop;
57 control_conn_watcher(eventloop_t & event_loop_p) : event_loop(&event_loop_p)
62 rearm read_ready(eventloop_t &loop, int fd) noexcept
64 return receive_event(loop, fd, IN_EVENTS);
67 rearm write_ready(eventloop_t &loop, int fd) noexcept
69 return receive_event(loop, fd, OUT_EVENTS);
72 void set_watches(int flags)
74 eventloop_t::bidi_fd_watcher::set_watches(*event_loop, flags);
78 inline rearm control_conn_watcher::receive_event(eventloop_t &loop, int fd, int flags) noexcept
80 return control_conn_cb(&loop, this, flags);
84 class control_conn_t : private service_listener
86 friend rearm control_conn_cb(eventloop_t *loop, control_conn_watcher *watcher, int revents);
88 control_conn_watcher iob;
90 service_set *services;
92 bool bad_conn_close = false; // close when finished output?
93 bool oom_close = false; // send final 'out of memory' indicator
95 // The packet length before we need to re-check if the packet is complete.
96 // process_packet() will not be called until the packet reaches this size.
102 template <typename T> using list = std::list<T>;
103 template <typename T> using vector = std::vector<T>;
105 // A mapping between service records and their associated numerical identifier used
107 using handle_t = uint32_t;
108 std::unordered_multimap<service_record *, handle_t> serviceKeyMap;
109 std::unordered_map<handle_t, service_record *> keyServiceMap;
111 // Buffer for outgoing packets. Each outgoing back is represented as a vector<char>.
112 list<vector<char>> outbuf;
113 // Current index within the first outgoing packet (all previous bytes have been sent).
114 unsigned outpkt_index = 0;
116 // Queue a packet to be sent
117 // Returns: false if the packet could not be queued and a suitable error packet
118 // could not be sent/queued (the connection should be closed);
119 // true (with bad_conn_close == false) if the packet was successfully
121 // true (with bad_conn_close == true) if the packet was not successfully
122 // queued (but a suitable error packate has been queued).
123 // The in/out watch enabled state will also be set appropriately.
124 bool queue_packet(vector<char> &&v) noexcept;
125 bool queue_packet(const char *pkt, unsigned size) noexcept;
128 // Returns: true (with bad_conn_close == false) if successful
129 // true (with bad_conn_close == true) if an error packet was queued
130 // false if an error occurred but no error packet could be queued
131 // (connection should be closed).
133 // std::bad_alloc - if an out-of-memory condition prevents processing
134 bool process_packet();
136 // Process a STARTSERVICE/STOPSERVICE packet. May throw std::bad_alloc.
137 bool process_start_stop(int pktType);
139 // Process a FINDSERVICE/LOADSERVICE packet. May throw std::bad_alloc.
140 bool process_find_load(int pktType);
142 // Process an UNPINSERVICE packet. May throw std::bad_alloc.
143 bool process_unpin_service();
145 bool list_services();
147 // Notify that data is ready to be read from the socket. Returns true if the connection should
149 bool data_ready() noexcept;
151 bool send_data() noexcept;
153 // Allocate a new handle for a service; may throw std::bad_alloc
154 handle_t allocate_service_handle(service_record *record);
156 service_record *find_service_for_key(uint32_t key)
159 return keyServiceMap.at(key);
161 catch (std::out_of_range &exc) {
166 // Close connection due to out-of-memory condition.
169 bad_conn_close = true;
171 iob.set_watches(OUT_EVENTS);
174 // Process service event broadcast.
175 // Note that this can potentially be called during packet processing (upon issuing
176 // service start/stop orders etc).
177 void service_event(service_record * service, service_event_t event) noexcept final override
179 // For each service handle corresponding to the event, send an information packet.
180 auto range = serviceKeyMap.equal_range(service);
181 auto & i = range.first;
182 auto & end = range.second;
185 uint32_t key = i->second;
186 std::vector<char> pkt;
187 constexpr int pktsize = 3 + sizeof(key);
188 pkt.reserve(pktsize);
189 pkt.push_back(DINIT_IP_SERVICEEVENT);
190 pkt.push_back(pktsize);
191 char * p = (char *) &key;
192 for (int j = 0; j < (int)sizeof(key); j++) {
195 pkt.push_back(static_cast<char>(event));
196 queue_packet(std::move(pkt));
200 catch (std::bad_alloc &exc) {
206 control_conn_t(eventloop_t &loop, service_set * services_p, int fd)
207 : iob(loop), loop(loop), services(services_p), chklen(0)
209 iob.add_watch(loop, fd, IN_EVENTS);
210 active_control_conns++;
213 bool rollback_complete() noexcept;
215 virtual ~control_conn_t() noexcept;
219 static rearm control_conn_cb(eventloop_t * loop, control_conn_watcher * watcher, int revents)
221 char * cc_addr = (reinterpret_cast<char *>(watcher)) - offsetof(control_conn_t, iob);
222 control_conn_t *conn = reinterpret_cast<control_conn_t *>(cc_addr);
223 if (revents & IN_EVENTS) {
224 if (conn->data_ready()) {
226 return rearm::REMOVED;
229 if (revents & OUT_EVENTS) {
230 if (conn->send_data()) {
232 return rearm::REMOVED;