diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06c301b --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.venv/ +dist +openbsd.egg-info/ +__pycache__ + +*.pyc +*.o +*.so + diff --git a/Makefile b/Makefile index 19aef5b..0240788 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 6633325..790a21b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -25,18 +30,18 @@ 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. @@ -44,15 +49,21 @@ Try removing the`rpath permission. 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 @@ -60,6 +71,6 @@ Use `openbsd.unveil()` to lock down restrictions. ## License -(c) 2019 Yuce Tekol +(c) 2019-2025 Yuce Tekol [BSD](LICENSE) diff --git a/openbsd/__init__.py b/openbsd/__init__.py index e73753b..274b357 100644 --- a/openbsd/__init__.py +++ b/openbsd/__init__.py @@ -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() @@ -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 diff --git a/openbsd/const.py b/openbsd/const.py new file mode 100644 index 0000000..c1a6ca1 --- /dev/null +++ b/openbsd/const.py @@ -0,0 +1,3 @@ + +CTL_HW = 6 +HW_NCPU = 3 diff --git a/openbsd/openbsd_builder.py b/openbsd/openbsd_builder.py index 7778796..537e204 100644 --- a/openbsd/openbsd_builder.py +++ b/openbsd/openbsd_builder.py @@ -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 #include + #include + #include """) if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index 3226321..4ccd796 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -cffi==1.12.3 -pycparser==2.19 +cffi==1.17.1 +pycparser==2.22 diff --git a/setup.py b/setup.py index 3b9a1c4..0daf0b6 100644 --- a/setup.py +++ b/setup.py @@ -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", @@ -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", @@ -34,7 +31,6 @@ "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Operating System :: POSIX :: BSD :: OpenBSD", - "License :: OSI Approved :: BSD License", ], )