Gennady Sharapov | 60d339f | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | #include <signal.h> |
| 9 | #include <sched.h> |
| 10 | #include <errno.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <setjmp.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <sys/ptrace.h> |
| 16 | #include <linux/ptrace.h> |
| 17 | #include <sys/wait.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <asm/ptrace.h> |
| 20 | #include <asm/unistd.h> |
| 21 | #include <asm/page.h> |
| 22 | #include "user_util.h" |
| 23 | #include "kern_util.h" |
| 24 | #include "user.h" |
| 25 | #include "signal_kern.h" |
Gennady Sharapov | 60d339f | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 26 | #include "sysdep/ptrace.h" |
| 27 | #include "sysdep/sigcontext.h" |
| 28 | #include "irq_user.h" |
| 29 | #include "ptrace_user.h" |
| 30 | #include "time_user.h" |
| 31 | #include "init.h" |
| 32 | #include "os.h" |
| 33 | #include "uml-config.h" |
| 34 | #include "choose-mode.h" |
| 35 | #include "mode.h" |
| 36 | #include "tempfile.h" |
| 37 | |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 38 | int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x, |
| 39 | int must_succeed) |
| 40 | { |
| 41 | int err; |
| 42 | |
| 43 | err = os_protect_memory((void *) addr, len, r, w, x); |
| 44 | if(err < 0){ |
| 45 | if(must_succeed) |
| 46 | panic("protect failed, err = %d", -err); |
| 47 | else return(err); |
| 48 | } |
| 49 | return(0); |
| 50 | } |
| 51 | |
Gennady Sharapov | ea2ba7d | 2006-01-08 01:01:31 -0800 | [diff] [blame] | 52 | void kill_child_dead(int pid) |
| 53 | { |
| 54 | kill(pid, SIGKILL); |
| 55 | kill(pid, SIGCONT); |
| 56 | do { |
| 57 | int n; |
| 58 | CATCH_EINTR(n = waitpid(pid, NULL, 0)); |
| 59 | if (n > 0) |
| 60 | kill(pid, SIGCONT); |
| 61 | else |
| 62 | break; |
| 63 | } while(1); |
| 64 | } |
| 65 | |
Gennady Sharapov | 60d339f | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 66 | /* |
| 67 | *------------------------- |
| 68 | * only for tt mode (will be deleted in future...) |
| 69 | *------------------------- |
| 70 | */ |
| 71 | |
| 72 | struct tramp { |
| 73 | int (*tramp)(void *); |
| 74 | void *tramp_data; |
| 75 | unsigned long temp_stack; |
| 76 | int flags; |
| 77 | int pid; |
| 78 | }; |
| 79 | |
| 80 | /* See above for why sigkill is here */ |
| 81 | |
| 82 | int sigkill = SIGKILL; |
| 83 | |
| 84 | int outer_tramp(void *arg) |
| 85 | { |
| 86 | struct tramp *t; |
| 87 | int sig = sigkill; |
| 88 | |
| 89 | t = arg; |
| 90 | t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2, |
| 91 | t->flags, t->tramp_data); |
| 92 | if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL); |
| 93 | kill(os_getpid(), sig); |
| 94 | _exit(0); |
| 95 | } |
| 96 | |
| 97 | int start_fork_tramp(void *thread_arg, unsigned long temp_stack, |
| 98 | int clone_flags, int (*tramp)(void *)) |
| 99 | { |
| 100 | struct tramp arg; |
| 101 | unsigned long sp; |
| 102 | int new_pid, status, err; |
| 103 | |
| 104 | /* The trampoline will run on the temporary stack */ |
| 105 | sp = stack_sp(temp_stack); |
| 106 | |
| 107 | clone_flags |= CLONE_FILES | SIGCHLD; |
| 108 | |
| 109 | arg.tramp = tramp; |
| 110 | arg.tramp_data = thread_arg; |
| 111 | arg.temp_stack = temp_stack; |
| 112 | arg.flags = clone_flags; |
| 113 | |
| 114 | /* Start the process and wait for it to kill itself */ |
| 115 | new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg); |
| 116 | if(new_pid < 0) |
| 117 | return(new_pid); |
| 118 | |
| 119 | CATCH_EINTR(err = waitpid(new_pid, &status, 0)); |
| 120 | if(err < 0) |
| 121 | panic("Waiting for outer trampoline failed - errno = %d", |
| 122 | errno); |
| 123 | |
| 124 | if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL)) |
| 125 | panic("outer trampoline didn't exit with SIGKILL, " |
| 126 | "status = %d", status); |
| 127 | |
| 128 | return(arg.pid); |
| 129 | } |
| 130 | |
| 131 | void forward_pending_sigio(int target) |
| 132 | { |
| 133 | sigset_t sigs; |
| 134 | |
| 135 | if(sigpending(&sigs)) |
| 136 | panic("forward_pending_sigio : sigpending failed"); |
| 137 | if(sigismember(&sigs, SIGIO)) |
| 138 | kill(target, SIGIO); |
| 139 | } |
| 140 | |