Python mmap.PROT_WRITE Examples

The following are 30 code examples of mmap.PROT_WRITE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mmap , or try the search function .
Example #1
Source File: simple-touch.py    From pywayland with Apache License 2.0 8 votes vote down vote up
def create_shm_buffer(touch, width, height):
    stride = width * 4
    size = stride * height

    with tempfile.TemporaryFile() as f:
        f.write(b"\x64" * size)
        f.flush()

        fd = f.fileno()
        touch["data"] = mmap.mmap(
            fd, size, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE
        )
        pool = touch["shm"].create_pool(fd, size)
        touch["buffer"] = pool.create_buffer(
            0, width, height, stride, WlShm.format.argb8888.value
        )
        pool.destroy() 
Example #2
Source File: motordevice.py    From ev3 with Apache License 2.0 6 votes vote down vote up
def open_device():
    global _initialized
    if not _initialized:
        global _pwmfile
        _pwmfile = os.open(lms2012.PWM_DEVICE_NAME, os.O_RDWR | os.O_SYNC)
        global _motorfile
        _motorfile = os.open(lms2012.MOTOR_DEVICE_NAME, os.O_RDWR | os.O_SYNC)
        MOTORDATAArrray = lms2012.MOTORDATA * 4
        global _motormm
        _motormm = mmap(fileno=_motorfile, length=sizeof(MOTORDATAArrray),
                        flags=MAP_SHARED, prot=PROT_READ | PROT_WRITE, offset=0)
        global _motordata
        _motordata = MOTORDATAArrray.from_buffer(_motormm)
        _initialized = True
        os.write(_pwmfile, struct.pack('B', lms2012.opPROGRAM_START))

# This does not look required. Types are TACHO (for the big engines) and MINITACHO for the gun one
# and firmware should recognize them properly.
# def set_types(types):
#    os.write(_pwmfile, struct.pack('5B', lms2012.opOUTPUT_SET_TYPE, *types)) 
Example #3
Source File: RemoteGraphicsView.py    From tf-pose with Apache License 2.0 6 votes vote down vote up
def __init__(self, *args, **kwds):
        ## Create shared memory for rendered image
        #pg.dbg(namespace={'r': self})
        if sys.platform.startswith('win'):
            self.shmtag = "pyqtgraph_shmem_" + ''.join([chr((random.getrandbits(20)%25) + 97) for i in range(20)])
            self.shm = mmap.mmap(-1, mmap.PAGESIZE, self.shmtag) # use anonymous mmap on windows
        else:
            self.shmFile = tempfile.NamedTemporaryFile(prefix='pyqtgraph_shmem_')
            self.shmFile.write(b'\x00' * (mmap.PAGESIZE+1))
            fd = self.shmFile.fileno()
            self.shm = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
        atexit.register(self.close)
        
        GraphicsView.__init__(self, *args, **kwds)
        self.scene().changed.connect(self.update)
        self.img = None
        self.renderTimer = QtCore.QTimer()
        self.renderTimer.timeout.connect(self.renderView)
        self.renderTimer.start(16) 
