klish_lua.c 12 KB

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