linux: add Libc accessor with renameat2(2)

Add a new utility that wraps ctypes.CDLL() for the self-embedded
libc.so. Initially, it only exposes renameat2(2), but more can be added
when needed in the future.

The Libc class is very similar to the existing LibCap class, with a
similar instantiation logic with singleton access.

In the future, the Libc class will allow access to other system calls
and libc.so functionality, when needed.
This commit is contained in:
David Rheinsberg 2022-09-23 10:03:40 +02:00 committed by Christian Kellner
parent ebbedd1e89
commit efe4ad4b92
2 changed files with 123 additions and 0 deletions

View file

@ -167,6 +167,63 @@ def test_fcntl_flock():
os.close(fd2)
def test_libc():
#
# Test that the Libc class can be instantiated and provides a suitable
# default singleton. Verify the expected interfaces exist (though tests
# for them are separate).
#
libc0 = linux.Libc.make()
libc1 = linux.Libc.default()
assert libc0 is not libc1
assert libc1 is linux.Libc.default()
assert libc0.AT_FDCWD
assert libc0.RENAME_EXCHANGE
assert libc0.RENAME_NOREPLACE
assert libc0.RENAME_WHITEOUT
assert libc0.renameat2
def test_libc_renameat2_errcheck():
#
# Verify the `renameat(2)` system call on `Libc` correctly turns errors into
# python exceptions.
#
libc = linux.Libc.default()
with pytest.raises(OSError):
libc.renameat2(oldpath=b"", newpath=b"")
def test_libc_renameat2_exchange(tmpdir):
#
# Verify the `renameat(2)` system call on `Libc` with the
# `RENAME_EXCHANGE` flag. This swaps two files atomically.
#
libc = linux.Libc.default()
with open(f"{tmpdir}/foo", "x", encoding="utf8") as f:
f.write("foo")
with open(f"{tmpdir}/bar", "x", encoding="utf8") as f:
f.write("bar")
libc.renameat2(
oldpath=f"{tmpdir}/foo".encode(),
newpath=f"{tmpdir}/bar".encode(),
flags=linux.Libc.RENAME_EXCHANGE,
)
with open(f"{tmpdir}/foo", "r", encoding="utf8") as f:
assert f.read() == "bar"
with open(f"{tmpdir}/bar", "r", encoding="utf8") as f:
assert f.read() == "foo"
def test_proc_boot_id():
#
# Test the `proc_boot_id()` function which reads the current boot-id