summaryrefslogtreecommitdiff
path: root/procfs.c
blob: 63aef3a10232e7bdd93ff900e86e40b69fba3787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "procfs.h"

int procfs_open(pid_t pid) {
	char path[2048];
	sprintf(path, "/proc/%d/mem", pid);
	return open(path, O_RDWR);
}

int read_page(int fd, uintptr_t index, uint8_t *data) {
	if (lseek(fd, index * PAGE_SIZE, SEEK_SET) == -1) 
		return -1;
	size_t n = 0;
	while (n < PAGE_SIZE) {
		ssize_t result = read(fd, data + n, PAGE_SIZE - n);
		if (result == -1) return -1;
		n += result;
	}
	return 0;
}