1
0

shell_execute.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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;
  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. lock.l_type = F_WRLCK;
  199. lock.l_whence = SEEK_SET;
  200. lock.l_start = 0;
  201. lock.l_len = 0;
  202. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  203. res = fcntl(lock_fd, F_SETLK, &lock);
  204. if (res != -1)
  205. break;
  206. if (EINTR == errno)
  207. continue;
  208. if ((EAGAIN == errno) || (EACCES == errno)) {
  209. if (0 == i)
  210. fprintf(stderr,
  211. "Try to get lock. Please wait...\n");
  212. sleep(1);
  213. continue;
  214. }
  215. break;
  216. }
  217. if (res == -1) {
  218. fprintf(stderr, "Can't get lock.\n");
  219. close(lock_fd);
  220. return -1;
  221. }
  222. return lock_fd;
  223. }
  224. /*-------------------------------------------------------- */
  225. static void clish_shell_unlock(int lock_fd)
  226. {
  227. struct flock lock;
  228. if (lock_fd == -1)
  229. return;
  230. lock.l_type = F_UNLCK;
  231. lock.l_whence = SEEK_SET;
  232. lock.l_start = 0;
  233. lock.l_len = 0;
  234. fcntl(lock_fd, F_SETLK, &lock);
  235. close(lock_fd);
  236. }
  237. /*----------------------------------------------------------- */
  238. int clish_shell_execute(clish_context_t *context, char **out)
  239. {
  240. clish_shell_t *this = context->shell;
  241. const clish_command_t *cmd = context->cmd;
  242. clish_action_t *action;
  243. int result = 0;
  244. char *lock_path = clish_shell__get_lockfile(this);
  245. int lock_fd = -1;
  246. sigset_t old_sigs;
  247. struct sigaction old_sigint, old_sigquit, old_sighup;
  248. clish_view_t *cur_view = clish_shell__get_view(this);
  249. unsigned int saved_wdog_timeout = this->wdog_timeout;
  250. assert(cmd);
  251. action = clish_command__get_action(cmd);
  252. /* Pre-change view if the command is from another depth/view */
  253. {
  254. clish_view_restore_t restore = clish_command__get_restore(cmd);
  255. if ((CLISH_RESTORE_VIEW == restore) &&
  256. (clish_command__get_pview(cmd) != cur_view)) {
  257. clish_view_t *view = clish_command__get_pview(cmd);
  258. clish_shell__set_pwd(this, NULL, view, NULL, context);
  259. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  260. (clish_command__get_depth(cmd) < this->depth)) {
  261. this->depth = clish_command__get_depth(cmd);
  262. }
  263. }
  264. /* Lock the lockfile */
  265. if (lock_path && clish_command__get_lock(cmd)) {
  266. lock_fd = clish_shell_lock(lock_path);
  267. if (-1 == lock_fd) {
  268. result = -1;
  269. goto error; /* Can't set lock */
  270. }
  271. }
  272. /* Ignore and block SIGINT, SIGQUIT, SIGHUP */
  273. if (!clish_command__get_interrupt(cmd)) {
  274. struct sigaction sa;
  275. sigset_t sigs;
  276. sa.sa_flags = 0;
  277. sigemptyset(&sa.sa_mask);
  278. sa.sa_handler = SIG_IGN;
  279. sigaction(SIGINT, &sa, &old_sigint);
  280. sigaction(SIGQUIT, &sa, &old_sigquit);
  281. sigaction(SIGHUP, &sa, &old_sighup);
  282. sigemptyset(&sigs);
  283. sigaddset(&sigs, SIGINT);
  284. sigaddset(&sigs, SIGQUIT);
  285. sigaddset(&sigs, SIGHUP);
  286. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  287. }
  288. /* Execute ACTION */
  289. result = clish_shell_exec_action(action, context, out);
  290. /* Restore SIGINT, SIGQUIT, SIGHUP */
  291. if (!clish_command__get_interrupt(cmd)) {
  292. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  293. /* Is the signals delivery guaranteed here (before
  294. sigaction restore) for previously blocked and
  295. pending signals? The simple test is working well.
  296. I don't want to use sigtimedwait() function bacause
  297. it needs a realtime extensions. The sigpending() with
  298. the sleep() is not nice too. Report bug if clish will
  299. get the SIGINT after non-interruptable action.
  300. */
  301. sigaction(SIGINT, &old_sigint, NULL);
  302. sigaction(SIGQUIT, &old_sigquit, NULL);
  303. sigaction(SIGHUP, &old_sighup, NULL);
  304. }
  305. /* Call config callback */
  306. if (!result && this->client_hooks->config_fn)
  307. this->client_hooks->config_fn(context);
  308. /* Call logging callback */
  309. if (clish_shell__get_log(this) && this->client_hooks->log_fn) {
  310. char *full_line = clish_shell__get_full_line(context);
  311. this->client_hooks->log_fn(context, full_line, result);
  312. lub_string_free(full_line);
  313. }
  314. /* Unlock the lockfile */
  315. if (lock_fd != -1)
  316. clish_shell_unlock(lock_fd);
  317. /* Move into the new view */
  318. if (!result) {
  319. clish_view_t *view = NULL;
  320. const char *view_str = clish_command__get_view(cmd);
  321. if (view_str) {
  322. char *view_exp = NULL;
  323. view_exp = clish_shell_expand(view_str,
  324. SHELL_VAR_NONE, context);
  325. view = clish_shell_find_view(this, view_exp);
  326. if (!view)
  327. fprintf(stderr, "System error: Can't "
  328. "change view to %s\n", view_exp);
  329. lub_string_free(view_exp);
  330. }
  331. /* Save the PWD */
  332. if (view) {
  333. char *line = clish_shell__get_line(context);
  334. clish_shell__set_pwd(this, line, view,
  335. clish_command__get_viewid(cmd), context);
  336. lub_string_free(line);
  337. }
  338. }
  339. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  340. on the "set watchdog <timeout>" command itself. */
  341. if (this->wdog_timeout && saved_wdog_timeout) {
  342. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  343. this->wdog_active = BOOL_TRUE;
  344. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  345. "seconds.\nWarning: Press any key to stop watchdog.\n",
  346. this->wdog_timeout);
  347. } else
  348. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  349. error:
  350. return result;
  351. }
  352. /*----------------------------------------------------------- */
  353. int clish_shell_exec_action(clish_action_t *action,
  354. clish_context_t *context, char **out)
  355. {
  356. clish_shell_t *this = context->shell;
  357. int result = 0;
  358. const char *builtin;
  359. char *script;
  360. builtin = clish_action__get_builtin(action);
  361. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  362. if (builtin) {
  363. clish_shell_builtin_fn_t *callback;
  364. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  365. result = -1;
  366. /* search for an internal command */
  367. callback = find_builtin_callback(clish_cmd_list, builtin);
  368. if (!callback) {
  369. /* search for a client command */
  370. callback = find_builtin_callback(
  371. this->client_hooks->cmd_list, builtin);
  372. }
  373. /* invoke the builtin callback */
  374. if (callback)
  375. result = callback(context, argv, script, out);
  376. if (argv)
  377. lub_argv_delete(argv);
  378. } else if (script) {
  379. /* now get the client to interpret the resulting script */
  380. result = this->client_hooks->script_fn(context, action, script, out);
  381. }
  382. lub_string_free(script);
  383. return result;
  384. }
  385. /*----------------------------------------------------------- */
  386. /*
  387. * Find out the previous view in the stack and go to it
  388. */
  389. static int clish_nested_up(clish_context_t *context, const lub_argv_t *argv,
  390. const char *script, char **out)
  391. {
  392. clish_shell_t *this = context->shell;
  393. if (!this)
  394. return -1;
  395. argv = argv; /* not used */
  396. /* If depth=0 than exit */
  397. if (0 == this->depth) {
  398. this->state = SHELL_STATE_CLOSING;
  399. return 0;
  400. }
  401. this->depth--;
  402. script = script; /* Happy compiler */
  403. out = out; /* Happy compiler */
  404. return 0;
  405. }
  406. /*----------------------------------------------------------- */
  407. /*
  408. * Builtin: NOP function
  409. */
  410. static int clish_nop(clish_context_t *context, const lub_argv_t *argv,
  411. const char *script, char **out)
  412. {
  413. context = context; /* Happy compiler */
  414. argv = argv; /* Happy compiler */
  415. script = script; /* Happy compiler */
  416. out = out; /* Happy compiler */
  417. return 0;
  418. }
  419. /*----------------------------------------------------------- */
  420. /*
  421. * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
  422. */
  423. static int clish_wdog(clish_context_t *context, const lub_argv_t *argv,
  424. const char *script, char **out)
  425. {
  426. const char *arg = lub_argv__get_arg(argv, 0);
  427. clish_shell_t *this = context->shell;
  428. /* Turn off watchdog if no args */
  429. if (!arg || ('\0' == *arg)) {
  430. this->wdog_timeout = 0;
  431. return 0;
  432. }
  433. this->wdog_timeout = (unsigned int)atoi(arg);
  434. script = script; /* Happy compiler */
  435. out = out; /* Happy compiler */
  436. return 0;
  437. }
  438. /*--------------------------------------------------------- */
  439. /*
  440. * Get the ACTION context as a macros
  441. */
  442. static int clish_macros(clish_context_t *context, const lub_argv_t *argv,
  443. const char *script, char **out)
  444. {
  445. if (!script) /* Nothing to do */
  446. return 0;
  447. *out = lub_string_dup(script);
  448. context = context; /* Happy compiler */
  449. argv = argv; /* Happy compiler */
  450. return 0;
  451. }
  452. /*----------------------------------------------------------- */
  453. const char *clish_shell__get_fifo(clish_shell_t * this)
  454. {
  455. char *name;
  456. int res;
  457. if (this->fifo_name) {
  458. if (0 == access(this->fifo_name, R_OK | W_OK))
  459. return this->fifo_name;
  460. unlink(this->fifo_name);
  461. lub_string_free(this->fifo_name);
  462. this->fifo_name = NULL;
  463. }
  464. do {
  465. char template[] = "/tmp/klish.fifo.XXXXXX";
  466. name = mktemp(template);
  467. if (name[0] == '\0')
  468. return NULL;
  469. res = mkfifo(name, 0600);
  470. if (res == 0)
  471. this->fifo_name = lub_string_dup(name);
  472. } while ((res < 0) && (EEXIST == errno));
  473. return this->fifo_name;
  474. }
  475. /*--------------------------------------------------------- */
  476. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  477. {
  478. return this->client_cookie;
  479. }
  480. /*-------------------------------------------------------- */
  481. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  482. {
  483. assert(this);
  484. this->log = log;
  485. }
  486. /*-------------------------------------------------------- */
  487. bool_t clish_shell__get_log(const clish_shell_t *this)
  488. {
  489. assert(this);
  490. return this->log;
  491. }
  492. /*----------------------------------------------------------- */