sys_request_server.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 reply;
  91. struct connection {
  92. struct instance *inst;
  93. LinkedList0Node connections_list_node;
  94. BConnection con;
  95. PacketProtoDecoder recv_decoder;
  96. PacketPassInterface recv_if;
  97. PacketPassFifoQueue send_queue;
  98. PacketStreamSender send_pss;
  99. LinkedList0 requests_list;
  100. LinkedList0 replies_list;
  101. int state;
  102. };
  103. struct request {
  104. struct connection *con;
  105. uint32_t request_id;
  106. LinkedList0Node requests_list_node;
  107. NCDValue request_data;
  108. struct reply *end_reply;
  109. NCDModuleProcess process;
  110. int terminating;
  111. int got_finished;
  112. };
  113. struct reply {
  114. struct connection *con;
  115. LinkedList0Node replies_list_node;
  116. PacketPassFifoQueueFlow send_qflow;
  117. PacketPassInterface *send_qflow_if;
  118. uint8_t *send_buf;
  119. };
  120. static void listener_handler (struct instance *o);
  121. static void connection_free (struct connection *c);
  122. static void connection_free_link (struct connection *c);
  123. static void connection_terminate (struct connection *c);
  124. static void connection_con_handler (struct connection *c, int event);
  125. static void connection_recv_decoder_handler_error (struct connection *c);
  126. static void connection_recv_if_handler_send (struct connection *c, uint8_t *data, int data_len);
  127. static int request_init (struct connection *c, uint32_t request_id, const uint8_t *data, int data_len);
  128. static void request_free (struct request *r);
  129. static struct request * find_request (struct connection *c, uint32_t request_id);
  130. static void request_process_handler_event (struct request *r, int event);
  131. static int request_process_func_getspecialobj (struct request *r, const char *name, NCDObject *out_object);
  132. static int request_process_request_obj_func_getvar (struct request *r, const char *name, NCDValue *out_value);
  133. static void request_terminate (struct request *r);
  134. static struct reply * reply_init (struct connection *c, uint32_t request_id, NCDValue *reply_data);
  135. static void reply_start (struct reply *r, uint32_t type);
  136. static void reply_free (struct reply *r);
  137. static void reply_send_qflow_if_handler_done (struct reply *r);
  138. static void instance_free (struct instance *o);
  139. static void listener_handler (struct instance *o)
  140. {
  141. ASSERT(!o->dying)
  142. BReactor *reactor = o->i->params->reactor;
  143. BPendingGroup *pg = BReactor_PendingGroup(reactor);
  144. struct connection *c = malloc(sizeof(*c));
  145. if (!c) {
  146. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  147. goto fail0;
  148. }
  149. c->inst = o;
  150. LinkedList0_Prepend(&o->connections_list, &c->connections_list_node);
  151. if (!BConnection_Init(&c->con, BCONNECTION_SOURCE_LISTENER(&o->listener, NULL), reactor, c, (BConnection_handler)connection_con_handler)) {
  152. ModuleLog(o->i, BLOG_ERROR, "BConnection_Init failed");
  153. goto fail1;
  154. }
  155. BConnection_SendAsync_Init(&c->con);
  156. BConnection_RecvAsync_Init(&c->con);
  157. StreamPassInterface *con_send_if = BConnection_SendAsync_GetIf(&c->con);
  158. StreamRecvInterface *con_recv_if = BConnection_RecvAsync_GetIf(&c->con);
  159. PacketPassInterface_Init(&c->recv_if, RECV_MTU, (PacketPassInterface_handler_send)connection_recv_if_handler_send, c, pg);
  160. if (!PacketProtoDecoder_Init(&c->recv_decoder, con_recv_if, &c->recv_if, pg, c, (PacketProtoDecoder_handler_error)connection_recv_decoder_handler_error)) {
  161. ModuleLog(o->i, BLOG_ERROR, "PacketProtoDecoder_Init failed");
  162. goto fail2;
  163. }
  164. PacketStreamSender_Init(&c->send_pss, con_send_if, PACKETPROTO_ENCLEN(SEND_MTU), pg);
  165. PacketPassFifoQueue_Init(&c->send_queue, PacketStreamSender_GetInput(&c->send_pss), pg);
  166. LinkedList0_Init(&c->requests_list);
  167. LinkedList0_Init(&c->replies_list);
  168. c->state = CONNECTION_STATE_RUNNING;
  169. ModuleLog(o->i, BLOG_INFO, "connection initialized");
  170. return;
  171. fail3:
  172. PacketStreamSender_Free(&c->send_pss);
  173. PacketProtoDecoder_Free(&c->recv_decoder);
  174. fail2:
  175. PacketPassInterface_Free(&c->recv_if);
  176. BConnection_RecvAsync_Free(&c->con);
  177. BConnection_SendAsync_Free(&c->con);
  178. BConnection_Free(&c->con);
  179. fail1:
  180. LinkedList0_Remove(&o->connections_list, &c->connections_list_node);
  181. free(c);
  182. fail0:
  183. return;
  184. }
  185. static void connection_free (struct connection *c)
  186. {
  187. struct instance *o = c->inst;
  188. ASSERT(c->state == CONNECTION_STATE_TERMINATING)
  189. ASSERT(LinkedList0_IsEmpty(&c->requests_list))
  190. ASSERT(LinkedList0_IsEmpty(&c->replies_list))
  191. LinkedList0_Remove(&o->connections_list, &c->connections_list_node);
  192. free(c);
  193. }
  194. static void connection_free_link (struct connection *c)
  195. {
  196. PacketPassFifoQueue_PrepareFree(&c->send_queue);
  197. LinkedList0Node *ln;
  198. while (ln = LinkedList0_GetFirst(&c->replies_list)) {
  199. struct reply *r = UPPER_OBJECT(ln, struct reply, replies_list_node);
  200. ASSERT(r->con == c)
  201. reply_free(r);
  202. }
  203. PacketPassFifoQueue_Free(&c->send_queue);
  204. PacketStreamSender_Free(&c->send_pss);
  205. PacketProtoDecoder_Free(&c->recv_decoder);
  206. PacketPassInterface_Free(&c->recv_if);
  207. BConnection_RecvAsync_Free(&c->con);
  208. BConnection_SendAsync_Free(&c->con);
  209. BConnection_Free(&c->con);
  210. }
  211. static void connection_terminate (struct connection *c)
  212. {
  213. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  214. for (LinkedList0Node *ln = LinkedList0_GetFirst(&c->requests_list); ln; ln = LinkedList0Node_Next(ln)) {
  215. struct request *r = UPPER_OBJECT(ln, struct request, requests_list_node);
  216. if (!r->terminating) {
  217. request_terminate(r);
  218. }
  219. }
  220. connection_free_link(c);
  221. c->state = CONNECTION_STATE_TERMINATING;
  222. if (LinkedList0_IsEmpty(&c->requests_list)) {
  223. connection_free(c);
  224. return;
  225. }
  226. }
  227. static void connection_con_handler (struct connection *c, int event)
  228. {
  229. struct instance *o = c->inst;
  230. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  231. ModuleLog(o->i, BLOG_INFO, "connection closed");
  232. connection_terminate(c);
  233. }
  234. static void connection_recv_decoder_handler_error (struct connection *c)
  235. {
  236. struct instance *o = c->inst;
  237. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  238. ModuleLog(o->i, BLOG_ERROR, "decoder error");
  239. connection_terminate(c);
  240. }
  241. static void connection_recv_if_handler_send (struct connection *c, uint8_t *data, int data_len)
  242. {
  243. struct instance *o = c->inst;
  244. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  245. ASSERT(data_len >= 0)
  246. ASSERT(data_len <= RECV_MTU)
  247. PacketPassInterface_Done(&c->recv_if);
  248. if (data_len < sizeof(struct requestproto_header)) {
  249. ModuleLog(o->i, BLOG_ERROR, "missing requestproto header");
  250. goto fail;
  251. }
  252. struct requestproto_header *header = (void *)data;
  253. uint32_t request_id = ltoh32(header->request_id);
  254. uint32_t type = ltoh32(header->type);
  255. switch (type) {
  256. case REQUESTPROTO_TYPE_CLIENT_REQUEST: {
  257. if (find_request(c, request_id)) {
  258. ModuleLog(o->i, BLOG_ERROR, "request with the same ID already exists");
  259. goto fail;
  260. }
  261. if (!request_init(c, request_id, data + sizeof(*header), data_len - sizeof(*header))) {
  262. goto fail;
  263. }
  264. } break;
  265. case REQUESTPROTO_TYPE_CLIENT_ABORT: {
  266. struct request *r = find_request(c, request_id);
  267. if (!r) {
  268. // this is expected if we finish before we get the abort
  269. return;
  270. }
  271. if (!r->terminating) {
  272. request_terminate(r);
  273. }
  274. } break;
  275. default:
  276. ModuleLog(o->i, BLOG_ERROR, "invalid requestproto type");
  277. goto fail;
  278. }
  279. return;
  280. fail:
  281. connection_terminate(c);
  282. }
  283. static int request_init (struct connection *c, uint32_t request_id, const uint8_t *data, int data_len)
  284. {
  285. struct instance *o = c->inst;
  286. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  287. ASSERT(!find_request(c, request_id))
  288. ASSERT(data_len >= 0)
  289. ASSERT(data_len <= RECV_PAYLOAD_MTU)
  290. struct request *r = malloc(sizeof(*r));
  291. if (!r) {
  292. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  293. goto fail0;
  294. }
  295. r->con = c;
  296. r->request_id = request_id;
  297. LinkedList0_Prepend(&c->requests_list, &r->requests_list_node);
  298. if (!NCDValueParser_Parse((const char *)data, data_len, &r->request_data)) {
  299. ModuleLog(o->i, BLOG_ERROR, "NCDValueParser_Parse failed");
  300. goto fail1;
  301. }
  302. if (!(r->end_reply = reply_init(c, request_id, NULL))) {
  303. goto fail2;
  304. }
  305. NCDValue args;
  306. if (!NCDValue_InitCopy(&args, o->args)) {
  307. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  308. goto fail3;
  309. }
  310. if (!NCDModuleProcess_Init(&r->process, o->i, o->request_handler_template, args, r, (NCDModuleProcess_handler_event)request_process_handler_event)) {
  311. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  312. NCDValue_Free(&args);
  313. goto fail3;
  314. }
  315. NCDModuleProcess_SetSpecialFuncs(&r->process, (NCDModuleProcess_func_getspecialobj)request_process_func_getspecialobj);
  316. r->terminating = 0;
  317. r->got_finished = 0;
  318. ModuleLog(o->i, BLOG_INFO, "request initialized");
  319. return 1;
  320. fail3:
  321. reply_free(r->end_reply);
  322. fail2:
  323. NCDValue_Free(&r->request_data);
  324. fail1:
  325. LinkedList0_Remove(&c->requests_list, &r->requests_list_node);
  326. free(r);
  327. fail0:
  328. return 0;
  329. }
  330. static void request_free (struct request *r)
  331. {
  332. struct connection *c = r->con;
  333. NCDModuleProcess_AssertFree(&r->process);
  334. if (c->state != CONNECTION_STATE_TERMINATING) {
  335. uint32_t type = r->got_finished ? REQUESTPROTO_TYPE_SERVER_FINISHED : REQUESTPROTO_TYPE_SERVER_ERROR;
  336. reply_start(r->end_reply, type);
  337. }
  338. NCDModuleProcess_Free(&r->process);
  339. NCDValue_Free(&r->request_data);
  340. LinkedList0_Remove(&c->requests_list, &r->requests_list_node);
  341. free(r);
  342. }
  343. static struct request * find_request (struct connection *c, uint32_t request_id)
  344. {
  345. for (LinkedList0Node *ln = LinkedList0_GetFirst(&c->requests_list); ln; ln = LinkedList0Node_Next(ln)) {
  346. struct request *r = UPPER_OBJECT(ln, struct request, requests_list_node);
  347. if (!r->terminating && r->request_id == request_id) {
  348. return r;
  349. }
  350. }
  351. return NULL;
  352. }
  353. static void request_process_handler_event (struct request *r, int event)
  354. {
  355. struct connection *c = r->con;
  356. struct instance *o = c->inst;
  357. switch (event) {
  358. case NCDMODULEPROCESS_EVENT_UP: {
  359. ASSERT(!r->terminating)
  360. } break;
  361. case NCDMODULEPROCESS_EVENT_DOWN: {
  362. ASSERT(!r->terminating)
  363. NCDModuleProcess_Continue(&r->process);
  364. } break;
  365. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  366. ASSERT(r->terminating)
  367. request_free(r);
  368. if (c->state == CONNECTION_STATE_TERMINATING && LinkedList0_IsEmpty(&c->requests_list)) {
  369. connection_free(c);
  370. if (o->dying && LinkedList0_IsEmpty(&o->connections_list)) {
  371. instance_free(o);
  372. return;
  373. }
  374. }
  375. } break;
  376. default: ASSERT(0);
  377. }
  378. }
  379. static int request_process_func_getspecialobj (struct request *r, const char *name, NCDObject *out_object)
  380. {
  381. if (!strcmp(name, "_request")) {
  382. *out_object = NCDObject_Build("sys.request_server.request", r, (NCDObject_func_getvar)request_process_request_obj_func_getvar, NULL);
  383. return 1;
  384. }
  385. return 0;
  386. }
  387. static int request_process_request_obj_func_getvar (struct request *r, const char *name, NCDValue *out_value)
  388. {
  389. struct instance *o = r->con->inst;
  390. if (!strcmp(name, "data")) {
  391. if (!NCDValue_InitCopy(out_value, &r->request_data)) {
  392. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  393. return 0;
  394. }
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. static void request_terminate (struct request *r)
  400. {
  401. ASSERT(!r->terminating)
  402. NCDModuleProcess_Terminate(&r->process);
  403. r->terminating = 1;
  404. }
  405. static struct reply * reply_init (struct connection *c, uint32_t request_id, NCDValue *reply_data)
  406. {
  407. struct instance *o = c->inst;
  408. ASSERT(c->state == CONNECTION_STATE_RUNNING)
  409. struct reply *r = malloc(sizeof(*r));
  410. if (!r) {
  411. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  412. goto fail0;
  413. }
  414. r->con = c;
  415. LinkedList0_Prepend(&c->replies_list, &r->replies_list_node);
  416. PacketPassFifoQueueFlow_Init(&r->send_qflow, &c->send_queue);
  417. r->send_qflow_if = PacketPassFifoQueueFlow_GetInput(&r->send_qflow);
  418. PacketPassInterface_Sender_Init(r->send_qflow_if, (PacketPassInterface_handler_done)reply_send_qflow_if_handler_done, r);
  419. struct reply_header {
  420. struct packetproto_header pp;
  421. struct requestproto_header rp;
  422. } __attribute__((packed));
  423. ExpString str;
  424. if (!ExpString_Init(&str)) {
  425. ModuleLog(o->i, BLOG_ERROR, "ExpString_Init failed");
  426. goto fail1;
  427. }
  428. if (!ExpString_AppendZeros(&str, sizeof(struct reply_header))) {
  429. ModuleLog(o->i, BLOG_ERROR, "ExpString_AppendBinary failed");
  430. goto fail2;
  431. }
  432. if (reply_data && !NCDValueGenerator_AppendGenerate(reply_data, &str)) {
  433. ModuleLog(o->i, BLOG_ERROR, "NCDValueGenerator_AppendGenerate failed");
  434. goto fail2;
  435. }
  436. size_t len = ExpString_Length(&str);
  437. if (len > INT_MAX || len > PACKETPROTO_ENCLEN(SEND_MTU) || len - sizeof(struct packetproto_header) > UINT16_MAX) {
  438. ModuleLog(o->i, BLOG_ERROR, "reply is too long");
  439. goto fail2;
  440. }
  441. r->send_buf = (uint8_t *)ExpString_Get(&str);
  442. struct reply_header *header = (void *)r->send_buf;
  443. header->pp.len = htol16(len - sizeof(header->pp));
  444. header->rp.request_id = htol32(request_id);
  445. return r;
  446. fail2:
  447. ExpString_Free(&str);
  448. fail1:
  449. PacketPassFifoQueueFlow_Free(&r->send_qflow);
  450. LinkedList0_Remove(&c->replies_list, &r->replies_list_node);
  451. free(r);
  452. fail0:
  453. return NULL;
  454. }
  455. static void reply_start (struct reply *r, uint32_t type)
  456. {
  457. struct reply_header {
  458. struct packetproto_header pp;
  459. struct requestproto_header rp;
  460. } __attribute__((packed));
  461. struct reply_header *header = (void *)r->send_buf;
  462. header->rp.type = htol32(type);
  463. int len = ltoh16(header->pp.len) + sizeof(struct packetproto_header);
  464. PacketPassInterface_Sender_Send(r->send_qflow_if, r->send_buf, len);
  465. }
  466. static void reply_free (struct reply *r)
  467. {
  468. struct connection *c = r->con;
  469. PacketPassFifoQueueFlow_AssertFree(&r->send_qflow);
  470. free(r->send_buf);
  471. PacketPassFifoQueueFlow_Free(&r->send_qflow);
  472. LinkedList0_Remove(&c->replies_list, &r->replies_list_node);
  473. free(r);
  474. }
  475. static void reply_send_qflow_if_handler_done (struct reply *r)
  476. {
  477. reply_free(r);
  478. }
  479. static void func_new (NCDModuleInst *i)
  480. {
  481. // allocate structure
  482. struct instance *o = malloc(sizeof(*o));
  483. if (!o) {
  484. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  485. goto fail0;
  486. }
  487. o->i = i;
  488. NCDModuleInst_Backend_SetUser(i, o);
  489. // check arguments
  490. NCDValue *socket_path_arg;
  491. NCDValue *request_handler_template_arg;
  492. NCDValue *args_arg;
  493. if (!NCDValue_ListRead(i->args, 3, &socket_path_arg, &request_handler_template_arg, &args_arg)) {
  494. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  495. goto fail1;
  496. }
  497. if (NCDValue_Type(socket_path_arg) != NCDVALUE_STRING || NCDValue_Type(request_handler_template_arg) != NCDVALUE_STRING ||
  498. NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  499. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  500. goto fail1;
  501. }
  502. o->socket_path = NCDValue_StringValue(socket_path_arg);
  503. o->request_handler_template = NCDValue_StringValue(request_handler_template_arg);
  504. o->args = args_arg;
  505. // make sure socket file doesn't exist
  506. if (unlink(o->socket_path) < 0 && errno != ENOENT) {
  507. ModuleLog(o->i, BLOG_ERROR, "unlink failed");
  508. goto fail1;
  509. }
  510. // init listener
  511. if (!BListener_InitUnix(&o->listener, o->socket_path, i->params->reactor, o, (BListener_handler)listener_handler)) {
  512. ModuleLog(o->i, BLOG_ERROR, "BListener_InitUnix failed");
  513. goto fail1;
  514. }
  515. // init connections list
  516. LinkedList0_Init(&o->connections_list);
  517. // set not dying
  518. o->dying = 0;
  519. // signal up
  520. NCDModuleInst_Backend_Up(i);
  521. return;
  522. fail1:
  523. free(o);
  524. fail0:
  525. NCDModuleInst_Backend_SetError(i);
  526. NCDModuleInst_Backend_Dead(i);
  527. }
  528. static void instance_free (struct instance *o)
  529. {
  530. NCDModuleInst *i = o->i;
  531. ASSERT(o->dying)
  532. ASSERT(LinkedList0_IsEmpty(&o->connections_list))
  533. // free structure
  534. free(o);
  535. NCDModuleInst_Backend_Dead(i);
  536. }
  537. static void func_die (void *vo)
  538. {
  539. struct instance *o = vo;
  540. NCDModuleInst *i = o->i;
  541. ASSERT(!o->dying)
  542. // free listener
  543. BListener_Free(&o->listener);
  544. // remove socket file
  545. if (unlink(o->socket_path)) {
  546. ModuleLog(o->i, BLOG_ERROR, "unlink failed");
  547. }
  548. // terminate connections
  549. LinkedList0Node *next_ln;
  550. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->connections_list); ln && (next_ln = LinkedList0Node_Next(ln)), ln; ln = next_ln) {
  551. struct connection *c = UPPER_OBJECT(ln, struct connection, connections_list_node);
  552. ASSERT(c->inst == o)
  553. if (c->state != CONNECTION_STATE_TERMINATING) {
  554. connection_terminate(c);
  555. }
  556. }
  557. // set dying
  558. o->dying = 1;
  559. // if no connections, die right away
  560. if (LinkedList0_IsEmpty(&o->connections_list)) {
  561. instance_free(o);
  562. return;
  563. }
  564. }
  565. static void reply_func_new (NCDModuleInst *i)
  566. {
  567. NCDValue *reply_data;
  568. if (!NCDValue_ListRead(i->args, 1, &reply_data)) {
  569. ModuleLog(i, BLOG_ERROR, "wrong arity");
  570. goto fail;
  571. }
  572. NCDModuleInst_Backend_Up(i);
  573. struct request *r = i->method_user;
  574. struct connection *c = r->con;
  575. if (r->terminating) {
  576. ModuleLog(i, BLOG_ERROR, "request is dying, cannot submit reply");
  577. goto fail;
  578. }
  579. struct reply *rpl = reply_init(c, r->request_id, reply_data);
  580. if (!rpl) {
  581. ModuleLog(i, BLOG_ERROR, "failed to submit reply");
  582. goto fail;
  583. }
  584. reply_start(rpl, REQUESTPROTO_TYPE_SERVER_REPLY);
  585. return;
  586. fail:
  587. NCDModuleInst_Backend_SetError(i);
  588. NCDModuleInst_Backend_Dead(i);
  589. }
  590. static void finish_func_new (NCDModuleInst *i)
  591. {
  592. if (!NCDValue_ListRead(i->args, 0)) {
  593. ModuleLog(i, BLOG_ERROR, "wrong arity");
  594. goto fail;
  595. }
  596. NCDModuleInst_Backend_Up(i);
  597. struct request *r = i->method_user;
  598. struct connection *c = r->con;
  599. if (r->terminating) {
  600. ModuleLog(i, BLOG_ERROR, "request is dying, cannot submit finished");
  601. goto fail;
  602. }
  603. r->got_finished = 1;
  604. request_terminate(r);
  605. return;
  606. fail:
  607. NCDModuleInst_Backend_SetError(i);
  608. NCDModuleInst_Backend_Dead(i);
  609. }
  610. static const struct NCDModule modules[] = {
  611. {
  612. .type = "sys.request_server",
  613. .func_new = func_new,
  614. .func_die = func_die
  615. }, {
  616. .type = "sys.request_server.request::reply",
  617. .func_new = reply_func_new
  618. }, {
  619. .type = "sys.request_server.request::finish",
  620. .func_new = finish_func_new
  621. }, {
  622. .type = NULL
  623. }
  624. };
  625. const struct NCDModuleGroup ncdmodule_sys_request_server = {
  626. .modules = modules
  627. };