1
0

shell_execute.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. lock.l_type = F_WRLCK;
  36. lock.l_whence = SEEK_SET;
  37. lock.l_start = 0;
  38. lock.l_len = 0;
  39. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  40. res = fcntl(lock_fd, F_SETLK, &lock);
  41. if (res != -1)
  42. break;
  43. if (EINTR == errno)
  44. continue;
  45. if ((EAGAIN == errno) || (EACCES == errno)) {
  46. if (0 == i)
  47. fprintf(stderr,
  48. "Warning: Try to get lock. Please wait...\n");
  49. sleep(1);
  50. continue;
  51. }
  52. break;
  53. }
  54. if (res == -1) {
  55. fprintf(stderr, "Error: Can't get lock.\n");
  56. close(lock_fd);
  57. return -1;
  58. }
  59. return lock_fd;
  60. }
  61. /*-------------------------------------------------------- */
  62. static void clish_shell_unlock(int lock_fd)
  63. {
  64. struct flock lock;
  65. if (lock_fd == -1)
  66. return;
  67. lock.l_type = F_UNLCK;
  68. lock.l_whence = SEEK_SET;
  69. lock.l_start = 0;
  70. lock.l_len = 0;
  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. clish_hook_action_fn_t *func = NULL;
  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. result = func(context, script, out);
  205. lub_string_free(script);
  206. return result;
  207. }
  208. /*----------------------------------------------------------- */
  209. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  210. {
  211. clish_sym_t *sym;
  212. clish_shell_t *shell = clish_context__get_shell(clish_context);
  213. void *func;
  214. if (!(sym = shell->hooks[type]))
  215. return NULL;
  216. if (shell->dryrun && !clish_sym__get_permanent(sym))
  217. return NULL;
  218. if (!(func = clish_sym__get_func(sym)))
  219. return NULL;
  220. return func;
  221. }
  222. /*----------------------------------------------------------- */
  223. CLISH_HOOK_ACCESS(clish_shell_exec_access)
  224. {
  225. clish_hook_access_fn_t *func = NULL;
  226. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_ACCESS);
  227. return func ? func(clish_context, access) : 0;
  228. }
  229. /*----------------------------------------------------------- */
  230. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  231. {
  232. clish_hook_config_fn_t *func = NULL;
  233. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  234. return func ? func(clish_context) : 0;
  235. }
  236. /*----------------------------------------------------------- */
  237. CLISH_HOOK_LOG(clish_shell_exec_log)
  238. {
  239. clish_hook_log_fn_t *func = NULL;
  240. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  241. return func ? func(clish_context, line, retcode) : 0;
  242. }
  243. /*----------------------------------------------------------- */
  244. const char *clish_shell__get_fifo(clish_shell_t * this)
  245. {
  246. char *name;
  247. int res;
  248. if (this->fifo_name) {
  249. if (0 == access(this->fifo_name, R_OK | W_OK))
  250. return this->fifo_name;
  251. unlink(this->fifo_name);
  252. lub_string_free(this->fifo_name);
  253. this->fifo_name = NULL;
  254. }
  255. do {
  256. char template[] = "/tmp/klish.fifo.XXXXXX";
  257. name = mktemp(template);
  258. if (name[0] == '\0')
  259. return NULL;
  260. res = mkfifo(name, 0600);
  261. if (res == 0)
  262. this->fifo_name = lub_string_dup(name);
  263. } while ((res < 0) && (EEXIST == errno));
  264. return this->fifo_name;
  265. }
  266. /*-------------------------------------------------------- */
  267. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  268. {
  269. assert(this);
  270. this->log = log;
  271. }
  272. /*-------------------------------------------------------- */
  273. bool_t clish_shell__get_log(const clish_shell_t *this)
  274. {
  275. assert(this);
  276. return this->log;
  277. }
  278. /*-------------------------------------------------------- */
  279. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  280. {
  281. this->dryrun = dryrun;
  282. }
  283. /*-------------------------------------------------------- */
  284. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  285. {
  286. return this->dryrun;
  287. }
  288. /*----------------------------------------------------------- */