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