klish_lua.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. val = NULL;
  167. if (pars) {
  168. entry = kpargv_command(pars);
  169. val = kentry_name(entry);
  170. }
  171. if (val) {
  172. if (!name) {
  173. lua_pushstring(L, "cmd");
  174. lua_pushstring(L, val);
  175. lua_rawset(L, -3);
  176. } else
  177. lua_pushstring(L, val);
  178. }
  179. if (name)
  180. return val?1:0;
  181. }
  182. if (!name || !strcmp(name, "pcmd")) {
  183. pars = kcontext_parent_pargv(context);
  184. val = NULL;
  185. if (pars) {
  186. entry = kpargv_command(pars);
  187. val = kentry_name(entry);
  188. }
  189. if (val) {
  190. if (!name) {
  191. lua_pushstring(L, "pcmd");
  192. lua_pushstring(L, val);
  193. lua_rawset(L, -3);
  194. } else
  195. lua_pushstring(L, val);
  196. }
  197. if (name)
  198. return val?1:0;
  199. }
  200. if (!name || !strcmp(name, "pid")) {
  201. pid_t pid = ksession_pid(kcontext_session(context));
  202. if (!name) {
  203. lua_pushstring(L, "pid");
  204. lua_pushinteger(L, pid);
  205. lua_rawset(L, -3);
  206. } else
  207. lua_pushinteger(L, pid);
  208. if (name)
  209. return 1;
  210. }
  211. if (!name || !strcmp(name, "uid")) {
  212. uid_t uid = ksession_uid(kcontext_session(context));
  213. if (!name) {
  214. lua_pushstring(L, "uid");
  215. lua_pushinteger(L, uid);
  216. lua_rawset(L, -3);
  217. } else
  218. lua_pushinteger(L, uid);
  219. if (name)
  220. return 1;
  221. }
  222. if (!name || !strcmp(name, "user")) {
  223. val = ksession_user(kcontext_session(context));
  224. if (val) {
  225. if (!name) {
  226. lua_pushstring(L, "user");
  227. lua_pushstring(L, val);
  228. lua_rawset(L, -3);
  229. } else
  230. lua_pushstring(L, val);
  231. }
  232. if (name)
  233. return val?1:0;
  234. }
  235. return name?0:1;
  236. }
  237. static int _luaB_par(lua_State *L, int parent, int multi)
  238. {
  239. unsigned int k = 0, i = 0;
  240. kcontext_t *context;
  241. const kpargv_t *pars;
  242. kpargv_pargs_node_t *par_i;
  243. kparg_t *p = NULL;
  244. const kentry_t *last_entry = NULL;
  245. struct lua_klish_data *ctx;
  246. const char *name = luaL_optstring(L, 1, NULL);
  247. if (multi)
  248. lua_newtable(L);
  249. else if (!name)
  250. return 0;
  251. ctx = lua_context(L);
  252. assert(ctx);
  253. context = ctx->context;
  254. assert(context);
  255. pars = (parent)?kcontext_parent_pargv(context):kcontext_pargv(context);
  256. if (!pars)
  257. return multi?1:0;
  258. par_i = kpargv_pargs_iter(pars);
  259. if (kpargv_pargs_len(pars) <= 0)
  260. return multi?1:0;
  261. while ((p = kpargv_pargs_each(&par_i))) {
  262. const kentry_t *entry = kparg_entry(p);
  263. const char *n = kentry_name(entry);
  264. if (!name) {
  265. if (last_entry != entry) {
  266. lua_pushnumber(L, ++k);
  267. lua_pushstring(L, n);
  268. lua_rawset(L, -3);
  269. lua_pushstring(L, n);
  270. lua_newtable(L);
  271. lua_rawset(L, -3);
  272. i = 0;
  273. }
  274. lua_pushstring(L, n);
  275. lua_rawget(L, -2);
  276. lua_pushnumber(L, ++ i);
  277. lua_pushstring(L, kparg_value(p));
  278. lua_rawset(L, -3);
  279. lua_pop(L, 1);
  280. last_entry = entry;
  281. } else if (!strcmp(n, name)) {
  282. if (!multi) {
  283. lua_pushstring(L, kparg_value(p));
  284. return 1;
  285. }
  286. lua_pushnumber(L, ++ k);
  287. lua_pushstring(L, kparg_value(p));
  288. lua_rawset(L, -3);
  289. }
  290. }
  291. return multi?1:0;
  292. }
  293. static int luaB_path(lua_State *L)
  294. {
  295. int k = 0;
  296. kpath_t *path = NULL;
  297. kpath_levels_node_t *iter = NULL;
  298. klevel_t *level = NULL;
  299. struct lua_klish_data *ctx;
  300. kcontext_t *context;
  301. ctx = lua_context(L);
  302. assert(ctx);
  303. context = ctx->context;
  304. assert(context);
  305. path = ksession_path(kcontext_session(context));
  306. assert(path);
  307. iter = kpath_iter(path);
  308. lua_newtable(L);
  309. while ((level = kpath_each(&iter))) {
  310. lua_pushnumber(L, ++ k);
  311. lua_pushstring(L, kentry_name(klevel_entry(level)));
  312. lua_rawset(L, -3);
  313. }
  314. return 1;
  315. }
  316. static int luaB_pars(lua_State *L)
  317. {
  318. return _luaB_par(L, 0, 1);
  319. }
  320. static int luaB_ppars(lua_State *L)
  321. {
  322. return _luaB_par(L, 1, 1);
  323. }
  324. static int luaB_par(lua_State *L)
  325. {
  326. return _luaB_par(L, 0, 0);
  327. }
  328. static int luaB_ppar(lua_State *L)
  329. {
  330. return _luaB_par(L, 1, 0);
  331. }
  332. static int luaopen_klish(lua_State *L)
  333. {
  334. luaL_newlib(L, klish_lib);
  335. return 1;
  336. }
  337. static int clish_env(lua_State *L)
  338. {
  339. luaL_requiref(L, "klish", luaopen_klish, 1);
  340. return 0;
  341. }
  342. static int package_path(struct lua_klish_data *ctx)
  343. {
  344. int rc = 0;
  345. char *str = NULL;
  346. char *path = ctx->package_path_sw;
  347. faux_str_cat(&str, "package.path=\"");
  348. faux_str_cat(&str, path);
  349. faux_str_cat(&str, "\"");
  350. rc = dostring(ctx, str);
  351. clear(ctx->L);
  352. faux_str_free(str);
  353. return rc;
  354. }
  355. static void lstop(lua_State *L, lua_Debug *ar)
  356. {
  357. lua_sethook(L, NULL, 0, 0);
  358. // luaL_error(L, "interrupted!");
  359. ar = ar; // Unused arg
  360. }
  361. static void laction (int i) {
  362. if (!globalL)
  363. return;
  364. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  365. globalL = NULL; // run only once
  366. i = i; // Happy compiler
  367. }
  368. static void locale_set()
  369. {
  370. setlocale(LC_NUMERIC, "C"); // to avoid . -> , in numbers
  371. setlocale(LC_CTYPE, "C"); // to avoid lower/upper problems
  372. }
  373. static void locale_reset()
  374. {
  375. setlocale(LC_NUMERIC, "");
  376. setlocale(LC_CTYPE, "");
  377. }
  378. static lua_State *lua_init(struct lua_klish_data *ctx)
  379. {
  380. lua_State *L = NULL;
  381. locale_set();
  382. #if LUA_VERSION_NUM >= 502
  383. L = luaL_newstate();
  384. #else
  385. L = lua_open();
  386. #endif
  387. if (!L) {
  388. fprintf(stderr, "Error: Failed to instantiate Lua interpreter\n");
  389. locale_reset();
  390. return NULL;
  391. }
  392. ctx->L = L; // lua state
  393. luaL_openlibs(L);
  394. if (ctx->package_path_sw && package_path(ctx)) {
  395. fprintf(stderr, "Error: Failed to define package env.\n");
  396. goto err;
  397. }
  398. if (clish_env(L)) {
  399. fprintf(stderr, "Error: Failed to define Lua clish env.\n");
  400. goto err;
  401. }
  402. if (ctx->autorun_path_sw) {
  403. if (loadscript(ctx, ctx->autorun_path_sw)) {
  404. goto err;
  405. }
  406. }
  407. globalL = L;
  408. locale_reset();
  409. return L;
  410. err:
  411. lua_close(L);
  412. locale_reset();
  413. return NULL;
  414. }
  415. static int exec_action(struct lua_klish_data *ctx, const char *script)
  416. {
  417. int rc = 0;
  418. lua_State *L = ctx->L;
  419. assert(L);
  420. globalL = L;
  421. lua_pushlightuserdata(L, ctx);
  422. lua_setglobal(L, LUA_CONTEXT);
  423. locale_set();
  424. rc = dostring(ctx, script);
  425. locale_reset();
  426. fflush(stdout);
  427. fflush(stderr);
  428. clear(L);
  429. return rc;
  430. }
  431. int klish_plugin_lua_action(kcontext_t *context)
  432. {
  433. int status = -1;
  434. const char *script = NULL;
  435. struct sigaction sig_old_int;
  436. struct sigaction sig_old_quit;
  437. struct sigaction sig_new;
  438. sigset_t sig_set;
  439. const kplugin_t *plugin;
  440. struct lua_klish_data *ctx;
  441. assert(context);
  442. plugin = kcontext_plugin(context);
  443. assert(plugin);
  444. ctx = kplugin_udata(plugin);
  445. assert(ctx);
  446. ctx->context = context;
  447. script = kcontext_script(context);
  448. if (!script) // Nothing to do
  449. return 0;
  450. sigemptyset(&sig_set);
  451. sig_new.sa_flags = 0;
  452. sig_new.sa_mask = sig_set;
  453. sig_new.sa_handler = laction;
  454. sigaction(SIGINT, &sig_new, &sig_old_int);
  455. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  456. status = exec_action(ctx, script);
  457. while ( wait(NULL) >= 0 || errno != ECHILD);
  458. // Restore SIGINT and SIGQUIT
  459. sigaction(SIGINT, &sig_old_int, NULL);
  460. sigaction(SIGQUIT, &sig_old_quit, NULL);
  461. return status;
  462. }
  463. static void free_ctx(struct lua_klish_data *ctx)
  464. {
  465. if (ctx->package_path_sw)
  466. faux_str_free(ctx->package_path_sw);
  467. if (ctx->autorun_path_sw)
  468. faux_str_free(ctx->autorun_path_sw);
  469. free(ctx);
  470. }
  471. int kplugin_lua_init(kcontext_t *context)
  472. {
  473. faux_ini_t *ini = NULL;
  474. kplugin_t *plugin = NULL;
  475. const char *p = NULL;
  476. struct lua_klish_data *ctx = NULL;
  477. const char *conf = NULL;
  478. assert(context);
  479. plugin = kcontext_plugin(context);
  480. assert(plugin);
  481. ctx = malloc(sizeof(*ctx));
  482. if (!ctx)
  483. return -1;
  484. conf = kplugin_conf(plugin);
  485. ctx->context = context;
  486. ctx->backtrace_sw = 1;
  487. ctx->package_path_sw = NULL;
  488. ctx->autorun_path_sw = NULL;
  489. ctx->L = NULL;
  490. if (conf) {
  491. ini = faux_ini_new();
  492. faux_ini_parse_str(ini, conf);
  493. p = faux_ini_find(ini, LUA_BACKTRACE_SW);
  494. ctx->backtrace_sw = p ? atoi(p) : 1;
  495. p = faux_ini_find(ini, LUA_PACKAGE_PATH_SW);
  496. ctx->package_path_sw = p ? faux_str_dup(p): NULL;
  497. p = faux_ini_find(ini, LUA_AUTORUN_SW);
  498. ctx->autorun_path_sw = p ? faux_str_dup(p): NULL;
  499. faux_ini_free(ini);
  500. }
  501. kplugin_set_udata(plugin, ctx);
  502. if (!lua_init(ctx)) {
  503. free_ctx(ctx);
  504. return -1;
  505. }
  506. kplugin_add_syms(plugin, ksym_new("lua", klish_plugin_lua_action));
  507. return 0;
  508. }
  509. int kplugin_lua_fini(kcontext_t *context)
  510. {
  511. kplugin_t *plugin = NULL;
  512. struct lua_klish_data *ctx = NULL;
  513. assert(context);
  514. plugin = kcontext_plugin(context);
  515. assert(plugin);
  516. ctx = kplugin_udata(plugin);
  517. if (!ctx)
  518. return 0;
  519. if (ctx->L)
  520. lua_close(ctx->L);
  521. free_ctx(ctx);
  522. return 0;
  523. }