summaryrefslogtreecommitdiff
path: root/src/__readonly.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/__readonly.c')
-rw-r--r--src/__readonly.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/__readonly.c b/src/__readonly.c
new file mode 100644
index 00000000..74837a91
--- /dev/null
+++ b/src/__readonly.c
@@ -0,0 +1,37 @@
+#include <stdlib.h>
+#include "_readonly.h"
+
+#ifdef _POSIX_C_SOURCE
+#include <sys/mman.h>
+#include <limits.h>
+#else
+#include "_syscall.h"
+#define mprotect(__ptr, __len, __prot) __syscall(__sys_mprotect, __ptr, __len, __prot)
+#define PROT_READ 1
+#define PROT_WRITE 2
+#define PAGESIZE 4096
+#endif
+
+void* __readonly(ro_action_t action, void *ptr)
+{
+ switch (action) {
+ case RO_ALLOC:
+ /* set magic to JK_READONLY */
+ /* set label to ptr */
+ return malloc(PAGESIZE);
+
+ case RO_FREE:
+ free(ptr);
+ return NULL;
+
+ case RO_LOCK:
+ mprotect(ptr, PAGESIZE, PROT_READ);
+ break;
+
+ case RO_UNLOCK:
+ mprotect(ptr, PAGESIZE, PROT_READ | PROT_WRITE);
+ break;
+ }
+
+ return ptr;
+}