diff options
| author | raven <citrons@mondecitronne.com> | 2026-04-08 22:51:39 -0500 |
|---|---|---|
| committer | raven <citrons@mondecitronne.com> | 2026-04-08 22:51:39 -0500 |
| commit | 18a86e1038b20cb6f8922beead08dcc24ba2a4d3 (patch) | |
| tree | 85cb74210d67d42763a4709d18af93aa60a8f400 /procfs.c | |
| parent | 4a3429a96b5b5ea7468540349aeb4535d5738053 (diff) | |
rewrite and port to SDL3
Diffstat (limited to 'procfs.c')
| -rw-r--r-- | procfs.c | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/procfs.c b/procfs.c deleted file mode 100644 index 1e70274..0000000 --- a/procfs.c +++ /dev/null @@ -1,102 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <stddef.h> -#include <stdint.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/mman.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); -} - -ssize_t procfs_maps(pid_t pid, struct procfs_map **maps) { - char path[2048]; - sprintf(path, "/proc/%d/maps", pid); - FILE *f = fopen(path, "r"); - if (!f) return -1; - - int n = 0; - int capacity = 2; - *maps = calloc(capacity, sizeof(**maps)); - if (!*maps) return -1; - while (1) { - void *base, *max; - char prot_str[4]; - int matches = fscanf(f, "%p-%p %4c", &base, &max, prot_str); - if (matches == EOF) goto eof; - if (matches < 3) goto err; - - int prot = 0; - for (int i = 0; i < 4; i++) { - switch (prot_str[i]) { - case 'r': - prot |= PROT_READ; - break; - case 'w': - prot |= PROT_WRITE; - break; - case 'x': - prot |= PROT_EXEC; - break; - default: - break; - } - } - if (prot == 0) prot = PROT_NONE; - - int c; - while ((c = getc(f)) != '\n') - if (c == EOF) goto eof; - - n++; - if (n > capacity) { - capacity <<= 1; - struct procfs_map *r = reallocarray(*maps, capacity, sizeof(*r)); - if (!r) goto err; - *maps = r; - } - (*maps)[n - 1] = (struct procfs_map) { - (uintptr_t) base, (uintptr_t) max, prot - }; - } -eof: - if (ferror(f)) goto err; - if (n == 0) free(*maps); - return n; -err: - free(*maps); - return -1; -} - -int read_mem(int fd, uintptr_t addr, uint8_t *data, size_t size) { - if (lseek(fd, addr, SEEK_SET) == -1) return -1; - size_t n = 0; - while (n < size) { - ssize_t result = read(fd, data + n, size - n); - if (result == -1) return -1; - n += result; - } - return 0; -} - -int write_mem(int fd, uintptr_t addr, uint8_t *data, size_t size) { - if (lseek(fd, addr, SEEK_SET) == -1) return -1; - size_t n = 0; - while (n < size) { - ssize_t result = write(fd, data + n, size - n); - if (result == -1) return -1; - n += result; - } - return 0; -} - -int read_page(int fd, uintptr_t index, uint8_t *data) { - return read_mem(fd, index * PAGE_SIZE, data, PAGE_SIZE); -} |
