initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / um / os-Linux / process.c
blobec4617b42886d056e4688017eea7385c1aa0b067
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 "os.h"
14 #include "user.h"
15 #include "user_util.h"
17 #define ARBITRARY_ADDR -1
18 #define FAILURE_PID -1
20 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
21 #define COMM_SCANF "%*[^)])"
23 unsigned long os_process_pc(int pid)
25 char proc_stat[STAT_PATH_LEN], buf[256];
26 unsigned long pc;
27 int fd, err;
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);
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);
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);
50 return(pc);
53 int os_process_parent(int pid)
55 char stat[STAT_PATH_LEN];
56 char data[256];
57 int parent, n, fd;
59 if(pid == -1) return(-1);
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);
68 n = os_read_file(fd, data, sizeof(data));
69 os_close_file(fd);
71 if(n < 0){
72 printk("Couldn't read '%s', err = %d\n", stat, -n);
73 return(FAILURE_PID);
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);
81 return(parent);
84 void os_stop_process(int pid)
86 kill(pid, SIGSTOP);
89 void os_kill_process(int pid, int reap_child)
91 kill(pid, SIGKILL);
92 if(reap_child)
93 CATCH_EINTR(waitpid(pid, NULL, 0));
97 void os_usr1_process(int pid)
99 kill(pid, SIGUSR1);
102 int os_getpid(void)
104 return(getpid());
107 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
108 int r, int w, int x)
110 void *loc;
111 int prot;
113 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
114 (x ? PROT_EXEC : 0);
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);
123 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
125 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
126 (x ? PROT_EXEC : 0));
128 if(mprotect(addr, len, prot) < 0)
129 return(-errno);
130 return(0);
133 int os_unmap_memory(void *addr, int len)
135 int err;
137 err = munmap(addr, len);
138 if(err < 0)
139 return(-errno);
140 return(0);
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: