shell_execute.c 12 KB

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