NCDModule.h 34 KB

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