shell_execute.c 8.6 KB

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