From 9c951856b8c626e8e0566689d562eb11b3c6c0fb Mon Sep 17 00:00:00 2001 From: Anton Burticica Date: Fri, 22 Nov 2024 19:11:46 +0200 Subject: [PATCH] Early loggin initialization, temporary disable tracing::instrument for C_Finalize/C_CloseAllSesssions --- Cargo.lock | 11 ++++++ native-pkcs11/Cargo.toml | 1 + native-pkcs11/src/lib.rs | 83 ++++++++++++++++++++++------------------ 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 925b356..d51588d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,6 +158,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctor" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "der" version = "0.7.9" @@ -388,6 +398,7 @@ name = "native-pkcs11" version = "0.2.22" dependencies = [ "cached", + "ctor", "native-pkcs11-core", "native-pkcs11-keychain", "native-pkcs11-traits", diff --git a/native-pkcs11/Cargo.toml b/native-pkcs11/Cargo.toml index b209147..661feca 100644 --- a/native-pkcs11/Cargo.toml +++ b/native-pkcs11/Cargo.toml @@ -12,6 +12,7 @@ license.workspace = true custom-function-list = [] [dependencies] +ctor = { version = "0.2" } cached = { version = "~0.54", default-features = false } native-pkcs11-core = { version = "^0.2.14", path = "../native-pkcs11-core" } native-pkcs11-traits = { version = "0.2.0", path = "../native-pkcs11-traits" } diff --git a/native-pkcs11/src/lib.rs b/native-pkcs11/src/lib.rs index aa946dd..de9b91f 100644 --- a/native-pkcs11/src/lib.rs +++ b/native-pkcs11/src/lib.rs @@ -17,6 +17,7 @@ #![deny(unsafe_op_in_unsafe_fn)] pub use native_pkcs11_core::Error; +use ctor::ctor; use native_pkcs11_traits::backend; use tracing::metadata::LevelFilter; use tracing_error::ErrorLayer; @@ -28,10 +29,7 @@ mod utils; use std::{ cmp, slice, - sync::{ - atomic::{AtomicBool, Ordering}, - Once, - }, + sync::atomic::{AtomicBool, Ordering}, }; use native_pkcs11_core::{ @@ -209,35 +207,41 @@ pub static mut FUNC_LIST: CK_FUNCTION_LIST = CK_FUNCTION_LIST { C_WaitForSlotEvent: Some(C_WaitForSlotEvent), }; -static TRACING_INIT: Once = Once::new(); +#[ctor] +fn init_tracing() { + let env_filter = EnvFilter::builder() + .with_default_directive(LevelFilter::WARN.into()) + .from_env_lossy(); + let force_stderr = std::env::var("NATIVE_PKCS11_LOG_STDERR").is_ok(); + if !force_stderr { + if let Ok(journald_layer) = tracing_journald::layer() { + let subscriber = Registry::default() + .with(journald_layer.with_syslog_identifier("native-pkcs11".into())) + .with(env_filter) + .with(ErrorLayer::default()); + if let Err(e) = tracing::subscriber::set_global_default(subscriber) { + eprintln!("failed to initialize logging: {e}"); + } + return; + } + } + + let subscriber = Registry::default() + .with( + tracing_subscriber::fmt::layer() + .with_writer(std::io::stderr) + .with_span_events(FmtSpan::ENTER), + ) + .with(env_filter) + .with(ErrorLayer::default()); + + if let Err(e) = tracing::subscriber::set_global_default(subscriber) { + eprintln!("failed to initialize logging: {e}"); + } +} cryptoki_fn!( fn C_Initialize(pInitArgs: CK_VOID_PTR) { - TRACING_INIT.call_once(|| { - let env_filter = EnvFilter::builder() - .with_default_directive(LevelFilter::WARN.into()) - .from_env_lossy(); - let force_stderr = std::env::var("NATIVE_PKCS11_LOG_STDERR").is_ok(); - if !force_stderr { - if let Ok(journald_layer) = tracing_journald::layer() { - _ = Registry::default() - .with(journald_layer.with_syslog_identifier("native-pkcs11".into())) - .with(env_filter) - .with(ErrorLayer::default()) - .try_init(); - return; - } - } - _ = Registry::default() - .with( - tracing_subscriber::fmt::layer() - .with_writer(std::io::stderr) - .with_span_events(FmtSpan::ENTER), - ) - .with(env_filter) - .with(ErrorLayer::default()) - .try_init(); - }); if !pInitArgs.is_null() { let args = unsafe { *(pInitArgs as CK_C_INITIALIZE_ARGS_PTR) }; if !args.pReserved.is_null() { @@ -251,16 +255,17 @@ cryptoki_fn!( } ); -cryptoki_fn!( - fn C_Finalize(pReserved: CK_VOID_PTR) { +pub extern "C" fn C_Finalize(pReserved: CK_VOID_PTR) -> CK_RV { + // TODO(bweeks): should this be `expr` instead of `block`? + result_to_rv(|| { initialized!(); if !pReserved.is_null() { return Err(Error::ArgumentsBad); } INITIALIZED.store(false, Ordering::SeqCst); Ok(()) - } -); + }) +} cryptoki_fn!( unsafe fn C_GetInfo(pInfo: CK_INFO_PTR) { @@ -474,14 +479,16 @@ cryptoki_fn!( } ); -cryptoki_fn!( - fn C_CloseAllSessions(slotID: CK_SLOT_ID) { +#[no_mangle] +pub extern "C" fn C_CloseAllSessions(slotID: CK_SLOT_ID) -> CK_RV { + // TODO(bweeks): should this be `expr` instead of `block`? + result_to_rv(|| { initialized!(); valid_slot!(slotID); sessions::close_all(); Ok(()) - } -); + }) +} cryptoki_fn!( unsafe fn C_GetSessionInfo(hSession: CK_SESSION_HANDLE, pInfo: CK_SESSION_INFO_PTR) {