Example #4
Source File: qemu.py    From redqueen with GNU Affero General Public License v3.0 6 votes vote down vote up
def init(self):
        self.control = safe_socket(socket.AF_UNIX)
        self.control.settimeout(None)
        self.control.setblocking(1)
        while True:
            try:
                self.control.connect(self.control_filename)
                break
            except socket_error:
                pass

        self.kafl_shm_f     = os.open(self.bitmap_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT)
        self.fs_shm_f       = os.open(self.payload_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(self.kafl_shm_f, self.bitmap_size)
        os.ftruncate(self.fs_shm_f, (128 << 10))

        self.kafl_shm       = mmap.mmap(self.kafl_shm_f, self.bitmap_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        self.fs_shm         = mmap.mmap(self.fs_shm_f, (128 << 10),  mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)

        return True 
Example #5
Source File: qemu.py    From redqueen with GNU Affero General Public License v3.0 6 votes vote down vote up
def __set_binary(self, filename, binaryfile, max_size):
        shm_fd = os.open(filename, os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(shm_fd, max_size)
        shm = mmap.mmap(shm_fd, max_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        shm.seek(0x0)
        shm.write('\x00' * max_size)
        shm.seek(0x0)

        f = open(binaryfile, "rb")
        bytes = f.read(1024)
        if bytes:
            shm.write(bytes)
        while bytes != "":
            bytes = f.read(1024)
            if bytes:
                shm.write(bytes)

        shm.flush()
        f.close()
        shm.close()
        os.close(shm_fd) 
Example #6
Source File: mappings.py    From manticore with GNU Affero General Public License v3.0 6 votes vote down vote up
def mmap(fd, offset, size):
    prot = MMAP.PROT_READ | MMAP.PROT_WRITE
    flags = MMAP.MAP_PRIVATE

    # When trying to map the contents of a file into memory, the offset must be a multiple of the page size (see
    # `man mmap`). So we need to align it before passing it to mmap(). Doing so also increases the size of the memory
    # area needed, so we need to account for that difference.
    aligned_offset = offset & ~0xFFF
    size += offset - aligned_offset

    if size & 0xFFF != 0:
        size = (size & ~0xFFF) + 0x1000
    assert size > 0

    result = mmap_function(0, size, prot, flags, fd, aligned_offset)
    assert result != ctypes.c_void_p(-1).value

    # Now when returning the pointer to the user, we need to skip the corrected offset so that the user doesn't end up
    # with a pointer to another region of the file than the one they requested.
    return ctypes.cast(result + offset - aligned_offset, ctypes.POINTER(ctypes.c_char)) 
Example #7
Source File: master.py    From redqueen with GNU Affero General Public License v3.0 6 votes vote down vote up
def wipe(self):
        filter_bitmap_fd = os.open("/dev/shm/kafl_filter0", os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(filter_bitmap_fd, self.config.config_values['BITMAP_SHM_SIZE'])
        filter_bitmap = mmap.mmap(filter_bitmap_fd, self.config.config_values['BITMAP_SHM_SIZE'], mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        for i in range(self.config.config_values['BITMAP_SHM_SIZE']):
            filter_bitmap[i] = '\x00'
        filter_bitmap.close()
        os.close(filter_bitmap_fd)

        filter_bitmap_fd = os.open("/dev/shm/kafl_tfilter", os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(filter_bitmap_fd, 0x1000000)
        filter_bitmap = mmap.mmap(filter_bitmap_fd, 0x1000000, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        for i in range(0x1000000):
            filter_bitmap[i] = '\x00'
        filter_bitmap.close()
        os.close(filter_bitmap_fd) 
Example #8
Source File: RemoteGraphicsView.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, *args, **kwds):
        ## Create shared memory for rendered image
        #pg.dbg(namespace={'r': self})
        if sys.platform.startswith('win'):
            self.shmtag = "pyqtgraph_shmem_" + ''.join([chr((random.getrandbits(20)%25) + 97) for i in range(20)])
            self.shm = mmap.mmap(-1, mmap.PAGESIZE, self.shmtag) # use anonymous mmap on windows
        else:
            self.shmFile = tempfile.NamedTemporaryFile(prefix='pyqtgraph_shmem_')
            self.shmFile.write(b'\x00' * (mmap.PAGESIZE+1))
            fd = self.shmFile.fileno()
            self.shm = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
        atexit.register(self.close)
        
        GraphicsView.__init__(self, *args, **kwds)
        self.scene().changed.connect(self.update)
        self.img = None
        self.renderTimer = QtCore.QTimer()
        self.renderTimer.timeout.connect(self.renderView)
        self.renderTimer.start(16) 
Example #9
Source File: shell_logger.py    From thefuck with MIT License 6 votes vote down vote up
def shell_logger(output):
    """Logs shell output to the `output`.

    Works like unix script command with `-f` flag.

    """
    if not os.environ.get('SHELL'):
        logs.warn("Shell logger doesn't support your platform.")
        sys.exit(1)

    fd = os.open(output, os.O_CREAT | os.O_TRUNC | os.O_RDWR)
    os.write(fd, b'\x00' * const.LOG_SIZE_IN_BYTES)
    buffer = mmap.mmap(fd, const.LOG_SIZE_IN_BYTES, mmap.MAP_SHARED, mmap.PROT_WRITE)
    return_code = _spawn(os.environ['SHELL'], partial(_read, buffer))

    sys.exit(return_code) 
Example #10
Source File: master.py    From kAFL with GNU General Public License v2.0 6 votes vote down vote up
def wipe(self):
    	filter_bitmap_fd = os.open("/dev/shm/kafl_filter0", os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(filter_bitmap_fd, self.config.config_values['BITMAP_SHM_SIZE'])
        filter_bitmap = mmap.mmap(filter_bitmap_fd, self.config.config_values['BITMAP_SHM_SIZE'], mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        for i in range(self.config.config_values['BITMAP_SHM_SIZE']):
        	filter_bitmap[i] = '\x00'
        filter_bitmap.close()
        os.close(filter_bitmap_fd)

        filter_bitmap_fd = os.open("/dev/shm/kafl_tfilter", os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(filter_bitmap_fd, 0x1000000)
        filter_bitmap = mmap.mmap(filter_bitmap_fd, 0x1000000, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        for i in range(0x1000000):
        	filter_bitmap[i] = '\x00'
        filter_bitmap.close()
        os.close(filter_bitmap_fd) 
Example #11
Source File: RemoteGraphicsView.py    From soapy with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, *args, **kwds):
        ## Create shared memory for rendered image
        #pg.dbg(namespace={'r': self})
        if sys.platform.startswith('win'):
            self.shmtag = "pyqtgraph_shmem_" + ''.join([chr((random.getrandbits(20)%25) + 97) for i in range(20)])
            self.shm = mmap.mmap(-1, mmap.PAGESIZE, self.shmtag) # use anonymous mmap on windows
        else:
            self.shmFile = tempfile.NamedTemporaryFile(prefix='pyqtgraph_shmem_')
            self.shmFile.write(b'\x00' * (mmap.PAGESIZE+1))
            fd = self.shmFile.fileno()
            self.shm = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
        atexit.register(self.close)
        
        GraphicsView.__init__(self, *args, **kwds)
        self.scene().changed.connect(self.update)
        self.img = None
        self.renderTimer = QtCore.QTimer()
        self.renderTimer.timeout.connect(self.renderView)
        self.renderTimer.start(16) 
Example #12
Source File: qemu.py    From kAFL with GNU General Public License v2.0 6 votes vote down vote up
def __set_binary(self, filename, binaryfile, max_size):
        shm_fd = os.open(filename, os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(shm_fd, max_size)
        shm = mmap.mmap(shm_fd, max_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        shm.seek(0x0)
        shm.write('\x00' * max_size)
        shm.seek(0x0)

        f = open(binaryfile, "rb")
        bytes = f.read(1024)
        if bytes:
            shm.write(bytes)
        while bytes != "":
            bytes = f.read(1024)
            if bytes:
                shm.write(bytes)

        f.close()
        shm.close()
        os.close(shm_fd) 
Example #13
Source File: qemu.py    From kAFL with GNU General Public License v2.0 6 votes vote down vote up
def init(self):
        self.control = socket.socket(socket.AF_UNIX)
        while True:
            try:
                self.control.connect(self.control_filename)
                #self.control.connect(self.control_filename)
                break
            except socket_error:
                pass
                #time.sleep(0.01)

        self.kafl_shm_f     = os.open(self.bitmap_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT)
        self.fs_shm_f       = os.open(self.payload_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT)
        #argv_fd             = os.open(self.argv_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(self.kafl_shm_f, self.bitmap_size)
        os.ftruncate(self.fs_shm_f, (128 << 10))
        #os.ftruncate(argv_fd, (4 << 10))

        self.kafl_shm       = mmap.mmap(self.kafl_shm_f, self.bitmap_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
        self.fs_shm         = mmap.mmap(self.fs_shm_f, (128 << 10),  mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)

        return True 
Example #14
Source File: Overclock.py    From PyMenu with GNU General Public License v3.0 6 votes vote down vote up
def setClockPython(newClock):
    with open("/dev/mem", os.O_RDWR) as f:
        CPPCR = (0x10 >> 2)

        m = newClock / 6
        if(m >= 200):
            m = 200
        
        if(m < 4):
            m = 4
        

        try:
            map.mmap(1024, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, 0x10000000) 
            map.seek(CPPCR)
            map.write((m << 24) | 0x090520)
            map.close()
        except Exception as ex:
            print(str(ex)) 
Example #15
Source File: iicdevice.py    From ev3 with Apache License 2.0 5 votes vote down vote up
def open_device():
    global _initialized
    if not _initialized:
        global _devcon
        from . import devcon as _devcon
        global _iicfile
        _iicfile = os.open(lms2012.IIC_DEVICE_NAME, os.O_RDWR)
        global _iicmm
        _iicmm = mmap(fileno=_iicfile, length=sizeof(lms2012.IIC),
                      flags=MAP_SHARED, prot=PROT_READ | PROT_WRITE, offset=0)
        global _iic
        _iic = lms2012.IIC.from_buffer(_iicmm)
        _initialized = True 
Example #16
Source File: snapshot.py    From hase with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, cpu: int, pid: int = -1) -> None:
        # data area must be a multiply of two
        data_size = (2 ** 9) * Libc.PAGESIZE  # == 2097152
        self.pmu = open_pt_event(cpu, pid)
        header_size = Libc.PAGESIZE

        self.buf = MMap(
            self.pmu.fd,
            header_size + data_size,
            mmap.PROT_READ | mmap.PROT_WRITE,
            mmap.MAP_SHARED,
        )

        self.header = MmapHeader(self.buf.addr, data_size)

        # aux area must be a multiply of two
        self.header.aux_size = Libc.PAGESIZE * (2 ** 14)  # == 67108864
        self.aux_buf = MMap(
            self.pmu.fd,
            self.header.aux_size,
            mmap.PROT_READ,
            mmap.MAP_SHARED,
            offset=self.header.aux_offset,
        )

        self.pmu.enable() 
Example #17
Source File: allocators.py    From devito with MIT License 5 votes vote down vote up
def free(self, c_pointer, total_size):
        # unprotect it, since free() accesses it, I think...
        self.lib.mprotect(c_pointer, total_size,
                          ctypes.c_int(mmap.PROT_READ | mmap.PROT_WRITE))
        self.lib.free(c_pointer) 
Example #18
Source File: native_function.py    From PythonForWindows with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_map(cls, size):
        prot = mmap.PROT_EXEC | mmap.PROT_WRITE | mmap.PROT_READ
        return cls(-1, size, prot=prot) 
Example #19
Source File: communicator.py    From redqueen with GNU Affero General Public License v3.0 5 votes vote down vote up
def __get_shm(self, type_id, slave_id):
        if slave_id in self.tmp_shm[type_id]:
            shm = self.tmp_shm[type_id][slave_id]
        else:
            shm_fd = os.open(self.files[type_id] + str(slave_id), os.O_RDWR | os.O_SYNC)
            shm = mmap.mmap(shm_fd, self.sizes[type_id]*self.tasks_per_requests, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
            self.tmp_shm[type_id][slave_id] = shm
        return shm 
Example #20
Source File: qemu.py    From redqueen with GNU Affero General Public License v3.0 5 votes vote down vote up
def open_global_bitmap(self):
        self.global_bitmap_fd = os.open(self.config.argument_values['work_dir'] + "/bitmaps/bitmap", os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(self.global_bitmap_fd, self.bitmap_size)
        self.global_bitmap = mmap.mmap(self.global_bitmap_fd, self.bitmap_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) 
Example #21
Source File: mmio.py    From throttled with MIT License 5 votes vote down vote up
def _open(self, physaddr, size):
        if not isinstance(physaddr, (int, long)):
            raise TypeError("Invalid physaddr type, should be integer.")
        if not isinstance(size, (int, long)):
            raise TypeError("Invalid size type, should be integer.")

        pagesize = os.sysconf(os.sysconf_names['SC_PAGESIZE'])

        self._physaddr = physaddr
        self._size = size
        self._aligned_physaddr = physaddr - (physaddr % pagesize)
        self._aligned_size = size + (physaddr - self._aligned_physaddr)

        try:
            fd = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
        except OSError as e:
            raise MMIOError(e.errno, "Opening /dev/mem: " + e.strerror)

        try:
            self.mapping = mmap.mmap(
                fd, self._aligned_size, flags=mmap.MAP_SHARED, prot=mmap.PROT_WRITE, offset=self._aligned_physaddr)
        except OSError as e:
            raise MMIOError(e.errno, "Mapping /dev/mem: " + e.strerror)

        try:
            os.close(fd)
        except OSError as e:
            raise MMIOError(e.errno, "Closing /dev/mem: " + e.strerror)

    # Methods 
Example #22
Source File: client.py    From HRShell with GNU General Public License v3.0 5 votes vote down vote up
def create_shellcode_Func(restype=c_int64, argtypes=()):
    if not is_os_64bit():
        restype = c_int32
    mm = mmap.mmap(-1, len(shellcode), flags=mmap.MAP_SHARED | mmap.MAP_ANONYMOUS, prot=mmap.PROT_WRITE | mmap.PROT_READ | mmap.PROT_EXEC)
    mm.write(shellcode)
    ctypes_buffer = c_int.from_buffer(mm)
    function = CFUNCTYPE(restype, *argtypes)(addressof(ctypes_buffer))
    function._avoid_gc_for_mmap = mm
    return function 
Example #23
Source File: lcd.py    From ev3 with Apache License 2.0 5 votes vote down vote up
def open_device():
    global _initialized
    if not _initialized:
        global _lcdfile
        _lcdfile = os.open(lms2012.LCD_DEVICE_NAME, os.O_RDWR)
        global lcdmm
        lcdmm = mmap(fileno=_lcdfile, length=MEM_WIDTH * SCREEN_HEIGHT,
                     flags=MAP_SHARED, prot=PROT_READ | PROT_WRITE, offset=0)
    _initialized = True 
Example #24
Source File: uartdevice.py    From ev3 with Apache License 2.0 5 votes vote down vote up
def open_device():
    global _initialized
    if not _initialized:
        global _devcon
        from . import devcon as _devcon
        global _uartfile
        _uartfile = os.open(lms2012.UART_DEVICE_NAME, os.O_RDWR)
        global uartmm
        uartmm = mmap(fileno=_uartfile, length=sizeof(lms2012.UART),
                      flags=MAP_SHARED, prot=PROT_READ | PROT_WRITE, offset=0)
        global _uart
        _uart = lms2012.UART.from_buffer(uartmm)
        _initialized = True 
Example #25
Source File: ui.py    From ev3 with Apache License 2.0 5 votes vote down vote up
def open_device():
    global _initialized
    if not _initialized:
        global _uifile
        _uifile = os.open(lms2012.UI_DEVICE_NAME, os.O_RDWR | os.O_SYNC)
        global _uimm
        _uimm = mmap(fileno=_uifile, length=sizeof(lms2012.UI),
                     flags=MAP_SHARED, prot=PROT_READ | PROT_WRITE, offset=0)
        global _ui
        _ui = lms2012.UI.from_buffer(_uimm)
        _initialized = True 
Example #26
Source File: qemu.py    From kAFL with GNU General Public License v2.0 5 votes vote down vote up
def open_global_bitmap(self):
        self.global_bitmap_fd = os.open(self.config.argument_values['work_dir'] + "/bitmap", os.O_RDWR | os.O_SYNC | os.O_CREAT)
        os.ftruncate(self.global_bitmap_fd, self.bitmap_size)
        self.global_bitmap = mmap.mmap(self.global_bitmap_fd, self.bitmap_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) 
Example #27
Source File: communicator.py    From kAFL with GNU General Public License v2.0 5 votes vote down vote up
def __get_shm(self, type_id, slave_id):
        if slave_id in self.tmp_shm[type_id]:
            shm = self.tmp_shm[type_id][slave_id]
        else:
            shm_fd = os.open(self.files[type_id] + str(slave_id), os.O_RDWR | os.O_SYNC)
            shm = mmap.mmap(shm_fd, self.sizes[type_id]*self.tasks_per_requests, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
            self.tmp_shm[type_id][slave_id] = shm
        return shm 
Example #28
Source File: surface.py    From pywayland with Apache License 2.0 5 votes vote down vote up
def create_buffer(window):
    stride = WIDTH * 4
    size = stride * HEIGHT

    with AnonymousFile(size) as fd:
        window.shm_data = mmap.mmap(
            fd, size, prot=mmap.PROT_READ | mmap.PROT_WRITE, flags=mmap.MAP_SHARED
        )
        pool = window.shm.create_pool(fd, size)
        buff = pool.create_buffer(0, WIDTH, HEIGHT, stride, WlShm.format.argb8888.value)
        pool.destroy()
    return buff 
Example #29
Source File: mmio.py    From python-periphery with MIT License 5 votes vote down vote up
def _open(self, physaddr, size):
        if not isinstance(physaddr, (int, long)):
            raise TypeError("Invalid physaddr type, should be integer.")
        if not isinstance(size, (int, long)):
            raise TypeError("Invalid size type, should be integer.")

        pagesize = os.sysconf(os.sysconf_names['SC_PAGESIZE'])

        self._physaddr = physaddr
        self._size = size
        self._aligned_physaddr = physaddr - (physaddr % pagesize)
        self._aligned_size = size + (physaddr - self._aligned_physaddr)

        try:
            fd = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
        except OSError as e:
            raise MMIOError(e.errno, "Opening /dev/mem: " + e.strerror)

        try:
            self.mapping = mmap.mmap(fd, self._aligned_size, flags=mmap.MAP_SHARED, prot=(mmap.PROT_READ | mmap.PROT_WRITE), offset=self._aligned_physaddr)
        except OSError as e:
            raise MMIOError(e.errno, "Mapping /dev/mem: " + e.strerror)

        try:
            os.close(fd)
        except OSError as e:
            raise MMIOError(e.errno, "Closing /dev/mem: " + e.strerror)

    # Methods 
Example #30
Source File: rpgpio_private.py    From PyCNC with MIT License 5 votes vote down vote up
def __init__(self, phys_address, size=PAGE_SIZE):
        """ Create object which maps physical memory to Python's mmap object.
        :param phys_address: based address of physical memory
        """
        self._size = size
        phys_address -= phys_address % PAGE_SIZE
        fd = self._open_dev("/dev/mem")
        self._memmap = mmap.mmap(fd, size, flags=mmap.MAP_SHARED,
                                 prot=mmap.PROT_READ | mmap.PROT_WRITE,
                                 offset=phys_address)
        self._close_dev(fd)
        atexit.register(self.cleanup)