PostgreSQL插件hook机制
admin
2023-05-20 15:42:27
0
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);
/* Hook for plugins to get control in ClientAuthentication() */
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;
/*
 * Module Load Callback
 */
void _PG_init(void)
{
    /* Define custom GUC variables */
    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);
    /* Install Hooks */
    original_client_auth_hook = ClientAuthentication_hook;
    ClientAuthentication_hook = auth_delay_checks;
}
/*
如果卸载则调用该函数,实际上是将ClientAuthentication_hook赋回原值
*/
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);
    }
}

相关内容

热门资讯

“控制”格陵兰岛,特朗普罕见列... 据美国《新闻周刊》和英国《独立报》8月2日报道,美国总统特朗普日前在接受采访时声称,格陵兰岛将在他本...
脑机接口,新突破,深度布局的1... (来源:逍遥财情) 8月1日,两项关于脑机接口行业的新国家标准开始实施,这两项标准相当于为整个行业制...
特朗普:与伊朗的谈判已开始,计... 当地时间8月3日,美国总统特朗普在社交平台“真实社交”(Truth Social)发文称,与伊朗的谈...
唐山电缆测绘现场案例 唐山电缆测绘现场案例 探测背景: 本次服务对象为唐山某测绘公司,因后期甲方需开展开挖作业,需提前明确...
聚焦能源与前沿科技!山西启动重... 为持续完善全省科技创新平台体系,夯实基础研究根基、打通科技成果产业化链条,加快培育新质生产力,赋能全...
四川:到2026年底推动阿里云... 《四川省推进算力设施优化布局提质建设和算电融合发展实施方案(2026—2028年)》近日印发。其中提...
瑙鲁正式更改国名 △瑙鲁(资料图)瑙鲁共和国政府日前在社交媒体上发布公告说,该国国名已由“Nauru”正式更改为“Na...
中国科学家首获威廉·诺德伯格奖... 当地时间8月3日,在意大利佛罗伦萨举办的第46届国际空间研究委员会(COSPAR)科学大会上,中国气...
和县科技馆开展“科普邂逅盛夏 ... 为丰富青少年暑期精神文化生活,普及推广自然科学知识,7 月 29 日,和县科技馆组织开展夏日主题科普...
外部链接跳转微信小程序失败的8... 在使用外链这样的三方工具将小程序转成跳转链接推广使用时,在跳转全流程中每一个环节都有可能导致跳转失败...