Created
May 2, 2021 14:38
-
-
Save khaotik/76f2d9ed311bed22ca6594e338849cf2 to your computer and use it in GitHub Desktop.
Simple memfd_create wrapper in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class memfd_create: | |
'''create in memory file with actual fileno using memfd_create | |
usage: | |
with memfd_create('file_name') as f: | |
pass # treat f as file-like in binary mode | |
''' | |
import ctypes, os | |
_libc = ctypes.CDLL(None) | |
_syscall = _libc.syscall | |
def __init__(self, name:str): | |
self.fd = self._syscall(319, name, 1) | |
def __enter__(self): | |
self.fp = self.os.fdopen(self.fd, 'r+b') | |
return self.fp | |
def __exit__(self, err_type, err_value, traceback): | |
self.fp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment