helper.c 3.7 KB

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