helper.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "os.h"
  16. struct helper_data {
  17. void (*pre_exec)(void*);
  18. void *pre_data;
  19. char **argv;
  20. int fd;
  21. };
  22. /* Debugging aid, changed only from gdb */
  23. int helper_pause = 0;
  24. static void helper_hup(int sig)
  25. {
  26. }
  27. static int helper_child(void *arg)
  28. {
  29. struct helper_data *data = arg;
  30. char **argv = data->argv;
  31. int errval;
  32. if(helper_pause){
  33. signal(SIGHUP, helper_hup);
  34. pause();
  35. }
  36. if(data->pre_exec != NULL)
  37. (*data->pre_exec)(data->pre_data);
  38. execvp(argv[0], argv);
  39. errval = errno;
  40. printk("execvp of '%s' failed - errno = %d\n", argv[0], errno);
  41. os_write_file(data->fd, &errval, sizeof(errval));
  42. os_kill_process(os_getpid(), 0);
  43. return(0);
  44. }
  45. /* Returns either the pid of the child process we run or -E* on failure.
  46. * XXX The alloc_stack here breaks if this is called in the tracing thread */
  47. int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
  48. unsigned long *stack_out)
  49. {
  50. struct helper_data data;
  51. unsigned long stack, sp;
  52. int pid, fds[2], ret, n;
  53. if((stack_out != NULL) && (*stack_out != 0))
  54. stack = *stack_out;
  55. else stack = alloc_stack(0, um_in_interrupt());
  56. if(stack == 0)
  57. return(-ENOMEM);
  58. ret = os_pipe(fds, 1, 0);
  59. if(ret < 0){
  60. printk("run_helper : pipe failed, ret = %d\n", -ret);
  61. goto out_free;
  62. }
  63. ret = os_set_exec_close(fds[1], 1);
  64. if(ret < 0){
  65. printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
  66. -ret);
  67. goto out_close;
  68. }
  69. sp = stack + page_size() - sizeof(void *);
  70. data.pre_exec = pre_exec;
  71. data.pre_data = pre_data;
  72. data.argv = argv;
  73. data.fd = fds[1];
  74. pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
  75. if(pid < 0){
  76. printk("run_helper : clone failed, errno = %d\n", errno);
  77. ret = -errno;
  78. goto out_close;
  79. }
  80. os_close_file(fds[1]);
  81. fds[1] = -1;
  82. /*Read the errno value from the child.*/
  83. n = os_read_file(fds[0], &ret, sizeof(ret));
  84. if(n < 0){
  85. printk("run_helper : read on pipe failed, ret = %d\n", -n);
  86. ret = n;
  87. os_kill_process(pid, 1);
  88. }
  89. else if(n != 0){
  90. CATCH_EINTR(n = waitpid(pid, NULL, 0));
  91. ret = -errno;
  92. } else {
  93. ret = pid;
  94. }
  95. out_close:
  96. if (fds[1] != -1)
  97. os_close_file(fds[1]);
  98. os_close_file(fds[0]);
  99. out_free:
  100. if(stack_out == NULL)
  101. free_stack(stack, 0);
  102. else *stack_out = stack;
  103. return(ret);
  104. }
  105. int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
  106. unsigned long *stack_out, int stack_order)
  107. {
  108. unsigned long stack, sp;
  109. int pid, status;
  110. stack = alloc_stack(stack_order, um_in_interrupt());
  111. if(stack == 0) return(-ENOMEM);
  112. sp = stack + (page_size() << stack_order) - sizeof(void *);
  113. pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
  114. if(pid < 0){
  115. printk("run_helper_thread : clone failed, errno = %d\n",
  116. errno);
  117. return(-errno);
  118. }
  119. if(stack_out == NULL){
  120. CATCH_EINTR(pid = waitpid(pid, &status, 0));
  121. if(pid < 0){
  122. printk("run_helper_thread - wait failed, errno = %d\n",
  123. errno);
  124. pid = -errno;
  125. }
  126. if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
  127. printk("run_helper_thread - thread returned status "
  128. "0x%x\n", status);
  129. free_stack(stack, stack_order);
  130. }
  131. else *stack_out = stack;
  132. return(pid);
  133. }
  134. int helper_wait(int pid, int block)
  135. {
  136. int ret;
  137. CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
  138. if(ret < 0){
  139. printk("helper_wait : waitpid failed, errno = %d\n", errno);
  140. return(-errno);
  141. }
  142. return(ret);
  143. }
  144. /*
  145. * Overrides for Emacs so that we follow Linus's tabbing style.
  146. * Emacs will notice this stuff at the end of the file and automatically
  147. * adjust the settings for this buffer only. This must remain at the end
  148. * of the file.
  149. * ---------------------------------------------------------------------------
  150. * Local variables:
  151. * c-file-style: "linux"
  152. * End:
  153. */