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