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