tt.c 4.1 KB

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