一般的plugin都是以动态库(可能还会需要些其它资源,这些其它内容我们不关心)形式呈现的,在宿主程序运行期间或启动时加载。
在Linux环境中,负责动态加载共享库的核心开发库是dl库,加载器是ld。dl库提供如下API用于程序运行时加载动态库:
1. void *dlopen(const char *, int flag);
2. void *dlsym(void *handle, const char *symbol);
3. int dllcose(void *handle);
4. char *dlerror();
这些函数这里不解释,man dlopen可以找到相应的文档。
下面是个示例,展示了在Linux环境中,通过主程序(hello_main)调用plugin(hello_ext)的实现方法:
1. hello.h文件(接口)
#ifndef _HELLO_H_
#define _HELLO_H_
typedef void process_fn(int a, int b);
typedef void post_fn(const char *);
struct module_entry
{
process_fn *process;
post_fn *post;
};
typedef void get_entry_fn(struct module_entry *);
#endif //_HELLO_H_
2. hello_main.c文件(主程序)
#include "hello.h"
#include
#include
#include
void println(const char *msg)
{
puts(msg);
putchar('\n');
}
int main(int argc, char *argv[])
{
if (argc != 3)
return -1;
const char *module_name = "hello_ext.so";
void *handle = dlopen(module_name, RTLD_LAZY);
get_entry_fn *getter = dlsym(handle, "get_module");
struct module_entry entry;
entry.post = println;
entry.process = NULL;
getter(&entry);
if (entry.process)
entry.process(atoi(argv[1]), atoi(argv[2]));
return 0;
}
3.hello_ext.c文件(plugin)
#include "hello.h"
#include
#include
//#include
void add(int a, int b);
static struct module_entry hello_ext_module = {
add,
NULL
};
void get_module(struct module_entry *entry)
{
hello_ext_module.post = entry->post;
entry->process = hello_ext_module.process;
// const char *module_name = "hello_main";
// void *handle = dlopen(module_name, RTLD_LAZY);
// get_entry_fn *getter = dlsym(handle, "get_module");
}
void add(int a, int b)
{
char s[64] = {0};
sprintf(s, "%d + %d = %d", a, b, a + b);
if (hello_ext_module.post)
hello_ext_module.post(s);
}
4.mk.sh(简单的编译脚本)
#!/bin/sh
rm -f *.o *.so hello_main
gcc *.c -c -fPIC -g
gcc hello_main.o -o hello_main -fPIC -g -ldl
gcc hello_ext.o -o hello_ext.so -fPIC -g -shared -ldl
编译(sh mk.sh)完成后,执行
./hello_main 3 4
主程序hello_main会打开hello_ext.so库,加载并执行get_module函数,得到一个module_entry(同时传递了一个post处理器给plugin),再调用plugin的process处理。
特别地,plugin在process后(add函数内),又反过来调用了主程序hello_main.c中定义的println函数--这是种常用的callback模式。
然而,查了下PHP的接口代码,发现在写PHP扩展时,新写的PHP既不需要链接PHP核心的库,也不需要动态加载核发接口的函数库(同时貌似也没有我这里定义的post回调函数),比如zend_parse_parmeters函数, 这个函数定义在zend_API.c中,而PHP在执行时,当发现需要用新的扩展时,却可以正常调用扩展,扩展也可以正常调用核心代码(如zend_parse_parmeters).不知道具体是怎么实现的。如果哪位高手知道,麻烦告知(在so/dll中调用主程序中函数的方式,非callback方式)。
最后,发现如果把println函数分离出hell_main, 做成一个动态库(hello.so),然后hello_main静态链接这个库,plugin hello_ext是可以直接调用println函数的。
而如果是动态链接(通过dlopen("hello.so", RTLD_LAZY/RTLD_NOW)),plugin hello_ext也无法直接调用println函数。
但ldd php发现,php主程序本身并没有链接php的什么核心库,不明白怎么回事。