I am writing a function to check if a .so is loaded under linux with the following code:
#include <iostream>
#include <dlfcn.h>
#include <unistd.h>
using namespace std;
bool isLibraryLoaded(const string& libPath)
{
return (nullptr != dlopen(libPath.c_str(), RTLD_NOW | RTLD_NOLOAD));
}
int main(int argc, char** argv)
{
const string libPath = "/path/to/library.so";
cout << "loaded: " << isLibraryLoaded(libPath) << endl;
sleep(8); // first execution of lsof
// load lib
void* handle = dlopen(libPath.c_str(), RTLD_NOW);
cout << "handle: " << handle << endl;
if (nullptr != handle)
{
cout << "loaded: " << isLibraryLoaded(libPath) << endl;
}
else
{
cout << "error: " << dlerror() << endl;
}
sleep(8); // second execution of lsof
// unload lib
if (0 == dlclose(handle))
{
cout << "loaded: " << isLibraryLoaded(libPath) << endl;
}
else
{
cout << "error: " << dlerror() << endl;
}
sleep(8); // third execution of lsof
return 0;
}
output of the program:
loaded: 0
handle: 0x6420b0
loaded: 1
loaded: 1
first execution of lsof:
# lsof /path/to/library.so
(return -1)
second execution of lsof:
# lsof /path/to/library.so
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
test 3240 me mem REG 8,33 333493040 5242954 /path/to/library.so
third execution of lsof:
# lsof /path/to/library.so
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
test 3240 me mem REG 8,33 333493040 5242954 /path/to/library.so
The program is very simple, the library should be loaded and unloaded again. But it is NOT. I am very confusing and have the following questions:
- Is there something wrong with my code?
- Why does dlclose() not really unload the library?
- What could be the potential reasons?
- Is there any way for C/C++ to detect if a library is loaded/unloaded?
My environment:
g++ (Ubuntu 6.4.0-17ubuntu1~16.04) 6.4.0 20180424
RTLD_NOLOADis not being honoured. It's documented as needing glibc 2.2 - do you have that? - Paul Sanders