This commit was manufactured by cvs2svn to create tag
[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 <sys/mman.h> 
11 #include <sys/stat.h>
12 #include <sys/ptrace.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 "uml-config.h"
32
33 #define COMMAND_LINE_SIZE _POSIX_ARG_MAX
34
35 /* Changed in linux_main and setup_arch, which run before SMP is started */
36 char command_line[COMMAND_LINE_SIZE] = { 0 };
37
38 void add_arg(char *cmd_line, char *arg)
39 {
40         if (strlen(cmd_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
41                 printf("add_arg: Too much command line!\n");
42                 exit(1);
43         }
44         if(strlen(cmd_line) > 0) strcat(cmd_line, " ");
45         strcat(cmd_line, arg);
46 }
47
48 void stop(void)
49 {
50         while(1) sleep(1000000);
51 }
52
53 void stack_protections(unsigned long address)
54 {
55         int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
56
57         if(mprotect((void *) address, page_size(), prot) < 0)
58                 panic("protecting stack failed, errno = %d", errno);
59 }
60
61 void task_protections(unsigned long address)
62 {
63         unsigned long guard = address + page_size();
64         unsigned long stack = guard + page_size();
65         int prot = 0, pages;
66
67 #ifdef notdef
68         if(mprotect((void *) stack, page_size(), prot) < 0)
69                 panic("protecting guard page failed, errno = %d", errno);
70 #endif
71         pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
72         prot = PROT_READ | PROT_WRITE | PROT_EXEC;
73         if(mprotect((void *) stack, pages * page_size(), prot) < 0)
74                 panic("protecting stack failed, errno = %d", errno);
75 }
76
77 int wait_for_stop(int pid, int sig, int cont_type, void *relay)
78 {
79         sigset_t *relay_signals = relay;
80         int status, ret;
81
82         while(1){
83                 ret = waitpid(pid, &status, WUNTRACED);
84                 if((ret < 0) ||
85                    !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
86                         if(ret < 0){
87                                 if(errno == EINTR) continue;
88                                 printk("wait failed, errno = %d\n",
89                                        errno);
90                         }
91                         else if(WIFEXITED(status)) 
92                                 printk("process exited with status %d\n", 
93                                        WEXITSTATUS(status));
94                         else if(WIFSIGNALED(status))
95                                 printk("process exited with signal %d\n", 
96                                        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 stopped with signal %d\n", 
113                                     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, int complain)
122 {
123         struct termios tt;
124         int err;
125
126         tcgetattr(fd, &tt);
127         cfmakeraw(&tt);
128         err = tcsetattr(fd, TCSANOW, &tt);
129         if((err < 0) && complain){
130                 printk("tcsetattr failed, errno = %d\n", errno);
131                 return(-errno);
132         }
133         return(0);
134 }
135
136 void setup_machinename(char *machine_out)
137 {
138         struct utsname host;
139
140         uname(&host);
141         strcpy(machine_out, host.machine);
142 }
143
144 char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
145
146 void setup_hostinfo(void)
147 {
148         struct utsname host;
149
150         uname(&host);
151         sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
152                 host.release, host.version, host.machine);
153 }
154
155 /*
156  * Overrides for Emacs so that we follow Linus's tabbing style.
157  * Emacs will notice this stuff at the end of the file and automatically
158  * adjust the settings for this buffer only.  This must remain at the end
159  * of the file.
160  * ---------------------------------------------------------------------------
161  * Local variables:
162  * c-file-style: "linux"
163  * End:
164  */