1
0

shell_execute.c 12 KB

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