PostgreSQL插件hook机制
短信预约 -IT技能 免费直播动态提醒
internal_load_library postgresql->
PG_init = (PG_init_t) pg_dlsym(file_scanner->handle, "_PG_init");
if (PG_init)
(*PG_init) ();
internal_unload_library(const char *libname)->
PG_fini = (PG_fini_t) pg_dlsym(file_scanner->handle, "_PG_fini");
if (PG_fini)
(*PG_fini) ();
以ClientAuthentication_hook_type为例
auth.h:
//声明插件使用的函数
extern void ClientAuthentication(Port *port);
typedef void (*ClientAuthentication_hook_type) (Port *, int);
extern PGDLLIMPORT ClientAuthentication_hook_type ClientAuthentication_hook;
auth.c:
//全局变量初始化为NULL,在_PG_init函数中进行初始化赋值,如果该插件加载,则ClientAuthentication_hook为
ClientAuthentication_hook_type ClientAuthentication_hook = NULL;
//如果ClientAuthentication_hook被赋值则执行植入的代码
InitPostgres->PerformAuthentication->ClientAuthentication->
if (ClientAuthentication_hook)
(*ClientAuthentication_hook) (port, status);
auth_delay.c:
static ClientAuthentication_hook_type original_client_auth_hook = NULL;
void _PG_init(void)
{
DefineCustomIntVariable("auth_delay.milliseconds",
"Milliseconds to delay before reporting authentication failure",
NULL,
&auth_delay_milliseconds,
0,
0, INT_MAX / 1000,
PGC_SIGHUP,
GUC_UNIT_MS,
NULL,
NULL,
NULL);
original_client_auth_hook = ClientAuthentication_hook;
ClientAuthentication_hook = auth_delay_checks;
}
void
_PG_fini(void)
{
ClientAuthentication_hook=original_client_auth_hook;
}
static void auth_delay_checks(Port *port, int status)
{
if (original_client_auth_hook)
original_client_auth_hook(port, status);
if (status != STATUS_OK){
pg_usleep(1000L * auth_delay_milliseconds);
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341