NCDModule.h 36 KB

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