shell_execute.c 11 KB

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