From b31d15c6f3f2fbc1d27521e7ce5c655756567fed Mon Sep 17 00:00:00 2001 From: chenjiayi Date: Fri, 3 Nov 2023 01:11:39 +0800 Subject: [PATCH 1/2] fix(devmaster): fix misspelt sysattr sas_address The 'sas_address' attribte is misspelt. --- exts/devmaster/src/lib/builtin/path_id.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exts/devmaster/src/lib/builtin/path_id.rs b/exts/devmaster/src/lib/builtin/path_id.rs index ecd730f8..303b7528 100644 --- a/exts/devmaster/src/lib/builtin/path_id.rs +++ b/exts/devmaster/src/lib/builtin/path_id.rs @@ -390,7 +390,7 @@ impl PathId { Err(_) => return None, }; - let sas_address = match asadev.get_sysattr_value("asa_address") { + let sas_address = match asadev.get_sysattr_value("sas_address") { Ok(addr) => addr, Err(_) => return None, }; @@ -430,7 +430,7 @@ impl PathId { Err(_) => return None, }; - let target = match sessiondev.get_sysattr_value("asa_address") { + let target = match sessiondev.get_sysattr_value("sas_address") { Ok(port) => port, Err(_) => return None, }; -- Gitee From 97f0853e3e847653d09d986c986c26b869e79e9c Mon Sep 17 00:00:00 2001 From: chenjiayi Date: Fri, 3 Nov 2023 01:47:16 +0800 Subject: [PATCH 2/2] fix(devmaster): check the validity of kmod context during new method The kmod_sys::kmod_new may return a null pointer. Check the validity of the pointer before using it to constuct the kmod instance. --- exts/devmaster/src/lib/builtin/kmod.rs | 21 +++++++++++++++++---- libs/kmod_rs/src/lib.rs | 17 ++++++++--------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/exts/devmaster/src/lib/builtin/kmod.rs b/exts/devmaster/src/lib/builtin/kmod.rs index ac73d1bb..cf718d41 100644 --- a/exts/devmaster/src/lib/builtin/kmod.rs +++ b/exts/devmaster/src/lib/builtin/kmod.rs @@ -24,14 +24,14 @@ use std::rc::Rc; /// kmod builtin command pub(crate) struct Kmod { /// kmod struct - kernel_module: Rc>, + kernel_module: Option>>, } impl Kmod { /// create Kmod pub(crate) fn new() -> Kmod { Kmod { - kernel_module: Rc::new(RefCell::new(kmod_rs::LibKmod::new())), + kernel_module: kmod_rs::LibKmod::new().map(|inner| Rc::new(RefCell::new(inner))), } } } @@ -53,7 +53,8 @@ impl Builtin for Kmod { ) -> Result { let device = exec_unit.get_device(); - if self.kernel_module.borrow().is_ctx_null() { + if self.kernel_module.is_none() { + log::error!("Kmod context is not loaded."); return Ok(true); } @@ -72,6 +73,8 @@ impl Builtin for Kmod { if !modalias.is_empty() { if let Err(e) = self .kernel_module + .as_ref() + .unwrap() .borrow_mut() .module_load_and_warn(&modalias, false) { @@ -82,6 +85,8 @@ impl Builtin for Kmod { for i in 2..argc { if let Err(e) = self .kernel_module + .as_ref() + .unwrap() .borrow_mut() .module_load_and_warn(&argv[i as usize], false) { @@ -94,7 +99,13 @@ impl Builtin for Kmod { /// builtin init function fn init(&self) { - if let Err(e) = self.kernel_module.borrow_mut().load_resources() { + if let Err(e) = self + .kernel_module + .as_ref() + .unwrap() + .borrow_mut() + .load_resources() + { log::error!("Load resources failed! {}", e); } } @@ -105,6 +116,8 @@ impl Builtin for Kmod { /// check whether builtin command should reload fn should_reload(&self) -> bool { self.kernel_module + .as_ref() + .unwrap() .borrow_mut() .validate_resources() .map_or(false, |e| { diff --git a/libs/kmod_rs/src/lib.rs b/libs/kmod_rs/src/lib.rs index d0efabc5..f95cd32a 100644 --- a/libs/kmod_rs/src/lib.rs +++ b/libs/kmod_rs/src/lib.rs @@ -73,21 +73,20 @@ impl Drop for LibKmod { } } -impl Default for LibKmod { - fn default() -> Self { - Self::new() - } -} - impl LibKmod { /// Create libkmod - pub fn new() -> LibKmod { + pub fn new() -> Option { let c = unsafe { kmod_sys::kmod_new(std::ptr::null(), std::ptr::null()) }; - LibKmod { + + if c.is_null() { + return None; + } + + Some(LibKmod { ctx: c, kmod_list_head: ptr::null::() as *mut kmod_sys::kmod_list, module: ptr::null::() as *mut kmod_sys::kmod_module, - } + }) } /// Create KmodListIter with internal members -- Gitee