openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/nbd/3.19-r0/nbd-3.19/nbd-server.c
void serveloop(GArray* servers, struct generic_conf* genconf)
{
    int i;
    int mmax, max;
    fd_set mset;
    fd_set rset;
    sigset_t blocking_mask;
    sigset_t original_mask;

    /*
     * Set up the master fd_set. The set of descriptors we need
     * to select() for never changes anyway and it buys us a *lot*
     * of time to only build this once. However, if we ever choose
     * to not fork() for clients anymore, we may have to revisit
     * this.
     */
    mmax = 0;
    FD_ZERO(&mset);
    for (i = 0; i < modernsocks->len; i++)
    {
        int sock = g_array_index(modernsocks, int, i);
        FD_SET(sock, &mset);
        mmax = sock > mmax ? sock : mmax;
    }

    /* Construct a signal mask which is used to make signal testing and
     * receiving an atomic operation to ensure no signal is received between
     * tests and blocking pselect(). */
    if (sigemptyset(&blocking_mask) == -1)
        err("failed to initialize blocking_mask: %m");

    if (sigaddset(&blocking_mask, SIGCHLD) == -1)
        err("failed to add SIGCHLD to blocking_mask: %m");

    if (sigaddset(&blocking_mask, SIGHUP) == -1)
        err("failed to add SIGHUP to blocking_mask: %m");

    if (sigaddset(&blocking_mask, SIGTERM) == -1)
        err("failed to add SIGTERM to blocking_mask: %m");

    if (sigprocmask(SIG_BLOCK, &blocking_mask, &original_mask) == -1)
        err("failed to block signals: %m");

    for (;;)
    {
        if (is_sigterm_caught)
        {
            is_sigterm_caught = 0;

            g_hash_table_foreach(children, killchild, NULL);
            unlink(pidfname);

            exit(EXIT_SUCCESS);
        }

        if (is_sigchld_caught)
        {
            int status;
            int* i;
            pid_t pid;

            is_sigchld_caught = 0;

            while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
            {
                if (WIFEXITED(status))
                {
                    msg(LOG_INFO, "Child exited with %d", WEXITSTATUS(status));
                }
                i = g_hash_table_lookup(children, &pid);
                if (!i)
                {
                    msg(LOG_INFO,
                        "SIGCHLD received for an unknown child with PID %ld",
                        (long)pid);
                }
                else
                {
                    DEBUG("Removing %d from the list of children", pid);
                    g_hash_table_remove(children, &pid);
                }
            }
        }

        /* SIGHUP causes the root server process to reconfigure
         * itself and add new export servers for each newly
         * found export configuration group, i.e. spawn new
         * server processes for each previously non-existent
         * export. This does not alter old runtime configuration
         * but just appends new exports. */
        if (is_sighup_caught)
        {
            int n;
            GError* gerror = NULL;

            msg(LOG_INFO, "reconfiguration request received");
            is_sighup_caught = 0; /* Reset to allow catching
                                   * it again. */

            n = append_new_servers(servers, &gerror);
            if (n == -1)
                msg(LOG_ERR, "failed to append new servers: %s",
                    gerror->message);

            for (i = servers->len - n; i < servers->len; ++i)
            {
                const SERVER server = g_array_index(servers, SERVER, i);

                msg(LOG_INFO, "reconfigured new server: %s", server.servename);
            }
        }

        memcpy(&rset, &mset, sizeof(fd_set));
        max = mmax;
        for (i = 0; i < childsocks->len; i++)
        {
            int sock = g_array_index(childsocks, int, i);
            FD_SET(sock, &rset);
            max = sock > max ? sock : max;
        }

        if (pselect(max + 1, &rset, NULL, NULL, NULL, &original_mask) > 0)
        {
            DEBUG("accept, ");
            for (i = 0; i < modernsocks->len; i++)
            {
                int sock = g_array_index(modernsocks, int, i);
                if (!FD_ISSET(sock, &rset))
                {
                    continue;
                }

                handle_modern_connection(servers, sock, genconf);
            }
            for (i = 0; i < childsocks->len; i++)
            {
                int sock = g_array_index(childsocks, int, i);

                if (FD_ISSET(sock, &rset))
                {
                    if (handle_childname(servers, sock) < 0)
                    {
                        close(sock);
                        g_array_remove_index(childsocks, i);
                    }
                }
            }
        }
    }
}
/**
 * Loop through the available servers, and serve them. Never returns.
 **/
