NCDModule.h 31 KB

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