Function nc::mprotect[][src]

pub fn mprotect(addr: usize, len: size_t, prot: i32) -> Result<(), Errno>
Expand description

Set protection on a region of memory.

// Initialize an anonymous mapping with 4 pages.
let map_length = 4 * nc::PAGE_SIZE;
let addr = nc::mmap(
    0,
    map_length,
    nc::PROT_READ | nc::PROT_WRITE,
    nc::MAP_PRIVATE | nc::MAP_ANONYMOUS,
    -1,
    0,
);
assert!(addr.is_ok());
let addr = addr.unwrap();

// Set the third page readonly. And we will run into SIGSEGV when updating it.
let ret = nc::mprotect(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::PROT_READ);
assert!(ret.is_ok());

assert!(nc::munmap(addr, map_length).is_ok());