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