[master] 4f38c11 Roll our on VRND_Seed() and us that all the time, rather than bother with compat-crap for srandomdev()
Poul-Henning Kamp
phk at FreeBSD.org
Fri Dec 20 13:19:06 CET 2013
commit 4f38c1169b0edbe7c18d6fa207a2da5da8a7f752
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Fri Dec 20 12:18:32 2013 +0000
Roll our on VRND_Seed() and us that all the time, rather than
bother with compat-crap for srandomdev()
diff --git a/bin/varnishd/cache/cache_main.c b/bin/varnishd/cache/cache_main.c
index 2749338..8e116c9 100644
--- a/bin/varnishd/cache/cache_main.c
+++ b/bin/varnishd/cache/cache_main.c
@@ -36,13 +36,11 @@
#include "common/heritage.h"
#include "vcli_priv.h"
+#include "vrnd.h"
#include "waiter/waiter.h"
#include "hash/hash_slinger.h"
-#ifndef HAVE_SRANDOMDEV
-#include "compat/srandomdev.h"
-#endif
volatile struct params *cache_param;
@@ -236,7 +234,7 @@ child_main(void)
BAN_Compile();
- srandomdev();
+ VRND_Seed();
srand48(random());
CLI_AddFuncs(debug_cmds);
diff --git a/bin/varnishd/mgt/mgt_cli.c b/bin/varnishd/mgt/mgt_cli.c
index e19241f..f977adb 100644
--- a/bin/varnishd/mgt/mgt_cli.c
+++ b/bin/varnishd/mgt/mgt_cli.c
@@ -52,15 +52,12 @@
#include "vcli_serve.h"
#include "vev.h"
#include "vlu.h"
+#include "vrnd.h"
#include "vss.h"
#include "vtcp.h"
#include "mgt_cli.h"
-#ifndef HAVE_SRANDOMDEV
-#include "compat/srandomdev.h"
-#endif
-
static int cli_i = -1, cli_o = -1;
static struct VCLS *cls;
static const char *secret_file;
@@ -259,7 +256,7 @@ mgt_cli_challenge(struct cli *cli)
{
int i;
- srandomdev();
+ VRND_Seed();
for (i = 0; i + 2L < sizeof cli->challenge; i++)
cli->challenge[i] = (random() % 26) + 'a';
cli->challenge[i++] = '\n';
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index cdd7ca6..cd007b1 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -56,15 +56,12 @@
#include "vfil.h"
#include "vin.h"
#include "vpf.h"
+#include "vrnd.h"
#include "vsha256.h"
#include "vtim.h"
#include "compat/daemon.h"
-#ifndef HAVE_SRANDOMDEV
-#include "compat/srandomdev.h"
-#endif
-
struct heritage heritage;
unsigned d_flag = 0;
pid_t mgt_pid;
@@ -332,7 +329,7 @@ make_secret(const char *dirname)
dirname, strerror(errno));
exit(1);
}
- srandomdev();
+ VRND_Seed();
for (i = 0; i < sizeof buf; i++)
buf[i] = random() & 0xff;
assert(sizeof buf == write(fd, buf, sizeof buf));
@@ -413,7 +410,7 @@ main(int argc, char * const *argv)
for (o = getdtablesize(); o > STDERR_FILENO; o--)
(void)close(o);
- srandomdev();
+ VRND_Seed();
mgt_got_fd(STDERR_FILENO);
diff --git a/bin/varnishtest/vtc_main.c b/bin/varnishtest/vtc_main.c
index d6169e6..9e2d0c8 100644
--- a/bin/varnishtest/vtc_main.c
+++ b/bin/varnishtest/vtc_main.c
@@ -44,12 +44,9 @@
#include "vev.h"
#include "vqueue.h"
+#include "vrnd.h"
#include "vtim.h"
-#ifndef HAVE_SRANDOMDEV
-#include "compat/srandomdev.h"
-#endif
-
#define MAX_FILESIZE (1024 * 1024)
@@ -270,7 +267,7 @@ start_test(void)
assert(jp->buf != MAP_FAILED);
memset(jp->buf, 0, jp->bufsiz);
- srandomdev();
+ VRND_Seed();
bprintf(tmpdir, "%s/vtc.%d.%08x", tmppath, (int)getpid(),
(unsigned)random());
AZ(mkdir(tmpdir, 0711));
diff --git a/configure.ac b/configure.ac
index cd346b3..ea2039a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -257,7 +257,6 @@ AC_SUBST(LIBUMEM)
# These functions are provided by libcompat on platforms where they
# are not available
AC_CHECK_FUNCS([setproctitle])
-AC_CHECK_FUNCS([srandomdev])
AC_SEARCH_LIBS(backtrace, [execinfo],[AC_DEFINE([HAVE_BACKTRACE],[1],[Define to 1 if the backtrace function exists])])
# white lie - we don't actually test it
AC_MSG_CHECKING([whether daemon() works])
diff --git a/include/Makefile.am b/include/Makefile.am
index 5b75aca..17633ce 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -40,7 +40,6 @@ nobase_noinst_HEADERS = \
binary_heap.h \
compat/daemon.h \
compat/execinfo.h \
- compat/srandomdev.h \
flopen.h \
libvcc.h \
persistent.h \
@@ -57,6 +56,7 @@ nobase_noinst_HEADERS = \
vmb.h \
vnum.h \
vpf.h \
+ vrnd.h \
vsub.h \
vss.h \
vtcp.h \
diff --git a/include/compat/srandomdev.h b/include/compat/srandomdev.h
deleted file mode 100644
index e1b8c78..0000000
--- a/include/compat/srandomdev.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2009 Varnish Software AS
- * All rights reserved.
- *
- * Author: Dag-Erling Smørgrav <des at des.no>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#ifndef COMPAT_SRANDOMDEV_H_INCLUDED
-#define COMPAT_SRANDOMDEV_H_INCLUDED
-
-#ifndef HAVE_SRANDOMDEV
-void srandomdev(void);
-#endif
-
-#endif
diff --git a/include/vrnd.h b/include/vrnd.h
new file mode 100644
index 0000000..ac46c0d
--- /dev/null
+++ b/include/vrnd.h
@@ -0,0 +1,32 @@
+/*-
+ * Copyright (c) 2013 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Random functions
+ */
+
+void VRND_Seed(void); /* Seed random(3) properly */
+
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index c471c9c..4e09fb9 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -27,6 +27,7 @@ libvarnish_la_SOURCES = \
vmb.c \
vpf.c \
vre.c \
+ vrnd.c \
vsa.c \
vsb.c \
vsha256.c \
diff --git a/lib/libvarnish/Makefile.phk b/lib/libvarnish/Makefile.phk
index 53f4cfe..677e687 100644
--- a/lib/libvarnish/Makefile.phk
+++ b/lib/libvarnish/Makefile.phk
@@ -16,6 +16,7 @@ LIB_SRC += vmb.c
LIB_SRC += vnum.c
LIB_SRC += vpf.c
LIB_SRC += vre.c
+LIB_SRC += vrnd.c
LIB_SRC += vsa.c
LIB_SRC += vsb.c
LIB_SRC += vsha256.c
diff --git a/lib/libvarnish/vrnd.c b/lib/libvarnish/vrnd.c
new file mode 100644
index 0000000..655cf7e
--- /dev/null
+++ b/lib/libvarnish/vrnd.c
@@ -0,0 +1,78 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Dag-Erling Smørgrav <des at des.no>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "vas.h"
+#include "vrnd.h"
+#include "vtim.h"
+#include "vsha256.h"
+
+void
+VRND_Seed(void)
+{
+ unsigned long seed;
+ struct SHA256Context ctx;
+ double d;
+ pid_t p;
+ unsigned char b[SHA256_LEN];
+ int fd;
+ ssize_t sz;
+
+ fd = open("/dev/urandom", O_RDONLY);
+ if (fd < 0)
+ fd = open("/dev/random", O_RDONLY);
+ if (fd >= 0) {
+ sz = read(fd, &seed, sizeof seed);
+ AZ(close(fd));
+ if (sz == sizeof seed) {
+ srandom(seed);
+ return;
+ }
+ }
+
+ SHA256_Init(&ctx);
+ d = VTIM_mono();
+ SHA256_Update(&ctx, &d, sizeof d);
+ d = VTIM_real();
+ SHA256_Update(&ctx, &d, sizeof d);
+ p = getpid();
+ SHA256_Update(&ctx, &p, sizeof p);
+ p = getppid();
+ SHA256_Update(&ctx, &p, sizeof p);
+ SHA256_Final(b, &ctx);
+ memcpy(&seed, b, sizeof seed);
+ srandom(seed);
+}
diff --git a/lib/libvarnishcompat/Makefile.am b/lib/libvarnishcompat/Makefile.am
index 698531f..543fbb2 100644
--- a/lib/libvarnishcompat/Makefile.am
+++ b/lib/libvarnishcompat/Makefile.am
@@ -9,5 +9,4 @@ libvarnishcompat_la_LDFLAGS = $(AM_LDFLAGS) -avoid-version
libvarnishcompat_la_SOURCES = \
daemon.c \
- execinfo.c \
- srandomdev.c
+ execinfo.c
diff --git a/lib/libvarnishcompat/srandomdev.c b/lib/libvarnishcompat/srandomdev.c
deleted file mode 100644
index 83b22c4..0000000
--- a/lib/libvarnishcompat/srandomdev.c
+++ /dev/null
@@ -1,73 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Dag-Erling Smørgrav <des at des.no>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include "config.h"
-
-#ifndef HAVE_SRANDOMDEV
-
-#include <sys/time.h>
-
-#include <fcntl.h>
-#include <stdlib.h>
-#include <time.h>
-#include <unistd.h>
-
-#include "compat/srandomdev.h"
-
-static int
-trydev(const char *fn, unsigned long *seed)
-{
- int fd;
- ssize_t sz;
-
- fd = open(fn, O_RDONLY);
- if (fd < 0)
- return (-1);
- sz = read(fd, seed, sizeof *seed);
- (void)close(fd);
- if (sz != sizeof *seed)
- return (-1);
- return (0);
-}
-
-void
-srandomdev(void)
-{
- struct timeval tv;
- unsigned long seed;
-
- if (trydev("/dev/urandom", &seed)) {
- if (trydev("/dev/random", &seed)) {
- gettimeofday(&tv, NULL);
- seed = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
- }
- }
- srandom(seed);
-}
-#endif
More information about the varnish-commit
mailing list