From: des Date: Tue, 8 Aug 2006 12:15:22 +0000 (+0000) Subject: Add a simple srandomdev() implementation inspired by the one in FreeBSD. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1adadfc2553bd46d7e8fa6ac3f4df67b5b615a47;p=varnish Add a simple srandomdev() implementation inspired by the one in FreeBSD. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@766 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/configure.ac b/varnish-cache/configure.ac index 586965ca..a6364955 100644 --- a/varnish-cache/configure.ac +++ b/varnish-cache/configure.ac @@ -61,6 +61,7 @@ AC_FUNC_VPRINTF AC_CHECK_FUNCS([strerror]) AC_FUNC_STRERROR_R AC_CHECK_FUNCS([socket]) +AC_CHECK_FUNCS([srandomdev]) AC_CHECK_FUNCS([strlcat]) AC_CHECK_FUNCS([strlcpy]) diff --git a/varnish-cache/include/Makefile.am b/varnish-cache/include/Makefile.am index 36bd3fe9..7c4f909e 100644 --- a/varnish-cache/include/Makefile.am +++ b/varnish-cache/include/Makefile.am @@ -6,6 +6,7 @@ noinst_HEADERS = \ cli_common.h \ cli_priv.h \ compat/asprintf.h \ + compat/srandomdev.h \ compat/strlcat.h \ compat/strlcpy.h \ compat/vasprintf.h \ diff --git a/varnish-cache/include/compat/srandomdev.h b/varnish-cache/include/compat/srandomdev.h new file mode 100644 index 00000000..ef2a637f --- /dev/null +++ b/varnish-cache/include/compat/srandomdev.h @@ -0,0 +1,12 @@ +/* + * $Id$ + */ + +#ifndef COMPAT_SRANDOMDEV_H_INCLUDED +#define COMPAT_SRANDOMDEV_H_INCLUDED + +#ifndef HAVE_SRANDOMDEV +void srandomdev(void); +#endif + +#endif diff --git a/varnish-cache/lib/libcompat/Makefile.am b/varnish-cache/lib/libcompat/Makefile.am index 01f59815..dcddb0b5 100644 --- a/varnish-cache/lib/libcompat/Makefile.am +++ b/varnish-cache/lib/libcompat/Makefile.am @@ -7,6 +7,7 @@ lib_LIBRARIES = libcompat.a libcompat_a_SOURCES = \ asprintf.c \ vasprintf.c \ + srandomdev.c \ strlcat.c \ strlcpy.c diff --git a/varnish-cache/lib/libcompat/srandomdev.c b/varnish-cache/lib/libcompat/srandomdev.c new file mode 100644 index 00000000..d3ce3058 --- /dev/null +++ b/varnish-cache/lib/libcompat/srandomdev.c @@ -0,0 +1,33 @@ +/* + * $Id$ + */ + +#ifndef HAVE_SRANDOMDEV + +#include + +#include +#include +#include +#include + +#include "compat/srandomdev.h" + +void +srandomdev(void) +{ + struct timeval tv; + unsigned int seed; + int fd; + + if ((fd = open("/dev/random", O_RDONLY)) >= 0) { + read(fd, &seed, sizeof seed); + close(fd); + } else { + gettimeofday(&tv, NULL); + /* NOTE: intentional use of uninitialized variable */ + seed ^= (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec; + } + srandom(seed); +} +#endif