pub unsafe fn open_socket(cfg: &TcpPort, addr: &SockAddr)
    -> Result<(), io::Error>
{
    use libc::{socket, setsockopt, bind, listen, dup2, AF_INET, SOCK_STREAM};
    use libc::{SOL_SOCKET, SO_REUSEADDR, SO_REUSEPORT};
    use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK, EINTR};
    let s = match socket(AF_INET, SOCK_STREAM, 0) {
        -1 => return Err(io::Error::last_os_error()),
        s => s,
    };
    // NOTE: we don't close socket on error here, because process will die
    // after any error

    if cfg.reuse_addr {
        if setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
                      mem::transmute(&1u32), size_of::<u32>() as u32) == -1
        {
            return Err(io::Error::last_os_error());
        }
    }
    if cfg.reuse_port {
        if setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
                      mem::transmute(&1u32), size_of::<u32>() as u32) == -1
        {
            return Err(io::Error::last_os_error());
        }
    }
    if cfg.set_non_block {
        let fl = loop {
            let fl = fcntl(s, F_GETFL);
            if fl == -1 {
                let err = io::Error::last_os_error();
                if err.raw_os_error() == Some(EINTR) {
                    continue;
                }
                return Err(err);
            }
            break fl;
        };
        if fcntl(s, F_SETFL, fl | O_NONBLOCK) == -1 {
            return Err(io::Error::last_os_error());
        }
    }
    let (sockaddr, len) = addr.as_ffi_pair();
    if bind(s, sockaddr, len) == -1 {
        return Err(io::Error::last_os_error());
    }
    if listen(s, cfg.listen_backlog as i32) == -1 {
        return Err(io::Error::last_os_error());
    }
    if s != cfg.fd {
        if dup2(s, cfg.fd) == -1 {
            return Err(io::Error::last_os_error());
        }
        close(s);
    }
    Ok(())
}
/// NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/// This function is executed in restricted child environment
///
/// No allocations, logging, etc. Just bare sys calls
NOTE
This function is executed in restricted child environment

No allocations, logging, etc. Just bare sys calls
NOTE This function is executed in restricted child environment No allocations logging etc . Just bare sys calls
pub unsafe fn open_socket(cfg: &TcpPort, addr: &SockAddr)
    -> Result<(), io::Error>
{
    use libc::{socket, setsockopt, bind, listen, dup2, AF_INET, SOCK_STREAM};
    use libc::{SOL_SOCKET, SO_REUSEADDR, SO_REUSEPORT};
    use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK, EINTR};
    let s = match socket(AF_INET, SOCK_STREAM, 0) {
        -1 => return Err(io::Error::last_os_error()),
        s => s,
    };
    // NOTE: we don't close socket on error here, because process will die
    // after any error
    if cfg.reuse_addr {
        if setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
                      mem::transmute(&1u32), size_of::<u32>() as u32) == -1
        {
            return Err(io::Error::last_os_error());
        }
    }
    if cfg.reuse_port {
        if setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
                      mem::transmute(&1u32), size_of::<u32>() as u32) == -1
        {
            return Err(io::Error::last_os_error());
        }
    }
    if cfg.set_non_block {
        let fl = loop {
            let fl = fcntl(s, F_GETFL);
            if fl == -1 {
                let err = io::Error::last_os_error();
                if err.raw_os_error() == Some(EINTR) {
                    continue;
                }
                return Err(err);
            }
            break fl;
        };
        if fcntl(s, F_SETFL, fl | O_NONBLOCK) == -1 {
            return Err(io::Error::last_os_error());
        }
    }
    let (sockaddr, len) = addr.as_ffi_pair();
    if bind(s, sockaddr, len) == -1 {
        return Err(io::Error::last_os_error());
    }
    if listen(s, cfg.listen_backlog as i32) == -1 {
        return Err(io::Error::last_os_error());
    }
    if s != cfg.fd {
        if dup2(s, cfg.fd) == -1 {
            return Err(io::Error::last_os_error());
        }
        close(s);
    }
    Ok(())
}
pub unsafe fn open_socket ( cfg : & TcpPort , addr : & SockAddr ) -> Result < ( ) , io :: Error > { use libc :: { socket , setsockopt , bind , listen , dup2 , AF_INET , SOCK_STREAM } ; use libc :: { SOL_SOCKET , SO_REUSEADDR , SO_REUSEPORT } ; use libc :: { fcntl , F_GETFL , F_SETFL , O_NONBLOCK , EINTR } ; let s = match socket ( AF_INET , SOCK_STREAM , 0 ) { - 1 => return Err ( io :: Error :: last_os_error ( ) ) , s => s , } ; // NOTE: we don't close socket on error here, because process will die // after any error if cfg . reuse_addr { if setsockopt ( s , SOL_SOCKET , SO_REUSEADDR , mem :: transmute ( & 1u32 ) , size_of :: < u32 > ( ) as u32 ) == - 1 { return Err ( io :: Error :: last_os_error ( ) ) ; } } if cfg . reuse_port { if setsockopt ( s , SOL_SOCKET , SO_REUSEPORT , mem :: transmute ( & 1u32 ) , size_of :: < u32 > ( ) as u32 ) == - 1 { return Err ( io :: Error :: last_os_error ( ) ) ; } } if cfg . set_non_block { let fl = loop { let fl = fcntl ( s , F_GETFL ) ; if fl == - 1 { let err = io :: Error :: last_os_error ( ) ; if err . raw_os_error ( ) == Some ( EINTR ) { continue ; } return Err ( err ) ; } break fl ; } ; if fcntl ( s , F_SETFL , fl | O_NONBLOCK ) == - 1 { return Err ( io :: Error :: last_os_error ( ) ) ; } } let ( sockaddr , len ) = addr . as_ffi_pair ( ) ; if bind ( s , sockaddr , len ) == - 1 { return Err ( io :: Error :: last_os_error ( ) ) ; } if listen ( s , cfg . listen_backlog as i32 ) == - 1 { return Err ( io :: Error :: last_os_error ( ) ) ; } if s != cfg . fd { if dup2 ( s , cfg . fd ) == - 1 { return Err ( io :: Error :: last_os_error ( ) ) ; } close ( s ) ; } Ok ( ( ) ) }
NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
cfg
&TcpPort
addr
&SockAddr
cfg
&TcpPort
addr
&SockAddr