helper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <sched.h>
  10. #include <sys/signal.h>
  11. #include <sys/wait.h>
  12. #include "user.h"
  13. #include "kern_util.h"
  14. #include "user_util.h"
  15. #include "os.h"
  16. struct helper_data {
  17. void (*pre_exec)(void*);
  18. void *pre_data;
  19. char **argv;
  20. int fd;
  21. };
  22. /* Debugging aid, changed only from gdb */
  23. int helper_pause = 0;
  24. static void helper_hup(int sig)
  25. {
  26. }
  27. static int helper_child(void *arg)
  28. {
  29. struct helper_data *data = arg;
  30. char **argv = data->argv;
  31. int errval;
  32. if (helper_pause){
  33. signal(SIGHUP, helper_hup);
  34. pause();
  35. }
  36. if (data->pre_exec != NULL)
  37. (*data->pre_exec)(data->pre_data);
  38. execvp(argv[0], argv);
  39. errval = -errno;
  40. printk("helper_child - execve of '%s' failed - errno = %d\n", argv[0], errno);
  41. os_write_file(data->fd, &errval, sizeof(errval));
  42. kill(os_getpid(), SIGKILL);
  43. return 0;
  44. }
  45. /* Returns either the pid of the child process we run or -E* on failure.
  46. * XXX The alloc_stack here breaks if this is called in the tracing thread, so
  47. * we need to receive a preallocated stack (a local buffer is ok). */
  48. int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
  49. unsigned long *stack_out)
  50. {
  51. struct helper_data data;
  52. unsigned long stack, sp;
  53. int pid, fds[2], ret, n;
  54. if ((stack_out != NULL) && (*stack_out != 0))
  55. stack = *stack_out;
  56. else
  57. stack = alloc_stack(0, __cant_sleep());
  58. if (stack == 0)
  59. return -ENOMEM;
  60. ret = os_pipe(fds, 1, 0);
  61. if (ret < 0) {
  62. printk("run_helper : pipe failed, ret = %d\n", -ret);
  63. goto out_free;
  64. }
  65. ret = os_set_exec_close(fds[1], 1);
  66. if (ret < 0) {
  67. printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
  68. -ret);
  69. goto out_close;
  70. }
  71. sp = stack + page_size() - sizeof(void *);
  72. data.pre_exec = pre_exec;
  73. data.pre_data = pre_data;
  74. data.argv = argv;
  75. data.fd = fds[1];
  76. pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
  77. if (pid < 0) {
  78. ret = -errno;
  79. printk("run_helper : clone failed, errno = %d\n", errno);
  80. goto out_close;
  81. }
  82. close(fds[1]);
  83. fds[1] = -1;
  84. /* Read the errno value from the child, if the exec failed, or get 0 if
  85. * the exec succeeded because the pipe fd was set as close-on-exec. */
  86. n = os_read_file(fds[0], &ret, sizeof(ret));
  87. if (n == 0) {
  88. ret = pid;
  89. } else {
  90. if (n < 0) {
  91. printk("run_helper : read on pipe failed, ret = %d\n",
  92. -n);
  93. ret = n;
  94. kill(pid, SIGKILL);
  95. }
  96. CATCH_EINTR(waitpid(pid, NULL, 0));
  97. }
  98. out_close:
  99. if (fds[1] != -1)
  100. close(fds[1]);
  101. close(fds[0]);
  102. out_free:
  103. if ((stack_out == NULL) || (*stack_out == 0))
  104. free_stack(stack, 0);
  105. return ret;
  106. }
  107. int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
  108. unsigned long *stack_out, int stack_order)
  109. {
  110. unsigned long stack, sp;
  111. int pid, status, err;
  112. stack = alloc_stack(stack_order, __cant_sleep());
  113. if (stack == 0)
  114. return -ENOMEM;
  115. sp = stack + (page_size() << stack_order) - sizeof(void *);
  116. pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
  117. if (pid < 0) {
  118. err = -errno;
  119. printk("run_helper_thread : clone failed, errno = %d\n",
  120. errno);
  121. return err;
  122. }
  123. if (stack_out == NULL) {
  124. CATCH_EINTR(pid = waitpid(pid, &status, 0));
  125. if (pid < 0) {
  126. err = -errno;
  127. printk("run_helper_thread - wait failed, errno = %d\n",
  128. errno);
  129. pid = err;
  130. }
  131. if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
  132. printk("run_helper_thread - thread returned status "
  133. "0x%x\n", status);
  134. free_stack(stack, stack_order);
  135. } else
  136. *stack_out = stack;
  137. return pid;
  138. }
  139. int helper_wait(int pid)
  140. {
  141. int ret;
  142. CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
  143. if (ret < 0) {
  144. ret = -errno;
  145. printk("helper_wait : waitpid failed, errno = %d\n", errno);
  146. }
  147. return ret;
  148. }