[libdispatch-dev] [PATCH] arc4random

Robert N. M. Watson robert at fledge.watson.org
Wed Nov 18 23:44:54 PST 2009


On 19 Nov 2009, at 02:41, Mark Heily wrote:

> Here's a patch I sent a couple days ago that may have been overlooked.

I did receive it, but given our conversation about it just being for testing purposes in a specific tool and not an issue of library code, I wonder if we shouldn't just make queue_finalizer.c use sranddev() and srand(). Does Solaris have an sranddev()? Jordan, would you object to us just moving to the more portable API entirely in the test tool as opposed to trying to emulate this BSD API?

Robert

> 
> Thanks,
> 
> - Mark
> 
> 
> Index: configure.ac
> ===================================================================
> --- configure.ac    (revision 166)
> +++ configure.ac    (working copy)
> @@ -191,7 +191,7 @@
> AC_CHECK_DECLS([FD_COPY], [], [], [[#include <sys/select.h>]])
> AC_CHECK_DECLS([SIGEMT], [], [], [[#include <signal.h>]])
> AC_CHECK_DECLS([VQ_UPDATE, VQ_VERYLOWDISK], [], [], [[#include <sys/mount.h>]])
> -AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf])
> +AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf arc4random])
> 
> AC_CHECK_DECLS([POSIX_SPAWN_START_SUSPENDED],
>   [have_posix_spawn_start_suspended=true],
> Index: testing/queue_finalizer.c
> ===================================================================
> --- testing/queue_finalizer.c    (revision 166)
> +++ testing/queue_finalizer.c    (working copy)
> @@ -32,6 +32,35 @@
> 
> void *ctxt_magic;
> 
> +/*
> +* Based on ISC BIND lib/isc/random.c
> +*/
> +#ifndef HAVE_ARC4RANDOM
> +static uint32_t
> +arc4random(void)
> +{
> +    unsigned int pid;
> +    static int arc4random_init;
> +
> +    /*
> +     * The low bits of pid generally change faster.
> +     * Xor them with the high bits of time which change slowly.
> +     */
> +    if (!arc4random_init) {
> +        pid = getpid();
> +        pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff);
> +        srand(time(NULL) ^ pid);
> +        arc4random_init = 1;
> +    }
> +
> +    /*
> +     * rand()'s lower bits are not random.
> +     * rand()'s upper bit is zero.
> +     */
> +     return ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000);
> +}
> +#endif /* ! HAVE_ARC4RANDOM */
> +
> static void
> finalizer(void *ctxt)
> {
> 




More information about the libdispatch-dev mailing list