NCDModule.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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/BLog.h>
  34. #include <system/BProcess.h>
  35. #include <udevmonitor/NCDUdevManager.h>
  36. #include <random/BRandom2.h>
  37. #include <ncd/NCDObject.h>
  38. #include <ncd/NCDStringIndex.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 inst the module instance
  70. * @param event event number
  71. */
  72. typedef void (*NCDModuleInst_func_event) (struct NCDModuleInst_s *inst, 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 inst the module instance
  80. * @param name name of the object as an {@link NCDStringIndex} identifier
  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) (struct NCDModuleInst_s *inst, NCD_string_id_t 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 p handle for the new process backend
  99. * @param template_name name of the template to create the process from
  100. * @return 1 on success, 0 on failure
  101. */
  102. typedef int (*NCDModuleInst_func_initprocess) (struct NCDModuleProcess_s *p, const char *template_name);
  103. /**
  104. * Function called when the module instance wants the interpreter to
  105. * initiate termination, as if it received an external terminatio request (signal).
  106. *
  107. * @param exit_code exit code to return the the operating system. This overrides any previously
  108. * set exit code, and will be overriden by a signal to the value 1.
  109. *
  110. */
  111. typedef void (*NCDModuleInst_func_interp_exit) (int exit_code);
  112. /**
  113. * Function called when the module instance wants the interpreter to
  114. * provide its extra command line arguments.
  115. *
  116. * @param mem value memory to use
  117. * @param out_value write value reference here on success
  118. * @return 1 if available, 0 if not available. If available, but out of memory, return 1
  119. * and an invalid value.
  120. */
  121. typedef int (*NCDModuleInst_func_interp_getargs) (NCDValMem *mem, NCDValRef *out_value);
  122. /**
  123. * Function called when the module instance wants the interpreter to
  124. * provide its retry time.
  125. *
  126. * @return retry time in milliseconds
  127. */
  128. typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void);
  129. #define NCDMODULEPROCESS_EVENT_UP 1
  130. #define NCDMODULEPROCESS_EVENT_DOWN 2
  131. #define NCDMODULEPROCESS_EVENT_TERMINATED 3
  132. /**
  133. * Handler which reports process state changes from the interpreter.
  134. * Possible events are:
  135. *
  136. * - NCDMODULEPROCESS_EVENT_UP: the process went up.
  137. * The process was in down state.
  138. * The process enters up state.
  139. *
  140. * - NCDMODULEPROCESS_EVENT_DOWN: the process went down.
  141. * The process was in up state.
  142. * The process enters waiting state.
  143. *
  144. * NOTE: the process enters waiting state, NOT down state, and is paused.
  145. * To allow the process to continue, call {@link NCDModuleProcess_Continue}.
  146. *
  147. * - NCDMODULEPROCESS_EVENT_TERMINATED: the process terminated.
  148. * The process was in terminating state.
  149. * The process enters terminated state.
  150. *
  151. * @param user as in {@link NCDModuleProcess_Init}
  152. * @param event event number
  153. */
  154. typedef void (*NCDModuleProcess_handler_event) (void *user, int event);
  155. /**
  156. * Function called when the interpreter wants to resolve a special
  157. * object in the process.
  158. * This function must have no side effects.
  159. *
  160. * @param user as in {@link NCDModuleProcess_Init}
  161. * @param name name of the object as an {@link NCDStringIndex} identifier
  162. * @param out_object the object will be returned here
  163. * @return 1 on success, 0 on failure
  164. */
  165. typedef int (*NCDModuleProcess_func_getspecialobj) (void *user, NCD_string_id_t name, NCDObject *out_object);
  166. #define NCDMODULEPROCESS_INTERP_EVENT_CONTINUE 1
  167. #define NCDMODULEPROCESS_INTERP_EVENT_TERMINATE 2
  168. /**
  169. * Function called to report process backend requests to the interpreter.
  170. * Possible events are:
  171. *
  172. * - NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: the process can continue.
  173. * The process backend was in waiting state.
  174. * The process backend enters down state.
  175. *
  176. * - NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: the process should terminate.
  177. * The process backend was in down, up or waiting state.
  178. * The process backend enters terminating state.
  179. *
  180. * The interpreter should call {@link NCDModuleProcess_Interp_Terminated}
  181. * when the process terminates.
  182. *
  183. * This function is not being called in event context. The interpreter should
  184. * only update its internal state, and visibly react only via jobs that it pushes
  185. * from within this function.
  186. *
  187. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  188. * @param event event number
  189. */
  190. typedef void (*NCDModuleProcess_interp_func_event) (void *user, int event);
  191. /**
  192. * Function called to have the interpreter resolve an object within the process
  193. * of a process backend.
  194. * This function must not have any side effects.
  195. *
  196. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  197. * @param name name of the object as an {@link NCDStringIndex} identifier
  198. * @param out_object the object will be returned here
  199. * @return 1 on success, 0 in failure
  200. */
  201. typedef int (*NCDModuleProcess_interp_func_getobj) (void *user, NCD_string_id_t name, NCDObject *out_object);
  202. struct NCDModule;
  203. /**
  204. * Contains parameters to the module initialization function
  205. * ({@link NCDModule_func_new2}) that are passed indirectly.
  206. */
  207. struct NCDModuleInst_new_params {
  208. /**
  209. * A reference to the argument list for the module instance.
  210. * The reference remains valid as long as the backend instance
  211. * exists.
  212. */
  213. NCDValRef args;
  214. /**
  215. * If the module instance corresponds to a method-like statement,
  216. * this pointer identifies the object it is being invoked with.
  217. * If the object is a statement (i.e. a {@link NCDModuleInst}), then this
  218. * will be the NCDModuleInst pointer, and {@link NCDModuleInst_Backend_GetUser}
  219. * can be called on this to retrieve the user pointer of the object.
  220. * On the other hand, if this is a method on an internal object built using
  221. * only {@link NCDObject_Build} or {@link NCDObject_Build2}, this pointer will
  222. * be whatever was passed as the "user" argument to those functions.
  223. */
  224. void *method_user;
  225. };
  226. /**
  227. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  228. * This itself only contains parameters related to communication between the
  229. * backend and the creator of the module instance; other parameters are passed
  230. * via the iparams member;
  231. */
  232. struct NCDModuleInst_params {
  233. /**
  234. * Callback to report state changes.
  235. */
  236. NCDModuleInst_func_event func_event;
  237. /**
  238. * Callback to resolve objects from the viewpoint of the instance.
  239. */
  240. NCDModuleInst_func_getobj func_getobj;
  241. /**
  242. * Log function which appends a log prefix with {@link BLog_Append}.
  243. */
  244. BLog_logfunc logfunc;
  245. /**
  246. * Pointer to an {@link NCDModuleInst_iparams} structure, which exposes
  247. * services provided by the interpreter.
  248. */
  249. const struct NCDModuleInst_iparams *iparams;
  250. };
  251. /**
  252. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  253. * This only contains parameters related to services provided by the interpreter.
  254. */
  255. struct NCDModuleInst_iparams {
  256. /**
  257. * Reactor we live in.
  258. */
  259. BReactor *reactor;
  260. /**
  261. * Process manager.
  262. */
  263. BProcessManager *manager;
  264. /**
  265. * Udev manager.
  266. */
  267. NCDUdevManager *umanager;
  268. /**
  269. * Random number generator.
  270. */
  271. BRandom2 *random2;
  272. /**
  273. * String index which keeps a mapping between strings and string identifiers.
  274. */
  275. NCDStringIndex *string_index;
  276. /**
  277. * Callback to create a new template process.
  278. */
  279. NCDModuleInst_func_initprocess func_initprocess;
  280. /**
  281. * Callback to request interpreter termination.
  282. */
  283. NCDModuleInst_func_interp_exit func_interp_exit;
  284. /**
  285. * Callback to get extra command line arguments.
  286. */
  287. NCDModuleInst_func_interp_getargs func_interp_getargs;
  288. /**
  289. * Callback to get retry time.
  290. */
  291. NCDModuleInst_func_interp_getretrytime func_interp_getretrytime;
  292. };
  293. /**
  294. * Module instance.
  295. * The module instance is initialized by the interpreter by calling
  296. * {@link NCDModuleInst_Init}. It is implemented by a module backend
  297. * specified in a {@link NCDModule}.
  298. */
  299. typedef struct NCDModuleInst_s {
  300. const struct NCDModule *m;
  301. const struct NCDModuleInst_params *params;
  302. void *inst_user;
  303. int state;
  304. int is_error;
  305. DebugObject d_obj;
  306. } NCDModuleInst;
  307. /**
  308. * Process created from a process template on behalf of a module backend
  309. * instance, implemented by the interpreter.
  310. */
  311. typedef struct NCDModuleProcess_s {
  312. NCDValRef args;
  313. void *user;
  314. NCDModuleProcess_handler_event handler_event;
  315. const struct NCDModuleInst_iparams *iparams; // TODO remove
  316. NCDModuleProcess_func_getspecialobj func_getspecialobj;
  317. int state;
  318. void *interp_user;
  319. NCDModuleProcess_interp_func_event interp_func_event;
  320. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  321. DebugObject d_obj;
  322. } NCDModuleProcess;
  323. /**
  324. * Initializes an instance of an NCD module.
  325. * The instance is initialized in down state.
  326. * WARNING: this directly calls the module backend; expect to be called back
  327. *
  328. * This and other non-Backend methods are the interpreter interface.
  329. * The Backend methods are the module backend interface and are documented
  330. * independently with their own logical states.
  331. *
  332. * @param n the instance
  333. * @param m structure of module functions implementing the module backend
  334. * @param mem preallocated memory for the instance. If m->prealloc_size == 0, this must be NULL;
  335. * if it is >0, it must point to a block of memory with at least that many bytes available,
  336. * and properly aligned for any object.
  337. * @param method_object the base object if the module being initialized is a method, otherwise NULL.
  338. * The caller must ensure that this object is of the type expected by the module
  339. * being initialized.
  340. * @param args arguments to the module. Must be a list value. Must be available and unchanged
  341. * as long as the instance exists.
  342. * @param user argument to callback functions
  343. * @param params more parameters, see {@link NCDModuleInst_params}
  344. */
  345. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem, const NCDObject *method_object, NCDValRef args, const struct NCDModuleInst_params *params);
  346. /**
  347. * Frees the instance.
  348. * The instance must be in dead state.
  349. *
  350. * @param n the instance
  351. */
  352. void NCDModuleInst_Free (NCDModuleInst *n);
  353. /**
  354. * Requests the instance to die.
  355. * The instance must be in down or up state.
  356. * The instance enters dying state.
  357. * WARNING: this directly calls the module backend; expect to be called back
  358. *
  359. * @param n the instance
  360. */
  361. void NCDModuleInst_Die (NCDModuleInst *n);
  362. /**
  363. * Informs the module that it is in a clean state to proceed.
  364. * The instance must be in down state.
  365. * WARNING: this directly calls the module backend; expect to be called back
  366. *
  367. * @param n the instance
  368. */
  369. void NCDModuleInst_Clean (NCDModuleInst *n);
  370. /**
  371. * Returns an {@link NCDObject} which can be used to resolve variables and objects
  372. * within this instance, as well as call its methods. The resulting object may only
  373. * be used immediately, and becomes invalid when the instance is freed.
  374. *
  375. * @param n the instance
  376. * @return an NCDObject for this instance
  377. */
  378. NCDObject NCDModuleInst_Object (NCDModuleInst *n);
  379. /**
  380. * Checks whether the module terminated unsuccessfully.
  381. * The instance must be in dead state.
  382. *
  383. * @param n the instance
  384. * @return 1 if module terminated unsuccessfully, 0 if successfully
  385. */
  386. int NCDModuleInst_HaveError (NCDModuleInst *n);
  387. /**
  388. * Sets the argument passed to handlers of a module backend instance.
  389. * If the alloc_size member in the {@link NCDModule} structure is >0,
  390. * the argument will automatically be set to the preallocated memory,
  391. * in which case there is no need to call this.
  392. *
  393. * @param n backend instance handle
  394. * @param user value to pass to future handlers for this backend instance
  395. */
  396. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  397. /**
  398. * Retuns the argument passed to handlers of a module backend instance;
  399. * see {@link NCDModuleInst_Backend_SetUser}.
  400. *
  401. * @param n backend instance handle
  402. * @return argument passed to handlers
  403. */
  404. void * NCDModuleInst_Backend_GetUser (NCDModuleInst *n);
  405. /**
  406. * Puts the backend instance into up state.
  407. * The instance must be in down state.
  408. * The instance enters up state.
  409. *
  410. * @param n backend instance handle
  411. */
  412. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  413. /**
  414. * Puts the backend instance into down state.
  415. * The instance must be in up state.
  416. * The instance enters down state.
  417. *
  418. * @param n backend instance handle
  419. */
  420. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  421. /**
  422. * Destroys the backend instance.
  423. * The backend instance handle becomes invalid and must not be used from
  424. * the backend any longer.
  425. *
  426. * @param n backend instance handle
  427. */
  428. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  429. /**
  430. * Resolves an object for a backend instance, from the point of the instance's
  431. * statement in the containing process.
  432. *
  433. * @param n backend instance handle
  434. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  435. * @param out_object the object will be returned here
  436. * @return 1 on success, 0 on failure
  437. */
  438. int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  439. /**
  440. * Logs a backend instance message.
  441. *
  442. * @param n backend instance handle
  443. * @param channel log channel
  444. * @param level loglevel
  445. * @param fmt format string as in printf, arguments follow
  446. */
  447. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  448. /**
  449. * Sets the error flag for the module instance.
  450. * The error flag only has no effect until the backend calls
  451. * {@link NCDModuleInst_Backend_Dead}.
  452. *
  453. * @param n backend instance handle
  454. */
  455. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  456. /**
  457. * Initiates interpreter termination.
  458. *
  459. * @param n backend instance handle
  460. * @param exit_code exit code to return to the operating system. This overrides
  461. * any previously set exit code, and will be overriden by a
  462. * termination signal to the value 1.
  463. */
  464. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  465. /**
  466. * Retrieves extra command line arguments passed to the interpreter.
  467. *
  468. * @param n backend instance handle
  469. * @param mem value memory to use
  470. * @param out_value the arguments will be written here on success as a list value
  471. * @return 1 if available, 0 if not available. If available, but out of memory, returns 1
  472. * and an invalid value.
  473. */
  474. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value);
  475. /**
  476. * Returns the retry time of the intepreter.
  477. *
  478. * @param n backend instance handle
  479. * @return retry time in milliseconds
  480. */
  481. btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  482. /**
  483. * Initializes a process in the interpreter from a process template.
  484. * This must be called on behalf of a module backend instance.
  485. * The process is initializes in down state.
  486. *
  487. * @param o the process
  488. * @param n backend instance whose interpreter will be providing the process
  489. * @param template_name name of the process template
  490. * @param args arguments to the process. Must be an invalid value or a list value.
  491. * The value must be available and unchanged while the process exists.
  492. * @param user argument to handlers
  493. * @param handler_event handler which reports events about the process from the
  494. * interpreter
  495. * @return 1 on success, 0 on failure
  496. */
  497. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  498. /**
  499. * Frees the process.
  500. * The process must be in terminated state.
  501. *
  502. * @param o the process
  503. */
  504. void NCDModuleProcess_Free (NCDModuleProcess *o);
  505. /**
  506. * Does nothing.
  507. * The process must be in terminated state.
  508. *
  509. * @param o the process
  510. */
  511. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  512. /**
  513. * Sets callback functions for providing special objects within the process.
  514. *
  515. * @param o the process
  516. * @param func_getspecialobj function for resolving special objects, or NULL
  517. */
  518. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  519. /**
  520. * Continues the process after the process went down.
  521. * The process must be in waiting state.
  522. * The process enters down state.
  523. *
  524. * @param o the process
  525. */
  526. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  527. /**
  528. * Requests the process to terminate.
  529. * The process must be in down, up or waiting state.
  530. * The process enters terminating state.
  531. *
  532. * @param o the process
  533. */
  534. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  535. /**
  536. * Resolves an object within the process from the point
  537. * at the end of the process.
  538. * This function has no side effects.
  539. *
  540. * @param o the process
  541. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  542. * @param out_object the object will be returned here
  543. * @return 1 on success, 0 on failure
  544. */
  545. int NCDModuleProcess_GetObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  546. /**
  547. * Sets callback functions for the interpreter to implement the
  548. * process backend.
  549. * Must be called from within {@link NCDModuleInst_func_initprocess}
  550. * if success is to be reported there.
  551. *
  552. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  553. * @param interp_user argument to callback functions
  554. * @param interp_func_event function for reporting continue/terminate requests
  555. * @param interp_func_getobj function for resolving objects within the process
  556. */
  557. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  558. NCDModuleProcess_interp_func_event interp_func_event,
  559. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  560. /**
  561. * Reports the process backend as up.
  562. * The process backend must be in down state.
  563. * The process backend enters up state.
  564. * WARNING: this directly calls the process creator; expect to be called back
  565. *
  566. * @param o process backend handle
  567. */
  568. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  569. /**
  570. * Reports the process backend as down.
  571. * The process backend must be in up state.
  572. * The process backend enters waiting state.
  573. * WARNING: this directly calls the process creator; expect to be called back
  574. *
  575. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  576. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  577. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  578. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  579. *
  580. * @param o process backend handle
  581. */
  582. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  583. /**
  584. * Reports termination of the process backend.
  585. * The process backend must be in terminating state.
  586. * The process backend handle becomes invalid and must not be used
  587. * by the interpreter any longer.
  588. * WARNING: this directly calls the process creator; expect to be called back
  589. *
  590. * @param o process backend handle
  591. */
  592. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  593. /**
  594. * Resolves a special process object for the process backend.
  595. *
  596. * @param o process backend handle
  597. * @param name name of the object as an {@link NCDStringIndex} identifier
  598. * @param out_object the object will be returned here
  599. * @return 1 on success, 0 on failure
  600. */
  601. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  602. /**
  603. * Function called before any instance of any backend in a module
  604. * group is created;
  605. *
  606. * @param params structure containing global resources, such as
  607. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}.
  608. * @return 1 on success, 0 on failure
  609. */
  610. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInst_iparams *params);
  611. /**
  612. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  613. * in a module group.
  614. * There are no backend instances alive from this module group.
  615. */
  616. typedef void (*NCDModule_func_globalfree) (void);
  617. /**
  618. * Handler called to create an new module backend instance.
  619. * The backend is initialized in down state.
  620. *
  621. * If the backend fails initialization, this function should report the backend
  622. * instance to have died with error by calling {@link NCDModuleInst_Backend_SetError}
  623. * and {@link NCDModuleInst_Backend_Dead}.
  624. *
  625. * @param o if the module specifies a positive alloc_size value in the {@link NCDModule}
  626. * structure, this will point to the allocated memory that can be used by the
  627. * module instance while it exists. Otherwise, it will be NULL.
  628. * @param i module backend instance handler. The backend may only use this handle via
  629. * the Backend functions of {@link NCDModuleInst}.
  630. */
  631. typedef void (*NCDModule_func_new2) (void *o, NCDModuleInst *i, const struct NCDModuleInst_new_params *params);
  632. /**
  633. * Handler called to request termination of a backend instance.
  634. * The backend instance was in down or up state.
  635. * The backend instance enters dying state.
  636. *
  637. * @param o see {@link NCDModuleInst_Backend_SetUser}
  638. */
  639. typedef void (*NCDModule_func_die) (void *o);
  640. /**
  641. * Function called to resolve a variable within a backend instance.
  642. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  643. * This function must not have any side effects.
  644. *
  645. * @param o see {@link NCDModuleInst_Backend_SetUser}
  646. * @param name name of the variable to resolve
  647. * @param mem value memory to use
  648. * @param out on success, the backend should initialize the value here
  649. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  650. * and an invalid value.
  651. */
  652. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValMem *mem, NCDValRef *out);
  653. /**
  654. * Function called to resolve a variable within a backend instance.
  655. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  656. * This function must not have any side effects.
  657. *
  658. * @param o see {@link NCDModuleInst_Backend_SetUser}
  659. * @param name name of the variable to resolve as an {@link NCDStringIndex} identifier
  660. * @param mem value memory to use
  661. * @param out on success, the backend should initialize the value here
  662. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  663. * and an invalid value.
  664. */
  665. typedef int (*NCDModule_func_getvar2) (void *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  666. /**
  667. * Function called to resolve an object within a backend instance.
  668. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  669. * This function must not have any side effects.
  670. *
  671. * @param o see {@link NCDModuleInst_Backend_SetUser}
  672. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  673. * @param out_object the object will be returned here
  674. * @return 1 on success, 0 on failure
  675. */
  676. typedef int (*NCDModule_func_getobj) (void *o, NCD_string_id_t name, NCDObject *out_object);
  677. /**
  678. * Handler called when the module instance is in a clean state.
  679. * This means that all statements preceding it in the process are
  680. * up, this statement is down, and all following statements are
  681. * uninitialized. When a backend instance goes down, it is guaranteed,
  682. * as long as it stays down, that either this will be called or
  683. * termination will be requested with {@link NCDModule_func_die}.
  684. * The backend instance was in down state.
  685. *
  686. * @param o see {@link NCDModuleInst_Backend_SetUser}
  687. */
  688. typedef void (*NCDModule_func_clean) (void *o);
  689. #define NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN (1 << 0)
  690. /**
  691. * Structure encapsulating the implementation of a module backend.
  692. */
  693. struct NCDModule {
  694. /**
  695. * If this implements a plain statement, the name of the statement.
  696. * If this implements a method, then "base_type::method_name".
  697. */
  698. const char *type;
  699. /**
  700. * The base type for methods operating on instances of this backend.
  701. * Any module with type of form "base_type::method_name" is considered
  702. * a method of instances of this backend.
  703. * If this is NULL, the base type will default to type.
  704. */
  705. const char *base_type;
  706. /**
  707. * Function called to create an new backend instance.
  708. */
  709. NCDModule_func_new2 func_new2;
  710. /**
  711. * Function called to request termination of a backend instance.
  712. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  713. */
  714. NCDModule_func_die func_die;
  715. /**
  716. * Function called to resolve a variable within the backend instance.
  717. * May be NULL.
  718. */
  719. NCDModule_func_getvar func_getvar;
  720. /**
  721. * Function called to resolve a variable within the backend instance.
  722. * May be NULL.
  723. */
  724. NCDModule_func_getvar2 func_getvar2;
  725. /**
  726. * Function called to resolve an object within the backend instance.
  727. * May be NULL.
  728. */
  729. NCDModule_func_getobj func_getobj;
  730. /**
  731. * Function called when the backend instance is in a clean state.
  732. * May be NULL.
  733. */
  734. NCDModule_func_clean func_clean;
  735. /**
  736. * Various flags.
  737. *
  738. * - NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  739. * Whether the interpreter is allowed to call func_getvar and func_getobj
  740. * even when the backend instance is in down state (as opposed to just
  741. * in up state.
  742. */
  743. int flags;
  744. /**
  745. * The amount of memory to preallocate for each module instance.
  746. * Preallocation can be used to avoid having to allocate memory from
  747. * module initialization. The memory can be accessed via the first
  748. * argument to {@link NCDModule_func_new2} and other calls (except if
  749. * the callback pointer is changed subsequently using
  750. * {@link NCDModuleInst_Backend_SetUser}).
  751. */
  752. int alloc_size;
  753. /**
  754. * The string identifier of this module's base_type (or type if base_type is
  755. * not specified) according to {@link NCDStringIndex}. This is initialized
  756. * by the interpreter on startup and should not be set by the module.
  757. */
  758. NCD_string_id_t base_type_id;
  759. };
  760. /**
  761. * Structure encapsulating a group of module backend implementations,
  762. * with global init and free functions.
  763. */
  764. struct NCDModuleGroup {
  765. /**
  766. * Function called before any instance of any module backend in this
  767. * group is crated. May be NULL.
  768. */
  769. NCDModule_func_globalinit func_globalinit;
  770. /**
  771. * Function called to clean up after {@link NCDModule_func_globalinit}.
  772. * May be NULL.
  773. */
  774. NCDModule_func_globalfree func_globalfree;
  775. /**
  776. * Array of module backends. The array must be terminated with a
  777. * structure that has a NULL type member.
  778. */
  779. struct NCDModule *modules;
  780. /**
  781. * A pointer to an array of requests for string identifiers. The 'str'
  782. * member of each element of this array should be set to a string which
  783. * is to be mapped to an identifier using {@link NCDStringIndex}. The
  784. * array must be terminated with an element with a NULL 'str' member.
  785. * The interpreter will use {@link NCDStringIndex_GetRequests} to set
  786. * the corresponds 'id' members.
  787. * This can be NULL if the module does not require mapping any strings.
  788. */
  789. struct NCD_string_request *strings;
  790. };
  791. #endif