Home:ALL Converter>Linux shared library is being loaded twice

Linux shared library is being loaded twice

Ask Time:2016-06-29T04:32:23         Author:ngoncalves

Json Formatter

I have the following setup:

  • a quite complex Qt+QML based application
  • a shared linux library that also has some Qt functionality

The shared library is injected onto the application using the LD_PRELOAD trick. Once loaded it starts a TCP server, through which it exposes the application internal objects. The goal is to access the application internals without specifically modifying the source code of the application.

The strange thing that I am seeing is that the shared library is being loaded twice and I cannot understand why. Since both the library and the application depend on Qt, I would understand if Linux would load multiple copies of the same Qt libraries.

But the application does not depend on the shared library and I am not overriding any functions on the application itself.

I would like to know:

  1. how to prevent the shared library from being loaded twice (I was thinking about using shell environment variables but it seems an ugly hack)

  2. what could possibly cause the shared library to be loaded twice

Edit

The comments from Employed-Russian (see below) steered me in the right direction for solving question #2. The target application is launching a child process which inherits the environment variables, LD_PRELOAD included. The child process is the reason why the library is being loaded twice.

As for question #1, I also followed his suggestion: the library initialization function simply unsets the environment variable LD_PRELOAD. As a result, child processes no longer reload the library.

Author:ngoncalves,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38085775/linux-shared-library-is-being-loaded-twice
yy