ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <fcntl.h>
9 #include <unistd.h>
10 #include <limits.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 saved_command_line[COMMAND_LINE_SIZE] = { 0 };
38 char command_line[COMMAND_LINE_SIZE] = { 0 };
39
40 void add_arg(char *cmd_line, char *arg)
41 {
42         if (strlen(cmd_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
43                 printf("add_arg: Too much command line!\n");
44                 exit(1);
45         }
46         if(strlen(cmd_line) > 0) strcat(cmd_line, " ");
47         strcat(cmd_line, arg);
48 }
49
50 void stop(void)
51 {
52         while(1) sleep(1000000);
53 }
54
55 void stack_protections(unsigned long address)
56 {
57         int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
58
59         if(mprotect((void *) address, page_size(), prot) < 0)
60                 panic("protecting stack failed, errno = %d", errno);
61 }
62
63 void task_protections(unsigned long address)
64 {
65         unsigned long guard = address + page_size();
66         unsigned long stack = guard + page_size();
67         int prot = 0, pages;
68
69 #ifdef notdef
70         if(mprotect((void *) stack, page_size(), prot) < 0)
71                 panic("protecting guard page failed, errno = %d", errno);
72 #endif
73         pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
74         prot = PROT_READ | PROT_WRITE | PROT_EXEC;
75         if(mprotect((void *) stack, pages * page_size(), prot) < 0)
76                 panic("protecting stack failed, errno = %d", errno);
77 }
78
79 int wait_for_stop(int pid, int sig, int cont_type, void *relay)
80 {
81         sigset_t *relay_signals = relay;
82         int status, ret;
83
84         while(1){
85                 if(((ret = waitpid(pid, &status, WUNTRACED)) < 0) ||
86                    !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
87                         if(ret < 0){
88                                 if(errno == EINTR) continue;
89                                 printk("wait failed, errno = %d\n",
90                                        errno);
91                         }
92                         else if(WIFEXITED(status)) 
93                                 printk("process exited with status %d\n", 
94                                        WEXITSTATUS(status));
95                         else if(WIFSIGNALED(status))
96                                 printk("process exited with signal %d\n", 
97                                        WTERMSIG(status));
98                         else if((WSTOPSIG(status) == SIGVTALRM) ||
99                                 (WSTOPSIG(status) == SIGALRM) ||
100                                 (WSTOPSIG(status) == SIGIO) ||
101                                 (WSTOPSIG(status) == SIGPROF) ||
102                                 (WSTOPSIG(status) == SIGCHLD) ||
103                                 (WSTOPSIG(status) == SIGWINCH) ||
104                                 (WSTOPSIG(status) == SIGINT)){
105                                 ptrace(cont_type, pid, 0, WSTOPSIG(status));
106                                 continue;
107                         }
108                         else if((relay_signals != NULL) &&
109                                 sigismember(relay_signals, WSTOPSIG(status))){
110                                 ptrace(cont_type, pid, 0, WSTOPSIG(status));
111                                 continue;
112                         }
113                         else printk("process stopped with signal %d\n", 
114                                     WSTOPSIG(status));
115                         panic("wait_for_stop failed to wait for %d to stop "
116                               "with %d\n", pid, sig);
117                 }
118                 return(status);
119         }
120 }
121
122 int clone_and_wait(int (*fn)(void *), void *arg, void *sp, int flags)
123 {
124         int pid;
125
126         pid = clone(fn, sp, flags, arg);
127         if(pid < 0) return(-1);
128         wait_for_stop(pid, SIGSTOP, PTRACE_CONT, NULL);
129         ptrace(PTRACE_CONT, pid, 0, 0);
130         return(pid);
131 }
132
133 int raw(int fd, int complain)
134 {
135         struct termios tt;
136         int err;
137
138         tcgetattr(fd, &tt);
139         cfmakeraw(&tt);
140         err = tcsetattr(fd, TCSANOW, &tt);
141         if((err < 0) && complain){
142                 printk("tcsetattr failed, errno = %d\n", errno);
143                 return(-errno);
144         }
145         return(0);
146 }
147
148 void setup_machinename(char *machine_out)
149 {
150         struct utsname host;
151
152         uname(&host);
153         strcpy(machine_out, host.machine);
154 }
155
156 char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
157
158 void setup_hostinfo(void)
159 {
160         struct utsname host;
161
162         uname(&host);
163         sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
164                 host.release, host.version, host.machine);
165 }
166
167 /*
168  * Overrides for Emacs so that we follow Linus's tabbing style.
169  * Emacs will notice this stuff at the end of the file and automatically
170  * adjust the settings for this buffer only.  This must remain at the end
171  * of the file.
172  * ---------------------------------------------------------------------------
173  * Local variables:
174  * c-file-style: "linux"
175  * End:
176  */