1use super::*;
42
43#[derive(Copy, Clone)]
44pub(crate) struct RealLibc;
45
46if_cfg_getresuid! {
47 #[allow(non_camel_case_types)]
48 pub(crate) type LibcFn_getresid<I> =
49 unsafe extern "C" fn(ruid: *mut I, euid: *mut I, suid: *mut I) -> c_int;
50}
51
52#[allow(non_camel_case_types)]
53pub(crate) type LibcFn_getgroups =
54 unsafe extern "C" fn(ngroups_max: c_int, groups: *mut gid_t) -> c_int;
55
56macro_rules! define_lmockable_type { {
57 $function:ident, $passwd:ident, $key:ty
58} => { paste!{
59 #[allow(non_camel_case_types)]
60 pub(crate) type [< LibcFn_ $function >] = unsafe extern "C" fn(
61 $key,
62 *mut libc::$passwd,
63 *mut c_char,
64 size_t,
65 *mut *mut libc::$passwd,
66 ) -> c_int;
67} } }
68
69define_lmockable_type!(getpwnam, passwd, *const c_char);
70define_lmockable_type!(getpwuid, passwd, uid_t);
71define_lmockable_type!(getgrnam, group, *const c_char);
72define_lmockable_type!(getgrgid, group, gid_t);
73
74#[derive(Deftly)]
75#[derive_deftly_adhoc]
76pub(crate) struct MockableLibcFunctions {
77 pub(crate) sysconf: unsafe extern "C" fn(c_int) -> c_long,
78 pub(crate) getuid: unsafe extern "C" fn() -> uid_t,
79 pub(crate) geteuid: unsafe extern "C" fn() -> uid_t,
80 pub(crate) getgid: unsafe extern "C" fn() -> gid_t,
81 pub(crate) getegid: unsafe extern "C" fn() -> gid_t,
82 #[cfg(not(any(target_os = "macos", target_os = "netbsd")))]
84 pub(crate) getresuid: LibcFn_getresid<uid_t>,
85 #[cfg(not(any(target_os = "macos", target_os = "netbsd")))]
87 pub(crate) getresgid: LibcFn_getresid<gid_t>,
88 pub(crate) getgroups: LibcFn_getgroups,
89 pub(crate) getpwnam_r: LibcFn_getpwnam,
90 pub(crate) getpwuid_r: LibcFn_getpwuid,
91 pub(crate) getgrnam_r: LibcFn_getgrnam,
92 pub(crate) getgrgid_r: LibcFn_getgrgid,
93}
94
95derive_deftly_adhoc! {
96 MockableLibcFunctions expect items:
97
98 impl Deref for RealLibc {
99 type Target = MockableLibcFunctions;
100
101 fn deref(&self) -> &MockableLibcFunctions {
102 static M: MockableLibcFunctions = MockableLibcFunctions {
103 $(
104 $fname: libc::$fname,
105 )
106 };
107 &M
108 }
109 }
110}
111
112pub(crate) trait MockableLibc:
113 Copy + Deref<Target = MockableLibcFunctions>
114{
115}
116
117impl MockableLibc for RealLibc {}