Skip to content

Commit

Permalink
Early loggin initialization, temporary disable tracing::instrument fo…
Browse files Browse the repository at this point in the history
…r C_Finalize/C_CloseAllSesssions
  • Loading branch information
ya-mouse committed Nov 22, 2024
1 parent 4e6bc70 commit 9c95185
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native-pkcs11/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
83 changes: 45 additions & 38 deletions native-pkcs11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,10 +29,7 @@ mod utils;
use std::{
cmp,
slice,
sync::{
atomic::{AtomicBool, Ordering},
Once,
},
sync::atomic::{AtomicBool, Ordering},
};

use native_pkcs11_core::{
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9c95185

Please sign in to comment.