2

我在 Linux(Ubuntu 12.04,gcc 4.6.3)上,试图弯曲 dlopen/close 以制作一个基于插件的应用程序,可以在必要时重新加载其插件(例如,如果它们被重新编译) .

基本原理很简单:dlopen插件;使用它,跟踪所有正在使用的符号。当需要重新加载时,清理所有符号并关闭插件。

我拼凑了一个简单的演示应用程序“test.cpp”:

#include <dlfcn.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
    if (argc > 1)
    {
        void* h = dlopen(argv[1], RTLD_NOW|RTLD_LOCAL);
        if (!h)
        {
            cerr << "ERROR: " << dlerror() << endl;
            return 1;
        }
        cin.get();
        if (dlclose(h))
        {
            cerr << "ERROR: " << dlerror() << endl;
            return 2;
        }
        cin.get();
    }
    return 0;
}

编译:

g++ test.cpp -o test -ldl

要制作一个可以作为参数传递给上述代码的简单库,请使用:

touch libtest.cpp && g++ -rdynamic -shared libtest.cpp -o libtest.so

然后运行:

./test ./libtest.so

这就是问题所在;如果在按下 [Enter] 一次后(即在加载和卸载库之后),您运行“pmap”以检查在“test”中加载了哪些库,它会告诉您 libtest.so 仍然存在!现在,尽管 dlclose() 有一个有效的返回,并且没有合理的方式使引用计数在此之前超过 1(这可以通过尝试第二个 dlclose() 来验证 - 它会给出一个错误返回,说明它已经关闭)。

因此,要么 Linux永远不会卸载 dlopen()ed 库(与文档相矛盾),要么“pmap”是错误的。如果是后者,是否有更可靠的方法来确定库是否仍在加载?

4

1 回答 1

2

对于以下程序,我与您的观察结果不同:

// file soq.c
#include <dlfcn.h>
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
using namespace std;
int main(int argc, char** argv)
{
  char cmd[60];
  snprintf(cmd, sizeof(cmd), "pmap %d", getpid());
    if (argc > 1)
    {
        void* h = dlopen(argv[1], RTLD_NOW|RTLD_LOCAL);
        if (!h)
        {
            cerr << "ERROR: " << dlerror() << endl;
            return 1;
        }
    cerr << "after dlopen " << argv[1] << endl;
    system(cmd);
        cin.get();
        if (dlclose(h))
        {
            cerr << "ERROR: " << dlerror() << endl;
            return 2;
        }
        cin.get();
    cerr << "after close " << argv[1] << endl;
    system(cmd);
    }
    return 0;
}

正如预期的那样,我得到了:

% ./soq ./libempty.so
./soq ./libempty.so
after dlopen ./libempty.so
5276:   ./soq ./libempty.so
0000000000400000      8K r-x--  /home/basile/tmp/soq
0000000000601000      4K rw---  /home/basile/tmp/soq
0000000001b4d000    132K rw---    [ anon ]
00007f1dbfd01000      4K r-x--  /home/basile/tmp/libempty.so
00007f1dbfd02000   2044K -----  /home/basile/tmp/libempty.so
00007f1dbff01000      4K rw---  /home/basile/tmp/libempty.so
00007f1dbff02000   1524K r-x--  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc007f000   2048K -----  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc027f000     16K r----  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc0283000      4K rw---  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc0284000     20K rw---    [ anon ]
00007f1dc0289000     84K r-x--  /lib/x86_64-linux-gnu/libgcc_s.so.1
00007f1dc029e000   2048K -----  /lib/x86_64-linux-gnu/libgcc_s.so.1
00007f1dc049e000      4K rw---  /lib/x86_64-linux-gnu/libgcc_s.so.1
00007f1dc049f000    516K r-x--  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc0520000   2044K -----  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc071f000      4K r----  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc0720000      4K rw---  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc0721000    928K r-x--  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0809000   2048K -----  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0a09000     32K r----  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0a11000      8K rw---  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0a13000     84K rw---    [ anon ]
00007f1dc0a28000      8K r-x--  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0a2a000   2048K -----  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0c2a000      4K r----  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0c2b000      4K rw---  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0c2c000    128K r-x--  /lib/x86_64-linux-gnu/ld-2.13.so
00007f1dc0e1c000     20K rw---    [ anon ]
00007f1dc0e49000      8K rw---    [ anon ]
00007f1dc0e4b000      4K r----  /lib/x86_64-linux-gnu/ld-2.13.so
00007f1dc0e4c000      4K rw---  /lib/x86_64-linux-gnu/ld-2.13.so
00007f1dc0e4d000      4K rw---    [ anon ]
00007fff076c3000    132K rw---    [ stack ]
00007fff077b4000      4K r-x--    [ anon ]
ffffffffff600000      4K r-x--    [ anon ]
 total            15984K




after close ./libempty.so
5276:   ./soq ./libempty.so
0000000000400000      8K r-x--  /home/basile/tmp/soq
0000000000601000      4K rw---  /home/basile/tmp/soq
0000000001b4d000    132K rw---    [ anon ]
00007f1dbff02000   1524K r-x--  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc007f000   2048K -----  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc027f000     16K r----  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc0283000      4K rw---  /lib/x86_64-linux-gnu/libc-2.13.so
00007f1dc0284000     20K rw---    [ anon ]
00007f1dc0289000     84K r-x--  /lib/x86_64-linux-gnu/libgcc_s.so.1
00007f1dc029e000   2048K -----  /lib/x86_64-linux-gnu/libgcc_s.so.1
00007f1dc049e000      4K rw---  /lib/x86_64-linux-gnu/libgcc_s.so.1
00007f1dc049f000    516K r-x--  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc0520000   2044K -----  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc071f000      4K r----  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc0720000      4K rw---  /lib/x86_64-linux-gnu/libm-2.13.so
00007f1dc0721000    928K r-x--  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0809000   2048K -----  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0a09000     32K r----  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0a11000      8K rw---  /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
00007f1dc0a13000     84K rw---    [ anon ]
00007f1dc0a28000      8K r-x--  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0a2a000   2048K -----  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0c2a000      4K r----  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0c2b000      4K rw---  /lib/x86_64-linux-gnu/libdl-2.13.so
00007f1dc0c2c000    128K r-x--  /lib/x86_64-linux-gnu/ld-2.13.so
00007f1dc0e1c000     20K rw---    [ anon ]
00007f1dc0e48000     12K rw---    [ anon ]
00007f1dc0e4b000      4K r----  /lib/x86_64-linux-gnu/ld-2.13.so
00007f1dc0e4c000      4K rw---  /lib/x86_64-linux-gnu/ld-2.13.so
00007f1dc0e4d000      4K rw---    [ anon ]
00007fff076c3000    132K rw---    [ stack ]
00007fff077b4000      4K r-x--    [ anon ]
ffffffffff600000      4K r-x--    [ anon ]
 total            13936K 

So you did run your pmap wrongly.

By the way, you could avoid any dlclose-ing in practice, as my manydl.c example demonstrates. In practice, not bothering dlclose-ing means only a tiny address space leak, not a big deal in practice. (You can dlopen nearly a million different shared objects without much hurt).

And to know when a shared object is unloaded, use the "destructor" functions of your dlclose-d plugin (e.g. destructor of static data in C++, or attribute((destructor)) in e.g. C code), because they are called from inside the unloading dlclose.

于 2012-06-14T22:22:42.523 回答