Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -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 <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | #include <limits.h> |
| 10 | #include <setjmp.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/utsname.h> |
| 14 | #include <sys/param.h> |
| 15 | #include <sys/time.h> |
| 16 | #include "asm/types.h" |
| 17 | #include <ctype.h> |
| 18 | #include <signal.h> |
| 19 | #include <wait.h> |
| 20 | #include <errno.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <sched.h> |
| 23 | #include <termios.h> |
| 24 | #include <string.h> |
| 25 | #include "user_util.h" |
| 26 | #include "kern_util.h" |
| 27 | #include "user.h" |
| 28 | #include "mem_user.h" |
| 29 | #include "init.h" |
| 30 | #include "helper.h" |
| 31 | #include "ptrace_user.h" |
| 32 | #include "uml-config.h" |
| 33 | |
| 34 | void stop(void) |
| 35 | { |
| 36 | while(1) sleep(1000000); |
| 37 | } |
| 38 | |
| 39 | void stack_protections(unsigned long address) |
| 40 | { |
| 41 | int prot = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 42 | |
| 43 | if(mprotect((void *) address, page_size(), prot) < 0) |
| 44 | panic("protecting stack failed, errno = %d", errno); |
| 45 | } |
| 46 | |
| 47 | void task_protections(unsigned long address) |
| 48 | { |
| 49 | unsigned long guard = address + page_size(); |
| 50 | unsigned long stack = guard + page_size(); |
| 51 | int prot = 0, pages; |
| 52 | |
| 53 | #ifdef notdef |
| 54 | if(mprotect((void *) stack, page_size(), prot) < 0) |
| 55 | panic("protecting guard page failed, errno = %d", errno); |
| 56 | #endif |
| 57 | pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2; |
| 58 | prot = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 59 | if(mprotect((void *) stack, pages * page_size(), prot) < 0) |
| 60 | panic("protecting stack failed, errno = %d", errno); |
| 61 | } |
| 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 | |
| 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 | int raw(int fd) |
| 107 | { |
| 108 | struct termios tt; |
| 109 | int err; |
| 110 | |
| 111 | CATCH_EINTR(err = tcgetattr(fd, &tt)); |
| 112 | if (err < 0) { |
| 113 | printk("tcgetattr failed, errno = %d\n", errno); |
| 114 | return(-errno); |
| 115 | } |
| 116 | |
| 117 | cfmakeraw(&tt); |
| 118 | |
| 119 | CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt)); |
| 120 | if (err < 0) { |
| 121 | printk("tcsetattr failed, errno = %d\n", errno); |
| 122 | return(-errno); |
| 123 | } |
| 124 | |
| 125 | /* XXX tcsetattr could have applied only some changes |
| 126 | * (and cfmakeraw() is a set of changes) */ |
| 127 | return(0); |
| 128 | } |
| 129 | |
| 130 | void setup_machinename(char *machine_out) |
| 131 | { |
| 132 | struct utsname host; |
| 133 | |
| 134 | uname(&host); |
| 135 | strcpy(machine_out, host.machine); |
| 136 | } |
| 137 | |
| 138 | char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1]; |
| 139 | |
| 140 | void setup_hostinfo(void) |
| 141 | { |
| 142 | struct utsname host; |
| 143 | |
| 144 | uname(&host); |
| 145 | sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename, |
| 146 | host.release, host.version, host.machine); |
| 147 | } |
| 148 | |
| 149 | int setjmp_wrapper(void (*proc)(void *, void *), ...) |
| 150 | { |
| 151 | va_list args; |
| 152 | sigjmp_buf buf; |
| 153 | int n; |
| 154 | |
| 155 | n = sigsetjmp(buf, 1); |
| 156 | if(n == 0){ |
| 157 | va_start(args, proc); |
| 158 | (*proc)(&buf, &args); |
| 159 | } |
| 160 | va_end(args); |
| 161 | return(n); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 166 | * Emacs will notice this stuff at the end of the file and automatically |
| 167 | * adjust the settings for this buffer only. This must remain at the end |
| 168 | * of the file. |
| 169 | * --------------------------------------------------------------------------- |
| 170 | * Local variables: |
| 171 | * c-file-style: "linux" |
| 172 | * End: |
| 173 | */ |