Jordan K. Hubbard wrote:
See below for the actual code. Any objections to adding something like this to libdispatch?
I'm not sure I really see why... This is something that should logically be handled instead by a HAVE_ARC4RANDOM check, with the alternate sides of the #ifdef being the appropriate calls to srand() and rand(), just as the ISC has done. No need to add shims just for the use of a single library, in other words. Am I missing something?
- Jordan
Thanks for the advice to keep things simple. Here is a patch to provide arc4random(3) that is based on the ISC code. There is one other consumer of arc4random() in the tree -- testing/fd_stress.c -- but this program is not built by default in Makefile.am, so I am not going to worry about it right now. Regards, - 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) {