[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 1


mprotect Subroutine

Purpose

Modifies access protections for memory mapping.

Library

Standard C Library (libc.a)

Syntax

#include <sys/types.h>
#include <sys/mman.h>


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

Description

The mprotect subroutine modifies the access protection of a mapped file region or anonymous memory region created by the mmap subroutine.

Parameters

addr
Specifies the address of the region to be modified. Must be a multiple of the page size returned by the sysconf subroutine using the _SC_PAGE_SIZE value for the Name parameter.

len
Specifies the length, in bytes, of the region to be modified. If the len parameter is not a multiple of the page size returned by the sysconf subroutine using the _SC_PAGE_SIZE value for the Name parameter, the length of the region will be rounded off to the next multiple of the page size.

prot
Specifies the new access permissions for the mapped region. Legitimate values for the prot parameter are the same as those permitted for the mmap (mmap or mmap64 Subroutine) subroutine, as follows:

PROT_READ
Region can be read.

PROT_WRITE
Region can be written.

PROT_EXEC
Region can be executed.

PROT_NONE
Region cannot be accessed.

 

Return Values

When successful, the mprotect subroutine returns 0. Otherwise, it returns -1 and sets the errno global variable to indicate the error.

Note: The return value for mprotect is 0 if it fails because the region given was not created by mmap unless XPG 1170 behavior is requested by setting the environment variable XPG_SUS_ENV to ON.

Error Codes

Attention: If the mprotect subroutine is unsuccessful because of a condition other than that specified by the EINVAL error code, the access protection for some pages in the (addr, addr + len) range may have been changed.

If the mprotect subroutine is unsuccessful, the errno global variable may be set to one of the following values:

EACCES The prot parameter specifies a protection that conflicts with the access permission set for the underlying file.
EINVAL The prot parameter is invalid, or the addr parameter is not a multiple of the page size as returned by the sysconf subroutine using the _SC_PAGE_SIZE value for the Name parameter.

ENOMEM The application has requested X/Open UNIX95 Specification compliant behavior and addresses in the range are invalid for the address space of the process or specify one or more pages which are not mapped.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]