2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
10 #include <linux/unistd.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];
29 sprintf(proc_stat, "/proc/%d/stat", pid);
30 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
32 printk("os_process_pc - couldn't open '%s', err = %d\n",
34 return(ARBITRARY_ADDR);
36 err = os_read_file(fd, buf, sizeof(buf));
38 printk("os_process_pc - couldn't read '%s', err = %d\n",
41 return(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);
53 int os_process_parent(int pid)
55 char stat[STAT_PATH_LEN];
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);
64 printk("Couldn't open '%s', err = %d\n", stat, -fd);
68 n = os_read_file(fd, data, sizeof(data));
72 printk("Couldn't read '%s', err = %d\n", stat, -n);
77 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
79 printk("Failed to scan '%s'\n", data);
84 void os_stop_process(int pid)
89 void os_kill_process(int pid, int reap_child)
93 CATCH_EINTR(waitpid(pid, NULL, 0));
97 void os_usr1_process(int pid)
107 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
113 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
116 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
118 if(loc == MAP_FAILED)
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)
133 int os_unmap_memory(void *addr, int len)
137 err = munmap(addr, len);
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
148 * ---------------------------------------------------------------------------
150 * c-file-style: "linux"