helper.c 3.8 KB

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