NCDModule.h 23 KB

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