helper.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sched.h>
  9. #include <linux/limits.h>
  10. #include <sys/socket.h>
  11. #include <sys/wait.h>
  12. #include "kern_constants.h"
  13. #include "kern_util.h"
  14. #include "os.h"
  15. #include "um_malloc.h"
  16. #include "user.h"
  17. struct helper_data {
  18. void (*pre_exec)(void*);
  19. void *pre_data;
  20. char **argv;
  21. int fd;
  22. char *buf;
  23. };
  24. static int helper_child(void *arg)
  25. {
  26. struct helper_data *data = arg;
  27. char **argv = data->argv;
  28. int err;
  29. if (data->pre_exec != NULL)
  30. (*data->pre_exec)(data->pre_data);
  31. err = execvp_noalloc(data->buf, argv[0], argv);
  32. /* If the exec succeeds, we don't get here */
  33. write(data->fd, &err, sizeof(err));
  34. return 0;
  35. }
  36. /* Returns either the pid of the child process we run or -E* on failure. */
  37. int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
  38. {
  39. struct helper_data data;
  40. unsigned long stack, sp;
  41. int pid, fds[2], ret, n;
  42. stack = alloc_stack(0, __cant_sleep());
  43. if (stack == 0)
  44. return -ENOMEM;
  45. ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
  46. if (ret < 0) {
  47. ret = -errno;
  48. printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n",
  49. errno);
  50. goto out_free;
  51. }
  52. ret = os_set_exec_close(fds[1]);
  53. if (ret < 0) {
  54. printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, "
  55. "ret = %d\n", -ret);
  56. goto out_close;
  57. }
  58. sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
  59. data.pre_exec = pre_exec;
  60. data.pre_data = pre_data;
  61. data.argv = argv;
  62. data.fd = fds[1];
  63. data.buf = __cant_sleep() ? uml_kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
  64. uml_kmalloc(PATH_MAX, UM_GFP_KERNEL);
  65. pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
  66. if (pid < 0) {
  67. ret = -errno;
  68. printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n",
  69. errno);
  70. goto out_free2;
  71. }
  72. close(fds[1]);
  73. fds[1] = -1;
  74. /*
  75. * Read the errno value from the child, if the exec failed, or get 0 if
  76. * the exec succeeded because the pipe fd was set as close-on-exec.
  77. */
  78. n = read(fds[0], &ret, sizeof(ret));
  79. if (n == 0) {
  80. ret = pid;
  81. } else {
  82. if (n < 0) {
  83. n = -errno;
  84. printk(UM_KERN_ERR "run_helper : read on pipe failed, "
  85. "ret = %d\n", -n);
  86. ret = n;
  87. }
  88. CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
  89. }
  90. out_free2:
  91. kfree(data.buf);
  92. out_close:
  93. if (fds[1] != -1)
  94. close(fds[1]);
  95. close(fds[0]);
  96. out_free:
  97. free_stack(stack, 0);
  98. return ret;
  99. }
  100. int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
  101. unsigned long *stack_out)
  102. {
  103. unsigned long stack, sp;
  104. int pid, status, err;
  105. stack = alloc_stack(0, __cant_sleep());
  106. if (stack == 0)
  107. return -ENOMEM;
  108. sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
  109. pid = clone(proc, (void *) sp, flags, arg);
  110. if (pid < 0) {
  111. err = -errno;
  112. printk(UM_KERN_ERR "run_helper_thread : clone failed, "
  113. "errno = %d\n", errno);
  114. return err;
  115. }
  116. if (stack_out == NULL) {
  117. CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
  118. if (pid < 0) {
  119. err = -errno;
  120. printk(UM_KERN_ERR "run_helper_thread - wait failed, "
  121. "errno = %d\n", errno);
  122. pid = err;
  123. }
  124. if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
  125. printk(UM_KERN_ERR "run_helper_thread - thread "
  126. "returned status 0x%x\n", status);
  127. free_stack(stack, 0);
  128. } else
  129. *stack_out = stack;
  130. return pid;
  131. }
  132. int helper_wait(int pid)
  133. {
  134. int ret, status;
  135. int wflags = __WCLONE;
  136. CATCH_EINTR(ret = waitpid(pid, &status, wflags));
  137. if (ret < 0) {
  138. printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
  139. "errno = %d\n", pid, errno);
  140. return -errno;
  141. } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  142. printk(UM_KERN_ERR "helper_wait : process %d exited with "
  143. "status 0x%x\n", pid, status);
  144. return -ECHILD;
  145. } else
  146. return 0;
  147. }