温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PostgreSQL插件hook机制

发布时间:2020-07-24 01:29:47 来源:网络 阅读:1059 作者:yzs的专栏 栏目:数据库
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);
    }
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI