Skip to content

Commit

Permalink
Add submodule and use per-thread-mutex for synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Aug 31, 2023
1 parent 351b089 commit 2cef9e7
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 50 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ jobs:
container:
image: fedora:38 # CURRENT DEVELOPMENT ENVIRONMENT
steps:
- name: Install git
run: dnf install -y git
- uses: actions/checkout@v3
with:
submodules: true
- name: Install dependencies for Fedora
run: >
dnf install -y
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ jobs:
TOOLCHAIN: 1.72.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN
runs-on: ubuntu-20.04
steps:
- name: Install git
run: sudo apt-get install git
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@master
with:
components: rustfmt
Expand All @@ -38,7 +42,11 @@ jobs:
TOOLCHAIN: 1.72.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN
runs-on: ubuntu-20.04
steps:
- name: Install git
run: sudo apt-get install git
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@master
with:
components: clippy
Expand Down Expand Up @@ -70,7 +78,11 @@ jobs:
dependencies: libcryptsetup-dev
runs-on: ubuntu-20.04
steps:
- name: Install git
run: sudo apt-get install git
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.TOOLCHAIN }}
Expand All @@ -95,7 +107,11 @@ jobs:
dependencies: libcryptsetup-dev libkeyutils-dev
runs-on: ubuntu-20.04
steps:
- name: Install git
run: sudo apt-get install git
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.TOOLCHAIN }}
Expand Down Expand Up @@ -187,7 +203,11 @@ jobs:
image: fedora:${{ matrix.container }}
options: --privileged -v /dev:/dev
steps:
- name: Install git
run: dnf install -y git
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.TOOLCHAIN }}
Expand All @@ -201,7 +221,11 @@ jobs:
runs-on: ubuntu-20.04
container: fedora:38 # CURRENT DEVELOPMENT ENVIRONMENT
steps:
- name: Install git
run: dnf install -y git
- uses: actions/checkout@v3
with:
submodules: true
- name: Install dependencies
run: dnf install -y make yamllint
- name: Run yamllint
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ jobs:
task: make -f Makefile clippy
runs-on: ubuntu-20.04
steps:
- name: Install git
run: sudo apt-get install git
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@master
with:
components: ${{ matrix.component }}
Expand All @@ -38,7 +42,11 @@ jobs:
container:
image: fedora:38 # CURRENT DEVELOPMENT ENVIRONMENT
steps:
- name: Install git
run: dnf install -y git
- uses: actions/checkout@v3
with:
submodules: true
- name: Install dependencies for Fedora
run: >
dnf install -y
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
runs-on: ubuntu-latest
container: fedora:39 # NEXT DEVELOPMENT ENVIRONMENT
steps:
- name: Install git
run: dnf install -y git
- uses: actions/checkout@v3
- name: Install dependencies
run: >
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "per-thread-mutex"]
path = per-thread-mutex
url = https://github.com/jbaublitz/per-thread-mutex
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ path = "./libcryptsetup-rs-sys"
either = "1.6.1"
libc = "0.2.143"
bitflags = "2.3.0"
log = "0.4.19"
serde_json = "1.0.0"
lazy_static = "1.4.0"

Expand All @@ -35,6 +36,7 @@ semver = "1.0.0"

[dev-dependencies]
base64 = "0.21.0"
env_logger = "0.10.0"
loopdev = "0.4.0"
nix = "0.26.0"
rand = "0.8.0"
Expand Down
1 change: 1 addition & 0 deletions per-thread-mutex
Submodule per-thread-mutex added at 4b331c
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mod keyslot;
mod log;
mod luks2;
mod mem;
#[cfg(feature = "mutex")]
mod mutex;
mod runtime;
mod settings;
mod status;
Expand Down Expand Up @@ -60,7 +62,7 @@ pub use crate::{
luks2::{
flags::CryptLuks2FlagsHandle,
reencrypt::{CryptLuks2ReencryptHandle, CryptParamsReencrypt, CryptParamsReencryptRef},
token::{CryptLuks2TokenHandle, CryptTokenInfo, TokenInput},
token::{register, CryptLuks2TokenHandle, CryptTokenInfo, TokenInput},
},
mem::SafeMemHandle,
runtime::{ActiveDevice, CryptRuntimeHandle},
Expand All @@ -77,7 +79,7 @@ pub type Result<T> = std::result::Result<T, LibcryptErr>;

#[cfg(feature = "mutex")]
lazy_static::lazy_static! {
static ref MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
static ref MUTEX: crate::mutex::PerThreadMutex = crate::mutex::PerThreadMutex::default();
}

#[cfg(not(feature = "mutex"))]
Expand Down
46 changes: 23 additions & 23 deletions src/luks2/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,29 +229,6 @@ impl<'a> CryptLuks2TokenHandle<'a> {
}
}

