[IPV4]: Correct rp_filter help text.
[linux-2.6/verdex.git] / arch / um / os-Linux / process.c
blob2d9d2ca39299f2e74ee2be3a47daf0d877d213e8
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <sys/mman.h>
11 #include <sys/wait.h>
12 #include <sys/mman.h>
13 #include <sys/syscall.h>
14 #include "ptrace_user.h"
15 #include "os.h"
16 #include "user.h"
17 #include "process.h"
18 #include "irq_user.h"
19 #include "kern_util.h"
20 #include "longjmp.h"
21 #include "skas_ptrace.h"
22 #include "kern_constants.h"
23 #include "uml-config.h"
24 #include "init.h"
26 #define ARBITRARY_ADDR -1
27 #define FAILURE_PID -1
29 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
30 #define COMM_SCANF "%*[^)])"
32 unsigned long os_process_pc(int pid)
34 char proc_stat[STAT_PATH_LEN], buf[256];
35 unsigned long pc;
36 int fd, err;
38 sprintf(proc_stat, "/proc/%d/stat", pid);
39 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
40 if(fd < 0){
41 printk("os_process_pc - couldn't open '%s', err = %d\n",
42 proc_stat, -fd);
43 return ARBITRARY_ADDR;
45 CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
46 if(err < 0){
47 printk("os_process_pc - couldn't read '%s', err = %d\n",
48 proc_stat, errno);
49 os_close_file(fd);
50 return ARBITRARY_ADDR;
52 os_close_file(fd);
53 pc = ARBITRARY_ADDR;
54 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
55 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
56 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
57 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
59 return pc;
62 int os_process_parent(int pid)
64 char stat[STAT_PATH_LEN];
65 char data[256];
66 int parent, n, fd;
68 if(pid == -1)
69 return -1;
71 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
72 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
73 if(fd < 0){
74 printk("Couldn't open '%s', err = %d\n", stat, -fd);
75 return FAILURE_PID;
78 CATCH_EINTR(n = read(fd, data, sizeof(data)));
79 os_close_file(fd);
81 if(n < 0){
82 printk("Couldn't read '%s', err = %d\n", stat, errno);
83 return FAILURE_PID;
86 parent = FAILURE_PID;
87 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
88 if(n != 1)
89 printk("Failed to scan '%s'\n", data);
91 return parent;
94 void os_stop_process(int pid)
96 kill(pid, SIGSTOP);
99 void os_kill_process(int pid, int reap_child)
101 kill(pid, SIGKILL);
102 if(reap_child)
103 CATCH_EINTR(waitpid(pid, NULL, 0));
107 /* This is here uniquely to have access to the userspace errno, i.e. the one
108 * used by ptrace in case of error.
111 long os_ptrace_ldt(long pid, long addr, long data)
113 int ret;
115 ret = ptrace(PTRACE_LDT, pid, addr, data);
117 if (ret < 0)
118 return -errno;
119 return ret;
122 /* Kill off a ptraced child by all means available. kill it normally first,
123 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
124 * which it can't exit directly.
127 void os_kill_ptraced_process(int pid, int reap_child)
129 kill(pid, SIGKILL);
130 ptrace(PTRACE_KILL, pid);
131 ptrace(PTRACE_CONT, pid);
132 if(reap_child)
133 CATCH_EINTR(waitpid(pid, NULL, 0));
136 #ifdef UML_CONFIG_MODE_TT
137 void os_usr1_process(int pid)
139 kill(pid, SIGUSR1);
141 #endif
143 /* Don't use the glibc version, which caches the result in TLS. It misses some
144 * syscalls, and also breaks with clone(), which does not unshare the TLS.
147 int os_getpid(void)
149 return syscall(__NR_getpid);
152 int os_getpgrp(void)
154 return getpgrp();
157 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
158 int r, int w, int x)
160 void *loc;
161 int prot;
163 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
164 (x ? PROT_EXEC : 0);
166 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
167 fd, off);
168 if(loc == MAP_FAILED)
169 return -errno;
170 return 0;
173 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
175 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
176 (x ? PROT_EXEC : 0));
178 if(mprotect(addr, len, prot) < 0)
179 return -errno;
180 return 0;
183 int os_unmap_memory(void *addr, int len)
185 int err;
187 err = munmap(addr, len);
188 if(err < 0)
189 return -errno;
190 return 0;
193 #ifndef MADV_REMOVE
194 #define MADV_REMOVE KERNEL_MADV_REMOVE
195 #endif
197 int __init os_drop_memory(void *addr, int length)
199 int err;
201 err = madvise(addr, length, MADV_REMOVE);
202 if(err < 0)
203 err = -errno;
204 return err;
207 int __init can_drop_memory(void)
209 void *addr;
210 int fd, ok = 0;
212 printk("Checking host MADV_REMOVE support...");
213 fd = create_mem_file(UM_KERN_PAGE_SIZE);
214 if(fd < 0){
215 printk("Creating test memory file failed, err = %d\n", -fd);
216 goto out;
219 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
220 MAP_SHARED, fd, 0);
221 if(addr == MAP_FAILED){
222 printk("Mapping test memory file failed, err = %d\n", -errno);
223 goto out_close;
226 if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){
227 printk("MADV_REMOVE failed, err = %d\n", -errno);
228 goto out_unmap;
231 printk("OK\n");
232 ok = 1;
234 out_unmap:
235 munmap(addr, UM_KERN_PAGE_SIZE);
236 out_close:
237 close(fd);
238 out:
239 return ok;
242 #ifdef UML_CONFIG_MODE_TT
243 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
245 int flags = 0, pages;
247 if(sig_stack != NULL){
248 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
249 set_sigstack(sig_stack, pages * UM_KERN_PAGE_SIZE);
250 flags = SA_ONSTACK;
252 if(usr1_handler){
253 struct sigaction sa;
255 sa.sa_handler = usr1_handler;
256 sigemptyset(&sa.sa_mask);
257 sa.sa_flags = flags;
258 sa.sa_restorer = NULL;
259 if(sigaction(SIGUSR1, &sa, NULL) < 0)
260 panic("init_new_thread_stack - sigaction failed - "
261 "errno = %d\n", errno);
264 #endif
266 void init_new_thread_signals(void)
268 set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
269 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
270 set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
271 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
272 set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
273 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
274 set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
275 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
276 set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
277 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
278 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
279 SA_ONSTACK, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
280 -1);
281 signal(SIGHUP, SIG_IGN);
283 init_irq_signals(1);
286 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
288 jmp_buf buf;
289 int n;
291 *jmp_ptr = &buf;
292 n = UML_SETJMP(&buf);
293 if(n != 0)
294 return n;
295 (*fn)(arg);
296 return 0;