Skip to content

Instantly share code, notes, and snippets.

@khaotik
Created May 2, 2021 14:38
Show Gist options
  • Save khaotik/76f2d9ed311bed22ca6594e338849cf2 to your computer and use it in GitHub Desktop.
Save khaotik/76f2d9ed311bed22ca6594e338849cf2 to your computer and use it in GitHub Desktop.
Simple memfd_create wrapper in python
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