loop: don't leak open fd to /dev

Close the file descriptor to `/dev` when we opened it.
This commit is contained in:
Lars Karlitski 2020-01-19 10:44:40 +01:00 committed by Tom Gundersen
parent 977f0a465b
commit 47dc1b5b92

View file

@ -1,3 +1,4 @@
import contextlib
import ctypes
import fcntl
import os
@ -92,11 +93,15 @@ class Loop:
expected device node
"""
if not dir_fd:
dir_fd = os.open("/dev", os.O_DIRECTORY)
self.devname = f"loop{minor}"
self.minor = minor
self.fd = os.open(self.devname, os.O_RDWR, dir_fd=dir_fd)
with contextlib.ExitStack() as stack:
if not dir_fd:
dir_fd = os.open("/dev", os.O_DIRECTORY)
stack.callback(lambda: os.close(dir_fd))
self.fd = os.open(self.devname, os.O_RDWR, dir_fd=dir_fd)
info = os.stat(self.fd)
if ((not stat.S_ISBLK(info.st_mode)) or
(not os.major(info.st_rdev) == self.LOOP_MAJOR) or