NCDModule.h 35 KB

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