shell_execute.c 14 KB

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