commit db06c5ea762ba1de457e4e5b9104ae95514e6775
parent c6517969b620b137bd0a61f5c154afe040c2561e
Author: Lorenz (xha) <me@xha.li>
Date: Sun, 3 Dec 2023 07:17:03 +0000
OpenBSD: add rt::pledge()
Signed-off-by: Lorenz (xha) <me@xha.li>
Diffstat:
2 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/rt/+openbsd/syscalls.ha b/rt/+openbsd/syscalls.ha
@@ -754,6 +754,39 @@ export fn listen(sockfd: int, backlog: u32) (void | errno) = {
// chflagsat
// pledge
+
+@symbol("pledge") fn libc_pledge(
+ promises: nullable *const u8,
+ execpromises: nullable *const u8
+) int;
+
+// The "stdio" pledge is always needed. Passing [[nullpromise]] to promises or
+// execpromises specifies to not change the current value. Check the pledge(2)
+// manual page for more information about the differrent promises.
+export fn pledge(
+ promises: (const str | nullpromise),
+ execpromises: (const str | nullpromise),
+) (void | errno) = {
+ let promises: nullable *u8 = match(promises) {
+ case let p: const str =>
+ yield cpath(p)!;
+ case nullpromise =>
+ yield null;
+ };
+
+ let execpromises: nullable *u8 = match(execpromises) {
+ case let ep: const str =>
+ yield copy_cpath(ep, pathbuf1)!;
+ case nullpromise =>
+ yield null;
+ };
+
+ let res = libc_pledge(promises, execpromises);
+ if (res == -1) {
+ return *__errno(): errno;
+ };
+};
+
// ppoll
@symbol("ppoll") fn libc_ppoll(
diff --git a/rt/+openbsd/types.ha b/rt/+openbsd/types.ha
@@ -19,6 +19,9 @@ export type nfds_t = uint;
export type mode_t = u32;
export type sigset = uint;
+// Passing this to a pledge() promise specifies to not change the current value.
+export type nullpromise = void;
+
// Maximum length of a file path including the NUL terminator.
export def PATH_MAX: size = 1024;