shell_execute.c 12 KB

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