shell_execute.c 7.1 KB

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