/// Register token handler
pub fn register(
name: &'static str,
open: libcryptsetup_rs_sys::crypt_token_open_func,
buffer_free: libcryptsetup_rs_sys::crypt_token_buffer_free_func,
validate: libcryptsetup_rs_sys::crypt_token_validate_func,
dump: libcryptsetup_rs_sys::crypt_token_dump_func,
) -> Result<(), LibcryptErr> {
if name.get(name.len() - 1..) != Some("\0") {
return Err(LibcryptErr::NoNull(name));
}
let handler = libcryptsetup_rs_sys::crypt_token_handler {
name: name.as_ptr().cast::<c_char>(),
open,
buffer_free,
validate,
dump,
};
errno!(mutex!(libcryptsetup_rs_sys::crypt_token_register(
&handler as *const libcryptsetup_rs_sys::crypt_token_handler,
)))
}

/// Activate device or check key using a token
pub fn activate_by_token<T>(
&mut self,
Expand Down Expand Up @@ -283,3 +260,26 @@ impl<'a> CryptLuks2TokenHandle<'a> {
.map(|rc| rc as c_uint)
}
}

/// Register token handler
pub fn register(
name: &'static str,
open: libcryptsetup_rs_sys::crypt_token_open_func,
buffer_free: libcryptsetup_rs_sys::crypt_token_buffer_free_func,
validate: libcryptsetup_rs_sys::crypt_token_validate_func,
dump: libcryptsetup_rs_sys::crypt_token_dump_func,
) -> Result<(), LibcryptErr> {
if name.get(name.len() - 1..) != Some("\0") {
return Err(LibcryptErr::NoNull(name));
}
let handler = libcryptsetup_rs_sys::crypt_token_handler {
name: name.as_ptr().cast::<c_char>(),
open,
buffer_free,
validate,
dump,
};
errno!(mutex!(libcryptsetup_rs_sys::crypt_token_register(
&handler as *const libcryptsetup_rs_sys::crypt_token_handler,
)))
}
26 changes: 1 addition & 25 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,7 @@ macro_rules! mutex {
( $libcryptsetup_call:expr ) => {{
#[cfg(feature = "mutex")]
#[allow(unused_variables)]
let lock = match $crate::MUTEX.lock() {
Ok(l) => Some(l),
Err(e) => {
panic!(
"The synchronization mutex can no longer be locked. \
Locking failed with error: {}; see Rust's documentation \
on Mutex poisoning here to decide how to proceed: \
https://doc.rust-lang.org/std/sync/struct.Mutex.html#errors",
e,
)
}
};
let lock = $crate::MUTEX.acquire();

#[cfg(not(feature = "mutex"))]
if *$crate::THREAD_ID != std::thread::current().id() {
Expand Down Expand Up @@ -444,19 +433,6 @@ mod test {
assert_eq!(PETestEnum::PartialEq, PETestEnum::try_from(3).unwrap());
}

#[cfg(feature = "mutex")]
#[test]
#[should_panic(expected = "The synchronization mutex can no longer be locked.")]
fn test_mutex_poisoning_panic() {
assert!(std::panic::catch_unwind(|| {
let _lock = crate::MUTEX.lock().unwrap();
panic!("Cause panic");
})
.is_err());

crate::status::get_sector_size(None);
}

#[cfg(not(feature = "mutex"))]
#[test]
#[should_panic(expected = "Enable the mutex feature")]
Expand Down
1 change: 1 addition & 0 deletions src/mutex.rs

0 comments on commit 2cef9e7

Please sign in to comment.