shell_execute.c 12 KB

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