blob: 1e126bfd31a7b89b5d9663ae4a1b7cefcb294422 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 "ptrace_user.h"
14#include "os.h"
15#include "user.h"
16#include "user_util.h"
17
18#define ARBITRARY_ADDR -1
19#define FAILURE_PID -1
20
21#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
22#define COMM_SCANF "%*[^)])"
23
24unsigned long os_process_pc(int pid)
25{
26 char proc_stat[STAT_PATH_LEN], buf[256];
27 unsigned long pc;
28 int fd, err;
29
30 sprintf(proc_stat, "/proc/%d/stat", pid);
31 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
32 if(fd < 0){
33 printk("os_process_pc - couldn't open '%s', err = %d\n",
34 proc_stat, -fd);
35 return(ARBITRARY_ADDR);
36 }
37 err = os_read_file(fd, buf, sizeof(buf));
38 if(err < 0){
39 printk("os_process_pc - couldn't read '%s', err = %d\n",
40 proc_stat, -err);
41 os_close_file(fd);
42 return(ARBITRARY_ADDR);
43 }
44 os_close_file(fd);
45 pc = ARBITRARY_ADDR;
46 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
47 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
48 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
49 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
50 }
51 return(pc);
52}
53
54int os_process_parent(int pid)
55{
56 char stat[STAT_PATH_LEN];
57 char data[256];
58 int parent, n, fd;
59
60 if(pid == -1) return(-1);
61
62 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
63 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
64 if(fd < 0){
65 printk("Couldn't open '%s', err = %d\n", stat, -fd);
66 return(FAILURE_PID);
67 }
68
69 n = os_read_file(fd, data, sizeof(data));
70 os_close_file(fd);
71
72 if(n < 0){
73 printk("Couldn't read '%s', err = %d\n", stat, -n);
74 return(FAILURE_PID);
75 }
76
77 parent = FAILURE_PID;
78 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
79 if(n != 1)
80 printk("Failed to scan '%s'\n", data);
81
82 return(parent);
83}
84
85void os_stop_process(int pid)
86{
87 kill(pid, SIGSTOP);
88}
89
90void os_kill_process(int pid, int reap_child)
91{
92 kill(pid, SIGKILL);
93 if(reap_child)
94 CATCH_EINTR(waitpid(pid, NULL, 0));
95
96}
97
98/* Kill off a ptraced child by all means available. kill it normally first,
99 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
100 * which it can't exit directly.
101 */
102
103void os_kill_ptraced_process(int pid, int reap_child)
104{
105 kill(pid, SIGKILL);
106 ptrace(PTRACE_KILL, pid);
107 ptrace(PTRACE_CONT, pid);
108 if(reap_child)
109 CATCH_EINTR(waitpid(pid, NULL, 0));
110}
111
112void os_usr1_process(int pid)
113{
114 kill(pid, SIGUSR1);
115}
116
117/*Don't use the glibc version, which caches the result in TLS. It misses some
118 * syscalls, and also breaks with clone(), which does not unshare the TLS.*/
119inline _syscall0(pid_t, getpid)
120
121int os_getpid(void)
122{
123 return(getpid());
124}
125
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700126int os_getpgrp(void)
127{
128 return getpgrp();
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
132 int r, int w, int x)
133{
134 void *loc;
135 int prot;
136
137 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
138 (x ? PROT_EXEC : 0);
139
140 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
141 fd, off);
142 if(loc == MAP_FAILED)
143 return(-errno);
144 return(0);
145}
146
147int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
148{
149 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
150 (x ? PROT_EXEC : 0));
151
152 if(mprotect(addr, len, prot) < 0)
153 return(-errno);
154 return(0);
155}
156
157int os_unmap_memory(void *addr, int len)
158{
159 int err;
160
161 err = munmap(addr, len);
162 if(err < 0)
163 return(-errno);
164 return(0);
165}
166
167/*
168 * Overrides for Emacs so that we follow Linus's tabbing style.
169 * Emacs will notice this stuff at the end of the file and automatically
170 * adjust the settings for this buffer only. This must remain at the end
171 * of the file.
172 * ---------------------------------------------------------------------------
173 * Local variables:
174 * c-file-style: "linux"
175 * End:
176 */