NCDModule.h 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 <stdarg.h>
  32. #include <misc/debug.h>
  33. #include <system/BReactor.h>
  34. #include <base/BLog.h>
  35. #include <ncd/NCDObject.h>
  36. #include <ncd/NCDStringIndex.h>
  37. #include <ncd/NCDEvaluator.h>
  38. #ifndef BADVPN_NO_PROCESS
  39. #include <system/BProcess.h>
  40. #endif
  41. #ifndef BADVPN_NO_UDEV
  42. #include <udevmonitor/NCDUdevManager.h>
  43. #endif
  44. #ifndef BADVPN_NO_RANDOM
  45. #include <random/BRandom2.h>
  46. #endif
  47. #define NCDMODULE_EVENT_UP 1
  48. #define NCDMODULE_EVENT_DOWN 2
  49. #define NCDMODULE_EVENT_DOWNUP 3
  50. #define NCDMODULE_EVENT_DEAD 4
  51. #define NCDMODULE_EVENT_DEADERROR 5
  52. struct NCDModuleInst_s;
  53. struct NCDModuleProcess_s;
  54. struct NCDModuleGroup;
  55. struct NCDInterpModule;
  56. struct NCDInterpModuleGroup;
  57. struct NCDInterpFunction;
  58. /**
  59. * Function called to inform the interpeter of state changes of the
  60. * module instance.
  61. * Possible events are:
  62. *
  63. * - NCDMODULE_EVENT_UP: the instance came up.
  64. * The instance was in down state.
  65. * The instance enters up state.
  66. *
  67. * - NCDMODULE_EVENT_DOWN: the instance went down.
  68. * The instance was in up state.
  69. * The instance enters down state.
  70. *
  71. * After the instance goes down, the interpreter should eventually call
  72. * {@link NCDModuleInst_Clean} or {@link NCDModuleInst_Die}, unless
  73. * the module goes up again.
  74. *
  75. * - NCDMODULE_EVENT_DEAD: the module died. To determine if the module
  76. * died with error, read the is_error member of {@link NCDModuleInst}.
  77. * The instance enters dead state.
  78. *
  79. * This function is not being called in event context. The interpreter should
  80. * only update its internal state, and visibly react only via jobs that it pushes
  81. * from within this function. The only exception is that it may free the
  82. * instance from within the NCDMODULE_EVENT_DEAD event.
  83. *
  84. * @param inst the module instance
  85. * @param event event number
  86. */
  87. typedef void (*NCDModuleInst_func_event) (struct NCDModuleInst_s *inst, int event);
  88. /**
  89. * Function called when the module instance wants the interpreter to
  90. * resolve an object from the point of view of its statement.
  91. * The instance will not be in dead state.
  92. * This function must not have any side effects.
  93. *
  94. * @param inst the module instance
  95. * @param name name of the object as an {@link NCDStringIndex} identifier
  96. * @param out_object the object will be returned here
  97. * @return 1 on success, 0 on failure
  98. */
  99. typedef int (*NCDModuleInst_func_getobj) (struct NCDModuleInst_s *inst, NCD_string_id_t name, NCDObject *out_object);
  100. /**
  101. * Function called when the module instance wants the interpreter to
  102. * create a new process backend from a process template.
  103. * The instance will not be in dead state.
  104. *
  105. * On success, the interpreter must have called {@link NCDModuleProcess_Interp_SetHandlers}
  106. * from within this function, to allow communication with the controller of the process.
  107. * On success, the new process backend enters down state.
  108. *
  109. * This function is not being called in event context. The interpreter should
  110. * only update its internal state, and visibly react only via jobs that it pushes
  111. * from within this function.
  112. *
  113. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  114. * @param p handle for the new process backend
  115. * @param template_name name of the template to create the process from,
  116. * as an {@link NCDStringIndex} identifier
  117. * @return 1 on success, 0 on failure
  118. */
  119. typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, NCD_string_id_t template_name);
  120. /**
  121. * Function called when the module instance wants the interpreter to
  122. * initiate termination, as if it received an external terminatio request (signal).
  123. *
  124. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  125. * @param exit_code exit code to return the the operating system. This overrides any previously
  126. * set exit code, and will be overriden by a signal to the value 1.
  127. *
  128. */
  129. typedef void (*NCDModuleInst_func_interp_exit) (void *user, int exit_code);
  130. /**
  131. * Function called when the module instance wants the interpreter to
  132. * provide its extra command line arguments.
  133. *
  134. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  135. * @param mem value memory to use
  136. * @param out_value write value reference here on success
  137. * @return 1 if available, 0 if not available. If available, but out of memory, return 1
  138. * and an invalid value.
  139. */
  140. typedef int (*NCDModuleInst_func_interp_getargs) (void *user, NCDValMem *mem, NCDValRef *out_value);
  141. /**
  142. * Function called when the module instance wants the interpreter to
  143. * provide its retry time.
  144. *
  145. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  146. * @return retry time in milliseconds
  147. */
  148. typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void *user);
  149. /**
  150. * Function called when the module instance wants the interpreter to
  151. * load a new module group.
  152. *
  153. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  154. * @param group module group to load
  155. * @return 1 on success, 0 on failure
  156. */
  157. typedef int (*NCDModuleInst_func_interp_loadgroup) (void *user, const struct NCDModuleGroup *group);
  158. #define NCDMODULEPROCESS_EVENT_UP 1
  159. #define NCDMODULEPROCESS_EVENT_DOWN 2
  160. #define NCDMODULEPROCESS_EVENT_TERMINATED 3
  161. /**
  162. * Handler which reports process state changes from the interpreter.
  163. * Possible events are:
  164. *
  165. * - NCDMODULEPROCESS_EVENT_UP: the process went up.
  166. * The process was in down state.
  167. * The process enters up state.
  168. *
  169. * - NCDMODULEPROCESS_EVENT_DOWN: the process went down.
  170. * The process was in up state.
  171. * The process enters waiting state.
  172. *
  173. * NOTE: the process enters waiting state, NOT down state, and is paused.
  174. * To allow the process to continue, call {@link NCDModuleProcess_Continue}.
  175. *
  176. * - NCDMODULEPROCESS_EVENT_TERMINATED: the process terminated.
  177. * The process was in terminating state.
  178. * The process enters terminated state.
  179. *
  180. * @param user pointer to the process. Use {@link UPPER_OBJECT} to retrieve the pointer
  181. * to the containing struct.
  182. * @param event event number
  183. */
  184. typedef void (*NCDModuleProcess_handler_event) (struct NCDModuleProcess_s *process, int event);
  185. /**
  186. * Function called when the interpreter wants to resolve a special
  187. * object in the process.
  188. * This function must have no side effects.
  189. *
  190. * @param user pointer to the process. Use {@link UPPER_OBJECT} to retrieve the pointer
  191. * to the containing struct.
  192. * @param name name of the object as an {@link NCDStringIndex} identifier
  193. * @param out_object the object will be returned here
  194. * @return 1 on success, 0 on failure
  195. */
  196. typedef int (*NCDModuleProcess_func_getspecialobj) (struct NCDModuleProcess_s *process, NCD_string_id_t name, NCDObject *out_object);
  197. #define NCDMODULEPROCESS_INTERP_EVENT_CONTINUE 1
  198. #define NCDMODULEPROCESS_INTERP_EVENT_TERMINATE 2
  199. /**
  200. * Function called to report process backend requests to the interpreter.
  201. * Possible events are:
  202. *
  203. * - NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: the process can continue.
  204. * The process backend was in waiting state.
  205. * The process backend enters down state.
  206. *
  207. * - NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: the process should terminate.
  208. * The process backend was in down, up or waiting state.
  209. * The process backend enters terminating state.
  210. *
  211. * The interpreter should call {@link NCDModuleProcess_Interp_Terminated}
  212. * when the process terminates.
  213. *
  214. * This function is not being called in event context. The interpreter should
  215. * only update its internal state, and visibly react only via jobs that it pushes
  216. * from within this function.
  217. *
  218. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  219. * @param event event number
  220. */
  221. typedef void (*NCDModuleProcess_interp_func_event) (void *user, int event);
  222. /**
  223. * Function called to have the interpreter resolve an object within the process
  224. * of a process backend.
  225. * This function must not have any side effects.
  226. *
  227. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  228. * @param name name of the object as an {@link NCDStringIndex} identifier
  229. * @param out_object the object will be returned here
  230. * @return 1 on success, 0 in failure
  231. */
  232. typedef int (*NCDModuleProcess_interp_func_getobj) (void *user, NCD_string_id_t name, NCDObject *out_object);
  233. struct NCDModule;
  234. /**
  235. * Contains parameters to the module initialization function
  236. * ({@link NCDModule_func_new2}) that are passed indirectly.
  237. */
  238. struct NCDModuleInst_new_params {
  239. /**
  240. * A reference to the argument list for the module instance.
  241. * The reference remains valid as long as the backend instance
  242. * exists. Unless the module has the NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  243. * flag set, it is guaranteed that any strings within the arguments will be
  244. * some kind of ContinuousString.
  245. */
  246. NCDValRef args;
  247. /**
  248. * If the module instance corresponds to a method-like statement,
  249. * this pointer identifies the object it is being invoked with.
  250. * If the object is a statement (i.e. a {@link NCDModuleInst}), then this
  251. * will be the NCDModuleInst pointer, and {@link NCDModuleInst_Backend_GetUser}
  252. * can be called on this to retrieve the pointer to preallocated memory for
  253. * the backend instance; *unless* {@link NCDModuleInst_Backend_PassMemToMethods}
  254. * was called for the object on which the method is being called, in which case
  255. * this will directly point to the preallocated memory.
  256. * On the other hand, if this is a method on an internal object built using
  257. * only {@link NCDObject_Build} or {@link NCDObject_BuildFull},
  258. * this pointer will be whatever was passed as the "data_ptr" argument, for the
  259. * first function, and as "method_user", for the latter function.
  260. */
  261. void *method_user;
  262. };
  263. /**
  264. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  265. * This itself only contains parameters related to communication between the
  266. * backend and the creator of the module instance; other parameters are passed
  267. * via the iparams member;
  268. */
  269. struct NCDModuleInst_params {
  270. /**
  271. * Callback to report state changes.
  272. */
  273. NCDModuleInst_func_event func_event;
  274. /**
  275. * Callback to resolve objects from the viewpoint of the instance.
  276. */
  277. NCDModuleInst_func_getobj func_getobj;
  278. /**
  279. * Log function which appends a log prefix with {@link BLog_Append}.
  280. */
  281. BLog_logfunc logfunc;
  282. /**
  283. * Pointer to an {@link NCDModuleInst_iparams} structure, which exposes
  284. * services provided by the interpreter.
  285. */
  286. const struct NCDModuleInst_iparams *iparams;
  287. };
  288. /**
  289. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  290. * This only contains parameters related to services provided by the interpreter.
  291. */
  292. struct NCDModuleInst_iparams {
  293. /**
  294. * Reactor we live in.
  295. */
  296. BReactor *reactor;
  297. #ifndef BADVPN_NO_PROCESS
  298. /**
  299. * Process manager.
  300. */
  301. BProcessManager *manager;
  302. #endif
  303. #ifndef BADVPN_NO_UDEV
  304. /**
  305. * Udev manager.
  306. */
  307. NCDUdevManager *umanager;
  308. #endif
  309. #ifndef BADVPN_NO_RANDOM
  310. /**
  311. * Random number generator.
  312. */
  313. BRandom2 *random2;
  314. #endif
  315. /**
  316. * String index which keeps a mapping between strings and string identifiers.
  317. */
  318. NCDStringIndex *string_index;
  319. /**
  320. * Pointer passed to the interpreter callbacks below, for state keeping.
  321. */
  322. void *user;
  323. /**
  324. * Callback to create a new template process.
  325. */
  326. NCDModuleInst_func_initprocess func_initprocess;
  327. /**
  328. * Callback to request interpreter termination.
  329. */
  330. NCDModuleInst_func_interp_exit func_interp_exit;
  331. /**
  332. * Callback to get extra command line arguments.
  333. */
  334. NCDModuleInst_func_interp_getargs func_interp_getargs;
  335. /**
  336. * Callback to get retry time.
  337. */
  338. NCDModuleInst_func_interp_getretrytime func_interp_getretrytime;
  339. /**
  340. * Callback to load a module group.
  341. */
  342. NCDModuleInst_func_interp_loadgroup func_loadgroup;
  343. };
  344. /**
  345. * Module instance.
  346. * The module instance is initialized by the interpreter by calling
  347. * {@link NCDModuleInst_Init}. It is implemented by a module backend
  348. * specified in a {@link NCDModule}.
  349. */
  350. typedef struct NCDModuleInst_s {
  351. const struct NCDInterpModule *m;
  352. const struct NCDModuleInst_params *params;
  353. void *mem; // not modified by NCDModuleInst (but passed to module)
  354. unsigned int state:3;
  355. unsigned int pass_mem_to_methods:1;
  356. unsigned int istate:3; // untouched by NCDModuleInst
  357. DebugObject d_obj;
  358. } NCDModuleInst;
  359. /**
  360. * Process created from a process template on behalf of a module backend
  361. * instance, implemented by the interpreter.
  362. */
  363. typedef struct NCDModuleProcess_s {
  364. NCDValRef args;
  365. NCDModuleProcess_handler_event handler_event;
  366. NCDModuleProcess_func_getspecialobj func_getspecialobj;
  367. void *interp_user;
  368. NCDModuleProcess_interp_func_event interp_func_event;
  369. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  370. #ifndef NDEBUG
  371. int state;
  372. #endif
  373. DebugObject d_obj;
  374. } NCDModuleProcess;
  375. /**
  376. * Initializes an instance of an NCD module.
  377. * The instance is initialized in down state.
  378. * WARNING: this directly calls the module backend; expect to be called back
  379. *
  380. * This and other non-Backend methods are the interpreter interface.
  381. * The Backend methods are the module backend interface and are documented
  382. * independently with their own logical states.
  383. *
  384. * NOTE: the instance structure \a n should have the member 'mem' initialized
  385. * to point to preallocated memory for the statement. This memory must be
  386. * at least m->prealloc_size big and must be properly aligned for any object.
  387. * The 'mem' pointer is never modified by NCDModuleInst, so that the interpreter
  388. * can use it as outside the lifetime of NCDModuleInst.
  389. *
  390. * @param n the instance
  391. * @param m pointer to the {@link NCDInterpModule} structure representing the module
  392. * to be instantiated
  393. * @param method_context a context pointer passed to the module backend, applicable to method-like
  394. * statements only. This should be set to the 'user' member of the
  395. * {@link NCDObject} which represents the base object for the method.
  396. * The caller must ensure that the NCDObject that was used is of the type
  397. * expected by the module being instanciated.
  398. * @param args arguments to the module. Must be a list value. Must be available and unchanged
  399. * as long as the instance exists. Unless the module has the
  400. * NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS flag set, any strings within the
  401. * arguments must be some kind of ContinuousString. This can be ensured by calling
  402. * {@link NCDValMem_ConvertNonContinuousStrings}.
  403. * @param user argument to callback functions
  404. * @param params more parameters, see {@link NCDModuleInst_params}
  405. */
  406. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDInterpModule *m, void *method_context, NCDValRef args, const struct NCDModuleInst_params *params);
  407. /**
  408. * Frees the instance.
  409. * The instance must be in dead state.
  410. *
  411. * @param n the instance
  412. */
  413. void NCDModuleInst_Free (NCDModuleInst *n);
  414. /**
  415. * Requests the instance to die.
  416. * The instance must be in down or up state.
  417. * The instance enters dying state.
  418. * WARNING: this directly calls the module backend; expect to be called back
  419. *
  420. * @param n the instance
  421. */
  422. void NCDModuleInst_Die (NCDModuleInst *n);
  423. /**
  424. * Attempts to destroy the instance immediately.
  425. * This function can be used to optimize destroying instances of modules which
  426. * don't specify any {@link NCDModule_func_die} handler. If immediate destruction
  427. * is not possible, this does nothing and returns 0; {@link NCDModuleInst_Die}
  428. * should be used to destroy the instance instead. If however immediate destruction
  429. * is possible, this destroys the module instance and returns 1; {@link NCDModuleInst_Free}
  430. * must not be called after that.
  431. * The instance must be in down or up state, as for {@link NCDModuleInst_Die}.
  432. *
  433. * @param n the instance
  434. * @return 1 if destruction was performed, 0 if not
  435. */
  436. int NCDModuleInst_TryFree (NCDModuleInst *n);
  437. /**
  438. * Informs the module that it is in a clean state to proceed.
  439. * The instance must be in down state.
  440. * WARNING: this directly calls the module backend; expect to be called back
  441. *
  442. * @param n the instance
  443. */
  444. void NCDModuleInst_Clean (NCDModuleInst *n);
  445. /**
  446. * Returns an {@link NCDObject} which can be used to resolve variables and objects
  447. * within this instance, as well as call its methods. The resulting object may only
  448. * be used immediately, and becomes invalid when the instance is freed.
  449. *
  450. * @param n the instance
  451. * @return an NCDObject for this instance
  452. */
  453. NCDObject NCDModuleInst_Object (NCDModuleInst *n);
  454. /**
  455. * If this is called, any methods called on this object will receive the preallocated
  456. * memory pointer as the object state pointer. This means that in the
  457. * {@link NCDModule_func_getvar2} function which is called when a method is created,
  458. * the preallocated memory should be accessed as params->method_user.
  459. * By default, however, params->method_user points to the NCDModuleInst of the base
  460. * object, and {@link NCDModuleInst_Backend_GetUser} is needed to retrieve the
  461. * preallocated memory pointer.
  462. */
  463. void NCDModuleInst_Backend_PassMemToMethods (NCDModuleInst *n);
  464. /**
  465. * Retuns the state pointer passed to handlers of a module backend instance;
  466. * see {@link NCDModule_func_new2}.
  467. *
  468. * @param n backend instance handle
  469. * @return argument passed to handlers
  470. */
  471. void * NCDModuleInst_Backend_GetUser (NCDModuleInst *n);
  472. /**
  473. * Puts the backend instance into up state.
  474. * The instance must be in down state.
  475. * The instance enters up state.
  476. *
  477. * @param n backend instance handle
  478. */
  479. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  480. /**
  481. * Puts the backend instance into down state.
  482. * The instance must be in up state.
  483. * The instance enters down state.
  484. *
  485. * @param n backend instance handle
  486. */
  487. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  488. /**
  489. * Puts the backend instance into down state, then immediatly back into the up state.
  490. * This effectively causes the interpreter to start backtracking to this statement.
  491. * The instance must be in up state, and remains in up state.
  492. *
  493. * @param n backend instance handle
  494. */
  495. void NCDModuleInst_Backend_DownUp (NCDModuleInst *n);
  496. /**
  497. * Destroys the backend instance.
  498. * The backend instance handle becomes invalid and must not be used from
  499. * the backend any longer.
  500. *
  501. * @param n backend instance handle
  502. */
  503. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  504. /**
  505. * Like {@link NCDModuleInst_Backend_Dead}, but also reports an error condition
  506. * to the interpreter.
  507. */
  508. void NCDModuleInst_Backend_DeadError (NCDModuleInst *n);
  509. /**
  510. * Resolves an object for a backend instance, from the point of the instance's
  511. * statement in the containing process.
  512. *
  513. * @param n backend instance handle
  514. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  515. * @param out_object the object will be returned here
  516. * @return 1 on success, 0 on failure
  517. */
  518. int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  519. /**
  520. * Logs a backend instance message.
  521. *
  522. * @param n backend instance handle
  523. * @param channel log channel
  524. * @param level loglevel
  525. * @param fmt format string as in printf, arguments follow
  526. */
  527. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  528. /**
  529. * Like {@link NCDModuleInst_Backend_Log}, but the extra arguments are passed
  530. * as a va_list. This allows creation of logging wrappers.
  531. */
  532. void NCDModuleInst_Backend_LogVarArg (NCDModuleInst *n, int channel, int level, const char *fmt, va_list vl);
  533. /**
  534. * Returns a logging context. The context is valid until the backend dies.
  535. */
  536. BLogContext NCDModuleInst_Backend_LogContext (NCDModuleInst *n);
  537. /**
  538. * Initiates interpreter termination.
  539. *
  540. * @param n backend instance handle
  541. * @param exit_code exit code to return to the operating system. This overrides
  542. * any previously set exit code, and will be overriden by a
  543. * termination signal to the value 1.
  544. */
  545. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  546. /**
  547. * Retrieves extra command line arguments passed to the interpreter.
  548. *
  549. * @param n backend instance handle
  550. * @param mem value memory to use
  551. * @param out_value the arguments will be written here on success as a list value
  552. * @return 1 if available, 0 if not available. If available, but out of memory, returns 1
  553. * and an invalid value.
  554. */
  555. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value);
  556. /**
  557. * Returns the retry time of the intepreter.
  558. *
  559. * @param n backend instance handle
  560. * @return retry time in milliseconds
  561. */
  562. btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  563. /**
  564. * Loads a module group into the interpreter.
  565. *
  566. * @param n backend instance handle
  567. * @param group module group to load
  568. * @return 1 on success, 0 on failure
  569. */
  570. int NCDModuleInst_Backend_InterpLoadGroup (NCDModuleInst *n, const struct NCDModuleGroup *group);
  571. /**
  572. * Initializes a process in the interpreter from a process template.
  573. * This must be called on behalf of a module backend instance.
  574. * The process is initializes in down state.
  575. *
  576. * @param o the process
  577. * @param n backend instance whose interpreter will be providing the process
  578. * @param template_name name of the process template as an {@link NCDStringIndex} identifier
  579. * @param args arguments to the process. Must be an invalid value or a list value.
  580. * The value must be available and unchanged while the process exists.
  581. * @param handler_event handler which reports events about the process from the
  582. * interpreter
  583. * @return 1 on success, 0 on failure
  584. */
  585. int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_id_t template_name, NCDValRef args, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  586. /**
  587. * Wrapper around {@link NCDModuleProcess_InitId} which takes the template name as an
  588. * {@link NCDValRef}, which must point to a string value.
  589. */
  590. int NCDModuleProcess_InitValue (NCDModuleProcess *o, NCDModuleInst *n, NCDValRef template_name, NCDValRef args, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  591. /**
  592. * Frees the process.
  593. * The process must be in terminated state.
  594. *
  595. * @param o the process
  596. */
  597. void NCDModuleProcess_Free (NCDModuleProcess *o);
  598. /**
  599. * Does nothing.
  600. * The process must be in terminated state.
  601. *
  602. * @param o the process
  603. */
  604. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  605. /**
  606. * Sets callback functions for providing special objects within the process.
  607. *
  608. * @param o the process
  609. * @param func_getspecialobj function for resolving special objects, or NULL
  610. */
  611. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  612. /**
  613. * Continues the process after the process went down.
  614. * The process must be in waiting state.
  615. * The process enters down state.
  616. *
  617. * @param o the process
  618. */
  619. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  620. /**
  621. * Requests the process to terminate.
  622. * The process must be in down, up or waiting state.
  623. * The process enters terminating state.
  624. *
  625. * @param o the process
  626. */
  627. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  628. /**
  629. * Resolves an object within the process from the point
  630. * at the end of the process.
  631. * This function has no side effects.
  632. *
  633. * @param o the process
  634. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  635. * @param out_object the object will be returned here
  636. * @return 1 on success, 0 on failure
  637. */
  638. int NCDModuleProcess_GetObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  639. /**
  640. * Sets callback functions for the interpreter to implement the
  641. * process backend.
  642. * Must be called from within {@link NCDModuleInst_func_initprocess}
  643. * if success is to be reported there.
  644. *
  645. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  646. * @param interp_user argument to callback functions
  647. * @param interp_func_event function for reporting continue/terminate requests
  648. * @param interp_func_getobj function for resolving objects within the process
  649. */
  650. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  651. NCDModuleProcess_interp_func_event interp_func_event,
  652. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  653. /**
  654. * Reports the process backend as up.
  655. * The process backend must be in down state.
  656. * The process backend enters up state.
  657. * WARNING: this directly calls the process creator; expect to be called back
  658. *
  659. * @param o process backend handle
  660. */
  661. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  662. /**
  663. * Reports the process backend as down.
  664. * The process backend must be in up state.
  665. * The process backend enters waiting state.
  666. * WARNING: this directly calls the process creator; expect to be called back
  667. *
  668. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  669. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  670. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  671. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  672. *
  673. * @param o process backend handle
  674. */
  675. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  676. /**
  677. * Reports termination of the process backend.
  678. * The process backend must be in terminating state.
  679. * The process backend handle becomes invalid and must not be used
  680. * by the interpreter any longer.
  681. * WARNING: this directly calls the process creator; expect to be called back
  682. *
  683. * @param o process backend handle
  684. */
  685. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  686. /**
  687. * Resolves a special process object for the process backend.
  688. *
  689. * @param o process backend handle
  690. * @param name name of the object as an {@link NCDStringIndex} identifier
  691. * @param out_object the object will be returned here
  692. * @return 1 on success, 0 on failure
  693. */
  694. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  695. /**
  696. * Function called before any instance of any backend in a module
  697. * group is created;
  698. *
  699. * @param params structure containing global resources, such as
  700. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}.
  701. * @return 1 on success, 0 on failure
  702. */
  703. typedef int (*NCDModule_func_globalinit) (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params);
  704. /**
  705. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  706. * in a module group.
  707. * There are no backend instances alive from this module group.
  708. */
  709. typedef void (*NCDModule_func_globalfree) (struct NCDInterpModuleGroup *group);
  710. /**
  711. * Handler called to create an new module backend instance.
  712. * The backend is initialized in down state.
  713. *
  714. * If the backend fails initialization, this function should report the backend
  715. * instance to have died with error by calling {@link NCDModuleInst_Backend_DeadError}.
  716. *
  717. * @param o if the module specifies a positive alloc_size value in the {@link NCDModule}
  718. * structure, this will point to the allocated memory that can be used by the
  719. * module instance while it exists. If the alloc_size is 0 (default), this may or
  720. * may not be NULL.
  721. * @param i module backend instance handler. The backend may only use this handle via
  722. * the Backend functions of {@link NCDModuleInst}.
  723. */
  724. typedef void (*NCDModule_func_new2) (void *o, NCDModuleInst *i, const struct NCDModuleInst_new_params *params);
  725. /**
  726. * Handler called to request termination of a backend instance.
  727. * The backend instance was in down or up state.
  728. * The backend instance enters dying state.
  729. *
  730. * @param o state pointer, as in {@link NCDModule_func_new2}
  731. */
  732. typedef void (*NCDModule_func_die) (void *o);
  733. /**
  734. * Function called to resolve a variable within a backend instance.
  735. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  736. * This function must not have any side effects.
  737. *
  738. * @param o state pointer, as in {@link NCDModule_func_new2}
  739. * @param name name of the variable to resolve
  740. * @param mem value memory to use
  741. * @param out on success, the backend should initialize the value here
  742. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  743. * and an invalid value.
  744. */
  745. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValMem *mem, NCDValRef *out);
  746. /**
  747. * Function called to resolve a variable within a backend instance.
  748. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  749. * This function must not have any side effects.
  750. *
  751. * @param o state pointer, as in {@link NCDModule_func_new2}
  752. * @param name name of the variable to resolve as an {@link NCDStringIndex} identifier
  753. * @param mem value memory to use
  754. * @param out on success, the backend should initialize the value here
  755. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  756. * and an invalid value.
  757. */
  758. typedef int (*NCDModule_func_getvar2) (void *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  759. /**
  760. * Function called to resolve an object within a backend instance.
  761. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  762. * This function must not have any side effects.
  763. *
  764. * @param o state pointer, as in {@link NCDModule_func_new2}
  765. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  766. * @param out_object the object will be returned here
  767. * @return 1 on success, 0 on failure
  768. */
  769. typedef int (*NCDModule_func_getobj) (void *o, NCD_string_id_t name, NCDObject *out_object);
  770. /**
  771. * Handler called when the module instance is in a clean state.
  772. * This means that all statements preceding it in the process are
  773. * up, this statement is down, and all following statements are
  774. * uninitialized. When a backend instance goes down, it is guaranteed,
  775. * as long as it stays down, that either this will be called or
  776. * termination will be requested with {@link NCDModule_func_die}.
  777. * The backend instance was in down state.
  778. *
  779. * @param o state pointer, as in {@link NCDModule_func_new2}
  780. */
  781. typedef void (*NCDModule_func_clean) (void *o);
  782. #define NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN (1 << 0)
  783. #define NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS (1 << 1)
  784. /**
  785. * Structure encapsulating the implementation of a module backend.
  786. */
  787. struct NCDModule {
  788. /**
  789. * If this implements a plain statement, the name of the statement.
  790. * If this implements a method, then "base_type::method_name".
  791. */
  792. const char *type;
  793. /**
  794. * The base type for methods operating on instances of this backend.
  795. * Any module with type of form "base_type::method_name" is considered
  796. * a method of instances of this backend.
  797. * If this is NULL, the base type will default to type.
  798. */
  799. const char *base_type;
  800. /**
  801. * Function called to create an new backend instance.
  802. */
  803. NCDModule_func_new2 func_new2;
  804. /**
  805. * Function called to request termination of a backend instance.
  806. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  807. */
  808. NCDModule_func_die func_die;
  809. /**
  810. * Function called to resolve a variable within the backend instance.
  811. * May be NULL.
  812. */
  813. NCDModule_func_getvar func_getvar;
  814. /**
  815. * Function called to resolve a variable within the backend instance.
  816. * May be NULL.
  817. */
  818. NCDModule_func_getvar2 func_getvar2;
  819. /**
  820. * Function called to resolve an object within the backend instance.
  821. * May be NULL.
  822. */
  823. NCDModule_func_getobj func_getobj;
  824. /**
  825. * Function called when the backend instance is in a clean state.
  826. * May be NULL.
  827. */
  828. NCDModule_func_clean func_clean;
  829. /**
  830. * Various flags.
  831. *
  832. * - NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  833. * Whether the interpreter is allowed to call func_getvar and func_getobj
  834. * even when the backend instance is in down state (as opposed to just
  835. * in up state.
  836. *
  837. * - NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  838. * If not set, strings within arguments which are not some kind of ContinuousString
  839. * will be converted to some kind of ContinuousString before the module's init
  840. * function is called. If set, they will not be, and the module must work with any
  841. * kind of strings (i.e. {@link NCDVal_StringData} may not be allowed).
  842. */
  843. int flags;
  844. /**
  845. * The amount of memory to preallocate for each module instance.
  846. * Preallocation can be used to avoid having to allocate memory from
  847. * module initialization. The memory can be accessed via the first
  848. * argument to {@link NCDModule_func_new2} and other calls.
  849. */
  850. int alloc_size;
  851. };
  852. /**
  853. * Structure encapsulating a group of module backend implementations,
  854. * with global init and free functions.
  855. */
  856. struct NCDModuleGroup {
  857. /**
  858. * Function called before any instance of any module backend in this
  859. * group is crated. May be NULL.
  860. */
  861. NCDModule_func_globalinit func_globalinit;
  862. /**
  863. * Function called to clean up after {@link NCDModule_func_globalinit}.
  864. * May be NULL.
  865. */
  866. NCDModule_func_globalfree func_globalfree;
  867. /**
  868. * Array of module backends. The array must be terminated with a
  869. * structure that has a NULL type member.
  870. */
  871. const struct NCDModule *modules;
  872. /**
  873. * A pointer to an array of strings which will be mapped to
  874. * {@link NCDStringIndex} string identifiers for the module to use.
  875. * The array must be terminated by NULL. The resulting string
  876. * identifiers will be available in the 'strings' member in
  877. * {@link NCDInterpModuleGroup}.
  878. */
  879. const char *const*strings;
  880. /**
  881. * A pointer to an array of global functions implemented by this module
  882. * group. The array must be terminated with a structure that has a NULL
  883. * func_name member. May be NULL.
  884. */
  885. struct NCDModuleFunction const *functions;
  886. };
  887. /**
  888. * Represents an {@link NCDModule} within an interpreter.
  889. * This structure is initialized by the interpreter when it loads a module group.
  890. */
  891. struct NCDInterpModule {
  892. /**
  893. * A copy of the original NCDModule structure,
  894. */
  895. struct NCDModule module;
  896. /**
  897. * The string identifier of this module's base_type (or type if base_type is
  898. * not specified) according to {@link NCDStringIndex}.
  899. */
  900. NCD_string_id_t base_type_id;
  901. /**
  902. * A pointer to the {@link NCDInterpModuleGroup} representing the group
  903. * this module belongs to.
  904. */
  905. struct NCDInterpModuleGroup *group;
  906. };
  907. /**
  908. * Represents an {@link NCDModuleGroup} within an interpreter.
  909. * This structure is initialized by the interpreter when it loads a module group.
  910. */
  911. struct NCDInterpModuleGroup {
  912. /**
  913. * A copy of the original NCDModuleGroup structure.
  914. */
  915. struct NCDModuleGroup group;
  916. /**
  917. * An array of string identifiers corresponding to the strings
  918. * in the 'strings' member of NCDModuleGroup. May be NULL if there
  919. * are no strings in the NCDModuleGroup.
  920. */
  921. NCD_string_id_t *strings;
  922. /**
  923. * Pointer which allows the module to keep private interpreter-wide state.
  924. * This can be freely modified by the module; the interpeter will not
  925. * read or write it.
  926. */
  927. void *group_state;
  928. };
  929. /**
  930. * Holds some things passed to function implementations which are presumed
  931. * to stay the same within an interpreter (for optimization purposes).
  932. */
  933. struct NCDModuleFunction_params {
  934. /**
  935. * Log function which appends a log prefix with {@link BLog_Append}.
  936. * Its user argument will be the user member of {@link NCDModuleFunction_eval_params}.
  937. */
  938. BLog_logfunc logfunc;
  939. /**
  940. * Pointer to an {@link NCDModuleInst_iparams} structure, which exposes
  941. * services provided by the interpreter.
  942. */
  943. const struct NCDModuleInst_iparams *iparams;
  944. };
  945. /**
  946. * Contains parameters to {@link NCDModuleFunction_func_eval} that are passed indirectly.
  947. */
  948. struct NCDModuleFunction_eval_params {
  949. /**
  950. * A pointer to the {@link NCDInterpFunction} structure for the function being called.
  951. */
  952. struct NCDInterpFunction const *ifunc;
  953. /**
  954. * Pointer to an additional structure needed for the call, containing callbacks to
  955. * the interpreter.
  956. */
  957. struct NCDModuleFunction_params const *params;
  958. /**
  959. * This is passed back to interpeter callbacks done as part of function evaluation.
  960. */
  961. void *interp_user;
  962. };
  963. /**
  964. * Callback for evaluating a function (defined by an {@link NCDModuleFunction} structure).
  965. *
  966. * @param args arguments to evaluate the function with.
  967. * These arguments are provided in a lazy manner - an argument
  968. * must be evaluated to obtain its value.
  969. * @param mem an existing value memory object to store the value into
  970. * @param out on success, *out should be set to the value reference to
  971. * the evaluated value, residing in the specified memory
  972. * object. It is permitted to return success but store an
  973. * invalid value reference here, which is understood as an
  974. * error.
  975. * @param params indirectly passed arguments
  976. * @return 1 on success, 0 on error (but see the description of the out parameter).
  977. */
  978. typedef int (*NCDModuleFunction_func_eval) (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params);
  979. /**
  980. * Returns a logging context for use by the function implementation during
  981. * function evaluation.
  982. */
  983. BLogContext NCDModuleFunction_LogContext (struct NCDModuleFunction_eval_params const *params);
  984. struct NCDModuleFunction {
  985. /**
  986. * The name of the function.
  987. * NULL to terminate the array of functions.
  988. */
  989. char const *func_name;
  990. /**
  991. * Callback for evaluating the function.
  992. *
  993. * @param group the group the function belongs to
  994. * @param func pointer to this structure. It can be used to implement
  995. * different functions in the same callback, by reading the
  996. * func_name member.
  997. * @param args used to access the arguments to the function, in a lazily
  998. * evaluated manner
  999. * @param mem an existing value memory object to store the result to
  1000. * @param out on success, *out should be set to the value reference to
  1001. * the evaluated value, residing in the specified memory
  1002. * object. It is permitted to return success but store an
  1003. * invalid value reference here, which is understood as an
  1004. * error.
  1005. * @return 1 on success, 0 on error (but see above).
  1006. */
  1007. NCDModuleFunction_func_eval func_eval;
  1008. };
  1009. /**
  1010. * Represents an {@link NCDModuleFunction} within an interpreter.
  1011. * This structure is initialized by the interpreter when it loads a module group.
  1012. */
  1013. struct NCDInterpFunction {
  1014. /**
  1015. * A copy of the original NCDModuleFunction structure.
  1016. */
  1017. struct NCDModuleFunction function;
  1018. /**
  1019. * The string identifier of this functions's name. according to
  1020. * {@link NCDStringIndex}.
  1021. */
  1022. NCD_string_id_t func_name_id;
  1023. /**
  1024. * A pointer to the {@link NCDInterpModuleGroup} representing the group
  1025. * this function belongs to.
  1026. */
  1027. struct NCDInterpModuleGroup *group;
  1028. };
  1029. #endif