process.c 5.8 KB
Newer Older
J
Jeff Dike 已提交
1 2
/*
 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include <stdio.h>
7
#include <stdlib.h>
J
Jeff Dike 已提交
8
#include <unistd.h>
L
Linus Torvalds 已提交
9 10
#include <errno.h>
#include <signal.h>
11
#include <fcntl.h>
L
Linus Torvalds 已提交
12
#include <sys/mman.h>
J
Jeff Dike 已提交
13
#include <sys/ptrace.h>
L
Linus Torvalds 已提交
14
#include <sys/wait.h>
J
Jeff Dike 已提交
15
#include <asm/unistd.h>
16 17 18 19
#include <init.h>
#include <longjmp.h>
#include <os.h>
#include <skas_ptrace.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29

#define ARBITRARY_ADDR -1
#define FAILURE_PID    -1

#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
#define COMM_SCANF "%*[^)])"

unsigned long os_process_pc(int pid)
{
	char proc_stat[STAT_PATH_LEN], buf[256];
30
	unsigned long pc = ARBITRARY_ADDR;
L
Linus Torvalds 已提交
31 32 33
	int fd, err;

	sprintf(proc_stat, "/proc/%d/stat", pid);
34
	fd = open(proc_stat, O_RDONLY, 0);
J
Jeff Dike 已提交
35 36
	if (fd < 0) {
		printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
37 38
		       "errno = %d\n", proc_stat, errno);
		goto out;
L
Linus Torvalds 已提交
39
	}
40
	CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
J
Jeff Dike 已提交
41 42 43
	if (err < 0) {
		printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
		       "err = %d\n", proc_stat, errno);
44
		goto out_close;
L
Linus Torvalds 已提交
45 46 47
	}
	os_close_file(fd);
	pc = ARBITRARY_ADDR;
J
Jeff Dike 已提交
48
	if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
49 50
		   "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
		   "%*d %*d %*d %*d %*d %lu", &pc) != 1)
J
Jeff Dike 已提交
51 52
		printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
		       buf);
53 54 55
 out_close:
	close(fd);
 out:
J
Jeff Dike 已提交
56
	return pc;
L
Linus Torvalds 已提交
57 58 59 60 61 62
}

int os_process_parent(int pid)
{
	char stat[STAT_PATH_LEN];
	char data[256];
63
	int parent = FAILURE_PID, n, fd;
L
Linus Torvalds 已提交
64

J
Jeff Dike 已提交
65
	if (pid == -1)
66
		return parent;
L
Linus Torvalds 已提交
67 68

	snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
69
	fd = open(stat, O_RDONLY, 0);
J
Jeff Dike 已提交
70
	if (fd < 0) {
71 72 73
		printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat,
		       errno);
		return parent;
L
Linus Torvalds 已提交
74 75
	}

76
	CATCH_EINTR(n = read(fd, data, sizeof(data)));
77
	close(fd);
L
Linus Torvalds 已提交
78

J
Jeff Dike 已提交
79
	if (n < 0) {
80
		printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat,
J
Jeff Dike 已提交
81
		       errno);
82
		return parent;
L
Linus Torvalds 已提交
83 84 85 86
	}

	parent = FAILURE_PID;
	n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
J
Jeff Dike 已提交
87 88
	if (n != 1)
		printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
L
Linus Torvalds 已提交
89

J
Jeff Dike 已提交
90
	return parent;
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98 99 100
}

void os_stop_process(int pid)
{
	kill(pid, SIGSTOP);
}

void os_kill_process(int pid, int reap_child)
{
	kill(pid, SIGKILL);
J
Jeff Dike 已提交
101
	if (reap_child)
102
		CATCH_EINTR(waitpid(pid, NULL, __WALL));
L
Linus Torvalds 已提交
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/* This is here uniquely to have access to the userspace errno, i.e. the one
 * used by ptrace in case of error.
 */

long os_ptrace_ldt(long pid, long addr, long data)
{
	int ret;

	ret = ptrace(PTRACE_LDT, pid, addr, data);

	if (ret < 0)
		return -errno;
	return ret;
}

L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129
/* Kill off a ptraced child by all means available.  kill it normally first,
 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
 * which it can't exit directly.
 */

void os_kill_ptraced_process(int pid, int reap_child)
{
	kill(pid, SIGKILL);
	ptrace(PTRACE_KILL, pid);
	ptrace(PTRACE_CONT, pid);
J
Jeff Dike 已提交
130
	if (reap_child)
131
		CATCH_EINTR(waitpid(pid, NULL, __WALL));
L
Linus Torvalds 已提交
132 133
}

134 135 136 137
/* Don't use the glibc version, which caches the result in TLS. It misses some
 * syscalls, and also breaks with clone(), which does not unshare the TLS.
 */

L
Linus Torvalds 已提交
138 139
int os_getpid(void)
{
J
Jeff Dike 已提交
140
	return syscall(__NR_getpid);
L
Linus Torvalds 已提交
141 142
}

