Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing multiple FD over an Unix Socket in a single Control Message doesn't work #464

Closed
roblabla opened this issue Nov 13, 2016 · 1 comment · Fixed by #918
Closed

Passing multiple FD over an Unix Socket in a single Control Message doesn't work #464

roblabla opened this issue Nov 13, 2016 · 1 comment · Fixed by #918

Comments

@roblabla
Copy link
Contributor

roblabla commented Nov 13, 2016

For some reason, passing multiple FD over a Unix Domain Socket doesn't seem to work. Below is some test code that is supposed to send two fd over a Unix Socket along with 8 bytes of data. For some reason, only one FD is sent though, the other is lost in the void.

extern crate nix;

use std::os::unix::net::UnixDatagram;
use std::os::unix::io::{RawFd, AsRawFd};
use nix::sys::socket::{CmsgSpace, ControlMessage, MsgFlags, sendmsg, recvmsg};
use nix::sys::uio::IoVec;


fn main() {
    let (send, receive) = UnixDatagram::pair().unwrap();
    let thread = std::thread::spawn(move || {
        let mut buf = [0u8; 8];
        let iovec = [IoVec::from_mut_slice(&mut buf)];
        let mut space = CmsgSpace::<[RawFd; 2]>::new();
        let (paneid, fds) = match recvmsg(receive.as_raw_fd(), &iovec, Some(&mut space), MsgFlags::empty()) {
            Ok(msg) => {
                let mut iter = msg.cmsgs();
                if let Some(ControlMessage::ScmRights(fds)) = iter.next() {
                    (iovec[0].as_slice(), fds.to_vec())
                } else {
                    panic!();
                }
            },
            Err(_) => {
                panic!();
            }
        };
        println!("paneid = {:?}, fds = {:?}", paneid, fds);
    });

    let slice = [1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8];
    let iov = [IoVec::from_slice(&slice)];
    let arr = [0, 1]; // Pass stdin and stdout
    let cmsg = [ControlMessage::ScmRights(&arr)];
    sendmsg(send.as_raw_fd(), &iov, &cmsg, MsgFlags::empty(), None).unwrap();
    thread.join().unwrap();
}

The output is paneid = [1, 2, 3, 4, 5, 6, 7, 8], fds = [6]

@roblabla
Copy link
Contributor Author

roblabla commented Nov 13, 2016

Similarly, sending multiple control messages doesn't seem to work either. The following code panics at runtime on the sendmsg line, with the error thread 'main' panicked at 'called``Result::unwrap()``on an``Err``value: Sys(EBADF)', src/libcore/result.rs:788 :

extern crate nix;

use std::os::unix::net::UnixDatagram;
use std::os::unix::io::{RawFd, AsRawFd};
use nix::sys::socket::{CmsgSpace, ControlMessage, MsgFlags, sendmsg, recvmsg};
use nix::sys::uio::IoVec;


fn main() {
    let (send, receive) = UnixDatagram::pair().unwrap();
    let thread = std::thread::spawn(move || {
        let mut buf = [0u8; 8];
        let iovec = [IoVec::from_mut_slice(&mut buf)];
        let mut space = CmsgSpace::<([RawFd; 1], CmsgSpace<[RawFd; 1]>)>::new();
        let (paneid, fds) = match recvmsg(receive.as_raw_fd(), &iovec, Some(&mut space), MsgFlags::empty()) {
            Ok(msg) => {
                let mut iter = msg.cmsgs();
                let fdone;
                let fdtwo;
                if let Some(ControlMessage::ScmRights(fds)) = iter.next() {
                    fdone = fds[0];
                } else {
                    panic!();
                }
                if let Some(ControlMessage::ScmRights(fds)) = iter.next() {
                    fdtwo = fds[0];
                } else {
                    panic!();
                }
                (iovec[0].as_slice(), [fdone, fdtwo])
            },
            Err(_) => {
                panic!();
            }
        };
        println!("paneid = {:?}, fds = {:?}", paneid, fds);
    });

    let slice = [1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8];
    let iov = [IoVec::from_slice(&slice)];
    let arrone = [0];
    let arrtwo = [1]; // pass stdin and stdout
    let cmsg = [ControlMessage::ScmRights(&arrone), ControlMessage::ScmRights(&arrtwo)];
    sendmsg(send.as_raw_fd(), &iov, &cmsg, MsgFlags::empty(), None).unwrap();
    thread.join().unwrap();
}

roblabla added a commit to roblabla/nix that referenced this issue Sep 1, 2017
bors bot added a commit that referenced this issue Jul 5, 2018
918: Fix passing multiple file descriptors / control messages via sendmsg r=asomers a=jonas-schievink

Fixes #464
Closes #874 because it's incorporated here
Closes #756 because it adds the test from that issue (with fixes)

Co-authored-by: alecmocatta <[email protected]>
bors bot added a commit that referenced this issue Jul 5, 2018
918: Fix passing multiple file descriptors / control messages via sendmsg r=asomers a=jonas-schievink

Fixes #464
Closes #874 because it's incorporated here
Closes #756 because it adds the test from that issue (with fixes)

Co-authored-by: alecmocatta <[email protected]>
@bors bors bot closed this as completed in #918 Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants