shell_execute.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 <sys/wait.h>
  17. #include <sys/uio.h>
  18. #include <signal.h>
  19. #include <fcntl.h>
  20. /* Empty signal handler to ignore signal but don't use SIG_IGN. */
  21. static void sigignore(int signo)
  22. {
  23. signo = signo; /* Happy compiler */
  24. return;
  25. }
  26. /*-------------------------------------------------------- */
  27. static int clish_shell_lock(const char *lock_path)
  28. {
  29. int i;
  30. int res = -1;
  31. int lock_fd = -1;
  32. struct flock lock;
  33. if (!lock_path)
  34. return -1;
  35. lock_fd = open(lock_path, O_WRONLY | O_CREAT, 00644);
  36. if (-1 == lock_fd) {
  37. fprintf(stderr, "Warning: Can't open lockfile %s.\n", lock_path);
  38. return -1;
  39. }
  40. #ifdef FD_CLOEXEC
  41. fcntl(lock_fd, F_SETFD, fcntl(lock_fd, F_GETFD) | FD_CLOEXEC);
  42. #endif
  43. memset(&lock, 0, sizeof(lock));
  44. lock.l_type = F_WRLCK;
  45. lock.l_whence = SEEK_SET;
  46. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  47. res = fcntl(lock_fd, F_SETLK, &lock);
  48. if (res != -1)
  49. break;
  50. if (EINTR == errno)
  51. continue;
  52. if ((EAGAIN == errno) || (EACCES == errno)) {
  53. if (0 == i)
  54. fprintf(stderr,
  55. "Warning: Try to get lock. Please wait...\n");
  56. sleep(1);
  57. continue;
  58. }
  59. if (EINVAL == errno)
  60. fprintf(stderr, "Error: Locking isn't supported by OS, consider \"--lockless\".\n");
  61. break;
  62. }
  63. if (res == -1) {
  64. fprintf(stderr, "Error: Can't get lock.\n");
  65. close(lock_fd);
  66. return -1;
  67. }
  68. return lock_fd;
  69. }
  70. /*-------------------------------------------------------- */
  71. static void clish_shell_unlock(int lock_fd)
  72. {
  73. struct flock lock;
  74. if (lock_fd == -1)
  75. return;
  76. memset(&lock, 0, sizeof(lock));
  77. lock.l_type = F_UNLCK;
  78. lock.l_whence = SEEK_SET;
  79. fcntl(lock_fd, F_SETLK, &lock);
  80. close(lock_fd);
  81. }
  82. /*----------------------------------------------------------- */
  83. // Prints return value in a case of machine oriented protocol
  84. void clish_shell_machine_retval(clish_shell_t *shell, int retval)
  85. {
  86. assert(shell);
  87. if (!shell)
  88. return;
  89. if (!clish_shell_is_machine_interface(shell))
  90. return;
  91. printf("\033[%dR\n", retval);
  92. fflush(stdout);
  93. }
  94. /*----------------------------------------------------------- */
  95. int clish_shell_execute(clish_context_t *context, char **out)
  96. {
  97. clish_shell_t *this = clish_context__get_shell(context);
  98. const clish_command_t *cmd = clish_context__get_cmd(context);
  99. int result = 0;
  100. const char *lock_path = clish_shell__get_lockfile(this);
  101. int lock_fd = -1;
  102. clish_view_t *cur_view = clish_shell__get_view(this);
  103. unsigned int saved_wdog_timeout = this->wdog_timeout;
  104. assert(cmd);
  105. /* Pre-change view if the command is from another depth/view */
  106. {
  107. clish_view_restore_e restore = clish_command__get_restore(cmd);
  108. if ((CLISH_RESTORE_VIEW == restore) &&
  109. (clish_command__get_pview(cmd) != cur_view)) {
  110. clish_view_t *view = clish_command__get_pview(cmd);
  111. clish_shell__set_pwd(this, NULL, view, NULL, context);
  112. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  113. (clish_command__get_depth(cmd) < this->depth)) {
  114. this->depth = clish_command__get_depth(cmd);
  115. }
  116. }
  117. /* Lock the lockfile */
  118. if (lock_path && clish_action__get_lock(clish_command__get_action(cmd))) {
  119. lock_fd = clish_shell_lock(lock_path);
  120. if (-1 == lock_fd) {
  121. result = -1;
  122. goto error; /* Can't set lock */
  123. }
  124. }
  125. /* Execute ACTION */
  126. clish_context__set_action(context, clish_command__get_action(cmd));
  127. result = clish_shell_exec_action(context, out);
  128. /* Call config callback */
  129. if (!result)
  130. clish_shell_exec_config(context);
  131. /* Call logging callback */
  132. if (clish_shell__get_log(this) &&
  133. clish_shell_check_hook(context, CLISH_SYM_TYPE_LOG)) {
  134. char *full_line = clish_shell__get_full_line(context);
  135. clish_shell_exec_log(context, full_line, result);
  136. lub_string_free(full_line);
  137. }
  138. if (clish_shell__get_canon_out(this) &&
  139. !clish_command__get_internal(cmd)) {
  140. char *space = NULL;
  141. char *full_line = clish_shell__get_full_line(context);
  142. if (this->depth > 0) {
  143. space = malloc(this->depth + 1);
  144. memset(space, ' ', this->depth);
  145. space[this->depth] = '\0';
  146. }
  147. printf("%s%s\n", space ? space : "", full_line);
  148. lub_string_free(full_line);
  149. if (space)
  150. free(space);
  151. }
  152. // Machine oriented protocol outputs return value
  153. clish_shell_machine_retval(this, result);
  154. /* Unlock the lockfile */
  155. if (lock_fd != -1)
  156. clish_shell_unlock(lock_fd);
  157. /* Move into the new view */
  158. if (!result) {
  159. char *viewname = clish_shell_expand(clish_command__get_viewname(cmd), SHELL_VAR_NONE, context);
  160. if (viewname) {
  161. /* Search for the view */
  162. clish_view_t *view = clish_shell_find_view(this, viewname);
  163. if (!view)
  164. fprintf(stderr, "System error: Can't "
  165. "change view to %s\n", viewname);
  166. lub_string_free(viewname);
  167. /* Save the PWD */
  168. if (view) {
  169. char *line = clish_shell__get_line(context);
  170. clish_shell__set_pwd(this, line, view,
  171. clish_command__get_viewid(cmd), context);
  172. lub_string_free(line);
  173. }
  174. }
  175. }
  176. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  177. on the "set watchdog <timeout>" command itself. */
  178. if (this->wdog_timeout && saved_wdog_timeout) {
  179. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  180. this->wdog_active = BOOL_TRUE;
  181. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  182. "seconds.\nWarning: Press any key to stop watchdog.\n",
  183. this->wdog_timeout);
  184. } else
  185. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  186. error:
  187. return result;
  188. }
  189. /*----------------------------------------------------------- */
  190. /* Execute oaction. It suppose the forked process to get
  191. * script's stdout. Then forked process write the output back
  192. * to klish.
  193. */
  194. static int clish_shell_exec_oaction(clish_hook_oaction_fn_t func,
  195. void *context, const char *script, char **out)
  196. {
  197. int result = -1;
  198. int real_stdout; /* Saved stdout handler */
  199. int pipe1[2], pipe2[2];
  200. pid_t cpid = -1;
  201. konf_buf_t *buf;
  202. if (pipe(pipe1))
  203. return -1;
  204. if (pipe(pipe2))
  205. goto stdout_error;
  206. /* Create process to read script's stdout */
  207. cpid = fork();
  208. if (cpid == -1) {
  209. fprintf(stderr, "Error: Can't fork the stdout-grabber process.\n"
  210. "Error: The ACTION will be not executed.\n");
  211. goto stdout_error;
  212. }
  213. /* Child: read action's stdout */
  214. if (cpid == 0) {
  215. lub_list_t *l;
  216. lub_list_node_t *node;
  217. struct iovec *iov;
  218. const int rsize = CLISH_STDOUT_CHUNK; /* Read chunk size */
  219. size_t cur_size = 0;
  220. ssize_t r = 0;
  221. close(pipe1[1]);
  222. close(pipe2[0]);
  223. l = lub_list_new(NULL, NULL);
  224. /* Read the result of script execution */
  225. while (1) {
  226. ssize_t ret;
  227. iov = malloc(sizeof(*iov));
  228. iov->iov_len = rsize;
  229. iov->iov_base = malloc(iov->iov_len);
  230. do {
  231. ret = readv(pipe1[0], iov, 1);
  232. } while ((ret < 0) && (errno == EINTR));
  233. if (ret <= 0) { /* Error or EOF */
  234. free(iov->iov_base);
  235. free(iov);
  236. break;
  237. }
  238. iov->iov_len = ret;
  239. lub_list_add(l, iov);
  240. /* Check the max size of buffer */
  241. cur_size += ret;
  242. if (cur_size >= CLISH_STDOUT_MAXBUF)
  243. break;
  244. }
  245. close(pipe1[0]);
  246. /* Write the result of script back to klish */
  247. while ((node = lub_list__get_head(l))) {
  248. iov = lub_list_node__get_data(node);
  249. lub_list_del(l, node);
  250. lub_list_node_free(node);
  251. r = write(pipe2[1], iov->iov_base, iov->iov_len);
  252. free(iov->iov_base);
  253. free(iov);
  254. }
  255. close(pipe2[1]);
  256. lub_list_free(l);
  257. _exit(r < 0 ? 1 : 0);
  258. }
  259. real_stdout = dup(STDOUT_FILENO);
  260. dup2(pipe1[1], STDOUT_FILENO);
  261. close(pipe1[0]);
  262. close(pipe1[1]);
  263. close(pipe2[1]);
  264. result = func(context, script);
  265. /* Restore real stdout */
  266. dup2(real_stdout, STDOUT_FILENO);
  267. close(real_stdout);
  268. /* Read the result of script execution */
  269. buf = konf_buf_new(pipe2[0]);
  270. while (konf_buf_read(buf) > 0);
  271. *out = konf_buf__dup_line(buf);
  272. konf_buf_delete(buf);
  273. close(pipe2[0]);
  274. /* Wait for the stdout-grabber process */
  275. while (waitpid(cpid, NULL, 0) != cpid);
  276. return result;
  277. stdout_error:
  278. close(pipe1[0]);
  279. close(pipe1[1]);
  280. return -1;
  281. }
  282. /*----------------------------------------------------------- */
  283. int clish_shell_exec_action(clish_context_t *context, char **out)
  284. {
  285. int result = -1;
  286. const clish_sym_t *sym;
  287. const char *rscript;
  288. char *script;
  289. const void *func = NULL; /* We don't know the func API at this time */
  290. const clish_action_t *action = clish_context__get_action(context);
  291. clish_shell_t *shell = clish_context__get_shell(context);
  292. bool_t intr = clish_action__get_interrupt(action);
  293. /* Signal vars */
  294. struct sigaction old_sigint, old_sigquit, old_sighup;
  295. struct sigaction sa;
  296. sigset_t old_sigs;
  297. if (!(sym = clish_action__get_builtin(action)))
  298. return 0;
  299. if (shell->dryrun &&
  300. !clish_sym__get_permanent(sym) &&
  301. !clish_action__get_permanent(action))
  302. return 0;
  303. if (!(func = clish_sym__get_func(sym))) {
  304. fprintf(stderr, "Error: Default ACTION symbol is not specified.\n");
  305. return -1;
  306. }
  307. /* Expand variables, but only if the sym or action want it */
  308. rscript = clish_action__get_script(action);
  309. if(clish_context__get_expand(context))
  310. script = clish_shell_expand(rscript, SHELL_VAR_ACTION, context);
  311. else
  312. script = lub_string_dup(rscript);
  313. /* Ignore and block SIGINT, SIGQUIT, SIGHUP.
  314. * The SIG_IGN is not a case because it will be inherited
  315. * while a fork(). It's necessary to ignore signals because
  316. * the klish itself and ACTION script share the same terminal.
  317. */
  318. sa.sa_flags = 0;
  319. sigemptyset(&sa.sa_mask);
  320. sa.sa_handler = sigignore; /* Empty signal handler */
  321. sigaction(SIGINT, &sa, &old_sigint);
  322. sigaction(SIGQUIT, &sa, &old_sigquit);
  323. sigaction(SIGHUP, &sa, &old_sighup);
  324. /* Block signals for children processes. The block state is inherited. */
  325. if (!intr) {
  326. sigset_t sigs;
  327. sigemptyset(&sigs);
  328. sigaddset(&sigs, SIGINT);
  329. sigaddset(&sigs, SIGQUIT);
  330. sigaddset(&sigs, SIGHUP);
  331. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  332. }
  333. /* Find out the function API */
  334. /* CLISH_SYM_API_SIMPLE */
  335. if (clish_sym__get_api(sym) == CLISH_SYM_API_SIMPLE) {
  336. result = ((clish_hook_action_fn_t *)func)(context, script, out);
  337. /* CLISH_SYM_API_STDOUT and output is not needed */
  338. } else if ((clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) && (!out)) {
  339. result = ((clish_hook_oaction_fn_t *)func)(context, script);
  340. /* CLISH_SYM_API_STDOUT and outpus is needed */
  341. } else if (clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) {
  342. result = clish_shell_exec_oaction((clish_hook_oaction_fn_t *)func,
  343. context, script, out);
  344. }
  345. /* Restore SIGINT, SIGQUIT, SIGHUP */
  346. if (!intr) {
  347. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  348. /* Is the signals delivery guaranteed here (before
  349. sigaction restore) for previously blocked and
  350. pending signals? The simple test is working well.
  351. I don't want to use sigtimedwait() function because
  352. it needs a realtime extensions. The sigpending() with
  353. the sleep() is not nice too. Report bug if clish will
  354. get the SIGINT after non-interruptable action.
  355. */
  356. }
  357. sigaction(SIGINT, &old_sigint, NULL);
  358. sigaction(SIGQUIT, &old_sigquit, NULL);
  359. sigaction(SIGHUP, &old_sighup, NULL);
  360. lub_string_free(script);
  361. return result;
  362. }
  363. /*----------------------------------------------------------- */
  364. const void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  365. {
  366. clish_sym_t *sym;
  367. clish_shell_t *shell = clish_context__get_shell(clish_context);
  368. const void *func;
  369. if (!(sym = shell->hooks[type]))
  370. return NULL;
  371. if (shell->dryrun && !clish_sym__get_permanent(sym))
  372. return NULL;
  373. if (!(func = clish_sym__get_func(sym)))
  374. return NULL;
  375. return func;
  376. }
  377. /*----------------------------------------------------------- */
  378. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  379. {
  380. clish_hook_config_fn_t *func = NULL;
  381. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  382. return func ? func(clish_context) : 0;
  383. }
  384. /*----------------------------------------------------------- */
  385. CLISH_HOOK_LOG(clish_shell_exec_log)
  386. {
  387. clish_hook_log_fn_t *func = NULL;
  388. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  389. return func ? func(clish_context, line, retcode) : 0;
  390. }
  391. /*----------------------------------------------------------- */
  392. char *clish_shell_mkfifo(clish_shell_t * this, char *name, size_t n)
  393. {
  394. int res;
  395. if (n < 1) /* Buffer too small */
  396. return NULL;
  397. do {
  398. strncpy(name, this->fifo_temp, n);
  399. name[n - 1] = '\0';
  400. mktemp(name);
  401. if (name[0] == '\0')
  402. return NULL;
  403. res = mkfifo(name, 0600);
  404. } while ((res < 0) && (EEXIST == errno));
  405. return name;
  406. }
  407. /*----------------------------------------------------------- */
  408. int clish_shell_rmfifo(clish_shell_t * this, const char *name)
  409. {
  410. this = this; // Happy compiler
  411. return unlink(name);
  412. }
  413. CLISH_SET(shell, bool_t, log);
  414. CLISH_GET(shell, bool_t, log);
  415. CLISH_SET(shell, bool_t, dryrun);
  416. CLISH_GET(shell, bool_t, dryrun);
  417. CLISH_SET(shell, bool_t, canon_out);
  418. CLISH_GET(shell, bool_t, canon_out);