openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/nss-pam-ldapd/0.9.8-r0/nss-pam-ldapd-0.9.8/nslcd/nslcd.c
static int create_socket(const char* filename)
{
    int sock;
    int i;
    struct sockaddr_un addr;
    /* create a socket */
    if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
    {
        log_log(LOG_ERR, "cannot create socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (sock >= (int)FD_SETSIZE)
    {
        log_log(LOG_ERR, "socket file descriptor number too high (%d)", sock);
        exit(EXIT_FAILURE);
    }
    /* remove existing named socket */
    if (unlink(filename) < 0)
    {
        log_log(LOG_DEBUG, "unlink() of %s failed (ignored): %s", filename,
                strerror(errno));
    }
    /* do not block on accept() */
    if ((i = fcntl(sock, F_GETFL, 0)) < 0)
    {
        log_log(LOG_ERR, "fctnl(F_GETFL) failed: %s", strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (fcntl(sock, F_SETFL, i | O_NONBLOCK) < 0)
    {
        log_log(LOG_ERR, "fctnl(F_SETFL,O_NONBLOCK) failed: %s",
                strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    /* create the directory if needed */
    mkdirname(filename);
    /* create socket address structure */
    memset(&addr, 0, sizeof(struct sockaddr_un));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
    addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
    /* bind to the named socket */
    if (bind(sock, (struct sockaddr*)&addr, SUN_LEN(&addr)))
    {
        log_log(LOG_ERR, "bind() to %s failed: %s", filename, strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    /* close the file descriptor on exec */
    if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
    {
        log_log(LOG_ERR, "fctnl(F_SETFL,FD_CLOEXEC) on %s failed: %s", filename,
                strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    /* set permissions of socket so anybody can do requests */
    /* Note: we use chmod() here instead of fchmod() because
       fchmod does not work on sockets
       http://www.opengroup.org/onlinepubs/009695399/functions/fchmod.html
       http://lkml.org/lkml/2005/5/16/11 */
    if (chmod(filename, (mode_t)0666))
    {
        log_log(LOG_ERR, "chmod(0666) of %s failed: %s", filename,
                strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    /* start listening for connections */
    if (listen(sock, SOMAXCONN) < 0)
    {
        log_log(LOG_ERR, "listen() failed: %s", strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    /* we're done */
    return sock;
}
/* returns a socket ready to answer requests from the client,
   exit()s on error */
returns a socket ready to answer requests from the client,
exit()s on error
returns a socket ready to answer requests from the client exit () s on error
static int create_socket(const char* filename)
{
    int sock;
    int i;
    struct sockaddr_un addr;
    if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
    {
        log_log(LOG_ERR, "cannot create socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (sock >= (int)FD_SETSIZE)
    {
        log_log(LOG_ERR, "socket file descriptor number too high (%d)", sock);
        exit(EXIT_FAILURE);
    }
    if (unlink(filename) < 0)
    {
        log_log(LOG_DEBUG, "unlink() of %s failed (ignored): %s", filename,
                strerror(errno));
    }
    if ((i = fcntl(sock, F_GETFL, 0)) < 0)
    {
        log_log(LOG_ERR, "fctnl(F_GETFL) failed: %s", strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (fcntl(sock, F_SETFL, i | O_NONBLOCK) < 0)
    {
        log_log(LOG_ERR, "fctnl(F_SETFL,O_NONBLOCK) failed: %s",
                strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    mkdirname(filename);
    memset(&addr, 0, sizeof(struct sockaddr_un));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
    addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
    if (bind(sock, (struct sockaddr*)&addr, SUN_LEN(&addr)))
    {
        log_log(LOG_ERR, "bind() to %s failed: %s", filename, strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
    {
        log_log(LOG_ERR, "fctnl(F_SETFL,FD_CLOEXEC) on %s failed: %s", filename,
                strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (chmod(filename, (mode_t)0666))
    {
        log_log(LOG_ERR, "chmod(0666) of %s failed: %s", filename,
                strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (listen(sock, SOMAXCONN) < 0)
    {
        log_log(LOG_ERR, "listen() failed: %s", strerror(errno));
        if (close(sock))
            log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno));
        exit(EXIT_FAILURE);
    }
    return sock;
}
static int create_socket ( const char * filename ) { int sock ; int i ; struct sockaddr_un addr ; if ( ( sock = socket ( PF_UNIX , SOCK_STREAM , 0 ) ) < 0 ) { log_log ( LOG_ERR , " " , strerror ( errno ) ) ; exit ( EXIT_FAILURE ) ; } if ( sock >= ( int ) FD_SETSIZE ) { log_log ( LOG_ERR , " " , sock ) ; exit ( EXIT_FAILURE ) ; } if ( unlink ( filename ) < 0 ) { log_log ( LOG_DEBUG , " " , filename , strerror ( errno ) ) ; } if ( ( i = fcntl ( sock , F_GETFL , 0 ) ) < 0 ) { log_log ( LOG_ERR , " " , strerror ( errno ) ) ; if ( close ( sock ) ) log_log ( LOG_WARNING , " " , strerror ( errno ) ) ; exit ( EXIT_FAILURE ) ; } if ( fcntl ( sock , F_SETFL , i | O_NONBLOCK ) < 0 ) { log_log ( LOG_ERR , " " , strerror ( errno ) ) ; if ( close ( sock ) ) log_log ( LOG_WARNING , " " , strerror ( errno ) ) ; exit ( EXIT_FAILURE ) ; } mkdirname ( filename ) ; memset ( & addr , 0 , sizeof ( struct sockaddr_un ) ) ; addr . sun_family = AF_UNIX ; strncpy ( addr . sun_path , filename , sizeof ( addr . sun_path ) ) ; addr . sun_path [ sizeof ( addr . sun_path ) - 1 ] = ' \0 ' ; if ( bind ( sock , ( struct sockaddr * ) & addr , SUN_LEN ( & addr ) ) ) { log_log ( LOG_ERR , " " , filename , strerror ( errno ) ) ; if ( close ( sock ) ) log_log ( LOG_WARNING , " " , strerror ( errno ) ) ; exit ( EXIT_FAILURE ) ; } if ( fcntl ( sock , F_SETFD , FD_CLOEXEC ) < 0 ) { log_log ( LOG_ERR , " " , filename , strerror ( errno ) ) ; if ( close ( sock ) ) log_log ( LOG_WARNING , " " , strerror ( errno ) ) ; exit ( EXIT_FAILURE ) ; } if ( chmod ( filename , ( mode_t ) 0666 ) ) { log_log ( LOG_ERR , " " , filename , strerror ( errno ) ) ; if ( close ( sock ) ) log_log ( LOG_WARNING , " " , strerror ( errno ) ) ; exit ( EXIT_FAILURE ) ; } if ( listen ( sock , SOMAXCONN ) < 0 ) { log_log ( LOG_ERR , " " , strerror ( errno ) ) ; if ( close ( sock ) ) log_log ( LOG_WARNING , " " , strerror ( errno ) ) ; exit ( EXIT_FAILURE ) ; } return sock ; }
returns a socket ready to answer requests from the client,
exit()s on error
returns a socket ready to answer requests from the client exit () s on error
/* create a socket */ /* remove existing named socket */ /* do not block on accept() */ /* create the directory if needed */ /* create socket address structure */ /* bind to the named socket */ /* close the file descriptor on exec */ /* set permissions of socket so anybody can do requests */ /* Note: we use chmod() here instead of fchmod() because
       fchmod does not work on sockets
       http://www.opengroup.org/onlinepubs/009695399/functions/fchmod.html
       http://lkml.org/lkml/2005/5/16/11 */ /* start listening for connections */ /* we're done */