helper.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <sched.h>
  10. #include <linux/limits.h>
  11. #include <sys/socket.h>
  12. #include <sys/wait.h>
  13. #include <kern_util.h>
  14. #include <os.h>
  15. #include <um_malloc.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, ret;
  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. CATCH_EINTR(ret = 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;
  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() ? uml_kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
  63. uml_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, __WALL));
  88. }
  89. if (ret < 0)
  90. printk(UM_KERN_ERR "run_helper : failed to exec %s on host: %s\n",
  91. argv[0], strerror(-ret));
  92. out_free2:
  93. kfree(data.buf);
  94. out_close:
  95. if (fds[1] != -1)
  96. close(fds[1]);
  97. close(fds[0]);
  98. out_free:
  99. free_stack(stack, 0);
  100. return ret;
  101. }
  102. int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
  103. unsigned long *stack_out)
  104. {
  105. unsigned long stack, sp;
  106. int pid, status, err;
  107. stack = alloc_stack(0, __cant_sleep());
  108. if (stack == 0)
  109. return -ENOMEM;
  110. sp = stack + UM_KERN_PAGE_SIZE;
  111. pid = clone(proc, (void *) sp, flags, arg);
  112. if (pid < 0) {
  113. err = -errno;
  114. printk(UM_KERN_ERR "run_helper_thread : clone failed, "
  115. "errno = %d\n", errno);
  116. return err;
  117. }
  118. if (stack_out == NULL) {
  119. CATCH_EINTR(pid = waitpid(pid, &status, __WALL));
  120. if (pid < 0) {
  121. err = -errno;
  122. printk(UM_KERN_ERR "run_helper_thread - wait failed, "
  123. "errno = %d\n", errno);
  124. pid = err;
  125. }
  126. if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
  127. printk(UM_KERN_ERR "run_helper_thread - thread "
  128. "returned status 0x%x\n", status);
  129. free_stack(stack, 0);
  130. } else
  131. *stack_out = stack;
  132. return pid;
  133. }
  134. int helper_wait(int pid)
  135. {
  136. int ret, status;
  137. int wflags = __WALL;
  138. CATCH_EINTR(ret = waitpid(pid, &status, wflags));
  139. if (ret < 0) {
  140. printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
  141. "errno = %d\n", pid, errno);
  142. return -errno;
  143. } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  144. printk(UM_KERN_ERR "helper_wait : process %d exited with "
  145. "status 0x%x\n", pid, status);
  146. return -ECHILD;
  147. } else
  148. return 0;
  149. }