diff options
| author | Emīls <emils@mullvad.net> | 2021-11-26 13:26:28 +0000 |
|---|---|---|
| committer | Emīls <emils@mullvad.net> | 2021-12-10 09:58:51 +0000 |
| commit | 6f562ca687bc6897eab1aea1bd97b95d64932ef8 (patch) | |
| tree | 3a4f09f5421e7bf223608fde88496b5fa16306be | |
| parent | b151c0659cf87b243c8306c45a9b68d257a144dc (diff) | |
| download | mullvadvpn-6f562ca687bc6897eab1aea1bd97b95d64932ef8.tar.xz mullvadvpn-6f562ca687bc6897eab1aea1bd97b95d64932ef8.zip | |
Rework get_group_id()
| -rw-r--r-- | talpid-core/src/macos.rs | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/talpid-core/src/macos.rs b/talpid-core/src/macos.rs index 79de8570e1..49f9ee6b2f 100644 --- a/talpid-core/src/macos.rs +++ b/talpid-core/src/macos.rs @@ -1,24 +1,23 @@ use std::{ffi::CStr, io}; /// Returns the GID of the specified group name -pub fn get_group_id(group_name: &CStr) -> Option<u32> { +pub fn get_group_id(group_name: &CStr) -> io::Result<u32> { // SAFETY: group_name is a valid CString let group = unsafe { libc::getgrnam(group_name.as_ptr() as *const _) }; if group.is_null() { - return None; + return Err(io::Error::from(io::ErrorKind::NotFound)); } // SAFETY: group is not null let gid = unsafe { (*group).gr_gid }; - Some(gid) + Ok(gid) } /// Sets group ID for the current process pub fn set_gid(gid: u32) -> io::Result<()> { - let result = unsafe { libc::setgid(gid) }; - if result == 0 { + if unsafe { libc::setgid(gid) } == 0 { Ok(()) } else { - Err(io::Error::from_raw_os_error(result)) + Err(io::Error::last_os_error()) } } @@ -30,7 +29,7 @@ pub fn bump_filehandle_limit() { rlim_max: 0, }; // SAFETY: `&mut limits` is a valid pointer parameter for the getrlimit syscall - let status = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits as *mut _) }; + let status = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) }; if status != 0 { log::error!( "Failed to get file handle limits: {}-{}", @@ -57,3 +56,12 @@ pub fn bump_filehandle_limit() { ); } } + +#[cfg(test)] +#[test] +fn test_unknown_group() { + let unknown_group = CStr::from_bytes_with_nul(b"asdunknown\0").unwrap(); + let group_err = get_group_id(unknown_group).unwrap_err(); + assert!(group_err.kind() == io::ErrorKind::NotFound) + +} |
