NCDModule.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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 <random/BRandom2.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 mem value memory to use
  120. * @param out_value write value reference here on success
  121. * @return 1 if available, 0 if not available. If available, but out of memory, return 1
  122. * and an invalid value.
  123. */
  124. typedef int (*NCDModuleInst_func_interp_getargs) (void *user, NCDValMem *mem, NCDValRef *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. BRandom2 *random2;
  212. };
  213. /**
  214. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  215. * This only contains parameters related to communication between the backend
  216. * and the creator of the module instance.
  217. */
  218. struct NCDModuleInst_params {
  219. /**
  220. * Callback to report state changes.
  221. */
  222. NCDModuleInst_func_event func_event;
  223. /**
  224. * Callback to resolve objects from the viewpoint of the instance.
  225. */
  226. NCDModuleInst_func_getobj func_getobj;
  227. /**
  228. * Log function which appends a log prefix with {@link BLog_Append}.
  229. */
  230. BLog_logfunc logfunc;
  231. };
  232. /**
  233. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  234. * This only contains parameters related to services provided by the interpreter.
  235. */
  236. struct NCDModuleInst_iparams {
  237. /**
  238. * Reactor we live in.
  239. */
  240. BReactor *reactor;
  241. /**
  242. * Process manager.
  243. */
  244. BProcessManager *manager;
  245. /**
  246. * Udev manager.
  247. */
  248. NCDUdevManager *umanager;
  249. /**
  250. * Random number generator.
  251. */
  252. BRandom2 *random2;
  253. /**
  254. * Callback to create a new template process.
  255. */
  256. NCDModuleInst_func_initprocess func_initprocess;
  257. /**
  258. * Callback to request interpreter termination.
  259. */
  260. NCDModuleInst_func_interp_exit func_interp_exit;
  261. /**
  262. * Callback to get extra command line arguments.
  263. */
  264. NCDModuleInst_func_interp_getargs func_interp_getargs;
  265. /**
  266. * Callback to get retry time.
  267. */
  268. NCDModuleInst_func_interp_getretrytime func_interp_getretrytime;
  269. };
  270. /**
  271. * Module instance.
  272. * The module instance is initialized by the interpreter by calling
  273. * {@link NCDModuleInst_Init}. It is implemented by a module backend
  274. * specified in a {@link NCDModule}.
  275. */
  276. typedef struct NCDModuleInst_s {
  277. const struct NCDModule *m;
  278. void *method_user;
  279. NCDValRef args;
  280. void *user;
  281. const struct NCDModuleInst_params *params;
  282. const struct NCDModuleInst_iparams *iparams;
  283. void *inst_user;
  284. int state;
  285. int is_error;
  286. DebugObject d_obj;
  287. } NCDModuleInst;
  288. /**
  289. * Process created from a process template on behalf of a module backend
  290. * instance, implemented by the interpreter.
  291. */
  292. typedef struct NCDModuleProcess_s {
  293. NCDValRef args;
  294. void *user;
  295. NCDModuleProcess_handler_event handler_event;
  296. NCDModuleProcess_func_getspecialobj func_getspecialobj;
  297. int state;
  298. void *interp_user;
  299. NCDModuleProcess_interp_func_event interp_func_event;
  300. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  301. DebugObject d_obj;
  302. } NCDModuleProcess;
  303. /**
  304. * Initializes an instance of an NCD module.
  305. * The instance is initialized in down state.
  306. * WARNING: this directly calls the module backend; expect to be called back
  307. *
  308. * This and other non-Backend methods are the interpreter interface.
  309. * The Backend methods are the module backend interface and are documented
  310. * independently with their own logical states.
  311. *
  312. * @param n the instance
  313. * @param m structure of module functions implementing the module backend
  314. * @param mem preallocated memory for the instance. If m->prealloc_size == 0, this must be NULL;
  315. * if it is >0, it must point to a block of memory with at least that many bytes available,
  316. * and properly aligned for any object.
  317. * @param method_object the base object if the module being initialized is a method, otherwise NULL.
  318. * The caller must ensure that this object is of the type expected by the module
  319. * being initialized.
  320. * @param args arguments to the module. Must be a list value. Must be available and unchanged
  321. * as long as the instance exists.
  322. * @param user argument to callback functions
  323. * @param params more parameters, see {@link NCDModuleInst_params}
  324. * @param iparams more parameters, see {@link NCDModuleInst_iparams}
  325. */
  326. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem, const NCDObject *method_object, NCDValRef args, void *user, const struct NCDModuleInst_params *params, const struct NCDModuleInst_iparams *iparams);
  327. /**
  328. * Frees the instance.
  329. * The instance must be in dead state.
  330. *
  331. * @param n the instance
  332. */
  333. void NCDModuleInst_Free (NCDModuleInst *n);
  334. /**
  335. * Requests the instance to die.
  336. * The instance must be in down or up state.
  337. * The instance enters dying state.
  338. * WARNING: this directly calls the module backend; expect to be called back
  339. *
  340. * @param n the instance
  341. */
  342. void NCDModuleInst_Die (NCDModuleInst *n);
  343. /**
  344. * Informs the module that it is in a clean state to proceed.
  345. * The instance must be in down state.
  346. * WARNING: this directly calls the module backend; expect to be called back
  347. *
  348. * @param n the instance
  349. */
  350. void NCDModuleInst_Clean (NCDModuleInst *n);
  351. /**
  352. * Returns an {@link NCDObject} which can be used to resolve variables and objects
  353. * within this instance, as well as call its methods. The resulting object may only
  354. * be used immediately, and becomes invalid when the instance is freed.
  355. *
  356. * @param n the instance
  357. * @return an NCDObject for this instance
  358. */
  359. NCDObject NCDModuleInst_Object (NCDModuleInst *n);
  360. /**
  361. * Checks whether the module terminated unsuccessfully.
  362. * The instance must be in dead state.
  363. *
  364. * @param n the instance
  365. * @return 1 if module terminated unsuccessfully, 0 if successfully
  366. */
  367. int NCDModuleInst_HaveError (NCDModuleInst *n);
  368. /**
  369. * Sets the argument passed to handlers of a module backend instance.
  370. * If the alloc_size member in the {@link NCDModule} structure is >0,
  371. * the argument will automatically be set to the preallocated memory,
  372. * in which case there is no need to call this.
  373. *
  374. * @param n backend instance handle
  375. * @param user value to pass to future handlers for this backend instance
  376. */
  377. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  378. /**
  379. * Retuns the argument passed to handlers of a module backend instance;
  380. * see {@link NCDModuleInst_Backend_SetUser}.
  381. *
  382. * @param n backend instance handle
  383. * @return argument passed to handlers
  384. */
  385. void * NCDModuleInst_Backend_GetUser (NCDModuleInst *n);
  386. /**
  387. * Puts the backend instance into up state.
  388. * The instance must be in down state.
  389. * The instance enters up state.
  390. *
  391. * @param n backend instance handle
  392. */
  393. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  394. /**
  395. * Puts the backend instance into down state.
  396. * The instance must be in up state.
  397. * The instance enters down state.
  398. *
  399. * @param n backend instance handle
  400. */
  401. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  402. /**
  403. * Destroys the backend instance.
  404. * The backend instance handle becomes invalid and must not be used from
  405. * the backend any longer.
  406. *
  407. * @param n backend instance handle
  408. */
  409. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  410. /**
  411. * Resolves an object for a backend instance, from the point of the instance's
  412. * statement in the containing process.
  413. *
  414. * @param n backend instance handle
  415. * @param name name of the object to resolve
  416. * @param out_object the object will be returned here
  417. * @return 1 on success, 0 on failure
  418. */
  419. int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *name, NCDObject *out_object) WARN_UNUSED;
  420. /**
  421. * Logs a backend instance message.
  422. *
  423. * @param n backend instance handle
  424. * @param channel log channel
  425. * @param level loglevel
  426. * @param fmt format string as in printf, arguments follow
  427. */
  428. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  429. /**
  430. * Sets the error flag for the module instance.
  431. * The error flag only has no effect until the backend calls
  432. * {@link NCDModuleInst_Backend_Dead}.
  433. *
  434. * @param n backend instance handle
  435. */
  436. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  437. /**
  438. * Initiates interpreter termination.
  439. *
  440. * @param n backend instance handle
  441. * @param exit_code exit code to return to the operating system. This overrides
  442. * any previously set exit code, and will be overriden by a
  443. * termination signal to the value 1.
  444. */
  445. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  446. /**
  447. * Retrieves extra command line arguments passed to the interpreter.
  448. *
  449. * @param n backend instance handle
  450. * @param mem value memory to use
  451. * @param out_value the arguments will be written here on success as a list value
  452. * @return 1 if available, 0 if not available. If available, but out of memory, returns 1
  453. * and an invalid value.
  454. */
  455. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value);
  456. /**
  457. * Returns the retry time of the intepreter.
  458. *
  459. * @param n backend instance handle
  460. * @return retry time in milliseconds
  461. */
  462. btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  463. /**
  464. * Initializes a process in the interpreter from a process template.
  465. * This must be called on behalf of a module backend instance.
  466. * The process is initializes in down state.
  467. *
  468. * @param o the process
  469. * @param n backend instance whose interpreter will be providing the process
  470. * @param template_name name of the process template
  471. * @param args arguments to the process. Must be an invalid value or a list value.
  472. * The value must be available and unchanged while the process exists.
  473. * @param user argument to handlers
  474. * @param handler_event handler which reports events about the process from the
  475. * interpreter
  476. * @return 1 on success, 0 on failure
  477. */
  478. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  479. /**
  480. * Frees the process.
  481. * The process must be in terminated state.
  482. *
  483. * @param o the process
  484. */
  485. void NCDModuleProcess_Free (NCDModuleProcess *o);
  486. /**
  487. * Does nothing.
  488. * The process must be in terminated state.
  489. *
  490. * @param o the process
  491. */
  492. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  493. /**
  494. * Sets callback functions for providing special objects within the process.
  495. *
  496. * @param o the process
  497. * @param func_getspecialobj function for resolving special objects, or NULL
  498. */
  499. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  500. /**
  501. * Continues the process after the process went down.
  502. * The process must be in waiting state.
  503. * The process enters down state.
  504. *
  505. * @param o the process
  506. */
  507. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  508. /**
  509. * Requests the process to terminate.
  510. * The process must be in down, up or waiting state.
  511. * The process enters terminating state.
  512. *
  513. * @param o the process
  514. */
  515. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  516. /**
  517. * Resolves an object within the process from the point
  518. * at the end of the process.
  519. * This function has no side effects.
  520. *
  521. * @param o the process
  522. * @param name name of the object to resolve
  523. * @param out_object the object will be returned here
  524. * @return 1 on success, 0 on failure
  525. */
  526. int NCDModuleProcess_GetObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  527. /**
  528. * Sets callback functions for the interpreter to implement the
  529. * process backend.
  530. * Must be called from within {@link NCDModuleInst_func_initprocess}
  531. * if success is to be reported there.
  532. *
  533. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  534. * @param interp_user argument to callback functions
  535. * @param interp_func_event function for reporting continue/terminate requests
  536. * @param interp_func_getobj function for resolving objects within the process
  537. */
  538. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  539. NCDModuleProcess_interp_func_event interp_func_event,
  540. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  541. /**
  542. * Reports the process backend as up.
  543. * The process backend must be in down state.
  544. * The process backend enters up state.
  545. * WARNING: this directly calls the process creator; expect to be called back
  546. *
  547. * @param o process backend handle
  548. */
  549. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  550. /**
  551. * Reports the process backend as down.
  552. * The process backend must be in up state.
  553. * The process backend enters waiting state.
  554. * WARNING: this directly calls the process creator; expect to be called back
  555. *
  556. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  557. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  558. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  559. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  560. *
  561. * @param o process backend handle
  562. */
  563. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  564. /**
  565. * Reports termination of the process backend.
  566. * The process backend must be in terminating state.
  567. * The process backend handle becomes invalid and must not be used
  568. * by the interpreter any longer.
  569. * WARNING: this directly calls the process creator; expect to be called back
  570. *
  571. * @param o process backend handle
  572. */
  573. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  574. /**
  575. * Resolves a special process object for the process backend.
  576. *
  577. * @param o process backend handle
  578. * @param name name of the object
  579. * @param out_object the object will be returned here
  580. * @return 1 on success, 0 on failure
  581. */
  582. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, const char *name, NCDObject *out_object) WARN_UNUSED;
  583. /**
  584. * Function called before any instance of any backend in a module
  585. * group is created;
  586. *
  587. * @param params structure containing global resources, in particular
  588. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}
  589. * @return 1 on success, 0 on failure
  590. */
  591. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
  592. /**
  593. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  594. * in a module group.
  595. * There are no backend instances alive from this module group.
  596. */
  597. typedef void (*NCDModule_func_globalfree) (void);
  598. /**
  599. * Handler called to create an new module backend instance.
  600. * The backend is initialized in down state.
  601. *
  602. * This handler should call {@link NCDModuleInst_Backend_SetUser} to provide
  603. * an argument for handlers of this backend instance if it needs to keep any
  604. * kind of state. Alternatively, the module can have the interpreter preallocate
  605. * a predefined amount of memory for each instance, in which case the
  606. * {@link NCDModule_func_new2} init function should be used instead of this one
  607. * (see alloc_size in the {@link NCDModule} structure).
  608. *
  609. * If the backend fails initialization, this function should report the backend
  610. * instance to have died with error by calling {@link NCDModuleInst_Backend_SetError}
  611. * and {@link NCDModuleInst_Backend_Dead}.
  612. *
  613. * @param i module backend instance handler. The backend may only use this handle via
  614. * the Backend functions of {@link NCDModuleInst}.
  615. */
  616. typedef void (*NCDModule_func_new) (NCDModuleInst *i);
  617. /**
  618. * Like {@link NCDModule_func_new}, but with the extra user argument, as in other module
  619. * instance handlers. The initial value of the argument, as used here, is a pointer to
  620. * preallocated memory of alloc_size bytes (from {@link NCDModule}), or NULL if alloc_size==0.
  621. */
  622. typedef void (*NCDModule_func_new2) (void *o, NCDModuleInst *i);
  623. /**
  624. * Handler called to request termination of a backend instance.
  625. * The backend instance was in down or up state.
  626. * The backend instance enters dying state.
  627. *
  628. * @param o see {@link NCDModuleInst_Backend_SetUser}
  629. */
  630. typedef void (*NCDModule_func_die) (void *o);
  631. /**
  632. * Function called to resolve a variable within a backend instance.
  633. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  634. * This function must not have any side effects.
  635. *
  636. * @param o see {@link NCDModuleInst_Backend_SetUser}
  637. * @param name name of the variable to resolve
  638. * @param mem value memory to use
  639. * @param out on success, the backend should initialize the value here
  640. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  641. * and an invalid value.
  642. */
  643. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValMem *mem, NCDValRef *out);
  644. /**
  645. * Function called to resolve an object within a backend instance.
  646. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  647. * This function must not have any side effects.
  648. *
  649. * @param o see {@link NCDModuleInst_Backend_SetUser}
  650. * @param name name of the object to resolve
  651. * @param out_object the object will be returned here
  652. * @return 1 on success, 0 on failure
  653. */
  654. typedef int (*NCDModule_func_getobj) (void *o, const char *name, NCDObject *out_object);
  655. /**
  656. * Handler called when the module instance is in a clean state.
  657. * This means that all statements preceding it in the process are
  658. * up, this statement is down, and all following statements are
  659. * uninitialized. When a backend instance goes down, it is guaranteed,
  660. * as long as it stays down, that either this will be called or
  661. * termination will be requested with {@link NCDModule_func_die}.
  662. * The backend instance was in down state.
  663. *
  664. * @param o see {@link NCDModuleInst_Backend_SetUser}
  665. */
  666. typedef void (*NCDModule_func_clean) (void *o);
  667. #define NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN (1 << 0)
  668. /**
  669. * Structure encapsulating the implementation of a module backend.
  670. */
  671. struct NCDModule {
  672. /**
  673. * If this implements a plain statement, the name of the statement.
  674. * If this implements a method, then "base_type::method_name".
  675. */
  676. const char *type;
  677. /**
  678. * The base type for methods operating on instances of this backend.
  679. * Any module with type of form "base_type::method_name" is considered
  680. * a method of instances of this backend.
  681. * If this is NULL, the base type will default to type.
  682. */
  683. const char *base_type;
  684. /**
  685. * Function called to create an new backend instance.
  686. * Only one of the two possible init functions must be set.
  687. */
  688. NCDModule_func_new func_new;
  689. /**
  690. * Function called to create an new backend instance, to be used with memory
  691. * preallocation.
  692. * Only one of the two possible init functions must be set.
  693. */
  694. NCDModule_func_new2 func_new2;
  695. /**
  696. * Function called to request termination of a backend instance.
  697. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  698. */
  699. NCDModule_func_die func_die;
  700. /**
  701. * Function called to resolve a variable within the backend instance.
  702. * May be NULL.
  703. */
  704. NCDModule_func_getvar func_getvar;
  705. /**
  706. * Function called to resolve an object within the backend instance.
  707. * May be NULL.
  708. */
  709. NCDModule_func_getobj func_getobj;
  710. /**
  711. * Function called when the backend instance is in a clean state.
  712. * May be NULL.
  713. */
  714. NCDModule_func_clean func_clean;
  715. /**
  716. * Various flags.
  717. *
  718. * - NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  719. * Whether the interpreter is allowed to call func_getvar and func_getobj
  720. * even when the backend instance is in down state (as opposed to just
  721. * in up state.
  722. */
  723. int flags;
  724. /**
  725. * The amount of memory to preallocate for each module instance.
  726. * Preallocation can be used to avoid having to allocate memory from
  727. * module initialization. To make use of preallocated memory, use the
  728. * {@link NCDModule_func_new2} variant of the init function.
  729. * The memory will be available from the point the init function is
  730. * called to the point the instance calls {@link NCDModuleInst_Backend_Dead}.
  731. * If alloc_size is >0, there is no need to call
  732. * {@link NCDModuleInst_Backend_SetUser}, as the the user argument will
  733. * automatically be set to a pointer to the preallocated memory.
  734. */
  735. int alloc_size;
  736. };
  737. /**
  738. * Structure encapsulating a group of module backend implementations,
  739. * with global init and free functions.
  740. */
  741. struct NCDModuleGroup {
  742. /**
  743. * Function called before any instance of any module backend in this
  744. * group is crated. May be NULL.
  745. */
  746. NCDModule_func_globalinit func_globalinit;
  747. /**
  748. * Function called to clean up after {@link NCDModule_func_globalinit}.
  749. * May be NULL.
  750. */
  751. NCDModule_func_globalfree func_globalfree;
  752. /**
  753. * Array of module backends. The array must be terminated with a
  754. * structure that has a NULL type member.
  755. */
  756. const struct NCDModule *modules;
  757. };
  758. #endif