shell_execute.c 12 KB

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