add a non-blocking mount poller to the PDDB

The problem with the current is_mounted() call is that it blocks
while the PDDB is requesting a password or actively formatting
(otherwise it doesn't block). It's actually just...partially blocking.

That doesn't cut it for libstd-linked system services that need to run but
before the PDDB comes up. In particular, we need to be able to respond
to time queries *somehow*, even before our timezone and offsets are
loaded from the PDDB.

I considered doing a callback mechanism for mounting, but this
is kind of complex and probably not worth it. Instead, a separate
thread that can return the value of a single boolean revealing
the mount state of the PDDB is created, and you can create an
object that's capable of inspecting that boolean.
This commit is contained in:
bunnie 2022-03-27 02:52:29 +08:00
parent a6af300157
commit a103536b58
4 changed files with 80 additions and 3 deletions

View file

@ -12,6 +12,7 @@ use std::num::NonZeroU32;
// note this name cannot be changed because it is baked into `libstd`
pub(crate) const SERVER_NAME_PDDB: &str = "_Plausibly Deniable Database_";
pub(crate) const SERVER_NAME_PDDB_POLLER: &str = "_PDDB Mount Poller_";
#[allow(dead_code)]
pub(crate) const BASIS_NAME_LEN: usize = 64; // don't want this too long anyways, because it's not recorded anywhere - users have to type it in.
#[allow(dead_code)]
@ -27,8 +28,8 @@ pub(crate) const PDDB_VERSION: u32 = 0x00_00_01_01;
#[allow(dead_code)]
// PDDB_A_LEN may be shorter than xous::PDDB_LEN, to speed up testing.
#[allow(dead_code)]
pub(crate) const PDDB_A_LEN: usize = xous::PDDB_LEN as usize;
// pub(crate) const PDDB_A_LEN: usize = 4 * 1024 * 1024;
//pub(crate) const PDDB_A_LEN: usize = xous::PDDB_LEN as usize;
pub(crate) const PDDB_A_LEN: usize = 4 * 1024 * 1024;
/// range for the starting point of a journal number, picked from a random seed
/// the goal is to reduce info leakage about the age of structures relative to each other
@ -113,6 +114,11 @@ pub(crate) enum Opcode {
/// quit the server
Quit,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum PollOp {
Poll,
Quit,
}
pub type ApiToken = [u32; 3];
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]

View file

@ -121,6 +121,7 @@ impl DictCacheEntry {
} else {
let mut index_cache = PlaintextCache { data: None, tag: None };
let mut data_cache = PlaintextCache { data: None, tag: None };
let mut errcnt = 0;
while try_entry < KEY_MAXCOUNT && key_count < self.key_count {
// cache our decryption data -- there's about 32 entries per page, and the scan is largely linear/sequential, so this should
// be a substantial savings in effort.
@ -129,7 +130,10 @@ impl DictCacheEntry {
if index_cache.data.is_none() || index_cache.tag.is_none() {
// somehow we hit a page where nothing was allocated (perhaps it was previously deleted?), or less likely, the data was corrupted. Note the isuse, skip past it.
log::warn!("Dictionary fill: encountered unallocated page at {} in the dictionary map. {}/{}", try_entry, key_count, self.key_count);
if (errcnt < 16) || (errcnt % 8192 == 0) {
log::warn!("Dictionary fill: encountered unallocated page at {} in the dictionary map. {}/{}", try_entry, key_count, self.key_count);
}
errcnt += 1;
try_entry += DK_PER_VPAGE;
} else {
let cache_pp = index_cache.tag.as_ref().expect("PP should be in existence, it was already checked...");

View file

@ -17,6 +17,7 @@ use std::thread::JoinHandle;
use core::sync::atomic::{AtomicU32, Ordering};
pub(crate) static REFCOUNT: AtomicU32 = AtomicU32::new(0);
pub(crate) static POLLER_REFCOUNT: AtomicU32 = AtomicU32::new(0);
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
enum CbOp {
@ -24,6 +25,40 @@ enum CbOp {
Quit
}
pub struct PddbMountPoller {
conn: CID
}
impl PddbMountPoller {
pub fn new() -> Self {
POLLER_REFCOUNT.fetch_add(1, Ordering::Relaxed);
let xns = xous_names::XousNames::new().unwrap();
let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB_POLLER).expect("can't connect to Pddb mount poller server");
PddbMountPoller { conn }
}
pub fn is_mounted_nonblocking(&self) -> bool {
match send_message(self.conn,
Message::new_blocking_scalar(PollOp::Poll.to_usize().unwrap(), 0, 0, 0, 0)
).expect("couldn't poll mount poller") {
xous::Result::Scalar1(is_mounted) => {
if is_mounted == 0 {
false
} else {
true
}
}
_ => false
}
}
}
impl Drop for PddbMountPoller {
fn drop(&mut self) {
if POLLER_REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
unsafe{xous::disconnect(self.conn).unwrap();}
}
}
}
/// The intention is that one Pddb management object is made per process, and this serves
/// as the gateway for parcelling out PddbKey objects, which are the equivalent of a File
/// in a convention system that implements read/write operations.

View file

@ -388,6 +388,8 @@ use std::thread;
use std::collections::HashMap;
use std::io::ErrorKind;
use core::fmt::Write;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use locales::t;
@ -436,6 +438,34 @@ fn xmain() -> ! {
// storage for the token lookup: given an ApiToken, return a dict/key/basis set. Basis can be None or specified.
let mut token_dict = HashMap::<ApiToken, TokenRecord>::new();
// mount poller thread
let is_mounted = Arc::new(AtomicBool::new(false));
let _ = thread::spawn({
let is_mounted = is_mounted.clone();
move || {
let xns = xous_names::XousNames::new().unwrap();
let poller_sid = xns.register_name(api::SERVER_NAME_PDDB_POLLER, None).expect("can't register server");
loop {
let msg = xous::receive_message(poller_sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(PollOp::Poll) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
if is_mounted.load(Ordering::SeqCst) {
xous::return_scalar(msg.sender, 1).unwrap();
} else {
xous::return_scalar(msg.sender, 0).unwrap();
}
}),
Some(PollOp::Quit) => {
xous::return_scalar(msg.sender, 0).unwrap();
break;
}
None => log::warn!("got unrecognized message: {:?}", msg),
}
}
xous::destroy_server(poller_sid).ok();
}
});
// run the CI tests if the option has been selected
#[cfg(all(
not(any(target_os = "none", target_os = "xous")),
@ -512,6 +542,7 @@ fn xmain() -> ! {
match ensure_password(&modals, &mut pddb_os) {
PasswordState::Correct => {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Correct) {
is_mounted.store(true, Ordering::SeqCst);
xous::return_scalar(msg.sender, 1).expect("couldn't return scalar");
} else {
xous::return_scalar(msg.sender, 0).expect("couldn't return scalar");
@ -519,6 +550,7 @@ fn xmain() -> ! {
},
PasswordState::Uninit => {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Uninit) {
is_mounted.store(true, Ordering::SeqCst);
xous::return_scalar(msg.sender, 1).expect("couldn't return scalar");
} else {
xous::return_scalar(msg.sender, 0).expect("couldn't return scalar");