Alex Rivera | Logout

Using dlopen, how can I cope with changes to the library file I have loaded?

Asked 2009-10-15T17:02:22.610
14

I have a program written in C++ which uses dlopen to load a dynamic library (Linux, i386, .so). When the library file is subsequently modified, my program tends to crash. This is understandable, since presumably the file is simply mapped into memory.

My question is: other than simply creating myself a copy of the file and dlopening that, is there way for me to load a shared object which is safe against subsequent modifications, or any way to recover from modifications to a shared object that I have loaded?

Clarification: The question is not "how can I install a new library without crashing the program", it is "if someone who I don't control is copying libraries around, is it possible for me to defend against that?"

Edit
Report

1 Answer

4

It is not possible to defend against someone overwriting your library if they have file write permission.

Because dlopen memory maps the library file, all changes to the file are visible in every process that has it open.

The dlopen function uses memory mapping because it is the most memory efficient way to use shared libraries. A private copy would waste memory.

As others have said, the proper way to replace a shared library in a Unix is to use unlink or rename, not to overwrite the library with a new copy. The install command will do this properly.

answered 2009-11-10T00:00:21.117

Your Answer