1
0

shell_execute.c 7.1 KB

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