Skip to main content

pseudoroot_core/
lib.rs

1//! Shared state, IPC, and session backing for pseudoroot.
2//!
3//! Both the CLI/API crate (`pseudoroot`) and the interposed cdylib
4//! (`pseudoroot-lib`) depend on this crate. It owns the inode-keyed fake
5//! metadata model and the two multi-process backends:
6//!
7//! - [`shm_map`] / [`shm_client`] — lock-free shared-memory hash table inherited
8//!   across `exec` within a session (default).
9//! - [`daemon_server`] / [`daemon_client`] — Unix-socket RPC for external or
10//!   in-process daemons (`pdrd`, `pdr start`, or `PSEUDOROOT_SESSION_SHM=0`).
11//!
12//! Per-process fallback lives in [`state`] (`global_state_read` /
13//! `global_state_write`), used when neither SHM nor daemon mode is active.
14//!
15//! See [the architecture guide](https://github.com/lu-zero/pseudoroot/blob/master/docs/architecture.md)
16//! for end-to-end flows and backend selection.
17
18#![deny(missing_docs)]
19
20pub mod daemon_client;
21pub mod daemon_server;
22pub mod protocol;
23pub mod shm_client;
24pub mod shm_map;
25pub mod state;
26
27pub use state::{FakeInode, FakeRootState, InodeKey};
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_fake_root_state_new() {
35        let state = FakeRootState::new();
36        assert_eq!(state.current_uid(), 0);
37        assert_eq!(state.current_gid(), 0);
38    }
39
40    #[test]
41    fn test_fake_root_state_set_current() {
42        let state = FakeRootState::new();
43        state.set_current(1234, 5678);
44        assert_eq!(state.current_uid(), 1234);
45        assert_eq!(state.current_gid(), 5678);
46    }
47
48    #[test]
49    fn test_fake_root_state_inode_mode() {
50        let state = FakeRootState::new();
51        let key = (1u64, 42u64);
52        let mut inode = FakeInode::new(0, 0);
53        inode.mode = Some(0o4755);
54        state.set_inode(key, inode.clone());
55        assert_eq!(state.get_inode(key), Some(inode));
56    }
57
58    #[test]
59    fn test_fake_root_state_upsert_chown() {
60        let state = FakeRootState::new();
61        state.upsert_chown((1, 2), 100, 200, 0, 0);
62        let inode = state.get_inode((1, 2)).unwrap();
63        assert_eq!(inode.uid, 100);
64        assert_eq!(inode.gid, 200);
65        state.upsert_chown((1, 2), FakeRootState::ID_UNCHANGED, 7, 0, 0);
66        let inode = state.get_inode((1, 2)).unwrap();
67        assert_eq!(inode.uid, 100);
68        assert_eq!(inode.gid, 7);
69    }
70
71    #[test]
72    fn test_fake_root_state_remove_inode() {
73        let state = FakeRootState::new();
74        let key = (1u64, 42u64);
75        let inode = FakeInode::new(3000, 4000);
76        state.set_inode(key, inode.clone());
77        assert_eq!(state.get_inode(key), Some(inode.clone()));
78
79        let removed = state.remove_inode(key);
80        assert_eq!(removed, Some(inode));
81        assert_eq!(state.get_inode(key), None);
82    }
83}