summaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/io')
-rw-r--r--src/io/io-ptrace.c82
-rw-r--r--src/io/io-uio.c41
-rw-r--r--src/io/io.h27
3 files changed, 150 insertions, 0 deletions
diff --git a/src/io/io-ptrace.c b/src/io/io-ptrace.c
new file mode 100644
index 0000000..50d0766
--- /dev/null
+++ b/src/io/io-ptrace.c
@@ -0,0 +1,82 @@
+#include "io.h"
+#include <stdio.h>
+#include <err.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+
+static size_t
+io_ptrace_do(const struct io *io, void *ptr, const size_t offset, const size_t size, size_t (*iofun)(void*, size_t, size_t, FILE*))
+{
+ if (fseek(io->backing, offset, SEEK_SET) != 0) {
+ warn("fseek(/proc/%u/mem, %zu)", io->pid, offset);
+ return 0;
+ }
+
+ return iofun(ptr, 1, size, io->backing);
+}
+
+static size_t
+io_ptrace_write(const struct io *io, const void *ptr, const size_t offset, const size_t size)
+{
+ clearerr(io->backing);
+ const size_t ret = io_ptrace_do(io, (void*)ptr, offset, size, (size_t(*)())fwrite);
+
+ if (ferror(io->backing))
+ warn("fwrite(/proc/%u/mem)", io->pid);
+
+ return ret;
+}
+
+static size_t
+io_ptrace_read(const struct io *io, void *ptr, const size_t offset, const size_t size)
+{
+ clearerr(io->backing);
+ const size_t ret = io_ptrace_do(io, ptr, offset, size, fread);
+
+ if (ferror(io->backing))
+ warn("fread(/proc/%u/mem)", io->pid);
+
+ return ret;
+}
+
+static void
+io_ptrace_cleanup(struct io *io)
+{
+ if (io->backing)
+ fclose(io->backing);
+
+ if (io->pid)
+ ptrace(PTRACE_DETACH, io->pid, 1, 0);
+}
+
+bool
+io_ptrace_init(struct io *io, const pid_t pid)
+{
+ *io = (struct io){ .pid = pid, .read = io_ptrace_read, .write = io_ptrace_write, .cleanup = io_ptrace_cleanup };
+
+ if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1L) {
+ warn("ptrace(PTRACE_ATTACH, %u, NULL, NULL)", pid);
+ goto fail;
+ }
+
+ {
+ int status;
+ if (waitpid(pid, &status, 0) == -1 || !WIFSTOPPED(status)) {
+ warn("waitpid(%u) == %d", pid, status);
+ goto fail;
+ }
+ }
+
+ char path[128];
+ snprintf(path, sizeof(path), "/proc/%u/mem", pid);
+ if (!(io->backing = fopen(path, "w+b"))) {
+ warn("fopen(%s)", path);
+ goto fail;
+ }
+
+ return true;
+
+fail:
+ io->cleanup(io);
+ return false;
+}
diff --git a/src/io/io-uio.c b/src/io/io-uio.c
new file mode 100644
index 0000000..dd9f873
--- /dev/null
+++ b/src/io/io-uio.c
@@ -0,0 +1,41 @@
+#include "io.h"
+#include <stdint.h>
+#include <err.h>
+#include <sys/uio.h>
+
+static size_t
+io_uio_do(const struct io *io, const void *ptr, const size_t offset, const size_t size, ssize_t (*iofun)(pid_t, const struct iovec*, unsigned long, const struct iovec*, unsigned long, unsigned long))
+{
+ const struct iovec lio = { .iov_base = (void*)ptr, .iov_len = size };
+ const struct iovec rio = { .iov_base = (void*)(intptr_t)offset, .iov_len = size };
+ return iofun(io->pid, &lio, 1, &rio, 1, 0);
+}
+
+static size_t
+io_uio_write(const struct io *io, const void *ptr, const size_t offset, const size_t size)
+{
+ const size_t ret = io_uio_do(io, ptr, offset, size, process_vm_writev);
+
+ if (ret == (size_t)-1)
+ warn("process_vm_writev(%u)", io->pid);
+
+ return (ret == (size_t)-1 ? 0 : ret);
+}
+
+static size_t
+io_uio_read(const struct io *io, void *ptr, const size_t offset, const size_t size)
+{
+ const size_t ret = io_uio_do(io, ptr, offset, size, process_vm_readv);
+
+ if (ret == (size_t)-1)
+ warn("process_vm_readv(%u)", io->pid);
+
+ return (ret == (size_t)-1 ? 0 : ret);
+}
+
+bool
+io_uio_init(struct io *io, const pid_t pid)
+{
+ *io = (struct io){ .pid = pid, .read = io_uio_read, .write = io_uio_write };
+ return true;
+}
diff --git a/src/io/io.h b/src/io/io.h
new file mode 100644
index 0000000..8a0639b
--- /dev/null
+++ b/src/io/io.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <stddef.h>
+#include <stdbool.h>
+#include <sys/types.h> // pid_t
+
+struct io {
+ void *backing;
+ size_t (*read)(const struct io *io, void *ptr, const size_t offset, const size_t size);
+ size_t (*write)(const struct io *io, const void *ptr, const size_t offset, const size_t size);
+ void (*cleanup)(struct io *io);
+ pid_t pid;
+};
+
+static inline void
+io_release(struct io *io)
+{
+ if (io->cleanup)
+ io->cleanup(io);
+ *io = (struct io){0};
+}
+
+bool
+io_uio_init(struct io *io, const pid_t pid);
+
+bool
+io_ptrace_init(struct io *io, const pid_t pid);