openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/libgpg-error/1.36-r0/libgpg-error-1.36/src/spawn-posix.c
gpg_err_code_t _gpgrt_wait_processes(const char** pgmnames, pid_t* pids,
                                     size_t count, int hang, int* r_exitcodes)
{
    gpg_err_code_t ec = 0;
    size_t i, left;
    int* dummy = NULL;

    if (!r_exitcodes)
    {
        dummy = r_exitcodes = xtrymalloc(sizeof *r_exitcodes * count);
        if (!dummy)
            return _gpg_err_code_from_syserror();
    }

    for (i = 0, left = count; i < count; i++)
    {
        int status = -1;

        if (pids[i] == (pid_t)(-1))
            return GPG_ERR_INV_VALUE;

        /* See if there was a previously stored result for this pid.  */
        if (get_result(pids[i], &status))
            left -= 1;

        r_exitcodes[i] = status;
    }

    while (left > 0)
    {
        pid_t pid;
        int status;

        _gpgrt_pre_syscall();
        while ((pid = waitpid(-1, &status, hang ? 0 : WNOHANG)) ==
                   (pid_t)(-1) &&
               errno == EINTR)
            ;
        _gpgrt_post_syscall();

        if (pid == (pid_t)(-1))
        {
            ec = _gpg_err_code_from_syserror();
            _gpgrt_log_error(_("waiting for processes to terminate"
                               " failed: %s\n"),
                             _gpg_strerror(ec));
            break;
        }
        else if (!pid)
        {
            ec = GPG_ERR_TIMEOUT; /* Still running.  */
            break;
        }
        else
        {
            for (i = 0; i < count; i++)
                if (pid == pids[i])
                    break;

            if (i == count)
            {
                /* No match, store this result.  */
                ec = store_result(pid, status);
                if (ec)
                    break;
                continue;
            }

            /* Process PIDS[i] died.  */
            if (r_exitcodes[i] != (pid_t)-1)
            {
                _gpgrt_log_error("PID %d was reused", pid);
                ec = GPG_ERR_GENERAL;
                break;
            }

            left -= 1;
            r_exitcodes[i] = status;
        }
    }

    for (i = 0; i < count; i++)
    {
        if (r_exitcodes[i] == -1)
            continue;

        if (WIFEXITED(r_exitcodes[i]) && WEXITSTATUS(r_exitcodes[i]) == 127)
        {
            _gpgrt_log_error(_("error running '%s': probably not installed\n"),
                             pgmnames[i]);
            ec = GPG_ERR_CONFIGURATION;
        }
        else if (WIFEXITED(r_exitcodes[i]) && WEXITSTATUS(r_exitcodes[i]))
        {
            if (dummy)
                _gpgrt_log_error(_("error running '%s': exit status %d\n"),
                                 pgmnames[i], WEXITSTATUS(r_exitcodes[i]));
            else
                r_exitcodes[i] = WEXITSTATUS(r_exitcodes[i]);
            ec = GPG_ERR_GENERAL;
        }
        else if (!WIFEXITED(r_exitcodes[i]))
        {
            _gpgrt_log_error(_("error running '%s': terminated\n"),
                             pgmnames[i]);
            ec = GPG_ERR_GENERAL;
        }
    }

    xfree(dummy);
    return ec;
}
/* See gpgrt-int.h for a description.
 *
 * FIXME: What about using a poll like data structure for the pids and
 * their exit codes?  The whole thing is anyway problematic in a
 * threaded processs because waitpid has no association between PIDS
 * and threads.
 */
