control.cc 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. #include <algorithm>
  2. #include <unordered_set>
  3. #include <climits>
  4. #include "dinit-env.h"
  5. #include "control.h"
  6. #include "service.h"
  7. #include "proc-service.h"
  8. // Server-side control protocol implementation. This implements the functionality that allows
  9. // clients (such as dinitctl) to query service state and issue commands to control services.
  10. // Control protocol versions:
  11. // 1 - dinit 0.16 and prior
  12. // 2 - dinit 0.17 (adds DINIT_CP_SETTRIGGER)
  13. namespace {
  14. constexpr auto OUT_EVENTS = dasynq::OUT_EVENTS;
  15. constexpr auto IN_EVENTS = dasynq::IN_EVENTS;
  16. // Control protocol minimum compatible version and current version:
  17. constexpr uint16_t min_compat_version = 1;
  18. constexpr uint16_t cp_version = 2;
  19. // check for value in a set
  20. template <typename T, int N, typename U>
  21. inline bool contains(const T (&v)[N], U i)
  22. {
  23. return std::find_if(std::begin(v), std::end(v),
  24. [=](T p){ return i == static_cast<U>(p); }) != std::end(v);
  25. }
  26. }
  27. bool control_conn_t::process_packet()
  28. {
  29. using std::string;
  30. // Note that where we call queue_packet, we must generally check the return value. If it
  31. // returns false it has either deleted the connection or marked it for deletion; we
  32. // shouldn't touch instance members after that point.
  33. int pktType = rbuf[0];
  34. if (pktType == DINIT_CP_QUERYVERSION) {
  35. // Responds with:
  36. // DINIT_RP_CVERSION, (2 byte) minimum compatible version, (2 byte) actual version
  37. char replyBuf[] = { DINIT_RP_CPVERSION, 0, 0, 0, 0 };
  38. memcpy(replyBuf + 1, &min_compat_version, 2);
  39. memcpy(replyBuf + 3, &cp_version, 2);
  40. if (! queue_packet(replyBuf, sizeof(replyBuf))) return false;
  41. rbuf.consume(1);
  42. return true;
  43. }
  44. if (pktType == DINIT_CP_FINDSERVICE || pktType == DINIT_CP_LOADSERVICE) {
  45. return process_find_load(pktType);
  46. }
  47. if (pktType == DINIT_CP_STARTSERVICE || pktType == DINIT_CP_STOPSERVICE
  48. || pktType == DINIT_CP_WAKESERVICE || pktType == DINIT_CP_RELEASESERVICE) {
  49. return process_start_stop(pktType);
  50. }
  51. if (pktType == DINIT_CP_UNPINSERVICE) {
  52. return process_unpin_service();
  53. }
  54. if (pktType == DINIT_CP_UNLOADSERVICE) {
  55. return process_unload_service();
  56. }
  57. if (pktType == DINIT_CP_RELOADSERVICE) {
  58. return process_reload_service();
  59. }
  60. if (pktType == DINIT_CP_SHUTDOWN) {
  61. // Shutdown/reboot
  62. if (rbuf.get_length() < 2) {
  63. chklen = 2;
  64. return true;
  65. }
  66. if (contains({shutdown_type_t::REMAIN, shutdown_type_t::HALT,
  67. shutdown_type_t::POWEROFF, shutdown_type_t::REBOOT}, rbuf[1])) {
  68. auto sd_type = static_cast<shutdown_type_t>(rbuf[1]);
  69. services->stop_all_services(sd_type);
  70. char ackBuf[] = { DINIT_RP_ACK };
  71. if (! queue_packet(ackBuf, 1)) return false;
  72. // Clear the packet from the buffer
  73. rbuf.consume(2);
  74. chklen = 0;
  75. return true;
  76. }
  77. // (otherwise fall through to below).
  78. }
  79. if (pktType == DINIT_CP_LISTSERVICES) {
  80. return list_services();
  81. }
  82. if (pktType == DINIT_CP_SERVICESTATUS) {
  83. return process_service_status();
  84. }
  85. if (pktType == DINIT_CP_ADD_DEP) {
  86. return add_service_dep();
  87. }
  88. if (pktType == DINIT_CP_REM_DEP) {
  89. return rm_service_dep();
  90. }
  91. if (pktType == DINIT_CP_QUERY_LOAD_MECH) {
  92. return query_load_mech();
  93. }
  94. if (pktType == DINIT_CP_ENABLESERVICE) {
  95. return add_service_dep(true);
  96. }
  97. if (pktType == DINIT_CP_QUERYSERVICENAME) {
  98. return process_query_name();
  99. }
  100. if (pktType == DINIT_CP_SETENV) {
  101. return process_setenv();
  102. }
  103. if (pktType == DINIT_CP_SETTRIGGER) {
  104. return process_set_trigger();
  105. }
  106. // Unrecognized: give error response
  107. char outbuf[] = { DINIT_RP_BADREQ };
  108. if (! queue_packet(outbuf, 1)) return false;
  109. bad_conn_close = true;
  110. iob.set_watches(OUT_EVENTS);
  111. return true;
  112. }
  113. bool control_conn_t::process_find_load(int pktType)
  114. {
  115. using std::string;
  116. constexpr int pkt_size = 4;
  117. if (rbuf.get_length() < pkt_size) {
  118. chklen = pkt_size;
  119. return true;
  120. }
  121. uint16_t svcSize;
  122. rbuf.extract((char *)&svcSize, 1, 2);
  123. if (svcSize <= 0 || svcSize > (1024 - 3)) {
  124. // Queue error response / mark connection bad
  125. char badreqRep[] = { DINIT_RP_BADREQ };
  126. if (! queue_packet(badreqRep, 1)) return false;
  127. bad_conn_close = true;
  128. iob.set_watches(OUT_EVENTS);
  129. return true;
  130. }
  131. chklen = svcSize + 3; // packet type + (2 byte) length + service name
  132. if (rbuf.get_length() < chklen) {
  133. // packet not complete yet; read more
  134. return true;
  135. }
  136. service_record * record = nullptr;
  137. string serviceName = rbuf.extract_string(3, svcSize);
  138. // Clear the packet from the buffer
  139. rbuf.consume(chklen);
  140. chklen = 0;
  141. char fail_code = DINIT_RP_NOSERVICE;
  142. if (pktType == DINIT_CP_LOADSERVICE) {
  143. // LOADSERVICE
  144. try {
  145. record = services->load_service(serviceName.c_str());
  146. }
  147. catch (service_description_exc &sdexc) {
  148. log_service_load_failure(sdexc);
  149. fail_code = DINIT_RP_SERVICE_DESC_ERR;
  150. }
  151. catch (service_not_found &snf) {
  152. log(loglevel_t::ERROR, "Could not load service ", snf.service_name, ": ",
  153. snf.exc_description);
  154. // fail_code = DINIT_RP_NOSERVICE; (already set)
  155. }
  156. catch (service_load_exc &slexc) {
  157. log(loglevel_t::ERROR, "Could not load service ", slexc.service_name, ": ",
  158. slexc.exc_description);
  159. fail_code = DINIT_RP_SERVICE_LOAD_ERR;
  160. }
  161. }
  162. else {
  163. // FINDSERVICE
  164. record = services->find_service(serviceName.c_str());
  165. }
  166. if (record == nullptr) {
  167. std::vector<char> rp_buf = { fail_code };
  168. if (! queue_packet(std::move(rp_buf))) return false;
  169. return true;
  170. }
  171. // Allocate a service handle
  172. handle_t handle = allocate_service_handle(record);
  173. std::vector<char> rp_buf;
  174. rp_buf.reserve(7);
  175. rp_buf.push_back(DINIT_RP_SERVICERECORD);
  176. rp_buf.push_back(static_cast<char>(record->get_state()));
  177. for (int i = 0; i < (int) sizeof(handle); i++) {
  178. rp_buf.push_back(*(((char *) &handle) + i));
  179. }
  180. rp_buf.push_back(static_cast<char>(record->get_target_state()));
  181. if (! queue_packet(std::move(rp_buf))) return false;
  182. return true;
  183. }
  184. bool control_conn_t::check_dependents(service_record *service, bool &had_dependents)
  185. {
  186. std::vector<char> reply_pkt;
  187. size_t num_depts = 0;
  188. for (service_dep *dep : service->get_dependents()) {
  189. if (dep->dep_type == dependency_type::REGULAR && dep->holding_acq) {
  190. num_depts++;
  191. // find or allocate a service handle
  192. handle_t dept_handle = allocate_service_handle(dep->get_from());
  193. if (reply_pkt.empty()) {
  194. // packet type, size
  195. reply_pkt.reserve(1 + sizeof(size_t) + sizeof(handle_t));
  196. reply_pkt.resize(1 + sizeof(size_t));
  197. reply_pkt[0] = DINIT_RP_DEPENDENTS;
  198. }
  199. auto old_size = reply_pkt.size();
  200. reply_pkt.resize(old_size + sizeof(handle_t));
  201. memcpy(reply_pkt.data() + old_size, &dept_handle, sizeof(dept_handle));
  202. }
  203. }
  204. if (num_depts != 0) {
  205. // There are affected dependents
  206. had_dependents = true;
  207. memcpy(reply_pkt.data() + 1, &num_depts, sizeof(num_depts));
  208. return queue_packet(std::move(reply_pkt));
  209. }
  210. had_dependents = false;
  211. return true;
  212. }
  213. bool control_conn_t::process_start_stop(int pktType)
  214. {
  215. using std::string;
  216. constexpr int pkt_size = 2 + sizeof(handle_t);
  217. if (rbuf.get_length() < pkt_size) {
  218. chklen = pkt_size;
  219. return true;
  220. }
  221. // 1 byte: packet type
  222. // 1 byte: flags eg. pin in requested state (0 = no pin, 1 = pin)
  223. // 4 bytes: service handle
  224. bool do_pin = ((rbuf[1] & 1) == 1);
  225. handle_t handle;
  226. rbuf.extract((char *) &handle, 2, sizeof(handle));
  227. service_record *service = find_service_for_key(handle);
  228. if (service == nullptr) {
  229. // Service handle is bad
  230. char badreqRep[] = { DINIT_RP_BADREQ };
  231. if (! queue_packet(badreqRep, 1)) return false;
  232. bad_conn_close = true;
  233. iob.set_watches(OUT_EVENTS);
  234. return true;
  235. }
  236. else {
  237. char ack_buf[1] = { DINIT_RP_ACK };
  238. switch (pktType) {
  239. case DINIT_CP_STARTSERVICE:
  240. // start service, mark as required
  241. if (services->is_shutting_down()) {
  242. ack_buf[0] = DINIT_RP_SHUTTINGDOWN;
  243. break;
  244. }
  245. if ((service->get_state() == service_state_t::STOPPED
  246. || service->get_state() == service_state_t::STOPPING)
  247. && service->is_stop_pinned()) {
  248. ack_buf[0] = DINIT_RP_PINNEDSTOPPED;
  249. break;
  250. }
  251. if (do_pin) service->pin_start();
  252. service->start();
  253. services->process_queues();
  254. if (service->get_state() == service_state_t::STARTED) ack_buf[0] = DINIT_RP_ALREADYSS;
  255. break;
  256. case DINIT_CP_STOPSERVICE:
  257. {
  258. // force service to stop
  259. bool do_restart = ((rbuf[1] & 4) == 4);
  260. bool gentle = ((rbuf[1] & 2) == 2);
  261. if (do_restart && services->is_shutting_down()) {
  262. ack_buf[0] = DINIT_RP_SHUTTINGDOWN;
  263. break;
  264. }
  265. if ((service->get_state() == service_state_t::STARTED
  266. || service->get_state() == service_state_t::STARTING)
  267. && service->is_start_pinned()) {
  268. ack_buf[0] = DINIT_RP_PINNEDSTARTED;
  269. break;
  270. }
  271. if (gentle) {
  272. // Check dependents; return appropriate response if any will be affected
  273. bool has_dependents;
  274. if (!check_dependents(service, has_dependents)) {
  275. return false;
  276. }
  277. if (has_dependents) {
  278. // Reply packet has already been sent
  279. goto clear_out;
  280. }
  281. }
  282. service_state_t wanted_state;
  283. if (do_restart) {
  284. if (! service->restart()) {
  285. ack_buf[0] = DINIT_RP_NAK;
  286. break;
  287. }
  288. wanted_state = service_state_t::STARTED;
  289. }
  290. else {
  291. if (do_pin) service->pin_stop();
  292. service->stop(true);
  293. service->forced_stop();
  294. wanted_state = service_state_t::STOPPED;
  295. }
  296. services->process_queues();
  297. if (service->get_state() == wanted_state && !do_restart) ack_buf[0] = DINIT_RP_ALREADYSS;
  298. break;
  299. }
  300. case DINIT_CP_WAKESERVICE:
  301. {
  302. // re-attach a service to its (started) dependents, causing it to start.
  303. if (services->is_shutting_down()) {
  304. ack_buf[0] = DINIT_RP_SHUTTINGDOWN;
  305. break;
  306. }
  307. if ((service->get_state() == service_state_t::STOPPED
  308. || service->get_state() == service_state_t::STOPPING)
  309. && service->is_stop_pinned()) {
  310. ack_buf[0] = DINIT_RP_PINNEDSTOPPED;
  311. break;
  312. }
  313. bool found_dpt = false;
  314. for (auto dpt : service->get_dependents()) {
  315. if (dpt->is_only_ordering()) continue;
  316. auto from = dpt->get_from();
  317. auto from_state = from->get_state();
  318. if (from_state == service_state_t::STARTED || from_state == service_state_t::STARTING) {
  319. found_dpt = true;
  320. if (!dpt->holding_acq) {
  321. dpt->get_from()->start_dep(*dpt);
  322. }
  323. }
  324. }
  325. if (!found_dpt) {
  326. ack_buf[0] = DINIT_RP_NAK;
  327. }
  328. if (do_pin) service->pin_start();
  329. services->process_queues();
  330. if (service->get_state() == service_state_t::STARTED) ack_buf[0] = DINIT_RP_ALREADYSS;
  331. break;
  332. }
  333. case DINIT_CP_RELEASESERVICE:
  334. // remove required mark, stop if not required by dependents
  335. if (do_pin) service->pin_stop();
  336. service->stop(false);
  337. services->process_queues();
  338. if (service->get_state() == service_state_t::STOPPED) ack_buf[0] = DINIT_RP_ALREADYSS;
  339. break;
  340. }
  341. if (! queue_packet(ack_buf, 1)) return false;
  342. }
  343. clear_out:
  344. // Clear the packet from the buffer
  345. rbuf.consume(pkt_size);
  346. chklen = 0;
  347. return true;
  348. }
  349. bool control_conn_t::process_unpin_service()
  350. {
  351. using std::string;
  352. constexpr int pkt_size = 1 + sizeof(handle_t);
  353. if (rbuf.get_length() < pkt_size) {
  354. chklen = pkt_size;
  355. return true;
  356. }
  357. // 1 byte: packet type
  358. // 4 bytes: service handle
  359. handle_t handle;
  360. rbuf.extract((char *) &handle, 1, sizeof(handle));
  361. service_record *service = find_service_for_key(handle);
  362. if (service == nullptr) {
  363. // Service handle is bad
  364. char badreqRep[] = { DINIT_RP_BADREQ };
  365. if (! queue_packet(badreqRep, 1)) return false;
  366. bad_conn_close = true;
  367. iob.set_watches(OUT_EVENTS);
  368. return true;
  369. }
  370. service->unpin();
  371. services->process_queues();
  372. char ack_buf[] = { (char) DINIT_RP_ACK };
  373. if (! queue_packet(ack_buf, 1)) return false;
  374. // Clear the packet from the buffer
  375. rbuf.consume(pkt_size);
  376. chklen = 0;
  377. return true;
  378. }
  379. bool control_conn_t::process_unload_service()
  380. {
  381. using std::string;
  382. constexpr int pkt_size = 1 + sizeof(handle_t);
  383. if (rbuf.get_length() < pkt_size) {
  384. chklen = pkt_size;
  385. return true;
  386. }
  387. // 1 byte: packet type
  388. // 4 bytes: service handle
  389. handle_t handle;
  390. rbuf.extract((char *) &handle, 1, sizeof(handle));
  391. service_record *service = find_service_for_key(handle);
  392. if (service == nullptr) {
  393. // Service handle is bad
  394. char badreq_rep[] = { DINIT_RP_BADREQ };
  395. if (! queue_packet(badreq_rep, 1)) return false;
  396. bad_conn_close = true;
  397. iob.set_watches(OUT_EVENTS);
  398. return true;
  399. }
  400. if (!service->has_lone_ref() || service->get_state() != service_state_t::STOPPED) {
  401. // Cannot unload: has other references
  402. char nak_rep[] = { DINIT_RP_NAK };
  403. if (! queue_packet(nak_rep, 1)) return false;
  404. }
  405. else {
  406. // unload
  407. service->prepare_for_unload();
  408. services->remove_service(service);
  409. delete service;
  410. // drop handle
  411. service_key_map.erase(service);
  412. key_service_map.erase(handle);
  413. // send ack
  414. char ack_buf[] = { (char) DINIT_RP_ACK };
  415. if (! queue_packet(ack_buf, 1)) return false;
  416. }
  417. // Clear the packet from the buffer
  418. rbuf.consume(pkt_size);
  419. chklen = 0;
  420. return true;
  421. }
  422. bool control_conn_t::process_reload_service()
  423. {
  424. using std::string;
  425. constexpr int pkt_size = 1 + sizeof(handle_t);
  426. if (rbuf.get_length() < pkt_size) {
  427. chklen = pkt_size;
  428. return true;
  429. }
  430. // 1 byte: packet type
  431. // 4 bytes: service handle
  432. handle_t handle;
  433. rbuf.extract((char *) &handle, 1, sizeof(handle));
  434. service_record *service = find_service_for_key(handle);
  435. if (service == nullptr) {
  436. // Service handle is bad
  437. char badreq_rep[] = { DINIT_RP_BADREQ };
  438. if (! queue_packet(badreq_rep, 1)) return false;
  439. bad_conn_close = true;
  440. iob.set_watches(OUT_EVENTS);
  441. return true;
  442. }
  443. if (! service->has_lone_ref(false)) {
  444. // Cannot unload: has other references
  445. char nak_rep[] = { DINIT_RP_NAK };
  446. if (! queue_packet(nak_rep, 1)) return false;
  447. }
  448. else {
  449. try {
  450. // reload
  451. auto *new_service = services->reload_service(service);
  452. if (new_service != service) {
  453. service->prepare_for_unload();
  454. services->replace_service(service, new_service);
  455. delete service;
  456. }
  457. else {
  458. service->remove_listener(this);
  459. }
  460. // drop handle
  461. key_service_map.erase(handle);
  462. service_key_map.erase(service);
  463. services->process_queues();
  464. // send ack
  465. char ack_buf[] = { (char) DINIT_RP_ACK };
  466. if (! queue_packet(ack_buf, 1)) return false;
  467. }
  468. catch (service_load_exc &slexc) {
  469. log(loglevel_t::ERROR, "Could not reload service ", slexc.service_name, ": ",
  470. slexc.exc_description);
  471. char nak_rep[] = { DINIT_RP_NAK };
  472. if (! queue_packet(nak_rep, 1)) return false;
  473. }
  474. }
  475. // Clear the packet from the buffer
  476. rbuf.consume(pkt_size);
  477. chklen = 0;
  478. return true;
  479. }
  480. constexpr static unsigned SIZEOF_INT_PIDT_UNION = ((sizeof(pid_t) > sizeof(int)) ? sizeof(pid_t) : sizeof(int));
  481. constexpr static unsigned STATUS_BUFFER_SIZE = 6 + SIZEOF_INT_PIDT_UNION;
  482. static void fill_status_buffer(char *buffer, service_record *service)
  483. {
  484. buffer[0] = static_cast<char>(service->get_state());
  485. buffer[1] = static_cast<char>(service->get_target_state());
  486. pid_t proc_pid = service->get_pid();
  487. char b0 = service->is_waiting_for_console() ? 1 : 0;
  488. b0 |= service->has_console() ? 2 : 0;
  489. b0 |= service->was_start_skipped() ? 4 : 0;
  490. b0 |= service->is_marked_active() ? 8 : 0;
  491. b0 |= (proc_pid != -1) ? 16 : 0;
  492. buffer[2] = b0;
  493. buffer[3] = static_cast<char>(service->get_stop_reason());
  494. buffer[4] = 0; // (if exec failed, these are replaced with stage)
  495. buffer[5] = 0;
  496. if (proc_pid != -1) {
  497. memcpy(buffer + 6, &proc_pid, sizeof(proc_pid));
  498. }
  499. else {
  500. // These values only make sense in STOPPING/STOPPED, but we'll fill them in regardless:
  501. if (buffer[3] == (char)stopped_reason_t::EXECFAILED) {
  502. base_process_service *bsp = (base_process_service *)service;
  503. run_proc_err exec_err = bsp->get_exec_err_info();
  504. uint16_t stage = (uint16_t)exec_err.stage;
  505. memcpy(buffer + 4, &stage, 2);
  506. memcpy(buffer + 6, &exec_err.st_errno, sizeof(int));
  507. }
  508. else {
  509. int exit_status = service->get_exit_status();
  510. memcpy(buffer + 6, &exit_status, sizeof(exit_status));
  511. }
  512. }
  513. }
  514. bool control_conn_t::list_services()
  515. {
  516. rbuf.consume(1); // clear request packet
  517. chklen = 0;
  518. try {
  519. auto slist = services->list_services();
  520. for (auto sptr : slist) {
  521. std::vector<char> pkt_buf;
  522. int hdrsize = 2 + STATUS_BUFFER_SIZE;
  523. const std::string &name = sptr->get_name();
  524. int nameLen = std::min((size_t)256, name.length());
  525. pkt_buf.resize(hdrsize + nameLen);
  526. pkt_buf[0] = DINIT_RP_SVCINFO;
  527. pkt_buf[1] = nameLen;
  528. fill_status_buffer(&pkt_buf[2], sptr);
  529. for (int i = 0; i < nameLen; i++) {
  530. pkt_buf[hdrsize+i] = name[i];
  531. }
  532. if (!queue_packet(std::move(pkt_buf))) return false;
  533. }
  534. char ack_buf[] = { (char) DINIT_RP_LISTDONE };
  535. if (! queue_packet(ack_buf, 1)) return false;
  536. return true;
  537. }
  538. catch (std::bad_alloc &exc)
  539. {
  540. do_oom_close();
  541. return true;
  542. }
  543. }
  544. bool control_conn_t::process_service_status()
  545. {
  546. constexpr int pkt_size = 1 + sizeof(handle_t);
  547. if (rbuf.get_length() < pkt_size) {
  548. chklen = pkt_size;
  549. return true;
  550. }
  551. handle_t handle;
  552. rbuf.extract(&handle, 1, sizeof(handle));
  553. rbuf.consume(pkt_size);
  554. chklen = 0;
  555. service_record *service = find_service_for_key(handle);
  556. if (service == nullptr || service->get_name().length() > std::numeric_limits<uint16_t>::max()) {
  557. char nak_rep[] = { DINIT_RP_NAK };
  558. return queue_packet(nak_rep, 1);
  559. }
  560. // Reply:
  561. // 1 byte packet type = DINIT_RP_SERVICESTATUS
  562. // 1 byte reserved ( = 0)
  563. // STATUS_BUFFER_SIZE bytes status
  564. std::vector<char> pkt_buf(2 + STATUS_BUFFER_SIZE);
  565. pkt_buf[0] = DINIT_RP_SERVICESTATUS;
  566. pkt_buf[1] = 0;
  567. fill_status_buffer(pkt_buf.data() + 2, service);
  568. return queue_packet(std::move(pkt_buf));
  569. }
  570. bool control_conn_t::add_service_dep(bool do_enable)
  571. {
  572. // 1 byte packet type
  573. // 1 byte dependency type
  574. // handle: "from"
  575. // handle: "to"
  576. constexpr int pkt_size = 2 + sizeof(handle_t) * 2;
  577. if (rbuf.get_length() < pkt_size) {
  578. chklen = pkt_size;
  579. return true;
  580. }
  581. handle_t from_handle;
  582. handle_t to_handle;
  583. rbuf.extract((char *) &from_handle, 2, sizeof(from_handle));
  584. rbuf.extract((char *) &to_handle, 2 + sizeof(from_handle), sizeof(to_handle));
  585. service_record *from_service = find_service_for_key(from_handle);
  586. service_record *to_service = find_service_for_key(to_handle);
  587. if (from_service == nullptr || to_service == nullptr || from_service == to_service) {
  588. // Service handle is bad
  589. char badreq_rep[] = { DINIT_RP_BADREQ };
  590. if (! queue_packet(badreq_rep, 1)) return false;
  591. bad_conn_close = true;
  592. iob.set_watches(OUT_EVENTS);
  593. return true;
  594. }
  595. // Check dependency type is valid:
  596. int dep_type_int = rbuf[1];
  597. if (! contains({dependency_type::MILESTONE, dependency_type::REGULAR,
  598. dependency_type::WAITS_FOR}, dep_type_int)) {
  599. char badreqRep[] = { DINIT_RP_BADREQ };
  600. if (! queue_packet(badreqRep, 1)) return false;
  601. bad_conn_close = true;
  602. iob.set_watches(OUT_EVENTS);
  603. }
  604. dependency_type dep_type = static_cast<dependency_type>(dep_type_int);
  605. // Check current service states are valid for given dep type
  606. if (dep_type == dependency_type::REGULAR) {
  607. if (from_service->get_state() != service_state_t::STOPPED &&
  608. to_service->get_state() != service_state_t::STARTED) {
  609. // Cannot create dependency now since it would be contradicted:
  610. char nak_rep[] = { DINIT_RP_NAK };
  611. if (! queue_packet(nak_rep, 1)) return false;
  612. rbuf.consume(pkt_size);
  613. chklen = 0;
  614. return true;
  615. }
  616. }
  617. // Check for creation of circular dependency chain
  618. std::unordered_set<service_record *> dep_marks;
  619. std::vector<service_record *> dep_queue;
  620. dep_queue.push_back(to_service);
  621. while (! dep_queue.empty()) {
  622. service_record * sr = dep_queue.back();
  623. dep_queue.pop_back();
  624. // iterate deps; if dep == from, abort; otherwise add to set/queue
  625. // (only add to queue if not already in set)
  626. for (auto &dep : sr->get_dependencies()) {
  627. service_record * dep_to = dep.get_to();
  628. if (dep_to == from_service) {
  629. // fail, circular dependency!
  630. char nak_rep[] = { DINIT_RP_NAK };
  631. if (! queue_packet(nak_rep, 1)) return false;
  632. rbuf.consume(pkt_size);
  633. chklen = 0;
  634. return true;
  635. }
  636. if (dep_marks.insert(dep_to).second) {
  637. dep_queue.push_back(dep_to);
  638. }
  639. }
  640. }
  641. dep_marks.clear();
  642. dep_queue.clear();
  643. bool dep_exists = false;
  644. service_dep * dep_record = nullptr;
  645. // Prevent creation of duplicate dependency:
  646. for (auto &dep : from_service->get_dependencies()) {
  647. service_record * dep_to = dep.get_to();
  648. if (dep_to == to_service && dep.dep_type == dep_type) {
  649. // Dependency already exists
  650. dep_exists = true;
  651. dep_record = &dep;
  652. break;
  653. }
  654. }
  655. if (! dep_exists) {
  656. // Create dependency:
  657. dep_record = &(from_service->add_dep(to_service, dep_type));
  658. services->process_queues();
  659. }
  660. if (do_enable && contains({service_state_t::STARTED, service_state_t::STARTING},
  661. from_service->get_state())) {
  662. // The dependency record is activated: mark it as holding acquisition of the dependency, and start
  663. // the dependency.
  664. if (!services->is_shutting_down()) {
  665. dep_record->get_from()->start_dep(*dep_record);
  666. services->process_queues();
  667. }
  668. }
  669. char ack_rep[] = { DINIT_RP_ACK };
  670. if (! queue_packet(ack_rep, 1)) return false;
  671. rbuf.consume(pkt_size);
  672. chklen = 0;
  673. return true;
  674. }
  675. bool control_conn_t::rm_service_dep()
  676. {
  677. // 1 byte packet type
  678. // 1 byte dependency type
  679. // handle: "from"
  680. // handle: "to"
  681. constexpr int pkt_size = 2 + sizeof(handle_t) * 2;
  682. if (rbuf.get_length() < pkt_size) {
  683. chklen = pkt_size;
  684. return true;
  685. }
  686. handle_t from_handle;
  687. handle_t to_handle;
  688. rbuf.extract((char *) &from_handle, 2, sizeof(from_handle));
  689. rbuf.extract((char *) &to_handle, 2 + sizeof(from_handle), sizeof(to_handle));
  690. service_record *from_service = find_service_for_key(from_handle);
  691. service_record *to_service = find_service_for_key(to_handle);
  692. if (from_service == nullptr || to_service == nullptr || from_service == to_service) {
  693. // Service handle is bad
  694. char badreq_rep[] = { DINIT_RP_BADREQ };
  695. if (! queue_packet(badreq_rep, 1)) return false;
  696. bad_conn_close = true;
  697. iob.set_watches(OUT_EVENTS);
  698. return true;
  699. }
  700. // Check dependency type is valid:
  701. int dep_type_int = rbuf[1];
  702. if (! contains({dependency_type::MILESTONE, dependency_type::REGULAR,
  703. dependency_type::WAITS_FOR}, dep_type_int)) {
  704. char badreqRep[] = { DINIT_RP_BADREQ };
  705. if (! queue_packet(badreqRep, 1)) return false;
  706. bad_conn_close = true;
  707. iob.set_watches(OUT_EVENTS);
  708. }
  709. dependency_type dep_type = static_cast<dependency_type>(dep_type_int);
  710. // Remove dependency:
  711. bool did_remove = from_service->rm_dep(to_service, dep_type);
  712. services->process_queues();
  713. char ack_rep[] = { did_remove ? (char)DINIT_RP_ACK : (char)DINIT_RP_NAK };
  714. if (! queue_packet(ack_rep, 1)) return false;
  715. rbuf.consume(pkt_size);
  716. chklen = 0;
  717. return true;
  718. }
  719. bool control_conn_t::process_query_name()
  720. {
  721. // 1 byte packet type
  722. // 1 byte reserved
  723. // handle: service
  724. constexpr int pkt_size = 2 + sizeof(handle_t);
  725. if (rbuf.get_length() < pkt_size) {
  726. chklen = pkt_size;
  727. return true;
  728. }
  729. handle_t handle;
  730. rbuf.extract(&handle, 2, sizeof(handle));
  731. rbuf.consume(pkt_size);
  732. chklen = 0;
  733. service_record *service = find_service_for_key(handle);
  734. if (service == nullptr || service->get_name().length() > std::numeric_limits<uint16_t>::max()) {
  735. char nak_rep[] = { DINIT_RP_NAK };
  736. return queue_packet(nak_rep, 1);
  737. }
  738. // Reply:
  739. // 1 byte packet type = DINIT_RP_SERVICENAME
  740. // 1 byte reserved
  741. // uint16_t length
  742. // N bytes name
  743. std::vector<char> reply;
  744. const std::string &name = service->get_name();
  745. uint16_t name_length = name.length();
  746. reply.resize(2 + sizeof(uint16_t) + name_length);
  747. reply[0] = DINIT_RP_SERVICENAME;
  748. memcpy(reply.data() + 2, &name_length, sizeof(name_length));
  749. memcpy(reply.data() + 2 + sizeof(uint16_t), name.c_str(), name_length);
  750. return queue_packet(std::move(reply));
  751. }
  752. bool control_conn_t::process_setenv()
  753. {
  754. using std::string;
  755. string envVar;
  756. typename string::size_type eq;
  757. constexpr int pkt_size = 4;
  758. char badreqRep[] = { DINIT_RP_BADREQ };
  759. char okRep[] = { DINIT_RP_ACK };
  760. if (rbuf.get_length() < pkt_size) {
  761. chklen = pkt_size;
  762. return true;
  763. }
  764. uint16_t envSize;
  765. rbuf.extract((char *)&envSize, 1, 2);
  766. if (envSize <= 0 || envSize > (1024 - 3)) {
  767. goto badreq;
  768. }
  769. chklen = envSize + 3; // packet type + (2 byte) length + envvar
  770. if (rbuf.get_length() < chklen) {
  771. // packet not complete yet; read more
  772. return true;
  773. }
  774. envVar = rbuf.extract_string(3, envSize);
  775. eq = envVar.find('=');
  776. if (!eq || eq == envVar.npos) {
  777. // Not found or at the beginning of the string
  778. goto badreq;
  779. }
  780. main_env.set_var(std::move(envVar));
  781. // Success response
  782. if (!queue_packet(okRep, 1)) return false;
  783. // Clear the packet from the buffer
  784. rbuf.consume(chklen);
  785. chklen = 0;
  786. return true;
  787. badreq:
  788. // Queue error response / mark connection bad
  789. if (! queue_packet(badreqRep, 1)) return false;
  790. bad_conn_close = true;
  791. iob.set_watches(OUT_EVENTS);
  792. return true;
  793. }
  794. bool control_conn_t::process_set_trigger()
  795. {
  796. // 1 byte packet type
  797. // handle: service
  798. // 1 byte trigger value
  799. constexpr int pkt_size = 2 + sizeof(handle_t);
  800. if (rbuf.get_length() < pkt_size) {
  801. chklen = pkt_size;
  802. return true;
  803. }
  804. handle_t handle;
  805. char trigger_val;
  806. rbuf.extract(&handle, 1, sizeof(handle));
  807. rbuf.extract(&trigger_val, 1 + sizeof(handle), sizeof(trigger_val));
  808. rbuf.consume(pkt_size);
  809. chklen = 0;
  810. service_record *service = find_service_for_key(handle);
  811. if (service == nullptr || service->get_type() != service_type_t::TRIGGERED) {
  812. char nak_rep[] = { DINIT_RP_NAK };
  813. return queue_packet(nak_rep, 1);
  814. }
  815. triggered_service *tservice = static_cast<triggered_service *>(service);
  816. tservice->set_trigger(trigger_val != 0);
  817. services->process_queues();
  818. char ack_rep[] = { DINIT_RP_ACK };
  819. return queue_packet(ack_rep, 1);
  820. }
  821. bool control_conn_t::query_load_mech()
  822. {
  823. rbuf.consume(1);
  824. chklen = 0;
  825. if (services->get_set_type_id() == SSET_TYPE_DIRLOAD) {
  826. dirload_service_set *dss = static_cast<dirload_service_set *>(services);
  827. std::vector<char> reppkt;
  828. reppkt.resize(2 + sizeof(uint32_t) * 2); // packet type, loader type, packet size, # dirs
  829. reppkt[0] = DINIT_RP_LOADER_MECH;
  830. reppkt[1] = SSET_TYPE_DIRLOAD;
  831. // Number of directories in load path:
  832. uint32_t sdirs = dss->get_service_dir_count();
  833. std::memcpy(reppkt.data() + 2 + sizeof(uint32_t), &sdirs, sizeof(sdirs));
  834. // Our current working directory, which above are relative to:
  835. // leave sizeof(uint32_t) for size, which we'll fill in afterwards:
  836. std::size_t curpos = reppkt.size() + sizeof(uint32_t);
  837. #ifdef PATH_MAX
  838. uint32_t try_path_size = PATH_MAX;
  839. #else
  840. uint32_t try_path_size = 2048;
  841. #endif
  842. char *wd;
  843. while (true) {
  844. std::size_t total_size = curpos + std::size_t(try_path_size);
  845. if (total_size < curpos) {
  846. // Overflow. In theory we could now limit to size_t max, but the size must already
  847. // be crazy long; let's abort.
  848. char ack_rep[] = { DINIT_RP_NAK };
  849. if (! queue_packet(ack_rep, 1)) return false;
  850. return true;
  851. }
  852. reppkt.resize(total_size);
  853. wd = getcwd(reppkt.data() + curpos, try_path_size);
  854. if (wd != nullptr) break;
  855. // Keep doubling the path size we try until it's big enough, or we get numeric overflow
  856. uint32_t new_try_path_size = try_path_size * uint32_t(2u);
  857. if (new_try_path_size < try_path_size) {
  858. // Overflow.
  859. char ack_rep[] = { DINIT_RP_NAK };
  860. return queue_packet(ack_rep, 1);
  861. }
  862. try_path_size = new_try_path_size;
  863. }
  864. uint32_t wd_len = std::strlen(reppkt.data() + curpos);
  865. reppkt.resize(curpos + std::size_t(wd_len));
  866. std::memcpy(reppkt.data() + curpos - sizeof(uint32_t), &wd_len, sizeof(wd_len));
  867. // Each directory in the load path:
  868. for (int i = 0; uint32_t(i) < sdirs; i++) {
  869. const char *sdir = dss->get_service_dir(i);
  870. uint32_t dlen = std::strlen(sdir);
  871. auto cursize = reppkt.size();
  872. reppkt.resize(cursize + sizeof(dlen) + dlen);
  873. std::memcpy(reppkt.data() + cursize, &dlen, sizeof(dlen));
  874. std::memcpy(reppkt.data() + cursize + sizeof(dlen), sdir, dlen);
  875. }
  876. // Total packet size:
  877. uint32_t fsize = reppkt.size();
  878. std::memcpy(reppkt.data() + 2, &fsize, sizeof(fsize));
  879. if (! queue_packet(std::move(reppkt))) return false;
  880. return true;
  881. }
  882. else {
  883. // If we don't know how to deal with the service set type, send a NAK reply:
  884. char ack_rep[] = { DINIT_RP_NAK };
  885. return queue_packet(ack_rep, 1);
  886. }
  887. }
  888. control_conn_t::handle_t control_conn_t::allocate_service_handle(service_record *record)
  889. {
  890. // Try to find a unique handle (integer) in a single pass. Since the map is ordered, we can search until
  891. // we find a gap in the handle values.
  892. handle_t candidate = 0;
  893. for (auto p : key_service_map) {
  894. if (p.first == candidate) ++candidate;
  895. else break;
  896. }
  897. bool is_unique = (service_key_map.find(record) == service_key_map.end());
  898. // The following operations perform allocation (can throw std::bad_alloc). If an exception occurs we
  899. // must undo any previous actions:
  900. if (is_unique) {
  901. record->add_listener(this);
  902. }
  903. try {
  904. key_service_map[candidate] = record;
  905. service_key_map.insert(std::make_pair(record, candidate));
  906. }
  907. catch (...) {
  908. if (is_unique) {
  909. record->remove_listener(this);
  910. }
  911. key_service_map.erase(candidate);
  912. }
  913. return candidate;
  914. }
  915. void control_conn_t::service_event(service_record *service, service_event_t event) noexcept
  916. {
  917. // For each service handle corresponding to the event, send an information packet.
  918. auto range = service_key_map.equal_range(service);
  919. auto &i = range.first;
  920. auto &end = range.second;
  921. try {
  922. while (i != end) {
  923. uint32_t key = i->second;
  924. std::vector<char> pkt;
  925. constexpr int pktsize = 3 + sizeof(key) + STATUS_BUFFER_SIZE;
  926. pkt.reserve(pktsize);
  927. pkt.push_back(DINIT_IP_SERVICEEVENT);
  928. pkt.push_back(pktsize);
  929. char *p = (char *)&key;
  930. for (unsigned j = 0; j < sizeof(key); j++) {
  931. pkt.push_back(*p++);
  932. }
  933. pkt.push_back(static_cast<char>(event));
  934. pkt.resize(pktsize);
  935. fill_status_buffer(pkt.data() + 3 + sizeof(key), service);
  936. queue_packet(std::move(pkt));
  937. ++i;
  938. }
  939. }
  940. catch (std::bad_alloc &exc) {
  941. do_oom_close();
  942. }
  943. }
  944. bool control_conn_t::queue_packet(const char *pkt, unsigned size) noexcept
  945. {
  946. int in_flag = bad_conn_close ? 0 : IN_EVENTS;
  947. bool was_empty = outbuf.empty();
  948. // If the queue is empty, we can try to write the packet out now rather than queueing it.
  949. // If the write is unsuccessful or partial, we queue the remainder.
  950. if (was_empty) {
  951. int wr = bp_sys::write(iob.get_watched_fd(), pkt, size);
  952. if (wr == -1) {
  953. if (errno == EPIPE) {
  954. return false;
  955. }
  956. if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
  957. log(loglevel_t::WARN, "Error writing to control connection: ", strerror(errno));
  958. return false;
  959. }
  960. // EAGAIN etc: fall through to below
  961. }
  962. else {
  963. if ((unsigned)wr == size) {
  964. // Ok, all written.
  965. iob.set_watches(in_flag);
  966. return true;
  967. }
  968. pkt += wr;
  969. size -= wr;
  970. }
  971. }
  972. // Create a vector out of the (remaining part of the) packet:
  973. try {
  974. outbuf.emplace_back(pkt, pkt + size);
  975. iob.set_watches(in_flag | OUT_EVENTS);
  976. return true;
  977. }
  978. catch (std::bad_alloc &baexc) {
  979. // Mark the connection bad, and stop reading further requests
  980. bad_conn_close = true;
  981. oom_close = true;
  982. if (was_empty) {
  983. // We can't send out-of-memory response as we already wrote as much as we
  984. // could above. Neither can we later send the response since we have currently
  985. // sent an incomplete packet. All we can do is close the connection.
  986. return false;
  987. }
  988. else {
  989. iob.set_watches(OUT_EVENTS);
  990. return true;
  991. }
  992. }
  993. }
  994. // This queue_packet method is frustratingly similar to the one above, but the subtle differences
  995. // make them extraordinary difficult to combine into a single method.
  996. bool control_conn_t::queue_packet(std::vector<char> &&pkt) noexcept
  997. {
  998. int in_flag = bad_conn_close ? 0 : IN_EVENTS;
  999. bool was_empty = outbuf.empty();
  1000. if (was_empty) {
  1001. outpkt_index = 0;
  1002. // We can try sending the packet immediately:
  1003. int wr = bp_sys::write(iob.get_watched_fd(), pkt.data(), pkt.size());
  1004. if (wr == -1) {
  1005. if (errno == EPIPE) {
  1006. return false;
  1007. }
  1008. if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
  1009. log(loglevel_t::WARN, "Error writing to control connection: ", strerror(errno));
  1010. return false;
  1011. }
  1012. // EAGAIN etc: fall through to below
  1013. }
  1014. else {
  1015. if ((unsigned)wr == pkt.size()) {
  1016. // Ok, all written.
  1017. iob.set_watches(in_flag);
  1018. return true;
  1019. }
  1020. outpkt_index = wr;
  1021. }
  1022. }
  1023. try {
  1024. outbuf.emplace_back(pkt);
  1025. iob.set_watches(in_flag | OUT_EVENTS);
  1026. return true;
  1027. }
  1028. catch (std::bad_alloc &baexc) {
  1029. // Mark the connection bad, and stop reading further requests
  1030. bad_conn_close = true;
  1031. oom_close = true;
  1032. if (was_empty) {
  1033. // We can't send out-of-memory response as we already wrote as much as we
  1034. // could above. Neither can we later send the response since we have currently
  1035. // sent an incomplete packet. All we can do is close the connection.
  1036. return false;
  1037. }
  1038. else {
  1039. iob.set_watches(OUT_EVENTS);
  1040. return true;
  1041. }
  1042. }
  1043. }
  1044. bool control_conn_t::data_ready() noexcept
  1045. {
  1046. int fd = iob.get_watched_fd();
  1047. int r = rbuf.fill(fd);
  1048. // Note file descriptor is non-blocking
  1049. if (r == -1) {
  1050. if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
  1051. log(loglevel_t::WARN, "Error writing to control connection: ", strerror(errno));
  1052. return true;
  1053. }
  1054. return false;
  1055. }
  1056. if (r == 0) {
  1057. return true;
  1058. }
  1059. // complete packet?
  1060. if (rbuf.get_length() >= chklen) {
  1061. try {
  1062. return !process_packet();
  1063. }
  1064. catch (std::bad_alloc &baexc) {
  1065. do_oom_close();
  1066. return false;
  1067. }
  1068. }
  1069. else if (rbuf.get_length() == rbuf.get_size()) {
  1070. // Too big packet
  1071. log(loglevel_t::WARN, "Received too-large control packet; dropping connection");
  1072. bad_conn_close = true;
  1073. iob.set_watches(OUT_EVENTS);
  1074. }
  1075. else {
  1076. int out_flags = (bad_conn_close || !outbuf.empty()) ? OUT_EVENTS : 0;
  1077. iob.set_watches(IN_EVENTS | out_flags);
  1078. }
  1079. return false;
  1080. }
  1081. bool control_conn_t::send_data() noexcept
  1082. {
  1083. if (outbuf.empty() && bad_conn_close) {
  1084. if (oom_close) {
  1085. // Send oom response
  1086. char oomBuf[] = { DINIT_RP_OOM };
  1087. bp_sys::write(iob.get_watched_fd(), oomBuf, 1);
  1088. }
  1089. return true;
  1090. }
  1091. vector<char> & pkt = outbuf.front();
  1092. char *data = pkt.data();
  1093. int written = bp_sys::write(iob.get_watched_fd(), data + outpkt_index, pkt.size() - outpkt_index);
  1094. if (written == -1) {
  1095. if (errno == EPIPE) {
  1096. // read end closed
  1097. return true;
  1098. }
  1099. else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
  1100. // spurious readiness notification?
  1101. }
  1102. else {
  1103. log(loglevel_t::ERROR, "Error writing to control connection: ", strerror(errno));
  1104. return true;
  1105. }
  1106. return false;
  1107. }
  1108. outpkt_index += written;
  1109. if (outpkt_index == pkt.size()) {
  1110. // We've finished this packet, move on to the next:
  1111. outbuf.pop_front();
  1112. outpkt_index = 0;
  1113. if (outbuf.empty() && ! oom_close) {
  1114. if (! bad_conn_close) {
  1115. iob.set_watches(IN_EVENTS);
  1116. }
  1117. else {
  1118. return true;
  1119. }
  1120. }
  1121. }
  1122. return false;
  1123. }
  1124. control_conn_t::~control_conn_t() noexcept
  1125. {
  1126. bp_sys::close(iob.get_watched_fd());
  1127. iob.deregister(loop);
  1128. // Clear service listeners
  1129. for (auto p : service_key_map) {
  1130. p.first->remove_listener(this);
  1131. }
  1132. active_control_conns--;
  1133. }