NCDModule.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /**
  2. * @file NCDModule.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifndef BADVPN_NCD_NCDMODULE_H
  23. #define BADVPN_NCD_NCDMODULE_H
  24. #include <misc/debug.h>
  25. #include <system/BReactor.h>
  26. #include <base/BPending.h>
  27. #include <base/BLog.h>
  28. #include <system/BProcess.h>
  29. #include <udevmonitor/NCDUdevManager.h>
  30. #include <ncd/NCDValue.h>
  31. #define NCDMODULE_EVENT_UP 1
  32. #define NCDMODULE_EVENT_DOWN 2
  33. #define NCDMODULE_EVENT_DEAD 3
  34. struct NCDModuleInst_s;
  35. struct NCDModuleProcess_s;
  36. /**
  37. * Function called to inform the interpeter of state changes of the
  38. * module instance.
  39. * Possible events are:
  40. *
  41. * - NCDMODULE_EVENT_UP: the instance came up.
  42. * The instance was in down state.
  43. * The instance enters up state.
  44. *
  45. * - NCDMODULE_EVENT_DOWN: the instance went down.
  46. * The instance was in up state.
  47. * The instance enters down state.
  48. *
  49. * After the instance goes down, the interpreter should eventually call
  50. * {@link NCDModuleInst_Clean} or {@link NCDModuleInst_Die}, unless
  51. * the module goes up again.
  52. *
  53. * - NCDMODULE_EVENT_DEAD: the module died.
  54. * The instance enters dead state.
  55. *
  56. * This function is not being called in event context. The interpreter should
  57. * only update its internal state, and visibly react only via jobs that it pushes
  58. * from within this function. The only exception is that it may free the
  59. * instance from within the NCDMODULE_EVENT_DEAD event.
  60. *
  61. * @param user as in {@link NCDModuleInst_Init}
  62. * @param event event number
  63. */
  64. typedef void (*NCDModuleInst_func_event) (void *user, int event);
  65. /**
  66. * Function called when the module instance wants the interpreter to
  67. * resolve a variable from the point of view of its statement.
  68. * The instance will not be in dead state.
  69. * This function must not have any side effects.
  70. *
  71. * @param user as in {@link NCDModuleInst_Init}
  72. * @param name name of the variable
  73. * @param out on success, the interpreter should initialize the value here
  74. * @return 1 on success, 0 on failure
  75. */
  76. typedef int (*NCDModuleInst_func_getvar) (void *user, const char *name, NCDValue *out);
  77. /**
  78. * Function called when the module instance wants the interpreter to
  79. * resolve an object from the point of view of its statement.
  80. * The instance will not be in dead state.
  81. * This function must not have any side effects.
  82. *
  83. * @param user as in {@link NCDModuleInst_Init}
  84. * @param name name of the object
  85. * @return object, or NULL on failure
  86. */
  87. typedef struct NCDModuleInst_s * (*NCDModuleInst_func_getobj) (void *user, const char *name);
  88. /**
  89. * Function called when the module instance wants the interpreter to
  90. * create a new process backend from a process template.
  91. * The instance will not be in dead state.
  92. *
  93. * On success, the interpreter must have called {@link NCDModuleProcess_Interp_SetHandlers}
  94. * from within this function, to allow communication with the controller of the process.
  95. * On success, the new process backend enters down state.
  96. *
  97. * This function is not being called in event context. The interpreter should
  98. * only update its internal state, and visibly react only via jobs that it pushes
  99. * from within this function.
  100. *
  101. * @param user as in {@link NCDModuleInst_Init}
  102. * @param p handle for the new process backend
  103. * @param template_name name of the template to create the process from
  104. * @param args process arguments. On success, the arguments become owned
  105. * by the interpreter. On failure, the interpreter must leave
  106. * them unchanged.
  107. * @return 1 on success, 0 on failure
  108. */
  109. typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, const char *template_name, NCDValue args);
  110. #define NCDMODULEPROCESS_EVENT_UP 1
  111. #define NCDMODULEPROCESS_EVENT_DOWN 2
  112. #define NCDMODULEPROCESS_EVENT_TERMINATED 3
  113. /**
  114. * Handler which reports process state changes from the interpreter.
  115. * Possible events are:
  116. *
  117. * - NCDMODULEPROCESS_EVENT_UP: the process went up.
  118. * The process was in down state.
  119. * The process enters up state.
  120. *
  121. * - NCDMODULEPROCESS_EVENT_DOWN: the process went down.
  122. * The process was in up state.
  123. * The process enters waiting state.
  124. *
  125. * NOTE: the process enters waiting state, NOT down state, and is paused.
  126. * To allow the process to continue, call {@link NCDModuleProcess_Continue}.
  127. *
  128. * - NCDMODULEPROCESS_EVENT_TERMINATED: the process terminated.
  129. * The process was in terminating state.
  130. * The process enters terminated state.
  131. *
  132. * @param user as in {@link NCDModuleProcess_Init}
  133. * @param event event number
  134. */
  135. typedef void (*NCDModuleProcess_handler_event) (void *user, int event);
  136. /**
  137. * Function called when the interpreter wants to resolve a special
  138. * variable in the process.
  139. * This function must have no side effects.
  140. *
  141. * @param user as in {@link NCDModuleProcess_Init}
  142. * @param name name of the variable
  143. * @param out on success, should initialize the value here
  144. * @return 1 on success, 0 on failure
  145. */
  146. typedef int (*NCDModuleProcess_func_getspecialvar) (void *user, const char *name, NCDValue *out);
  147. /**
  148. * Function called when the interpreter wants to resolve a special
  149. * object in the process.
  150. * This function must have no side effects.
  151. *
  152. * @param user as in {@link NCDModuleProcess_Init}
  153. * @param name name of the object
  154. * @return object, or NULL on failure
  155. */
  156. typedef struct NCDModuleInst_s * (*NCDModuleProcess_func_getspecialobj) (void *user, const char *name);
  157. #define NCDMODULEPROCESS_INTERP_EVENT_CONTINUE 1
  158. #define NCDMODULEPROCESS_INTERP_EVENT_TERMINATE 2
  159. /**
  160. * Function called to report process backend requests to the interpreter.
  161. * Possible events are:
  162. *
  163. * - NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: the process can continue.
  164. * The process backend was in waiting state.
  165. * The process backend enters down state.
  166. *
  167. * - NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: the process should terminate.
  168. * The process backend was in down, up or waiting state.
  169. * The process backend enters terminating state.
  170. *
  171. * The interpreter should call {@link NCDModuleProcess_Interp_Terminated}
  172. * when the process terminates.
  173. *
  174. * This function is not being called in event context. The interpreter should
  175. * only update its internal state, and visibly react only via jobs that it pushes
  176. * from within this function.
  177. *
  178. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  179. * @param event event number
  180. */
  181. typedef void (*NCDModuleProcess_interp_func_event) (void *user, int event);
  182. /**
  183. * Function called to have the interpreter resolve a variable within the process
  184. * of a process backend.
  185. * This function must not have any side effects.
  186. *
  187. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  188. * @param name name of the variable
  189. * @param out on success, the interpreter should initialize the value here
  190. * @return 1 on success, 0 on failure
  191. */
  192. typedef int (*NCDModuleProcess_interp_func_getvar) (void *user, const char *name, NCDValue *out);
  193. /**
  194. * Function called to have the interpreter resolve an object within the process
  195. * of a process backend.
  196. * This function must not have any side effects.
  197. *
  198. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  199. * @param name name of the object
  200. * @return object, or NULL on failure
  201. */
  202. typedef struct NCDModuleInst_s * (*NCDModuleProcess_interp_func_getobj) (void *user, const char *name);
  203. struct NCDModule;
  204. struct NCDModuleInitParams {
  205. BReactor *reactor;
  206. BProcessManager *manager;
  207. NCDUdevManager *umanager;
  208. };
  209. /**
  210. * Module instance.
  211. * The module instance is initialized by the interpreter by calling
  212. * {@link NCDModuleInst_Init}. It is implemented by a module backend
  213. * specified in a {@link NCDModule}.
  214. */
  215. typedef struct NCDModuleInst_s {
  216. const struct NCDModule *m;
  217. struct NCDModuleInst_s *method_object;
  218. NCDValue *args;
  219. BReactor *reactor;
  220. BProcessManager *manager;
  221. NCDUdevManager *umanager;
  222. void *user;
  223. NCDModuleInst_func_event func_event;
  224. NCDModuleInst_func_getvar func_getvar;
  225. NCDModuleInst_func_getobj func_getobj;
  226. NCDModuleInst_func_initprocess func_initprocess;
  227. BLog_logfunc logfunc;
  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. void *user;
  244. NCDModuleProcess_handler_event handler_event;
  245. NCDModuleProcess_func_getspecialvar func_getspecialvar;
  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_getvar interp_func_getvar;
  252. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  253. DebugObject d_obj;
  254. } NCDModuleProcess;
  255. /**
  256. * Initializes an instance of an NCD module.
  257. * The instance is initialized in down state.
  258. *
  259. * This and other non-Backend methods are the interpreter interface.
  260. * The Backend methods are the module backend interface and are documented
  261. * independently with their own logical states.
  262. *
  263. * @param n the instance
  264. * @param m structure of module functions implementing the module backend
  265. * @param method_object the base module if the module being initialized is a method, otherwise NULL.
  266. * The caller must ensure that this module is of the type expected by the module
  267. * being initialized.
  268. * @param args arguments to the module. Must be a NCDVALUE_LIST value. Must be available as long as
  269. * the instance is freed.
  270. * @param reactor reactor we live in
  271. * @param manager process manager
  272. * @param umanager udev manager
  273. * @param user argument to callback functions
  274. * @param func_event callback to report state changes
  275. * @param func_getvar callback to resolve variables from the viewpoint of the instance
  276. * @param func_getobj callback to resolve objects from the viewpoint of the instance
  277. * @param logfunc log function which appends a log prefix with {@link BLog_Append}
  278. */
  279. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, NCDModuleInst *method_object, NCDValue *args, BReactor *reactor, BProcessManager *manager, NCDUdevManager *umanager, void *user,
  280. NCDModuleInst_func_event func_event,
  281. NCDModuleInst_func_getvar func_getvar,
  282. NCDModuleInst_func_getobj func_getobj,
  283. NCDModuleInst_func_initprocess func_initprocess,
  284. BLog_logfunc logfunc);
  285. /**
  286. * Frees the instance.
  287. * The instance must be in dead state.
  288. *
  289. * @param n the instance
  290. */
  291. void NCDModuleInst_Free (NCDModuleInst *n);
  292. /**
  293. * Requests the instance to die.
  294. * The instance must be in down or up state.
  295. * The instance enters dying state.
  296. *
  297. * @param n the instance
  298. */
  299. void NCDModuleInst_Die (NCDModuleInst *n);
  300. /**
  301. * Informs the module that it is in a clean state to proceed.
  302. * The instance must be in down state.
  303. *
  304. * @param n the instance
  305. */
  306. void NCDModuleInst_Clean (NCDModuleInst *n);
  307. /**
  308. * Resolves a variable within the instance.
  309. * The instance must be in up state.
  310. * This function does not have any side effects.
  311. *
  312. * @param n the instance
  313. * @param name name of the variable to resolve
  314. * @param out the value will be initialized here if successful
  315. * @return 1 on success, 0 on failure
  316. */
  317. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out) WARN_UNUSED;
  318. /**
  319. * Resolves an object within the instance.
  320. * The instance must be in up state.
  321. * This function does not have any side effects.
  322. *
  323. * @param n the instance
  324. * @param name name of the object to resolve
  325. * @return module instance, or NULL on failure
  326. */
  327. NCDModuleInst * NCDModuleInst_GetObj (NCDModuleInst *n, const char *name) WARN_UNUSED;
  328. /**
  329. * Checks whether the module terminated unsuccessfully.
  330. * The instance must be in dead state.
  331. *
  332. * @param n the instance
  333. * @return 1 if module terminated unsuccessfully, 0 if successfully
  334. */
  335. int NCDModuleInst_HaveError (NCDModuleInst *n);
  336. /**
  337. * Sets the argument passed to handlers of a module backend instance.
  338. *
  339. * @param n backend instance handle
  340. * @param user value to pass to future handlers for this backend instance
  341. */
  342. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  343. /**
  344. * Puts the backend instance into up state.
  345. * The instance must be in down state.
  346. * The instance enters up state.
  347. *
  348. * @param n backend instance handle
  349. */
  350. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  351. /**
  352. * Puts the backend instance into down state.
  353. * The instance must be in up state.
  354. * The instance enters down state.
  355. *
  356. * @param n backend instance handle
  357. */
  358. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  359. /**
  360. * Destroys the backend instance.
  361. * The backend instance handle becomes invalid and must not be used from
  362. * the backend any longer.
  363. *
  364. * @param n backend instance handle
  365. */
  366. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  367. /**
  368. * Resolves a variable for a backend instance, from the point of the instance's
  369. * statement in the containing process.
  370. *
  371. * @param n backend instance handle
  372. * @param name name of the variable to resolve
  373. * @param out the value will be initialized here if successful
  374. * @return 1 on success, 0 on failure
  375. */
  376. int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *name, NCDValue *out) WARN_UNUSED;
  377. /**
  378. * Resolves an object for a backend instance, from the point of the instance's
  379. * statement in the containing process.
  380. *
  381. * @param n backend instance handle
  382. * @param name name of the object to resolve
  383. * @return module instance, or NULL on failure
  384. */
  385. NCDModuleInst * NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *name) WARN_UNUSED;
  386. /**
  387. * Logs a backend instance message.
  388. *
  389. * @param n backend instance handle
  390. * @param channel log channel
  391. * @param level loglevel
  392. * @param fmt format string as in printf, arguments follow
  393. */
  394. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  395. /**
  396. * Sets the error flag for the module instance.
  397. * The error flag only has no effect until the backend calls
  398. * {@link NCDModuleInst_Backend_Dead}.
  399. *
  400. * @param n backend instance handle
  401. */
  402. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  403. /**
  404. * Initializes a process in the interpreter from a process template.
  405. * This must be called on behalf of a module backend instance.
  406. * The process is initializes in down state.
  407. *
  408. * @param o the process
  409. * @param n backend instance whose interpreter will be providing the process
  410. * @param template_name name of the process template
  411. * @param args arguments to the process. On success, the arguments become owned
  412. * by the interpreter. On failure, they are left untouched.
  413. * @param user argument to handlers
  414. * @param handler_event handler which reports events about the process from the
  415. * interpreter
  416. * @return 1 on success, 0 on failure
  417. */
  418. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_event handler_event);
  419. /**
  420. * Frees the process.
  421. * The process must be in terminated state.
  422. *
  423. * @param o the process
  424. */
  425. void NCDModuleProcess_Free (NCDModuleProcess *o);
  426. /**
  427. * Sets callback functions for providing special variables and objects within
  428. * the process.
  429. *
  430. * @param o the process
  431. * @param func_getspecialvar function for resolving special variables, or NULL
  432. * @param func_getspecialobj function for resolving special objects, or NULL
  433. */
  434. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialvar func_getspecialvar, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  435. /**
  436. * Continues the process after the process went down.
  437. * The process must be in waiting state.
  438. * The process enters down state.
  439. *
  440. * @param o the process
  441. */
  442. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  443. /**
  444. * Requests the process to terminate.
  445. * The process must be in down, up or waiting state.
  446. * The process enters terminating state.
  447. *
  448. * @param o the process
  449. */
  450. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  451. /**
  452. * Resolves a variable within the process from the point
  453. * at the end of the process.
  454. * This function has no side effects.
  455. *
  456. * @param o the process
  457. * @param name name of the variable to resolve
  458. * @param out the value will be initialized here if successful
  459. * @return 1 on success, 0 on failure
  460. */
  461. int NCDModuleProcess_GetVar (NCDModuleProcess *o, const char *name, NCDValue *out) WARN_UNUSED;
  462. /**
  463. * Resolves an object within the process from the point
  464. * at the end of the process.
  465. * This function has no side effects.
  466. *
  467. * @param o the process
  468. * @param name name of the object to resolve
  469. * @return module instance, or NULL on failure
  470. */
  471. NCDModuleInst * NCDModuleProcess_GetObj (NCDModuleProcess *o, const char *name) WARN_UNUSED;
  472. /**
  473. * Sets callback functions for the interpreter to implement the
  474. * process backend.
  475. * Must be called from within {@link NCDModuleInst_func_initprocess}
  476. * if success is to be reported there.
  477. *
  478. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  479. * @param interp_user argument to callback functions
  480. * @param interp_func_event function for reporting continue/terminate requests
  481. * @param interp_func_getvar function for resolving variables within the process
  482. * @param interp_func_getobj function for resolving objects within the process
  483. */
  484. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  485. NCDModuleProcess_interp_func_event interp_func_event,
  486. NCDModuleProcess_interp_func_getvar interp_func_getvar,
  487. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  488. /**
  489. * Reports the process backend as up.
  490. * The process backend must be in down state.
  491. * The process backend enters up state.
  492. *
  493. * @param o process backend handle
  494. */
  495. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  496. /**
  497. * Reports the process backend as down.
  498. * The process backend must be in up state.
  499. * The process backend enters waiting state.
  500. *
  501. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  502. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  503. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  504. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  505. *
  506. * @param o process backend handle
  507. */
  508. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  509. /**
  510. * Reports termination of the process backend.
  511. * The process backend must be in terminating state.
  512. * The process backend handle becomes invalid and must not be used
  513. * by the interpreter any longer.
  514. *
  515. * @param o process backend handle
  516. */
  517. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  518. /**
  519. * Resolves a special process variable for the process backend.
  520. *
  521. * @param o process backend handle
  522. * @param name name of the variable
  523. * @param out the value will be initialized here if successful
  524. * @return 1 on success, 0 on failure
  525. */
  526. int NCDModuleProcess_Interp_GetSpecialVar (NCDModuleProcess *o, const char *name, NCDValue *out) WARN_UNUSED;
  527. /**
  528. * Resolves a special process object for the process backend.
  529. *
  530. * @param o process backend handle
  531. * @param name name of the object
  532. * @return object, or NULL on failure
  533. */
  534. NCDModuleInst * NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, const char *name) 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.
  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.
  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. * @return object, or NULL on failure
  592. */
  593. typedef NCDModuleInst * (*NCDModule_func_getobj) (void *o, const char *name);
  594. /**
  595. * Handler called when the module instance is in a clean state.
  596. * This means that all statements preceding it in the process are
  597. * up, this statement is down, and all following statements are
  598. * uninitialized. When a backend instance goes down, it is guaranteed,
  599. * as long as it stays down, that either this will be called or
  600. * termination will be requested with {@link NCDModule_func_die}.
  601. * The backend instance was in down state.
  602. *
  603. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  604. */
  605. typedef void (*NCDModule_func_clean) (void *o);
  606. /**
  607. * Structure encapsulating the implementation of a module backend.
  608. */
  609. struct NCDModule {
  610. /**
  611. * If this implements a plain statement, the name of the statement.
  612. * If this implements a method, then "base_type::method_name".
  613. */
  614. const char *type;
  615. /**
  616. * The base type for methods operating on instances of this backend.
  617. * Any module with type of form "base_type::method_name" is considered
  618. * a method of instances of this backend.
  619. * If this is NULL, the base type will default to type.
  620. */
  621. const char *base_type;
  622. /**
  623. * Function called to create an new backend instance.
  624. */
  625. NCDModule_func_new func_new;
  626. /**
  627. * Function called to request termination of a backend instance.
  628. */
  629. NCDModule_func_die func_die;
  630. /**
  631. * Function called to resolve a variable within the backend instance.
  632. * May be NULL.
  633. */
  634. NCDModule_func_getvar func_getvar;
  635. /**
  636. * Function called to resolve an object within the backend instance.
  637. * May be NULL.
  638. */
  639. NCDModule_func_getobj func_getobj;
  640. /**
  641. * Function called when the backend instance is in a clean state.
  642. * May be NULL.
  643. */
  644. NCDModule_func_clean func_clean;
  645. };
  646. /**
  647. * Structure encapsulating a group of module backend implementations,
  648. * with global init and free functions.
  649. */
  650. struct NCDModuleGroup {
  651. /**
  652. * Function called before any instance of any module backend in this
  653. * group is crated. May be NULL.
  654. */
  655. NCDModule_func_globalinit func_globalinit;
  656. /**
  657. * Function called to clean up after {@link NCDModule_func_globalinit}.
  658. * May be NULL.
  659. */
  660. NCDModule_func_globalfree func_globalfree;
  661. /**
  662. * Array of module backends. The array must be terminated with a
  663. * structure that has a NULL type member.
  664. */
  665. const struct NCDModule *modules;
  666. };
  667. #endif