about summary refs log tree commit diff
path: root/src/facility
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-21 00:45:48 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-21 00:45:48 -0700
commitf6cb132d4e026db2b5fcb467bca50eab1c50f04c (patch)
treefc1e7452c9f6d1c2902cf03a3d784dba4e1e30f4 /src/facility
parent8a06e8a8aae8c77de1d93833edaeed4d9cb5fb1d (diff)
perform the ioctl()
and clean up the returned memory, too

Force-Push: yes
Change-Id: I01ff1c94fd97a185656a05ce432471fa67cb08e5
Diffstat (limited to 'src/facility')
-rw-r--r--src/facility/envsys.rs63
1 files changed, 60 insertions, 3 deletions
diff --git a/src/facility/envsys.rs b/src/facility/envsys.rs
index 34068cc..31ad2de 100644
--- a/src/facility/envsys.rs
+++ b/src/facility/envsys.rs
@@ -2,14 +2,32 @@ use crate::error::Result;
 use crate::facility::{ CreatableFacility, Facility, FacilityType };
 
 use std::any::Any;
+use std::ffi::{ c_void, OsStr };
+use std::os::unix::ffi::OsStrExt;
+use std::slice::from_raw_parts;
 
 use async_fs::File;
+use rustix::ioctl::{ ioctl, Opcode, Getter };
+use rustix::ioctl::opcode::read_write;
+use rustix::mm::munmap;
 
 
 pub struct EnvSys {
   sysmon: File,
 }
 
+#[repr(C)]
+struct PropertyList {
+}
+
+#[repr(C)]
+#[derive(Debug)]
+struct PropertyListReference {
+  data: *mut u8,
+  size: usize,
+}
+
+
 impl Facility for EnvSys {
   fn as_any_mut(&mut self) -> &mut dyn Any {
     self
@@ -23,10 +41,49 @@ impl CreatableFacility for EnvSys {
     println!("loading envsys");
     let sysmon = File::open("/dev/sysmon").await?;
 
-    Ok(EnvSys {
-      sysmon
-    })
+    let envsys = EnvSys { sysmon };
+
+    envsys.get_dictionary().await?;
+
+    Ok(envsys)
   }
+}
+
+impl EnvSys {
+  const ENVSYS_GETDICTIONARY: Opcode =
+            read_write::<PropertyListReference>(b'E', 0);
+  #[allow(dead_code)]
+  const ENVSYS_SETDICTIONARY: Opcode =
+            read_write::<PropertyListReference>(b'E', 1);
+  #[allow(dead_code)]
+  const ENVSYS_REMOVEPROPS: Opcode =
+            read_write::<PropertyListReference>(b'E', 2);
+
+  async fn get_dictionary(&self) -> Result<()> {
+    let call = unsafe {
+      Getter::<{ Self::ENVSYS_GETDICTIONARY }, PropertyListReference>::new()
+    };
+
+    /*   If the call fails, we can safely return because there's no resulting
+     * memory allocation.
+     */
+    let result = unsafe { ioctl(&self.sysmon, call) }?;
 
+    println!("foo {:?}", result);
+    let slice: &[u8] = unsafe { from_raw_parts(result.data, result.size) };
+    let xml = OsStr::from_bytes(slice);
+    println!("xml {:?}", xml);
+
+    /*   So. Fun story. Inspecting NetBSD src/common/lib/libprop/prop_kern.c,
+     * we find that the kernel creates a new memory mapping for the dictionary
+     * it returns. When we're done, we're expected to munmap() it. Wild.
+     *
+     *   Anyway, if munmap() fails we have no way to clean up regardless, so we
+     * just return.
+     */
+    unsafe { munmap(result.data as *mut c_void, result.size) }?;
+
+    Ok(())
+  }
 }