uktil: add libc.memfd_create() wrapper

This is required for python3.6 where there is no `os.memfd_create()`
yet. Can be removed once we move to python3.8+.
This commit is contained in:
Michael Vogt 2024-08-12 16:37:13 +02:00 committed by Achilleas Koutsou
parent 0abdfb9041
commit 09e78c52d9
2 changed files with 41 additions and 0 deletions

View file

@ -490,6 +490,29 @@ class Libc:
setattr(proto, "errcheck", self._errcheck_errno)
setattr(proto, "__name__", "futimens")
self.futimens = proto
# prototype: _memfd_create() (takes a byte type name)
# (can be removed once we move to python3.8)
proto = ctypes.CFUNCTYPE(
ctypes.c_int, # restype (return type)
ctypes.c_char_p,
ctypes.c_uint,
use_errno=True,
)(
("memfd_create", self._lib),
(
(1, "name"),
(1, "flags", 0),
),
)
setattr(proto, "errcheck", self._errcheck_errno)
setattr(proto, "__name__", "memfd_create")
self._memfd_create = proto
# (can be removed once we move to python3.8)
def memfd_create(self, name: str, flags: int = 0) -> int:
""" create an anonymous file """
char_p_name = name.encode()
return self._memfd_create(char_p_name, flags)
@staticmethod
def make() -> "Libc":

View file

@ -187,6 +187,7 @@ def test_libc():
assert libc0.RENAME_NOREPLACE
assert libc0.RENAME_WHITEOUT
assert libc0.renameat2
assert libc0.memfd_create
def test_libc_renameat2_errcheck():
@ -226,6 +227,23 @@ def test_libc_renameat2_exchange(tmpdir):
assert f.read() == "foo"
def test_libc_memfd_create_errcheck():
libc = linux.Libc.default()
with pytest.raises(OSError) as exc:
libc.memfd_create("foo", -1)
assert "Invalid argument" in str(exc.value)
def test_libc_memfd_create():
libc = linux.Libc.default()
fd = libc.memfd_create("foo", 0)
os.write(fd, b"file content")
fd2 = os.dup(fd)
os.close(fd)
os.lseek(fd2, 0, 0)
assert os.read(fd2, 4096) == b"file content"
def test_proc_boot_id():
#
# Test the `proc_boot_id()` function which reads the current boot-id