[PATCH] uml: separate libc-dependent helper code
[safe/jmp/linux-2.6] / arch / um / kernel / user_util.c
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 "ptrace_user.h"
31 #include "uml-config.h"
32
33 void stop(void)
34 {
35         while(1) sleep(1000000);
36 }
37
38 void stack_protections(unsigned long address)
39 {
40         int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
41
42         if(mprotect((void *) address, page_size(), prot) < 0)
43                 panic("protecting stack failed, errno = %d", errno);
44 }
45
46 void task_protections(unsigned long address)
47 {
48         unsigned long guard = address + page_size();
49         unsigned long stack = guard + page_size();
50         int prot = 0, pages;
51
52 #ifdef notdef
53         if(mprotect((void *) stack, page_size(), prot) < 0)
54                 panic("protecting guard page failed, errno = %d", errno);
55 #endif
56         pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
57         prot = PROT_READ | PROT_WRITE | PROT_EXEC;
58         if(mprotect((void *) stack, pages * page_size(), prot) < 0)
59                 panic("protecting stack failed, errno = %d", errno);
60 }
61
62 int wait_for_stop(int pid, int sig, int cont_type, void *relay)
63 {
64         sigset_t *relay_signals = relay;
65         int status, ret;
66
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
105 int raw(int fd)
106 {
107         struct termios tt;
108         int err;
109
110         CATCH_EINTR(err = tcgetattr(fd, &tt));
111         if(err < 0)
112                 return -errno;
113
114         cfmakeraw(&tt);
115
116         CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
117         if(err < 0)
118                 return -errno;
119
120         /* XXX tcsetattr could have applied only some changes
121          * (and cfmakeraw() is a set of changes) */
122         return(0);
123 }
124
125 void setup_machinename(char *machine_out)
126 {
127         struct utsname host;
128
129         uname(&host);
130 #if defined(UML_CONFIG_UML_X86) && !defined(UML_CONFIG_64BIT)
131         if (!strcmp(host.machine, "x86_64")) {
132                 strcpy(machine_out, "i686");
133                 return;
134         }
135 #endif
136         strcpy(machine_out, host.machine);
137 }
138
139 char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
140
141 void setup_hostinfo(void)
142 {
143         struct utsname host;
144
145         uname(&host);
146         sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
147                 host.release, host.version, host.machine);
148 }
149
150 int setjmp_wrapper(void (*proc)(void *, void *), ...)
151 {
152         va_list args;
153         sigjmp_buf buf;
154         int n;
155
156         n = sigsetjmp(buf, 1);
157         if(n == 0){
158                 va_start(args, proc);
159                 (*proc)(&buf, &args);
160         }
161         va_end(args);
162         return(n);
163 }
164
165 /*
166  * Overrides for Emacs so that we follow Linus's tabbing style.
167  * Emacs will notice this stuff at the end of the file and automatically
168  * adjust the settings for this buffer only.  This must remain at the end
169  * of the file.
170  * ---------------------------------------------------------------------------
171  * Local variables:
172  * c-file-style: "linux"
173  * End:
174  */