shell_execute.c 11 KB

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