1
0

shell_execute.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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;
  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 and SIGQUIT */
  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. sigemptyset(&sigs);
  259. sigaddset(&sigs, SIGINT);
  260. sigaddset(&sigs, SIGQUIT);
  261. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  262. }
  263. /* Execute ACTION */
  264. result = clish_shell_exec_action(action, context, out);
  265. /* Restore SIGINT and SIGQUIT */
  266. if (!clish_command__get_interrupt(cmd)) {
  267. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  268. /* Is the signals delivery guaranteed here (before
  269. sigaction restore) for previously blocked and
  270. pending signals? The simple test is working well.
  271. I don't want to use sigtimedwait() function bacause
  272. it needs a realtime extensions. The sigpending() with
  273. the sleep() is not nice too. Report bug if clish will
  274. get the SIGINT after non-interruptable action.
  275. */
  276. sigaction(SIGINT, &old_sigint, NULL);
  277. sigaction(SIGQUIT, &old_sigquit, NULL);
  278. }
  279. /* Call config callback */
  280. if (!result && this->client_hooks->config_fn)
  281. this->client_hooks->config_fn(context);
  282. /* Call logging callback */
  283. if (clish_shell__get_log(this) && this->client_hooks->log_fn) {
  284. char *full_line = clish_shell__get_full_line(context);
  285. this->client_hooks->log_fn(context, full_line, result);
  286. lub_string_free(full_line);
  287. }
  288. /* Unlock the lockfile */
  289. if (lock_fd != -1)
  290. clish_shell_unlock(lock_fd);
  291. /* Move into the new view */
  292. if (!result) {
  293. clish_view_t *view = clish_command__get_view(cmd);
  294. /* Save the PWD */
  295. if (view) {
  296. char *line = clish_shell__get_line(context);
  297. clish_shell__set_pwd(this, line, view,
  298. clish_command__get_viewid(cmd), context);
  299. lub_string_free(line);
  300. }
  301. }
  302. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  303. on the "set watchdog <timeout>" command itself. */
  304. if (this->wdog_timeout && saved_wdog_timeout) {
  305. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  306. this->wdog_active = BOOL_TRUE;
  307. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  308. "seconds.\nWarning: Press any key to stop watchdog.\n",
  309. this->wdog_timeout);
  310. } else
  311. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  312. error:
  313. return result;
  314. }
  315. /*----------------------------------------------------------- */
  316. int clish_shell_exec_action(clish_action_t *action,
  317. clish_context_t *context, char **out)
  318. {
  319. clish_shell_t *this = context->shell;
  320. int result = 0;
  321. const char *builtin;
  322. char *script;
  323. builtin = clish_action__get_builtin(action);
  324. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  325. if (builtin) {
  326. clish_shell_builtin_fn_t *callback;
  327. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  328. result = -1;
  329. /* search for an internal command */
  330. callback = find_builtin_callback(clish_cmd_list, builtin);
  331. if (!callback) {
  332. /* search for a client command */
  333. callback = find_builtin_callback(
  334. this->client_hooks->cmd_list, builtin);
  335. }
  336. /* invoke the builtin callback */
  337. if (callback)
  338. result = callback(context, argv);
  339. if (argv)
  340. lub_argv_delete(argv);
  341. } else if (script) {
  342. /* now get the client to interpret the resulting script */
  343. result = this->client_hooks->script_fn(context, action, script, out);
  344. }
  345. lub_string_free(script);
  346. return result;
  347. }
  348. /*----------------------------------------------------------- */
  349. /*
  350. * Find out the previous view in the stack and go to it
  351. */
  352. static int clish_nested_up(clish_context_t *context, const lub_argv_t *argv)
  353. {
  354. clish_shell_t *this = context->shell;
  355. if (!this)
  356. return -1;
  357. argv = argv; /* not used */
  358. /* If depth=0 than exit */
  359. if (0 == this->depth) {
  360. this->state = SHELL_STATE_CLOSING;
  361. return 0;
  362. }
  363. this->depth--;
  364. return 0;
  365. }
  366. /*----------------------------------------------------------- */
  367. /*
  368. * Builtin: NOP function
  369. */
  370. static int clish_nop(clish_context_t *context, const lub_argv_t *argv)
  371. {
  372. return 0;
  373. }
  374. /*----------------------------------------------------------- */
  375. /*
  376. * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
  377. */
  378. static int clish_wdog(clish_context_t *context, const lub_argv_t *argv)
  379. {
  380. const char *arg = lub_argv__get_arg(argv, 0);
  381. clish_shell_t *this = context->shell;
  382. /* Turn off watchdog if no args */
  383. if (!arg || ('\0' == *arg)) {
  384. this->wdog_timeout = 0;
  385. return 0;
  386. }
  387. this->wdog_timeout = (unsigned int)atoi(arg);
  388. return 0;
  389. }
  390. /*----------------------------------------------------------- */
  391. const char *clish_shell__get_fifo(clish_shell_t * this)
  392. {
  393. char *name;
  394. int res;
  395. if (this->fifo_name) {
  396. if (0 == access(this->fifo_name, R_OK | W_OK))
  397. return this->fifo_name;
  398. unlink(this->fifo_name);
  399. lub_string_free(this->fifo_name);
  400. this->fifo_name = NULL;
  401. }
  402. do {
  403. char template[] = "/tmp/klish.fifo.XXXXXX";
  404. name = mktemp(template);
  405. if (name[0] == '\0')
  406. return NULL;
  407. res = mkfifo(name, 0600);
  408. if (res == 0)
  409. this->fifo_name = lub_string_dup(name);
  410. } while ((res < 0) && (EEXIST == errno));
  411. return this->fifo_name;
  412. }
  413. /*--------------------------------------------------------- */
  414. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  415. {
  416. return this->client_cookie;
  417. }
  418. /*-------------------------------------------------------- */
  419. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  420. {
  421. assert(this);
  422. this->log = log;
  423. }
  424. /*-------------------------------------------------------- */
  425. bool_t clish_shell__get_log(const clish_shell_t *this)
  426. {
  427. assert(this);
  428. return this->log;
  429. }
  430. /*----------------------------------------------------------- */