7 releases

0.1.6 Feb 24, 2023
0.1.5 Dec 3, 2022
0.1.4 May 14, 2020
0.1.3 Apr 13, 2020
0.1.1 Mar 13, 2019

#470 in Unix APIs

Download history 2579/week @ 2024-06-03 2976/week @ 2024-06-10 2242/week @ 2024-06-17 3676/week @ 2024-06-24 2808/week @ 2024-07-01 4189/week @ 2024-07-08 3602/week @ 2024-07-15 3539/week @ 2024-07-22 3366/week @ 2024-07-29 2955/week @ 2024-08-05 4139/week @ 2024-08-12 3631/week @ 2024-08-19 5348/week @ 2024-08-26 5009/week @ 2024-09-02 7014/week @ 2024-09-09 8651/week @ 2024-09-16

26,058 downloads per month
Used in 15 crates (3 directly)

MIT license

39KB
355 lines

passfd

Build Status

Unix sockets possess magic ability to transfer file descriptors from one process to another (unrelated) process using obscure SCM_RIGHTS API. This little crate adds extension methods to UnixStream to use it.


lib.rs:

passfd allows passing file descriptors between unrelated processes using Unix sockets.

Both tokio 0.1 and 0.2 are supported with tokio_01 and tokio_02 features. Please note that these features rely on internal representation of UnixStream and are unsafe.

Example usage

Process 1 (sender)

use passfd::FdPassingExt;
use std::fs::File;
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixListener;

let file = File::open("/etc/passwd").unwrap();
let listener = UnixListener::bind("/tmp/test.sock").unwrap();
let (stream, _) = listener.accept().unwrap();
stream.send_fd(file.as_raw_fd()).unwrap();

Process 2 (receiver)

use passfd::FdPassingExt;
use std::fs::File;
use std::io::Read;
use std::os::unix::io::FromRawFd;
use std::os::unix::net::UnixStream;

let stream = UnixStream::connect("/tmp/test.sock").unwrap();
let fd = stream.recv_fd().unwrap();
let mut file = unsafe { File::from_raw_fd(fd) };
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
println!("{}", buf);

Dependencies

~0–8.5MB
~70K SLoC