helper.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <sched.h>
  10. #include <sys/signal.h>
  11. #include <sys/wait.h>
  12. #include "user.h"
  13. #include "kern_util.h"
  14. #include "user_util.h"
  15. #include "helper.h"
  16. #include "os.h"
  17. struct helper_data {
  18. void (*pre_exec)(void*);
  19. void *pre_data;
  20. char **argv;
  21. int fd;
  22. };
  23. /* Debugging aid, changed only from gdb */
  24. int helper_pause = 0;
  25. static void helper_hup(int sig)
  26. {
  27. }
  28. static int helper_child(void *arg)
  29. {
  30. struct helper_data *data = arg;
  31. char **argv = data->argv;
  32. int errval;
  33. if(helper_pause){
  34. signal(SIGHUP, helper_hup);
  35. pause();
  36. }
  37. if(data->pre_exec != NULL)
  38. (*data->pre_exec)(data->pre_data);
  39. execvp(argv[0], argv);
  40. errval = errno;
  41. printk("execvp of '%s' failed - errno = %d\n", argv[0], errno);
  42. os_write_file(data->fd, &errval, sizeof(errval));
  43. os_kill_process(os_getpid(), 0);
  44. return(0);
  45. }
  46. /* Returns either the pid of the child process we run or -E* on failure.
  47. * XXX The alloc_stack here breaks if this is called in the tracing thread */
  48. int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
  49. unsigned long *stack_out)
  50. {
  51. struct helper_data data;
  52. unsigned long stack, sp;
  53. int pid, fds[2], ret, n;
  54. if((stack_out != NULL) && (*stack_out != 0))
  55. stack = *stack_out;
  56. else stack = alloc_stack(0, um_in_interrupt());
  57. if(stack == 0)
  58. return(-ENOMEM);
  59. ret = os_pipe(fds, 1, 0);
  60. if(ret < 0){
  61. printk("run_helper : pipe failed, ret = %d\n", -ret);
  62. goto out_free;
  63. }
  64. ret = os_set_exec_close(fds[1], 1);
  65. if(ret < 0){
  66. printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
  67. -ret);
  68. goto out_close;
  69. }
  70. sp = stack + page_size() - sizeof(void *);
  71. data.pre_exec = pre_exec;
  72. data.pre_data = pre_data;
  73. data.argv = argv;
  74. data.fd = fds[1];
  75. pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
  76. if(pid < 0){
  77. printk("run_helper : clone failed, errno = %d\n", errno);
  78. ret = -errno;
  79. goto out_close;
  80. }
  81. os_close_file(fds[1]);
  82. fds[1] = -1;
  83. /*Read the errno value from the child.*/
  84. n = os_read_file(fds[0], &ret, sizeof(ret));
  85. if(n < 0){
  86. printk("run_helper : read on pipe failed, ret = %d\n", -n);
  87. ret = n;
  88. os_kill_process(pid, 1);
  89. }
  90. else if(n != 0){
  91. CATCH_EINTR(n = waitpid(pid, NULL, 0));
  92. ret = -errno;
  93. } else {
  94. ret = pid;
  95. }
  96. out_close:
  97. if (fds[1] != -1)
  98. os_close_file(fds[1]);
  99. os_close_file(fds[0]);
  100. out_free:
  101. if(stack_out == NULL)
  102. free_stack(stack, 0);
  103. else *stack_out = stack;
  104. return(ret);
  105. }
  106. int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
  107. unsigned long *stack_out, int stack_order)
  108. {
  109. unsigned long stack, sp;
  110. int pid, status;
  111. stack = alloc_stack(stack_order, um_in_interrupt());
  112. if(stack == 0) return(-ENOMEM);
  113. sp = stack + (page_size() << stack_order) - sizeof(void *);
  114. pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
  115. if(pid < 0){
  116. printk("run_helper_thread : clone failed, errno = %d\n",
  117. errno);
  118. return(-errno);
  119. }
  120. if(stack_out == NULL){
  121. CATCH_EINTR(pid = waitpid(pid, &status, 0));
  122. if(pid < 0){
  123. printk("run_helper_thread - wait failed, errno = %d\n",
  124. errno);
  125. pid = -errno;
  126. }
  127. if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
  128. printk("run_helper_thread - thread returned status "
  129. "0x%x\n", status);
  130. free_stack(stack, stack_order);
  131. }
  132. else *stack_out = stack;
  133. return(pid);
  134. }
  135. int helper_wait(int pid)
  136. {
  137. int ret;
  138. CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
  139. if(ret < 0){
  140. printk("helper_wait : waitpid failed, errno = %d\n", errno);
  141. return(-errno);
  142. }
  143. return(ret);
  144. }