Переглянути джерело

LibSystem: Add pledge() and unveil() wrappers that return ErrorOr<void>

These will be more ergonomic to use together with TRY(). :^)
Andreas Kling 3 роки тому
батько
коміт
dc486fa3f9

+ 1 - 0
Userland/Libraries/LibSystem/CMakeLists.txt

@@ -1,4 +1,5 @@
 set(SOURCES
+    Wrappers.cpp
     syscall.cpp
 )
 

+ 36 - 0
Userland/Libraries/LibSystem/Wrappers.cpp

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibSystem/Wrappers.h>
+#include <LibSystem/syscall.h>
+
+namespace System {
+
+ErrorOr<void> pledge(StringView promises, StringView execpromises)
+{
+    Syscall::SC_pledge_params params {
+        { promises.characters_without_null_termination(), promises.length() },
+        { execpromises.characters_without_null_termination(), execpromises.length() },
+    };
+    int rc = syscall(SC_pledge, &params);
+    if (rc < 0)
+        return Error::from_errno(-rc);
+    return {};
+}
+
+ErrorOr<void> unveil(StringView path, StringView permissions)
+{
+    Syscall::SC_unveil_params params {
+        { path.characters_without_null_termination(), path.length() },
+        { permissions.characters_without_null_termination(), permissions.length() },
+    };
+    int rc = syscall(SC_unveil, &params);
+    if (rc < 0)
+        return Error::from_errno(-rc);
+    return {};
+}
+
+}

+ 16 - 0
Userland/Libraries/LibSystem/Wrappers.h

@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Error.h>
+
+namespace System {
+
+ErrorOr<void> pledge(StringView promises, StringView execpromises);
+ErrorOr<void> unveil(StringView path, StringView permissions);
+
+}