/// Creates a new `UnixControlData` instance for the provided `message_header`. This includes
/// reading the associated data from the `task` (e.g., files from file descriptors).
Creates a new `UnixControlData` instance for the provided `message_header`. This includes
reading the associated data from the `task` .
Creates a new ` UnixControlData ` instance for the provided ` message_header ` . This includes reading the associated data from the ` task ` .
pub fn new(task: &Task, message_header: cmsghdr) -> Result<Self, Errno> {
        match message_header.cmsg_type {
            SCM_RIGHTS => {
                // Compute the number of file descriptors that fit in the provided bytes.
                let bytes_per_file_descriptor = std::mem::size_of::<FdNumber>();
                let num_file_descriptors = message_header.data_length() / bytes_per_file_descriptor;
                // Get the files associated with the provided file descriptors.
                let files = (0..num_file_descriptors * bytes_per_file_descriptor)
                    .step_by(bytes_per_file_descriptor)
                    .map(|index| NativeEndian::read_i32(&message_header.cmsg_data[index..]))
                    .map(|fd| task.files.get(FdNumber::from_raw(fd)))
                    .collect::<Result<Vec<FileHandle>, Errno>>()?;
                Ok(UnixControlData::Rights(files))
            }
            SCM_CREDENTIALS => {
                if message_header.data_length() < std::mem::size_of::<ucred>() {
                    return error!(EINVAL);
                }
                let credentials =
                    ucred::read_from(&message_header.cmsg_data[..std::mem::size_of::<ucred>()])
                        .ok_or(errno!(EINVAL))?;
                Ok(UnixControlData::Credentials(credentials))
            }
            SCM_SECURITY => Ok(UnixControlData::Security(
                message_header.cmsg_data[..message_header.cmsg_len].to_owned(),
            )),
            _ => return error!(EINVAL),
        }
    }
pub fn new ( task : & Task , message_header : cmsghdr ) -> Result < Self , Errno > { match message_header . cmsg_type { SCM_RIGHTS => { // Compute the number of file descriptors that fit in the provided bytes. let bytes_per_file_descriptor = std :: mem :: size_of :: < FdNumber > ( ) ; let num_file_descriptors = message_header . data_length ( ) / bytes_per_file_descriptor ; // Get the files associated with the provided file descriptors. let files = ( 0 .. num_file_descriptors * bytes_per_file_descriptor ) . step_by ( bytes_per_file_descriptor ) . map ( | index | NativeEndian :: read_i32 ( & message_header . cmsg_data [ index .. ] ) ) . map ( | fd | task . files . get ( FdNumber :: from_raw ( fd ) ) ) . collect :: < Result < Vec < FileHandle > , Errno > > ( ) ? ; Ok ( UnixControlData :: Rights ( files ) ) } SCM_CREDENTIALS => { if message_header . data_length ( ) < std :: mem :: size_of :: < ucred > ( ) { return error ! ( EINVAL ) ; } let credentials = ucred :: read_from ( & message_header . cmsg_data [ .. std :: mem :: size_of :: < ucred > ( ) ] ) . ok_or ( errno ! ( EINVAL ) ) ? ; Ok ( UnixControlData :: Credentials ( credentials ) ) } SCM_SECURITY => Ok ( UnixControlData :: Security ( message_header . cmsg_data [ .. message_header . cmsg_len ] . to_owned ( ) , ) ) , _ => return error ! ( EINVAL ) , } }
Creates a new `UnixControlData` instance for the provided `message_header`.
Creates a new ` UnixControlData ` instance for the provided ` message_header ` .
task
&Task
message_header
cmsghdr