NCDModule.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /**
  2. * @file NCDModule.h
  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. #ifndef BADVPN_NCD_NCDMODULE_H
  30. #define BADVPN_NCD_NCDMODULE_H
  31. #include <misc/debug.h>
  32. #include <system/BReactor.h>
  33. #include <base/BLog.h>
  34. #include <system/BProcess.h>
  35. #include <udevmonitor/NCDUdevManager.h>
  36. #include <random/BRandom2.h>
  37. #include <ncd/NCDObject.h>
  38. #define NCDMODULE_EVENT_UP 1
  39. #define NCDMODULE_EVENT_DOWN 2
  40. #define NCDMODULE_EVENT_DEAD 3
  41. struct NCDModuleInst_s;
  42. struct NCDModuleProcess_s;
  43. /**
  44. * Function called to inform the interpeter of state changes of the
  45. * module instance.
  46. * Possible events are:
  47. *
  48. * - NCDMODULE_EVENT_UP: the instance came up.
  49. * The instance was in down state.
  50. * The instance enters up state.
  51. *
  52. * - NCDMODULE_EVENT_DOWN: the instance went down.
  53. * The instance was in up state.
  54. * The instance enters down state.
  55. *
  56. * After the instance goes down, the interpreter should eventually call
  57. * {@link NCDModuleInst_Clean} or {@link NCDModuleInst_Die}, unless
  58. * the module goes up again.
  59. *
  60. * - NCDMODULE_EVENT_DEAD: the module died.
  61. * The instance enters dead state.
  62. *
  63. * This function is not being called in event context. The interpreter should
  64. * only update its internal state, and visibly react only via jobs that it pushes
  65. * from within this function. The only exception is that it may free the
  66. * instance from within the NCDMODULE_EVENT_DEAD event.
  67. *
  68. * @param user as in {@link NCDModuleInst_Init}
  69. * @param event event number
  70. */
  71. typedef void (*NCDModuleInst_func_event) (void *user, int event);
  72. /**
  73. * Function called when the module instance wants the interpreter to
  74. * resolve an object from the point of view of its statement.
  75. * The instance will not be in dead state.
  76. * This function must not have any side effects.
  77. *
  78. * @param user as in {@link NCDModuleInst_Init}
  79. * @param name name of the object
  80. * @param out_object the object will be returned here
  81. * @return 1 on success, 0 on failure
  82. */
  83. typedef int (*NCDModuleInst_func_getobj) (void *user, const char *name, NCDObject *out_object);
  84. /**
  85. * Function called when the module instance wants the interpreter to
  86. * create a new process backend from a process template.
  87. * The instance will not be in dead state.
  88. *
  89. * On success, the interpreter must have called {@link NCDModuleProcess_Interp_SetHandlers}
  90. * from within this function, to allow communication with the controller of the process.
  91. * On success, the new process backend enters down state.
  92. *
  93. * This function is not being called in event context. The interpreter should
  94. * only update its internal state, and visibly react only via jobs that it pushes
  95. * from within this function.
  96. *
  97. * @param user as in {@link NCDModuleInst_Init}
  98. * @param p handle for the new process backend
  99. * @param template_name name of the template to create the process from
  100. * @return 1 on success, 0 on failure
  101. */
  102. typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, const char *template_name);
  103. /**
  104. * Function called when the module instance wants the interpreter to
  105. * initiate termination, as if it received an external terminatio request (signal).
  106. *
  107. * @param user as in {@link NCDModuleInst_Init}
  108. * @param exit_code exit code to return the the operating system. This overrides any previously
  109. * set exit code, and will be overriden by a signal to the value 1.
  110. *
  111. */
  112. typedef void (*NCDModuleInst_func_interp_exit) (void *user, int exit_code);
  113. /**
  114. * Function called when the module instance wants the interpreter to
  115. * provide its extra command line arguments.
  116. *
  117. * @param user as in {@link NCDModuleInst_Init}
  118. * @param mem value memory to use
  119. * @param out_value write value reference here on success
  120. * @return 1 if available, 0 if not available. If available, but out of memory, return 1
  121. * and an invalid value.
  122. */
  123. typedef int (*NCDModuleInst_func_interp_getargs) (void *user, NCDValMem *mem, NCDValRef *out_value);
  124. /**
  125. * Function called when the module instance wants the interpreter to
  126. * provide its retry time.
  127. *
  128. * @param user as in {@link NCDModuleInst_Init}
  129. * @return retry time in milliseconds
  130. */
  131. typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void *user);
  132. #define NCDMODULEPROCESS_EVENT_UP 1
  133. #define NCDMODULEPROCESS_EVENT_DOWN 2
  134. #define NCDMODULEPROCESS_EVENT_TERMINATED 3
  135. /**
  136. * Handler which reports process state changes from the interpreter.
  137. * Possible events are:
  138. *
  139. * - NCDMODULEPROCESS_EVENT_UP: the process went up.
  140. * The process was in down state.
  141. * The process enters up state.
  142. *
  143. * - NCDMODULEPROCESS_EVENT_DOWN: the process went down.
  144. * The process was in up state.
  145. * The process enters waiting state.
  146. *
  147. * NOTE: the process enters waiting state, NOT down state, and is paused.
  148. * To allow the process to continue, call {@link NCDModuleProcess_Continue}.
  149. *
  150. * - NCDMODULEPROCESS_EVENT_TERMINATED: the process terminated.
  151. * The process was in terminating state.
  152. * The process enters terminated state.
  153. *
  154. * @param user as in {@link NCDModuleProcess_Init}
  155. * @param event event number
  156. */
  157. typedef void (*NCDModuleProcess_handler_event) (void *user, int event);
  158. /**
  159. * Function called when the interpreter wants to resolve a special
  160. * object in the process.
  161. * This function must have no side effects.
  162. *
  163. * @param user as in {@link NCDModuleProcess_Init}
  164. * @param name name of the object
  165. * @param out_object the object will be returned here
  166. * @return 1 on success, 0 on failure
  167. */
  168. typedef int (*NCDModuleProcess_func_getspecialobj) (void *user, const char *name, NCDObject *out_object);
  169. #define NCDMODULEPROCESS_INTERP_EVENT_CONTINUE 1
  170. #define NCDMODULEPROCESS_INTERP_EVENT_TERMINATE 2
  171. /**
  172. * Function called to report process backend requests to the interpreter.
  173. * Possible events are:
  174. *
  175. * - NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: the process can continue.
  176. * The process backend was in waiting state.
  177. * The process backend enters down state.
  178. *
  179. * - NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: the process should terminate.
  180. * The process backend was in down, up or waiting state.
  181. * The process backend enters terminating state.
  182. *
  183. * The interpreter should call {@link NCDModuleProcess_Interp_Terminated}
  184. * when the process terminates.
  185. *
  186. * This function is not being called in event context. The interpreter should
  187. * only update its internal state, and visibly react only via jobs that it pushes
  188. * from within this function.
  189. *
  190. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  191. * @param event event number
  192. */
  193. typedef void (*NCDModuleProcess_interp_func_event) (void *user, int event);
  194. /**
  195. * Function called to have the interpreter resolve an object within the process
  196. * of a process backend.
  197. * This function must not have any side effects.
  198. *
  199. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  200. * @param name name of the object
  201. * @param out_object the object will be returned here
  202. * @return 1 on success, 0 in failure
  203. */
  204. typedef int (*NCDModuleProcess_interp_func_getobj) (void *user, const char *name, NCDObject *out_object);
  205. struct NCDModule;
  206. struct NCDModuleInitParams {
  207. BReactor *reactor;
  208. BProcessManager *manager;
  209. NCDUdevManager *umanager;
  210. BRandom2 *random2;
  211. };
  212. /**
  213. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  214. * This only contains parameters related to communication between the backend
  215. * and the creator of the module instance.
  216. */
  217. struct NCDModuleInst_params {
  218. /**
  219. * Callback to report state changes.
  220. */
  221. NCDModuleInst_func_event func_event;
  222. /**
  223. * Callback to resolve objects from the viewpoint of the instance.
  224. */
  225. NCDModuleInst_func_getobj func_getobj;
  226. /**
  227. * Log function which appends a log prefix with {@link BLog_Append}.
  228. */
  229. BLog_logfunc logfunc;
  230. };
  231. /**
  232. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  233. * This only contains parameters related to services provided by the interpreter.
  234. */
  235. struct NCDModuleInst_iparams {
  236. /**
  237. * Reactor we live in.
  238. */
  239. BReactor *reactor;
  240. /**
  241. * Process manager.
  242. */
  243. BProcessManager *manager;
  244. /**
  245. * Udev manager.
  246. */
  247. NCDUdevManager *umanager;
  248. /**
  249. * Random number generator.
  250. */
  251. BRandom2 *random2;
  252. /**
  253. * Callback to create a new template process.
  254. */
  255. NCDModuleInst_func_initprocess func_initprocess;
  256. /**
  257. * Callback to request interpreter termination.
  258. */
  259. NCDModuleInst_func_interp_exit func_interp_exit;
  260. /**
  261. * Callback to get extra command line arguments.
  262. */
  263. NCDModuleInst_func_interp_getargs func_interp_getargs;
  264. /**
  265. * Callback to get retry time.
  266. */
  267. NCDModuleInst_func_interp_getretrytime func_interp_getretrytime;
  268. };
  269. /**
  270. * Module instance.
  271. * The module instance is initialized by the interpreter by calling
  272. * {@link NCDModuleInst_Init}. It is implemented by a module backend
  273. * specified in a {@link NCDModule}.
  274. */
  275. typedef struct NCDModuleInst_s {
  276. const struct NCDModule *m;
  277. void *method_user;
  278. NCDValRef args;
  279. void *user;
  280. const struct NCDModuleInst_params *params;
  281. const struct NCDModuleInst_iparams *iparams;
  282. void *inst_user;
  283. int state;
  284. int is_error;
  285. DebugObject d_obj;
  286. } NCDModuleInst;
  287. /**
  288. * Process created from a process template on behalf of a module backend
  289. * instance, implemented by the interpreter.
  290. */
  291. typedef struct NCDModuleProcess_s {
  292. NCDValRef args;
  293. void *user;
  294. NCDModuleProcess_handler_event handler_event;
  295. NCDModuleProcess_func_getspecialobj func_getspecialobj;
  296. int state;
  297. void *interp_user;
  298. NCDModuleProcess_interp_func_event interp_func_event;
  299. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  300. DebugObject d_obj;
  301. } NCDModuleProcess;
  302. /**
  303. * Initializes an instance of an NCD module.
  304. * The instance is initialized in down state.
  305. * WARNING: this directly calls the module backend; expect to be called back
  306. *
  307. * This and other non-Backend methods are the interpreter interface.
  308. * The Backend methods are the module backend interface and are documented
  309. * independently with their own logical states.
  310. *
  311. * @param n the instance
  312. * @param m structure of module functions implementing the module backend
  313. * @param mem preallocated memory for the instance. If m->prealloc_size == 0, this must be NULL;
  314. * if it is >0, it must point to a block of memory with at least that many bytes available,
  315. * and properly aligned for any object.
  316. * @param method_object the base object if the module being initialized is a method, otherwise NULL.
  317. * The caller must ensure that this object is of the type expected by the module
  318. * being initialized.
  319. * @param args arguments to the module. Must be a list value. Must be available and unchanged
  320. * as long as the instance exists.
  321. * @param user argument to callback functions
  322. * @param params more parameters, see {@link NCDModuleInst_params}
  323. * @param iparams more parameters, see {@link NCDModuleInst_iparams}
  324. */
  325. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem, const NCDObject *method_object, NCDValRef args, void *user, const struct NCDModuleInst_params *params, const struct NCDModuleInst_iparams *iparams);
  326. /**
  327. * Frees the instance.
  328. * The instance must be in dead state.
  329. *
  330. * @param n the instance
  331. */
  332. void NCDModuleInst_Free (NCDModuleInst *n);
  333. /**
  334. * Requests the instance to die.
  335. * The instance must be in down or up state.
  336. * The instance enters dying state.
  337. * WARNING: this directly calls the module backend; expect to be called back
  338. *
  339. * @param n the instance
  340. */
  341. void NCDModuleInst_Die (NCDModuleInst *n);
  342. /**
  343. * Informs the module that it is in a clean state to proceed.
  344. * The instance must be in down state.
  345. * WARNING: this directly calls the module backend; expect to be called back
  346. *
  347. * @param n the instance
  348. */
  349. void NCDModuleInst_Clean (NCDModuleInst *n);
  350. /**
  351. * Returns an {@link NCDObject} which can be used to resolve variables and objects
  352. * within this instance, as well as call its methods. The resulting object may only
  353. * be used immediately, and becomes invalid when the instance is freed.
  354. *
  355. * @param n the instance
  356. * @return an NCDObject for this instance
  357. */
  358. NCDObject NCDModuleInst_Object (NCDModuleInst *n);
  359. /**
  360. * Checks whether the module terminated unsuccessfully.
  361. * The instance must be in dead state.
  362. *
  363. * @param n the instance
  364. * @return 1 if module terminated unsuccessfully, 0 if successfully
  365. */
  366. int NCDModuleInst_HaveError (NCDModuleInst *n);
  367. /**
  368. * Sets the argument passed to handlers of a module backend instance.
  369. * If the alloc_size member in the {@link NCDModule} structure is >0,
  370. * the argument will automatically be set to the preallocated memory,
  371. * in which case there is no need to call this.
  372. *
  373. * @param n backend instance handle
  374. * @param user value to pass to future handlers for this backend instance
  375. */
  376. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  377. /**
  378. * Retuns the argument passed to handlers of a module backend instance;
  379. * see {@link NCDModuleInst_Backend_SetUser}.
  380. *
  381. * @param n backend instance handle
  382. * @return argument passed to handlers
  383. */
  384. void * NCDModuleInst_Backend_GetUser (NCDModuleInst *n);
  385. /**
  386. * Puts the backend instance into up state.
  387. * The instance must be in down state.
  388. * The instance enters up state.
  389. *
  390. * @param n backend instance handle
  391. */
  392. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  393. /**
  394. * Puts the backend instance into down state.
  395. * The instance must be in up state.
  396. * The instance enters down state.
  397. *
  398. * @param n backend instance handle
  399. */
  400. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  401. /**
  402. * Destroys the backend instance.
  403. * The backend instance handle becomes invalid and must not be used from
  404. * the backend any longer.
  405. *
  406. * @param n backend instance handle
  407. */
  408. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  409. /**
  410. * Resolves an object for a backend instance, from the point of the instance's
  411. * statement in the containing process.
  412. *
  413. * @param n backend instance handle
  414. * @param name name of the object to resolve
  415. * @param out_object the object will be returned here
  416. * @return 1 on success, 0 on failure
  417. */
  418. int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *name, NCDObject *out_object) WARN_UNUSED;
  419. /**
  420. * Logs a backend instance message.
  421. *
  422. * @param n backend instance handle
  423. * @param channel log channel
  424. * @param level loglevel
  425. * @param fmt format string as in printf, arguments follow
  426. */
  427. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  428. /**
  429. * Sets the error flag for the module instance.
  430. * The error flag only has no effect until the backend calls
  431. * {@link NCDModuleInst_Backend_Dead}.
  432. *
  433. * @param n backend instance handle
  434. */
  435. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  436. /**
  437. * Initiates interpreter termination.
  438. *
  439. * @param n backend instance handle
  440. * @param exit_code exit code to return to the operating system. This overrides
  441. * any previously set exit code, and will be overriden by a
  442. * termination signal to the value 1.
  443. */
  444. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  445. /**
  446. * Retrieves extra command line arguments passed to the interpreter.
  447. *
  448. * @param n backend instance handle
  449. * @param mem value memory to use
  450. * @param out_value the arguments will be written here on success as a list value
  451. * @return 1 if available, 0 if not available. If available, but out of memory, returns 1
  452. * and an invalid value.
  453. */
  454. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value);
  455. /**
  456. * Returns the retry time of the intepreter.
  457. *
  458. * @param n backend instance handle
  459. * @return retry time in milliseconds
  460. */
  461. btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  462. /**
  463. * Initializes a process in the interpreter from a process template.
  464. * This must be called on behalf of a module backend instance.
  465. * The process is initializes in down state.
  466. *
  467. * @param o the process
  468. * @param n backend instance whose interpreter will be providing the process
  469. * @param template_name name of the process template
  470. * @param args arguments to the process. Must be an invalid value or a list value.
  471. * The value must be available and unchanged while the process exists.
  472. * @param user argument to handlers
  473. * @param handler_event handler which reports events about the process from the
  474. * interpreter
  475. * @return 1 on success, 0 on failure
  476. */
  477. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  478. /**
  479. * Frees the process.
  480. * The process must be in terminated state.
  481. *
  482. * @param o the process
  483. */
  484. void NCDModuleProcess_Free (NCDModuleProcess *o);
  485. /**
  486. * Does nothing.
  487. * The process must be in terminated state.
  488. *
  489. * @param o the process
  490. */
  491. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  492. /**
  493. * Sets callback functions for providing special objects within the process.
  494. *
  495. * @param o the process
  496. * @param func_getspecialobj function for resolving special objects, or NULL
  497. */
  498. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  499. /**
  500. * Continues the process after the process went down.
  501. * The process must be in waiting state.
  502. * The process enters down state.
  503. *
  504. * @param o the process
  505. */
  506. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  507. /**
  508. * Requests the process to terminate.
  509. * The process must be in down, up or waiting state.
  510. * The process enters terminating state.
  511. *
  512. * @param o the process
  513. */
  514. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  515. /**
  516. * Resolves an object within the process from the point
  517. * at the end of the process.
  518. * This function has no side effects.
  519. *
  520. * @param o the process
  521. * @param name name of the object to resolve
  522. * @param out_object the object will be returned here
  523. * @return 1 on success, 0 on failure
  524. */
  525. int NCDModuleProcess_GetObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  526. /**
  527. * Sets callback functions for the interpreter to implement the
  528. * process backend.
  529. * Must be called from within {@link NCDModuleInst_func_initprocess}
  530. * if success is to be reported there.
  531. *
  532. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  533. * @param interp_user argument to callback functions
  534. * @param interp_func_event function for reporting continue/terminate requests
  535. * @param interp_func_getobj function for resolving objects within the process
  536. */
  537. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  538. NCDModuleProcess_interp_func_event interp_func_event,
  539. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  540. /**
  541. * Reports the process backend as up.
  542. * The process backend must be in down state.
  543. * The process backend enters up state.
  544. * WARNING: this directly calls the process creator; expect to be called back
  545. *
  546. * @param o process backend handle
  547. */
  548. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  549. /**
  550. * Reports the process backend as down.
  551. * The process backend must be in up state.
  552. * The process backend enters waiting state.
  553. * WARNING: this directly calls the process creator; expect to be called back
  554. *
  555. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  556. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  557. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  558. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  559. *
  560. * @param o process backend handle
  561. */
  562. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  563. /**
  564. * Reports termination of the process backend.
  565. * The process backend must be in terminating state.
  566. * The process backend handle becomes invalid and must not be used
  567. * by the interpreter any longer.
  568. * WARNING: this directly calls the process creator; expect to be called back
  569. *
  570. * @param o process backend handle
  571. */
  572. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  573. /**
  574. * Resolves a special process object for the process backend.
  575. *
  576. * @param o process backend handle
  577. * @param name name of the object
  578. * @param out_object the object will be returned here
  579. * @return 1 on success, 0 on failure
  580. */
  581. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  582. /**
  583. * Function called before any instance of any backend in a module
  584. * group is created;
  585. *
  586. * @param params structure containing global resources, in particular
  587. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}
  588. * @return 1 on success, 0 on failure
  589. */
  590. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
  591. /**
  592. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  593. * in a module group.
  594. * There are no backend instances alive from this module group.
  595. */
  596. typedef void (*NCDModule_func_globalfree) (void);
  597. /**
  598. * Handler called to create an new module backend instance.
  599. * The backend is initialized in down state.
  600. *
  601. * This handler should call {@link NCDModuleInst_Backend_SetUser} to provide
  602. * an argument for handlers of this backend instance if it needs to keep any
  603. * kind of state. Alternatively, the module can have the interpreter preallocate
  604. * a predefined amount of memory for each instance, in which case the
  605. * {@link NCDModule_func_new2} init function should be used instead of this one
  606. * (see alloc_size in the {@link NCDModule} structure).
  607. *
  608. * If the backend fails initialization, this function should report the backend
  609. * instance to have died with error by calling {@link NCDModuleInst_Backend_SetError}
  610. * and {@link NCDModuleInst_Backend_Dead}.
  611. *
  612. * @param i module backend instance handler. The backend may only use this handle via
  613. * the Backend functions of {@link NCDModuleInst}.
  614. */
  615. typedef void (*NCDModule_func_new) (NCDModuleInst *i);
  616. /**
  617. * Like {@link NCDModule_func_new}, but with the extra user argument, as in other module
  618. * instance handlers. The initial value of the argument, as used here, is a pointer to
  619. * preallocated memory of alloc_size bytes (from {@link NCDModule}), or NULL if alloc_size==0.
  620. */
  621. typedef void (*NCDModule_func_new2) (void *o, NCDModuleInst *i);
  622. /**
  623. * Handler called to request termination of a backend instance.
  624. * The backend instance was in down or up state.
  625. * The backend instance enters dying state.
  626. *
  627. * @param o see {@link NCDModuleInst_Backend_SetUser}
  628. */
  629. typedef void (*NCDModule_func_die) (void *o);
  630. /**
  631. * Function called to resolve a variable within a backend instance.
  632. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  633. * This function must not have any side effects.
  634. *
  635. * @param o see {@link NCDModuleInst_Backend_SetUser}
  636. * @param name name of the variable to resolve
  637. * @param mem value memory to use
  638. * @param out on success, the backend should initialize the value here
  639. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  640. * and an invalid value.
  641. */
  642. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValMem *mem, NCDValRef *out);
  643. /**
  644. * Function called to resolve an object within a backend instance.
  645. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  646. * This function must not have any side effects.
  647. *
  648. * @param o see {@link NCDModuleInst_Backend_SetUser}
  649. * @param name name of the object to resolve
  650. * @param out_object the object will be returned here
  651. * @return 1 on success, 0 on failure
  652. */
  653. typedef int (*NCDModule_func_getobj) (void *o, const char *name, NCDObject *out_object);
  654. /**
  655. * Handler called when the module instance is in a clean state.
  656. * This means that all statements preceding it in the process are
  657. * up, this statement is down, and all following statements are
  658. * uninitialized. When a backend instance goes down, it is guaranteed,
  659. * as long as it stays down, that either this will be called or
  660. * termination will be requested with {@link NCDModule_func_die}.
  661. * The backend instance was in down state.
  662. *
  663. * @param o see {@link NCDModuleInst_Backend_SetUser}
  664. */
  665. typedef void (*NCDModule_func_clean) (void *o);
  666. #define NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN (1 << 0)
  667. /**
  668. * Structure encapsulating the implementation of a module backend.
  669. */
  670. struct NCDModule {
  671. /**
  672. * If this implements a plain statement, the name of the statement.
  673. * If this implements a method, then "base_type::method_name".
  674. */
  675. const char *type;
  676. /**
  677. * The base type for methods operating on instances of this backend.
  678. * Any module with type of form "base_type::method_name" is considered
  679. * a method of instances of this backend.
  680. * If this is NULL, the base type will default to type.
  681. */
  682. const char *base_type;
  683. /**
  684. * Function called to create an new backend instance.
  685. * Only one of the two possible init functions must be set.
  686. */
  687. NCDModule_func_new func_new;
  688. /**
  689. * Function called to create an new backend instance, to be used with memory
  690. * preallocation.
  691. * Only one of the two possible init functions must be set.
  692. */
  693. NCDModule_func_new2 func_new2;
  694. /**
  695. * Function called to request termination of a backend instance.
  696. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  697. */
  698. NCDModule_func_die func_die;
  699. /**
  700. * Function called to resolve a variable within the backend instance.
  701. * May be NULL.
  702. */
  703. NCDModule_func_getvar func_getvar;
  704. /**
  705. * Function called to resolve an object within the backend instance.
  706. * May be NULL.
  707. */
  708. NCDModule_func_getobj func_getobj;
  709. /**
  710. * Function called when the backend instance is in a clean state.
  711. * May be NULL.
  712. */
  713. NCDModule_func_clean func_clean;
  714. /**
  715. * Various flags.
  716. *
  717. * - NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  718. * Whether the interpreter is allowed to call func_getvar and func_getobj
  719. * even when the backend instance is in down state (as opposed to just
  720. * in up state.
  721. */
  722. int flags;
  723. /**
  724. * The amount of memory to preallocate for each module instance.
  725. * Preallocation can be used to avoid having to allocate memory from
  726. * module initialization. To make use of preallocated memory, use the
  727. * {@link NCDModule_func_new2} variant of the init function.
  728. * The memory will be available from the point the init function is
  729. * called to the point the instance calls {@link NCDModuleInst_Backend_Dead}.
  730. * If alloc_size is >0, there is no need to call
  731. * {@link NCDModuleInst_Backend_SetUser}, as the the user argument will
  732. * automatically be set to a pointer to the preallocated memory.
  733. */
  734. int alloc_size;
  735. };
  736. /**
  737. * Structure encapsulating a group of module backend implementations,
  738. * with global init and free functions.
  739. */
  740. struct NCDModuleGroup {
  741. /**
  742. * Function called before any instance of any module backend in this
  743. * group is crated. May be NULL.
  744. */
  745. NCDModule_func_globalinit func_globalinit;
  746. /**
  747. * Function called to clean up after {@link NCDModule_func_globalinit}.
  748. * May be NULL.
  749. */
  750. NCDModule_func_globalfree func_globalfree;
  751. /**
  752. * Array of module backends. The array must be terminated with a
  753. * structure that has a NULL type member.
  754. */
  755. const struct NCDModule *modules;
  756. };
  757. #endif