shell_execute.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. if (clish_shell__get_canon_out(this) &&
  128. !clish_command__get_internal(cmd)) {
  129. char *space = NULL;
  130. char *full_line = clish_shell__get_full_line(context);
  131. if (this->depth > 0) {
  132. space = malloc(this->depth + 1);
  133. memset(space, ' ', this->depth);
  134. space[this->depth] = '\0';
  135. }
  136. printf("%s%s\n", space ? space : "", full_line);
  137. lub_string_free(full_line);
  138. if (space)
  139. free(space);
  140. }
  141. /* Unlock the lockfile */
  142. if (lock_fd != -1)
  143. clish_shell_unlock(lock_fd);
  144. /* Move into the new view */
  145. if (!result) {
  146. char *viewname = clish_shell_expand(clish_command__get_viewname(cmd), SHELL_VAR_NONE, context);
  147. if (viewname) {
  148. /* Search for the view */
  149. clish_view_t *view = clish_shell_find_view(this, viewname);
  150. if (!view)
  151. fprintf(stderr, "System error: Can't "
  152. "change view to %s\n", viewname);
  153. lub_string_free(viewname);
  154. /* Save the PWD */
  155. if (view) {
  156. char *line = clish_shell__get_line(context);
  157. clish_shell__set_pwd(this, line, view,
  158. clish_command__get_viewid(cmd), context);
  159. lub_string_free(line);
  160. }
  161. }
  162. }
  163. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  164. on the "set watchdog <timeout>" command itself. */
  165. if (this->wdog_timeout && saved_wdog_timeout) {
  166. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  167. this->wdog_active = BOOL_TRUE;
  168. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  169. "seconds.\nWarning: Press any key to stop watchdog.\n",
  170. this->wdog_timeout);
  171. } else
  172. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  173. error:
  174. return result;
  175. }
  176. /*----------------------------------------------------------- */
  177. /* Execute oaction. It suppose the forked process to get
  178. * script's stdout. Then forked process write the output back
  179. * to klish.
  180. */
  181. static int clish_shell_exec_oaction(clish_hook_oaction_fn_t func,
  182. void *context, const char *script, char **out)
  183. {
  184. int result = -1;
  185. int real_stdout; /* Saved stdout handler */
  186. int pipe1[2], pipe2[2];
  187. pid_t cpid = -1;
  188. konf_buf_t *buf;
  189. if (pipe(pipe1))
  190. return -1;
  191. if (pipe(pipe2))
  192. goto stdout_error;
  193. /* Create process to read script's stdout */
  194. cpid = fork();
  195. if (cpid == -1) {
  196. fprintf(stderr, "Error: Can't fork the stdout-grabber process.\n"
  197. "Error: The ACTION will be not executed.\n");
  198. goto stdout_error;
  199. }
  200. /* Child: read action's stdout */
  201. if (cpid == 0) {
  202. lub_list_t *l;
  203. lub_list_node_t *node;
  204. struct iovec *iov;
  205. const int rsize = CLISH_STDOUT_CHUNK; /* Read chunk size */
  206. size_t cur_size = 0;
  207. ssize_t r = 0;
  208. close(pipe1[1]);
  209. close(pipe2[0]);
  210. l = lub_list_new(NULL);
  211. /* Read the result of script execution */
  212. while (1) {
  213. ssize_t ret;
  214. iov = malloc(sizeof(*iov));
  215. iov->iov_len = rsize;
  216. iov->iov_base = malloc(iov->iov_len);
  217. do {
  218. ret = readv(pipe1[0], iov, 1);
  219. } while ((ret < 0) && (errno == EINTR));
  220. if (ret <= 0) { /* Error or EOF */
  221. free(iov->iov_base);
  222. free(iov);
  223. break;
  224. }
  225. iov->iov_len = ret;
  226. lub_list_add(l, iov);
  227. /* Check the max size of buffer */
  228. cur_size += ret;
  229. if (cur_size >= CLISH_STDOUT_MAXBUF)
  230. break;
  231. }
  232. close(pipe1[0]);
  233. /* Write the result of script back to klish */
  234. while ((node = lub_list__get_head(l))) {
  235. iov = lub_list_node__get_data(node);
  236. lub_list_del(l, node);
  237. lub_list_node_free(node);
  238. r = write(pipe2[1], iov->iov_base, iov->iov_len);
  239. free(iov->iov_base);
  240. free(iov);
  241. }
  242. close(pipe2[1]);
  243. lub_list_free(l);
  244. _exit(r < 0 ? 1 : 0);
  245. }
  246. real_stdout = dup(STDOUT_FILENO);
  247. dup2(pipe1[1], STDOUT_FILENO);
  248. close(pipe1[0]);
  249. close(pipe1[1]);
  250. close(pipe2[1]);
  251. result = func(context, script);
  252. /* Restore real stdout */
  253. dup2(real_stdout, STDOUT_FILENO);
  254. close(real_stdout);
  255. /* Read the result of script execution */
  256. buf = konf_buf_new(pipe2[0]);
  257. while (konf_buf_read(buf) > 0);
  258. *out = konf_buf__dup_line(buf);
  259. konf_buf_delete(buf);
  260. close(pipe2[0]);
  261. /* Wait for the stdout-grabber process */
  262. while (waitpid(cpid, NULL, 0) != cpid);
  263. return result;
  264. stdout_error:
  265. close(pipe1[0]);
  266. close(pipe1[1]);
  267. return -1;
  268. }
  269. /*----------------------------------------------------------- */
  270. int clish_shell_exec_action(clish_context_t *context, char **out, bool_t intr)
  271. {
  272. int result = -1;
  273. const clish_sym_t *sym;
  274. char *script;
  275. const void *func = NULL; /* We don't know the func API at this time */
  276. const clish_action_t *action = clish_context__get_action(context);
  277. clish_shell_t *shell = clish_context__get_shell(context);
  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. /*-------------------------------------------------------- */
  391. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  392. {
  393. assert(this);
  394. this->log = log;
  395. }
  396. /*-------------------------------------------------------- */
  397. bool_t clish_shell__get_log(const clish_shell_t *this)
  398. {
  399. assert(this);
  400. return this->log;
  401. }
  402. /*-------------------------------------------------------- */
  403. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  404. {
  405. this->dryrun = dryrun;
  406. }
  407. /*-------------------------------------------------------- */
  408. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  409. {
  410. return this->dryrun;
  411. }
  412. /*-------------------------------------------------------- */
  413. void clish_shell__set_canon_out(clish_shell_t *this, bool_t canon_out)
  414. {
  415. this->canon_out = canon_out;
  416. }
  417. /*-------------------------------------------------------- */
  418. bool_t clish_shell__get_canon_out(const clish_shell_t *this)
  419. {
  420. return this->canon_out;
  421. }
  422. /*----------------------------------------------------------- */