Skip to content

Instantly share code, notes, and snippets.

@miquels
Created August 2, 2018 20:42
Show Gist options
  • Save miquels/5a15d362ec7c3528ade3431d9b637495 to your computer and use it in GitHub Desktop.
Save miquels/5a15d362ec7c3528ade3431d9b637495 to your computer and use it in GitHub Desktop.
enable SO_PEERCRED in Rust on a UNIX domain socket
extern crate libc;
extern crate tokio_uds;
use std::os::unix::io::AsRawFd;
// this is automatically enabled on stream socket - only for
// datagram sockets do you need to manually enable it.
fn enable_so_passcred(unix_socket: &tokio_uds::UnixListener) {
let fd = unix_socket.as_raw_fd();
let val: libc::c_int = 1;
let r = unsafe {
libc::setsockopt(fd,
libc::SOL_SOCKET,
libc::SO_PASSCRED,
&val as *const libc::c_int as *const libc::c_void,
std::mem::size_of::<libc::c_int>() as libc::socklen_t)
};
if r != 0 {
eprintln!("cannot set SO_PASSCRED");
std::process::exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment