helper.c 3.8 KB

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