NCDModule.h 36 KB

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