I'm wondering... When I'm starting a program X that uses a shared library A, and while the program runs, I modify the shared library on the disk, and run another program Y that depends on the same shared library. Will that program Y will use the already-in-memory version of the shared library, or will it load a distinct instance of the shared library that has the subsequent modifications?
How is it determined whether or not to share a loaded library or reload it from disk?
The dynamic loader just does regular old open(2)
and mmap(2)
calls, and a memory map bumps inode refcounts the same way an open fd does. So if you do the usual atomic-file-replacement trick for the library, write out your changes in a new copy of the file and then rename(2)
it over the old name, anything that's started after that point will pick up the new inode and the new contents, but running programs will keep using the old inode and the old contents.
If you modify the library in place, naturally any programs started after the write
call will pick up your changes. The more interesting question is what happens to processes that already had it mapped. The answer is probably either "the system won't let you do that" or "unspecified, depends on details of the page cache implementation". Poking casually at the Linux implementation (which is what I have to hand): The glibc dynamic loader uses MAP_DENYWRITE
for all its shared library maps, which is not documented but sounds like it means "make this file unmodifiable while the mapping exists". However, I can't find anything in the kernel sources that makes MAP_DENYWRITE
do anything; it might be a historical vestige or some such.
It also uses MAP_PRIVATE
. http://pubs.opengroup.org/onlinepubs/7908799/xsh/mmap.html says that "It is unspecified whether modifications to the underlying object done after the MAP_PRIVATE mapping is established are visible through the MAP_PRIVATE mapping." So you might or might not be able to modify a shared library image underneath a running process, depending on the details of the page cache implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With