shell_execute.c 15 KB

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