See gpgrt-int.h for a description.
FIXME: What about using a poll like data structure for the pids and
their exit codes.  The whole thing is anyway problematic in a
threaded processs because waitpid has no association between PIDS
and threads.
See gpgrt - int . h for a description . FIXME : What about using a poll like data structure for the pids and their exit codes . The whole thing is anyway problematic in a threaded processs because waitpid has no association between PIDS and threads .
gpg_err_code_t _gpgrt_wait_processes(const char** pgmnames, pid_t* pids,
                                     size_t count, int hang, int* r_exitcodes)
{
    gpg_err_code_t ec = 0;
    size_t i, left;
    int* dummy = NULL;
    if (!r_exitcodes)
    {
        dummy = r_exitcodes = xtrymalloc(sizeof *r_exitcodes * count);
        if (!dummy)
            return _gpg_err_code_from_syserror();
    }
    for (i = 0, left = count; i < count; i++)
    {
        int status = -1;
        if (pids[i] == (pid_t)(-1))
            return GPG_ERR_INV_VALUE;
        if (get_result(pids[i], &status))
            left -= 1;
        r_exitcodes[i] = status;
    }
    while (left > 0)
    {
        pid_t pid;
        int status;
        _gpgrt_pre_syscall();
        while ((pid = waitpid(-1, &status, hang ? 0 : WNOHANG)) ==
                   (pid_t)(-1) &&
               errno == EINTR)
            ;
        _gpgrt_post_syscall();
        if (pid == (pid_t)(-1))
        {
            ec = _gpg_err_code_from_syserror();
            _gpgrt_log_error(_("waiting for processes to terminate"
                               " failed: %s\n"),
                             _gpg_strerror(ec));
            break;
        }
        else if (!pid)
        {
            ec = GPG_ERR_TIMEOUT; 
            break;
        }
        else
        {
            for (i = 0; i < count; i++)
                if (pid == pids[i])
                    break;
            if (i == count)
            {
                ec = store_result(pid, status);
                if (ec)
                    break;
                continue;
            }
            if (r_exitcodes[i] != (pid_t)-1)
            {
                _gpgrt_log_error("PID %d was reused", pid);
                ec = GPG_ERR_GENERAL;
                break;
            }
            left -= 1;
            r_exitcodes[i] = status;
        }
    }
    for (i = 0; i < count; i++)
    {
        if (r_exitcodes[i] == -1)
            continue;
        if (WIFEXITED(r_exitcodes[i]) && WEXITSTATUS(r_exitcodes[i]) == 127)
        {
            _gpgrt_log_error(_("error running '%s': probably not installed\n"),
                             pgmnames[i]);
            ec = GPG_ERR_CONFIGURATION;
        }
        else if (WIFEXITED(r_exitcodes[i]) && WEXITSTATUS(r_exitcodes[i]))
        {
            if (dummy)
                _gpgrt_log_error(_("error running '%s': exit status %d\n"),
                                 pgmnames[i], WEXITSTATUS(r_exitcodes[i]));
            else
                r_exitcodes[i] = WEXITSTATUS(r_exitcodes[i]);
            ec = GPG_ERR_GENERAL;
        }
        else if (!WIFEXITED(r_exitcodes[i]))
        {
            _gpgrt_log_error(_("error running '%s': terminated\n"),
                             pgmnames[i]);
            ec = GPG_ERR_GENERAL;
        }
    }
    xfree(dummy);
    return ec;
}
gpg_err_code_t _gpgrt_wait_processes ( const char * * pgmnames , pid_t * pids , size_t count , int hang , int * r_exitcodes ) { gpg_err_code_t ec = 0 ; size_t i , left ; int * dummy = NULL ; if ( ! r_exitcodes ) { dummy = r_exitcodes = xtrymalloc ( sizeof * r_exitcodes * count ) ; if ( ! dummy ) return _gpg_err_code_from_syserror ( ) ; } for ( i = 0 , left = count ; i < count ; i ++ ) { int status = -1 ; if ( pids [ i ] == ( pid_t ) ( -1 ) ) return GPG_ERR_INV_VALUE ; if ( get_result ( pids [ i ] , & status ) ) left -= 1 ; r_exitcodes [ i ] = status ; } while ( left > 0 ) { pid_t pid ; int status ; _gpgrt_pre_syscall ( ) ; while ( ( pid = waitpid ( -1 , & status , hang ? 0 : WNOHANG ) ) == ( pid_t ) ( -1 ) && errno == EINTR ) ; _gpgrt_post_syscall ( ) ; if ( pid == ( pid_t ) ( -1 ) ) { ec = _gpg_err_code_from_syserror ( ) ; _gpgrt_log_error ( _ ( " " " \n " ) , _gpg_strerror ( ec ) ) ; break ; } else if ( ! pid ) { ec = GPG_ERR_TIMEOUT ; break ; } else { for ( i = 0 ; i < count ; i ++ ) if ( pid == pids [ i ] ) break ; if ( i == count ) { ec = store_result ( pid , status ) ; if ( ec ) break ; continue ; } if ( r_exitcodes [ i ] != ( pid_t ) - 1 ) { _gpgrt_log_error ( " " , pid ) ; ec = GPG_ERR_GENERAL ; break ; } left -= 1 ; r_exitcodes [ i ] = status ; } } for ( i = 0 ; i < count ; i ++ ) { if ( r_exitcodes [ i ] == -1 ) continue ; if ( WIFEXITED ( r_exitcodes [ i ] ) && WEXITSTATUS ( r_exitcodes [ i ] ) == 127 ) { _gpgrt_log_error ( _ ( " \n " ) , pgmnames [ i ] ) ; ec = GPG_ERR_CONFIGURATION ; } else if ( WIFEXITED ( r_exitcodes [ i ] ) && WEXITSTATUS ( r_exitcodes [ i ] ) ) { if ( dummy ) _gpgrt_log_error ( _ ( " \n " ) , pgmnames [ i ] , WEXITSTATUS ( r_exitcodes [ i ] ) ) ; else r_exitcodes [ i ] = WEXITSTATUS ( r_exitcodes [ i ] ) ; ec = GPG_ERR_GENERAL ; } else if ( ! WIFEXITED ( r_exitcodes [ i ] ) ) { _gpgrt_log_error ( _ ( " \n " ) , pgmnames [ i ] ) ; ec = GPG_ERR_GENERAL ; } } xfree ( dummy ) ; return ec ; }
See gpgrt-int.h for a description.
See gpgrt - int . h for a description .
/* See if there was a previously stored result for this pid.  */ /* Still running.  */ /* No match, store this result.  */ /* Process PIDS[i] died.  */