NCDModule.h 25 KB

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