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