J
Jeff Dike 已提交
143 144 145 146 147
int os_getpgrp(void)
{
	return getpgrp();
}

L
Linus Torvalds 已提交
148 149 150 151 152 153
int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
		  int r, int w, int x)
{
	void *loc;
	int prot;

J
Jeff Dike 已提交
154
	prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
L
Linus Torvalds 已提交
155 156 157 158
		(x ? PROT_EXEC : 0);

	loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
		     fd, off);
J
Jeff Dike 已提交
159
	if (loc == MAP_FAILED)
J
Jeff Dike 已提交
160 161
		return -errno;
	return 0;
L
Linus Torvalds 已提交
162 163 164 165
}

int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
{
J
Jeff Dike 已提交
166
	int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
L
Linus Torvalds 已提交
167 168
		    (x ? PROT_EXEC : 0));

J
Jeff Dike 已提交
169
	if (mprotect(addr, len, prot) < 0)
J
Jeff Dike 已提交
170
		return -errno;
J
Jeff Dike 已提交
171 172

	return 0;
L
Linus Torvalds 已提交
173 174 175 176
}

int os_unmap_memory(void *addr, int len)
{
J
Jeff Dike 已提交
177
	int err;
L
Linus Torvalds 已提交
178

J
Jeff Dike 已提交
179 180
	err = munmap(addr, len);
	if (err < 0)
J
Jeff Dike 已提交
181
		return -errno;
J
Jeff Dike 已提交
182
	return 0;
L
Linus Torvalds 已提交
183 184
}

J
Jeff Dike 已提交
185
#ifndef MADV_REMOVE
J
Jeff Dike 已提交
186
#define MADV_REMOVE KERNEL_MADV_REMOVE
J
Jeff Dike 已提交
187 188
#endif

J
Jeff Dike 已提交
189
int os_drop_memory(void *addr, int length)
J
Jeff Dike 已提交
190 191 192 193
{
	int err;

	err = madvise(addr, length, MADV_REMOVE);
J
Jeff Dike 已提交
194
	if (err < 0)
J
Jeff Dike 已提交
195 196 197 198
		err = -errno;
	return err;
}

J
Jeff Dike 已提交
199
int __init can_drop_memory(void)
J
Jeff Dike 已提交
200 201
{
	void *addr;
202
	int fd, ok = 0;
J
Jeff Dike 已提交
203

J
Jeff Dike 已提交
204
	printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
J
Jeff Dike 已提交
205
	fd = create_mem_file(UM_KERN_PAGE_SIZE);
J
Jeff Dike 已提交
206 207 208
	if (fd < 0) {
		printk(UM_KERN_ERR "Creating test memory file failed, "
		       "err = %d\n", -fd);
209
		goto out;
J
Jeff Dike 已提交
210 211 212
	}

	addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
J
Jeff Dike 已提交
213
		      MAP_SHARED, fd, 0);
J
Jeff Dike 已提交
214 215 216
	if (addr == MAP_FAILED) {
		printk(UM_KERN_ERR "Mapping test memory file failed, "
		       "err = %d\n", -errno);
217
		goto out_close;
J
Jeff Dike 已提交
218 219
	}

J
Jeff Dike 已提交
220 221
	if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
		printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
222
		goto out_unmap;
J
Jeff Dike 已提交
223 224
	}

J
Jeff Dike 已提交
225
	printk(UM_KERN_CONT "OK\n");
226 227 228 229 230 231 232 233
	ok = 1;

out_unmap:
	munmap(addr, UM_KERN_PAGE_SIZE);
out_close:
	close(fd);
out:
	return ok;
J
Jeff Dike 已提交
234 235
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static int os_page_mincore(void *addr)
{
	char vec[2];
	int ret;

	ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
	if (ret < 0) {
		if (errno == ENOMEM || errno == EINVAL)
			return 0;
		else
			return -errno;
	}

	return vec[0] & 1;
}

int os_mincore(void *addr, unsigned long len)
{
	char *vec;
	int ret, i;

	if (len <= UM_KERN_PAGE_SIZE)
		return os_page_mincore(addr);

	vec = calloc(1, (len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE);
	if (!vec)
		return -ENOMEM;

	ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
	if (ret < 0) {
		if (errno == ENOMEM || errno == EINVAL)
			ret = 0;
		else
			ret = -errno;

		goto out;
	}

	for (i = 0; i < ((len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE); i++) {
		if (!(vec[i] & 1)) {
			ret = 0;
			goto out;
		}
	}

	ret = 1;
out:
	free(vec);
	return ret;
}

287
void init_new_thread_signals(void)
288
{
289 290 291 292 293
	set_handler(SIGSEGV);
	set_handler(SIGTRAP);
	set_handler(SIGFPE);
	set_handler(SIGILL);
	set_handler(SIGBUS);
294
	signal(SIGHUP, SIG_IGN);
295
	set_handler(SIGIO);
J
Jeff Dike 已提交
296
	signal(SIGWINCH, SIG_IGN);
297
}