Loop through the available servers, and serve them. Never returns.
Loop through the available servers and serve them . Never returns .
void serveloop(GArray* servers, struct generic_conf* genconf)
{
    int i;
    int mmax, max;
    fd_set mset;
    fd_set rset;
    sigset_t blocking_mask;
    sigset_t original_mask;
    mmax = 0;
    FD_ZERO(&mset);
    for (i = 0; i < modernsocks->len; i++)
    {
        int sock = g_array_index(modernsocks, int, i);
        FD_SET(sock, &mset);
        mmax = sock > mmax ? sock : mmax;
    }
    if (sigemptyset(&blocking_mask) == -1)
        err("failed to initialize blocking_mask: %m");
    if (sigaddset(&blocking_mask, SIGCHLD) == -1)
        err("failed to add SIGCHLD to blocking_mask: %m");
    if (sigaddset(&blocking_mask, SIGHUP) == -1)
        err("failed to add SIGHUP to blocking_mask: %m");
    if (sigaddset(&blocking_mask, SIGTERM) == -1)
        err("failed to add SIGTERM to blocking_mask: %m");
    if (sigprocmask(SIG_BLOCK, &blocking_mask, &original_mask) == -1)
        err("failed to block signals: %m");
    for (;;)
    {
        if (is_sigterm_caught)
        {
            is_sigterm_caught = 0;
            g_hash_table_foreach(children, killchild, NULL);
            unlink(pidfname);
            exit(EXIT_SUCCESS);
        }
        if (is_sigchld_caught)
        {
            int status;
            int* i;
            pid_t pid;
            is_sigchld_caught = 0;
            while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
            {
                if (WIFEXITED(status))
                {
                    msg(LOG_INFO, "Child exited with %d", WEXITSTATUS(status));
                }
                i = g_hash_table_lookup(children, &pid);
                if (!i)
                {
                    msg(LOG_INFO,
                        "SIGCHLD received for an unknown child with PID %ld",
                        (long)pid);
                }
                else
                {
                    DEBUG("Removing %d from the list of children", pid);
                    g_hash_table_remove(children, &pid);
                }
            }
        }
        if (is_sighup_caught)
        {
            int n;
            GError* gerror = NULL;
            msg(LOG_INFO, "reconfiguration request received");
            is_sighup_caught = 0; 
            n = append_new_servers(servers, &gerror);
            if (n == -1)
                msg(LOG_ERR, "failed to append new servers: %s",
                    gerror->message);
            for (i = servers->len - n; i < servers->len; ++i)
            {
                const SERVER server = g_array_index(servers, SERVER, i);
                msg(LOG_INFO, "reconfigured new server: %s", server.servename);
            }
        }
        memcpy(&rset, &mset, sizeof(fd_set));
        max = mmax;
        for (i = 0; i < childsocks->len; i++)
        {
            int sock = g_array_index(childsocks, int, i);
            FD_SET(sock, &rset);
            max = sock > max ? sock : max;
        }
        if (pselect(max + 1, &rset, NULL, NULL, NULL, &original_mask) > 0)
        {
            DEBUG("accept, ");
            for (i = 0; i < modernsocks->len; i++)
            {
                int sock = g_array_index(modernsocks, int, i);
                if (!FD_ISSET(sock, &rset))
                {
                    continue;
                }
                handle_modern_connection(servers, sock, genconf);
            }
            for (i = 0; i < childsocks->len; i++)
            {
                int sock = g_array_index(childsocks, int, i);
                if (FD_ISSET(sock, &rset))
                {
                    if (handle_childname(servers, sock) < 0)
                    {
                        close(sock);
                        g_array_remove_index(childsocks, i);
                    }
                }
            }
        }
    }
}
void serveloop ( GArray * servers , struct generic_conf * genconf ) { int i ; int mmax , max ; fd_set mset ; fd_set rset ; sigset_t blocking_mask ; sigset_t original_mask ; mmax = 0 ; FD_ZERO ( & mset ) ; for ( i = 0 ; i < modernsocks -> len ; i ++ ) { int sock = g_array_index ( modernsocks , int , i ) ; FD_SET ( sock , & mset ) ; mmax = sock > mmax ? sock : mmax ; } if ( sigemptyset ( & blocking_mask ) == -1 ) err ( " " ) ; if ( sigaddset ( & blocking_mask , SIGCHLD ) == -1 ) err ( " " ) ; if ( sigaddset ( & blocking_mask , SIGHUP ) == -1 ) err ( " " ) ; if ( sigaddset ( & blocking_mask , SIGTERM ) == -1 ) err ( " " ) ; if ( sigprocmask ( SIG_BLOCK , & blocking_mask , & original_mask ) == -1 ) err ( " " ) ; for ( ; ; ) { if ( is_sigterm_caught ) { is_sigterm_caught = 0 ; g_hash_table_foreach ( children , killchild , NULL ) ; unlink ( pidfname ) ; exit ( EXIT_SUCCESS ) ; } if ( is_sigchld_caught ) { int status ; int * i ; pid_t pid ; is_sigchld_caught = 0 ; while ( ( pid = waitpid ( -1 , & status , WNOHANG ) ) > 0 ) { if ( WIFEXITED ( status ) ) { msg ( LOG_INFO , " " , WEXITSTATUS ( status ) ) ; } i = g_hash_table_lookup ( children , & pid ) ; if ( ! i ) { msg ( LOG_INFO , " " , ( long ) pid ) ; } else { DEBUG ( " " , pid ) ; g_hash_table_remove ( children , & pid ) ; } } } if ( is_sighup_caught ) { int n ; GError * gerror = NULL ; msg ( LOG_INFO , " " ) ; is_sighup_caught = 0 ; n = append_new_servers ( servers , & gerror ) ; if ( n == -1 ) msg ( LOG_ERR , " " , gerror -> message ) ; for ( i = servers -> len - n ; i < servers -> len ; ++ i ) { const SERVER server = g_array_index ( servers , SERVER , i ) ; msg ( LOG_INFO , " " , server . servename ) ; } } memcpy ( & rset , & mset , sizeof ( fd_set ) ) ; max = mmax ; for ( i = 0 ; i < childsocks -> len ; i ++ ) { int sock = g_array_index ( childsocks , int , i ) ; FD_SET ( sock , & rset ) ; max = sock > max ? sock : max ; } if ( pselect ( max + 1 , & rset , NULL , NULL , NULL , & original_mask ) > 0 ) { DEBUG ( " " ) ; for ( i = 0 ; i < modernsocks -> len ; i ++ ) { int sock = g_array_index ( modernsocks , int , i ) ; if ( ! FD_ISSET ( sock , & rset ) ) { continue ; } handle_modern_connection ( servers , sock , genconf ) ; } for ( i = 0 ; i < childsocks -> len ; i ++ ) { int sock = g_array_index ( childsocks , int , i ) ; if ( FD_ISSET ( sock , & rset ) ) { if ( handle_childname ( servers , sock ) < 0 ) { close ( sock ) ; g_array_remove_index ( childsocks , i ) ; } } } } } }
Loop through the available servers, and serve them.
Loop through the available servers and serve them .
/*
     * Set up the master fd_set. The set of descriptors we need
     * to select() for never changes anyway and it buys us a *lot*
     * of time to only build this once. However, if we ever choose
     * to not fork() for clients anymore, we may have to revisit
     * this.
     */ /* Construct a signal mask which is used to make signal testing and
     * receiving an atomic operation to ensure no signal is received between
     * tests and blocking pselect(). */ /* SIGHUP causes the root server process to reconfigure
         * itself and add new export servers for each newly
         * found export configuration group, i.e. spawn new
         * server processes for each previously non-existent
         * export. This does not alter old runtime configuration
         * but just appends new exports. */ /* Reset to allow catching
                                   * it again. */