ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / os-Linux / process.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
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 "os.h"
13 #include "user.h"
14
15 unsigned long os_process_pc(int pid)
16 {
17         char proc_stat[sizeof("/proc/#####/stat\0")], buf[256];
18         unsigned long pc;
19         int fd;
20
21         sprintf(proc_stat, "/proc/%d/stat", pid);
22         fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
23         if(fd < 0){
24                 printk("os_process_pc - couldn't open '%s', errno = %d\n", 
25                        proc_stat, errno);
26                 return(-1);
27         }
28         if(read(fd, buf, sizeof(buf)) < 0){
29                 printk("os_process_pc - couldn't read '%s', errno = %d\n", 
30                        proc_stat, errno);
31                 close(fd);
32                 return(-1);
33         }
34         close(fd);
35         pc = -1;
36         if(sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*d %*d %*d "
37                   "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
38                   "%*d %*d %*d %*d %ld", &pc) != 1){
39                 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
40         }
41         return(pc);
42 }
43
44 int os_process_parent(int pid)
45 {
46         char stat[sizeof("/proc/nnnnn/stat\0")];
47         char data[256];
48         int parent, n, fd;
49
50         if(pid == -1) return(-1);
51
52         snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
53         fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
54         if(fd < 0){
55                 printk("Couldn't open '%s', errno = %d\n", stat, -fd);
56                 return(-1);
57         }
58
59         n = read(fd, data, sizeof(data));
60         close(fd);
61
62         if(n < 0){
63                 printk("Couldn't read '%s', errno = %d\n", stat);
64                 return(-1);
65         }
66
67         parent = -1;
68         /* XXX This will break if there is a space in the command */
69         n = sscanf(data, "%*d %*s %*c %d", &parent);
70         if(n != 1) printk("Failed to scan '%s'\n", data);
71
72         return(parent);
73 }
74
75 void os_stop_process(int pid)
76 {
77         kill(pid, SIGSTOP);
78 }
79
80 void os_kill_process(int pid, int reap_child)
81 {
82         kill(pid, SIGKILL);
83         if(reap_child)
84                 waitpid(pid, NULL, 0);
85                 
86 }
87
88 void os_usr1_process(int pid)
89 {
90         kill(pid, SIGUSR1);
91 }
92
93 int os_getpid(void)
94 {
95         return(getpid());
96 }
97
98 int os_map_memory(void *virt, int fd, unsigned long off, unsigned long len, 
99                   int r, int w, int x)
100 {
101         void *loc;
102         int prot;
103
104         prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | 
105                 (x ? PROT_EXEC : 0);
106
107         loc = mmap((void *) virt, len, prot, MAP_SHARED | MAP_FIXED, 
108                    fd, off);
109         if(loc == MAP_FAILED)
110                 return(-errno);
111         return(0);
112 }
113
114 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
115 {
116         int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | 
117                     (x ? PROT_EXEC : 0));
118
119         if(mprotect(addr, len, prot) < 0)
120                 return(-errno);
121         return(0);
122 }
123
124 int os_unmap_memory(void *addr, int len)
125 {
126         int err;
127
128         err = munmap(addr, len);
129         if(err < 0) return(-errno);
130         return(0);
131 }
132
133 /*
134  * Overrides for Emacs so that we follow Linus's tabbing style.
135  * Emacs will notice this stuff at the end of the file and automatically
136  * adjust the settings for this buffer only.  This must remain at the end
137  * of the file.
138  * ---------------------------------------------------------------------------
139  * Local variables:
140  * c-file-style: "linux"
141  * End:
142  */