kbuild: fix silentoldconfig with make O=
[linux-2.6/verdex.git] / arch / um / os-Linux / process.c
blobd32413e4b4ce21faa2f3b61dbb690b6fecd90d87
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
6 #include <stdio.h>
7 #include <errno.h>
8 #include <signal.h>
9 #include <setjmp.h>
10 #include <linux/unistd.h>
11 #include <sys/mman.h>
12 #include <sys/wait.h>
13 #include "ptrace_user.h"
14 #include "os.h"
15 #include "user.h"
16 #include "user_util.h"
17 #include "signal_user.h"
18 #include "process.h"
19 #include "irq_user.h"
20 #include "kern_util.h"
22 #define ARBITRARY_ADDR -1
23 #define FAILURE_PID -1
25 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
26 #define COMM_SCANF "%*[^)])"
28 unsigned long os_process_pc(int pid)
30 char proc_stat[STAT_PATH_LEN], buf[256];
31 unsigned long pc;
32 int fd, err;
34 sprintf(proc_stat, "/proc/%d/stat", pid);
35 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
36 if(fd < 0){
37 printk("os_process_pc - couldn't open '%s', err = %d\n",
38 proc_stat, -fd);
39 return(ARBITRARY_ADDR);
41 err = os_read_file(fd, buf, sizeof(buf));
42 if(err < 0){
43 printk("os_process_pc - couldn't read '%s', err = %d\n",
44 proc_stat, -err);
45 os_close_file(fd);
46 return(ARBITRARY_ADDR);
48 os_close_file(fd);
49 pc = ARBITRARY_ADDR;
50 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
51 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
52 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
53 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
55 return(pc);
58 int os_process_parent(int pid)
60 char stat[STAT_PATH_LEN];
61 char data[256];
62 int parent, n, fd;
64 if(pid == -1) return(-1);
66 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
67 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
68 if(fd < 0){
69 printk("Couldn't open '%s', err = %d\n", stat, -fd);
70 return(FAILURE_PID);
73 n = os_read_file(fd, data, sizeof(data));
74 os_close_file(fd);
76 if(n < 0){
77 printk("Couldn't read '%s', err = %d\n", stat, -n);
78 return(FAILURE_PID);
81 parent = FAILURE_PID;
82 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
83 if(n != 1)
84 printk("Failed to scan '%s'\n", data);
86 return(parent);
89 void os_stop_process(int pid)
91 kill(pid, SIGSTOP);
94 void os_kill_process(int pid, int reap_child)
96 kill(pid, SIGKILL);
97 if(reap_child)
98 CATCH_EINTR(waitpid(pid, NULL, 0));
102 /* Kill off a ptraced child by all means available. kill it normally first,
103 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
104 * which it can't exit directly.
107 void os_kill_ptraced_process(int pid, int reap_child)
109 kill(pid, SIGKILL);
110 ptrace(PTRACE_KILL, pid);
111 ptrace(PTRACE_CONT, pid);
112 if(reap_child)
113 CATCH_EINTR(waitpid(pid, NULL, 0));
116 void os_usr1_process(int pid)
118 kill(pid, SIGUSR1);
121 /* Don't use the glibc version, which caches the result in TLS. It misses some
122 * syscalls, and also breaks with clone(), which does not unshare the TLS.
125 inline _syscall0(pid_t, getpid)
127 int os_getpid(void)
129 return(getpid());
132 int os_getpgrp(void)
134 return getpgrp();
137 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
138 int r, int w, int x)
140 void *loc;
141 int prot;
143 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
144 (x ? PROT_EXEC : 0);
146 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
147 fd, off);
148 if(loc == MAP_FAILED)
149 return(-errno);
150 return(0);
153 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
155 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
156 (x ? PROT_EXEC : 0));
158 if(mprotect(addr, len, prot) < 0)
159 return(-errno);
160 return(0);
163 int os_unmap_memory(void *addr, int len)
165 int err;
167 err = munmap(addr, len);
168 if(err < 0)
169 return(-errno);
170 return(0);
173 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
175 int flags = 0, pages;
177 if(sig_stack != NULL){
178 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
179 set_sigstack(sig_stack, pages * page_size());
180 flags = SA_ONSTACK;
182 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
185 void init_new_thread_signals(int altstack)
187 int flags = altstack ? SA_ONSTACK : 0;
189 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
190 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
191 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
192 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
193 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
194 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
195 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
196 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
197 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
198 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
199 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
200 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
201 signal(SIGHUP, SIG_IGN);
203 init_irq_signals(altstack);
206 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
208 sigjmp_buf buf;
209 int n;
211 *jmp_ptr = &buf;
212 n = sigsetjmp(buf, 1);
213 if(n != 0)
214 return(n);
215 (*fn)(arg);
216 return(0);
220 * Overrides for Emacs so that we follow Linus's tabbing style.
221 * Emacs will notice this stuff at the end of the file and automatically
222 * adjust the settings for this buffer only. This must remain at the end
223 * of the file.
224 * ---------------------------------------------------------------------------
225 * Local variables:
226 * c-file-style: "linux"
227 * End: