shell_execute.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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, "Warning: 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. "Warning: Try to get lock. Please wait...\n");
  46. sleep(1);
  47. continue;
  48. }
  49. break;
  50. }
  51. if (res == -1) {
  52. fprintf(stderr, "Error: 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 = clish_context__get_shell(context);
  75. const clish_command_t *cmd = clish_context__get_cmd(context);
  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. clish_context__set_action(context, 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)
  140. clish_shell_exec_config(context);
  141. /* Call logging callback */
  142. if (clish_shell__get_log(this) &&
  143. clish_shell_check_hook(context, CLISH_SYM_TYPE_LOG)) {
  144. char *full_line = clish_shell__get_full_line(context);
  145. clish_shell_exec_log(context, full_line, result);
  146. lub_string_free(full_line);
  147. }
  148. /* Unlock the lockfile */
  149. if (lock_fd != -1)
  150. clish_shell_unlock(lock_fd);
  151. /* Move into the new view */
  152. if (!result) {
  153. char *viewname = clish_shell_expand(clish_command__get_viewname(cmd), SHELL_VAR_NONE, context);
  154. if (viewname) {
  155. /* Search for the view */
  156. clish_view_t *view = clish_shell_find_view(this, viewname);
  157. lub_string_free(viewname);
  158. /* Save the PWD */
  159. if (view) {
  160. char *line = clish_shell__get_line(context);
  161. clish_shell__set_pwd(this, line, view,
  162. clish_command__get_viewid(cmd), context);
  163. lub_string_free(line);
  164. }
  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_hook_action_fn_t *func = NULL;
  187. const clish_action_t *action = clish_context__get_action(context);
  188. clish_shell_t *shell = clish_context__get_shell(context);
  189. if (!(sym = clish_action__get_builtin(action)))
  190. return 0;
  191. if (shell->dryrun && !clish_sym__get_permanent(sym))
  192. return 0;
  193. if (!(func = clish_sym__get_func(sym))) {
  194. fprintf(stderr, "Error: Default ACTION symbol is not specified.\n");
  195. return -1;
  196. }
  197. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  198. result = func(context, script, out);
  199. lub_string_free(script);
  200. return result;
  201. }
  202. /*----------------------------------------------------------- */
  203. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  204. {
  205. clish_sym_t *sym;
  206. clish_shell_t *shell = clish_context__get_shell(clish_context);
  207. void *func;
  208. if (!(sym = shell->hooks[type]))
  209. return NULL;
  210. if (shell->dryrun && !clish_sym__get_permanent(sym))
  211. return NULL;
  212. if (!(func = clish_sym__get_func(sym)))
  213. return NULL;
  214. return func;
  215. }
  216. /*----------------------------------------------------------- */
  217. CLISH_HOOK_ACCESS(clish_shell_exec_access)
  218. {
  219. clish_hook_access_fn_t *func = NULL;
  220. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_ACCESS);
  221. return func ? func(clish_context, access) : 0;
  222. }
  223. /*----------------------------------------------------------- */
  224. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  225. {
  226. clish_hook_config_fn_t *func = NULL;
  227. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  228. return func ? func(clish_context) : 0;
  229. }
  230. /*----------------------------------------------------------- */
  231. CLISH_HOOK_LOG(clish_shell_exec_log)
  232. {
  233. clish_hook_log_fn_t *func = NULL;
  234. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  235. return func ? func(clish_context, line, retcode) : 0;
  236. }
  237. /*----------------------------------------------------------- */
  238. const char *clish_shell__get_fifo(clish_shell_t * this)
  239. {
  240. char *name;
  241. int res;
  242. if (this->fifo_name) {
  243. if (0 == access(this->fifo_name, R_OK | W_OK))
  244. return this->fifo_name;
  245. unlink(this->fifo_name);
  246. lub_string_free(this->fifo_name);
  247. this->fifo_name = NULL;
  248. }
  249. do {
  250. char template[] = "/tmp/klish.fifo.XXXXXX";
  251. name = mktemp(template);
  252. if (name[0] == '\0')
  253. return NULL;
  254. res = mkfifo(name, 0600);
  255. if (res == 0)
  256. this->fifo_name = lub_string_dup(name);
  257. } while ((res < 0) && (EEXIST == errno));
  258. return this->fifo_name;
  259. }
  260. /*-------------------------------------------------------- */
  261. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  262. {
  263. assert(this);
  264. this->log = log;
  265. }
  266. /*-------------------------------------------------------- */
  267. bool_t clish_shell__get_log(const clish_shell_t *this)
  268. {
  269. assert(this);
  270. return this->log;
  271. }
  272. /*-------------------------------------------------------- */
  273. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  274. {
  275. this->dryrun = dryrun;
  276. }
  277. /*-------------------------------------------------------- */
  278. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  279. {
  280. return this->dryrun;
  281. }
  282. /*----------------------------------------------------------- */