shell_execute.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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)
  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 = context->action;
  188. clish_shell_t *shell = context->shell;
  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. return -1;
  195. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  196. result = func(context, script, out);
  197. lub_string_free(script);
  198. return result;
  199. }
  200. /*----------------------------------------------------------- */
  201. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  202. {
  203. clish_sym_t *sym;
  204. clish_context_t *context = (clish_context_t *)clish_context;
  205. clish_shell_t *shell = context->shell;
  206. void *func;
  207. if (!(sym = shell->hooks[type]))
  208. return NULL;
  209. if (shell->dryrun && !clish_sym__get_permanent(sym))
  210. return NULL;
  211. if (!(func = clish_sym__get_func(sym)))
  212. return NULL;
  213. return func;
  214. }
  215. /*----------------------------------------------------------- */
  216. CLISH_HOOK_INIT(clish_shell_exec_init)
  217. {
  218. clish_hook_init_fn_t *func = NULL;
  219. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_INIT);
  220. return func ? func(clish_context) : 0;
  221. }
  222. /*----------------------------------------------------------- */
  223. CLISH_HOOK_FINI(clish_shell_exec_fini)
  224. {
  225. clish_hook_fini_fn_t *func = NULL;
  226. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_FINI);
  227. return func ? func(clish_context) : 0;
  228. }
  229. /*----------------------------------------------------------- */
  230. CLISH_HOOK_ACCESS(clish_shell_exec_access)
  231. {
  232. clish_hook_access_fn_t *func = NULL;
  233. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_ACCESS);
  234. return func ? func(clish_context, access) : 0;
  235. }
  236. /*----------------------------------------------------------- */
  237. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  238. {
  239. clish_hook_config_fn_t *func = NULL;
  240. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  241. return func ? func(clish_context) : 0;
  242. }
  243. /*----------------------------------------------------------- */
  244. CLISH_HOOK_LOG(clish_shell_exec_log)
  245. {
  246. clish_hook_log_fn_t *func = NULL;
  247. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  248. return func ? func(clish_context, line, retcode) : 0;
  249. }
  250. /*----------------------------------------------------------- */
  251. const char *clish_shell__get_fifo(clish_shell_t * this)
  252. {
  253. char *name;
  254. int res;
  255. if (this->fifo_name) {
  256. if (0 == access(this->fifo_name, R_OK | W_OK))
  257. return this->fifo_name;
  258. unlink(this->fifo_name);
  259. lub_string_free(this->fifo_name);
  260. this->fifo_name = NULL;
  261. }
  262. do {
  263. char template[] = "/tmp/klish.fifo.XXXXXX";
  264. name = mktemp(template);
  265. if (name[0] == '\0')
  266. return NULL;
  267. res = mkfifo(name, 0600);
  268. if (res == 0)
  269. this->fifo_name = lub_string_dup(name);
  270. } while ((res < 0) && (EEXIST == errno));
  271. return this->fifo_name;
  272. }
  273. /*--------------------------------------------------------- */
  274. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  275. {
  276. return this->client_cookie;
  277. }
  278. /*-------------------------------------------------------- */
  279. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  280. {
  281. assert(this);
  282. this->log = log;
  283. }
  284. /*-------------------------------------------------------- */
  285. bool_t clish_shell__get_log(const clish_shell_t *this)
  286. {
  287. assert(this);
  288. return this->log;
  289. }
  290. /*-------------------------------------------------------- */
  291. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  292. {
  293. this->dryrun = dryrun;
  294. }
  295. /*-------------------------------------------------------- */
  296. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  297. {
  298. return this->dryrun;
  299. }
  300. /*----------------------------------------------------------- */