shell_execute.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * shell_execute.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include "lub/argv.h"
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <sys/file.h>
  16. #include <signal.h>
  17. #include <fcntl.h>
  18. /*
  19. * These are the internal commands for this framework.
  20. */
  21. static clish_shell_builtin_fn_t
  22. clish_close,
  23. clish_overview,
  24. clish_source,
  25. clish_source_nostop,
  26. clish_history,
  27. clish_nested_up,
  28. clish_nop,
  29. clish_wdog,
  30. clish_macros;
  31. static clish_shell_builtin_t clish_cmd_list[] = {
  32. {"clish_close", clish_close},
  33. {"clish_overview", clish_overview},
  34. {"clish_source", clish_source},
  35. {"clish_source_nostop", clish_source_nostop},
  36. {"clish_history", clish_history},
  37. {"clish_nested_up", clish_nested_up},
  38. {"clish_nop", clish_nop},
  39. {"clish_wdog", clish_wdog},
  40. {"clish_macros", clish_macros},
  41. {NULL, NULL}
  42. };
  43. /*----------------------------------------------------------- */
  44. /* Terminate the current shell session */
  45. static int clish_close(clish_context_t *context, const lub_argv_t *argv,
  46. const char *script, char **out)
  47. {
  48. /* the exception proves the rule... */
  49. clish_shell_t *this = (clish_shell_t *)context->shell;
  50. argv = argv; /* not used */
  51. this->state = SHELL_STATE_CLOSING;
  52. return 0;
  53. }
  54. /*----------------------------------------------------------- */
  55. /*
  56. Open a file and interpret it as a script in the context of a new
  57. thread. Whether the script continues after command, but not script,
  58. errors depends on the value of the stop_on_error flag.
  59. */
  60. static int clish_source_internal(clish_context_t *context,
  61. const lub_argv_t * argv, bool_t stop_on_error)
  62. {
  63. int result = -1;
  64. const char *filename = lub_argv__get_arg(argv, 0);
  65. struct stat fileStat;
  66. /* the exception proves the rule... */
  67. clish_shell_t *this = (clish_shell_t *)context->shell;
  68. /*
  69. * Check file specified is not a directory
  70. */
  71. if ((0 == stat((char *)filename, &fileStat)) &&
  72. (!S_ISDIR(fileStat.st_mode))) {
  73. /*
  74. * push this file onto the file stack associated with this
  75. * session. This will be closed by clish_shell_pop_file()
  76. * when it is finished with.
  77. */
  78. result = clish_shell_push_file(this, filename,
  79. stop_on_error);
  80. }
  81. return result ? -1 : 0;
  82. }
  83. /*----------------------------------------------------------- */
  84. /*
  85. Open a file and interpret it as a script in the context of a new
  86. thread. Invoking a script in this way will cause the script to
  87. stop on the first error
  88. */
  89. static int clish_source(clish_context_t *context, const lub_argv_t *argv,
  90. const char *script, char **out)
  91. {
  92. return (clish_source_internal(context, argv, BOOL_TRUE));
  93. }
  94. /*----------------------------------------------------------- */
  95. /*
  96. Open a file and interpret it as a script in the context of a new
  97. thread. Invoking a script in this way will cause the script to
  98. continue after command, but not script, errors.
  99. */
  100. static int clish_source_nostop(clish_context_t *context, const lub_argv_t *argv,
  101. const char *script, char **out)
  102. {
  103. return (clish_source_internal(context, argv, BOOL_FALSE));
  104. }
  105. /*----------------------------------------------------------- */
  106. /*
  107. Show the shell overview
  108. */
  109. static int clish_overview(clish_context_t *context, const lub_argv_t *argv,
  110. const char *script, char **out)
  111. {
  112. clish_shell_t *this = context->shell;
  113. argv = argv; /* not used */
  114. tinyrl_printf(this->tinyrl, "%s\n", context->shell->overview);
  115. return 0;
  116. }
  117. /*----------------------------------------------------------- */
  118. static int clish_history(clish_context_t *context, const lub_argv_t *argv,
  119. const char *script, char **out)
  120. {
  121. clish_shell_t *this = context->shell;
  122. tinyrl_history_t *history = tinyrl__get_history(this->tinyrl);
  123. tinyrl_history_iterator_t iter;
  124. const tinyrl_history_entry_t *entry;
  125. unsigned limit = 0;
  126. const char *arg = lub_argv__get_arg(argv, 0);
  127. if (arg && ('\0' != *arg)) {
  128. limit = (unsigned)atoi(arg);
  129. if (0 == limit) {
  130. /* unlimit the history list */
  131. (void)tinyrl_history_unstifle(history);
  132. } else {
  133. /* limit the scope of the history list */
  134. tinyrl_history_stifle(history, limit);
  135. }
  136. }
  137. for (entry = tinyrl_history_getfirst(history, &iter);
  138. entry; entry = tinyrl_history_getnext(&iter)) {
  139. /* dump the details of this entry */
  140. tinyrl_printf(this->tinyrl,
  141. "%5d %s\n",
  142. tinyrl_history_entry__get_index(entry),
  143. tinyrl_history_entry__get_line(entry));
  144. }
  145. return 0;
  146. }
  147. /*----------------------------------------------------------- */
  148. /*
  149. * Searches for a builtin command to execute
  150. */
  151. static clish_shell_builtin_fn_t *find_builtin_callback(const
  152. clish_shell_builtin_t * cmd_list, const char *name)
  153. {
  154. const clish_shell_builtin_t *result;
  155. /* search a list of commands */
  156. for (result = cmd_list; result && result->name; result++) {
  157. if (0 == strcmp(name, result->name))
  158. break;
  159. }
  160. return (result && result->name) ? result->callback : NULL;
  161. }
  162. /*----------------------------------------------------------- */
  163. void clish_shell_cleanup_script(void *script)
  164. {
  165. /* simply release the memory */
  166. lub_string_free(script);
  167. }
  168. /*-------------------------------------------------------- */
  169. static int clish_shell_lock(const char *lock_path)
  170. {
  171. int i;
  172. int res;
  173. int lock_fd = -1;
  174. struct flock lock;
  175. if (!lock_path)
  176. return -1;
  177. lock_fd = open(lock_path, O_WRONLY | O_CREAT, 00644);
  178. if (-1 == lock_fd) {
  179. fprintf(stderr, "Can't open lockfile %s.\n", lock_path);
  180. return -1;
  181. }
  182. lock.l_type = F_WRLCK;
  183. lock.l_whence = SEEK_SET;
  184. lock.l_start = 0;
  185. lock.l_len = 0;
  186. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  187. res = fcntl(lock_fd, F_SETLK, &lock);
  188. if (res != -1)
  189. break;
  190. if (EINTR == errno)
  191. continue;
  192. if ((EAGAIN == errno) || (EACCES == errno)) {
  193. if (0 == i)
  194. fprintf(stderr,
  195. "Try to get lock. Please wait...\n");
  196. sleep(1);
  197. continue;
  198. }
  199. break;
  200. }
  201. if (res == -1) {
  202. fprintf(stderr, "Can't get lock.\n");
  203. close(lock_fd);
  204. return -1;
  205. }
  206. return lock_fd;
  207. }
  208. /*-------------------------------------------------------- */
  209. static void clish_shell_unlock(int lock_fd)
  210. {
  211. struct flock lock;
  212. if (lock_fd == -1)
  213. return;
  214. lock.l_type = F_UNLCK;
  215. lock.l_whence = SEEK_SET;
  216. lock.l_start = 0;
  217. lock.l_len = 0;
  218. fcntl(lock_fd, F_SETLK, &lock);
  219. close(lock_fd);
  220. }
  221. /*----------------------------------------------------------- */
  222. int clish_shell_execute(clish_context_t *context, char **out)
  223. {
  224. clish_shell_t *this = context->shell;
  225. const clish_command_t *cmd = context->cmd;
  226. clish_action_t *action;
  227. int result = 0;
  228. char *lock_path = clish_shell__get_lockfile(this);
  229. int lock_fd = -1;
  230. sigset_t old_sigs;
  231. struct sigaction old_sigint, old_sigquit, old_sighup;
  232. clish_view_t *cur_view = clish_shell__get_view(this);
  233. unsigned int saved_wdog_timeout = this->wdog_timeout;
  234. assert(cmd);
  235. action = clish_command__get_action(cmd);
  236. /* Pre-change view if the command is from another depth/view */
  237. {
  238. clish_view_restore_t restore = clish_command__get_restore(cmd);
  239. if ((CLISH_RESTORE_VIEW == restore) &&
  240. (clish_command__get_pview(cmd) != cur_view)) {
  241. clish_view_t *view = clish_command__get_pview(cmd);
  242. clish_shell__set_pwd(this, NULL, view, NULL, context);
  243. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  244. (clish_command__get_depth(cmd) < this->depth)) {
  245. this->depth = clish_command__get_depth(cmd);
  246. }
  247. }
  248. /* Lock the lockfile */
  249. if (lock_path && clish_command__get_lock(cmd)) {
  250. lock_fd = clish_shell_lock(lock_path);
  251. if (-1 == lock_fd) {
  252. result = -1;
  253. goto error; /* Can't set lock */
  254. }
  255. }
  256. /* Ignore and block SIGINT, SIGQUIT, SIGHUP */
  257. if (!clish_command__get_interrupt(cmd)) {
  258. struct sigaction sa;
  259. sigset_t sigs;
  260. sa.sa_flags = 0;
  261. sigemptyset(&sa.sa_mask);
  262. sa.sa_handler = SIG_IGN;
  263. sigaction(SIGINT, &sa, &old_sigint);
  264. sigaction(SIGQUIT, &sa, &old_sigquit);
  265. sigaction(SIGHUP, &sa, &old_sighup);
  266. sigemptyset(&sigs);
  267. sigaddset(&sigs, SIGINT);
  268. sigaddset(&sigs, SIGQUIT);
  269. sigaddset(&sigs, SIGHUP);
  270. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  271. }
  272. /* Execute ACTION */
  273. result = clish_shell_exec_action(action, context, out);
  274. /* Restore SIGINT, SIGQUIT, SIGHUP */
  275. if (!clish_command__get_interrupt(cmd)) {
  276. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  277. /* Is the signals delivery guaranteed here (before
  278. sigaction restore) for previously blocked and
  279. pending signals? The simple test is working well.
  280. I don't want to use sigtimedwait() function bacause
  281. it needs a realtime extensions. The sigpending() with
  282. the sleep() is not nice too. Report bug if clish will
  283. get the SIGINT after non-interruptable action.
  284. */
  285. sigaction(SIGINT, &old_sigint, NULL);
  286. sigaction(SIGQUIT, &old_sigquit, NULL);
  287. sigaction(SIGHUP, &old_sighup, NULL);
  288. }
  289. /* Call config callback */
  290. if (!result && this->client_hooks->config_fn)
  291. this->client_hooks->config_fn(context);
  292. /* Call logging callback */
  293. if (clish_shell__get_log(this) && this->client_hooks->log_fn) {
  294. char *full_line = clish_shell__get_full_line(context);
  295. this->client_hooks->log_fn(context, full_line, result);
  296. lub_string_free(full_line);
  297. }
  298. /* Unlock the lockfile */
  299. if (lock_fd != -1)
  300. clish_shell_unlock(lock_fd);
  301. /* Move into the new view */
  302. if (!result) {
  303. clish_view_t *view = clish_command__get_view(cmd);
  304. /* Save the PWD */
  305. if (view) {
  306. char *line = clish_shell__get_line(context);
  307. clish_shell__set_pwd(this, line, view,
  308. clish_command__get_viewid(cmd), context);
  309. lub_string_free(line);
  310. }
  311. }
  312. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  313. on the "set watchdog <timeout>" command itself. */
  314. if (this->wdog_timeout && saved_wdog_timeout) {
  315. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  316. this->wdog_active = BOOL_TRUE;
  317. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  318. "seconds.\nWarning: Press any key to stop watchdog.\n",
  319. this->wdog_timeout);
  320. } else
  321. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  322. error:
  323. return result;
  324. }
  325. /*----------------------------------------------------------- */
  326. int clish_shell_exec_action(clish_action_t *action,
  327. clish_context_t *context, char **out)
  328. {
  329. clish_shell_t *this = context->shell;
  330. int result = 0;
  331. const char *builtin;
  332. char *script;
  333. builtin = clish_action__get_builtin(action);
  334. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  335. if (builtin) {
  336. clish_shell_builtin_fn_t *callback;
  337. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  338. result = -1;
  339. /* search for an internal command */
  340. callback = find_builtin_callback(clish_cmd_list, builtin);
  341. if (!callback) {
  342. /* search for a client command */
  343. callback = find_builtin_callback(
  344. this->client_hooks->cmd_list, builtin);
  345. }
  346. /* invoke the builtin callback */
  347. if (callback)
  348. result = callback(context, argv, script, out);
  349. if (argv)
  350. lub_argv_delete(argv);
  351. } else if (script) {
  352. /* now get the client to interpret the resulting script */
  353. result = this->client_hooks->script_fn(context, action, script, out);
  354. }
  355. lub_string_free(script);
  356. return result;
  357. }
  358. /*----------------------------------------------------------- */
  359. /*
  360. * Find out the previous view in the stack and go to it
  361. */
  362. static int clish_nested_up(clish_context_t *context, const lub_argv_t *argv,
  363. const char *script, char **out)
  364. {
  365. clish_shell_t *this = context->shell;
  366. if (!this)
  367. return -1;
  368. argv = argv; /* not used */
  369. /* If depth=0 than exit */
  370. if (0 == this->depth) {
  371. this->state = SHELL_STATE_CLOSING;
  372. return 0;
  373. }
  374. this->depth--;
  375. return 0;
  376. }
  377. /*----------------------------------------------------------- */
  378. /*
  379. * Builtin: NOP function
  380. */
  381. static int clish_nop(clish_context_t *context, const lub_argv_t *argv,
  382. const char *script, char **out)
  383. {
  384. return 0;
  385. }
  386. /*----------------------------------------------------------- */
  387. /*
  388. * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
  389. */
  390. static int clish_wdog(clish_context_t *context, const lub_argv_t *argv,
  391. const char *script, char **out)
  392. {
  393. const char *arg = lub_argv__get_arg(argv, 0);
  394. clish_shell_t *this = context->shell;
  395. /* Turn off watchdog if no args */
  396. if (!arg || ('\0' == *arg)) {
  397. this->wdog_timeout = 0;
  398. return 0;
  399. }
  400. this->wdog_timeout = (unsigned int)atoi(arg);
  401. return 0;
  402. }
  403. /*--------------------------------------------------------- */
  404. /*
  405. * Get the ACTION context as a macros
  406. */
  407. static int clish_macros(clish_context_t *context, const lub_argv_t *argv,
  408. const char *script, char **out)
  409. {
  410. if (!script) /* Nothing to do */
  411. return 0;
  412. *out = lub_string_dup(script);
  413. return 0;
  414. }
  415. /*----------------------------------------------------------- */
  416. const char *clish_shell__get_fifo(clish_shell_t * this)
  417. {
  418. char *name;
  419. int res;
  420. if (this->fifo_name) {
  421. if (0 == access(this->fifo_name, R_OK | W_OK))
  422. return this->fifo_name;
  423. unlink(this->fifo_name);
  424. lub_string_free(this->fifo_name);
  425. this->fifo_name = NULL;
  426. }
  427. do {
  428. char template[] = "/tmp/klish.fifo.XXXXXX";
  429. name = mktemp(template);
  430. if (name[0] == '\0')
  431. return NULL;
  432. res = mkfifo(name, 0600);
  433. if (res == 0)
  434. this->fifo_name = lub_string_dup(name);
  435. } while ((res < 0) && (EEXIST == errno));
  436. return this->fifo_name;
  437. }
  438. /*--------------------------------------------------------- */
  439. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  440. {
  441. return this->client_cookie;
  442. }
  443. /*-------------------------------------------------------- */
  444. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  445. {
  446. assert(this);
  447. this->log = log;
  448. }
  449. /*-------------------------------------------------------- */
  450. bool_t clish_shell__get_log(const clish_shell_t *this)
  451. {
  452. assert(this);
  453. return this->log;
  454. }
  455. /*----------------------------------------------------------- */