lib/socket-util-unix.c
/* Creates a Unix domain socket in the given 'style' (either SOCK_DGRAM or
 * SOCK_STREAM) that is bound to '*bind_path' (if 'bind_path' is non-null) and
 * connected to '*connect_path' (if 'connect_path' is non-null).  If 'nonblock'
 * is true, the socket is made non-blocking.
 *
 * Returns the socket's fd if successful, otherwise a negative errno value. */


Returns the socket's fd if successful, otherwise a negative errno value.
Returns the socket ' s fd if successful otherwise a negative errno value .
int
make_unix_socket(int style, bool nonblock,
                 const char *bind_path, const char *connect_path)
{
    int error;
    int fd;
    fd = socket(PF_UNIX, style, 0);
    if (fd < 0) {
        return -errno;
    }
    if (nonblock) {
        error = set_nonblocking(fd);
        if (error) {
            goto error;
        }
    }
    if (bind_path) {
        char linkname[MAX_UN_LEN + 1];
        struct sockaddr_un un;
        socklen_t un_len;
        int dirfd;
        if (unlink(bind_path) && errno != ENOENT) {
            VLOG_WARN("unlinking \"%s\": %s\n",
                      bind_path, ovs_strerror(errno));
        }
        fatal_signal_add_file_to_unlink(bind_path);
        error = make_sockaddr_un(bind_path, &un, &un_len, &dirfd, linkname);
        if (!error) {
            error = bind_unix_socket(fd, (struct sockaddr *) &un, un_len);
        }
        free_sockaddr_un(dirfd, linkname);
        if (error) {
            goto error;
        }
    }
    if (connect_path) {
        char linkname[MAX_UN_LEN + 1];
        struct sockaddr_un un;
        socklen_t un_len;
        int dirfd;
        error = make_sockaddr_un(connect_path, &un, &un_len, &dirfd, linkname);
        if (!error
            && connect(fd, (struct sockaddr*) &un, un_len)
            && errno != EINPROGRESS) {
            error = errno;
        }
        free_sockaddr_un(dirfd, linkname);
        if (error) {
            goto error;
        }
    }
    return fd;
error:
    if (error == EAGAIN) {
        error = EPROTO;
    }
    if (bind_path) {
        fatal_signal_unlink_file_now(bind_path);
    }
    close(fd);
    return -error;
}
int make_unix_socket ( int style , bool nonblock , const char * bind_path , const char * connect_path ) { int error ; int fd ; fd = socket ( PF_UNIX , style , 0 ) ; if ( fd < 0 ) { return - errno ; } if ( nonblock ) { error = set_nonblocking ( fd ) ; if ( error ) { goto error ; } } if ( bind_path ) { char linkname [ MAX_UN_LEN + 1 ] ; struct sockaddr_un un ; socklen_t un_len ; int dirfd ; if ( unlink ( bind_path ) && errno != ENOENT ) { VLOG_WARN ( " \" \" \n " , bind_path , ovs_strerror ( errno ) ) ; } fatal_signal_add_file_to_unlink ( bind_path ) ; error = make_sockaddr_un ( bind_path , & un , & un_len , & dirfd , linkname ) ; if ( ! error ) { error = bind_unix_socket ( fd , ( struct sockaddr * ) & un , un_len ) ; } free_sockaddr_un ( dirfd , linkname ) ; if ( error ) { goto error ; } } if ( connect_path ) { char linkname [ MAX_UN_LEN + 1 ] ; struct sockaddr_un un ; socklen_t un_len ; int dirfd ; error = make_sockaddr_un ( connect_path , & un , & un_len , & dirfd , linkname ) ; if ( ! error && connect ( fd , ( struct sockaddr * ) & un , un_len ) && errno != EINPROGRESS ) { error = errno ; } free_sockaddr_un ( dirfd , linkname ) ; if ( error ) { goto error ; } } return fd ; error : if ( error == EAGAIN ) { error = EPROTO ; } if ( bind_path ) { fatal_signal_unlink_file_now ( bind_path ) ; } close ( fd ) ; return - error ; }
Creates a Unix domain socket in the given 'style' (either SOCK_DGRAM or
 SOCK_STREAM) that is bound to '*bind_path' (if 'bind_path' is non-null) and
 connected to '*connect_path' (if 'connect_path' is non-null).
Creates a Unix domain socket in the given ' style ' ( either SOCK_DGRAM or SOCK_STREAM ) that is bound to ' * bind_path ' ( if ' bind_path ' is non - null ) and connected to ' * connect_path ' ( if ' connect_path ' is non - null ) .
/* Set nonblocking mode right away, if we want it.  This prevents blocking
     * in connect(), if connect_path != NULL.  (In turn, that's a corner case:
     * it will only happen if style is SOCK_STREAM or SOCK_SEQPACKET, and only
     * if a backlog of un-accepted connections has built up in the kernel.)  */