mprotect() — Set protection of memory mapping
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4.2 |
both |
Format
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/mman.h>
int mprotect(void *addr, size_t len, int prot);
General description
- PROT_READ
- page can be read
- PROT_WRITE
- page can be written
- PROT_EXEC
- page can be executed
- PROT_NONE
- page cannot be accessed
The range provided by the Map_address and Map_length may span regular maps as well as __MAP_MEGA maps. Mprotect affects __MAP_MEGA maps very differently than regular maps. The difference is in the scope of the change. When a change is made to a __MAP_MEGA map, the change affects all processes which are currently mapped to the same file-offset range represented by the pages within the provided range. For example, changing a file-offset range (storage pages) that is currently in use with a protection of write to a protection of read, makes the file-offset range read for all processes, not just the current one. In other words, the changes are global. On the other hand, changes to regular maps affect only the process that issues mprotect.
When mprotect() fails for reasons other than EINVAL, the protection on some of the pages in the range [addr, addr + len) may have been changed.
Returned value
If successful, mprotect() returns 0.
- Error Code
- Description
- EACCES
- The prot argument specifies a protection that violates the access permission the process has to the underlying memory object.
- EAGAIN
- The prot argument specifies PROT_WRITE over a MAP_PRIVATE mapping and there are insufficient memory resources to reserve for locking the private page.
- EINVAL
- The addr argument is not a multiple of the page size as returned by sysconf().
- ENOMEM
- Addresses in the range [addr, addr + len) are invalid for the address space of a process, or specify one or more pages