helper.c 3.9 KB

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