LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Memory mapped file descriptor opened after execl is empty but should have content (https://www.linuxquestions.org/questions/linux-general-1/memory-mapped-file-descriptor-opened-after-execl-is-empty-but-should-have-content-4175433700/)

kickuindajunk 10-23-2012 11:43 AM

Memory mapped file descriptor opened after execl is empty but should have content
 
Hi guys,

I am working on sharing memory to a execl'd process using mmap MAP_SHARED memory. I open a file before execl, mmap the returned fd, write to the region, leave the fd open and pass the opened fd to the execl'd process. I believe I have a lapse in my understanding how MMAP works under-the-hood and as a result I'm not seeing the content in my execl'd process.

Here is the basic logic/order of my code:

open("/dev/null", fd);
addr = mmap(NULL, sb.size, PROTO_READ|PROTO_WRITE, MAP_SHARED, fd, 0);
memcpy(addr, stuff, size(stuff));
//addr now has content
execl("./test", "test", fd_str, NULL);

In test:
new_addr=mmap(NULL, sb.size, PROTO_READ|PROTO_WRITE, MAP_SHARED, atoi(fd_str), 0);
//new_addr is all empty.

Let me know if this is the right forum for this question. Thanks for your help.

just an FYI, from the man pages for execve, file descriptors are left open on execve unless FD_CLOEXEC is set.

neonsignal 11-03-2012 10:47 AM

If you are wanting to share via a file backed memory map, then you cannot use "/dev/null" as the file backing, it will need to be a real file. Otherwise any data written will end up in... /dev/null!

If you don't want a real file being mapped, you can create an anonymous memory map, but this will not be available to the target of the exec.

Depending on your application, perhaps a more suitable method would be a shared memory segment. By using shmget/shmat, you will be able to pass the shared memory segment on to the new process (and to any child processes). Even nicer, the key token to access it can be made a constant, rather than having to pass it via arguments.


All times are GMT -5. The time now is 02:44 PM.