about summary refs log tree commit diff stats
path: root/openbsd/__init__.py
blob: 1aab185866a4baa5a4508f59ad0f7faec1d66d04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #00
import sys
from cffi import FFI
from ._openbsd import lib as _lib

__all__ = ["pledge", "unveil"]

_ffi = FFI()


def pledge(promises=None, execpromises=None):
    promises = _ffi.NULL if promises is None else _encode(promises)
    execpromises = _ffi.NULL if execpromises is None else _encode(execpromises)
    ret = _lib.pledge(promises, execpromises)
    if ret < 0:
        errno = _ffi.errno
        raise OSError(errno, _decode(_ffi.string(_lib.strerror(errno), 256)))


def unveil(path=None, permissions=None):
    path = _ffi.NULL if path is None else _encode(path)
    permissions = _ffi.NULL if permissions is None else _encode(permissions)
    ret = _lib.unveil(path, permissions)
    if ret < 0:
        errno = _ffi.errno
        raise OSError(errno, _decode(_ffi.string(_lib.strerror(errno), 256)))


if isinstance(b"openbsd", str):
    # Python 2
    def _encode(text):
        if isinstance(text, unicode):
            return text.encode("ascii")
        return text

    def _decode(text):
        return text
else:
    # Python 3
    def _encode(text):
        if isinstance(text, str):
            return text.encode("ascii")
        return text

    def _decode(text):
        return text.decode("ascii")