NCDModule.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /**
  2. * @file NCDModule.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_NCD_NCDMODULE_H
  30. #define BADVPN_NCD_NCDMODULE_H
  31. #include <misc/debug.h>
  32. #include <system/BReactor.h>
  33. #include <base/BPending.h>
  34. #include <base/BLog.h>
  35. #include <system/BProcess.h>
  36. #include <udevmonitor/NCDUdevManager.h>
  37. #include <ncd/NCDValue.h>
  38. #include <ncd/NCDObject.h>
  39. #define NCDMODULE_EVENT_UP 1
  40. #define NCDMODULE_EVENT_DOWN 2
  41. #define NCDMODULE_EVENT_DEAD 3
  42. struct NCDModuleInst_s;
  43. struct NCDModuleProcess_s;
  44. /**
  45. * Function called to inform the interpeter of state changes of the
  46. * module instance.
  47. * Possible events are:
  48. *
  49. * - NCDMODULE_EVENT_UP: the instance came up.
  50. * The instance was in down state.
  51. * The instance enters up state.
  52. *
  53. * - NCDMODULE_EVENT_DOWN: the instance went down.
  54. * The instance was in up state.
  55. * The instance enters down state.
  56. *
  57. * After the instance goes down, the interpreter should eventually call
  58. * {@link NCDModuleInst_Clean} or {@link NCDModuleInst_Die}, unless
  59. * the module goes up again.
  60. *
  61. * - NCDMODULE_EVENT_DEAD: the module died.
  62. * The instance enters dead state.
  63. *
  64. * This function is not being called in event context. The interpreter should
  65. * only update its internal state, and visibly react only via jobs that it pushes
  66. * from within this function. The only exception is that it may free the
  67. * instance from within the NCDMODULE_EVENT_DEAD event.
  68. *
  69. * @param user as in {@link NCDModuleInst_Init}
  70. * @param event event number
  71. */
  72. typedef void (*NCDModuleInst_func_event) (void *user, int event);
  73. /**
  74. * Function called when the module instance wants the interpreter to
  75. * resolve an object from the point of view of its statement.
  76. * The instance will not be in dead state.
  77. * This function must not have any side effects.
  78. *
  79. * @param user as in {@link NCDModuleInst_Init}
  80. * @param name name of the object
  81. * @param out_object the object will be returned here
  82. * @return 1 on success, 0 on failure
  83. */
  84. typedef int (*NCDModuleInst_func_getobj) (void *user, const char *name, NCDObject *out_object);
  85. /**
  86. * Function called when the module instance wants the interpreter to
  87. * create a new process backend from a process template.
  88. * The instance will not be in dead state.
  89. *
  90. * On success, the interpreter must have called {@link NCDModuleProcess_Interp_SetHandlers}
  91. * from within this function, to allow communication with the controller of the process.
  92. * On success, the new process backend enters down state.
  93. *
  94. * This function is not being called in event context. The interpreter should
  95. * only update its internal state, and visibly react only via jobs that it pushes
  96. * from within this function.
  97. *
  98. * @param user as in {@link NCDModuleInst_Init}
  99. * @param p handle for the new process backend
  100. * @param template_name name of the template to create the process from
  101. * @return 1 on success, 0 on failure
  102. */
  103. typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, const char *template_name);
  104. /**
  105. * Function called when the module instance wants the interpreter to
  106. * initiate termination, as if it received an external terminatio request (signal).
  107. *
  108. * @param user as in {@link NCDModuleInst_Init}
  109. * @param exit_code exit code to return the the operating system. This overrides any previously
  110. * set exit code, and will be overriden by a signal to the value 1.
  111. *
  112. */
  113. typedef void (*NCDModuleInst_func_interp_exit) (void *user, int exit_code);
  114. /**
  115. * Function called when the module instance wants the interpreter to
  116. * provide its extra command line arguments.
  117. *
  118. * @param user as in {@link NCDModuleInst_Init}
  119. * @param out_value on success, the interpreter will write the list of extra command
  120. * line arguments here
  121. * @return 1 on success, 0 on failure
  122. *
  123. */
  124. typedef int (*NCDModuleInst_func_interp_getargs) (void *user, NCDValue *out_value);
  125. /**
  126. * Function called when the module instance wants the interpreter to
  127. * provide its retry time.
  128. *
  129. * @param user as in {@link NCDModuleInst_Init}
  130. * @return retry time in milliseconds
  131. */
  132. typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void *user);
  133. #define NCDMODULEPROCESS_EVENT_UP 1
  134. #define NCDMODULEPROCESS_EVENT_DOWN 2
  135. #define NCDMODULEPROCESS_EVENT_TERMINATED 3
  136. /**
  137. * Handler which reports process state changes from the interpreter.
  138. * Possible events are:
  139. *
  140. * - NCDMODULEPROCESS_EVENT_UP: the process went up.
  141. * The process was in down state.
  142. * The process enters up state.
  143. *
  144. * - NCDMODULEPROCESS_EVENT_DOWN: the process went down.
  145. * The process was in up state.
  146. * The process enters waiting state.
  147. *
  148. * NOTE: the process enters waiting state, NOT down state, and is paused.
  149. * To allow the process to continue, call {@link NCDModuleProcess_Continue}.
  150. *
  151. * - NCDMODULEPROCESS_EVENT_TERMINATED: the process terminated.
  152. * The process was in terminating state.
  153. * The process enters terminated state.
  154. *
  155. * @param user as in {@link NCDModuleProcess_Init}
  156. * @param event event number
  157. */
  158. typedef void (*NCDModuleProcess_handler_event) (void *user, int event);
  159. /**
  160. * Function called when the interpreter wants to resolve a special
  161. * object in the process.
  162. * This function must have no side effects.
  163. *
  164. * @param user as in {@link NCDModuleProcess_Init}
  165. * @param name name of the object
  166. * @param out_object the object will be returned here
  167. * @return 1 on success, 0 on failure
  168. */
  169. typedef int (*NCDModuleProcess_func_getspecialobj) (void *user, const char *name, NCDObject *out_object);
  170. #define NCDMODULEPROCESS_INTERP_EVENT_CONTINUE 1
  171. #define NCDMODULEPROCESS_INTERP_EVENT_TERMINATE 2
  172. /**
  173. * Function called to report process backend requests to the interpreter.
  174. * Possible events are:
  175. *
  176. * - NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: the process can continue.
  177. * The process backend was in waiting state.
  178. * The process backend enters down state.
  179. *
  180. * - NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: the process should terminate.
  181. * The process backend was in down, up or waiting state.
  182. * The process backend enters terminating state.
  183. *
  184. * The interpreter should call {@link NCDModuleProcess_Interp_Terminated}
  185. * when the process terminates.
  186. *
  187. * This function is not being called in event context. The interpreter should
  188. * only update its internal state, and visibly react only via jobs that it pushes
  189. * from within this function.
  190. *
  191. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  192. * @param event event number
  193. */
  194. typedef void (*NCDModuleProcess_interp_func_event) (void *user, int event);
  195. /**
  196. * Function called to have the interpreter resolve an object within the process
  197. * of a process backend.
  198. * This function must not have any side effects.
  199. *
  200. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  201. * @param name name of the object
  202. * @param out_object the object will be returned here
  203. * @return 1 on success, 0 in failure
  204. */
  205. typedef int (*NCDModuleProcess_interp_func_getobj) (void *user, const char *name, NCDObject *out_object);
  206. struct NCDModule;
  207. struct NCDModuleInitParams {
  208. BReactor *reactor;
  209. BProcessManager *manager;
  210. NCDUdevManager *umanager;
  211. };
  212. /**
  213. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  214. * This only contains parameters related to communication between the backend
  215. * and the creator of the module instance.
  216. */
  217. struct NCDModuleInst_params {
  218. /**
  219. * Callback to report state changes.
  220. */
  221. NCDModuleInst_func_event func_event;
  222. /**
  223. * Callback to resolve objects from the viewpoint of the instance.
  224. */
  225. NCDModuleInst_func_getobj func_getobj;
  226. /**
  227. * Log function which appends a log prefix with {@link BLog_Append}.
  228. */
  229. BLog_logfunc logfunc;
  230. };
  231. /**
  232. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  233. * This only contains parameters related to services provided by the interpreter.
  234. */
  235. struct NCDModuleInst_iparams {
  236. /**
  237. * Reactor we live in.
  238. */
  239. BReactor *reactor;
  240. /**
  241. * Process manager.
  242. */
  243. BProcessManager *manager;
  244. /**
  245. * Udev manager.
  246. */
  247. NCDUdevManager *umanager;
  248. /**
  249. * Callback to create a new template process.
  250. */
  251. NCDModuleInst_func_initprocess func_initprocess;
  252. /**
  253. * Callback to request interpreter termination.
  254. */
  255. NCDModuleInst_func_interp_exit func_interp_exit;
  256. /**
  257. * Callback to get extra command line arguments.
  258. */
  259. NCDModuleInst_func_interp_getargs func_interp_getargs;
  260. /**
  261. * Callback to get retry time.
  262. */
  263. NCDModuleInst_func_interp_getretrytime func_interp_getretrytime;
  264. };
  265. /**
  266. * Module instance.
  267. * The module instance is initialized by the interpreter by calling
  268. * {@link NCDModuleInst_Init}. It is implemented by a module backend
  269. * specified in a {@link NCDModule}.
  270. */
  271. typedef struct NCDModuleInst_s {
  272. const struct NCDModule *m;
  273. void *method_user;
  274. NCDValue *args;
  275. void *user;
  276. const struct NCDModuleInst_params *params;
  277. const struct NCDModuleInst_iparams *iparams;
  278. void *inst_user;
  279. BPending job;
  280. int state;
  281. int is_error;
  282. DebugObject d_obj;
  283. } NCDModuleInst;
  284. /**
  285. * Process created from a process template on behalf of a module backend
  286. * instance, implemented by the interpreter.
  287. */
  288. typedef struct NCDModuleProcess_s {
  289. NCDValue args;
  290. void *user;
  291. NCDModuleProcess_handler_event handler_event;
  292. NCDModuleProcess_func_getspecialobj func_getspecialobj;
  293. BPending event_job;
  294. int state;
  295. int no_args;
  296. void *interp_user;
  297. NCDModuleProcess_interp_func_event interp_func_event;
  298. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  299. DebugObject d_obj;
  300. } NCDModuleProcess;
  301. /**
  302. * Initializes an instance of an NCD module.
  303. * The instance is initialized in down state.
  304. *
  305. * This and other non-Backend methods are the interpreter interface.
  306. * The Backend methods are the module backend interface and are documented
  307. * independently with their own logical states.
  308. *
  309. * @param n the instance
  310. * @param m structure of module functions implementing the module backend
  311. * @param method_object the base object if the module being initialized is a method, otherwise NULL.
  312. * The caller must ensure that this object is of the type expected by the module
  313. * being initialized.
  314. * @param args arguments to the module. Must be a NCDVALUE_LIST value. Must be available as long as
  315. * the instance is freed.
  316. * @param user argument to callback functions
  317. * @param params more parameters, see {@link NCDModuleInst_params}
  318. * @param iparams more parameters, see {@link NCDModuleInst_iparams}
  319. */
  320. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, const NCDObject *method_object, NCDValue *args, void *user, const struct NCDModuleInst_params *params, const struct NCDModuleInst_iparams *iparams);
  321. /**
  322. * Frees the instance.
  323. * The instance must be in dead state.
  324. *
  325. * @param n the instance
  326. */
  327. void NCDModuleInst_Free (NCDModuleInst *n);
  328. /**
  329. * Requests the instance to die.
  330. * The instance must be in down or up state.
  331. * The instance enters dying state.
  332. *
  333. * @param n the instance
  334. */
  335. void NCDModuleInst_Die (NCDModuleInst *n);
  336. /**
  337. * Informs the module that it is in a clean state to proceed.
  338. * The instance must be in down state.
  339. *
  340. * @param n the instance
  341. */
  342. void NCDModuleInst_Clean (NCDModuleInst *n);
  343. /**
  344. * Returns an {@link NCDObject} which can be used to resolve variables and objects
  345. * within this instance, as well as call its methods. The resulting object may only
  346. * be used immediately, and becomes invalid when the instance is freed.
  347. *
  348. * @param n the instance
  349. * @return an NCDObject for this instance
  350. */
  351. NCDObject NCDModuleInst_Object (NCDModuleInst *n);
  352. /**
  353. * Checks whether the module terminated unsuccessfully.
  354. * The instance must be in dead state.
  355. *
  356. * @param n the instance
  357. * @return 1 if module terminated unsuccessfully, 0 if successfully
  358. */
  359. int NCDModuleInst_HaveError (NCDModuleInst *n);
  360. /**
  361. * Sets the argument passed to handlers of a module backend instance.
  362. *
  363. * @param n backend instance handle
  364. * @param user value to pass to future handlers for this backend instance
  365. */
  366. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  367. /**
  368. * Retuns the argument passed to handlers of a module backend instance,
  369. * i.e. what was set in {@link NCDModuleInst_Backend_SetUser} (or NULL
  370. * by default).
  371. *
  372. * @param n backend instance handle
  373. * @return argument passed to handlers
  374. */
  375. void * NCDModuleInst_Backend_GetUser (NCDModuleInst *n);
  376. /**
  377. * Puts the backend instance into up state.
  378. * The instance must be in down state.
  379. * The instance enters up state.
  380. *
  381. * @param n backend instance handle
  382. */
  383. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  384. /**
  385. * Puts the backend instance into down state.
  386. * The instance must be in up state.
  387. * The instance enters down state.
  388. *
  389. * @param n backend instance handle
  390. */
  391. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  392. /**
  393. * Destroys the backend instance.
  394. * The backend instance handle becomes invalid and must not be used from
  395. * the backend any longer.
  396. *
  397. * @param n backend instance handle
  398. */
  399. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  400. /**
  401. * Resolves an object for a backend instance, from the point of the instance's
  402. * statement in the containing process.
  403. *
  404. * @param n backend instance handle
  405. * @param name name of the object to resolve
  406. * @param out_object the object will be returned here
  407. * @return 1 on success, 0 on failure
  408. */
  409. int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *name, NCDObject *out_object) WARN_UNUSED;
  410. /**
  411. * Logs a backend instance message.
  412. *
  413. * @param n backend instance handle
  414. * @param channel log channel
  415. * @param level loglevel
  416. * @param fmt format string as in printf, arguments follow
  417. */
  418. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  419. /**
  420. * Sets the error flag for the module instance.
  421. * The error flag only has no effect until the backend calls
  422. * {@link NCDModuleInst_Backend_Dead}.
  423. *
  424. * @param n backend instance handle
  425. */
  426. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  427. /**
  428. * Initiates interpreter termination.
  429. *
  430. * @param n backend instance handle
  431. * @param exit_code exit code to return to the operating system. This overrides
  432. * any previously set exit code, and will be overriden by a
  433. * termination signal to the value 1.
  434. */
  435. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  436. /**
  437. * Retrieves extra command line arguments passed to the interpreter.
  438. *
  439. * @param n backend instance handle
  440. * @param out_value the arguments will be written here on success as a list value
  441. * @return 1 on success, 0 on failure
  442. */
  443. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValue *out_value);
  444. /**
  445. * Returns the retry time of the intepreter.
  446. *
  447. * @param n backend instance handle
  448. * @return retry time in milliseconds
  449. */
  450. btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  451. /**
  452. * Initializes a process in the interpreter from a process template.
  453. * This must be called on behalf of a module backend instance.
  454. * The process is initializes in down state.
  455. *
  456. * @param o the process
  457. * @param n backend instance whose interpreter will be providing the process
  458. * @param template_name name of the process template
  459. * @param args arguments to the process. On success, the arguments become owned
  460. * by the interpreter. On failure, they are left untouched.
  461. * @param user argument to handlers
  462. * @param handler_event handler which reports events about the process from the
  463. * interpreter
  464. * @return 1 on success, 0 on failure
  465. */
  466. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  467. /**
  468. * Frees the process.
  469. * The process must be in terminated state.
  470. *
  471. * @param o the process
  472. */
  473. void NCDModuleProcess_Free (NCDModuleProcess *o);
  474. /**
  475. * Does nothing.
  476. * The process must be in terminated state.
  477. *
  478. * @param o the process
  479. */
  480. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  481. /**
  482. * Sets callback functions for providing special objects within the process.
  483. *
  484. * @param o the process
  485. * @param func_getspecialobj function for resolving special objects, or NULL
  486. */
  487. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  488. /**
  489. * Makes the process itself not resolve the _args and _argN special objects.
  490. * After calling this, the only special objects the interpreter can resolve
  491. * are those provided by the creator of the process via {@link NCDModuleProcess_SetSpecialFuncs}.
  492. *
  493. * @param o the process
  494. */
  495. void NCDModuleProcess_SetNoArgs (NCDModuleProcess *o);
  496. /**
  497. * Continues the process after the process went down.
  498. * The process must be in waiting state.
  499. * The process enters down state.
  500. *
  501. * @param o the process
  502. */
  503. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  504. /**
  505. * Requests the process to terminate.
  506. * The process must be in down, up or waiting state.
  507. * The process enters terminating state.
  508. *
  509. * @param o the process
  510. */
  511. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  512. /**
  513. * Resolves an object within the process from the point
  514. * at the end of the process.
  515. * This function has no side effects.
  516. *
  517. * @param o the process
  518. * @param name name of the object to resolve
  519. * @param out_object the object will be returned here
  520. * @return 1 on success, 0 on failure
  521. */
  522. int NCDModuleProcess_GetObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  523. /**
  524. * Sets callback functions for the interpreter to implement the
  525. * process backend.
  526. * Must be called from within {@link NCDModuleInst_func_initprocess}
  527. * if success is to be reported there.
  528. *
  529. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  530. * @param interp_user argument to callback functions
  531. * @param interp_func_event function for reporting continue/terminate requests
  532. * @param interp_func_getobj function for resolving objects within the process
  533. */
  534. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  535. NCDModuleProcess_interp_func_event interp_func_event,
  536. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  537. /**
  538. * Reports the process backend as up.
  539. * The process backend must be in down state.
  540. * The process backend enters up state.
  541. *
  542. * @param o process backend handle
  543. */
  544. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  545. /**
  546. * Reports the process backend as down.
  547. * The process backend must be in up state.
  548. * The process backend enters waiting state.
  549. *
  550. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  551. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  552. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  553. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  554. *
  555. * @param o process backend handle
  556. */
  557. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  558. /**
  559. * Reports termination of the process backend.
  560. * The process backend must be in terminating state.
  561. * The process backend handle becomes invalid and must not be used
  562. * by the interpreter any longer.
  563. *
  564. * @param o process backend handle
  565. */
  566. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  567. /**
  568. * Resolves a special process object for the process backend.
  569. *
  570. * @param o process backend handle
  571. * @param name name of the object
  572. * @param out_object the object will be returned here
  573. * @return 1 on success, 0 on failure
  574. */
  575. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  576. /**
  577. * Function called before any instance of any backend in a module
  578. * group is created;
  579. *
  580. * @param params structure containing global resources, in particular
  581. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}
  582. * @return 1 on success, 0 on failure
  583. */
  584. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
  585. /**
  586. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  587. * in a module group.
  588. * There are no backend instances alive from this module group.
  589. */
  590. typedef void (*NCDModule_func_globalfree) (void);
  591. /**
  592. * Handler called to create an new module backend instance.
  593. * The backend is initialized in down state.
  594. *
  595. * This handler should call {@link NCDModuleInst_Backend_SetUser} to provide
  596. * an argument for handlers of this backend instance.
  597. *
  598. * If the backend fails initialization, this function should report the backend
  599. * instance to have died with error by calling {@link NCDModuleInst_Backend_SetError}
  600. * and {@link NCDModuleInst_Backend_Dead}.
  601. *
  602. * @param i module backend instance handler. The backend may only use this handle via
  603. * the Backend functions of {@link NCDModuleInst}.
  604. */
  605. typedef void (*NCDModule_func_new) (NCDModuleInst *i);
  606. /**
  607. * Handler called to request termination of a backend instance.
  608. * The backend instance was in down or up state.
  609. * The backend instance enters dying state.
  610. *
  611. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  612. */
  613. typedef void (*NCDModule_func_die) (void *o);
  614. /**
  615. * Function called to resolve a variable within a backend instance.
  616. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  617. * This function must not have any side effects.
  618. *
  619. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  620. * @param name name of the variable to resolve
  621. * @param out on success, the backend should initialize the value here
  622. * @return 1 on success, 0 on failure
  623. */
  624. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValue *out);
  625. /**
  626. * Function called to resolve an object within a backend instance.
  627. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  628. * This function must not have any side effects.
  629. *
  630. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  631. * @param name name of the object to resolve
  632. * @param out_object the object will be returned here
  633. * @return 1 on success, 0 on failure
  634. */
  635. typedef int (*NCDModule_func_getobj) (void *o, const char *name, NCDObject *out_object);
  636. /**
  637. * Handler called when the module instance is in a clean state.
  638. * This means that all statements preceding it in the process are
  639. * up, this statement is down, and all following statements are
  640. * uninitialized. When a backend instance goes down, it is guaranteed,
  641. * as long as it stays down, that either this will be called or
  642. * termination will be requested with {@link NCDModule_func_die}.
  643. * The backend instance was in down state.
  644. *
  645. * @param o as in {@link NCDModuleInst_Backend_SetUser}, or NULL by default
  646. */
  647. typedef void (*NCDModule_func_clean) (void *o);
  648. /**
  649. * Structure encapsulating the implementation of a module backend.
  650. */
  651. struct NCDModule {
  652. /**
  653. * If this implements a plain statement, the name of the statement.
  654. * If this implements a method, then "base_type::method_name".
  655. */
  656. const char *type;
  657. /**
  658. * The base type for methods operating on instances of this backend.
  659. * Any module with type of form "base_type::method_name" is considered
  660. * a method of instances of this backend.
  661. * If this is NULL, the base type will default to type.
  662. */
  663. const char *base_type;
  664. /**
  665. * Function called to create an new backend instance.
  666. */
  667. NCDModule_func_new func_new;
  668. /**
  669. * Function called to request termination of a backend instance.
  670. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  671. */
  672. NCDModule_func_die func_die;
  673. /**
  674. * Function called to resolve a variable within the backend instance.
  675. * May be NULL.
  676. */
  677. NCDModule_func_getvar func_getvar;
  678. /**
  679. * Function called to resolve an object within the backend instance.
  680. * May be NULL.
  681. */
  682. NCDModule_func_getobj func_getobj;
  683. /**
  684. * Function called when the backend instance is in a clean state.
  685. * May be NULL.
  686. */
  687. NCDModule_func_clean func_clean;
  688. /**
  689. * Whether the interpreter is allowed to call func_getvar and func_getobj
  690. * even when the backend instance is in down state (as opposed to just
  691. * in up state).
  692. */
  693. int can_resolve_when_down;
  694. };
  695. /**
  696. * Structure encapsulating a group of module backend implementations,
  697. * with global init and free functions.
  698. */
  699. struct NCDModuleGroup {
  700. /**
  701. * Function called before any instance of any module backend in this
  702. * group is crated. May be NULL.
  703. */
  704. NCDModule_func_globalinit func_globalinit;
  705. /**
  706. * Function called to clean up after {@link NCDModule_func_globalinit}.
  707. * May be NULL.
  708. */
  709. NCDModule_func_globalfree func_globalfree;
  710. /**
  711. * Array of module backends. The array must be terminated with a
  712. * structure that has a NULL type member.
  713. */
  714. const struct NCDModule *modules;
  715. };
  716. #endif