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);
    }
}

相关内容

热门资讯

特朗普刚走,普京火速访华所为何... 作者:艾文2026年5月15日下午,美国总统特朗普的专机刚刚离开北京,莫斯科方面就迫不及待地对外宣布...
伊朗最好适可而止,见好就收 很多人都注意到中美这次达成的共识,抛开经贸层面的不谈,地缘及国际政治层面的,成果也是相当丰富。特朗普...
武大食堂爆满,学生无座可坐 5月13日,武汉大学正式取消社会公众进校预约制度。但校园开放后,不少学生在社交平台上发帖反映,部分游...
美以讨论重启对伊战事,特朗普:... 美国总统特朗普17日在社交媒体发文称,如果伊朗不迅速行动,“将一无所有”。特朗普写道:“对伊朗而言,...
张雪回应拿下第五冠:准备冲击年... 5月17日,2026世界超级摩托车锦标赛(WSBK)捷克站结束WorldSSP组别第二回合正赛争夺。...
特朗普就台湾问题表态 美国总统特朗普结束访华后,就台湾问题明确表态,不希望看到有人走向“独立”。
日防卫白皮书草案又炒“中国威胁... 【环球时报驻日本特约记者 初欣 环球时报记者 郭媛丹】日本政府2026年版《防卫白皮书》草案15日被...
47年,一条产业链托起一台冠军... 从嘉陵CJ50到张雪机车820RR——47年,一条产业链托起一台冠军摩托车(经济聚焦)本报记者 王欣...
核电站遭袭,阿联酋与卡塔尔、约... 当地时间17日,阿联酋强烈谴责针对位于宰夫拉地区巴拉卡核电站外围一台发电机的袭击。此次袭击由一架从西...
没处休、没空休、不敢休,保洁员... 一些商场、写字楼没有设置工间休息室,他们没空休息、无处休息——保洁员该在哪儿歇脚(金台视线)本报记者...