klish_lua.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. if (!kentry_container(entry)) {
  267. lua_pushnumber(L, ++k);
  268. lua_pushstring(L, n);
  269. lua_rawset(L, -3);
  270. }
  271. lua_pushstring(L, n);
  272. lua_newtable(L);
  273. lua_rawset(L, -3);
  274. i = 0;
  275. }
  276. lua_pushstring(L, n);
  277. lua_rawget(L, -2);
  278. lua_pushnumber(L, ++ i);
  279. lua_pushstring(L, kparg_value(p));
  280. lua_rawset(L, -3);
  281. lua_pop(L, 1);
  282. last_entry = entry;
  283. } else if (!strcmp(n, name)) {
  284. if (!multi) {
  285. lua_pushstring(L, kparg_value(p));
  286. return 1;
  287. }
  288. lua_pushnumber(L, ++ k);
  289. lua_pushstring(L, kparg_value(p));
  290. lua_rawset(L, -3);
  291. }
  292. }
  293. return multi?1:0;
  294. }
  295. static int luaB_path(lua_State *L)
  296. {
  297. int k = 0;
  298. kpath_t *path = NULL;
  299. kpath_levels_node_t *iter = NULL;
  300. klevel_t *level = NULL;
  301. struct lua_klish_data *ctx;
  302. kcontext_t *context;
  303. ctx = lua_context(L);
  304. assert(ctx);
  305. context = ctx->context;
  306. assert(context);
  307. path = ksession_path(kcontext_session(context));
  308. assert(path);
  309. iter = kpath_iter(path);
  310. lua_newtable(L);
  311. while ((level = kpath_each(&iter))) {
  312. lua_pushnumber(L, ++ k);
  313. lua_pushstring(L, kentry_name(klevel_entry(level)));
  314. lua_rawset(L, -3);
  315. }
  316. return 1;
  317. }
  318. static int luaB_pars(lua_State *L)
  319. {
  320. return _luaB_par(L, 0, 1);
  321. }
  322. static int luaB_ppars(lua_State *L)
  323. {
  324. return _luaB_par(L, 1, 1);
  325. }
  326. static int luaB_par(lua_State *L)
  327. {
  328. return _luaB_par(L, 0, 0);
  329. }
  330. static int luaB_ppar(lua_State *L)
  331. {
  332. return _luaB_par(L, 1, 0);
  333. }
  334. static int luaopen_klish(lua_State *L)
  335. {
  336. luaL_newlib(L, klish_lib);
  337. return 1;
  338. }
  339. static int clish_env(lua_State *L)
  340. {
  341. luaL_requiref(L, "klish", luaopen_klish, 1);
  342. return 0;
  343. }
  344. static int package_path(struct lua_klish_data *ctx)
  345. {
  346. int rc = 0;
  347. char *str = NULL;
  348. char *path = ctx->package_path_sw;
  349. faux_str_cat(&str, "package.path=\"");
  350. faux_str_cat(&str, path);
  351. faux_str_cat(&str, "\"");
  352. rc = dostring(ctx, str);
  353. clear(ctx->L);
  354. faux_str_free(str);
  355. return rc;
  356. }
  357. static void lstop(lua_State *L, lua_Debug *ar)
  358. {
  359. lua_sethook(L, NULL, 0, 0);
  360. // luaL_error(L, "interrupted!");
  361. ar = ar; // Unused arg
  362. }
  363. static void laction (int i) {
  364. if (!globalL)
  365. return;
  366. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  367. globalL = NULL; // run only once
  368. i = i; // Happy compiler
  369. }
  370. static void locale_set()
  371. {
  372. setlocale(LC_NUMERIC, "C"); // to avoid . -> , in numbers
  373. setlocale(LC_CTYPE, "C"); // to avoid lower/upper problems
  374. }
  375. static void locale_reset()
  376. {
  377. setlocale(LC_NUMERIC, "");
  378. setlocale(LC_CTYPE, "");
  379. }
  380. static lua_State *lua_init(struct lua_klish_data *ctx)
  381. {
  382. lua_State *L = NULL;
  383. locale_set();
  384. #if LUA_VERSION_NUM >= 502
  385. L = luaL_newstate();
  386. #else
  387. L = lua_open();
  388. #endif
  389. if (!L) {
  390. fprintf(stderr, "Error: Failed to instantiate Lua interpreter\n");
  391. locale_reset();
  392. return NULL;
  393. }
  394. ctx->L = L; // lua state
  395. luaL_openlibs(L);
  396. if (ctx->package_path_sw && package_path(ctx)) {
  397. fprintf(stderr, "Error: Failed to define package env.\n");
  398. goto err;
  399. }
  400. if (clish_env(L)) {
  401. fprintf(stderr, "Error: Failed to define Lua clish env.\n");
  402. goto err;
  403. }
  404. if (ctx->autorun_path_sw) {
  405. if (loadscript(ctx, ctx->autorun_path_sw)) {
  406. goto err;
  407. }
  408. }
  409. globalL = L;
  410. locale_reset();
  411. return L;
  412. err:
  413. lua_close(L);
  414. locale_reset();
  415. return NULL;
  416. }
  417. static int exec_action(struct lua_klish_data *ctx, const char *script)
  418. {
  419. int rc = 0;
  420. lua_State *L = ctx->L;
  421. assert(L);
  422. globalL = L;
  423. lua_pushlightuserdata(L, ctx);
  424. lua_setglobal(L, LUA_CONTEXT);
  425. locale_set();
  426. rc = dostring(ctx, script);
  427. locale_reset();
  428. fflush(stdout);
  429. fflush(stderr);
  430. clear(L);
  431. return rc;
  432. }
  433. int klish_plugin_lua_action(kcontext_t *context)
  434. {
  435. int status = -1;
  436. const char *script = NULL;
  437. struct sigaction sig_old_int;
  438. struct sigaction sig_old_quit;
  439. struct sigaction sig_new;
  440. sigset_t sig_set;
  441. const kplugin_t *plugin;
  442. struct lua_klish_data *ctx;
  443. assert(context);
  444. plugin = kcontext_plugin(context);
  445. assert(plugin);
  446. ctx = kplugin_udata(plugin);
  447. assert(ctx);
  448. ctx->context = context;
  449. script = kcontext_script(context);
  450. if (!script) // Nothing to do
  451. return 0;
  452. sigemptyset(&sig_set);
  453. sig_new.sa_flags = 0;
  454. sig_new.sa_mask = sig_set;
  455. sig_new.sa_handler = laction;
  456. sigaction(SIGINT, &sig_new, &sig_old_int);
  457. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  458. status = exec_action(ctx, script);
  459. while ( wait(NULL) >= 0 || errno != ECHILD);
  460. // Restore SIGINT and SIGQUIT
  461. sigaction(SIGINT, &sig_old_int, NULL);
  462. sigaction(SIGQUIT, &sig_old_quit, NULL);
  463. return status;
  464. }
  465. static void free_ctx(struct lua_klish_data *ctx)
  466. {
  467. if (ctx->package_path_sw)
  468. faux_str_free(ctx->package_path_sw);
  469. if (ctx->autorun_path_sw)
  470. faux_str_free(ctx->autorun_path_sw);
  471. free(ctx);
  472. }
  473. int kplugin_lua_init(kcontext_t *context)
  474. {
  475. faux_ini_t *ini = NULL;
  476. kplugin_t *plugin = NULL;
  477. const char *p = NULL;
  478. struct lua_klish_data *ctx = NULL;
  479. const char *conf = NULL;
  480. assert(context);
  481. plugin = kcontext_plugin(context);
  482. assert(plugin);
  483. ctx = malloc(sizeof(*ctx));
  484. if (!ctx)
  485. return -1;
  486. conf = kplugin_conf(plugin);
  487. ctx->context = context;
  488. ctx->backtrace_sw = 1;
  489. ctx->package_path_sw = NULL;
  490. ctx->autorun_path_sw = NULL;
  491. ctx->L = NULL;
  492. if (conf) {
  493. ini = faux_ini_new();
  494. faux_ini_parse_str(ini, conf);
  495. p = faux_ini_find(ini, LUA_BACKTRACE_SW);
  496. ctx->backtrace_sw = p ? atoi(p) : 1;
  497. p = faux_ini_find(ini, LUA_PACKAGE_PATH_SW);
  498. ctx->package_path_sw = p ? faux_str_dup(p): NULL;
  499. p = faux_ini_find(ini, LUA_AUTORUN_SW);
  500. ctx->autorun_path_sw = p ? faux_str_dup(p): NULL;
  501. faux_ini_free(ini);
  502. }
  503. kplugin_set_udata(plugin, ctx);
  504. if (!lua_init(ctx)) {
  505. free_ctx(ctx);
  506. return -1;
  507. }
  508. kplugin_add_syms(plugin, ksym_new("lua", klish_plugin_lua_action));
  509. return 0;
  510. }
  511. int kplugin_lua_fini(kcontext_t *context)
  512. {
  513. kplugin_t *plugin = NULL;
  514. struct lua_klish_data *ctx = NULL;
  515. assert(context);
  516. plugin = kcontext_plugin(context);
  517. assert(plugin);
  518. ctx = kplugin_udata(plugin);
  519. if (!ctx)
  520. return 0;
  521. if (ctx->L)
  522. lua_close(ctx->L);
  523. free_ctx(ctx);
  524. return 0;
  525. }