summaryrefslogtreecommitdiffhomepage
path: root/src/os-unix.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/os-unix.h')
-rw-r--r--src/os-unix.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/os-unix.h b/src/os-unix.h
new file mode 100644
index 0000000..4ca0d4a
--- /dev/null
+++ b/src/os-unix.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright © 2021 Michael Smith <mikesmiffy128@gmail.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* Unix-specific definitions for os.h - don't include this directly! */
+
+#ifdef __GNUC__
+#define EXPORT __attribute__((visibility("default")))
+#else
+#define EXPORT int novis[-!!"visibility attribute requires Clang or GCC"];
+#endif
+#define IMPORT
+
+typedef char os_char;
+#define OS_LIT(x) x
+#define os_snprintf snprintf
+#define os_strchr strchr
+#define os_strcmp strcmp
+#define os_strcpy strcpy
+#define os_strlen strlen
+#define os_fopen fopen
+#define os_open open
+#define os_access access
+#define os_stat stat
+#define os_getenv getenv
+
+#define OS_DLSUFFIX ".so"
+
+#define OS_MAIN main
+
+#define os_dlsym dlsym
+
+// unix mprot flags are much nicer but cannot be defined in terms of the windows
+// ones, so we use the windows ones and define them in terms of the unix ones.
+// another victory for stupid!
+#define PAGE_NOACCESS 0
+#define PAGE_READONLY PROT_READ
+#define PAGE_READWRITE PROT_READ | PROT_WRITE
+#define PAGE_EXECUTE_READ PROT_READ | PROT_EXEC
+#define PAGE_EXECUTE_READWRITE PROT_READ | PROT_WRITE | PROT_EXEC
+
+static inline bool os_mprot(void *addr, int len, int fl) {
+ // round down address and round up size
+ addr = (void *)((unsigned long)addr & ~(4095));
+ len = len + 4095 & ~(4095);
+ return mprotect(addr, len, fl) != -1;
+}
+
+static inline void os_randombytes(void *buf, int sz) {
+ // if this call fails, the system is doomed, so loop until success and pray.
+ while (getentropy(buf, sz) == -1);
+}
+
+// vi: sw=4 ts=4 noet tw=80 cc=80