Skip to content

sendmsg fails with EINVAL when sending ScmRights over unconnected socket #884

@swsnr

Description

@swsnr

I can send a file descriptor over a connected unix socket just fine with the following code:

let target = UnixDatagram::unbound().unwrap();
let fd = write_and_seal("rustix_send_fd_connected_socket");

target.connect(JOURNALD_PATH).unwrap();
let fds = &[fd.as_fd()];
let scm_rights = rustix::net::SendAncillaryMessage::ScmRights(fds);
let mut buffer = vec![0; scm_rights.size()];
let mut buffer = SendAncillaryBuffer::new(&mut buffer);
assert!(buffer.push(scm_rights), "Failed to push ScmRights message");
sendmsg(&target, &[], &mut buffer, SendFlags::NOSIGNAL).unwrap();

I can also send an FD over an unconnected socket with libc, see https://github.com/swsnr/rustix-sendfd/blob/86540a6d37ee8670c77f467ddde3d7430d22ccb8/src/lib.rs#L74 (too long to paste here).

However, doing that in rustix with the following code fails with called Result::unwrap() on an Err value: Os { code: 22, kind: InvalidInput, message: "Invalid argument" } at sendmsg_unix:

let target = UnixDatagram::unbound().unwrap();
let fd = write_and_seal("rustix_send_fd_unconnected_socket");

let fds = &[fd.as_fd()];
let scm_rights = rustix::net::SendAncillaryMessage::ScmRights(fds);
let mut buffer = vec![0; scm_rights.size()];
let mut buffer = SendAncillaryBuffer::new(&mut buffer);
assert!(buffer.push(scm_rights), "Failed to push ScmRights message");
let sockaddr = SocketAddrUnix::new(JOURNALD_PATH).unwrap();
sendmsg_unix(&target, &sockaddr, &[], &mut buffer, SendFlags::NOSIGNAL).unwrap();

What did I do wrong here?

See https://github.com/swsnr/rustix-sendfd for a repo with three tests which reproduce this, and the corresponding workflow for the actual failure at https://github.com/swsnr/rustix-sendfd/actions/runs/6572703065/job/17854300669#step:4:33

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions