helper.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. {
  44. struct helper_data data;
  45. unsigned long stack, sp;
  46. int pid, fds[2], ret, n;
  47. stack = alloc_stack(0, __cant_sleep());
  48. if (stack == 0)
  49. return -ENOMEM;
  50. ret = os_pipe(fds, 1, 0);
  51. if (ret < 0) {
  52. printk("run_helper : pipe failed, ret = %d\n", -ret);
  53. goto out_free;
  54. }
  55. ret = os_set_exec_close(fds[1], 1);
  56. if (ret < 0) {
  57. printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
  58. -ret);
  59. goto out_close;
  60. }
  61. sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
  62. data.pre_exec = pre_exec;
  63. data.pre_data = pre_data;
  64. data.argv = argv;
  65. data.fd = fds[1];
  66. data.buf = __cant_sleep() ? kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
  67. kmalloc(PATH_MAX, UM_GFP_KERNEL);
  68. pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
  69. if (pid < 0) {
  70. ret = -errno;
  71. printk("run_helper : clone failed, errno = %d\n", errno);
  72. goto out_free2;
  73. }
  74. close(fds[1]);
  75. fds[1] = -1;
  76. /*
  77. * Read the errno value from the child, if the exec failed, or get 0 if
  78. * the exec succeeded because the pipe fd was set as close-on-exec.
  79. */
  80. n = read(fds[0], &ret, sizeof(ret));
  81. if (n == 0) {
  82. ret = pid;
  83. } else {
  84. if (n < 0) {
  85. n = -errno;
  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. free_stack(stack, 0);
  101. return ret;
  102. }
  103. int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
  104. unsigned long *stack_out)
  105. {
  106. unsigned long stack, sp;
  107. int pid, status, err;
  108. stack = alloc_stack(0, __cant_sleep());
  109. if (stack == 0)
  110. return -ENOMEM;
  111. sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
  112. pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
  113. if (pid < 0) {
  114. err = -errno;
  115. printk("run_helper_thread : clone failed, errno = %d\n",
  116. errno);
  117. return err;
  118. }
  119. if (stack_out == NULL) {
  120. CATCH_EINTR(pid = waitpid(pid, &status, 0));
  121. if (pid < 0) {
  122. err = -errno;
  123. printk("run_helper_thread - wait failed, errno = %d\n",
  124. errno);
  125. pid = err;
  126. }
  127. if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
  128. printk("run_helper_thread - thread returned status "
  129. "0x%x\n", status);
  130. free_stack(stack, 0);
  131. } else
  132. *stack_out = stack;
  133. return pid;
  134. }
  135. int helper_wait(int pid)
  136. {
  137. int ret;
  138. CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
  139. if (ret < 0) {
  140. ret = -errno;
  141. printk("helper_wait : waitpid failed, errno = %d\n", errno);
  142. }
  143. return ret;
  144. }