Using mmap over a file

I am trying to allow two different processes to communicate using the memory mapping of the same file. However, I am having some problems with this. I have a feeling that this is due to the way I use the open () call and pass my file descriptor to mmap.

Here is my code, do you see something wrong?

Object Code 1:

16 FILE* temp = fopen(theSharedFileName, "w"); 17 fseek(temp, fileSize-1, SEEK_SET); 18 fprintf(temp, "0"); // make the file a certain size 19 fseek(temp, 0, SEEK_CUR); 20 21 int sharedFileName = fileno(temp); ... 31 sharedArea = (MyStruct*)mmap(0, fileSize, 32 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, sharedFileName, 0); 

I use the "w" file mode since Object 1 will ever be created once, and I want it to reset to have any previously existing data.

Object Code 2:

  130 FILE* tempFile = fopen(sharedFileName, "a"); 131 int theFile = fileno(tempFile); ... 135 sharedArea = (MyStruct*)mmap(NULL, fileSize, 136 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, theFile, 0); 
+4
source share
3 answers

A few questions:

  • Avoid mixing high-level I / O operations (fopen (), fseek ()) and some low-level operations such as mmap (). Although you can get a low-level file descriptor using fileno (), it's like taking the longest route to get to the same place. Also, just using mmap () breaks compatibility outside of BSD and POSIX, so you get nothing using the standard C I / O functions. Just use open () and lseek () directly.
  • It makes no sense to use stream formatted input-output (fprintf ()) in the same file as memory mapping. When you save a memory card, you implicitly tell the system that you intend to use it as data with direct access (direct indexing). fprintf () to output the stream, you usually use it for sequential access. In fact, although it is possible, it is unusual to see fprintf () and fseek () in the same descriptor (this is not even portable, but due to the previous item, I do not consider portability).
  • The protection must match the protection of the open file. Since you pass "w" to fopen () and PROT_READ | PROT_WRITE | PROT_EXEC PROT_READ | PROT_WRITE | PROT_EXEC PROT_READ | PROT_WRITE | PROT_EXEC in mmap (), you are breaking this restriction. This also indicates why you shouldn't mix high-level I / O with memory mapping: how do you guarantee that fopen(...,"w") will open a file with the correct flags? This is supposed to be the "implementation-detail" for the C library. If you want to copy a card with a file with read and write permissions, you must use the low-level open(theSharedFileName, O_RDWR) to open the file.
  • Do not use PROT_WRITE and PROT_EXEC together. It is not tolerated and , this is a security risk. Read about W ^ X and executable space protection .
+24
source

If you can use C ++ and libraries such as ACE or Boost that protect you from low-level details and provide an easier abstraction for IPC .

+2
source

As others have said, do not use fopen () and friends for this.

Part of the problem you are experiencing may be due to the fact that fprintf () may have streaming buffers, so it may not actually modify the file and therefore be visible to another process if expected. You can add fflush (), but read () and write () do not perform application-level buffering, which is part of why they are more suitable.

+1
source

All Articles