shell_execute.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. close(pipe1[1]);
  194. close(pipe2[0]);
  195. l = lub_list_new(NULL);
  196. /* Read the result of script execution */
  197. while (1) {
  198. ssize_t ret;
  199. iov = malloc(sizeof(*iov));
  200. iov->iov_len = rsize;
  201. iov->iov_base = malloc(iov->iov_len);
  202. do {
  203. ret = readv(pipe1[0], iov, 1);
  204. } while ((ret < 0) && (errno == EINTR));
  205. if (ret <= 0) { /* Error or EOF */
  206. free(iov->iov_base);
  207. free(iov);
  208. break;
  209. }
  210. iov->iov_len = ret;
  211. lub_list_add(l, iov);
  212. /* Check the max size of buffer */
  213. cur_size += ret;
  214. if (cur_size >= CLISH_STDOUT_MAXBUF)
  215. break;
  216. }
  217. close(pipe1[0]);
  218. /* Write the result of script back to klish */
  219. while ((node = lub_list__get_head(l))) {
  220. iov = lub_list_node__get_data(node);
  221. lub_list_del(l, node);
  222. lub_list_node_free(node);
  223. write(pipe2[1], iov->iov_base, iov->iov_len);
  224. free(iov->iov_base);
  225. free(iov);
  226. }
  227. close(pipe2[1]);
  228. lub_list_free(l);
  229. _exit(0);
  230. }
  231. real_stdout = dup(STDOUT_FILENO);
  232. dup2(pipe1[1], STDOUT_FILENO);
  233. close(pipe1[0]);
  234. close(pipe1[1]);
  235. close(pipe2[1]);
  236. result = func(context, script);
  237. /* Restore real stdout */
  238. dup2(real_stdout, STDOUT_FILENO);
  239. close(real_stdout);
  240. /* Read the result of script execution */
  241. buf = konf_buf_new(pipe2[0]);
  242. while (konf_buf_read(buf) > 0);
  243. *out = konf_buf__dup_line(buf);
  244. konf_buf_delete(buf);
  245. close(pipe2[0]);
  246. /* Wait for the stdout-grabber process */
  247. waitpid(cpid, NULL, 0);
  248. return result;
  249. stdout_error:
  250. close(pipe1[0]);
  251. close(pipe1[1]);
  252. return -1;
  253. }
  254. /*----------------------------------------------------------- */
  255. int clish_shell_exec_action(clish_context_t *context, char **out, bool_t intr)
  256. {
  257. int result = -1;
  258. clish_sym_t *sym;
  259. char *script;
  260. void *func = NULL; /* We don't know the func API at this time */
  261. const clish_action_t *action = clish_context__get_action(context);
  262. clish_shell_t *shell = clish_context__get_shell(context);
  263. /* Signal vars */
  264. struct sigaction old_sigint, old_sigquit, old_sighup;
  265. struct sigaction sa;
  266. sigset_t old_sigs;
  267. if (!(sym = clish_action__get_builtin(action)))
  268. return 0;
  269. if (shell->dryrun && !clish_sym__get_permanent(sym))
  270. return 0;
  271. if (!(func = clish_sym__get_func(sym))) {
  272. fprintf(stderr, "Error: Default ACTION symbol is not specified.\n");
  273. return -1;
  274. }
  275. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  276. /* Ignore and block SIGINT, SIGQUIT, SIGHUP.
  277. * The SIG_IGN is not a case because it will be inherited
  278. * while a fork(). It's necessary to ignore signals because
  279. * the klish itself and ACTION script share the same terminal.
  280. */
  281. sa.sa_flags = 0;
  282. sigemptyset(&sa.sa_mask);
  283. sa.sa_handler = sigignore; /* Empty signal handler */
  284. sigaction(SIGINT, &sa, &old_sigint);
  285. sigaction(SIGQUIT, &sa, &old_sigquit);
  286. sigaction(SIGHUP, &sa, &old_sighup);
  287. /* Block signals for children processes. The block state is inherited. */
  288. if (!intr) {
  289. sigset_t sigs;
  290. sigemptyset(&sigs);
  291. sigaddset(&sigs, SIGINT);
  292. sigaddset(&sigs, SIGQUIT);
  293. sigaddset(&sigs, SIGHUP);
  294. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  295. }
  296. /* Find out the function API */
  297. /* CLISH_SYM_API_SIMPLE */
  298. if (clish_sym__get_api(sym) == CLISH_SYM_API_SIMPLE) {
  299. result = ((clish_hook_action_fn_t *)func)(context, script, out);
  300. /* CLISH_SYM_API_STDOUT and output is not needed */
  301. } else if ((clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) && (!out)) {
  302. result = ((clish_hook_oaction_fn_t *)func)(context, script);
  303. /* CLISH_SYM_API_STDOUT and outpus is needed */
  304. } else if (clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) {
  305. result = clish_shell_exec_oaction((clish_hook_oaction_fn_t *)func,
  306. context, script, out);
  307. }
  308. /* Restore SIGINT, SIGQUIT, SIGHUP */
  309. if (!intr) {
  310. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  311. /* Is the signals delivery guaranteed here (before
  312. sigaction restore) for previously blocked and
  313. pending signals? The simple test is working well.
  314. I don't want to use sigtimedwait() function because
  315. it needs a realtime extensions. The sigpending() with
  316. the sleep() is not nice too. Report bug if clish will
  317. get the SIGINT after non-interruptable action.
  318. */
  319. }
  320. sigaction(SIGINT, &old_sigint, NULL);
  321. sigaction(SIGQUIT, &old_sigquit, NULL);
  322. sigaction(SIGHUP, &old_sighup, NULL);
  323. lub_string_free(script);
  324. return result;
  325. }
  326. /*----------------------------------------------------------- */
  327. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  328. {
  329. clish_sym_t *sym;
  330. clish_shell_t *shell = clish_context__get_shell(clish_context);
  331. void *func;
  332. if (!(sym = shell->hooks[type]))
  333. return NULL;
  334. if (shell->dryrun && !clish_sym__get_permanent(sym))
  335. return NULL;
  336. if (!(func = clish_sym__get_func(sym)))
  337. return NULL;
  338. return func;
  339. }
  340. /*----------------------------------------------------------- */
  341. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  342. {
  343. clish_hook_config_fn_t *func = NULL;
  344. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  345. return func ? func(clish_context) : 0;
  346. }
  347. /*----------------------------------------------------------- */
  348. CLISH_HOOK_LOG(clish_shell_exec_log)
  349. {
  350. clish_hook_log_fn_t *func = NULL;
  351. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  352. return func ? func(clish_context, line, retcode) : 0;
  353. }
  354. /*----------------------------------------------------------- */
  355. char *clish_shell_mkfifo(clish_shell_t * this, char *name, size_t n)
  356. {
  357. int res;
  358. if (n < 1) /* Buffer too small */
  359. return NULL;
  360. do {
  361. strncpy(name, this->fifo_temp, n);
  362. name[n - 1] = '\0';
  363. mktemp(name);
  364. if (name[0] == '\0')
  365. return NULL;
  366. res = mkfifo(name, 0600);
  367. } while ((res < 0) && (EEXIST == errno));
  368. return name;
  369. }
  370. /*----------------------------------------------------------- */
  371. int clish_shell_rmfifo(clish_shell_t * this, const char *name)
  372. {
  373. return unlink(name);
  374. }
  375. /*-------------------------------------------------------- */
  376. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  377. {
  378. assert(this);
  379. this->log = log;
  380. }
  381. /*-------------------------------------------------------- */
  382. bool_t clish_shell__get_log(const clish_shell_t *this)
  383. {
  384. assert(this);
  385. return this->log;
  386. }
  387. /*-------------------------------------------------------- */
  388. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  389. {
  390. this->dryrun = dryrun;
  391. }
  392. /*-------------------------------------------------------- */
  393. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  394. {
  395. return this->dryrun;
  396. }
  397. /*----------------------------------------------------------- */