shell_execute.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 = 1024; /* Read chunk size */
  218. close(pipe1[1]);
  219. close(pipe2[0]);
  220. l = lub_list_new(NULL);
  221. /* Read the result of script execution */
  222. while (1) {
  223. ssize_t ret;
  224. iov = malloc(sizeof(*iov));
  225. iov->iov_len = rsize;
  226. iov->iov_base = malloc(iov->iov_len);
  227. do {
  228. ret = readv(pipe1[0], iov, 1);
  229. } while ((ret < 0) && (errno == EINTR));
  230. if (ret <= 0) { /* Error or EOF */
  231. free(iov->iov_base);
  232. free(iov);
  233. break;
  234. }
  235. iov->iov_len = ret;
  236. lub_list_add(l, iov);
  237. }
  238. close(pipe1[0]);
  239. /* Write the result of script back to klish */
  240. while ((node = lub_list__get_head(l))) {
  241. iov = lub_list_node__get_data(node);
  242. lub_list_del(l, node);
  243. lub_list_node_free(node);
  244. write(pipe2[1], iov->iov_base, iov->iov_len);
  245. free(iov->iov_base);
  246. free(iov);
  247. }
  248. close(pipe2[1]);
  249. lub_list_free(l);
  250. _exit(0);
  251. }
  252. real_stdout = dup(STDOUT_FILENO);
  253. dup2(pipe1[1], STDOUT_FILENO);
  254. close(pipe1[0]);
  255. close(pipe1[1]);
  256. close(pipe2[1]);
  257. result = func(context, script);
  258. /* Restore real stdout */
  259. dup2(real_stdout, STDOUT_FILENO);
  260. close(real_stdout);
  261. /* Read the result of script execution */
  262. buf = konf_buf_new(pipe2[0]);
  263. while (konf_buf_read(buf) > 0);
  264. *out = konf_buf__dup_line(buf);
  265. konf_buf_delete(buf);
  266. close(pipe2[0]);
  267. /* Wait for the stdout-grabber process */
  268. waitpid(cpid, NULL, 0);
  269. return result;
  270. stdout_error:
  271. close(pipe1[0]);
  272. close(pipe1[1]);
  273. return -1;
  274. }
  275. /*----------------------------------------------------------- */
  276. int clish_shell_exec_action(clish_context_t *context, char **out)
  277. {
  278. int result = -1;
  279. clish_sym_t *sym;
  280. char *script;
  281. void *func = NULL; /* We don't know the func API at this time */
  282. const clish_action_t *action = clish_context__get_action(context);
  283. clish_shell_t *shell = clish_context__get_shell(context);
  284. if (!(sym = clish_action__get_builtin(action)))
  285. return 0;
  286. if (shell->dryrun && !clish_sym__get_permanent(sym))
  287. return 0;
  288. if (!(func = clish_sym__get_func(sym))) {
  289. fprintf(stderr, "Error: Default ACTION symbol is not specified.\n");
  290. return -1;
  291. }
  292. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  293. /* Find out the function API */
  294. /* CLISH_SYM_API_SIMPLE */
  295. if (clish_sym__get_api(sym) == CLISH_SYM_API_SIMPLE) {
  296. result = ((clish_hook_action_fn_t *)func)(context, script, out);
  297. /* CLISH_SYM_API_STDOUT and output is not needed */
  298. } else if ((clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) && (!out)) {
  299. result = ((clish_hook_oaction_fn_t *)func)(context, script);
  300. /* CLISH_SYM_API_STDOUT and outpus is needed */
  301. } else if (clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) {
  302. result = clish_shell_exec_oaction((clish_hook_oaction_fn_t *)func,
  303. context, script, out);
  304. }
  305. lub_string_free(script);
  306. return result;
  307. }
  308. /*----------------------------------------------------------- */
  309. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  310. {
  311. clish_sym_t *sym;
  312. clish_shell_t *shell = clish_context__get_shell(clish_context);
  313. void *func;
  314. if (!(sym = shell->hooks[type]))
  315. return NULL;
  316. if (shell->dryrun && !clish_sym__get_permanent(sym))
  317. return NULL;
  318. if (!(func = clish_sym__get_func(sym)))
  319. return NULL;
  320. return func;
  321. }
  322. /*----------------------------------------------------------- */
  323. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  324. {
  325. clish_hook_config_fn_t *func = NULL;
  326. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  327. return func ? func(clish_context) : 0;
  328. }
  329. /*----------------------------------------------------------- */
  330. CLISH_HOOK_LOG(clish_shell_exec_log)
  331. {
  332. clish_hook_log_fn_t *func = NULL;
  333. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  334. return func ? func(clish_context, line, retcode) : 0;
  335. }
  336. /*----------------------------------------------------------- */
  337. const char *clish_shell__get_fifo(clish_shell_t * this)
  338. {
  339. char *name;
  340. int res;
  341. if (this->fifo_name) {
  342. if (0 == access(this->fifo_name, R_OK | W_OK))
  343. return this->fifo_name;
  344. unlink(this->fifo_name);
  345. lub_string_free(this->fifo_name);
  346. this->fifo_name = NULL;
  347. }
  348. do {
  349. char template[] = "/tmp/klish.fifo.XXXXXX";
  350. name = mktemp(template);
  351. if (name[0] == '\0')
  352. return NULL;
  353. res = mkfifo(name, 0600);
  354. if (res == 0)
  355. this->fifo_name = lub_string_dup(name);
  356. } while ((res < 0) && (EEXIST == errno));
  357. return this->fifo_name;
  358. }
  359. /*-------------------------------------------------------- */
  360. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  361. {
  362. assert(this);
  363. this->log = log;
  364. }
  365. /*-------------------------------------------------------- */
  366. bool_t clish_shell__get_log(const clish_shell_t *this)
  367. {
  368. assert(this);
  369. return this->log;
  370. }
  371. /*-------------------------------------------------------- */
  372. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  373. {
  374. this->dryrun = dryrun;
  375. }
  376. /*-------------------------------------------------------- */
  377. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  378. {
  379. return this->dryrun;
  380. }
  381. /*----------------------------------------------------------- */