NCDModule.h 32 KB

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