NCDModule.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. * Returns a logging context. The context is valid until the backend dies.
  533. */
  534. BLogContext NCDModuleInst_Backend_LogContext (NCDModuleInst *n);
  535. /**
  536. * Initiates interpreter termination.
  537. *
  538. * @param n backend instance handle
  539. * @param exit_code exit code to return to the operating system. This overrides
  540. * any previously set exit code, and will be overriden by a
  541. * termination signal to the value 1.
  542. */
  543. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  544. /**
  545. * Retrieves extra command line arguments passed to the interpreter.
  546. *
  547. * @param n backend instance handle
  548. * @param mem value memory to use
  549. * @param out_value the arguments will be written here on success as a list value
  550. * @return 1 if available, 0 if not available. If available, but out of memory, returns 1
  551. * and an invalid value.
  552. */
  553. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value);
  554. /**
  555. * Returns the retry time of the intepreter.
  556. *
  557. * @param n backend instance handle
  558. * @return retry time in milliseconds
  559. */
  560. btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  561. /**
  562. * Loads a module group into the interpreter.
  563. *
  564. * @param n backend instance handle
  565. * @param group module group to load
  566. * @return 1 on success, 0 on failure
  567. */
  568. int NCDModuleInst_Backend_InterpLoadGroup (NCDModuleInst *n, const struct NCDModuleGroup *group);
  569. /**
  570. * Initializes a process in the interpreter from a process template.
  571. * This must be called on behalf of a module backend instance.
  572. * The process is initializes in down state.
  573. *
  574. * @param o the process
  575. * @param n backend instance whose interpreter will be providing the process
  576. * @param template_name name of the process template as an {@link NCDStringIndex} identifier
  577. * @param args arguments to the process. Must be an invalid value or a list value.
  578. * The value must be available and unchanged while the process exists.
  579. * @param handler_event handler which reports events about the process from the
  580. * interpreter
  581. * @return 1 on success, 0 on failure
  582. */
  583. int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_id_t template_name, NCDValRef args, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  584. /**
  585. * Wrapper around {@link NCDModuleProcess_InitId} which takes the template name as an
  586. * {@link NCDValRef}, which must point to a string value.
  587. */
  588. int NCDModuleProcess_InitValue (NCDModuleProcess *o, NCDModuleInst *n, NCDValRef template_name, NCDValRef args, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  589. /**
  590. * Frees the process.
  591. * The process must be in terminated state.
  592. *
  593. * @param o the process
  594. */
  595. void NCDModuleProcess_Free (NCDModuleProcess *o);
  596. /**
  597. * Does nothing.
  598. * The process must be in terminated state.
  599. *
  600. * @param o the process
  601. */
  602. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  603. /**
  604. * Sets callback functions for providing special objects within the process.
  605. *
  606. * @param o the process
  607. * @param func_getspecialobj function for resolving special objects, or NULL
  608. */
  609. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  610. /**
  611. * Continues the process after the process went down.
  612. * The process must be in waiting state.
  613. * The process enters down state.
  614. *
  615. * @param o the process
  616. */
  617. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  618. /**
  619. * Requests the process to terminate.
  620. * The process must be in down, up or waiting state.
  621. * The process enters terminating state.
  622. *
  623. * @param o the process
  624. */
  625. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  626. /**
  627. * Resolves an object within the process from the point
  628. * at the end of the process.
  629. * This function has no side effects.
  630. *
  631. * @param o the process
  632. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  633. * @param out_object the object will be returned here
  634. * @return 1 on success, 0 on failure
  635. */
  636. int NCDModuleProcess_GetObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  637. /**
  638. * Sets callback functions for the interpreter to implement the
  639. * process backend.
  640. * Must be called from within {@link NCDModuleInst_func_initprocess}
  641. * if success is to be reported there.
  642. *
  643. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  644. * @param interp_user argument to callback functions
  645. * @param interp_func_event function for reporting continue/terminate requests
  646. * @param interp_func_getobj function for resolving objects within the process
  647. */
  648. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  649. NCDModuleProcess_interp_func_event interp_func_event,
  650. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  651. /**
  652. * Reports the process backend as up.
  653. * The process backend must be in down state.
  654. * The process backend enters up state.
  655. * WARNING: this directly calls the process creator; expect to be called back
  656. *
  657. * @param o process backend handle
  658. */
  659. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  660. /**
  661. * Reports the process backend as down.
  662. * The process backend must be in up state.
  663. * The process backend enters waiting state.
  664. * WARNING: this directly calls the process creator; expect to be called back
  665. *
  666. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  667. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  668. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  669. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  670. *
  671. * @param o process backend handle
  672. */
  673. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  674. /**
  675. * Reports termination of the process backend.
  676. * The process backend must be in terminating state.
  677. * The process backend handle becomes invalid and must not be used
  678. * by the interpreter any longer.
  679. * WARNING: this directly calls the process creator; expect to be called back
  680. *
  681. * @param o process backend handle
  682. */
  683. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  684. /**
  685. * Resolves a special process object for the process backend.
  686. *
  687. * @param o process backend handle
  688. * @param name name of the object as an {@link NCDStringIndex} identifier
  689. * @param out_object the object will be returned here
  690. * @return 1 on success, 0 on failure
  691. */
  692. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  693. /**
  694. * Function called before any instance of any backend in a module
  695. * group is created;
  696. *
  697. * @param params structure containing global resources, such as
  698. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}.
  699. * @return 1 on success, 0 on failure
  700. */
  701. typedef int (*NCDModule_func_globalinit) (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params);
  702. /**
  703. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  704. * in a module group.
  705. * There are no backend instances alive from this module group.
  706. */
  707. typedef void (*NCDModule_func_globalfree) (struct NCDInterpModuleGroup *group);
  708. /**
  709. * Handler called to create an new module backend instance.
  710. * The backend is initialized in down state.
  711. *
  712. * If the backend fails initialization, this function should report the backend
  713. * instance to have died with error by calling {@link NCDModuleInst_Backend_DeadError}.
  714. *
  715. * @param o if the module specifies a positive alloc_size value in the {@link NCDModule}
  716. * structure, this will point to the allocated memory that can be used by the
  717. * module instance while it exists. If the alloc_size is 0 (default), this may or
  718. * may not be NULL.
  719. * @param i module backend instance handler. The backend may only use this handle via
  720. * the Backend functions of {@link NCDModuleInst}.
  721. */
  722. typedef void (*NCDModule_func_new2) (void *o, NCDModuleInst *i, const struct NCDModuleInst_new_params *params);
  723. /**
  724. * Handler called to request termination of a backend instance.
  725. * The backend instance was in down or up state.
  726. * The backend instance enters dying state.
  727. *
  728. * @param o state pointer, as in {@link NCDModule_func_new2}
  729. */
  730. typedef void (*NCDModule_func_die) (void *o);
  731. /**
  732. * Function called to resolve a variable within a backend instance.
  733. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  734. * This function must not have any side effects.
  735. *
  736. * @param o state pointer, as in {@link NCDModule_func_new2}
  737. * @param name name of the variable to resolve
  738. * @param mem value memory to use
  739. * @param out on success, the backend should initialize the value here
  740. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  741. * and an invalid value.
  742. */
  743. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValMem *mem, NCDValRef *out);
  744. /**
  745. * Function called to resolve a variable within a backend instance.
  746. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  747. * This function must not have any side effects.
  748. *
  749. * @param o state pointer, as in {@link NCDModule_func_new2}
  750. * @param name name of the variable to resolve as an {@link NCDStringIndex} identifier
  751. * @param mem value memory to use
  752. * @param out on success, the backend should initialize the value here
  753. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  754. * and an invalid value.
  755. */
  756. typedef int (*NCDModule_func_getvar2) (void *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  757. /**
  758. * Function called to resolve an object within a backend instance.
  759. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  760. * This function must not have any side effects.
  761. *
  762. * @param o state pointer, as in {@link NCDModule_func_new2}
  763. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  764. * @param out_object the object will be returned here
  765. * @return 1 on success, 0 on failure
  766. */
  767. typedef int (*NCDModule_func_getobj) (void *o, NCD_string_id_t name, NCDObject *out_object);
  768. /**
  769. * Handler called when the module instance is in a clean state.
  770. * This means that all statements preceding it in the process are
  771. * up, this statement is down, and all following statements are
  772. * uninitialized. When a backend instance goes down, it is guaranteed,
  773. * as long as it stays down, that either this will be called or
  774. * termination will be requested with {@link NCDModule_func_die}.
  775. * The backend instance was in down state.
  776. *
  777. * @param o state pointer, as in {@link NCDModule_func_new2}
  778. */
  779. typedef void (*NCDModule_func_clean) (void *o);
  780. #define NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN (1 << 0)
  781. #define NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS (1 << 1)
  782. /**
  783. * Structure encapsulating the implementation of a module backend.
  784. */
  785. struct NCDModule {
  786. /**
  787. * If this implements a plain statement, the name of the statement.
  788. * If this implements a method, then "base_type::method_name".
  789. */
  790. const char *type;
  791. /**
  792. * The base type for methods operating on instances of this backend.
  793. * Any module with type of form "base_type::method_name" is considered
  794. * a method of instances of this backend.
  795. * If this is NULL, the base type will default to type.
  796. */
  797. const char *base_type;
  798. /**
  799. * Function called to create an new backend instance.
  800. */
  801. NCDModule_func_new2 func_new2;
  802. /**
  803. * Function called to request termination of a backend instance.
  804. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  805. */
  806. NCDModule_func_die func_die;
  807. /**
  808. * Function called to resolve a variable within the backend instance.
  809. * May be NULL.
  810. */
  811. NCDModule_func_getvar func_getvar;
  812. /**
  813. * Function called to resolve a variable within the backend instance.
  814. * May be NULL.
  815. */
  816. NCDModule_func_getvar2 func_getvar2;
  817. /**
  818. * Function called to resolve an object within the backend instance.
  819. * May be NULL.
  820. */
  821. NCDModule_func_getobj func_getobj;
  822. /**
  823. * Function called when the backend instance is in a clean state.
  824. * May be NULL.
  825. */
  826. NCDModule_func_clean func_clean;
  827. /**
  828. * Various flags.
  829. *
  830. * - NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  831. * Whether the interpreter is allowed to call func_getvar and func_getobj
  832. * even when the backend instance is in down state (as opposed to just
  833. * in up state.
  834. *
  835. * - NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  836. * If not set, strings within arguments which are not some kind of ContinuousString
  837. * will be converted to some kind of ContinuousString before the module's init
  838. * function is called. If set, they will not be, and the module must work with any
  839. * kind of strings (i.e. {@link NCDVal_StringData} may not be allowed).
  840. */
  841. int flags;
  842. /**
  843. * The amount of memory to preallocate for each module instance.
  844. * Preallocation can be used to avoid having to allocate memory from
  845. * module initialization. The memory can be accessed via the first
  846. * argument to {@link NCDModule_func_new2} and other calls.
  847. */
  848. int alloc_size;
  849. };
  850. /**
  851. * Structure encapsulating a group of module backend implementations,
  852. * with global init and free functions.
  853. */
  854. struct NCDModuleGroup {
  855. /**
  856. * Function called before any instance of any module backend in this
  857. * group is crated. May be NULL.
  858. */
  859. NCDModule_func_globalinit func_globalinit;
  860. /**
  861. * Function called to clean up after {@link NCDModule_func_globalinit}.
  862. * May be NULL.
  863. */
  864. NCDModule_func_globalfree func_globalfree;
  865. /**
  866. * Array of module backends. The array must be terminated with a
  867. * structure that has a NULL type member.
  868. */
  869. const struct NCDModule *modules;
  870. /**
  871. * A pointer to an array of strings which will be mapped to
  872. * {@link NCDStringIndex} string identifiers for the module to use.
  873. * The array must be terminated by NULL. The resulting string
  874. * identifiers will be available in the 'strings' member in
  875. * {@link NCDInterpModuleGroup}.
  876. */
  877. const char *const*strings;
  878. };
  879. /**
  880. * Represents an {@link NCDModule} within an interpreter.
  881. * This structure is initialized by the interpreter when it loads a module group.
  882. */
  883. struct NCDInterpModule {
  884. /**
  885. * A copy of the original NCDModule structure,
  886. */
  887. struct NCDModule module;
  888. /**
  889. * The string identifier of this module's base_type (or type if base_type is
  890. * not specified) according to {@link NCDStringIndex}.
  891. */
  892. NCD_string_id_t base_type_id;
  893. /**
  894. * A pointer to the {@link NCDInterpModuleGroup} representing the group
  895. * this module belongs to.
  896. */
  897. struct NCDInterpModuleGroup *group;
  898. };
  899. /**
  900. * Represents an {@link NCDModuleGroup} within an interpreter.
  901. * This structure is initialized by the interpreter when it loads a module group.
  902. */
  903. struct NCDInterpModuleGroup {
  904. /**
  905. * A copy of the original NCDModuleGroup structure.
  906. */
  907. struct NCDModuleGroup group;
  908. /**
  909. * An array of string identifiers corresponding to the strings
  910. * in the 'strings' member of NCDModuleGroup. May be NULL if there
  911. * are no strings in the NCDModuleGroup.
  912. */
  913. NCD_string_id_t *strings;
  914. /**
  915. * Pointer which allows the module to keep private interpreter-wide state.
  916. * This can be freely modified by the module; the interpeter will not
  917. * read or write it.
  918. */
  919. void *group_state;
  920. };
  921. #endif