tt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "time_user.h"
  30. #include "init.h"
  31. #include "os.h"
  32. #include "uml-config.h"
  33. #include "choose-mode.h"
  34. #include "mode.h"
  35. #include "tempfile.h"
  36. int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
  37. int must_succeed)
  38. {
  39. int err;
  40. err = os_protect_memory((void *) addr, len, r, w, x);
  41. if(err < 0){
  42. if(must_succeed)
  43. panic("protect failed, err = %d", -err);
  44. else return(err);
  45. }
  46. return(0);
  47. }
  48. void kill_child_dead(int pid)
  49. {
  50. kill(pid, SIGKILL);
  51. kill(pid, SIGCONT);
  52. do {
  53. int n;
  54. CATCH_EINTR(n = waitpid(pid, NULL, 0));
  55. if (n > 0)
  56. kill(pid, SIGCONT);
  57. else
  58. break;
  59. } while(1);
  60. }
  61. /*
  62. *-------------------------
  63. * only for tt mode (will be deleted in future...)
  64. *-------------------------
  65. */
  66. struct tramp {
  67. int (*tramp)(void *);
  68. void *tramp_data;
  69. unsigned long temp_stack;
  70. int flags;
  71. int pid;
  72. };
  73. /* See above for why sigkill is here */
  74. int sigkill = SIGKILL;
  75. int outer_tramp(void *arg)
  76. {
  77. struct tramp *t;
  78. int sig = sigkill;
  79. t = arg;
  80. t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
  81. t->flags, t->tramp_data);
  82. if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
  83. kill(os_getpid(), sig);
  84. _exit(0);
  85. }
  86. int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
  87. int clone_flags, int (*tramp)(void *))
  88. {
  89. struct tramp arg;
  90. unsigned long sp;
  91. int new_pid, status, err;
  92. /* The trampoline will run on the temporary stack */
  93. sp = stack_sp(temp_stack);
  94. clone_flags |= CLONE_FILES | SIGCHLD;
  95. arg.tramp = tramp;
  96. arg.tramp_data = thread_arg;
  97. arg.temp_stack = temp_stack;
  98. arg.flags = clone_flags;
  99. /* Start the process and wait for it to kill itself */
  100. new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
  101. if(new_pid < 0)
  102. return(new_pid);
  103. CATCH_EINTR(err = waitpid(new_pid, &status, 0));
  104. if(err < 0)
  105. panic("Waiting for outer trampoline failed - errno = %d",
  106. errno);
  107. if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
  108. panic("outer trampoline didn't exit with SIGKILL, "
  109. "status = %d", status);
  110. return(arg.pid);
  111. }
  112. void forward_pending_sigio(int target)
  113. {
  114. sigset_t sigs;
  115. if(sigpending(&sigs))
  116. panic("forward_pending_sigio : sigpending failed");
  117. if(sigismember(&sigs, SIGIO))
  118. kill(target, SIGIO);
  119. }