shell_execute.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <sys/wait.h>
  17. #include <signal.h>
  18. #include <fcntl.h>
  19. /*-------------------------------------------------------- */
  20. static int clish_shell_lock(const char *lock_path)
  21. {
  22. int i;
  23. int res = -1;
  24. int lock_fd = -1;
  25. struct flock lock;
  26. if (!lock_path)
  27. return -1;
  28. lock_fd = open(lock_path, O_WRONLY | O_CREAT, 00644);
  29. if (-1 == lock_fd) {
  30. fprintf(stderr, "Warning: Can't open lockfile %s.\n", lock_path);
  31. return -1;
  32. }
  33. #ifdef FD_CLOEXEC
  34. fcntl(lock_fd, F_SETFD, fcntl(lock_fd, F_GETFD) | FD_CLOEXEC);
  35. #endif
  36. memset(&lock, 0, sizeof(lock));
  37. lock.l_type = F_WRLCK;
  38. lock.l_whence = SEEK_SET;
  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. if (EINVAL == errno)
  53. fprintf(stderr, "Error: Locking isn't supported by OS, consider \"--lockless\".\n");
  54. break;
  55. }
  56. if (res == -1) {
  57. fprintf(stderr, "Error: Can't get lock.\n");
  58. close(lock_fd);
  59. return -1;
  60. }
  61. return lock_fd;
  62. }
  63. /*-------------------------------------------------------- */
  64. static void clish_shell_unlock(int lock_fd)
  65. {
  66. struct flock lock;
  67. if (lock_fd == -1)
  68. return;
  69. memset(&lock, 0, sizeof(lock));
  70. lock.l_type = F_UNLCK;
  71. lock.l_whence = SEEK_SET;
  72. fcntl(lock_fd, F_SETLK, &lock);
  73. close(lock_fd);
  74. }
  75. /*----------------------------------------------------------- */
  76. int clish_shell_execute(clish_context_t *context, char **out)
  77. {
  78. clish_shell_t *this = clish_context__get_shell(context);
  79. const clish_command_t *cmd = clish_context__get_cmd(context);
  80. int result = 0;
  81. char *lock_path = clish_shell__get_lockfile(this);
  82. int lock_fd = -1;
  83. sigset_t old_sigs;
  84. struct sigaction old_sigint, old_sigquit, old_sighup;
  85. clish_view_t *cur_view = clish_shell__get_view(this);
  86. unsigned int saved_wdog_timeout = this->wdog_timeout;
  87. assert(cmd);
  88. /* Pre-change view if the command is from another depth/view */
  89. {
  90. clish_view_restore_t restore = clish_command__get_restore(cmd);
  91. if ((CLISH_RESTORE_VIEW == restore) &&
  92. (clish_command__get_pview(cmd) != cur_view)) {
  93. clish_view_t *view = clish_command__get_pview(cmd);
  94. clish_shell__set_pwd(this, NULL, view, NULL, context);
  95. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  96. (clish_command__get_depth(cmd) < this->depth)) {
  97. this->depth = clish_command__get_depth(cmd);
  98. }
  99. }
  100. /* Lock the lockfile */
  101. if (lock_path && clish_command__get_lock(cmd)) {
  102. lock_fd = clish_shell_lock(lock_path);
  103. if (-1 == lock_fd) {
  104. result = -1;
  105. goto error; /* Can't set lock */
  106. }
  107. }
  108. /* Ignore and block SIGINT, SIGQUIT, SIGHUP */
  109. if (!clish_command__get_interrupt(cmd)) {
  110. struct sigaction sa;
  111. sigset_t sigs;
  112. sa.sa_flags = 0;
  113. sigemptyset(&sa.sa_mask);
  114. sa.sa_handler = SIG_IGN;
  115. sigaction(SIGINT, &sa, &old_sigint);
  116. sigaction(SIGQUIT, &sa, &old_sigquit);
  117. sigaction(SIGHUP, &sa, &old_sighup);
  118. sigemptyset(&sigs);
  119. sigaddset(&sigs, SIGINT);
  120. sigaddset(&sigs, SIGQUIT);
  121. sigaddset(&sigs, SIGHUP);
  122. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  123. }
  124. /* Execute ACTION */
  125. clish_context__set_action(context, clish_command__get_action(cmd));
  126. result = clish_shell_exec_action(context, out);
  127. /* Restore SIGINT, SIGQUIT, SIGHUP */
  128. if (!clish_command__get_interrupt(cmd)) {
  129. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  130. /* Is the signals delivery guaranteed here (before
  131. sigaction restore) for previously blocked and
  132. pending signals? The simple test is working well.
  133. I don't want to use sigtimedwait() function bacause
  134. it needs a realtime extensions. The sigpending() with
  135. the sleep() is not nice too. Report bug if clish will
  136. get the SIGINT after non-interruptable action.
  137. */
  138. sigaction(SIGINT, &old_sigint, NULL);
  139. sigaction(SIGQUIT, &old_sigquit, NULL);
  140. sigaction(SIGHUP, &old_sighup, NULL);
  141. }
  142. /* Call config callback */
  143. if (!result)
  144. clish_shell_exec_config(context);
  145. /* Call logging callback */
  146. if (clish_shell__get_log(this) &&
  147. clish_shell_check_hook(context, CLISH_SYM_TYPE_LOG)) {
  148. char *full_line = clish_shell__get_full_line(context);
  149. clish_shell_exec_log(context, full_line, result);
  150. lub_string_free(full_line);
  151. }
  152. /* Unlock the lockfile */
  153. if (lock_fd != -1)
  154. clish_shell_unlock(lock_fd);
  155. /* Move into the new view */
  156. if (!result) {
  157. char *viewname = clish_shell_expand(clish_command__get_viewname(cmd), SHELL_VAR_NONE, context);
  158. if (viewname) {
  159. /* Search for the view */
  160. clish_view_t *view = clish_shell_find_view(this, viewname);
  161. if (!view)
  162. fprintf(stderr, "System error: Can't "
  163. "change view to %s\n", viewname);
  164. lub_string_free(viewname);
  165. /* Save the PWD */
  166. if (view) {
  167. char *line = clish_shell__get_line(context);
  168. clish_shell__set_pwd(this, line, view,
  169. clish_command__get_viewid(cmd), context);
  170. lub_string_free(line);
  171. }
  172. }
  173. }
  174. /* Set appropriate timeout. Workaround: Don't turn on watchdog
  175. on the "set watchdog <timeout>" command itself. */
  176. if (this->wdog_timeout && saved_wdog_timeout) {
  177. tinyrl__set_timeout(this->tinyrl, this->wdog_timeout);
  178. this->wdog_active = BOOL_TRUE;
  179. fprintf(stderr, "Warning: The watchdog is active. Timeout is %u "
  180. "seconds.\nWarning: Press any key to stop watchdog.\n",
  181. this->wdog_timeout);
  182. } else
  183. tinyrl__set_timeout(this->tinyrl, this->idle_timeout);
  184. error:
  185. return result;
  186. }
  187. /*----------------------------------------------------------- */
  188. /* Execute oaction. It suppose the forked process to get
  189. * script's stdout. Then forked process write the output back
  190. * to klish.
  191. */
  192. static int clish_shell_exec_oaction(clish_hook_oaction_fn_t func,
  193. void *context, const char *script, char **out)
  194. {
  195. int result = -1;
  196. int real_stdout; /* Saved stdout handler */
  197. int pipe1[2], pipe2[2];
  198. pid_t cpid = -1;
  199. konf_buf_t *buf;
  200. if (pipe(pipe1))
  201. return -1;
  202. if (pipe(pipe2))
  203. goto stdout_error;
  204. /* Create process to read script's stdout */
  205. cpid = fork();
  206. if (cpid == -1) {
  207. fprintf(stderr, "Error: Can't fork the stdout-grabber process.\n"
  208. "Error: The ACTION will be not executed.\n");
  209. goto stdout_error;
  210. }
  211. /* Child: read action's stdout */
  212. if (cpid == 0) {
  213. char *str;
  214. int len;
  215. close(pipe1[1]);
  216. close(pipe2[0]);
  217. /* Read the result of script execution */
  218. buf = konf_buf_new(pipe1[0]);
  219. while (konf_buf_read(buf) > 0);
  220. close(pipe1[0]);
  221. len = konf_buf__get_len(buf);
  222. str = konf_buf__get_buf(buf);
  223. write(pipe2[1], str, len);
  224. close(pipe2[1]);
  225. konf_buf_delete(buf);
  226. _exit(0);
  227. }
  228. real_stdout = dup(STDOUT_FILENO);
  229. dup2(pipe1[1], STDOUT_FILENO);
  230. close(pipe1[0]);
  231. close(pipe1[1]);
  232. close(pipe2[1]);
  233. result = func(context, script);
  234. /* Restore real stdout */
  235. dup2(real_stdout, STDOUT_FILENO);
  236. close(real_stdout);
  237. /* Read the result of script execution */
  238. buf = konf_buf_new(pipe2[0]);
  239. while (konf_buf_read(buf) > 0);
  240. *out = konf_buf__dup_line(buf);
  241. konf_buf_delete(buf);
  242. close(pipe2[0]);
  243. /* Wait for the stdout-grabber process */
  244. waitpid(cpid, NULL, 0);
  245. return result;
  246. stdout_error:
  247. close(pipe1[0]);
  248. close(pipe1[1]);
  249. return -1;
  250. }
  251. /*----------------------------------------------------------- */
  252. int clish_shell_exec_action(clish_context_t *context, char **out)
  253. {
  254. int result = -1;
  255. clish_sym_t *sym;
  256. char *script;
  257. void *func = NULL; /* We don't know the func API at this time */
  258. const clish_action_t *action = clish_context__get_action(context);
  259. clish_shell_t *shell = clish_context__get_shell(context);
  260. if (!(sym = clish_action__get_builtin(action)))
  261. return 0;
  262. if (shell->dryrun && !clish_sym__get_permanent(sym))
  263. return 0;
  264. if (!(func = clish_sym__get_func(sym))) {
  265. fprintf(stderr, "Error: Default ACTION symbol is not specified.\n");
  266. return -1;
  267. }
  268. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  269. /* Find out the function API */
  270. /* CLISH_SYM_API_SIMPLE */
  271. if (clish_sym__get_api(sym) == CLISH_SYM_API_SIMPLE) {
  272. result = ((clish_hook_action_fn_t *)func)(context, script, out);
  273. /* CLISH_SYM_API_STDOUT and output is not needed */
  274. } else if ((clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) && (!out)) {
  275. result = ((clish_hook_oaction_fn_t *)func)(context, script);
  276. /* CLISH_SYM_API_STDOUT and outpus is needed */
  277. } else if (clish_sym__get_api(sym) == CLISH_SYM_API_STDOUT) {
  278. result = clish_shell_exec_oaction((clish_hook_oaction_fn_t *)func,
  279. context, script, out);
  280. }
  281. lub_string_free(script);
  282. return result;
  283. }
  284. /*----------------------------------------------------------- */
  285. void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
  286. {
  287. clish_sym_t *sym;
  288. clish_shell_t *shell = clish_context__get_shell(clish_context);
  289. void *func;
  290. if (!(sym = shell->hooks[type]))
  291. return NULL;
  292. if (shell->dryrun && !clish_sym__get_permanent(sym))
  293. return NULL;
  294. if (!(func = clish_sym__get_func(sym)))
  295. return NULL;
  296. return func;
  297. }
  298. /*----------------------------------------------------------- */
  299. CLISH_HOOK_CONFIG(clish_shell_exec_config)
  300. {
  301. clish_hook_config_fn_t *func = NULL;
  302. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_CONFIG);
  303. return func ? func(clish_context) : 0;
  304. }
  305. /*----------------------------------------------------------- */
  306. CLISH_HOOK_LOG(clish_shell_exec_log)
  307. {
  308. clish_hook_log_fn_t *func = NULL;
  309. func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_LOG);
  310. return func ? func(clish_context, line, retcode) : 0;
  311. }
  312. /*----------------------------------------------------------- */
  313. const char *clish_shell__get_fifo(clish_shell_t * this)
  314. {
  315. char *name;
  316. int res;
  317. if (this->fifo_name) {
  318. if (0 == access(this->fifo_name, R_OK | W_OK))
  319. return this->fifo_name;
  320. unlink(this->fifo_name);
  321. lub_string_free(this->fifo_name);
  322. this->fifo_name = NULL;
  323. }
  324. do {
  325. char template[] = "/tmp/klish.fifo.XXXXXX";
  326. name = mktemp(template);
  327. if (name[0] == '\0')
  328. return NULL;
  329. res = mkfifo(name, 0600);
  330. if (res == 0)
  331. this->fifo_name = lub_string_dup(name);
  332. } while ((res < 0) && (EEXIST == errno));
  333. return this->fifo_name;
  334. }
  335. /*-------------------------------------------------------- */
  336. void clish_shell__set_log(clish_shell_t *this, bool_t log)
  337. {
  338. assert(this);
  339. this->log = log;
  340. }
  341. /*-------------------------------------------------------- */
  342. bool_t clish_shell__get_log(const clish_shell_t *this)
  343. {
  344. assert(this);
  345. return this->log;
  346. }
  347. /*-------------------------------------------------------- */
  348. void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
  349. {
  350. this->dryrun = dryrun;
  351. }
  352. /*-------------------------------------------------------- */
  353. bool_t clish_shell__get_dryrun(const clish_shell_t *this)
  354. {
  355. return this->dryrun;
  356. }
  357. /*----------------------------------------------------------- */