shell_execute.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. clish_view_t *view = clish_command__get_view(cmd);
  154. /* Save the PWD */
  155. if (view) {
  156. char *line = clish_shell__get_line(context);
  157. clish_shell__set_pwd(this, line, view,
  158. clish_command__get_viewid(cmd), context);
  159. lub_string_free(line);
  160. }
  161. }
  162. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  163. on the "set watchdog <timeout>" command itself. */
  164. if (this->wdog_timeout && saved_wdog_timeout) {
  165. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  166. this->wdog_active = BOOL_TRUE;
  167. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  168. "seconds.\nWarning: Press any key to stop watchdog.\n",
  169. this->wdog_timeout);
  170. } else
  171. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  172. error:
  173. return result;
  174. }
  175. /*----------------------------------------------------------- */
  176. int clish_shell_exec_action(clish_context_t *context, char **out)
  177. {
  178. int result = -1;
  179. clish_sym_t *sym;
  180. char *script;
  181. clish_hook_action_fn_t *func = NULL;
  182. const clish_action_t *action = context->action;
  183. clish_shell_t *shell = context->shell;
  184. if (!(sym = clish_action__get_builtin(action)))
  185. return 0;
  186. if (shell->dryrun && !clish_sym__get_permanent(sym))
  187. return 0;
  188. if (!(func = clish_sym__get_func(sym)))
  189. return -1;
  190. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  191. result = func(context, script, out);
  192. lub_string_free(script);
  193. return result;
  194. }
  195. /*----------------------------------------------------------- */
  196. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  197. {
  198. clish_sym_t *sym;
  199. clish_context_t *context = (clish_context_t *)clish_context;
  200. clish_shell_t *shell = context->shell;
  201. void *func;
  202. if (!(sym = shell->hooks[type]))
  203. return NULL;
  204. if (shell->dryrun && !clish_sym__get_permanent(sym))
  205. return NULL;
  206. if (!(func = clish_sym__get_func(sym)))
  207. return NULL;
  208. return func;
  209. }
  210. /*----------------------------------------------------------- */
  211. CLISH_HOOK_INIT(clish_shell_exec_init)
  212. {
  213. clish_hook_init_fn_t *func = NULL;
  214. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_INIT);
  215. return func ? func(clish_context) : 0;
  216. }
  217. /*----------------------------------------------------------- */
  218. CLISH_HOOK_FINI(clish_shell_exec_fini)
  219. {
  220. clish_hook_fini_fn_t *func = NULL;
  221. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_FINI);
  222. return func ? func(clish_context) : 0;
  223. }
  224. /*----------------------------------------------------------- */
  225. CLISH_HOOK_ACCESS(clish_shell_exec_access)
  226. {
  227. clish_hook_access_fn_t *func = NULL;
  228. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_ACCESS);
  229. return func ? func(clish_context, access) : 0;
  230. }
  231. /*----------------------------------------------------------- */
  232. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  233. {
  234. clish_hook_config_fn_t *func = NULL;
  235. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  236. return func ? func(clish_context) : 0;
  237. }
  238. /*----------------------------------------------------------- */
  239. CLISH_HOOK_LOG(clish_shell_exec_log)
  240. {
  241. clish_hook_log_fn_t *func = NULL;
  242. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  243. return func ? func(clish_context, line, retcode) : 0;
  244. }
  245. /*----------------------------------------------------------- */
  246. const char *clish_shell__get_fifo(clish_shell_t * this)
  247. {
  248. char *name;
  249. int res;
  250. if (this->fifo_name) {
  251. if (0 == access(this->fifo_name, R_OK | W_OK))
  252. return this->fifo_name;
  253. unlink(this->fifo_name);
  254. lub_string_free(this->fifo_name);
  255. this->fifo_name = NULL;
  256. }
  257. do {
  258. char template[] = "/tmp/klish.fifo.XXXXXX";
  259. name = mktemp(template);
  260. if (name[0] == '\0')
  261. return NULL;
  262. res = mkfifo(name, 0600);
  263. if (res == 0)
  264. this->fifo_name = lub_string_dup(name);
  265. } while ((res < 0) && (EEXIST == errno));
  266. return this->fifo_name;
  267. }
  268. /*--------------------------------------------------------- */
  269. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  270. {
  271. return this->client_cookie;
  272. }
  273. /*-------------------------------------------------------- */
  274. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  275. {
  276. assert(this);
  277. this->log = log;
  278. }
  279. /*-------------------------------------------------------- */
  280. bool_t clish_shell__get_log(const clish_shell_t *this)
  281. {
  282. assert(this);
  283. return this->log;
  284. }
  285. /*-------------------------------------------------------- */
  286. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  287. {
  288. this->dryrun = dryrun;
  289. }
  290. /*-------------------------------------------------------- */
  291. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  292. {
  293. return this->dryrun;
  294. }
  295. /*----------------------------------------------------------- */