Pull define-node-cleanup into release branch
[powerpc.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/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                 return -errno;
114
115         cfmakeraw(&tt);
116
117         CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
118         if(err < 0)
119                 return -errno;
120
121         /* XXX tcsetattr could have applied only some changes
122          * (and cfmakeraw() is a set of changes) */
123         return(0);
124 }
125
126 void setup_machinename(char *machine_out)
127 {
128         struct utsname host;
129
130         uname(&host);
131 #if defined(UML_CONFIG_UML_X86) && !defined(UML_CONFIG_64BIT)
132         if (!strcmp(host.machine, "x86_64")) {
133                 strcpy(machine_out, "i686");
134                 return;
135         }
136 #endif
137         strcpy(machine_out, host.machine);
138 }
139
140 char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
141
142 void setup_hostinfo(void)
143 {
144         struct utsname host;
145
146         uname(&host);
147         sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
148                 host.release, host.version, host.machine);
149 }
150
151 int setjmp_wrapper(void (*proc)(void *, void *), ...)
152 {
153         va_list args;
154         sigjmp_buf buf;
155         int n;
156
157         n = sigsetjmp(buf, 1);
158         if(n == 0){
159                 va_start(args, proc);
160                 (*proc)(&buf, &args);
161         }
162         va_end(args);
163         return(n);
164 }
165
166 /*
167  * Overrides for Emacs so that we follow Linus's tabbing style.
168  * Emacs will notice this stuff at the end of the file and automatically
169  * adjust the settings for this buffer only.  This must remain at the end
170  * of the file.
171  * ---------------------------------------------------------------------------
172  * Local variables:
173  * c-file-style: "linux"
174  * End:
175  */