shell_execute.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. static int clish_shell_lock(const char *lock_path)
  163. {
  164. int i;
  165. int res;
  166. int lock_fd = -1;
  167. struct flock lock;
  168. if (!lock_path)
  169. return -1;
  170. lock_fd = open(lock_path, O_WRONLY | O_CREAT, 00644);
  171. if (-1 == lock_fd) {
  172. fprintf(stderr, "Can't open lockfile %s.\n", lock_path);
  173. return -1;
  174. }
  175. lock.l_type = F_WRLCK;
  176. lock.l_whence = SEEK_SET;
  177. lock.l_start = 0;
  178. lock.l_len = 0;
  179. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  180. res = fcntl(lock_fd, F_SETLK, &lock);
  181. if (res != -1)
  182. break;
  183. if (EINTR == errno)
  184. continue;
  185. if ((EAGAIN == errno) || (EACCES == errno)) {
  186. if (0 == i)
  187. fprintf(stderr,
  188. "Try to get lock. Please wait...\n");
  189. sleep(1);
  190. continue;
  191. }
  192. break;
  193. }
  194. if (res == -1) {
  195. fprintf(stderr, "Can't get lock.\n");
  196. close(lock_fd);
  197. return -1;
  198. }
  199. return lock_fd;
  200. }
  201. /*-------------------------------------------------------- */
  202. static void clish_shell_unlock(int lock_fd)
  203. {
  204. struct flock lock;
  205. if (lock_fd == -1)
  206. return;
  207. lock.l_type = F_UNLCK;
  208. lock.l_whence = SEEK_SET;
  209. lock.l_start = 0;
  210. lock.l_len = 0;
  211. fcntl(lock_fd, F_SETLK, &lock);
  212. close(lock_fd);
  213. }
  214. /*----------------------------------------------------------- */
  215. int clish_shell_execute(clish_context_t *context, char **out)
  216. {
  217. clish_shell_t *this = context->shell;
  218. const clish_command_t *cmd = context->cmd;
  219. clish_action_t *action;
  220. int result = 0;
  221. char *lock_path = clish_shell__get_lockfile(this);
  222. int lock_fd = -1;
  223. sigset_t old_sigs;
  224. struct sigaction old_sigint, old_sigquit, old_sighup;
  225. clish_view_t *cur_view = clish_shell__get_view(this);
  226. unsigned int saved_wdog_timeout = this->wdog_timeout;
  227. assert(cmd);
  228. action = clish_command__get_action(cmd);
  229. /* Pre-change view if the command is from another depth/view */
  230. {
  231. clish_view_restore_t restore = clish_command__get_restore(cmd);
  232. if ((CLISH_RESTORE_VIEW == restore) &&
  233. (clish_command__get_pview(cmd) != cur_view)) {
  234. clish_view_t *view = clish_command__get_pview(cmd);
  235. clish_shell__set_pwd(this, NULL, view, NULL, context);
  236. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  237. (clish_command__get_depth(cmd) < this->depth)) {
  238. this->depth = clish_command__get_depth(cmd);
  239. }
  240. }
  241. /* Lock the lockfile */
  242. if (lock_path && clish_command__get_lock(cmd)) {
  243. lock_fd = clish_shell_lock(lock_path);
  244. if (-1 == lock_fd) {
  245. result = -1;
  246. goto error; /* Can't set lock */
  247. }
  248. }
  249. /* Ignore and block SIGINT, SIGQUIT, SIGHUP */
  250. if (!clish_command__get_interrupt(cmd)) {
  251. struct sigaction sa;
  252. sigset_t sigs;
  253. sa.sa_flags = 0;
  254. sigemptyset(&sa.sa_mask);
  255. sa.sa_handler = SIG_IGN;
  256. sigaction(SIGINT, &sa, &old_sigint);
  257. sigaction(SIGQUIT, &sa, &old_sigquit);
  258. sigaction(SIGHUP, &sa, &old_sighup);
  259. sigemptyset(&sigs);
  260. sigaddset(&sigs, SIGINT);
  261. sigaddset(&sigs, SIGQUIT);
  262. sigaddset(&sigs, SIGHUP);
  263. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  264. }
  265. /* Execute ACTION */
  266. result = clish_shell_exec_action(action, context, out);
  267. /* Restore SIGINT, SIGQUIT, SIGHUP */
  268. if (!clish_command__get_interrupt(cmd)) {
  269. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  270. /* Is the signals delivery guaranteed here (before
  271. sigaction restore) for previously blocked and
  272. pending signals? The simple test is working well.
  273. I don't want to use sigtimedwait() function bacause
  274. it needs a realtime extensions. The sigpending() with
  275. the sleep() is not nice too. Report bug if clish will
  276. get the SIGINT after non-interruptable action.
  277. */
  278. sigaction(SIGINT, &old_sigint, NULL);
  279. sigaction(SIGQUIT, &old_sigquit, NULL);
  280. sigaction(SIGHUP, &old_sighup, NULL);
  281. }
  282. /* Call config callback */
  283. if (!result && this->client_hooks->config_fn)
  284. this->client_hooks->config_fn(context);
  285. /* Call logging callback */
  286. if (clish_shell__get_log(this) && this->client_hooks->log_fn) {
  287. char *full_line = clish_shell__get_full_line(context);
  288. this->client_hooks->log_fn(context, full_line, result);
  289. lub_string_free(full_line);
  290. }
  291. /* Unlock the lockfile */
  292. if (lock_fd != -1)
  293. clish_shell_unlock(lock_fd);
  294. /* Move into the new view */
  295. if (!result) {
  296. clish_view_t *view = clish_command__get_view(cmd);
  297. /* Save the PWD */
  298. if (view) {
  299. char *line = clish_shell__get_line(context);
  300. clish_shell__set_pwd(this, line, view,
  301. clish_command__get_viewid(cmd), context);
  302. lub_string_free(line);
  303. }
  304. }
  305. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  306. on the "set watchdog <timeout>" command itself. */
  307. if (this->wdog_timeout && saved_wdog_timeout) {
  308. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  309. this->wdog_active = BOOL_TRUE;
  310. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  311. "seconds.\nWarning: Press any key to stop watchdog.\n",
  312. this->wdog_timeout);
  313. } else
  314. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  315. error:
  316. return result;
  317. }
  318. /*----------------------------------------------------------- */
  319. int clish_shell_exec_action(clish_action_t *action,
  320. clish_context_t *context, char **out)
  321. {
  322. clish_shell_t *this = context->shell;
  323. int result = 0;
  324. const char *builtin;
  325. char *script;
  326. builtin = clish_action__get_builtin(action);
  327. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  328. if (builtin) {
  329. clish_shell_builtin_fn_t *callback;
  330. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  331. result = -1;
  332. /* search for an internal command */
  333. callback = find_builtin_callback(clish_cmd_list, builtin);
  334. if (!callback) {
  335. /* search for a client command */
  336. callback = find_builtin_callback(
  337. this->client_hooks->cmd_list, builtin);
  338. }
  339. /* invoke the builtin callback */
  340. if (callback)
  341. result = callback(context, argv);
  342. if (argv)
  343. lub_argv_delete(argv);
  344. } else if (script) {
  345. /* now get the client to interpret the resulting script */
  346. result = this->client_hooks->script_fn(context, action, script, out);
  347. }
  348. lub_string_free(script);
  349. return result;
  350. }
  351. /*----------------------------------------------------------- */
  352. /*
  353. * Find out the previous view in the stack and go to it
  354. */
  355. static int clish_nested_up(clish_context_t *context, const lub_argv_t *argv)
  356. {
  357. clish_shell_t *this = context->shell;
  358. if (!this)
  359. return -1;
  360. argv = argv; /* not used */
  361. /* If depth=0 than exit */
  362. if (0 == this->depth) {
  363. this->state = SHELL_STATE_CLOSING;
  364. return 0;
  365. }
  366. this->depth--;
  367. return 0;
  368. }
  369. /*----------------------------------------------------------- */
  370. /*
  371. * Builtin: NOP function
  372. */
  373. static int clish_nop(clish_context_t *context, const lub_argv_t *argv)
  374. {
  375. return 0;
  376. }
  377. /*----------------------------------------------------------- */
  378. /*
  379. * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
  380. */
  381. static int clish_wdog(clish_context_t *context, const lub_argv_t *argv)
  382. {
  383. const char *arg = lub_argv__get_arg(argv, 0);
  384. clish_shell_t *this = context->shell;
  385. /* Turn off watchdog if no args */
  386. if (!arg || ('\0' == *arg)) {
  387. this->wdog_timeout = 0;
  388. return 0;
  389. }
  390. this->wdog_timeout = (unsigned int)atoi(arg);
  391. return 0;
  392. }
  393. /*----------------------------------------------------------- */
  394. const char *clish_shell__get_fifo(clish_shell_t * this)
  395. {
  396. char *name;
  397. int res;
  398. if (this->fifo_name) {
  399. if (0 == access(this->fifo_name, R_OK | W_OK))
  400. return this->fifo_name;
  401. unlink(this->fifo_name);
  402. lub_string_free(this->fifo_name);
  403. this->fifo_name = NULL;
  404. }
  405. do {
  406. char template[] = "/tmp/klish.fifo.XXXXXX";
  407. name = mktemp(template);
  408. if (name[0] == '\0')
  409. return NULL;
  410. res = mkfifo(name, 0600);
  411. if (res == 0)
  412. this->fifo_name = lub_string_dup(name);
  413. } while ((res < 0) && (EEXIST == errno));
  414. return this->fifo_name;
  415. }
  416. /*--------------------------------------------------------- */
  417. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  418. {
  419. return this->client_cookie;
  420. }
  421. /*-------------------------------------------------------- */
  422. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  423. {
  424. assert(this);
  425. this->log = log;
  426. }
  427. /*-------------------------------------------------------- */
  428. bool_t clish_shell__get_log(const clish_shell_t *this)
  429. {
  430. assert(this);
  431. return this->log;
  432. }
  433. /*----------------------------------------------------------- */