kscheme.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/error.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kplugin.h>
  10. #include <klish/kentry.h>
  11. #include <klish/kscheme.h>
  12. #include <klish/kcontext.h>
  13. #include <klish/kustore.h>
  14. struct kscheme_s {
  15. faux_list_t *plugins;
  16. faux_list_t *entrys;
  17. kustore_t *ustore;
  18. };
  19. // Simple methods
  20. // PLUGIN list
  21. KGET(scheme, faux_list_t *, plugins);
  22. KCMP_NESTED(scheme, plugin, name);
  23. KCMP_NESTED_BY_KEY(scheme, plugin, name);
  24. KADD_NESTED(scheme, kplugin_t *, plugins);
  25. KFIND_NESTED(scheme, plugin);
  26. KNESTED_LEN(scheme, plugins);
  27. KNESTED_ITER(scheme, plugins);
  28. KNESTED_EACH(scheme, kplugin_t *, plugins);
  29. // ENTRY list
  30. KGET(scheme, faux_list_t *, entrys);
  31. KCMP_NESTED(scheme, entry, name);
  32. KCMP_NESTED_BY_KEY(scheme, entry, name);
  33. KADD_NESTED(scheme, kentry_t *, entrys);
  34. KFIND_NESTED(scheme, entry);
  35. KNESTED_LEN(scheme, entrys);
  36. KNESTED_ITER(scheme, entrys);
  37. KNESTED_EACH(scheme, kentry_t *, entrys);
  38. kscheme_t *kscheme_new(void)
  39. {
  40. kscheme_t *scheme = NULL;
  41. scheme = faux_zmalloc(sizeof(*scheme));
  42. assert(scheme);
  43. if (!scheme)
  44. return NULL;
  45. // PLUGIN list
  46. scheme->plugins = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  47. kscheme_plugin_compare, kscheme_plugin_kcompare,
  48. (void (*)(void *))kplugin_free);
  49. assert(scheme->plugins);
  50. // ENTRY list
  51. // Must be unsorted because order is important
  52. scheme->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  53. kscheme_entry_compare, kscheme_entry_kcompare,
  54. (void (*)(void *))kentry_free);
  55. assert(scheme->entrys);
  56. // User store
  57. scheme->ustore = kustore_new();
  58. assert(scheme->ustore);
  59. return scheme;
  60. }
  61. void kscheme_free(kscheme_t *scheme)
  62. {
  63. if (!scheme)
  64. return;
  65. // kustore_free() must be before plugin_free() because plugin_free()
  66. // does dlclose() and ustore free function is will not be accessible
  67. kustore_free(scheme->ustore);
  68. faux_list_free(scheme->plugins);
  69. faux_list_free(scheme->entrys);
  70. faux_free(scheme);
  71. }
  72. #define TAG "PLUGIN"
  73. static bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
  74. faux_error_t *error)
  75. {
  76. bool_t retcode = BOOL_TRUE;
  77. kscheme_plugins_node_t *iter = NULL;
  78. kplugin_t *plugin = NULL;
  79. assert(scheme);
  80. if (!scheme)
  81. return BOOL_FALSE;
  82. assert(scheme->plugins);
  83. if (!context)
  84. return BOOL_FALSE;
  85. iter = kscheme_plugins_iter(scheme);
  86. while ((plugin = kscheme_plugins_each(&iter))) {
  87. int init_retcode = 0;
  88. if (!kplugin_load(plugin)) {
  89. faux_error_sprintf(error,
  90. TAG ": Can't load plugin \"%s\"",
  91. kplugin_name(plugin));
  92. retcode = BOOL_FALSE;
  93. continue; // Try to load all plugins
  94. }
  95. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_INIT);
  96. kcontext_set_plugin(context, plugin);
  97. if ((init_retcode = kplugin_init(plugin, context)) < 0) {
  98. faux_error_sprintf(error,
  99. TAG ": Can't init plugin \"%s\" (%d)",
  100. kplugin_name(plugin), init_retcode);
  101. retcode = BOOL_FALSE;
  102. continue;
  103. }
  104. }
  105. return retcode;
  106. }
  107. static bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
  108. faux_error_t *error)
  109. {
  110. kscheme_plugins_node_t *iter = NULL;
  111. kplugin_t *plugin = NULL;
  112. assert(scheme);
  113. if (!scheme)
  114. return BOOL_FALSE;
  115. assert(scheme->plugins);
  116. if (!context)
  117. return BOOL_FALSE;
  118. iter = kscheme_plugins_iter(scheme);
  119. while ((plugin = kscheme_plugins_each(&iter))) {
  120. int fini_retcode = -1;
  121. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_FINI);
  122. kcontext_set_plugin(context, plugin);
  123. if ((fini_retcode = kplugin_fini(plugin, context)) < 0) {
  124. faux_error_sprintf(error,
  125. TAG ": Can't fini plugin \"%s\" (%d)",
  126. kplugin_name(plugin), fini_retcode);
  127. }
  128. }
  129. return BOOL_TRUE;
  130. }
  131. bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  132. {
  133. assert(scheme);
  134. if (!scheme)
  135. return BOOL_FALSE;
  136. if (!context)
  137. return BOOL_FALSE;
  138. if (!kscheme_fini_plugins(scheme, context, error))
  139. return BOOL_FALSE;
  140. return BOOL_TRUE;
  141. }
  142. static ksym_t *kscheme_find_sym(const kscheme_t *scheme, const char *name,
  143. kplugin_t **src_plugin)
  144. {
  145. char *saveptr = NULL;
  146. const char *delim = "@";
  147. char *plugin_name = NULL;
  148. char *cmd_name = NULL;
  149. char *full_name = NULL;
  150. ksym_t *sym = NULL;
  151. kplugin_t *plugin = NULL; // Source of sym
  152. assert(scheme);
  153. if (!scheme)
  154. return NULL;
  155. assert(name);
  156. if (!name)
  157. return NULL;
  158. // Parse full name to get sym name and optional plugin name
  159. full_name = faux_str_dup(name);
  160. cmd_name = strtok_r(full_name, delim, &saveptr);
  161. if (!cmd_name) {
  162. faux_str_free(full_name);
  163. return NULL;
  164. }
  165. plugin_name = strtok_r(NULL, delim, &saveptr);
  166. // Search for symbol within specified PLUGIN only
  167. if (plugin_name) {
  168. plugin = kscheme_find_plugin(scheme, plugin_name);
  169. if (!plugin) {
  170. faux_str_free(full_name);
  171. return NULL;
  172. }
  173. sym = kplugin_find_sym(plugin, cmd_name);
  174. // Search for symbol within all PLUGINs
  175. } else {
  176. kscheme_plugins_node_t *iter = NULL;
  177. iter = kscheme_plugins_iter(scheme);
  178. while ((plugin = kscheme_plugins_each(&iter))) {
  179. sym = kplugin_find_sym(plugin, cmd_name);
  180. if (sym)
  181. break;
  182. }
  183. }
  184. if (sym && src_plugin)
  185. *src_plugin = plugin;
  186. faux_str_free(full_name);
  187. return sym;
  188. }
  189. bool_t kscheme_prepare_action_list(kscheme_t *scheme, kentry_t *entry,
  190. faux_error_t *error)
  191. {
  192. faux_list_node_t *iter = NULL;
  193. kaction_t *action = NULL;
  194. bool_t retcode = BOOL_TRUE;
  195. faux_list_t *action_list = NULL;
  196. assert(scheme);
  197. if (!scheme)
  198. return BOOL_FALSE;
  199. assert(entry);
  200. if (!entry)
  201. return BOOL_FALSE;
  202. action_list = kentry_actions(entry);
  203. assert(action_list);
  204. if (!action_list)
  205. return BOOL_FALSE;
  206. if (faux_list_is_empty(action_list))
  207. return BOOL_TRUE;
  208. iter = faux_list_head(action_list);
  209. while ((action = (kaction_t *)faux_list_each(&iter))) {
  210. ksym_t *sym = NULL;
  211. kplugin_t *plugin = NULL;
  212. const char *sym_ref = kaction_sym_ref(action);
  213. sym = kscheme_find_sym(scheme, sym_ref, &plugin);
  214. if (!sym) {
  215. faux_error_sprintf(error, "Can't find symbol \"%s\"",
  216. sym_ref);
  217. retcode = BOOL_FALSE;
  218. continue;
  219. }
  220. kaction_set_sym(action, sym);
  221. kaction_set_plugin(action, plugin);
  222. // Filter can't contain sync symbols
  223. if ((kentry_filter(entry) != KENTRY_FILTER_FALSE) &&
  224. kaction_is_sync(action)) {
  225. faux_error_sprintf(error, "Filter \"%s\" can't contain "
  226. "sync symbol \"%s\"",
  227. kentry_name(entry), sym_ref);
  228. retcode = BOOL_FALSE;
  229. continue;
  230. }
  231. }
  232. return retcode;
  233. }
  234. kentry_t *kscheme_find_entry_by_path(const kscheme_t *scheme, const char *name)
  235. {
  236. char *saveptr = NULL;
  237. const char *delim = "/";
  238. char *entry_name = NULL;
  239. char *full_name = NULL;
  240. kentry_t *entry = NULL;
  241. assert(scheme);
  242. if (!scheme)
  243. return NULL;
  244. assert(name);
  245. if (!name)
  246. return NULL;
  247. // Get first component of ENTRY path. It will be searched for
  248. // within scheme.
  249. full_name = faux_str_dup(name);
  250. entry_name = strtok_r(full_name, delim, &saveptr);
  251. if (!entry_name) {
  252. faux_str_free(full_name);
  253. return NULL;
  254. }
  255. entry = kscheme_find_entry(scheme, entry_name);
  256. if (!entry) {
  257. faux_str_free(full_name);
  258. return NULL;
  259. }
  260. // Search nested ENTRYs
  261. while ((entry_name = strtok_r(NULL, delim, &saveptr))) {
  262. entry = kentry_find_entry(entry, entry_name);
  263. if (!entry)
  264. break;
  265. }
  266. faux_str_free(full_name);
  267. return entry;
  268. }
  269. bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
  270. faux_error_t *error) {
  271. kentry_entrys_node_t *iter = NULL;
  272. kentry_t *nested_entry = NULL;
  273. bool_t retcode = BOOL_TRUE;
  274. const char *ref = NULL;
  275. kentry_t *ref_entry = NULL;
  276. assert(scheme);
  277. if (!scheme)
  278. return BOOL_FALSE;
  279. assert(entry);
  280. if (!entry)
  281. return BOOL_FALSE;
  282. // Firstly if ENTRY is link to another ENTRY then make a copy
  283. if (kentry_ref_str(entry)) {
  284. ref_entry = entry;
  285. // Find the most deep real (non-link) object to reference to it
  286. // i.e. the link can't reference a link.
  287. while ((ref = kentry_ref_str(ref_entry))) {
  288. ref_entry = kscheme_find_entry_by_path(scheme, ref);
  289. if (!ref_entry) {
  290. faux_error_sprintf(error, "Can't find ENTRY \"%s\"", ref);
  291. return BOOL_FALSE;
  292. }
  293. }
  294. if (!kentry_link(entry, ref_entry)) {
  295. faux_error_sprintf(error, "Can't create link to ENTRY \"%s\"", ref);
  296. return BOOL_FALSE;
  297. }
  298. return BOOL_TRUE;
  299. }
  300. // ACTIONs
  301. if (!kscheme_prepare_action_list(scheme, entry, error))
  302. retcode = BOOL_FALSE;
  303. // Create fast links to nested entries with special purposes. Do it
  304. // before preparing child elements because some fields can be inhereted
  305. // by child from its parent
  306. iter = kentry_entrys_iter(entry);
  307. while ((nested_entry = kentry_entrys_each(&iter)))
  308. kentry_set_nested_by_purpose(entry,
  309. kentry_purpose(nested_entry), nested_entry);
  310. // Inherit LOG from parent if it's not specified explicitly
  311. if (!kentry_nested_by_purpose(entry, KENTRY_PURPOSE_LOG)) {
  312. kentry_t *parent = kentry_parent(entry);
  313. if (parent) { // Get LOG from parent entry
  314. kentry_set_nested_by_purpose(entry, KENTRY_PURPOSE_LOG,
  315. kentry_nested_by_purpose(parent,
  316. KENTRY_PURPOSE_LOG));
  317. } else { // High level entries has no parents
  318. iter = kscheme_entrys_iter(scheme);
  319. while ((nested_entry = kscheme_entrys_each(&iter))) {
  320. if (kentry_purpose(nested_entry) !=
  321. KENTRY_PURPOSE_LOG)
  322. continue;
  323. kentry_set_nested_by_purpose(entry,
  324. KENTRY_PURPOSE_LOG, nested_entry);
  325. }
  326. }
  327. }
  328. // Process nested ENTRYs
  329. iter = kentry_entrys_iter(entry);
  330. while ((nested_entry = kentry_entrys_each(&iter)))
  331. if (!kscheme_prepare_entry(scheme, nested_entry, error))
  332. retcode = BOOL_FALSE;
  333. return retcode;
  334. }
  335. /** @brief Prepares schema for execution.
  336. *
  337. * It loads plugins, link unresolved symbols, then iterates all the
  338. * objects and link them to each other, check access
  339. * permissions. Without this function the schema is not fully functional.
  340. */
  341. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  342. {
  343. kscheme_entrys_node_t *entrys_iter = NULL;
  344. kentry_t *entry = NULL;
  345. assert(scheme);
  346. if (!scheme)
  347. return BOOL_FALSE;
  348. if (!context)
  349. return BOOL_FALSE;
  350. if (!kscheme_load_plugins(scheme, context, error))
  351. return BOOL_FALSE;
  352. // Iterate ENTRYs
  353. entrys_iter = kscheme_entrys_iter(scheme);
  354. while ((entry = kscheme_entrys_each(&entrys_iter))) {
  355. if (!kscheme_prepare_entry(scheme, entry, error))
  356. return BOOL_FALSE;
  357. }
  358. return BOOL_TRUE;
  359. }
  360. bool_t kscheme_named_udata_new(kscheme_t *scheme,
  361. const char *name, void *data, kudata_data_free_fn free_fn)
  362. {
  363. kudata_t *udata = NULL;
  364. assert(scheme);
  365. if (!scheme)
  366. return BOOL_FALSE;
  367. assert(scheme->ustore);
  368. udata = kustore_slot_new(scheme->ustore, name, data, free_fn);
  369. if (!udata)
  370. return BOOL_FALSE;
  371. return BOOL_TRUE;
  372. }
  373. void *kscheme_named_udata(kscheme_t *scheme, const char *name)
  374. {
  375. assert(scheme);
  376. if (!scheme)
  377. return BOOL_FALSE;
  378. assert(scheme->ustore);
  379. return kustore_slot_data(scheme->ustore, name);
  380. }
  381. bool_t kscheme_init_session_plugins(kscheme_t *scheme, kcontext_t *context,
  382. faux_error_t *error)
  383. {
  384. bool_t retcode = BOOL_TRUE;
  385. kscheme_plugins_node_t *iter = NULL;
  386. kplugin_t *plugin = NULL;
  387. assert(scheme);
  388. if (!scheme)
  389. return BOOL_FALSE;
  390. assert(scheme->plugins);
  391. if (!context)
  392. return BOOL_FALSE;
  393. iter = kscheme_plugins_iter(scheme);
  394. while ((plugin = kscheme_plugins_each(&iter))) {
  395. int init_retcode = 0;
  396. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_INIT);
  397. kcontext_set_plugin(context, plugin);
  398. if ((init_retcode = kplugin_init_session(plugin, context)) < 0) {
  399. faux_error_sprintf(error,
  400. TAG ": Can't init session for plugin \"%s\" (%d)",
  401. kplugin_name(plugin), init_retcode);
  402. retcode = BOOL_FALSE;
  403. continue;
  404. }
  405. }
  406. return retcode;
  407. }
  408. bool_t kscheme_fini_session_plugins(kscheme_t *scheme, kcontext_t *context,
  409. faux_error_t *error)
  410. {
  411. kscheme_plugins_node_t *iter = NULL;
  412. kplugin_t *plugin = NULL;
  413. assert(scheme);
  414. if (!scheme)
  415. return BOOL_FALSE;
  416. assert(scheme->plugins);
  417. if (!context)
  418. return BOOL_FALSE;
  419. iter = kscheme_plugins_iter(scheme);
  420. while ((plugin = kscheme_plugins_each(&iter))) {
  421. int fini_retcode = -1;
  422. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_FINI);
  423. kcontext_set_plugin(context, plugin);
  424. if ((fini_retcode = kplugin_fini_session(plugin, context)) < 0) {
  425. faux_error_sprintf(error,
  426. TAG ": Can't fini session for plugin \"%s\" (%d)",
  427. kplugin_name(plugin), fini_retcode);
  428. }
  429. }
  430. return BOOL_TRUE;
  431. }