sys_request_server.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. * @file sys_request_server.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * A simple a IPC interface for NCD to talk to other processes over a Unix socket.
  32. *
  33. * Synopsis:
  34. * sys.request_server(string socket_path, string request_handler_template, list args)
  35. *
  36. * Description:
  37. * Initializes a request server on the given socket path. Requests are served by
  38. * starting a template process for every request. Multiple such processes may
  39. * exist simultaneously. Termination of these processess may be initiated at
  40. * any time if the request server no longer needs the request in question served.
  41. * The payload of a request is a value, and can be accessed as _request.data
  42. * from within the handler process. Replies to the request can be sent using
  43. * _request->reply(data); replies are values too. Finally, _request->finish()
  44. * should be called to indicate that no further replies will be sent. Calling
  45. * finish() will immediately initiate termination of the handler process.
  46. * Requests can be sent to NCD using the badvpn-ncd-request program.
  47. *
  48. * Synopsis:
  49. * sys.request_server.request::reply(reply_data)
  50. *
  51. * Synopsis:
  52. * sys.request_server.request::finish()
  53. */
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <limits.h>
  57. #include <inttypes.h>
  58. #include <unistd.h>
  59. #include <errno.h>
  60. #include <misc/offset.h>
  61. #include <misc/debug.h>
  62. #include <misc/byteorder.h>
  63. #include <protocol/packetproto.h>
  64. #include <protocol/requestproto.h>
  65. #include <structure/LinkedList0.h>
  66. #include <system/BConnection.h>
  67. #include <flow/PacketProtoDecoder.h>
  68. #include <flow/PacketStreamSender.h>
  69. #include <flow/PacketPassFifoQueue.h>
  70. #include <ncd/NCDValueParser.h>
  71. #include <ncd/NCDValueGenerator.h>
  72. #include <ncd/NCDModule.h>
  73. #include <generated/blog_channel_ncd_sys_request_server.h>
  74. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  75. #define SEND_PAYLOAD_MTU 32768
  76. #define RECV_PAYLOAD_MTU 32768
  77. #define SEND_MTU (SEND_PAYLOAD_MTU + sizeof(struct requestproto_header))
  78. #define RECV_MTU (RECV_PAYLOAD_MTU + sizeof(struct requestproto_header))
  79. struct instance {
  80. NCDModuleInst *i;
  81. char *socket_path;
  82. char *request_handler_template;
  83. NCDValue *args;
  84. BListener listener;
  85. LinkedList0 connections_list;
  86. int dying;
  87. };
  88. #define CONNECTION_STATE_RUNNING 1
  89. #define CONNECTION_STATE_TERMINATING 2
  90. struct connection {
  91. struct instance *inst;
  92. LinkedList0Node connections_list_node;
  93. BConnection con;
  94. PacketProtoDecoder recv_decoder;
  95. PacketPassInterface recv_if;
  96. PacketPassFifoQueue send_queue;
  97. PacketStreamSender send_pss;
  98. LinkedList0 requests_list;
  99. LinkedList0 replies_list;
  100. int state;
  101. };
  102. struct request {
  103. struct connection *con;
  104. uint32_t request_id;
  105. LinkedList0Node requests_list_node;
  106. NCDValue request_data;
  107. NCDModuleProcess process;
  108. BPending finish_job;
  109. int finished;
  110. };
  111. struct reply {
  112. struct connection *con;
  113. LinkedList0Node replies_list_node;
  114. PacketPassFifoQueueFlow send_qflow;
  115. PacketPassInterface *send_qflow_if;
  116. uint8_t *send_buf;
  117. };
  118. static void listener_handler (struct instance *o);
  119. static void connection_free (struct connection *c);
  120. static void connection_free_link (struct connection *c);
  121. static void connection_terminate (struct connection *c);
  122. static void connection_con_handler (struct connection *c, int event);
  123. static void connection_recv_decoder_handler_error (struct connection *c);
  124. static void connection_recv_if_handler_send (struct connection *c, uint8_t *data, int data_len);
  125. static int request_init (struct connection *c, uint32_t request_id, const uint8_t *data, int data_len);
  126. static void request_free (struct request *r);
  127. static void request_process_handler_event (struct request *r, int event);
  128. static int request_process_func_getspecialobj (struct request *r, const char *name, NCDObject *out_object);
  129. static int request_process_request_obj_func_getvar (struct request *r, const char *name, NCDValue *out_value);
  130. static void request_finish_job_handler (struct request *r);
  131. static int reply_init (struct connection *c, uint32_t request_id, uint32_t flags, NCDValue *reply_data);
  132. static void reply_free (struct reply *r);
  133. static void reply_send_qflow_if_handler_done (struct reply *r);
  134. static void instance_free (struct instance *o);
  135. static void listener_handler (struct instance *o)
  136. {
  137. ASSERT(!o->dying)
  138. BReactor *reactor = o->i->params->reactor;
  139. BPendingGroup *pg = BReactor_PendingGroup(reactor);
  140. struct connection *c = malloc(sizeof(*c));
  141. if (!c) {
  142. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  143. goto fail0;
  144. }
  145. c->inst = o;
  146. LinkedList0_Prepend(&o->connections_list, &c->connections_list_node);
  147. if (!BConnection_Init(&c->con, BCONNECTION_SOURCE_LISTENER(&o->listener, NULL), reactor, c, (BConnection_handler)connection_con_handler)) {
  148. ModuleLog(o->i, BLOG_ERROR, "BConnection_Init failed");
  149. goto fail1;
  150. }
  151. BConnection_SendAsync_Init(&c->con);
  152. BConnection_RecvAsync_Init(&c->con);
  153. StreamPassInterface *con_send_if = BConnection_SendAsync_GetIf(&c->con);
  154. StreamRecvInterface *con_recv_if = BConnection_RecvAsync_GetIf(&c->con);
  155. PacketPassInterface_Init(&c->recv_if, RECV_MTU, (PacketPassInterface_handler_send)connection_recv_if_handler_send, c, pg);
  156. if (!PacketProtoDecoder_Init(&c->recv_decoder, con_recv_if, &c->recv_if, pg, c, (PacketProtoDecoder_handler_error)connection_recv_decoder_handler_error)) {
  157. ModuleLog(o->i, BLOG_ERROR, "PacketProtoDecoder_Init failed");
  158. goto fail2;
  159. }
  160. PacketStreamSender_Init(&c->send_pss, con_send_if, PACKETPROTO_ENCLEN(SEND_MTU), pg);
  161. PacketPassFifoQueue_Init(&c->send_queue, PacketStreamSender_GetInput(&c->send_pss), pg);
  162. LinkedList0_Init(&c->requests_list);
  163. LinkedList0_Init(&c->replies_list);
  164. c->state = CONNECTION_STATE_RUNNING;
  165. ModuleLog(o->i, BLOG_INFO, "connection initialized");
  166. return;
  167. fail3:
  168. PacketStreamSender_Free(&c->send_pss);
  169. PacketProtoDecoder_Free(&c->recv_decoder);
  170. fail2:
  171. PacketPassInterface_Free(&c->recv_if);
  172. BConnection_RecvAsync_Free(&c->con);
  173. BConnection_SendAsync_Free(&c->con);
  174. BConnection_Free(&c->con);
  175. fail1:
  176. LinkedList0_Remove(&o->connections_list, &c->connections_list_node);
  177. free(c);
  178. fail0:
  179. return;
  180. }
  181. static void connection_free (struct connection *c)
  182. {
  183. struct instance *o = c->inst;
  184. ASSERT(c->state == CONNECTION_STATE_TERMINATING)
  185. ASSERT(LinkedList0_IsEmpty(&c->requests_list))
  186. ASSERT(LinkedList0_IsEmpty(&c->replies_list))
  187. LinkedList0_Remove(&o->connections_list, &c->connections_list_node);
  188. free(c);
  189. }
  190. static void connection_free_link (struct connection *c)
  191. {
  192. PacketPassFifoQueue_PrepareFree(&c->send_queue);
  193. LinkedList0Node *ln;
  194. while (ln = LinkedList0_GetFirst(&c->replies_list)) {
  195. struct reply *r = UPPER_OBJECT(ln, struct reply, replies_list_node);
  196. ASSERT(r->con == c)
  197. reply_free(r);
  198. }
  199. PacketPassFifoQueue_Free(&c->send_queue);
  200. PacketStreamSender_Free(&c->send_pss);
  201. PacketProtoDecoder_Free(&c->recv_decoder);
  202. PacketPassInterface_Free(&c->recv_if);
  203. BConnection_RecvAsync_Free(&c->con);
  204. BConnection_SendAsync_Free(&c->con);
  205. BConnection_Free(&c->con);
  206. }
  207. static void connection_terminate (struct connection *c)
  208. {
  209. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  210. for (LinkedList0Node *ln = LinkedList0_GetFirst(&c->requests_list); ln; ln = LinkedList0Node_Next(ln)) {
  211. struct request *r = UPPER_OBJECT(ln, struct request, requests_list_node);
  212. if (!r->finished) {
  213. NCDModuleProcess_Terminate(&r->process);
  214. }
  215. BPending_Unset(&r->finish_job);
  216. }
  217. connection_free_link(c);
  218. c->state = CONNECTION_STATE_TERMINATING;
  219. if (LinkedList0_IsEmpty(&c->requests_list)) {
  220. connection_free(c);
  221. return;
  222. }
  223. }
  224. static void connection_con_handler (struct connection *c, int event)
  225. {
  226. struct instance *o = c->inst;
  227. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  228. ModuleLog(o->i, BLOG_INFO, "connection closed");
  229. connection_terminate(c);
  230. }
  231. static void connection_recv_decoder_handler_error (struct connection *c)
  232. {
  233. struct instance *o = c->inst;
  234. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  235. ModuleLog(o->i, BLOG_INFO, "decoder error");
  236. connection_terminate(c);
  237. }
  238. static void connection_recv_if_handler_send (struct connection *c, uint8_t *data, int data_len)
  239. {
  240. struct instance *o = c->inst;
  241. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  242. ASSERT(data_len >= 0)
  243. ASSERT(data_len <= RECV_MTU)
  244. PacketPassInterface_Done(&c->recv_if);
  245. if (data_len < sizeof(struct requestproto_header)) {
  246. ModuleLog(o->i, BLOG_INFO, "missing requestproto header");
  247. return;
  248. }
  249. struct requestproto_header *header = (struct requestproto_header *)data;
  250. uint32_t request_id = ltoh32(header->request_id);
  251. uint32_t flags = ltoh32(header->flags);
  252. if (flags != REQUESTPROTO_REQUEST_FLAG) {
  253. ModuleLog(o->i, BLOG_INFO, "invalid requestproto flags");
  254. return;
  255. }
  256. request_init(c, request_id, data + sizeof(*header), data_len - sizeof(*header));
  257. }
  258. static int request_init (struct connection *c, uint32_t request_id, const uint8_t *data, int data_len)
  259. {
  260. struct instance *o = c->inst;
  261. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  262. ASSERT(data_len >= 0)
  263. ASSERT(data_len <= RECV_PAYLOAD_MTU)
  264. struct request *r = malloc(sizeof(*r));
  265. if (!r) {
  266. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  267. goto fail0;
  268. }
  269. r->con = c;
  270. r->request_id = request_id;
  271. LinkedList0_Prepend(&c->requests_list, &r->requests_list_node);
  272. if (!NCDValueParser_Parse(data, data_len, &r->request_data)) {
  273. ModuleLog(o->i, BLOG_ERROR, "NCDValueParser_Parse failed");
  274. goto fail1;
  275. }
  276. NCDValue args;
  277. if (!NCDValue_InitCopy(&args, o->args)) {
  278. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  279. goto fail2;
  280. }
  281. if (!NCDModuleProcess_Init(&r->process, o->i, o->request_handler_template, args, r, (NCDModuleProcess_handler_event)request_process_handler_event)) {
  282. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  283. NCDValue_Free(&args);
  284. goto fail2;
  285. }
  286. NCDModuleProcess_SetSpecialFuncs(&r->process, (NCDModuleProcess_func_getspecialobj)request_process_func_getspecialobj);
  287. BPending_Init(&r->finish_job, BReactor_PendingGroup(o->i->params->reactor), (BPending_handler)request_finish_job_handler, r);
  288. r->finished = 0;
  289. ModuleLog(o->i, BLOG_INFO, "request initialized");
  290. return 1;
  291. fail2:
  292. NCDValue_Free(&r->request_data);
  293. fail1:
  294. LinkedList0_Remove(&c->requests_list, &r->requests_list_node);
  295. free(r);
  296. fail0:
  297. return 0;
  298. }
  299. static void request_free (struct request *r)
  300. {
  301. struct connection *c = r->con;
  302. BPending_Free(&r->finish_job);
  303. NCDModuleProcess_Free(&r->process);
  304. NCDValue_Free(&r->request_data);
  305. LinkedList0_Remove(&c->requests_list, &r->requests_list_node);
  306. free(r);
  307. }
  308. static void request_process_handler_event (struct request *r, int event)
  309. {
  310. struct connection *c = r->con;
  311. struct instance *o = c->inst;
  312. switch (event) {
  313. case NCDMODULEPROCESS_EVENT_UP: {
  314. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  315. } break;
  316. case NCDMODULEPROCESS_EVENT_DOWN: {
  317. NCDModuleProcess_Continue(&r->process);
  318. } break;
  319. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  320. ASSERT(r->finished || c->state == CONNECTION_STATE_TERMINATING)
  321. request_free(r);
  322. if (c->state == CONNECTION_STATE_TERMINATING && LinkedList0_IsEmpty(&c->requests_list)) {
  323. connection_free(c);
  324. if (o->dying && LinkedList0_IsEmpty(&o->connections_list)) {
  325. instance_free(o);
  326. return;
  327. }
  328. }
  329. } break;
  330. default: ASSERT(0);
  331. }
  332. }
  333. static int request_process_func_getspecialobj (struct request *r, const char *name, NCDObject *out_object)
  334. {
  335. if (!strcmp(name, "_request")) {
  336. *out_object = NCDObject_Build("sys.request_server.request", r, (NCDObject_func_getvar)request_process_request_obj_func_getvar, NULL);
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. static int request_process_request_obj_func_getvar (struct request *r, const char *name, NCDValue *out_value)
  342. {
  343. struct instance *o = r->con->inst;
  344. if (!strcmp(name, "data")) {
  345. if (!NCDValue_InitCopy(out_value, &r->request_data)) {
  346. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  347. return 0;
  348. }
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. static void request_finish_job_handler (struct request *r)
  354. {
  355. struct connection *c = r->con;
  356. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  357. ASSERT(!r->finished)
  358. NCDModuleProcess_Terminate(&r->process);
  359. r->finished = 1;
  360. }
  361. static int reply_init (struct connection *c, uint32_t request_id, uint32_t flags, NCDValue *reply_data)
  362. {
  363. struct instance *o = c->inst;
  364. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  365. struct reply *r = malloc(sizeof(*r));
  366. if (!r) {
  367. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  368. goto fail0;
  369. }
  370. r->con = c;
  371. LinkedList0_Prepend(&c->replies_list, &r->replies_list_node);
  372. PacketPassFifoQueueFlow_Init(&r->send_qflow, &c->send_queue);
  373. r->send_qflow_if = PacketPassFifoQueueFlow_GetInput(&r->send_qflow);
  374. PacketPassInterface_Sender_Init(r->send_qflow_if, (PacketPassInterface_handler_done)reply_send_qflow_if_handler_done, r);
  375. struct reply_header {
  376. struct packetproto_header pp;
  377. struct requestproto_header rp;
  378. };
  379. ExpString str;
  380. if (!ExpString_Init(&str)) {
  381. ModuleLog(o->i, BLOG_ERROR, "ExpString_Init failed");
  382. goto fail1;
  383. }
  384. if (!ExpString_AppendZeros(&str, sizeof(struct reply_header))) {
  385. ModuleLog(o->i, BLOG_ERROR, "ExpString_AppendBinary failed");
  386. goto fail2;
  387. }
  388. if (reply_data && !NCDValueGenerator_AppendGenerate(reply_data, &str)) {
  389. ModuleLog(o->i, BLOG_ERROR, "NCDValueGenerator_AppendGenerate failed");
  390. goto fail2;
  391. }
  392. size_t len = ExpString_Length(&str);
  393. if (len > INT_MAX || len > PACKETPROTO_ENCLEN(SEND_MTU) || len - sizeof(struct packetproto_header) > UINT16_MAX) {
  394. ModuleLog(o->i, BLOG_ERROR, "reply is too long");
  395. goto fail2;
  396. }
  397. r->send_buf = ExpString_Get(&str);
  398. struct reply_header *header = (struct reply_header *)r->send_buf;
  399. header->pp.len = htol16(len - sizeof(struct packetproto_header));
  400. header->rp.request_id = htol32(request_id);
  401. header->rp.flags = htol32(flags);
  402. PacketPassInterface_Sender_Send(r->send_qflow_if, r->send_buf, len);
  403. return 1;
  404. fail2:
  405. ExpString_Free(&str);
  406. fail1:
  407. PacketPassFifoQueueFlow_Free(&r->send_qflow);
  408. LinkedList0_Remove(&c->replies_list, &r->replies_list_node);
  409. free(r);
  410. fail0:
  411. return 0;
  412. }
  413. static void reply_free (struct reply *r)
  414. {
  415. struct connection *c = r->con;
  416. PacketPassFifoQueueFlow_AssertFree(&r->send_qflow);
  417. free(r->send_buf);
  418. PacketPassFifoQueueFlow_Free(&r->send_qflow);
  419. LinkedList0_Remove(&c->replies_list, &r->replies_list_node);
  420. free(r);
  421. }
  422. static void reply_send_qflow_if_handler_done (struct reply *r)
  423. {
  424. reply_free(r);
  425. }
  426. static void func_new (NCDModuleInst *i)
  427. {
  428. // allocate structure
  429. struct instance *o = malloc(sizeof(*o));
  430. if (!o) {
  431. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  432. goto fail0;
  433. }
  434. o->i = i;
  435. NCDModuleInst_Backend_SetUser(i, o);
  436. // check arguments
  437. NCDValue *socket_path_arg;
  438. NCDValue *request_handler_template_arg;
  439. NCDValue *args_arg;
  440. if (!NCDValue_ListRead(i->args, 3, &socket_path_arg, &request_handler_template_arg, &args_arg)) {
  441. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  442. goto fail1;
  443. }
  444. if (NCDValue_Type(socket_path_arg) != NCDVALUE_STRING || NCDValue_Type(request_handler_template_arg) != NCDVALUE_STRING ||
  445. NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  446. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  447. goto fail1;
  448. }
  449. o->socket_path = NCDValue_StringValue(socket_path_arg);
  450. o->request_handler_template = NCDValue_StringValue(request_handler_template_arg);
  451. o->args = args_arg;
  452. // make sure socket file doesn't exist
  453. if (unlink(o->socket_path) < 0 && errno != ENOENT) {
  454. ModuleLog(o->i, BLOG_ERROR, "unlink failed");
  455. goto fail1;
  456. }
  457. // init listener
  458. if (!BListener_InitUnix(&o->listener, o->socket_path, i->params->reactor, o, (BListener_handler)listener_handler)) {
  459. ModuleLog(o->i, BLOG_ERROR, "BListener_InitUnix failed");
  460. goto fail1;
  461. }
  462. // init connections list
  463. LinkedList0_Init(&o->connections_list);
  464. // set not dying
  465. o->dying = 0;
  466. // signal up
  467. NCDModuleInst_Backend_Up(i);
  468. return;
  469. fail1:
  470. free(o);
  471. fail0:
  472. NCDModuleInst_Backend_SetError(i);
  473. NCDModuleInst_Backend_Dead(i);
  474. }
  475. static void instance_free (struct instance *o)
  476. {
  477. NCDModuleInst *i = o->i;
  478. ASSERT(o->dying)
  479. ASSERT(LinkedList0_IsEmpty(&o->connections_list))
  480. // free structure
  481. free(o);
  482. NCDModuleInst_Backend_Dead(i);
  483. }
  484. static void func_die (void *vo)
  485. {
  486. struct instance *o = vo;
  487. NCDModuleInst *i = o->i;
  488. ASSERT(!o->dying)
  489. // free listener
  490. BListener_Free(&o->listener);
  491. // remove socket file
  492. if (unlink(o->socket_path)) {
  493. ModuleLog(o->i, BLOG_ERROR, "unlink failed");
  494. }
  495. // terminate connections
  496. LinkedList0Node *next_ln;
  497. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->connections_list); ln && (next_ln = LinkedList0Node_Next(ln)), ln; ln = next_ln) {
  498. struct connection *c = UPPER_OBJECT(ln, struct connection, connections_list_node);
  499. ASSERT(c->inst == o)
  500. if (c->state != CONNECTION_STATE_TERMINATING) {
  501. connection_terminate(c);
  502. }
  503. }
  504. // set dying
  505. o->dying = 1;
  506. // if no connections, die right away
  507. if (LinkedList0_IsEmpty(&o->connections_list)) {
  508. instance_free(o);
  509. return;
  510. }
  511. }
  512. static void reply_func_new (NCDModuleInst *i)
  513. {
  514. NCDValue *reply_data;
  515. if (!NCDValue_ListRead(i->args, 1, &reply_data)) {
  516. ModuleLog(i, BLOG_ERROR, "wrong arity");
  517. goto fail;
  518. }
  519. NCDModuleInst_Backend_Up(i);
  520. struct request *r = i->method_user;
  521. struct connection *c = r->con;
  522. if (c->state != CONNECTION_STATE_RUNNING) {
  523. ModuleLog(i, BLOG_ERROR, "connection is terminating, cannot submit reply");
  524. goto fail;
  525. }
  526. if (r->finished || BPending_IsSet(&r->finish_job)) {
  527. ModuleLog(i, BLOG_ERROR, "request is already finished, cannot submit reply");
  528. goto fail;
  529. }
  530. if (!reply_init(c, r->request_id, REQUESTPROTO_REPLY_FLAG_DATA, reply_data)) {
  531. ModuleLog(i, BLOG_ERROR, "failed to submit reply");
  532. goto fail;
  533. }
  534. return;
  535. fail:
  536. NCDModuleInst_Backend_SetError(i);
  537. NCDModuleInst_Backend_Dead(i);
  538. }
  539. static void finish_func_new (NCDModuleInst *i)
  540. {
  541. if (!NCDValue_ListRead(i->args, 0)) {
  542. ModuleLog(i, BLOG_ERROR, "wrong arity");
  543. goto fail;
  544. }
  545. NCDModuleInst_Backend_Up(i);
  546. struct request *r = i->method_user;
  547. struct connection *c = r->con;
  548. if (c->state != CONNECTION_STATE_RUNNING) {
  549. ModuleLog(i, BLOG_ERROR, "connection is terminating, cannot submit reply");
  550. goto fail;
  551. }
  552. if (r->finished || BPending_IsSet(&r->finish_job)) {
  553. ModuleLog(i, BLOG_ERROR, "request is already finished, cannot submit reply");
  554. goto fail;
  555. }
  556. BPending_Set(&r->finish_job);
  557. if (!reply_init(c, r->request_id, REQUESTPROTO_REPLY_FLAG_END, NULL)) {
  558. ModuleLog(i, BLOG_ERROR, "failed to submit reply");
  559. BPending_Unset(&r->finish_job); // don't terminate request process!
  560. goto fail;
  561. }
  562. return;
  563. fail:
  564. NCDModuleInst_Backend_SetError(i);
  565. NCDModuleInst_Backend_Dead(i);
  566. }
  567. static const struct NCDModule modules[] = {
  568. {
  569. .type = "sys.request_server",
  570. .func_new = func_new,
  571. .func_die = func_die
  572. }, {
  573. .type = "sys.request_server.request::reply",
  574. .func_new = reply_func_new
  575. }, {
  576. .type = "sys.request_server.request::finish",
  577. .func_new = finish_func_new
  578. }, {
  579. .type = NULL
  580. }
  581. };
  582. const struct NCDModuleGroup ncdmodule_sys_request_server = {
  583. .modules = modules
  584. };