Can I change an mmap()ing from RO to RW and back? - Unix

This is a discussion on Can I change an mmap()ing from RO to RW and back? - Unix ; Dear Experts, I have a file that's currently mmap()ed read-write into multiple processes. Most of the time the processes only read from it, but they occassionally write, most often appending at the end. This works, but in a paranoid moment ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Can I change an mmap()ing from RO to RW and back?

  1. Can I change an mmap()ing from RO to RW and back?

    Dear Experts,

    I have a file that's currently mmap()ed read-write into multiple
    processes. Most of the time the processes only read from it, but they
    occassionally write, most often appending at the end.

    This works, but in a paranoid moment I became concerned that if a
    process crashed it could write garbage into the file. So I'd like to
    make the mmap()ed region read-only, except during the brief periods when
    it's intending to write to it.

    As far as I can see, it's only possible to do this by unmapping the old
    mapping and calling mmap() again with the appropriate flags. This isn't
    really acceptable as the new mapping could be at a different address.

    Have I missed some way of doing this? My hopes were raised briefly when
    I found mremap, but that does something different.

    A portable solution would of course be best, but in practice this code
    is only ever going to run on Linux.

    Thanks for any ideas,

    Phil.

  2. Re: Can I change an mmap()ing from RO to RW and back?

    On Mon, 26 May 2008 11:32:18 +0100 Phil wrote:

    | I have a file that's currently mmap()ed read-write into multiple
    | processes. Most of the time the processes only read from it, but they
    | occassionally write, most often appending at the end.
    |
    | This works, but in a paranoid moment I became concerned that if a
    | process crashed it could write garbage into the file. So I'd like to
    | make the mmap()ed region read-only, except during the brief periods when
    | it's intending to write to it.
    |
    | As far as I can see, it's only possible to do this by unmapping the old
    | mapping and calling mmap() again with the appropriate flags. This isn't
    | really acceptable as the new mapping could be at a different address.
    |
    | Have I missed some way of doing this? My hopes were raised briefly when
    | I found mremap, but that does something different.

    When you use the flag MAP_FIXED in argument 4 of mmap(), you specify the
    start address and it will use it, and it will even unmap what was there
    before. If there is some reason it cannot map into that address, you
    would not have gotten it there in the first call to mmap().

    So do the first mmap() with 0 for the start address and let the kernel pick
    the address. Keep the file descriptor open. Then call mmap() with MAP_FIXED
    and PROT_WRITE when you need to carefully change something. Then call mmap()
    again with MAP_FIXED and PROT_READ when done if you need to read from it
    between the writes.


    | A portable solution would of course be best, but in practice this code
    | is only ever going to run on Linux.

    The above uses what is defined in mmap(). So it should be portable. I do
    not know how well it will do in performance. There may be a performance hit
    if you do the remaps often.

    --
    |WARNING: Due to extreme spam, googlegroups.com is blocked. Due to ignorance |
    | by the abuse department, bellsouth.net is blocked. If you post to |
    | Usenet from these places, find another Usenet provider ASAP. |
    | Phil Howard KA9WGN (email for humans: first name in lower case at ipal.net) |

  3. Re: Can I change an mmap()ing from RO to RW and back?

    Phil writes:
    > I have a file that's currently mmap()ed read-write into multiple
    > processes. Most of the time the processes only read from it, but they
    > occassionally write, most often appending at the end.
    >
    > This works, but in a paranoid moment I became concerned that if a
    > process crashed it could write garbage into the file. So I'd like to
    > make the mmap()ed region read-only, except during the brief periods
    > when it's intending to write to it.


    This can be done with 'mprotect'.

  4. Re: Can I change an mmap()ing from RO to RW and back?

    phil-news-nospam@ipal.net writes:
    >On Mon, 26 May 2008 11:32:18 +0100 Phil wrote:
    >
    >| I have a file that's currently mmap()ed read-write into multiple
    >| processes. Most of the time the processes only read from it, but they
    >| occassionally write, most often appending at the end.
    >|
    >| This works, but in a paranoid moment I became concerned that if a
    >| process crashed it could write garbage into the file. So I'd like to
    >| make the mmap()ed region read-only, except during the brief periods when
    >| it's intending to write to it.
    >|
    >| As far as I can see, it's only possible to do this by unmapping the old
    >| mapping and calling mmap() again with the appropriate flags. This isn't
    >| really acceptable as the new mapping could be at a different address.
    >|
    >| Have I missed some way of doing this? My hopes were raised briefly when
    >| I found mremap, but that does something different.
    >
    >When you use the flag MAP_FIXED in argument 4 of mmap(), you specify the
    >start address and it will use it, and it will even unmap what was there
    >before. If there is some reason it cannot map into that address, you
    >would not have gotten it there in the first call to mmap().


    I don't believe that this is a very good idea for the following reasons:

    1) MAP_FIXED is not a standard[*] flag to the mmap call. An implementation
    is free to ignore it, or implement it in any fashion it likes.

    2) If, between the unmap and the subsequent MAP_FIXED a library function
    was called that resulted in a dlopen, which happened to load the
    shared object into memory at the former location of the mmap mapping,
    which the implmentation is perfectly free to do, unexpected results
    may ensue.

    The caller would be much better off using mprotect as Rainer indicated.

    scott
    [*] It is implementation-defined whether MAP_FIXED shall be supported.
    [XSI] [Option Start] MAP_FIXED shall be supported on XSI-conformant systems. [Option End]

  5. Re: Can I change an mmap()ing from RO to RW and back?

    Rainer Weikusat wrote:
    > Phil writes:
    >> I have a file that's currently mmap()ed read-write into multiple
    >> processes. Most of the time the processes only read from it, but they
    >> occassionally write, most often appending at the end.
    >>
    >> This works, but in a paranoid moment I became concerned that if a
    >> process crashed it could write garbage into the file. So I'd like to
    >> make the mmap()ed region read-only, except during the brief periods
    >> when it's intending to write to it.

    >
    > This can be done with 'mprotect'.


    Thanks, that looks like just what I need. I can even make the control
    more fine-grained if I want e.g. when I'm only supposed to be appending
    to the file.


    Phil.

+ Reply to Thread