ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.