1
0

shell_execute.c 12 KB

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