Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.venv/
dist
openbsd.egg-info/
__pycache__

*.pyc
*.o
*.so

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ release: clean build
twine upload dist/*

clean:
rm -rf dist build openbsd.egg-info/
rm -rf dist build openbsd.egg-info/
rm -rf openbsd/__pycache__ openbsd/_openbsd.c openbsd/*.o openbsd/*.so
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ Python bindings for some OpenBSD-specific APIs. Currently the following are supp

* `pledge`
* `unveil`
* `get_cpu_count` (via `sysctl`)

## Change Log

### v0.2.0 (2025-09-07)

* Added the `get_cpu_count()` function

### v0.1.0 (2019-05-03)

* Initial release.
Expand All @@ -25,41 +30,47 @@ Openbsd is on PyPI. You can install it using pip:
## Usage

Import `openbsd` first:
```python
~~~python
import openbsd
```
~~~

### pledge

See: https://man.openbsd.org/pledge.2

```python
~~~python
openbsd.pledge("stdio rpath")
print(open("/etc/resolv.conf"))
```
~~~

Try removing the`rpath permission.

### unveil

See: https://man.openbsd.org/unveil.2

```python
~~~python
openbsd.unveil("/etc", "r")
print(open("/etc/resolv.conf"))
```
~~~

Try opening `/bin/ksh`.

Use `openbsd.unveil()` to lock down restrictions.

### get_cpu_count

~~~python
cpu_count = openbsd.get_cpu_count()
~~~


## Similar Projects

* [PyPledge](https://gitlab.com/i80and/pypledge): Python binding for the OpenBSD pledge(2) system call. Uses ctypes.

## License

(c) 2019 Yuce Tekol
(c) 2019-2025 Yuce Tekol

[BSD](LICENSE)
44 changes: 31 additions & 13 deletions openbsd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

import sys
import os
from typing import Any

from cffi import FFI
from ._openbsd import lib as _lib

from openbsd._openbsd import lib as _lib
from openbsd.const import CTL_HW, HW_NCPU

__all__ = ["pledge", "unveil"]
_ffi = FFI()
Expand Down Expand Up @@ -46,17 +50,31 @@ def unveil(path=None, permissions=None):
raise OSError(errno, os.strerror(errno))


if isinstance(b"openbsd", str):
# Python 2
def _encode(text):
if isinstance(text, unicode):
return text.encode("ascii")
return text
else:
# Python 3
def _encode(text):
if isinstance(text, str):
return text.encode("ascii")
return text
def _sysctl(mib: list[int], ret_type:str, ret_size:int, set_type=None, set_size=0) -> Any:
if set_type:
raise ValueError("set_type is not supported")
else:
set_type = _ffi.NULL
fmib = _ffi.new("int[]", mib)
value = _ffi.new(ret_type)
size = _ffi.new("size_t*", ret_size)
ret = _lib.sysctl(fmib, len(mib), value, size, set_type, set_size)
if ret < 0:
errno = _ffi.errno
raise OSError(errno, os.strerror(errno))
return value[0]


def get_cpu_count() -> int:
"""Return CPU count."""
# TODO: find ptr_size auto
ptr_size = 8
return _sysctl([CTL_HW, HW_NCPU], "int*", ptr_size)


def _encode(text):
if isinstance(text, str):
return text.encode("ascii")
return text


3 changes: 3 additions & 0 deletions openbsd/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

CTL_HW = 6
HW_NCPU = 3
6 changes: 6 additions & 0 deletions openbsd/openbsd_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
ffibuilder.cdef('''
int pledge(const char *promises, const char *execpromises);
int unveil(const char *path, const char *permissions);
int sysctl(const int *name, unsigned namelen, void *oldp, size_t *oldlenp,
void *newp, size_t newlen);
void perror(const char *string);
''')

ffibuilder.set_source("openbsd._openbsd",
"""
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
""")

if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cffi==1.12.3
pycparser==2.19
cffi==1.17.1
pycparser==2.22
10 changes: 3 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# Author: yuce
# Created on: 2019-05-03, at: 01:50 +0300

import sys
import os
import io
import os.path
from setuptools import setup

with io.open("README.md", encoding="utf-8") as f:
long_description = f.read()

setup(name="openbsd",
version="0.1.0",
version="0.2.0",
url="",
download_url="https://github.com/yuce/pyopenbsd",
author="Yuce Tekol",
Expand All @@ -22,9 +19,9 @@
license="BSD",
packages=["openbsd"],
keywords=["OpenBSD"],
setup_requires=["cffi>=1.12.3"],
setup_requires=["cffi>=1.17.1"],
cffi_modules=["openbsd/openbsd_builder.py:ffibuilder"],
install_requires=["cffi>=1.12.3"],
install_requires=["cffi>=1.17.1"],
tests_require=["pytest", "pytest-cov"],
classifiers=[
"Development Status :: 3 - Alpha",
Expand All @@ -34,7 +31,6 @@
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Operating System :: POSIX :: BSD :: OpenBSD",
"License :: OSI Approved :: BSD License",
],

)