[PATCH] fix memory scribble in arch/i386/pci/fixup.c
[linux-2.6/verdex.git] / arch / um / os-Linux / process.c
blob1e126bfd31a7b89b5d9663ae4a1b7cefcb294422
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 <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"
18 #define ARBITRARY_ADDR -1
19 #define FAILURE_PID -1
21 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
22 #define COMM_SCANF "%*[^)])"
24 unsigned long os_process_pc(int pid)
26 char proc_stat[STAT_PATH_LEN], buf[256];
27 unsigned long pc;
28 int fd, err;
30 sprintf(proc_stat, "/proc/%d/stat", pid);
31 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
32 if(fd < 0){
33 printk("os_process_pc - couldn't open '%s', err = %d\n",
34 proc_stat, -fd);
35 return(ARBITRARY_ADDR);
37 err = os_read_file(fd, buf, sizeof(buf));
38 if(err < 0){
39 printk("os_process_pc - couldn't read '%s', err = %d\n",
40 proc_stat, -err);
41 os_close_file(fd);
42 return(ARBITRARY_ADDR);
44 os_close_file(fd);
45 pc = ARBITRARY_ADDR;
46 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
47 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
48 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
49 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
51 return(pc);
54 int os_process_parent(int pid)
56 char stat[STAT_PATH_LEN];
57 char data[256];
58 int parent, n, fd;
60 if(pid == -1) return(-1);
62 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
63 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
64 if(fd < 0){
65 printk("Couldn't open '%s', err = %d\n", stat, -fd);
66 return(FAILURE_PID);
69 n = os_read_file(fd, data, sizeof(data));
70 os_close_file(fd);
72 if(n < 0){
73 printk("Couldn't read '%s', err = %d\n", stat, -n);
74 return(FAILURE_PID);
77 parent = FAILURE_PID;
78 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
79 if(n != 1)
80 printk("Failed to scan '%s'\n", data);
82 return(parent);
85 void os_stop_process(int pid)
87 kill(pid, SIGSTOP);
90 void os_kill_process(int pid, int reap_child)
92 kill(pid, SIGKILL);
93 if(reap_child)
94 CATCH_EINTR(waitpid(pid, NULL, 0));
98 /* Kill off a ptraced child by all means available. kill it normally first,
99 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
100 * which it can't exit directly.
103 void os_kill_ptraced_process(int pid, int reap_child)
105 kill(pid, SIGKILL);
106 ptrace(PTRACE_KILL, pid);
107 ptrace(PTRACE_CONT, pid);
108 if(reap_child)
109 CATCH_EINTR(waitpid(pid, NULL, 0));
112 void os_usr1_process(int pid)
114 kill(pid, SIGUSR1);
117 /*Don't use the glibc version, which caches the result in TLS. It misses some
118 * syscalls, and also breaks with clone(), which does not unshare the TLS.*/
119 inline _syscall0(pid_t, getpid)
121 int os_getpid(void)
123 return(getpid());
126 int os_getpgrp(void)
128 return getpgrp();
131 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
132 int r, int w, int x)
134 void *loc;
135 int prot;
137 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
138 (x ? PROT_EXEC : 0);
140 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
141 fd, off);
142 if(loc == MAP_FAILED)
143 return(-errno);
144 return(0);
147 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
149 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
150 (x ? PROT_EXEC : 0));
152 if(mprotect(addr, len, prot) < 0)
153 return(-errno);
154 return(0);
157 int os_unmap_memory(void *addr, int len)
159 int err;
161 err = munmap(addr, len);
162 if(err < 0)
163 return(-errno);
164 return(0);
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: