mprotect

Modifies access protections of memory mapping.

Format

#include  <mman.h>

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

Arguments

addr
The address of the region that you want to modify.
len
The length in bytes of the region that you want to modify.
prot
Access permission, as defined in the <mman.h> header file. Specify either PROT_NONE, PROT_READ, or PROT_WRITE.

Description

This function modifies the access protection of a mapped file or shared memory region.

The addr and len arguments specify the address and length in bytes of the region that you want to modify. The len argument must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE). If len is not a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE), the length of the region is rounded up to the next multiple of the page size.

The prot argument specifies access permissions for the mapped region. Specify one of the following:
PROT_NONE  No access 
PROT_READ  Read-only 
PROT_WRITE  Read/Write access 

The mprotect function does not modify the access permission of any region that lies outside of the specified region, except that the effect on addresses between the end of the region, and the end of the page containing the end of the region, is unspecified.

If the mprotect function fails under a condition other than that specified by EINVAL, the access protection of some of the pages in the range [addr, addr + len] can change. Suppose the error occurs on some page at an addr2; mprotect can modify protections of all whole pages in the range [addr, addr2].

See also sysconf in this section.

Return Values
Indicates success. 
-1  Indicates an error; errno is set to one of the following values:

  • EACCESS - The prot argument specifies a protection that conflicts with the access permission set for the underlying file.

  • EINVAL - The prot argument is invalid, or the addr argument is not a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE).

  • EFAULT - The range [addr, addr + len] includes an invalid address.
 


Previous Page | Next Page | Table of Contents | Index