This repository has been archived by the owner on May 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Implement safe sendmsg/recvmsg abstractions #16
Open
teisenbe
wants to merge
5
commits into
rust-lang-nursery:master
Choose a base branch
from
teisenbe:sendmsg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extern crate gcc; | ||
|
||
fn main() { | ||
gcc::compile_library("libcmsg_manip.a", &["src/cmsg_manip/cmsg.c"]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
extern crate libc; | ||
extern crate unix_socket; | ||
|
||
use std::os::unix::io::{AsRawFd, FromRawFd}; | ||
use std::path::Path; | ||
|
||
#[cfg(feature = "sendmsg")] | ||
use unix_socket::{ControlMsg, UCred, UnixDatagram, RecvMsgFlags, SendMsgFlags}; | ||
|
||
#[cfg(feature = "sendmsg")] | ||
fn handle_parent(sock: UnixDatagram) { | ||
let (parent2, child2) = UnixDatagram::pair().unwrap(); | ||
|
||
let cmsg = ControlMsg::Rights(vec![child2.as_raw_fd()]); | ||
let cmsg2 = unsafe { ControlMsg::Credentials(UCred{ | ||
pid: libc::getpid(), | ||
uid: libc::getuid(), | ||
gid: libc::getgid(), | ||
}) }; | ||
println!("cmsg {:?}", cmsg2); | ||
let sent_bytes = sock.sendmsg::<&Path>(None, &[&[]], &[cmsg, cmsg2], SendMsgFlags::new()).unwrap(); | ||
assert_eq!(sent_bytes, 0); | ||
drop(child2); | ||
println!("Parent sent child SCM_RIGHTS fd"); | ||
|
||
let mut buf = &mut [0u8; 4096]; | ||
let read = parent2.recv(buf).unwrap(); | ||
assert_eq!(&buf[..read], "Hello, world!".as_bytes()); | ||
println!("Parent received message from child via SCM_RIGHTS fd"); | ||
} | ||
|
||
#[cfg(feature = "sendmsg")] | ||
fn handle_child(sock: UnixDatagram) { | ||
sock.set_passcred(true).unwrap(); | ||
let mut cmsg_buf = &mut [0u8; 4096]; | ||
let flags = RecvMsgFlags::new().cmsg_cloexec(true); | ||
let result = sock.recvmsg(&[&mut[]], cmsg_buf, flags).unwrap(); | ||
assert_eq!(result.control_msgs.len(), 2); | ||
|
||
let mut new_sock = None; | ||
let mut creds = None; | ||
for cmsg in result.control_msgs { | ||
match cmsg.clone() { | ||
ControlMsg::Rights(fds) => { | ||
assert!(new_sock.is_none()); | ||
assert_eq!(fds.len(), 1); | ||
unsafe { | ||
new_sock = Some(UnixDatagram::from_raw_fd(fds[0])); | ||
} | ||
println!("Child received SCM_RIGHTS fd"); | ||
}, | ||
ControlMsg::Credentials(ucred) => { | ||
assert!(creds.is_none()); | ||
creds = Some(ucred); | ||
println!("Child received SCM_CREDENTIALS"); | ||
}, | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
let creds = creds.unwrap(); | ||
unsafe { | ||
assert_eq!(creds.uid, libc::getuid()); | ||
assert_eq!(creds.gid, libc::getgid()); | ||
assert!(creds.pid != 0); | ||
} | ||
let sent = new_sock.unwrap().send("Hello, world!".as_bytes()).unwrap(); | ||
println!("Child sent message to parent via SCM_RIGHTS fd"); | ||
assert_eq!(sent, 13); | ||
} | ||
|
||
#[cfg(feature = "sendmsg")] | ||
fn main() { | ||
let (parent_sock, child_sock) = UnixDatagram::pair().unwrap(); | ||
let pid = unsafe { libc::fork() }; | ||
if pid == 0 { | ||
handle_child(child_sock); | ||
} else { | ||
handle_parent(parent_sock); | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "sendmsg"))] | ||
fn main() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Need to use GNU_SOURCE for ucred struct | ||
#define _GNU_SOURCE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this specifically necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nevermind, looks like it's required to get ucred. |
||
#include <sys/socket.h> | ||
|
||
size_t cmsghdr_size = sizeof(struct cmsghdr); | ||
size_t iovec_size = sizeof(struct iovec); | ||
size_t msghdr_size = sizeof(struct msghdr); | ||
size_t ucred_size = sizeof(struct ucred); | ||
|
||
int scm_credentials = SCM_CREDENTIALS; | ||
int scm_rights = SCM_RIGHTS; | ||
int so_passcred = SO_PASSCRED; | ||
|
||
int msg_eor = MSG_EOR; | ||
int msg_trunc = MSG_TRUNC; | ||
int msg_ctrunc = MSG_CTRUNC; | ||
int msg_errqueue = MSG_ERRQUEUE; | ||
int msg_dontwait = MSG_DONTWAIT; | ||
int msg_cmsg_cloexec = MSG_CMSG_CLOEXEC; | ||
int msg_nosignal = MSG_NOSIGNAL; | ||
int msg_peek = MSG_PEEK; | ||
int msg_waitall = MSG_WAITALL; | ||
|
||
struct cmsghdr * cmsg_firsthdr(struct msghdr *msgh) { | ||
return CMSG_FIRSTHDR(msgh); | ||
} | ||
|
||
struct cmsghdr * cmsg_nxthdr(struct msghdr *msgh, struct cmsghdr *cmsg) { | ||
return CMSG_NXTHDR(msgh, cmsg); | ||
} | ||
|
||
size_t cmsg_align(size_t length) { | ||
return CMSG_ALIGN(length); | ||
} | ||
|
||
size_t cmsg_space(size_t length) { | ||
return CMSG_SPACE(length); | ||
} | ||
|
||
size_t cmsg_len(size_t length) { | ||
return CMSG_LEN(length); | ||
} | ||
|
||
unsigned char * cmsg_data(struct cmsghdr *cmsg) { | ||
return CMSG_DATA(cmsg); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what contexts would people want to disable this feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the SCM_RIGHTS stuff isn't portable past Linux (POSIX doesn't define what gets passed in the message, I believe)