add callback for monitoring basis lock/unlock events

This commit is contained in:
bunnie 2022-12-29 00:10:42 +08:00
parent 8d42feb051
commit 5370c333da
3 changed files with 74 additions and 5 deletions

View file

@ -239,6 +239,9 @@ pub(crate) enum Opcode {
/// Mount was attempted
MountAttempted = 53,
/// Basis monitoring - reports when the basis order has changed
BasisMonitor = 54,
/// This key type could not be decoded
InvalidOpcode = u32::MAX as _,
}

View file

@ -255,6 +255,27 @@ impl Pddb {
}
ret
}
/// The caller of this function will block and return only if the order of Bases has changed.
pub fn monitor_basis(&self) -> Vec::<String> {
let list_alloc = PddbBasisList {
list: [xous_ipc::String::<BASIS_NAME_LEN>::default(); 63],
num: 0
};
let mut buf = Buffer::into_buf(list_alloc).expect("Couldn't convert to memory structure");
buf.lend_mut(self.conn, Opcode::BasisMonitor.to_u32().unwrap()).expect("Couldn't execute ListBasis opcode");
let list = buf.to_original::<PddbBasisList, _>().expect("couldn't restore list structure");
if list.num > list.list.len() as u32 {
log::warn!("Number of open basis larger than our IPC structure. May need to refactor this API.");
}
let mut ret = Vec::<String>::new();
for (index, name) in list.list.iter().enumerate() {
if index as u32 == list.num {
break;
}
ret.push(name.as_str().expect("name is not valid utf-8").to_string());
}
ret
}
/// returns the latest basis that is opened -- this is where all new values are being sent by default
/// if the PDDB is not mounted, returns None
pub fn latest_basis(&self) -> Option<String> {

View file

@ -592,6 +592,9 @@ fn wrapped_main() -> ! {
let mut mount_notifications = Vec::<xous::MessageSender>::new();
let mut attempt_notifications = Vec::<xous::MessageSender>::new();
// track the basis monitor requester.
let mut basis_monitor_notifications = Vec::<xous::MessageEnvelope>::new();
// track heap usage
let mut initial_heap: usize = 0;
let mut latest_heap: usize = 0;
@ -681,7 +684,7 @@ fn wrapped_main() -> ! {
} else {
match ensure_password(&modals, &mut pddb_os, pw_cid) {
PasswordState::Correct => {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Correct, time_resetter) {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Correct, time_resetter, &mut basis_monitor_notifications) {
is_mounted.store(true, Ordering::SeqCst);
for requester in mount_notifications.drain(..) {
xous::return_scalar2(requester, 0, 0).expect("couldn't return scalar");
@ -694,7 +697,7 @@ fn wrapped_main() -> ! {
}
},
PasswordState::Uninit => {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Uninit, time_resetter) {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Uninit, time_resetter, &mut basis_monitor_notifications) {
for requester in mount_notifications.drain(..) {
xous::return_scalar2(requester, 0, 0).expect("couldn't return scalar");
}
@ -745,7 +748,7 @@ fn wrapped_main() -> ! {
latest_cache = current_cache;
if (latest_heap > initial_heap
&& (latest_heap - initial_heap) > HEAP_GC_THRESH)
// this line is mostly so this triggers occassionally in hosted mode where heap usage is faked;
// this line is mostly so this triggers occasionally in hosted mode where heap usage is faked;
// in practice heap threshold will always hit before cache threshold
|| current_cache > HEAP_GC_THRESH {
log::info!("PDDB trim threshold reached: {} heap, {} cache", latest_heap, basis_cache.cache_size());
@ -765,6 +768,9 @@ fn wrapped_main() -> ! {
list_ipc.num = basis_list.len() as u32;
buffer.replace(list_ipc).unwrap();
}
Opcode::BasisMonitor => {
basis_monitor_notifications.push(msg);
}
Opcode::ListBasisStd => {
if let Some(mem) = msg.body.memory_message_mut() {
mem.offset = None;
@ -872,6 +878,9 @@ fn wrapped_main() -> ! {
basis_cache.basis_add(basis);
finished = true;
log::info!("{}PDDB.UNLOCKOK,{},{}", xous::BOOKEND_START, mgmt.name.as_str().unwrap(), xous::BOOKEND_END);
if basis_monitor_notifications.len() > 0 {
notify_basis_change(&mut basis_monitor_notifications, basis_cache.basis_list());
}
mgmt.code = PddbRequestCode::NoErr;
} else {
log::info!("{}PDDB.BADPASS,{},{}", xous::BOOKEND_START, mgmt.name.as_str().unwrap(), xous::BOOKEND_END);
@ -912,7 +921,12 @@ fn wrapped_main() -> ! {
match mgmt.code {
PddbRequestCode::Close => {
match basis_cache.basis_unmount(&mut pddb_os, mgmt.name.as_str().expect("name is not valid utf-8")) {
Ok(_) => mgmt.code = PddbRequestCode::NoErr,
Ok(_) => {
mgmt.code = PddbRequestCode::NoErr;
if basis_monitor_notifications.len() > 0 {
notify_basis_change(&mut basis_monitor_notifications, basis_cache.basis_list());
}
}
Err(e) => match e.kind() {
ErrorKind::NotFound => mgmt.code = PddbRequestCode::NotFound,
_ => mgmt.code = PddbRequestCode::InternalError,
@ -1847,6 +1861,9 @@ fn wrapped_main() -> ! {
}
// finally, unmount the system basis
basis_cache.basis_unmount(&mut pddb_os, PDDB_DEFAULT_SYSTEM_BASIS).expect("can't unmount system basis");
if basis_monitor_notifications.len() > 0 {
notify_basis_change(&mut basis_monitor_notifications, basis_cache.basis_list());
}
if basis_cache.basis_list().len() == 0 {
is_mounted.store(false, Ordering::SeqCst);
log::info!(".System basis is unmounted.");
@ -1955,13 +1972,23 @@ fn ensure_password(modals: &modals::Modals, pddb_os: &mut PddbOs, _pw_cid: xous:
}
}
}
fn try_mount_or_format(modals: &modals::Modals, pddb_os: &mut PddbOs, basis_cache: &mut BasisCache, pw_state: PasswordState, time_resetter: xous::CID) -> bool {
fn try_mount_or_format(
modals: &modals::Modals,
pddb_os: &mut PddbOs,
basis_cache: &mut BasisCache,
pw_state: PasswordState,
time_resetter: xous::CID,
basis_monitor_notifications: &mut Vec::<xous::MessageEnvelope>
) -> bool {
log::info!("Attempting to mount the PDDB");
if pw_state == PasswordState::Correct {
modals.dynamic_notification(Some(t!("pddb.waitmount", xous::LANG)), None).unwrap();
if let Some(sys_basis) = pddb_os.pddb_mount() {
log::info!("PDDB mount operation finished successfully");
basis_cache.basis_add(sys_basis);
if basis_monitor_notifications.len() > 0 {
notify_basis_change(basis_monitor_notifications, basis_cache.basis_list());
}
modals.dynamic_notification_close().unwrap();
return true
}
@ -2026,6 +2053,9 @@ fn try_mount_or_format(modals: &modals::Modals, pddb_os: &mut PddbOs, basis_cach
if let Some(sys_basis) = pddb_os.pddb_mount() {
log::info!("PDDB mount operation finished successfully");
basis_cache.basis_add(sys_basis);
if basis_monitor_notifications.len() > 0 {
notify_basis_change(basis_monitor_notifications, basis_cache.basis_list());
}
modals.dynamic_notification_close().unwrap();
true
} else {
@ -2191,6 +2221,21 @@ fn notify_of_disconnect(pddb_os: &mut PddbOs, token_dict: &HashMap::<ApiToken, T
}
}
fn notify_basis_change(basis_monitor_notifications: &mut Vec::<xous::MessageEnvelope>, basis_list: Vec::<String>) {
for mut sender in basis_monitor_notifications.drain(..) {
let mut response = unsafe {
Buffer::from_memory_message_mut(sender.body.memory_message_mut().unwrap())
};
let mut list_ipc = response.to_original::<PddbBasisList, _>().unwrap();
for (src, dst) in basis_list.iter().zip(list_ipc.list.iter_mut()) {
dst.clear();
write!(dst, "{}", src).expect("couldn't write basis name");
}
list_ipc.num = basis_list.len() as u32;
response.replace(list_ipc).unwrap();
}
}
pub(crate) fn heap_usage() -> usize {
match xous::rsyscall(xous::SysCall::IncreaseHeap(0, xous::MemoryFlags::R)).expect("couldn't get heap size") {
xous::Result::MemoryRange(m) => {