tt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <sched.h>
  9. #include <errno.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <sys/time.h>
  13. #include <sys/ptrace.h>
  14. #include <linux/ptrace.h>
  15. #include <sys/wait.h>
  16. #include <sys/mman.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/unistd.h>
  19. #include <asm/page.h>
  20. #include "user_util.h"
  21. #include "kern_util.h"
  22. #include "user.h"
  23. #include "signal_kern.h"
  24. #include "sysdep/ptrace.h"
  25. #include "sysdep/sigcontext.h"
  26. #include "irq_user.h"
  27. #include "ptrace_user.h"
  28. #include "init.h"
  29. #include "os.h"
  30. #include "uml-config.h"
  31. #include "choose-mode.h"
  32. #include "mode.h"
  33. #include "tempfile.h"
  34. int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
  35. int must_succeed)
  36. {
  37. int err;
  38. err = os_protect_memory((void *) addr, len, r, w, x);
  39. if(err < 0){
  40. if(must_succeed)
  41. panic("protect failed, err = %d", -err);
  42. else return(err);
  43. }
  44. return(0);
  45. }
  46. void kill_child_dead(int pid)
  47. {
  48. kill(pid, SIGKILL);
  49. kill(pid, SIGCONT);
  50. do {
  51. int n;
  52. CATCH_EINTR(n = waitpid(pid, NULL, 0));
  53. if (n > 0)
  54. kill(pid, SIGCONT);
  55. else
  56. break;
  57. } while(1);
  58. }
  59. void stop(void)
  60. {
  61. while(1) sleep(1000000);
  62. }
  63. int wait_for_stop(int pid, int sig, int cont_type, void *relay)
  64. {
  65. sigset_t *relay_signals = relay;
  66. int status, ret;
  67. while(1){
  68. CATCH_EINTR(ret = waitpid(pid, &status, WUNTRACED));
  69. if((ret < 0) ||
  70. !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
  71. if(ret < 0){
  72. printk("wait failed, errno = %d\n",
  73. errno);
  74. }
  75. else if(WIFEXITED(status))
  76. printk("process %d exited with status %d\n",
  77. pid, WEXITSTATUS(status));
  78. else if(WIFSIGNALED(status))
  79. printk("process %d exited with signal %d\n",
  80. pid, WTERMSIG(status));
  81. else if((WSTOPSIG(status) == SIGVTALRM) ||
  82. (WSTOPSIG(status) == SIGALRM) ||
  83. (WSTOPSIG(status) == SIGIO) ||
  84. (WSTOPSIG(status) == SIGPROF) ||
  85. (WSTOPSIG(status) == SIGCHLD) ||
  86. (WSTOPSIG(status) == SIGWINCH) ||
  87. (WSTOPSIG(status) == SIGINT)){
  88. ptrace(cont_type, pid, 0, WSTOPSIG(status));
  89. continue;
  90. }
  91. else if((relay_signals != NULL) &&
  92. sigismember(relay_signals, WSTOPSIG(status))){
  93. ptrace(cont_type, pid, 0, WSTOPSIG(status));
  94. continue;
  95. }
  96. else printk("process %d stopped with signal %d\n",
  97. pid, WSTOPSIG(status));
  98. panic("wait_for_stop failed to wait for %d to stop "
  99. "with %d\n", pid, sig);
  100. }
  101. return(status);
  102. }
  103. }
  104. void forward_ipi(int fd, int pid)
  105. {
  106. int err;
  107. err = os_set_owner(fd, pid);
  108. if(err < 0)
  109. printk("forward_ipi: set_owner failed, fd = %d, me = %d, "
  110. "target = %d, err = %d\n", fd, os_getpid(), pid, -err);
  111. }
  112. /*
  113. *-------------------------
  114. * only for tt mode (will be deleted in future...)
  115. *-------------------------
  116. */
  117. struct tramp {
  118. int (*tramp)(void *);
  119. void *tramp_data;
  120. unsigned long temp_stack;
  121. int flags;
  122. int pid;
  123. };
  124. /* See above for why sigkill is here */
  125. int sigkill = SIGKILL;
  126. int outer_tramp(void *arg)
  127. {
  128. struct tramp *t;
  129. int sig = sigkill;
  130. t = arg;
  131. t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
  132. t->flags, t->tramp_data);
  133. if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
  134. kill(os_getpid(), sig);
  135. _exit(0);
  136. }
  137. int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
  138. int clone_flags, int (*tramp)(void *))
  139. {
  140. struct tramp arg;
  141. unsigned long sp;
  142. int new_pid, status, err;
  143. /* The trampoline will run on the temporary stack */
  144. sp = stack_sp(temp_stack);
  145. clone_flags |= CLONE_FILES | SIGCHLD;
  146. arg.tramp = tramp;
  147. arg.tramp_data = thread_arg;
  148. arg.temp_stack = temp_stack;
  149. arg.flags = clone_flags;
  150. /* Start the process and wait for it to kill itself */
  151. new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
  152. if(new_pid < 0)
  153. return(new_pid);
  154. CATCH_EINTR(err = waitpid(new_pid, &status, 0));
  155. if(err < 0)
  156. panic("Waiting for outer trampoline failed - errno = %d",
  157. errno);
  158. if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
  159. panic("outer trampoline didn't exit with SIGKILL, "
  160. "status = %d", status);
  161. return(arg.pid);
  162. }
  163. void forward_pending_sigio(int target)
  164. {
  165. sigset_t sigs;
  166. if(sigpending(&sigs))
  167. panic("forward_pending_sigio : sigpending failed");
  168. if(sigismember(&sigs, SIGIO))
  169. kill(target, SIGIO);
  170. }