shell_execute.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <signal.h>
  17. #include <fcntl.h>
  18. /*-------------------------------------------------------- */
  19. static int clish_shell_lock(const char *lock_path)
  20. {
  21. int i;
  22. int res;
  23. int lock_fd = -1;
  24. struct flock lock;
  25. if (!lock_path)
  26. return -1;
  27. lock_fd = open(lock_path, O_WRONLY | O_CREAT, 00644);
  28. if (-1 == lock_fd) {
  29. fprintf(stderr, "Can't open lockfile %s.\n", lock_path);
  30. return -1;
  31. }
  32. lock.l_type = F_WRLCK;
  33. lock.l_whence = SEEK_SET;
  34. lock.l_start = 0;
  35. lock.l_len = 0;
  36. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  37. res = fcntl(lock_fd, F_SETLK, &lock);
  38. if (res != -1)
  39. break;
  40. if (EINTR == errno)
  41. continue;
  42. if ((EAGAIN == errno) || (EACCES == errno)) {
  43. if (0 == i)
  44. fprintf(stderr,
  45. "Try to get lock. Please wait...\n");
  46. sleep(1);
  47. continue;
  48. }
  49. break;
  50. }
  51. if (res == -1) {
  52. fprintf(stderr, "Can't get lock.\n");
  53. close(lock_fd);
  54. return -1;
  55. }
  56. return lock_fd;
  57. }
  58. /*-------------------------------------------------------- */
  59. static void clish_shell_unlock(int lock_fd)
  60. {
  61. struct flock lock;
  62. if (lock_fd == -1)
  63. return;
  64. lock.l_type = F_UNLCK;
  65. lock.l_whence = SEEK_SET;
  66. lock.l_start = 0;
  67. lock.l_len = 0;
  68. fcntl(lock_fd, F_SETLK, &lock);
  69. close(lock_fd);
  70. }
  71. /*----------------------------------------------------------- */
  72. int clish_shell_execute(clish_context_t *context, char **out)
  73. {
  74. clish_shell_t *this = context->shell;
  75. const clish_command_t *cmd = context->cmd;
  76. int result = 0;
  77. char *lock_path = clish_shell__get_lockfile(this);
  78. int lock_fd = -1;
  79. sigset_t old_sigs;
  80. struct sigaction old_sigint, old_sigquit, old_sighup;
  81. clish_view_t *cur_view = clish_shell__get_view(this);
  82. unsigned int saved_wdog_timeout = this->wdog_timeout;
  83. assert(cmd);
  84. /* Pre-change view if the command is from another depth/view */
  85. {
  86. clish_view_restore_t restore = clish_command__get_restore(cmd);
  87. if ((CLISH_RESTORE_VIEW == restore) &&
  88. (clish_command__get_pview(cmd) != cur_view)) {
  89. clish_view_t *view = clish_command__get_pview(cmd);
  90. clish_shell__set_pwd(this, NULL, view, NULL, context);
  91. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  92. (clish_command__get_depth(cmd) < this->depth)) {
  93. this->depth = clish_command__get_depth(cmd);
  94. }
  95. }
  96. /* Lock the lockfile */
  97. if (lock_path && clish_command__get_lock(cmd)) {
  98. lock_fd = clish_shell_lock(lock_path);
  99. if (-1 == lock_fd) {
  100. result = -1;
  101. goto error; /* Can't set lock */
  102. }
  103. }
  104. /* Ignore and block SIGINT, SIGQUIT, SIGHUP */
  105. if (!clish_command__get_interrupt(cmd)) {
  106. struct sigaction sa;
  107. sigset_t sigs;
  108. sa.sa_flags = 0;
  109. sigemptyset(&sa.sa_mask);
  110. sa.sa_handler = SIG_IGN;
  111. sigaction(SIGINT, &sa, &old_sigint);
  112. sigaction(SIGQUIT, &sa, &old_sigquit);
  113. sigaction(SIGHUP, &sa, &old_sighup);
  114. sigemptyset(&sigs);
  115. sigaddset(&sigs, SIGINT);
  116. sigaddset(&sigs, SIGQUIT);
  117. sigaddset(&sigs, SIGHUP);
  118. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  119. }
  120. /* Execute ACTION */
  121. context->action = clish_command__get_action(cmd);
  122. result = clish_shell_exec_action(context, out);
  123. /* Restore SIGINT, SIGQUIT, SIGHUP */
  124. if (!clish_command__get_interrupt(cmd)) {
  125. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  126. /* Is the signals delivery guaranteed here (before
  127. sigaction restore) for previously blocked and
  128. pending signals? The simple test is working well.
  129. I don't want to use sigtimedwait() function bacause
  130. it needs a realtime extensions. The sigpending() with
  131. the sleep() is not nice too. Report bug if clish will
  132. get the SIGINT after non-interruptable action.
  133. */
  134. sigaction(SIGINT, &old_sigint, NULL);
  135. sigaction(SIGQUIT, &old_sigquit, NULL);
  136. sigaction(SIGHUP, &old_sighup, NULL);
  137. }
  138. /* Call config callback */
  139. if (!result && this->hooks->config_fn)
  140. this->hooks->config_fn(context);
  141. /* Call logging callback */
  142. if (clish_shell__get_log(this) && this->hooks->log_fn) {
  143. char *full_line = clish_shell__get_full_line(context);
  144. this->hooks->log_fn(context, full_line, result);
  145. lub_string_free(full_line);
  146. }
  147. /* Unlock the lockfile */
  148. if (lock_fd != -1)
  149. clish_shell_unlock(lock_fd);
  150. /* Move into the new view */
  151. if (!result) {
  152. clish_view_t *view = clish_command__get_view(cmd);
  153. /* Save the PWD */
  154. if (view) {
  155. char *line = clish_shell__get_line(context);
  156. clish_shell__set_pwd(this, line, view,
  157. clish_command__get_viewid(cmd), context);
  158. lub_string_free(line);
  159. }
  160. }
  161. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  162. on the "set watchdog <timeout>" command itself. */
  163. if (this->wdog_timeout && saved_wdog_timeout) {
  164. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  165. this->wdog_active = BOOL_TRUE;
  166. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  167. "seconds.\nWarning: Press any key to stop watchdog.\n",
  168. this->wdog_timeout);
  169. } else
  170. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  171. error:
  172. return result;
  173. }
  174. /*----------------------------------------------------------- */
  175. int clish_shell_exec_action(clish_context_t *context, char **out)
  176. {
  177. int result = -1;
  178. clish_sym_t *sym;
  179. char *script;
  180. clish_plugin_fn_t *func = NULL;
  181. const clish_action_t *action = context->action;
  182. clish_shell_t *shell = context->shell;
  183. if (!(sym = clish_action__get_builtin(action)))
  184. return 0;
  185. if (shell->dryrun && !clish_sym__get_permanent(sym))
  186. return 0;
  187. if (!(func = clish_sym__get_func(sym)))
  188. return -1;
  189. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  190. result = func(context, script, out);
  191. lub_string_free(script);
  192. return result;
  193. }
  194. /*----------------------------------------------------------- */
  195. const char *clish_shell__get_fifo(clish_shell_t * this)
  196. {
  197. char *name;
  198. int res;
  199. if (this->fifo_name) {
  200. if (0 == access(this->fifo_name, R_OK | W_OK))
  201. return this->fifo_name;
  202. unlink(this->fifo_name);
  203. lub_string_free(this->fifo_name);
  204. this->fifo_name = NULL;
  205. }
  206. do {
  207. char template[] = "/tmp/klish.fifo.XXXXXX";
  208. name = mktemp(template);
  209. if (name[0] == '\0')
  210. return NULL;
  211. res = mkfifo(name, 0600);
  212. if (res == 0)
  213. this->fifo_name = lub_string_dup(name);
  214. } while ((res < 0) && (EEXIST == errno));
  215. return this->fifo_name;
  216. }
  217. /*--------------------------------------------------------- */
  218. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  219. {
  220. return this->client_cookie;
  221. }
  222. /*-------------------------------------------------------- */
  223. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  224. {
  225. assert(this);
  226. this->log = log;
  227. }
  228. /*-------------------------------------------------------- */
  229. bool_t clish_shell__get_log(const clish_shell_t *this)
  230. {
  231. assert(this);
  232. return this->log;
  233. }
  234. /*-------------------------------------------------------- */
  235. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  236. {
  237. this->dryrun = dryrun;
  238. }
  239. /*-------------------------------------------------------- */
  240. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  241. {
  242. return this->dryrun;
  243. }
  244. /*----------------------------------------------------------- */