sys_start_process.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /**
  2. * @file sys_start_process.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. * Synopsis:
  32. * sys.start_process(list command, string mode)
  33. *
  34. * Variables:
  35. * is_error - "true" if there was an error starting the process, "false" if the process
  36. * has been started successfully
  37. *
  38. * Synopsis:
  39. * sys.start_process::wait()
  40. *
  41. * Variables:
  42. * exit_status - the exit code if the process terminated normally, -1 if it terminated
  43. * with a signal
  44. *
  45. * Synopsis:
  46. * sys.start_process::terminate()
  47. * sys.start_process::kill()
  48. *
  49. * Synopsis:
  50. * sys.start_process::read_pipe()
  51. *
  52. * Description:
  53. * Creates a read interface to the process's standard output. Data is read using the
  54. * read() method on this object. Read errors are reported implicitly by this statement
  55. * going down and the 'is_error' variable changing to "true".
  56. * When read_pipe() is initialized for a process, it takes ownership of the read pipe
  57. * to the process. When read_pipe() is requested to terminate, it will close the pipe.
  58. * Attempting to initialize read_pipe() on a process which was not started with 'r'
  59. * in the mode argument, or where another read_pipe() object has already taken ownership
  60. * of the read pipe, will result in throwing an error to the interpreter.
  61. *
  62. * Variables:
  63. * string is_error - "true" if there was a read error, "false" if not
  64. *
  65. * Synopsis:
  66. * sys.start_process::read_pipe::read()
  67. *
  68. * Description:
  69. * Reads some data. If a read error occurs, it is reported implicitly via the
  70. * read_pipe() object going down. If end of file is reached, this and any future read()
  71. * operations will indicate that via the 'not_eof' variable. It is guaranteed that after
  72. * EOF is reached, the read_pipe() object will not go down to report any errors.
  73. * WARNING: if a read() is requested to terminate before it has completed, the
  74. * read_pipe() will become unusable and any read() invocation after that will
  75. * throw an error to the interpreter.
  76. *
  77. * Variables:
  78. * string (empty) - data that was read, or an empty string on EOF
  79. * string not_eof - "true" is EOF was not reached, "false" if it was
  80. *
  81. * Synopsis:
  82. * sys.start_process::write_pipe()
  83. *
  84. * Description:
  85. * Creates a write interface to the process's standard input. Data is written using the
  86. * write() method on this object. Write errors are reported implicitly by this statement
  87. * going down and the ''is_error variable changing to "true".
  88. * When write_pipe() is initialized for a process, it takes ownership of the write pipe
  89. * to the process. When write_pipe() is requested to terminate, it will close the pipe
  90. * (unless the close() has been used).
  91. * Attempting to initialize write_pipe() on a process which was not started with 'w'
  92. * in the mode argument, or where another write_pipe() object has already taken ownership
  93. * of the write pope, will result in throwing an error to the interpreter.
  94. *
  95. * Variables:
  96. * string is_error - "true" if there was a write error, "false" if not
  97. *
  98. * Synopsis:
  99. * sys.start_process::write_pipe::write(string data)
  100. *
  101. * Description:
  102. * Writes the given data. If a write error occurs, it is reported implicitly via the
  103. * write_pipe() object going down.
  104. * WARNING: if a write() is requested to terminate before it has completed, the
  105. * write_pipe() will become unusable and any write() or close() invocation after
  106. * that will throw an error to the interpreter.
  107. *
  108. * Synopsis:
  109. * sys.start_process::write_pipe::close(string data)
  110. *
  111. * Description:
  112. * Closes the write pipe. This will make whatever is reading the other end of the pipe
  113. * encounter EOF after it has read any pending data. It is guaranteed that after the
  114. * pipe is closed, the write_pipe() object will not go down to report any errors.
  115. * After close() is performed, any further write() or close() calls are disallowed and
  116. * will throw errors to the interpreter.
  117. */
  118. #include <stdlib.h>
  119. #include <string.h>
  120. #include <stdio.h>
  121. #include <inttypes.h>
  122. #include <limits.h>
  123. #include <unistd.h>
  124. #include <misc/offset.h>
  125. #include <structure/LinkedList0.h>
  126. #include <system/BProcess.h>
  127. #include <system/BConnection.h>
  128. #include <ncd/NCDModule.h>
  129. #include <ncd/static_strings.h>
  130. #include <ncd/extra/NCDBuf.h>
  131. #include <ncd/extra/value_utils.h>
  132. #include <ncd/extra/build_cmdline.h>
  133. #include <generated/blog_channel_ncd_sys_start_process.h>
  134. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  135. #define READ_BUF_SIZE 8192
  136. #define PROCESS_STATE_ERROR 1
  137. #define PROCESS_STATE_RUNNING 2
  138. #define PROCESS_STATE_TERMINATED 3
  139. #define PROCESS_STATE_DYING 4
  140. #define READER_STATE_RUNNING 1
  141. #define READER_STATE_EOF 2
  142. #define READER_STATE_ERROR 3
  143. #define READER_STATE_ABORTED 4
  144. #define WRITER_STATE_RUNNING 1
  145. #define WRITER_STATE_CLOSED 2
  146. #define WRITER_STATE_ERROR 3
  147. #define WRITER_STATE_ABORTED 4
  148. struct process_instance {
  149. NCDModuleInst *i;
  150. BProcess process;
  151. LinkedList0 waits_list;
  152. int read_fd;
  153. int write_fd;
  154. int exit_status;
  155. int state;
  156. };
  157. struct wait_instance {
  158. NCDModuleInst *i;
  159. struct process_instance *pinst;
  160. LinkedList0Node waits_list_node;
  161. int exit_status;
  162. };
  163. struct read_pipe_instance {
  164. NCDModuleInst *i;
  165. int state;
  166. int read_fd;
  167. BConnection connection;
  168. NCDBufStore store;
  169. struct read_instance *read_inst;
  170. };
  171. struct read_instance {
  172. NCDModuleInst *i;
  173. struct read_pipe_instance *read_pipe_inst;
  174. NCDBuf *buf;
  175. size_t read_size;
  176. };
  177. struct write_pipe_instance {
  178. NCDModuleInst *i;
  179. int state;
  180. int write_fd;
  181. BConnection connection;
  182. struct write_instance *write_inst;
  183. };
  184. struct write_instance {
  185. NCDModuleInst *i;
  186. struct write_pipe_instance *write_pipe_inst;
  187. const char *data;
  188. size_t length;
  189. };
  190. static int parse_mode (NCDModuleInst *i, NCDValRef mode_arg, int *out_read, int *out_write)
  191. {
  192. if (!NCDVal_IsString(mode_arg)) {
  193. ModuleLog(i, BLOG_ERROR, "mode argument must be a string");
  194. return 0;
  195. }
  196. const char *data = NCDVal_StringData(mode_arg);
  197. size_t length = NCDVal_StringLength(mode_arg);
  198. *out_read = 0;
  199. *out_write = 0;
  200. while (length > 0) {
  201. if (*data == 'r') {
  202. *out_read = 1;
  203. }
  204. else if (*data == 'w') {
  205. *out_write = 1;
  206. }
  207. else {
  208. ModuleLog(i, BLOG_ERROR, "invalid character in mode argument");
  209. return 0;
  210. }
  211. data++;
  212. length--;
  213. }
  214. return 1;
  215. }
  216. static void process_free (struct process_instance *o)
  217. {
  218. // close write fd
  219. if (o->write_fd != -1) {
  220. if (close(o->write_fd) < 0) {
  221. ModuleLog(o->i, BLOG_ERROR, "close failed");
  222. }
  223. }
  224. // close read fd
  225. if (o->read_fd != -1) {
  226. if (close(o->read_fd) < 0) {
  227. ModuleLog(o->i, BLOG_ERROR, "close failed");
  228. }
  229. }
  230. NCDModuleInst_Backend_Dead(o->i);
  231. }
  232. static void process_handler (void *vo, int normally, uint8_t normally_exit_status)
  233. {
  234. struct process_instance *o = vo;
  235. ASSERT(o->state == PROCESS_STATE_RUNNING || o->state == PROCESS_STATE_DYING)
  236. ModuleLog(o->i, BLOG_INFO, "process terminated");
  237. // free process
  238. BProcess_Free(&o->process);
  239. // remember exit code
  240. o->exit_status = (!normally ? -1 : normally_exit_status);
  241. // finish waits
  242. LinkedList0Node *ln;
  243. while ((ln = LinkedList0_GetFirst(&o->waits_list))) {
  244. struct wait_instance *winst = UPPER_OBJECT(ln, struct wait_instance, waits_list_node);
  245. ASSERT(winst->pinst == o)
  246. LinkedList0_Remove(&o->waits_list, &winst->waits_list_node);
  247. winst->pinst = NULL;
  248. winst->exit_status = o->exit_status;
  249. NCDModuleInst_Backend_Up(winst->i);
  250. }
  251. // if we have been requested to die, then die now
  252. if (o->state == PROCESS_STATE_DYING) {
  253. process_free(o);
  254. return;
  255. }
  256. // set state
  257. o->state = PROCESS_STATE_TERMINATED;
  258. }
  259. static void process_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  260. {
  261. struct process_instance *o = vo;
  262. o->i = i;
  263. NCDModuleInst_Backend_PassMemToMethods(i);
  264. // check arguments
  265. NCDValRef command_arg;
  266. NCDValRef mode_arg;
  267. if (!NCDVal_ListRead(params->args, 2, &command_arg, &mode_arg)) {
  268. ModuleLog(i, BLOG_ERROR, "wrong arity");
  269. goto fail0;
  270. }
  271. // parse mode
  272. int is_read;
  273. int is_write;
  274. if (!parse_mode(i, mode_arg, &is_read, &is_write)) {
  275. goto fail0;
  276. }
  277. // prepare for creating pipes
  278. int fds[3];
  279. int fds_map[2];
  280. int num_fds = 0;
  281. int read_fd = -1;
  282. int write_fd = -1;
  283. // create read pipe
  284. if (is_read) {
  285. int pipefd[2];
  286. if (pipe(pipefd) < 0) {
  287. ModuleLog(i, BLOG_ERROR, "pipe failed");
  288. goto error1;
  289. }
  290. read_fd = pipefd[0];
  291. fds[num_fds] = pipefd[1];
  292. fds_map[num_fds++] = STDOUT_FILENO;
  293. }
  294. // create write pipe
  295. if (is_write) {
  296. int pipefd[2];
  297. if (pipe(pipefd) < 0) {
  298. ModuleLog(i, BLOG_ERROR, "pipe failed");
  299. goto error1;
  300. }
  301. write_fd = pipefd[1];
  302. fds[num_fds] = pipefd[0];
  303. fds_map[num_fds++] = STDIN_FILENO;
  304. }
  305. // terminate fds array
  306. fds[num_fds] = -1;
  307. // build process parameters struct
  308. struct BProcess_params p_params = {};
  309. p_params.fds = fds;
  310. p_params.fds_map = fds_map;
  311. // build command line
  312. char *exec;
  313. CmdLine cl;
  314. if (!ncd_build_cmdline(i, BLOG_CURRENT_CHANNEL, command_arg, &exec, &cl)) {
  315. goto error1;
  316. }
  317. // start process
  318. int res = BProcess_Init2(&o->process, i->params->iparams->manager, process_handler, o, exec, CmdLine_Get(&cl), p_params);
  319. CmdLine_Free(&cl);
  320. free(exec);
  321. if (!res) {
  322. ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
  323. goto error1;
  324. }
  325. // close child fds
  326. while (num_fds-- > 0) {
  327. if (close(fds[num_fds]) < 0) {
  328. ModuleLog(i, BLOG_ERROR, "close failed");
  329. }
  330. }
  331. // init waits list
  332. LinkedList0_Init(&o->waits_list);
  333. // remember our fds
  334. o->read_fd = read_fd;
  335. o->write_fd = write_fd;
  336. // set state
  337. o->state = PROCESS_STATE_RUNNING;
  338. // go up
  339. NCDModuleInst_Backend_Up(i);
  340. return;
  341. fail0:
  342. NCDModuleInst_Backend_DeadError(i);
  343. return;
  344. error1:
  345. if (write_fd != -1) {
  346. if (close(write_fd) < 0) {
  347. ModuleLog(i, BLOG_ERROR, "close failed");
  348. }
  349. }
  350. if (read_fd != -1) {
  351. if (close(read_fd) < 0) {
  352. ModuleLog(i, BLOG_ERROR, "close failed");
  353. }
  354. }
  355. while (num_fds-- > 0) {
  356. if (close(fds[num_fds]) < 0) {
  357. ModuleLog(i, BLOG_ERROR, "close failed");
  358. }
  359. }
  360. o->read_fd = -1;
  361. o->write_fd = -1;
  362. o->state = PROCESS_STATE_ERROR;
  363. NCDModuleInst_Backend_Up(i);
  364. }
  365. static void process_func_die (void *vo)
  366. {
  367. struct process_instance *o = vo;
  368. ASSERT(o->state != PROCESS_STATE_DYING)
  369. // if process is not running, die immediately
  370. if (o->state != PROCESS_STATE_RUNNING) {
  371. process_free(o);
  372. return;
  373. }
  374. ModuleLog(o->i, BLOG_INFO, "terminating process");
  375. // send termination signal
  376. BProcess_Terminate(&o->process);
  377. // set state
  378. o->state = PROCESS_STATE_DYING;
  379. }
  380. static int process_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  381. {
  382. struct process_instance *o = vo;
  383. if (name == NCD_STRING_IS_ERROR) {
  384. int is_error = (o->state == PROCESS_STATE_ERROR);
  385. *out = ncd_make_boolean(mem, is_error, o->i->params->iparams->string_index);
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. static void wait_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  391. {
  392. struct wait_instance *o = vo;
  393. o->i = i;
  394. if (!NCDVal_ListRead(params->args, 0)) {
  395. ModuleLog(i, BLOG_ERROR, "wrong arity");
  396. goto fail0;
  397. }
  398. struct process_instance *pinst = params->method_user;
  399. if (pinst->state == PROCESS_STATE_ERROR) {
  400. ModuleLog(i, BLOG_ERROR, "wait() is disallowed after the process has failed to start");
  401. goto fail0;
  402. }
  403. if (pinst->state == PROCESS_STATE_TERMINATED) {
  404. // not waiting, set no pinst
  405. o->pinst = NULL;
  406. // remember exit code
  407. o->exit_status = pinst->exit_status;
  408. // go up
  409. NCDModuleInst_Backend_Up(i);
  410. } else {
  411. // waitint, set pinst
  412. o->pinst = pinst;
  413. // insert to waits list
  414. LinkedList0_Prepend(&pinst->waits_list, &o->waits_list_node);
  415. }
  416. return;
  417. fail0:
  418. NCDModuleInst_Backend_DeadError(i);
  419. }
  420. static void wait_func_die (void *vo)
  421. {
  422. struct wait_instance *o = vo;
  423. // remove from waits list
  424. if (o->pinst) {
  425. LinkedList0_Remove(&o->pinst->waits_list, &o->waits_list_node);
  426. }
  427. NCDModuleInst_Backend_Dead(o->i);
  428. }
  429. static int wait_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  430. {
  431. struct wait_instance *o = vo;
  432. ASSERT(!o->pinst)
  433. if (name == NCD_STRING_EXIT_STATUS) {
  434. if (o->exit_status == -1) {
  435. *out = NCDVal_NewString(mem, "-1");
  436. } else {
  437. *out = ncd_make_uintmax(mem, o->exit_status);
  438. }
  439. return 1;
  440. }
  441. return 0;
  442. }
  443. static void terminate_kill_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_kill)
  444. {
  445. if (!NCDVal_ListRead(params->args, 0)) {
  446. ModuleLog(i, BLOG_ERROR, "wrong arity");
  447. goto fail0;
  448. }
  449. struct process_instance *pinst = params->method_user;
  450. if (pinst->state == PROCESS_STATE_ERROR) {
  451. ModuleLog(i, BLOG_ERROR, "terminate()/kill() is disallowed after the process has failed to start");
  452. goto fail0;
  453. }
  454. if (pinst->state != PROCESS_STATE_TERMINATED) {
  455. if (is_kill) {
  456. BProcess_Kill(&pinst->process);
  457. } else {
  458. BProcess_Terminate(&pinst->process);
  459. }
  460. }
  461. NCDModuleInst_Backend_Up(i);
  462. return;
  463. fail0:
  464. NCDModuleInst_Backend_DeadError(i);
  465. }
  466. static void terminate_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  467. {
  468. terminate_kill_new_common(vo, i, params, 0);
  469. }
  470. static void kill_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  471. {
  472. terminate_kill_new_common(vo, i, params, 1);
  473. }
  474. static void read_pipe_free_connection (struct read_pipe_instance *o)
  475. {
  476. // disconnect read instance
  477. if (o->read_inst) {
  478. ASSERT(o->read_inst->read_pipe_inst == o)
  479. o->read_inst->read_pipe_inst = NULL;
  480. }
  481. // free store
  482. NCDBufStore_Free(&o->store);
  483. // free connection read interface
  484. BConnection_RecvAsync_Free(&o->connection);
  485. // free connection
  486. BConnection_Free(&o->connection);
  487. // close fd
  488. if (close(o->read_fd) < 0) {
  489. ModuleLog(o->i, BLOG_ERROR, "close failed");
  490. }
  491. }
  492. static void read_pipe_abort (struct read_pipe_instance *o)
  493. {
  494. ASSERT(o->state == READER_STATE_RUNNING)
  495. // release connection resources
  496. read_pipe_free_connection(o);
  497. // set state
  498. o->state = READER_STATE_ABORTED;
  499. }
  500. static void read_pipe_connection_handler (void *vo, int event)
  501. {
  502. struct read_pipe_instance *o = vo;
  503. ASSERT(o->state == READER_STATE_RUNNING)
  504. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  505. // if we have read operation, make it finish with eof
  506. if (o->read_inst) {
  507. ASSERT(o->read_inst->read_pipe_inst == o)
  508. ASSERT(o->read_inst->buf)
  509. o->read_inst->read_pipe_inst = NULL;
  510. o->read_inst->read_size = 0;
  511. NCDModuleInst_Backend_Up(o->read_inst->i);
  512. o->read_inst = NULL;
  513. }
  514. // free connection resources
  515. read_pipe_free_connection(o);
  516. // set state closed
  517. o->state = READER_STATE_EOF;
  518. return;
  519. }
  520. ModuleLog(o->i, BLOG_ERROR, "read pipe error");
  521. // free connection resources
  522. read_pipe_free_connection(o);
  523. // set state error
  524. o->state = READER_STATE_ERROR;
  525. // backtrack
  526. NCDModuleInst_Backend_DownUp(o->i);
  527. }
  528. static void read_pipe_recv_handler_done (void *vo, int data_len)
  529. {
  530. struct read_pipe_instance *o = vo;
  531. ASSERT(o->state == READER_STATE_RUNNING)
  532. ASSERT(o->read_inst)
  533. ASSERT(o->read_inst->read_pipe_inst == o)
  534. ASSERT(o->read_inst->buf)
  535. ASSERT(data_len > 0)
  536. ASSERT(data_len <= NCDBufStore_BufSize(&o->store))
  537. // finish read operation
  538. o->read_inst->read_pipe_inst = NULL;
  539. o->read_inst->read_size = data_len;
  540. NCDModuleInst_Backend_Up(o->read_inst->i);
  541. o->read_inst = NULL;
  542. }
  543. static void read_pipe_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  544. {
  545. struct read_pipe_instance *o = vo;
  546. o->i = i;
  547. NCDModuleInst_Backend_PassMemToMethods(i);
  548. if (!NCDVal_ListRead(params->args, 0)) {
  549. ModuleLog(i, BLOG_ERROR, "wrong arity");
  550. goto fail0;
  551. }
  552. struct process_instance *pinst = params->method_user;
  553. if (pinst->read_fd == -1) {
  554. ModuleLog(i, BLOG_ERROR, "process did not start successfully, was not opened for reading or a read_pipe was already created");
  555. goto fail0;
  556. }
  557. // init connection
  558. if (!BConnection_Init(&o->connection, BConnection_source_pipe(pinst->read_fd), i->params->iparams->reactor, o, read_pipe_connection_handler)) {
  559. ModuleLog(i, BLOG_ERROR, "BConnection_Init failed");
  560. goto fail0;
  561. }
  562. // init connection read interface
  563. BConnection_RecvAsync_Init(&o->connection);
  564. // set recv done callback
  565. StreamRecvInterface_Receiver_Init(BConnection_RecvAsync_GetIf(&o->connection), read_pipe_recv_handler_done, o);
  566. // init store
  567. NCDBufStore_Init(&o->store, READ_BUF_SIZE);
  568. // set variables
  569. o->state = READER_STATE_RUNNING;
  570. o->read_fd = pinst->read_fd;
  571. o->read_inst = NULL;
  572. // steal read fd from process instance
  573. pinst->read_fd = -1;
  574. // go up
  575. NCDModuleInst_Backend_Up(i);
  576. return;
  577. fail0:
  578. NCDModuleInst_Backend_DeadError(i);
  579. }
  580. static void read_pipe_func_die (void *vo)
  581. {
  582. struct read_pipe_instance *o = vo;
  583. // free connection resources
  584. if (o->state == READER_STATE_RUNNING) {
  585. read_pipe_free_connection(o);
  586. }
  587. NCDModuleInst_Backend_Dead(o->i);
  588. }
  589. static int read_pipe_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  590. {
  591. struct read_pipe_instance *o = vo;
  592. if (name == NCD_STRING_IS_ERROR) {
  593. int is_error = (o->state == READER_STATE_ERROR);
  594. *out = ncd_make_boolean(mem, is_error, o->i->params->iparams->string_index);
  595. return 1;
  596. }
  597. return 0;
  598. }
  599. static void read_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  600. {
  601. struct read_instance *o = vo;
  602. o->i = i;
  603. if (!NCDVal_ListRead(params->args, 0)) {
  604. ModuleLog(i, BLOG_ERROR, "wrong arity");
  605. goto fail0;
  606. }
  607. struct read_pipe_instance *read_pipe_inst = params->method_user;
  608. // check if a read error has already occured
  609. if (read_pipe_inst->state == READER_STATE_ERROR) {
  610. ModuleLog(i, BLOG_ERROR, "read() is disallowed after a read error has occured");
  611. goto fail0;
  612. }
  613. // check if the read_pipe has been aborted
  614. if (read_pipe_inst->state == READER_STATE_ABORTED) {
  615. ModuleLog(i, BLOG_ERROR, "read() is disallowed after a read() has been aborted");
  616. goto fail0;
  617. }
  618. // if EOF has already been encountered, complete the read immediately
  619. if (read_pipe_inst->state == READER_STATE_EOF) {
  620. o->buf = NULL;
  621. o->read_pipe_inst = NULL;
  622. o->read_size = 0;
  623. NCDModuleInst_Backend_Up(i);
  624. return;
  625. }
  626. ASSERT(read_pipe_inst->state == READER_STATE_RUNNING)
  627. // check if there's already a read in progress
  628. if (read_pipe_inst->read_inst) {
  629. ModuleLog(i, BLOG_ERROR, "read() is disallowed while another read() is in progress");
  630. goto fail0;
  631. }
  632. // get buffer
  633. o->buf = NCDBufStore_GetBuf(&read_pipe_inst->store);
  634. if (!o->buf) {
  635. ModuleLog(i, BLOG_ERROR, "NCDBufStore_GetBuf failed");
  636. goto fail0;
  637. }
  638. // set read_pipe
  639. o->read_pipe_inst = read_pipe_inst;
  640. // register read in read_pipe
  641. read_pipe_inst->read_inst = o;
  642. // receive
  643. size_t buf_size = NCDBufStore_BufSize(&read_pipe_inst->store);
  644. int to_read = (buf_size > INT_MAX ? INT_MAX : buf_size);
  645. StreamRecvInterface_Receiver_Recv(BConnection_RecvAsync_GetIf(&read_pipe_inst->connection), (uint8_t *)NCDBuf_Data(o->buf), to_read);
  646. return;
  647. fail0:
  648. NCDModuleInst_Backend_DeadError(i);
  649. }
  650. static void read_func_die (void *vo)
  651. {
  652. struct read_instance *o = vo;
  653. // if we're receiving, abort read_pipe
  654. if (o->read_pipe_inst) {
  655. ASSERT(o->read_pipe_inst->state == READER_STATE_RUNNING)
  656. ASSERT(o->read_pipe_inst->read_inst == o)
  657. ASSERT(o->buf)
  658. read_pipe_abort(o->read_pipe_inst);
  659. }
  660. // release buffer
  661. if (o->buf) {
  662. NCDRefTarget_Deref(NCDBuf_RefTarget(o->buf));
  663. }
  664. NCDModuleInst_Backend_Dead(o->i);
  665. }
  666. static int read_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  667. {
  668. struct read_instance *o = vo;
  669. ASSERT(!o->read_pipe_inst)
  670. ASSERT(!(o->read_size > 0) || o->buf)
  671. if (name == NCD_STRING_EMPTY) {
  672. if (o->read_size > 0) {
  673. *out = NCDVal_NewExternalString(mem, NCDBuf_Data(o->buf), o->read_size, NCDBuf_RefTarget(o->buf));
  674. } else {
  675. *out = NCDVal_NewIdString(mem, NCD_STRING_EMPTY, o->i->params->iparams->string_index);
  676. }
  677. return 1;
  678. }
  679. if (name == NCD_STRING_NOT_EOF) {
  680. int not_eof = (o->read_size > 0);
  681. *out = ncd_make_boolean(mem, not_eof, o->i->params->iparams->string_index);
  682. return 1;
  683. }
  684. return 0;
  685. }
  686. static void write_pipe_free_connection (struct write_pipe_instance *o)
  687. {
  688. // disconnect write instance
  689. if (o->write_inst) {
  690. ASSERT(o->write_inst->write_pipe_inst == o)
  691. o->write_inst->write_pipe_inst = NULL;
  692. }
  693. // free connection send interface
  694. BConnection_SendAsync_Free(&o->connection);
  695. // free connection
  696. BConnection_Free(&o->connection);
  697. // close fd
  698. if (close(o->write_fd) < 0) {
  699. ModuleLog(o->i, BLOG_ERROR, "close failed");
  700. }
  701. }
  702. static void write_pipe_abort (struct write_pipe_instance *o)
  703. {
  704. ASSERT(o->state == WRITER_STATE_RUNNING)
  705. // release connection resources
  706. write_pipe_free_connection(o);
  707. // set state
  708. o->state = WRITER_STATE_ABORTED;
  709. }
  710. static void write_pipe_close (struct write_pipe_instance *o)
  711. {
  712. ASSERT(o->state == WRITER_STATE_RUNNING)
  713. // release connection resources
  714. write_pipe_free_connection(o);
  715. // set state
  716. o->state = WRITER_STATE_CLOSED;
  717. }
  718. static void write_pipe_connection_handler (void *vo, int event)
  719. {
  720. struct write_pipe_instance *o = vo;
  721. ASSERT(o->state == WRITER_STATE_RUNNING)
  722. ModuleLog(o->i, BLOG_ERROR, "write pipe error");
  723. // free connection resources
  724. write_pipe_free_connection(o);
  725. // set state error
  726. o->state = WRITER_STATE_ERROR;
  727. // backtrack
  728. NCDModuleInst_Backend_DownUp(o->i);
  729. }
  730. static void write_pipe_send_handler_done (void *vo, int data_len)
  731. {
  732. struct write_pipe_instance *o = vo;
  733. ASSERT(o->state == WRITER_STATE_RUNNING)
  734. ASSERT(o->write_inst)
  735. ASSERT(o->write_inst->write_pipe_inst == o)
  736. ASSERT(data_len > 0)
  737. ASSERT(data_len <= o->write_inst->length)
  738. // update write progress
  739. o->write_inst->data += data_len;
  740. o->write_inst->length -= data_len;
  741. // if there is more data, start another write operation
  742. if (o->write_inst->length > 0) {
  743. size_t to_send = (o->write_inst->length > INT_MAX ? INT_MAX : o->write_inst->length);
  744. StreamPassInterface_Sender_Send(BConnection_SendAsync_GetIf(&o->connection), (uint8_t *)o->write_inst->data, to_send);
  745. return;
  746. }
  747. // finish write operation
  748. o->write_inst->write_pipe_inst = NULL;
  749. NCDModuleInst_Backend_Up(o->write_inst->i);
  750. o->write_inst = NULL;
  751. }
  752. static void write_pipe_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  753. {
  754. struct write_pipe_instance *o = vo;
  755. o->i = i;
  756. NCDModuleInst_Backend_PassMemToMethods(i);
  757. if (!NCDVal_ListRead(params->args, 0)) {
  758. ModuleLog(i, BLOG_ERROR, "wrong arity");
  759. goto fail0;
  760. }
  761. struct process_instance *pinst = params->method_user;
  762. if (pinst->write_fd == -1) {
  763. ModuleLog(i, BLOG_ERROR, "process did not start successfully, was not opened for writing or a write_pipe was already created");
  764. goto fail0;
  765. }
  766. // init connection
  767. if (!BConnection_Init(&o->connection, BConnection_source_pipe(pinst->write_fd), i->params->iparams->reactor, o, write_pipe_connection_handler)) {
  768. ModuleLog(i, BLOG_ERROR, "BConnection_Init failed");
  769. goto fail0;
  770. }
  771. // init connection send interface
  772. BConnection_SendAsync_Init(&o->connection);
  773. // set send done callback
  774. StreamPassInterface_Sender_Init(BConnection_SendAsync_GetIf(&o->connection), write_pipe_send_handler_done, o);
  775. // set variables
  776. o->state = WRITER_STATE_RUNNING;
  777. o->write_fd = pinst->write_fd;
  778. o->write_inst = NULL;
  779. // steal write fd from process instance
  780. pinst->write_fd = -1;
  781. // go up
  782. NCDModuleInst_Backend_Up(i);
  783. return;
  784. fail0:
  785. NCDModuleInst_Backend_DeadError(i);
  786. }
  787. static void write_pipe_func_die (void *vo)
  788. {
  789. struct write_pipe_instance *o = vo;
  790. // free connection resources
  791. if (o->state == WRITER_STATE_RUNNING) {
  792. write_pipe_free_connection(o);
  793. }
  794. NCDModuleInst_Backend_Dead(o->i);
  795. }
  796. static int write_pipe_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  797. {
  798. struct write_pipe_instance *o = vo;
  799. if (name == NCD_STRING_IS_ERROR) {
  800. int is_error = (o->state == WRITER_STATE_ERROR);
  801. *out = ncd_make_boolean(mem, is_error, o->i->params->iparams->string_index);
  802. return 1;
  803. }
  804. return 0;
  805. }
  806. static void write_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  807. {
  808. struct write_instance *o = vo;
  809. o->i = i;
  810. NCDValRef data_arg;
  811. if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
  812. ModuleLog(i, BLOG_ERROR, "wrong arity");
  813. goto fail0;
  814. }
  815. if (!NCDVal_IsString(data_arg)) {
  816. ModuleLog(i, BLOG_ERROR, "wrong type");
  817. goto fail0;
  818. }
  819. struct write_pipe_instance *write_pipe_inst = params->method_user;
  820. // check if a write error has already occured
  821. if (write_pipe_inst->state == WRITER_STATE_ERROR) {
  822. ModuleLog(i, BLOG_ERROR, "write() is disallowed after a write error has occured");
  823. goto fail0;
  824. }
  825. // check if the write_pipe has been aborted
  826. if (write_pipe_inst->state == WRITER_STATE_ABORTED) {
  827. ModuleLog(i, BLOG_ERROR, "write() is disallowed after a write() has been aborted");
  828. goto fail0;
  829. }
  830. // check if the write_pipe has been aborted
  831. if (write_pipe_inst->state == WRITER_STATE_CLOSED) {
  832. ModuleLog(i, BLOG_ERROR, "write() is disallowed after close() has been called");
  833. goto fail0;
  834. }
  835. ASSERT(write_pipe_inst->state == WRITER_STATE_RUNNING)
  836. // check if there's already a write in progress
  837. if (write_pipe_inst->write_inst) {
  838. ModuleLog(i, BLOG_ERROR, "write() is disallowed while another write() is in progress");
  839. goto fail0;
  840. }
  841. // initialize write progress state
  842. o->data = NCDVal_StringData(data_arg);
  843. o->length = NCDVal_StringLength(data_arg);
  844. // if there's nothing to send, go up immediately
  845. if (o->length == 0) {
  846. o->write_pipe_inst = NULL;
  847. NCDModuleInst_Backend_Up(i);
  848. return;
  849. }
  850. // set write_pipe
  851. o->write_pipe_inst = write_pipe_inst;
  852. // register write in write_pipe
  853. write_pipe_inst->write_inst = o;
  854. // start send operation
  855. size_t to_send = (o->length > INT_MAX ? INT_MAX : o->length);
  856. StreamPassInterface_Sender_Send(BConnection_SendAsync_GetIf(&write_pipe_inst->connection), (uint8_t *)o->data, to_send);
  857. return;
  858. fail0:
  859. NCDModuleInst_Backend_DeadError(i);
  860. }
  861. static void write_func_die (void *vo)
  862. {
  863. struct write_instance *o = vo;
  864. // if we're sending, abort write_pipe
  865. if (o->write_pipe_inst) {
  866. ASSERT(o->write_pipe_inst->state == WRITER_STATE_RUNNING)
  867. ASSERT(o->write_pipe_inst->write_inst == o)
  868. write_pipe_abort(o->write_pipe_inst);
  869. }
  870. NCDModuleInst_Backend_Dead(o->i);
  871. }
  872. static void close_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  873. {
  874. if (!NCDVal_ListRead(params->args, 0)) {
  875. ModuleLog(i, BLOG_ERROR, "wrong arity");
  876. goto fail0;
  877. }
  878. struct write_pipe_instance *write_pipe_inst = params->method_user;
  879. // check if a write error has already occured
  880. if (write_pipe_inst->state == WRITER_STATE_ERROR) {
  881. ModuleLog(i, BLOG_ERROR, "close() is disallowed after a write error has occured");
  882. goto fail0;
  883. }
  884. // check if the write_pipe has been aborted
  885. if (write_pipe_inst->state == WRITER_STATE_ABORTED) {
  886. ModuleLog(i, BLOG_ERROR, "close() is disallowed after a write() has been aborted");
  887. goto fail0;
  888. }
  889. // check if the write_pipe has been closed
  890. if (write_pipe_inst->state == WRITER_STATE_CLOSED) {
  891. ModuleLog(i, BLOG_ERROR, "close() is disallowed after close() has been called");
  892. goto fail0;
  893. }
  894. // close
  895. write_pipe_close(write_pipe_inst);
  896. // go up
  897. NCDModuleInst_Backend_Up(i);
  898. return;
  899. fail0:
  900. NCDModuleInst_Backend_DeadError(i);
  901. }
  902. static struct NCDModule modules[] = {
  903. {
  904. .type = "sys.start_process",
  905. .func_new2 = process_func_new,
  906. .func_die = process_func_die,
  907. .func_getvar2 = process_func_getvar,
  908. .alloc_size = sizeof(struct process_instance)
  909. }, {
  910. .type = "sys.start_process::wait",
  911. .func_new2 = wait_func_new,
  912. .func_die = wait_func_die,
  913. .func_getvar2 = wait_func_getvar,
  914. .alloc_size = sizeof(struct wait_instance)
  915. }, {
  916. .type = "sys.start_process::terminate",
  917. .func_new2 = terminate_func_new,
  918. }, {
  919. .type = "sys.start_process::kill",
  920. .func_new2 = kill_func_new,
  921. }, {
  922. .type = "sys.start_process::read_pipe",
  923. .func_new2 = read_pipe_func_new,
  924. .func_die = read_pipe_func_die,
  925. .func_getvar2 = read_pipe_func_getvar,
  926. .alloc_size = sizeof(struct read_pipe_instance)
  927. }, {
  928. .type = "sys.start_process::read_pipe::read",
  929. .func_new2 = read_func_new,
  930. .func_die = read_func_die,
  931. .func_getvar2 = read_func_getvar,
  932. .alloc_size = sizeof(struct read_instance)
  933. }, {
  934. .type = "sys.start_process::write_pipe",
  935. .func_new2 = write_pipe_func_new,
  936. .func_die = write_pipe_func_die,
  937. .func_getvar2 = write_pipe_func_getvar,
  938. .alloc_size = sizeof(struct write_pipe_instance)
  939. }, {
  940. .type = "sys.start_process::write_pipe::write",
  941. .func_new2 = write_func_new,
  942. .func_die = write_func_die,
  943. .alloc_size = sizeof(struct write_instance)
  944. }, {
  945. .type = "sys.start_process::write_pipe::close",
  946. .func_new2 = close_func_new
  947. }, {
  948. .type = NULL
  949. }
  950. };
  951. const struct NCDModuleGroup ncdmodule_sys_start_process = {
  952. .modules = modules
  953. };