helper.c 3.7 KB

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