Skip to content

Commit 5fa27e9

Browse files
authored
Add the ClientAuthentication_hook (pgcentralfoundation#2231)
Co-authored-by: damien <[email protected]>
1 parent 2a7ad3a commit 5fa27e9

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

pgrx-examples/hooks/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use pgrx::prelude::*;
99

1010
::pgrx::pg_module_magic!(name, version);
1111

12+
fn knock_knock() {
13+
todo!()
14+
}
15+
1216
fn delete_must_have_a_where(query: PgBox<pg_sys::Query>) {
1317
if query.commandType != pg_sys::CmdType::CMD_DELETE {
1418
return ();
@@ -33,6 +37,25 @@ fn only_superusers_can_truncate(pstmt: PgBox<pg_sys::PlannedStmt>) {
3337
}
3438

3539
unsafe fn register_hooks() {
40+
41+
//
42+
// Client Authentication hook
43+
//
44+
static mut PREV_CLIENTAUTHENTICATION_HOOK: pg_sys::libpq::ClientAuthentication_hook_type = None;
45+
PREV_CLIENTAUTHENTICATION_HOOK = pg_sys::libpq::ClientAuthentication_hook;
46+
pg_sys::libpq::ClientAuthentication_hook = Some(client_authentication_hook);
47+
48+
#[pg_guard]
49+
unsafe extern "C-unwind" fn client_authentication_hook(
50+
port: *mut pg_sys::libpq::be::Port,
51+
status: i32,
52+
) {
53+
knock_knock();
54+
if let Some(prev_hook) = PREV_CLIENTAUTHENTICATION_HOOK {
55+
pg_guard_ffi_boundary(|| prev_hook(port, status));
56+
}
57+
}
58+
3659
//
3760
// Post Parse Analyze hook
3861
//

pgrx-pg-sys/src/libpq.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,13 @@ pub mod be {
203203
raw_buf_remaining: isize,
204204
}
205205
}
206+
207+
// The ClientAuthentication Hook cannot be implemented simply with bindgen
208+
/// Hook type to get control in ClientAuthentication()
209+
pub type ClientAuthentication_hook_type =
210+
Option<unsafe extern "C-unwind" fn(port: *mut be::Port, status: i32)>;
211+
212+
#[pgrx_macros::pg_guard]
213+
unsafe extern "C-unwind" {
214+
pub static mut ClientAuthentication_hook: ClientAuthentication_hook_type;
215+
}

0 commit comments

Comments
 (0)