shell_execute.c 13 KB

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