shell_execute.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #ifdef FD_CLOEXEC
  33. fcntl(lock_fd, F_SETFD, fcntl(lock_fd, F_GETFD) | FD_CLOEXEC);
  34. #endif
  35. memset(&lock, 0, sizeof(lock));
  36. lock.l_type = F_WRLCK;
  37. lock.l_whence = SEEK_SET;
  38. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  39. res = fcntl(lock_fd, F_SETLK, &lock);
  40. if (res != -1)
  41. break;
  42. if (EINTR == errno)
  43. continue;
  44. if ((EAGAIN == errno) || (EACCES == errno)) {
  45. if (0 == i)
  46. fprintf(stderr,
  47. "Warning: Try to get lock. Please wait...\n");
  48. sleep(1);
  49. continue;
  50. }
  51. if (EINVAL == errno)
  52. fprintf(stderr, "Error: Locking isn't supported by OS, consider \"--lockless\".\n");
  53. break;
  54. }
  55. if (res == -1) {
  56. fprintf(stderr, "Error: Can't get lock.\n");
  57. close(lock_fd);
  58. return -1;
  59. }
  60. return lock_fd;
  61. }
  62. /*-------------------------------------------------------- */
  63. static void clish_shell_unlock(int lock_fd)
  64. {
  65. struct flock lock;
  66. if (lock_fd == -1)
  67. return;
  68. memset(&lock, 0, sizeof(lock));
  69. lock.l_type = F_UNLCK;
  70. lock.l_whence = SEEK_SET;
  71. fcntl(lock_fd, F_SETLK, &lock);
  72. close(lock_fd);
  73. }
  74. /*----------------------------------------------------------- */
  75. int clish_shell_execute(clish_context_t *context, char **out)
  76. {
  77. clish_shell_t *this = clish_context__get_shell(context);
  78. const clish_command_t *cmd = clish_context__get_cmd(context);
  79. int result = 0;
  80. char *lock_path = clish_shell__get_lockfile(this);
  81. int lock_fd = -1;
  82. sigset_t old_sigs;
  83. struct sigaction old_sigint, old_sigquit, old_sighup;
  84. clish_view_t *cur_view = clish_shell__get_view(this);
  85. unsigned int saved_wdog_timeout = this->wdog_timeout;
  86. assert(cmd);
  87. /* Pre-change view if the command is from another depth/view */
  88. {
  89. clish_view_restore_t restore = clish_command__get_restore(cmd);
  90. if ((CLISH_RESTORE_VIEW == restore) &&
  91. (clish_command__get_pview(cmd) != cur_view)) {
  92. clish_view_t *view = clish_command__get_pview(cmd);
  93. clish_shell__set_pwd(this, NULL, view, NULL, context);
  94. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  95. (clish_command__get_depth(cmd) < this->depth)) {
  96. this->depth = clish_command__get_depth(cmd);
  97. }
  98. }
  99. /* Lock the lockfile */
  100. if (lock_path && clish_command__get_lock(cmd)) {
  101. lock_fd = clish_shell_lock(lock_path);
  102. if (-1 == lock_fd) {
  103. result = -1;
  104. goto error; /* Can't set lock */
  105. }
  106. }
  107. /* Ignore and block SIGINT, SIGQUIT, SIGHUP */
  108. if (!clish_command__get_interrupt(cmd)) {
  109. struct sigaction sa;
  110. sigset_t sigs;
  111. sa.sa_flags = 0;
  112. sigemptyset(&sa.sa_mask);
  113. sa.sa_handler = SIG_IGN;
  114. sigaction(SIGINT, &sa, &old_sigint);
  115. sigaction(SIGQUIT, &sa, &old_sigquit);
  116. sigaction(SIGHUP, &sa, &old_sighup);
  117. sigemptyset(&sigs);
  118. sigaddset(&sigs, SIGINT);
  119. sigaddset(&sigs, SIGQUIT);
  120. sigaddset(&sigs, SIGHUP);
  121. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  122. }
  123. /* Execute ACTION */
  124. clish_context__set_action(context, clish_command__get_action(cmd));
  125. result = clish_shell_exec_action(context, out);
  126. /* Restore SIGINT, SIGQUIT, SIGHUP */
  127. if (!clish_command__get_interrupt(cmd)) {
  128. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  129. /* Is the signals delivery guaranteed here (before
  130. sigaction restore) for previously blocked and
  131. pending signals? The simple test is working well.
  132. I don't want to use sigtimedwait() function bacause
  133. it needs a realtime extensions. The sigpending() with
  134. the sleep() is not nice too. Report bug if clish will
  135. get the SIGINT after non-interruptable action.
  136. */
  137. sigaction(SIGINT, &old_sigint, NULL);
  138. sigaction(SIGQUIT, &old_sigquit, NULL);
  139. sigaction(SIGHUP, &old_sighup, NULL);
  140. }
  141. /* Call config callback */
  142. if (!result)
  143. clish_shell_exec_config(context);
  144. /* Call logging callback */
  145. if (clish_shell__get_log(this) &&
  146. clish_shell_check_hook(context, CLISH_SYM_TYPE_LOG)) {
  147. char *full_line = clish_shell__get_full_line(context);
  148. clish_shell_exec_log(context, full_line, result);
  149. lub_string_free(full_line);
  150. }
  151. /* Unlock the lockfile */
  152. if (lock_fd != -1)
  153. clish_shell_unlock(lock_fd);
  154. /* Move into the new view */
  155. if (!result) {
  156. char *viewname = clish_shell_expand(clish_command__get_viewname(cmd), SHELL_VAR_NONE, context);
  157. if (viewname) {
  158. /* Search for the view */
  159. clish_view_t *view = clish_shell_find_view(this, viewname);
  160. if (!view)
  161. fprintf(stderr, "System error: Can't "
  162. "change view to %s\n", viewname);
  163. lub_string_free(viewname);
  164. /* Save the PWD */
  165. if (view) {
  166. char *line = clish_shell__get_line(context);
  167. clish_shell__set_pwd(this, line, view,
  168. clish_command__get_viewid(cmd), context);
  169. lub_string_free(line);
  170. }
  171. }
  172. }
  173. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  174. on the "set watchdog <timeout>" command itself. */
  175. if (this->wdog_timeout && saved_wdog_timeout) {
  176. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  177. this->wdog_active = BOOL_TRUE;
  178. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  179. "seconds.\nWarning: Press any key to stop watchdog.\n",
  180. this->wdog_timeout);
  181. } else
  182. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  183. error:
  184. return result;
  185. }
  186. /*----------------------------------------------------------- */
  187. int clish_shell_exec_action(clish_context_t *context, char **out)
  188. {
  189. int result = -1;
  190. clish_sym_t *sym;
  191. char *script;
  192. void *func = NULL; /* We don't know the func API at this time */
  193. const clish_action_t *action = clish_context__get_action(context);
  194. clish_shell_t *shell = clish_context__get_shell(context);
  195. if (!(sym = clish_action__get_builtin(action)))
  196. return 0;
  197. if (shell->dryrun && !clish_sym__get_permanent(sym))
  198. return 0;
  199. if (!(func = clish_sym__get_func(sym))) {
  200. fprintf(stderr, "Error: Default ACTION symbol is not specified.\n");
  201. return -1;
  202. }
  203. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  204. /* Find out the function API */
  205. if (clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) {
  206. result = ((clish_hook_oaction_fn_t *)func)(context, script);
  207. } else { /* CLISH_SYM_API_SIMPLE */
  208. result = ((clish_hook_action_fn_t *)func)(context, script, out);
  209. }
  210. lub_string_free(script);
  211. return result;
  212. }
  213. /*----------------------------------------------------------- */
  214. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  215. {
  216. clish_sym_t *sym;
  217. clish_shell_t *shell = clish_context__get_shell(clish_context);
  218. void *func;
  219. if (!(sym = shell->hooks[type]))
  220. return NULL;
  221. if (shell->dryrun && !clish_sym__get_permanent(sym))
  222. return NULL;
  223. if (!(func = clish_sym__get_func(sym)))
  224. return NULL;
  225. return func;
  226. }
  227. /*----------------------------------------------------------- */
  228. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  229. {
  230. clish_hook_config_fn_t *func = NULL;
  231. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  232. return func ? func(clish_context) : 0;
  233. }
  234. /*----------------------------------------------------------- */
  235. CLISH_HOOK_LOG(clish_shell_exec_log)
  236. {
  237. clish_hook_log_fn_t *func = NULL;
  238. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  239. return func ? func(clish_context, line, retcode) : 0;
  240. }
  241. /*----------------------------------------------------------- */
  242. const char *clish_shell__get_fifo(clish_shell_t * this)
  243. {
  244. char *name;
  245. int res;
  246. if (this->fifo_name) {
  247. if (0 == access(this->fifo_name, R_OK | W_OK))
  248. return this->fifo_name;
  249. unlink(this->fifo_name);
  250. lub_string_free(this->fifo_name);
  251. this->fifo_name = NULL;
  252. }
  253. do {
  254. char template[] = "/tmp/klish.fifo.XXXXXX";
  255. name = mktemp(template);
  256. if (name[0] == '\0')
  257. return NULL;
  258. res = mkfifo(name, 0600);
  259. if (res == 0)
  260. this->fifo_name = lub_string_dup(name);
  261. } while ((res < 0) && (EEXIST == errno));
  262. return this->fifo_name;
  263. }
  264. /*-------------------------------------------------------- */
  265. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  266. {
  267. assert(this);
  268. this->log = log;
  269. }
  270. /*-------------------------------------------------------- */
  271. bool_t clish_shell__get_log(const clish_shell_t *this)
  272. {
  273. assert(this);
  274. return this->log;
  275. }
  276. /*-------------------------------------------------------- */
  277. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  278. {
  279. this->dryrun = dryrun;
  280. }
  281. /*-------------------------------------------------------- */
  282. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  283. {
  284. return this->dryrun;
  285. }
  286. /*----------------------------------------------------------- */