sys_request_server.c 23 KB

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