NCDModule.h 23 KB

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