HPlogo HP-UX Reference Volume 3 of 5 > m

mprotect(2)

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

NAME

mprotect — set protection of memory mapping

SYNOPSIS

#include <sys/mman.h> int mprotect(void *addr, size_t len, int prot);

DESCRIPTION

The mprotect() function changes the access protections on the mappings specified by the range [addr, addr+len], rounding len up to the next multiple of the page size as returned by sysconf(), to be that specified by prot. Legitimate values for prot are the same as those permitted for mmap() and are defined in <sys/mman.h>:

PROT_READ

Page can be read.

PROT_WRITE

Page can be written.

PROT_EXEC

Page can be executed.

PROT_NONE

Page cannot be accessed.

When mprotect() fails for reasons other than EINVAL, the protections on some of the pages in the range [addr, addr+len] may have been changed.

RETURN VALUE

Upon successful completion, mprotect() returns 0. Otherwise, it returns -1 and sets errno to indicate the error.

ERRORS

The mprotect() function will fail if:

[EACCES]

The prot argument specifies a protection that violates the access permission the process has to the underlying memory object.

[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 which are not mapped.

The mprotect() function may fail if:

[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.

SEE ALSO

mmap(2), sysconf(2), <sys/mman.h>.

CHANGE HISTORY

First released in Issue 4, Version 2.

mprotect HP-UX EXTENSIONS

SYNOPSIS (HP-UX)

int mprotect( caddr_t addr, size_t len, int prot );

DESCRIPTION

If the address range does not correspond to one created by a successful call to mmap(), mprotect() returns an error. prot determines whether read, write, execute, or some combination of accesses are permitted to the data being mapped.

If the address range being modified corresponds to a mapped file that was mapped with MAP_SHARED, mprotect() grants write access permission only if the file descriptor used to map the file was opened for writing. If the address range corresponds to a mapped file that was mapped with the MAP_PRIVATE or the MAP_ANONYMOUS flag, mprotect() grants all requested access permissions.

For example, suppose an error occurs on some page at an addr2; mprotect() may have modified the protections of all whole pages in the range [addr,addr2].

ERRORS

[EINVAL]

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

[EFAULT]

The range specified by [addr, addr+len] (from, and including, addr to, but not including, addr+len) is invalid for a process' address space, or the range specifies one or more unmapped pages.

AUTHOR

mprotect() was developed by HP, AT&T, and OSF.

STANDARDS CONFORMANCE

mprotect(): AES, SVID3

© Hewlett-Packard Development Company, L.P.