NCDModule.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. NCDModuleInst *n;
  272. NCDValue args;
  273. void *user;
  274. NCDModuleProcess_handler_event handler_event;
  275. NCDModuleProcess_func_getspecialobj func_getspecialobj;
  276. BPending event_job;
  277. int state;
  278. void *interp_user;
  279. NCDModuleProcess_interp_func_event interp_func_event;
  280. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  281. DebugObject d_obj;
  282. } NCDModuleProcess;
  283. /**
  284. * Initializes an instance of an NCD module.
  285. * The instance is initialized in down state.
  286. *
  287. * This and other non-Backend methods are the interpreter interface.
  288. * The Backend methods are the module backend interface and are documented
  289. * independently with their own logical states.
  290. *
  291. * @param n the instance
  292. * @param m structure of module functions implementing the module backend
  293. * @param method_object the base object if the module being initialized is a method, otherwise NULL.
  294. * The caller must ensure that this object is of the type expected by the module
  295. * being initialized.
  296. * @param args arguments to the module. Must be a NCDVALUE_LIST value. Must be available as long as
  297. * the instance is freed.
  298. * @param user argument to callback functions
  299. * @param params remaining parameters, see {@link NCDModuleInst_params}. These are passed indirectly
  300. * because they are usually always the same, to reduce memory usage, and the number of
  301. * arguments to this function.
  302. */
  303. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, const NCDObject *method_object, NCDValue *args, void *user, const struct NCDModuleInst_params *params);
  304. /**
  305. * Frees the instance.
  306. * The instance must be in dead state.
  307. *
  308. * @param n the instance
  309. */
  310. void NCDModuleInst_Free (NCDModuleInst *n);
  311. /**
  312. * Requests the instance to die.
  313. * The instance must be in down or up state.
  314. * The instance enters dying state.
  315. *
  316. * @param n the instance
  317. */
  318. void NCDModuleInst_Die (NCDModuleInst *n);
  319. /**
  320. * Informs the module that it is in a clean state to proceed.
  321. * The instance must be in down state.
  322. *
  323. * @param n the instance
  324. */
  325. void NCDModuleInst_Clean (NCDModuleInst *n);
  326. /**
  327. * Returns an {@link NCDObject} which can be used to resolve variables and objects
  328. * within this instance, as well as call its methods. The resulting object may only
  329. * be used immediately, and becomes invalid when the instance is freed.
  330. *
  331. * @param n the instance
  332. * @return an NCDObject for this instance
  333. */
  334. NCDObject NCDModuleInst_Object (NCDModuleInst *n);
  335. /**
  336. * Checks whether the module terminated unsuccessfully.
  337. * The instance must be in dead state.
  338. *
  339. * @param n the instance
  340. * @return 1 if module terminated unsuccessfully, 0 if successfully
  341. */
  342. int NCDModuleInst_HaveError (NCDModuleInst *n);
  343. /**
  344. * Sets the argument passed to handlers of a module backend instance.
  345. *
  346. * @param n backend instance handle
  347. * @param user value to pass to future handlers for this backend instance
  348. */
  349. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  350. /**
  351. * Puts the backend instance into up state.
  352. * The instance must be in down state.
  353. * The instance enters up state.
  354. *
  355. * @param n backend instance handle
  356. */
  357. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  358. /**
  359. * Puts the backend instance into down state.
  360. * The instance must be in up state.
  361. * The instance enters down state.
  362. *
  363. * @param n backend instance handle
  364. */
  365. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  366. /**
  367. * Destroys the backend instance.
  368. * The backend instance handle becomes invalid and must not be used from
  369. * the backend any longer.
  370. *
  371. * @param n backend instance handle
  372. */
  373. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  374. /**
  375. * Resolves an object for a backend instance, from the point of the instance's
  376. * statement in the containing process.
  377. *
  378. * @param n backend instance handle
  379. * @param name name of the object to resolve
  380. * @param out_object the object will be returned here
  381. * @return 1 on success, 0 on failure
  382. */
  383. int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *name, NCDObject *out_object) WARN_UNUSED;
  384. /**
  385. * Logs a backend instance message.
  386. *
  387. * @param n backend instance handle
  388. * @param channel log channel
  389. * @param level loglevel
  390. * @param fmt format string as in printf, arguments follow
  391. */
  392. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  393. /**
  394. * Sets the error flag for the module instance.
  395. * The error flag only has no effect until the backend calls
  396. * {@link NCDModuleInst_Backend_Dead}.
  397. *
  398. * @param n backend instance handle
  399. */
  400. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  401. /**
  402. * Initiates interpreter termination.
  403. *
  404. * @param n backend instance handle
  405. * @param exit_code exit code to return to the operating system. This overrides
  406. * any previously set exit code, and will be overriden by a
  407. * termination signal to the value 1.
  408. */
  409. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  410. /**
  411. * Retrieves extra command line arguments passed to the interpreter.
  412. *
  413. * @param n backend instance handle
  414. * @param out_value the arguments will be written here on success as a list value
  415. * @return 1 on success, 0 on failure
  416. */
  417. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValue *out_value);
  418. /**
  419. * Initializes a process in the interpreter from a process template.
  420. * This must be called on behalf of a module backend instance.
  421. * The process is initializes in down state.
  422. *
  423. * @param o the process
  424. * @param n backend instance whose interpreter will be providing the process
  425. * @param template_name name of the process template
  426. * @param args arguments to the process. On success, the arguments become owned
  427. * by the interpreter. On failure, they are left untouched.
  428. * @param user argument to handlers
  429. * @param handler_event handler which reports events about the process from the
  430. * interpreter
  431. * @return 1 on success, 0 on failure
  432. */
  433. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  434. /**
  435. * Frees the process.
  436. * The process must be in terminated state.
  437. *
  438. * @param o the process
  439. */
  440. void NCDModuleProcess_Free (NCDModuleProcess *o);
  441. /**
  442. * Does nothing.
  443. * The process must be in terminated state.
  444. *
  445. * @param o the process
  446. */
  447. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  448. /**
  449. * Sets callback functions for providing special objects within the process.
  450. *
  451. * @param o the process
  452. * @param func_getspecialobj function for resolving special objects, or NULL
  453. */
  454. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  455. /**
  456. * Continues the process after the process went down.
  457. * The process must be in waiting state.
  458. * The process enters down state.
  459. *
  460. * @param o the process
  461. */
  462. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  463. /**
  464. * Requests the process to terminate.
  465. * The process must be in down, up or waiting state.
  466. * The process enters terminating state.
  467. *
  468. * @param o the process
  469. */
  470. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  471. /**
  472. * Resolves an object within the process from the point
  473. * at the end of the process.
  474. * This function has no side effects.
  475. *
  476. * @param o the process
  477. * @param name name of the object to resolve
  478. * @param out_object the object will be returned here
  479. * @return 1 on success, 0 on failure
  480. */
  481. int NCDModuleProcess_GetObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  482. /**
  483. * Sets callback functions for the interpreter to implement the
  484. * process backend.
  485. * Must be called from within {@link NCDModuleInst_func_initprocess}
  486. * if success is to be reported there.
  487. *
  488. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  489. * @param interp_user argument to callback functions
  490. * @param interp_func_event function for reporting continue/terminate requests
  491. * @param interp_func_getobj function for resolving objects within the process
  492. */
  493. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  494. NCDModuleProcess_interp_func_event interp_func_event,
  495. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  496. /**
  497. * Reports the process backend as up.
  498. * The process backend must be in down state.
  499. * The process backend enters up state.
  500. *
  501. * @param o process backend handle
  502. */
  503. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  504. /**
  505. * Reports the process backend as down.
  506. * The process backend must be in up state.
  507. * The process backend enters waiting state.
  508. *
  509. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  510. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  511. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  512. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  513. *
  514. * @param o process backend handle
  515. */
  516. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  517. /**
  518. * Reports termination of the process backend.
  519. * The process backend must be in terminating state.
  520. * The process backend handle becomes invalid and must not be used
  521. * by the interpreter any longer.
  522. *
  523. * @param o process backend handle
  524. */
  525. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  526. /**
  527. * Resolves a special process object for the process backend.
  528. *
  529. * @param o process backend handle
  530. * @param name name of the object
  531. * @param out_object the object will be returned here
  532. * @return 1 on success, 0 on failure
  533. */
  534. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  535. /**
  536. * Function called before any instance of any backend in a module
  537. * group is created;
  538. *
  539. * @param params structure containing global resources, in particular
  540. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}
  541. * @return 1 on success, 0 on failure
  542. */
  543. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
  544. /**
  545. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  546. * in a module group.
  547. * There are no backend instances alive from this module group.
  548. */
  549. typedef void (*NCDModule_func_globalfree) (void);
  550. /**
  551. * Handler called to create an new module backend instance.
  552. * The backend is initialized in down state.
  553. *
  554. * This handler should call {@link NCDModuleInst_Backend_SetUser} to provide
  555. * an argument for handlers of this backend instance.
  556. *
  557. * If the backend fails initialization, this function should report the backend
  558. * instance to have died with error by calling {@link NCDModuleInst_Backend_SetError}
  559. * and {@link NCDModuleInst_Backend_Dead}.
  560. *
  561. * @param i module backend instance handler. The backend may only use this handle via
  562. * the Backend functions of {@link NCDModuleInst}.
  563. */
  564. typedef void (*NCDModule_func_new) (NCDModuleInst *i);
  565. /**
  566. * Handler called to request termination of a backend instance.
  567. * The backend instance was in down or up state.
  568. * The backend instance enters dying state.
  569. *
  570. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  571. */
  572. typedef void (*NCDModule_func_die) (void *o);
  573. /**
  574. * Function called to resolve a variable within a backend instance.
  575. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  576. * This function must not have any side effects.
  577. *
  578. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  579. * @param name name of the variable to resolve
  580. * @param out on success, the backend should initialize the value here
  581. * @return 1 on success, 0 on failure
  582. */
  583. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValue *out);
  584. /**
  585. * Function called to resolve an object within a backend instance.
  586. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  587. * This function must not have any side effects.
  588. *
  589. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  590. * @param name name of the object to resolve
  591. * @param out_object the object will be returned here
  592. * @return 1 on success, 0 on failure
  593. */
  594. typedef int (*NCDModule_func_getobj) (void *o, const char *name, NCDObject *out_object);
  595. /**
  596. * Handler called when the module instance is in a clean state.
  597. * This means that all statements preceding it in the process are
  598. * up, this statement is down, and all following statements are
  599. * uninitialized. When a backend instance goes down, it is guaranteed,
  600. * as long as it stays down, that either this will be called or
  601. * termination will be requested with {@link NCDModule_func_die}.
  602. * The backend instance was in down state.
  603. *
  604. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  605. */
  606. typedef void (*NCDModule_func_clean) (void *o);
  607. /**
  608. * Structure encapsulating the implementation of a module backend.
  609. */
  610. struct NCDModule {
  611. /**
  612. * If this implements a plain statement, the name of the statement.
  613. * If this implements a method, then "base_type::method_name".
  614. */
  615. const char *type;
  616. /**
  617. * The base type for methods operating on instances of this backend.
  618. * Any module with type of form "base_type::method_name" is considered
  619. * a method of instances of this backend.
  620. * If this is NULL, the base type will default to type.
  621. */
  622. const char *base_type;
  623. /**
  624. * Function called to create an new backend instance.
  625. */
  626. NCDModule_func_new func_new;
  627. /**
  628. * Function called to request termination of a backend instance.
  629. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  630. */
  631. NCDModule_func_die func_die;
  632. /**
  633. * Function called to resolve a variable within the backend instance.
  634. * May be NULL.
  635. */
  636. NCDModule_func_getvar func_getvar;
  637. /**
  638. * Function called to resolve an object within the backend instance.
  639. * May be NULL.
  640. */
  641. NCDModule_func_getobj func_getobj;
  642. /**
  643. * Function called when the backend instance is in a clean state.
  644. * May be NULL.
  645. */
  646. NCDModule_func_clean func_clean;
  647. /**
  648. * Whether the interpreter is allowed to call func_getvar and func_getobj
  649. * even when the backend instance is in down state (as opposed to just
  650. * in up state).
  651. */
  652. int can_resolve_when_down;
  653. };
  654. /**
  655. * Structure encapsulating a group of module backend implementations,
  656. * with global init and free functions.
  657. */
  658. struct NCDModuleGroup {
  659. /**
  660. * Function called before any instance of any module backend in this
  661. * group is crated. May be NULL.
  662. */
  663. NCDModule_func_globalinit func_globalinit;
  664. /**
  665. * Function called to clean up after {@link NCDModule_func_globalinit}.
  666. * May be NULL.
  667. */
  668. NCDModule_func_globalfree func_globalfree;
  669. /**
  670. * Array of module backends. The array must be terminated with a
  671. * structure that has a NULL type member.
  672. */
  673. const struct NCDModule *modules;
  674. };
  675. #endif