[Previous] [Contents] [Next]

mprotect()

Change memory protection

Synopsis:

#include <sys/mman.h>

int mprotect( const void * addr, 
              size_t len,
              int prot );

Library:

libc

Description:

The mprotect() function changes the access protections on any mappings residing in the range starting at addr, and continuing for len bytes.

The prot parameter specifies the new access capabilities to the mapped memory region(s). The bits that can be combined in this field are defined in <sys/mman.h> and listed below:

PROT_EXEC
The region can be executed.
PROT_NOCACHE
Disable caching of the region (for example, can be used to access dual ported memory).
PROT_NONE
The region can't be accessed.
PROT_READ
The region can be read.
PROT_WRITE
The region can be written.

Returns:

0
Success.
-1
An error occurred (errno is set).

Note: If mprotect() fails, the protections on some of the pages in the address range starting at addr and continuing for len bytes may have been changed.

Errors:

EACCES
The memory object wasn't opened for read, regardless of the protection specified.

The memory object wasn't opened for write, and PROT_WRITE was specified for a MAP_SHARED type mapping.

EAGAIN
The prot argument specifies PROT_WRITE on a MAP_PRIVATE mapping and there's insufficient memory resources to reserve for locking the private pages (if required).
ENOMEM
The addresses in the range starting at addr and continuing for len bytes are outside the range allowed for the address space of a process, or specify one or more pages that are not mapped.

The prot argument specifies PROT_WRITE on a MAP_PRIVATE mapping, and locking the private pages (if required) would need more space than the system can supply to reserve for doing so.

ENOSYS
The function mprotect() isn't supported by this implementation.

Classification:

POSIX 1003.1 (Realtime Extensions)

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

errno, mmap(), munmap(), shm_open(), shm_unlink()


[Previous] [Contents] [Next]