vserver 1.9.3
[linux-2.6.git] / 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/ptrace.h>
14 #include <sys/utsname.h>
15 #include <sys/param.h>
16 #include <sys/time.h>
17 #include "asm/types.h"
18 #include <ctype.h>
19 #include <signal.h>
20 #include <wait.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <sched.h>
24 #include <termios.h>
25 #include <string.h>
26 #include "user_util.h"
27 #include "kern_util.h"
28 #include "user.h"
29 #include "mem_user.h"
30 #include "init.h"
31 #include "helper.h"
32 #include "uml-config.h"
33
34 #define COMMAND_LINE_SIZE _POSIX_ARG_MAX
35
36 /* Changed in linux_main and setup_arch, which run before SMP is started */
37 char command_line[COMMAND_LINE_SIZE] = { 0 };
38
39 void add_arg(char *cmd_line, char *arg)
40 {
41         if (strlen(cmd_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
42                 printf("add_arg: Too much command line!\n");
43                 exit(1);
44         }
45         if(strlen(cmd_line) > 0) strcat(cmd_line, " ");
46         strcat(cmd_line, arg);
47 }
48
49 void stop(void)
50 {
51         while(1) sleep(1000000);
52 }
53
54 void stack_protections(unsigned long address)
55 {
56         int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
57
58         if(mprotect((void *) address, page_size(), prot) < 0)
59                 panic("protecting stack failed, errno = %d", errno);
60 }
61
62 void task_protections(unsigned long address)
63 {
64         unsigned long guard = address + page_size();
65         unsigned long stack = guard + page_size();
66         int prot = 0, pages;
67
68 #ifdef notdef
69         if(mprotect((void *) stack, page_size(), prot) < 0)
70                 panic("protecting guard page failed, errno = %d", errno);
71 #endif
72         pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
73         prot = PROT_READ | PROT_WRITE | PROT_EXEC;
74         if(mprotect((void *) stack, pages * page_size(), prot) < 0)
75                 panic("protecting stack failed, errno = %d", errno);
76 }
77
78 int wait_for_stop(int pid, int sig, int cont_type, void *relay)
79 {
80         sigset_t *relay_signals = relay;
81         int status, ret;
82
83         while(1){
84                 CATCH_EINTR(ret = waitpid(pid, &status, WUNTRACED));
85                 if((ret < 0) ||
86                    !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
87                         if(ret < 0){
88                                 printk("wait failed, errno = %d\n",
89                                        errno);
90                         }
91                         else if(WIFEXITED(status)) 
92                                 printk("process %d exited with status %d\n",
93                                        pid, WEXITSTATUS(status));
94                         else if(WIFSIGNALED(status))
95                                 printk("process %d exited with signal %d\n",
96                                        pid, WTERMSIG(status));
97                         else if((WSTOPSIG(status) == SIGVTALRM) ||
98                                 (WSTOPSIG(status) == SIGALRM) ||
99                                 (WSTOPSIG(status) == SIGIO) ||
100                                 (WSTOPSIG(status) == SIGPROF) ||
101                                 (WSTOPSIG(status) == SIGCHLD) ||
102                                 (WSTOPSIG(status) == SIGWINCH) ||
103                                 (WSTOPSIG(status) == SIGINT)){
104                                 ptrace(cont_type, pid, 0, WSTOPSIG(status));
105                                 continue;
106                         }
107                         else if((relay_signals != NULL) &&
108                                 sigismember(relay_signals, WSTOPSIG(status))){
109                                 ptrace(cont_type, pid, 0, WSTOPSIG(status));
110                                 continue;
111                         }
112                         else printk("process %d stopped with signal %d\n",
113                                     pid, WSTOPSIG(status));
114                         panic("wait_for_stop failed to wait for %d to stop "
115                               "with %d\n", pid, sig);
116                 }
117                 return(status);
118         }
119 }
120
121 int raw(int fd)
122 {
123         struct termios tt;
124         int err;
125
126         CATCH_EINTR(err = tcgetattr(fd, &tt));
127         if (err < 0) {
128                         printk("tcgetattr failed, errno = %d\n", errno);
129                 return(-errno);
130         }
131
132         cfmakeraw(&tt);
133
134         CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
135         if (err < 0) {
136                         printk("tcsetattr failed, errno = %d\n", errno);
137                 return(-errno);
138         }
139
140         /* XXX tcsetattr could have applied only some changes
141          * (and cfmakeraw() is a set of changes) */
142         return(0);
143 }
144
145 void setup_machinename(char *machine_out)
146 {
147         struct utsname host;
148
149         uname(&host);
150         strcpy(machine_out, host.machine);
151 }
152
153 char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
154
155 void setup_hostinfo(void)
156 {
157         struct utsname host;
158
159         uname(&host);
160         sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
161                 host.release, host.version, host.machine);
162 }
163
164 int setjmp_wrapper(void (*proc)(void *, void *), ...)
165 {
166         va_list args;
167         sigjmp_buf buf;
168         int n;
169
170         n = sigsetjmp(buf, 1);
171         if(n == 0){
172                 va_start(args, proc);
173                 (*proc)(&buf, &args);
174         }
175         va_end(args);
176         return(n);
177 }
178
179 /*
180  * Overrides for Emacs so that we follow Linus's tabbing style.
181  * Emacs will notice this stuff at the end of the file and automatically
182  * adjust the settings for this buffer only.  This must remain at the end
183  * of the file.
184  * ---------------------------------------------------------------------------
185  * Local variables:
186  * c-file-style: "linux"
187  * End:
188  */