NCDModule.h 26 KB

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