diff src/libpam/pam_ffi.rs @ 98:b87100c5eed4

Start on environment variables, and make pointers nicer. This starts work on the PAM environment handling, and in so doing, introduces the CHeapBox and CHeapString structs. These are analogous to Box and CString, but they're located on the C heap rather than being Rust-managed memory. This is because environment variables deal with even more pointers and it turns out we can lose a lot of manual freeing using homemade smart pointers.
author Paul Fisher <paul@pfish.zone>
date Tue, 24 Jun 2025 04:25:25 -0400
parents efe2f5f8b5b2
children 94b51fa4f797
line wrap: on
line diff
--- a/src/libpam/pam_ffi.rs	Mon Jun 23 19:10:34 2025 -0400
+++ b/src/libpam/pam_ffi.rs	Tue Jun 24 04:25:25 2025 -0400
@@ -2,7 +2,7 @@
 
 #![allow(non_camel_case_types, non_upper_case_globals)]
 
-use crate::libpam::memory::Immovable;
+use crate::libpam::memory::{CHeapBox, Immovable};
 use std::ffi::{c_int, c_uint, c_void, CStr};
 use std::marker::PhantomData;
 use std::ptr;
@@ -26,15 +26,14 @@
 /// This has the same structure as [`BinaryAnswer`](crate::libpam::answer::BinaryAnswer)
 /// and [`TextAnswer`](crate::libpam::answer::TextAnswer).
 #[repr(C)]
-#[derive(Debug)]
+#[derive(Debug, Default)]
 pub struct Answer {
-    /// Pointer to the data returned in an answer.
-    /// For most answers, this will be a [`CStr`],
+    /// Owned pointer to the data returned in an answer.
+    /// For most answers, this will be a [`CHeapString`],
     /// but for [`BinaryQAndA`](crate::conv::BinaryQAndA)s (a Linux-PAM extension),
-    /// this will be [`CBinaryData`](crate::libpam::memory::CBinaryData).
-    ///
-    /// No matter what, this can be freed with a simple [`libc::free`].
-    pub data: *mut c_void,
+    /// this will be a [`CHeapBox`] of
+    /// [`CBinaryData`](crate::libpam::memory::CBinaryData).
+    pub data: Option<CHeapBox<c_void>>,
     /// Unused. Just here for the padding.
     return_code: c_int,
     _marker: Immovable,
@@ -57,7 +56,7 @@
     /// For most requests, this will be an owned [`CStr`],
     /// but for requests with style `PAM_BINARY_PROMPT`,
     /// this will be `CBinaryData` (a Linux-PAM extension).
-    pub data: *mut c_void,
+    pub data: Option<CHeapBox<c_void>>,
     pub _marker: Immovable,
 }