klish_lua.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include <locale.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <sys/wait.h>
  6. #include <signal.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <klish/kplugin.h>
  10. #include <klish/kcontext.h>
  11. #include <faux/ini.h>
  12. #include <faux/str.h>
  13. #include <lua.h>
  14. #include <lualib.h>
  15. #include <lauxlib.h>
  16. #include "lua-compat.h"
  17. #define LUA_CONTEXT "klish_context"
  18. #define LUA_AUTORUN_SW "autostart"
  19. #define LUA_BACKTRACE_SW "backtrace"
  20. #define LUA_PACKAGE_PATH_SW "package.path"
  21. const uint8_t kplugin_lua_major = KPLUGIN_MAJOR;
  22. const uint8_t kplugin_lua_minor = KPLUGIN_MINOR;
  23. const uint8_t kplugin_lua_opt_global = 1; // RTLD_GLOBAL flag for dlopen()
  24. struct lua_klish_data {
  25. lua_State *L;
  26. kcontext_t *context;
  27. char *package_path_sw;
  28. char *autorun_path_sw;
  29. int backtrace_sw; // show traceback
  30. };
  31. static lua_State *globalL = NULL;
  32. static int luaB_par(lua_State *L);
  33. static int luaB_ppar(lua_State *L);
  34. static int luaB_pars(lua_State *L);
  35. static int luaB_ppars(lua_State *L);
  36. static int luaB_path(lua_State *L);
  37. static int luaB_context(lua_State *L);
  38. static const luaL_Reg klish_lib[] = {
  39. { "par", luaB_par },
  40. { "ppar", luaB_ppar },
  41. { "pars", luaB_pars },
  42. { "ppars", luaB_ppars },
  43. { "path", luaB_path },
  44. { "context", luaB_context },
  45. { NULL, NULL }
  46. };
  47. #if LUA_VERSION_NUM >= 502
  48. static int traceback (lua_State *L)
  49. {
  50. const char *msg = lua_tostring(L, 1);
  51. if (msg)
  52. luaL_traceback(L, L, msg, 1);
  53. else if (!lua_isnoneornil(L, 1)) { // is there an error object?
  54. if (!luaL_callmeta(L, 1, "__tostring")) // try its 'tostring' metamethod
  55. lua_pushliteral(L, "(no error message)");
  56. }
  57. return 1;
  58. }
  59. #else
  60. static int traceback (lua_State *L)
  61. {
  62. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  63. if (!lua_istable(L, -1)) {
  64. lua_pop(L, 1);
  65. return 1;
  66. }
  67. lua_getfield(L, -1, "traceback");
  68. if (!lua_isfunction(L, -1)) {
  69. lua_pop(L, 2);
  70. return 1;
  71. }
  72. lua_pushvalue(L, 1); // pass error message
  73. lua_pushinteger(L, 2); // skip this function and traceback
  74. lua_call(L, 2, 1); // call debug.traceback
  75. return 1;
  76. }
  77. #endif
  78. static int report (lua_State *L, int status)
  79. {
  80. if (status && !lua_isnil(L, -1)) {
  81. const char *msg = lua_tostring(L, -1);
  82. if (msg == NULL)
  83. msg = "(error object is not a string)";
  84. fprintf(stderr,"Error: %s\n", msg);
  85. lua_pop(L, 1);
  86. status = -1;
  87. }
  88. return status;
  89. }
  90. static int docall(struct lua_klish_data *ctx, int narg)
  91. {
  92. int status = 0;
  93. int base = 0;
  94. if (ctx->backtrace_sw) {
  95. base = lua_gettop(ctx->L) - narg; // function index
  96. lua_pushcfunction(ctx->L, traceback); // push traceback function
  97. lua_insert(ctx->L, base); // put it under chunk and args
  98. }
  99. status = lua_pcall(ctx->L, narg, LUA_MULTRET, base);
  100. if (ctx->backtrace_sw)
  101. lua_remove(ctx->L, base); // remove traceback function
  102. // force a complete garbage collection in case of errors
  103. if (status != 0)
  104. lua_gc(ctx->L, LUA_GCCOLLECT, 0);
  105. return status;
  106. }
  107. static int clear(lua_State *L)
  108. {
  109. int N = lua_gettop(L);
  110. lua_pop(L, N);
  111. return 0;
  112. }
  113. static int loadscript(struct lua_klish_data *ctx, const char *path)
  114. {
  115. int status = 0;
  116. status = luaL_loadfile(ctx->L, path);
  117. if (!status) {
  118. status = docall(ctx, 0);
  119. }
  120. status = report(ctx->L, status);
  121. clear(ctx->L);
  122. return status;
  123. }
  124. static int dostring(struct lua_klish_data *ctx, const char *s)
  125. {
  126. int status = luaL_loadstring(ctx->L, s) || docall(ctx, 0);
  127. return report(ctx->L, status);
  128. }
  129. static struct lua_klish_data *lua_context(lua_State *L)
  130. {
  131. struct lua_klish_data *ctx;
  132. lua_getglobal(L, LUA_CONTEXT);
  133. ctx = lua_touserdata(L, -1);
  134. lua_pop(L, 1);
  135. return ctx;
  136. }
  137. static int luaB_context(lua_State *L)
  138. {
  139. const kpargv_t *pars = NULL;
  140. const kentry_t *entry;
  141. const char *val = NULL;
  142. struct lua_klish_data *ctx;
  143. kcontext_t *context;
  144. const char *name = luaL_optstring(L, 1, NULL);
  145. if (!name)
  146. lua_newtable(L);
  147. ctx = lua_context(L);
  148. assert(ctx);
  149. context = ctx->context;
  150. assert(context);
  151. if (!name || !strcmp(name, "val")) {
  152. val = kcontext_candidate_value(context);
  153. if (val) {
  154. if (!name) {
  155. lua_pushstring(L, "val");
  156. lua_pushstring(L, kcontext_candidate_value(context));
  157. lua_rawset(L, -3);
  158. } else
  159. lua_pushstring(L, val);
  160. }
  161. if (name)
  162. return val?1:0;
  163. }
  164. if (!name || !strcmp(name, "cmd")) {
  165. pars = kcontext_pargv(context);
  166. entry = kpargv_command(pars);
  167. val = kentry_name(entry);
  168. if (val) {
  169. if (!name) {
  170. lua_pushstring(L, "cmd");
  171. lua_pushstring(L, val);
  172. lua_rawset(L, -3);
  173. } else
  174. lua_pushstring(L, val);
  175. }
  176. if (name)
  177. return val?1:0;
  178. }
  179. if (!name || !strcmp(name, "pcmd")) {
  180. pars = kcontext_parent_pargv(context);
  181. entry = kpargv_command(pars);
  182. val = kentry_name(entry);
  183. if (val) {
  184. if (!name) {
  185. lua_pushstring(L, "pcmd");
  186. lua_pushstring(L, val);
  187. lua_rawset(L, -3);
  188. } else
  189. lua_pushstring(L, val);
  190. }
  191. if (name)
  192. return val?1:0;
  193. }
  194. if (!name || !strcmp(name, "pid")) {
  195. pid_t pid = ksession_pid(kcontext_session(context));
  196. if (!name) {
  197. lua_pushstring(L, "pid");
  198. lua_pushinteger(L, pid);
  199. lua_rawset(L, -3);
  200. } else
  201. lua_pushinteger(L, pid);
  202. if (name)
  203. return 1;
  204. }
  205. if (!name || !strcmp(name, "uid")) {
  206. uid_t uid = ksession_uid(kcontext_session(context));
  207. if (!name) {
  208. lua_pushstring(L, "uid");
  209. lua_pushinteger(L, uid);
  210. lua_rawset(L, -3);
  211. } else
  212. lua_pushinteger(L, uid);
  213. if (name)
  214. return 1;
  215. }
  216. if (!name || !strcmp(name, "user")) {
  217. val = ksession_user(kcontext_session(context));
  218. if (val) {
  219. if (!name) {
  220. lua_pushstring(L, "user");
  221. lua_pushstring(L, val);
  222. lua_rawset(L, -3);
  223. } else
  224. lua_pushstring(L, val);
  225. }
  226. if (name)
  227. return val?1:0;
  228. }
  229. return name?0:1;
  230. }
  231. static int _luaB_par(lua_State *L, int parent, int multi)
  232. {
  233. unsigned int k = 0, i = 0;
  234. kcontext_t *context;
  235. const kpargv_t *pars;
  236. kpargv_pargs_node_t *par_i;
  237. kparg_t *p = NULL;
  238. const kentry_t *last_entry = NULL;
  239. struct lua_klish_data *ctx;
  240. const char *name = luaL_optstring(L, 1, NULL);
  241. if (multi)
  242. lua_newtable(L);
  243. else if (!name)
  244. return 0;
  245. ctx = lua_context(L);
  246. assert(ctx);
  247. context = ctx->context;
  248. assert(context);
  249. pars = (parent)?kcontext_parent_pargv(context):kcontext_pargv(context);
  250. if (!pars)
  251. return multi?1:0;
  252. par_i = kpargv_pargs_iter(pars);
  253. if (kpargv_pargs_len(pars) <= 0)
  254. return multi?1:0;
  255. while ((p = kpargv_pargs_each(&par_i))) {
  256. const kentry_t *entry = kparg_entry(p);
  257. const char *n = kentry_name(entry);
  258. if (!name) {
  259. if (last_entry != entry) {
  260. lua_pushnumber(L, ++k);
  261. lua_pushstring(L, n);
  262. lua_rawset(L, -3);
  263. lua_pushstring(L, n);
  264. lua_newtable(L);
  265. lua_rawset(L, -3);
  266. i = 0;
  267. }
  268. lua_pushstring(L, n);
  269. lua_rawget(L, -2);
  270. lua_pushnumber(L, ++ i);
  271. lua_pushstring(L, kparg_value(p));
  272. lua_rawset(L, -3);
  273. lua_pop(L, 1);
  274. last_entry = entry;
  275. } else if (!strcmp(n, name)) {
  276. if (!multi) {
  277. lua_pushstring(L, kparg_value(p));
  278. return 1;
  279. }
  280. lua_pushnumber(L, ++ k);
  281. lua_pushstring(L, kparg_value(p));
  282. lua_rawset(L, -3);
  283. }
  284. }
  285. return multi?1:0;
  286. }
  287. static int luaB_path(lua_State *L)
  288. {
  289. int k = 0;
  290. kpath_t *path = NULL;
  291. kpath_levels_node_t *iter = NULL;
  292. klevel_t *level = NULL;
  293. struct lua_klish_data *ctx;
  294. kcontext_t *context;
  295. ctx = lua_context(L);
  296. assert(ctx);
  297. context = ctx->context;
  298. assert(context);
  299. path = ksession_path(kcontext_session(context));
  300. assert(path);
  301. iter = kpath_iter(path);
  302. lua_newtable(L);
  303. while ((level = kpath_each(&iter))) {
  304. lua_pushnumber(L, ++ k);
  305. lua_pushstring(L, kentry_name(klevel_entry(level)));
  306. lua_rawset(L, -3);
  307. }
  308. return 1;
  309. }
  310. static int luaB_pars(lua_State *L)
  311. {
  312. return _luaB_par(L, 0, 1);
  313. }
  314. static int luaB_ppars(lua_State *L)
  315. {
  316. return _luaB_par(L, 1, 1);
  317. }
  318. static int luaB_par(lua_State *L)
  319. {
  320. return _luaB_par(L, 0, 0);
  321. }
  322. static int luaB_ppar(lua_State *L)
  323. {
  324. return _luaB_par(L, 1, 0);
  325. }
  326. static int luaopen_klish(lua_State *L)
  327. {
  328. luaL_newlib(L, klish_lib);
  329. return 1;
  330. }
  331. static int clish_env(lua_State *L)
  332. {
  333. luaL_requiref(L, "klish", luaopen_klish, 1);
  334. return 0;
  335. }
  336. static int package_path(struct lua_klish_data *ctx)
  337. {
  338. int rc = 0;
  339. char *str = NULL;
  340. char *path = ctx->package_path_sw;
  341. faux_str_cat(&str, "package.path=\"");
  342. faux_str_cat(&str, path);
  343. faux_str_cat(&str, "\"");
  344. rc = dostring(ctx, str);
  345. clear(ctx->L);
  346. faux_str_free(str);
  347. return rc;
  348. }
  349. static void lstop(lua_State *L, lua_Debug *ar)
  350. {
  351. lua_sethook(L, NULL, 0, 0);
  352. // luaL_error(L, "interrupted!");
  353. ar = ar; // Unused arg
  354. }
  355. static void laction (int i) {
  356. if (!globalL)
  357. return;
  358. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  359. globalL = NULL; // run only once
  360. i = i; // Happy compiler
  361. }
  362. static void locale_set()
  363. {
  364. setlocale(LC_NUMERIC, "C"); // to avoid . -> , in numbers
  365. setlocale(LC_CTYPE, "C"); // to avoid lower/upper problems
  366. }
  367. static void locale_reset()
  368. {
  369. setlocale(LC_NUMERIC, "");
  370. setlocale(LC_CTYPE, "");
  371. }
  372. static lua_State *lua_init(struct lua_klish_data *ctx)
  373. {
  374. lua_State *L = NULL;
  375. locale_set();
  376. #if LUA_VERSION_NUM >= 502
  377. L = luaL_newstate();
  378. #else
  379. L = lua_open();
  380. #endif
  381. if (!L) {
  382. fprintf(stderr, "Error: Failed to instantiate Lua interpreter\n");
  383. locale_reset();
  384. return NULL;
  385. }
  386. ctx->L = L; // lua state
  387. luaL_openlibs(L);
  388. if (ctx->package_path_sw && package_path(ctx)) {
  389. fprintf(stderr, "Error: Failed to define package env.\n");
  390. goto err;
  391. }
  392. if (clish_env(L)) {
  393. fprintf(stderr, "Error: Failed to define Lua clish env.\n");
  394. goto err;
  395. }
  396. if (ctx->autorun_path_sw) {
  397. if (loadscript(ctx, ctx->autorun_path_sw)) {
  398. goto err;
  399. }
  400. }
  401. globalL = L;
  402. locale_reset();
  403. return L;
  404. err:
  405. lua_close(L);
  406. locale_reset();
  407. return NULL;
  408. }
  409. static int exec_action(struct lua_klish_data *ctx, const char *script)
  410. {
  411. int rc = 0;
  412. lua_State *L = ctx->L;
  413. assert(L);
  414. globalL = L;
  415. lua_pushlightuserdata(L, ctx);
  416. lua_setglobal(L, LUA_CONTEXT);
  417. locale_set();
  418. rc = dostring(ctx, script);
  419. locale_reset();
  420. fflush(stdout);
  421. fflush(stderr);
  422. clear(L);
  423. return rc;
  424. }
  425. int klish_plugin_lua_action(kcontext_t *context)
  426. {
  427. int status = -1;
  428. const char *script = NULL;
  429. struct sigaction sig_old_int;
  430. struct sigaction sig_old_quit;
  431. struct sigaction sig_new;
  432. sigset_t sig_set;
  433. const kplugin_t *plugin;
  434. struct lua_klish_data *ctx;
  435. assert(context);
  436. plugin = kcontext_plugin(context);
  437. assert(plugin);
  438. ctx = kplugin_udata(plugin);
  439. assert(ctx);
  440. ctx->context = context;
  441. script = kcontext_script(context);
  442. if (!script) // Nothing to do
  443. return 0;
  444. sigemptyset(&sig_set);
  445. sig_new.sa_flags = 0;
  446. sig_new.sa_mask = sig_set;
  447. sig_new.sa_handler = laction;
  448. sigaction(SIGINT, &sig_new, &sig_old_int);
  449. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  450. status = exec_action(ctx, script);
  451. while ( wait(NULL) >= 0 || errno != ECHILD);
  452. // Restore SIGINT and SIGQUIT
  453. sigaction(SIGINT, &sig_old_int, NULL);
  454. sigaction(SIGQUIT, &sig_old_quit, NULL);
  455. return status;
  456. }
  457. static void free_ctx(struct lua_klish_data *ctx)
  458. {
  459. if (ctx->package_path_sw)
  460. faux_str_free(ctx->package_path_sw);
  461. if (ctx->autorun_path_sw)
  462. faux_str_free(ctx->autorun_path_sw);
  463. free(ctx);
  464. }
  465. int kplugin_lua_init(kcontext_t *context)
  466. {
  467. faux_ini_t *ini = NULL;
  468. kplugin_t *plugin = NULL;
  469. const char *p = NULL;
  470. struct lua_klish_data *ctx = NULL;
  471. const char *conf = NULL;
  472. assert(context);
  473. plugin = kcontext_plugin(context);
  474. assert(plugin);
  475. ctx = malloc(sizeof(*ctx));
  476. if (!ctx)
  477. return -1;
  478. conf = kplugin_conf(plugin);
  479. ctx->context = context;
  480. ctx->backtrace_sw = 1;
  481. ctx->package_path_sw = NULL;
  482. ctx->autorun_path_sw = NULL;
  483. ctx->L = NULL;
  484. if (conf) {
  485. ini = faux_ini_new();
  486. faux_ini_parse_str(ini, conf);
  487. p = faux_ini_find(ini, LUA_BACKTRACE_SW);
  488. ctx->backtrace_sw = p ? atoi(p) : 1;
  489. p = faux_ini_find(ini, LUA_PACKAGE_PATH_SW);
  490. ctx->package_path_sw = p ? faux_str_dup(p): NULL;
  491. p = faux_ini_find(ini, LUA_AUTORUN_SW);
  492. ctx->autorun_path_sw = p ? faux_str_dup(p): NULL;
  493. faux_ini_free(ini);
  494. }
  495. kplugin_set_udata(plugin, ctx);
  496. if (!lua_init(ctx)) {
  497. free_ctx(ctx);
  498. return -1;
  499. }
  500. kplugin_add_syms(plugin, ksym_new("lua", klish_plugin_lua_action));
  501. return 0;
  502. }
  503. int kplugin_lua_fini(kcontext_t *context)
  504. {
  505. kplugin_t *plugin = NULL;
  506. struct lua_klish_data *ctx = NULL;
  507. assert(context);
  508. plugin = kcontext_plugin(context);
  509. assert(plugin);
  510. ctx = kplugin_udata(plugin);
  511. if (!ctx)
  512. return 0;
  513. if (ctx->L)
  514. lua_close(ctx->L);
  515. free_ctx(ctx);
  516. return 0;
  517. }