shell_execute.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 (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(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 (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 (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 (!callback) {
  249. /* search for a client command */
  250. callback = find_builtin_callback(
  251. this->client_hooks->cmd_list, builtin);
  252. }
  253. /* invoke the builtin callback */
  254. if (callback)
  255. result = callback(this, argv);
  256. if (argv)
  257. lub_argv_delete(argv);
  258. } else if (script) {
  259. /* now get the client to interpret the resulting script */
  260. result = this->client_hooks->script_fn(this, cmd, script, out);
  261. }
  262. pthread_cleanup_pop(1);
  263. /* Restore SIGINT and SIGQUIT */
  264. if (!clish_command__get_interrupt(cmd)) {
  265. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  266. /* Is the signals delivery guaranteed here (before
  267. sigaction restore) for previously blocked and
  268. pending signals? The simple test is working well.
  269. I don't want to use sigtimedwait() function bacause
  270. it needs a realtime extensions. The sigpending() with
  271. the sleep() is not nice too. Report bug if clish will
  272. get the SIGINT after non-interruptable action.
  273. */
  274. sigaction(SIGINT, &old_sigint, NULL);
  275. sigaction(SIGQUIT, &old_sigquit, NULL);
  276. }
  277. /* Call config callback */
  278. if ((BOOL_TRUE == result) && this->client_hooks->config_fn)
  279. this->client_hooks->config_fn(this, cmd, pargv);
  280. /* Unlock the lockfile */
  281. if (lock_fd != -1) {
  282. flock(lock_fd, LOCK_UN);
  283. close(lock_fd);
  284. }
  285. /* Move into the new view */
  286. if (BOOL_TRUE == result) {
  287. clish_view_t *view = clish_command__get_view(cmd);
  288. char *viewid = clish_command__get_viewid(cmd,
  289. this->viewid, pargv);
  290. if (view) {
  291. /* Save the current config PWD */
  292. char *line = clish_variable__get_line(cmd, pargv);
  293. clish_shell__set_pwd(this,
  294. clish_command__get_depth(cmd),
  295. line, this->view, this->viewid);
  296. lub_string_free(line);
  297. /* Change view */
  298. this->view = view;
  299. }
  300. if (viewid || view) {
  301. /* cleanup */
  302. lub_string_free(this->viewid);
  303. this->viewid = viewid;
  304. }
  305. }
  306. return result;
  307. }
  308. /*----------------------------------------------------------- */
  309. /*
  310. * Find out the previous view in the stack and go to it
  311. */
  312. static bool_t clish_nested_up(const clish_shell_t * shell, const lub_argv_t * argv)
  313. {
  314. clish_shell_t *this = (clish_shell_t *) shell;
  315. clish_view_t *view = NULL;
  316. char *viewid = NULL;
  317. int depth = 0;
  318. if (!shell)
  319. return BOOL_FALSE;
  320. argv = argv; /* not used */
  321. depth = clish_view__get_depth(this->view);
  322. /* If depth=0 than exit */
  323. if (0 == depth) {
  324. this->state = SHELL_STATE_CLOSING;
  325. return BOOL_TRUE;
  326. }
  327. depth--;
  328. view = clish_shell__get_pwd_view(this, depth);
  329. viewid = clish_shell__get_pwd_viewid(this, depth);
  330. if (!view)
  331. return BOOL_FALSE;
  332. this->view = view;
  333. lub_string_free(this->viewid);
  334. this->viewid = viewid ? lub_string_dup(viewid) : NULL;
  335. return BOOL_TRUE;
  336. }
  337. /*----------------------------------------------------------- */
  338. const char * clish_shell__get_fifo(clish_shell_t * this)
  339. {
  340. char *name;
  341. int res;
  342. if (this->fifo_name) {
  343. if (0 == access(this->fifo_name, R_OK | W_OK))
  344. return this->fifo_name;
  345. unlink(this->fifo_name);
  346. lub_string_free(this->fifo_name);
  347. this->fifo_name = NULL;
  348. }
  349. do {
  350. char template[] = "/tmp/klish.fifo.XXXXXX";
  351. name = mktemp(template);
  352. if (name[0] == '\0')
  353. return NULL;
  354. res = mkfifo(name, 0600);
  355. if (res == 0)
  356. this->fifo_name = lub_string_dup(name);
  357. } while ((res < 0) && (EEXIST == errno));
  358. return this->fifo_name;
  359. }
  360. /*----------------------------------------------------------- */