shell_execute.c 14 KB

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