Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-06-2005, 08:38 AM   #1
LQ Newbie
Registered: Oct 2003
Posts: 18

Rep: Reputation: 0
mprotect() return codes

I'm getting an ENOMEM error from a call to mprotect(), which from the man pages means "Internal Kernel structures couldn't be allocated", which I don't really follow - too obtuse for me.

Because the memory that I'm trying to change the permissions on exists already, I'm not trying to create a new space so what is mprotect doing? And why does it fail?


Old 05-07-2005, 07:37 PM   #2
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
You probably want to pass the protection parameters to mmap() in the first place.

If you're not using mmap(), do be aware that a) this violates the POSIX standard and b) mprotect() works on the entire page (usually around 4kb) that happens to contain the address range you pass in; it's mostly used by the kernel internally, and by mmap().

The problem is that mprotect() uses a kernel-level function that allocates memory in the kernel-equivalent of the heap. Kernel memory allocation is subject to a number of different rules about where in RAM a block of memory can be allocated. If there doesn't happen to be any memory free in the right place, the allocation will fail. (For user-level memory, this would trigger the entire memory page to be swapped out and the memory allocation to be re-tried, but the kernel is primarily concerned with speed and so doesn't normally do this.)

The upshot of this is that allocation of kernel memory isn't guarenteed, and any function that requires allocating kernel memory, like mprotect(), can fail on memory, even if you technically have memory free in your system.

I would put this in a while loop, with an exit condition on timeout or when not returning ENOMEM. It might also help you to usleep() or schedule during the loop, as this will at least give the kernel a chance to swap out some RAM.
Old 05-09-2005, 07:38 AM   #3
LQ Newbie
Registered: Oct 2003
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks for the reply.

Thing is, I'm using mprotect() to (try and) change the access rights of the current executing process, the memory the program itself is loaded into, so will waiting around actually be of any benefit? Because surely the kernel has already allocated the memory in the right place?

I was a little perplexed by the whole thing because I've tried the program on two machines, on two different operating systems and there hasn't been a problem. But on a third machine I keep getting this ENOMEM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
PPP library & pppd, chat return codes ruchika Linux - Networking 1 03-08-2011 02:50 AM
Are the hex codes for colors in a jpg the same codes as used in html? abefroman Linux - Security 3 07-31-2005 03:21 PM
return codes in c exvor Programming 4 01-21-2005 08:45 PM
mprotect Damaged Soul Programming 0 11-28-2004 05:18 AM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration