Skip to content

Commit

Permalink
kadmin: params, db_args: impl Clone, Sync, Send (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
rissson authored Dec 1, 2024
1 parent 47c3669 commit 74ae69e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 15 deletions.
26 changes: 26 additions & 0 deletions kadmin/src/db_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ pub struct DbArgs {
_ptr_vec: Vec<*mut c_char>,
}

// Pointees are contained in the struct itself
unsafe impl Send for DbArgs {}
unsafe impl Sync for DbArgs {}

impl Clone for DbArgs {
fn clone(&self) -> Self {
let mut _origin_args = vec![];
let mut _ptr_vec = vec![];
for arg in &self._origin_args {
let c_arg = arg.clone();
_ptr_vec.push(c_arg.as_ptr().cast_mut());
_origin_args.push(c_arg);
}
// Null terminated
_ptr_vec.push(null_mut());

let db_args = _ptr_vec.as_mut_ptr();

DbArgs {
db_args,
_origin_args,
_ptr_vec,
}
}
}

impl DbArgs {
/// Construct a new [`DbArgsBuilder`]
pub fn builder() -> DbArgsBuilder {
Expand Down
124 changes: 109 additions & 15 deletions kadmin/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,97 @@ pub struct Params {
/// doesn't become invalid while this struct lives.
pub(crate) params: kadm5_config_params,

/// Store [`CString`] that are in `params`
_strings: Vec<Option<CString>>,
/// Store [`CString`] that is in `params`
_realm: Option<CString>,
/// Store [`CString`] that is in `params`
_admin_server: Option<CString>,
/// Store [`CString`] that is in `params`
_dbname: Option<CString>,
/// Store [`CString`] that is in `params`
_acl_file: Option<CString>,
/// Store [`CString`] that is in `params`
_dict_file: Option<CString>,
/// Store [`CString`] that is in `params`
_stash_file: Option<CString>,
}

// Pointees are contained in the struct itself
unsafe impl Send for Params {}
unsafe impl Sync for Params {}

impl Clone for Params {
fn clone(&self) -> Self {
let _realm = self._realm.clone();
let _admin_server = self._admin_server.clone();
let _dbname = self._dbname.clone();
let _acl_file = self._acl_file.clone();
let _dict_file = self._dict_file.clone();
let _stash_file = self._stash_file.clone();
Self {
params: kadm5_config_params {
mask: self.params.mask,
realm: if let Some(realm) = &_realm {
realm.as_ptr().cast_mut()
} else {
null_mut()
},
kadmind_port: self.params.kadmind_port,
kpasswd_port: self.params.kpasswd_port,

admin_server: if let Some(admin_server) = &_admin_server {
admin_server.as_ptr().cast_mut()
} else {
null_mut()
},

dbname: if let Some(dbname) = &_dbname {
dbname.as_ptr().cast_mut()
} else {
null_mut()
},
acl_file: if let Some(acl_file) = &_acl_file {
acl_file.as_ptr().cast_mut()
} else {
null_mut()
},
dict_file: if let Some(dict_file) = &_dict_file {
dict_file.as_ptr().cast_mut()
} else {
null_mut()
},
mkey_from_kbd: 0,
stash_file: if let Some(stash_file) = &_stash_file {
stash_file.as_ptr().cast_mut()
} else {
null_mut()
},
mkey_name: null_mut(),
enctype: 0,
max_life: 0,
max_rlife: 0,
expiration: 0,
flags: 0,
keysalts: null_mut(),
num_keysalts: 0,
kvno: 0,
iprop_enabled: 0,
iprop_ulogsize: 0,
iprop_poll_time: 0,
iprop_logfile: null_mut(),
iprop_port: 0,
iprop_resync_timeout: 0,
kadmind_listen: null_mut(),
kpasswd_listen: null_mut(),
iprop_listen: null_mut(),
},
_realm,
_admin_server,
_dbname,
_acl_file,
_dict_file,
_stash_file,
}
}
}

impl Params {
Expand Down Expand Up @@ -124,46 +213,46 @@ impl ParamsBuilder {

/// Construct [`Params`] from the provided options
pub fn build(self) -> Result<Params> {
let realm = self.realm.map(CString::new).transpose()?;
let admin_server = self.admin_server.map(CString::new).transpose()?;
let dbname = self.dbname.map(CString::new).transpose()?;
let acl_file = self.acl_file.map(CString::new).transpose()?;
let dict_file = self.dict_file.map(CString::new).transpose()?;
let stash_file = self.stash_file.map(CString::new).transpose()?;
let _realm = self.realm.map(CString::new).transpose()?;
let _admin_server = self.admin_server.map(CString::new).transpose()?;
let _dbname = self.dbname.map(CString::new).transpose()?;
let _acl_file = self.acl_file.map(CString::new).transpose()?;
let _dict_file = self.dict_file.map(CString::new).transpose()?;
let _stash_file = self.stash_file.map(CString::new).transpose()?;

let params = kadm5_config_params {
mask: self.mask,
realm: if let Some(realm) = &realm {
realm: if let Some(realm) = &_realm {
realm.as_ptr().cast_mut()
} else {
null_mut()
},
kadmind_port: self.kadmind_port,
kpasswd_port: self.kpasswd_port,

admin_server: if let Some(admin_server) = &admin_server {
admin_server: if let Some(admin_server) = &_admin_server {
admin_server.as_ptr().cast_mut()
} else {
null_mut()
},

dbname: if let Some(dbname) = &dbname {
dbname: if let Some(dbname) = &_dbname {
dbname.as_ptr().cast_mut()
} else {
null_mut()
},
acl_file: if let Some(acl_file) = &acl_file {
acl_file: if let Some(acl_file) = &_acl_file {
acl_file.as_ptr().cast_mut()
} else {
null_mut()
},
dict_file: if let Some(dict_file) = &dict_file {
dict_file: if let Some(dict_file) = &_dict_file {
dict_file.as_ptr().cast_mut()
} else {
null_mut()
},
mkey_from_kbd: 0,
stash_file: if let Some(stash_file) = &stash_file {
stash_file: if let Some(stash_file) = &_stash_file {
stash_file.as_ptr().cast_mut()
} else {
null_mut()
Expand All @@ -190,7 +279,12 @@ impl ParamsBuilder {

Ok(Params {
params,
_strings: vec![realm, admin_server, dbname, acl_file, dict_file, stash_file],
_realm,
_admin_server,
_dbname,
_acl_file,
_dict_file,
_stash_file,
})
}
}
Expand Down

0 comments on commit 74ae69e

Please sign in to comment.