On Tue, Jul 22, 2014 at 5:39 PM, Antti Kantee <[email protected]> wrote:
> On 22/07/14 16:33, Justin Cormack wrote:
>>> Why is the code in a .h?  Not that it makes functional difference, but
>>> it's just ... odd.
>>
>> Yes I know. Couldn't think of another saner way to not duplicate code and
>> to not leave public symbols that should be internal. Suggestions?
>
> I can't currently see any public symbol apart from rumpuser_getrandom().
>   But if you're referring to the init bit, there are a number of symbols
> in librumpuser in the rumpuser__ (double underscore) namespace that are
> cross-module and internal.
>
> Could of course restrict their visibility at least in shared libs, but
> not sure why worry about conflicts in rumpuser__.

not sure why I hadnt noticed there was already a namespace...

improved version...
diff -urN src/lib/librumpuser/Makefile rsrc/lib/librumpuser/Makefile
--- src/lib/librumpuser/Makefile        2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/Makefile       2014-07-22 18:47:05.414540389 +0100
@@ -41,7 +41,7 @@
 .error Unsupported rumpuser threading type: ${RUMPUSER_THREADS}
 .endif
 
-SRCS+=         rumpuser_component.c
+SRCS+=         rumpuser_component.c rumpuser_random.c
 SRCS+=         rumpuser_file.c rumpuser_mem.c
 
 SRCS+=         rumpuser_errtrans.c rumpuser_sigtrans.c
diff -urN src/lib/librumpuser/rumpfiber.c rsrc/lib/librumpuser/rumpfiber.c
--- src/lib/librumpuser/rumpfiber.c     2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/rumpfiber.c    2014-07-22 21:13:11.202140129 +0100
@@ -416,20 +416,9 @@
                return 1;
        }
 
-#ifdef RUMPUSER_USE_DEVRANDOM
-       uint32_t rv;
-       int fd;
-
-       if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
-               srandom(time(NULL));
-       } else {
-               if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
-                       srandom(time(NULL));
-               else
-                       srandom(rv);
-               close(fd);
+       if (rumpuser__random_init() != 0) {
+               return 1;
        }
-#endif
 
         rumpuser__hyp = *hyp;
 
@@ -570,26 +559,6 @@
        return 0;
 }
 
-int
-rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
-{
-       size_t origlen = buflen;
-       uint32_t *p = buf;
-       uint32_t tmp;
-       int chunk;
-
-       do {
-               chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
-               tmp = RUMPUSER_RANDOM();
-               memcpy(p, &tmp, chunk);
-               p++;
-               buflen -= chunk;
-       } while (chunk);
-
-       *retp = origlen;
-       ET(0);
-}
-
 /* thread functions */
 
 TAILQ_HEAD(waithead, waiter);
diff -urN src/lib/librumpuser/rumpuser.c rsrc/lib/librumpuser/rumpuser.c
--- src/lib/librumpuser/rumpuser.c      2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/rumpuser.c     2014-07-22 21:12:37.350141675 +0100
@@ -63,20 +63,9 @@
                return 1;
        }
 
-#ifdef RUMPUSER_USE_DEVRANDOM
-       uint32_t rv;
-       int fd;
-
-       if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
-               srandom(time(NULL));
-       } else {
-               if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
-                       srandom(time(NULL));
-               else
-                       srandom(rv);
-               close(fd);
+       if (rumpuser__random_init() != 0) {
+               return 1;
        }
-#endif
 
        rumpuser__thrinit();
        rumpuser__hyp = *hyp;
@@ -275,23 +264,3 @@
                raise(sig);
        return 0;
 }
-
-int
-rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
-{
-       size_t origlen = buflen;
-       uint32_t *p = buf;
-       uint32_t tmp;
-       int chunk;
-
-       do {
-               chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
-               tmp = RUMPUSER_RANDOM();
-               memcpy(p, &tmp, chunk);
-               p++;
-               buflen -= chunk;
-       } while (chunk);
-
-       *retp = origlen;
-       ET(0);
-}
diff -urN src/lib/librumpuser/rumpuser_int.h rsrc/lib/librumpuser/rumpuser_int.h
--- src/lib/librumpuser/rumpuser_int.h  2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/rumpuser_int.h 2014-07-22 18:46:32.414541896 +0100
@@ -101,3 +101,5 @@
 #else
 #define ET(_v_) return (_v_) ? rumpuser__errtrans(_v_) : 0;
 #endif
+
+int rumpuser__random_init(void);
diff -urN src/lib/librumpuser/rumpuser_port.h 
rsrc/lib/librumpuser/rumpuser_port.h
--- src/lib/librumpuser/rumpuser_port.h 2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/rumpuser_port.h        2014-07-22 20:59:42.230177068 
+0100
@@ -218,11 +218,9 @@
 #define __STRING(x)    #x
 #endif
 
-#if defined(__linux__) || defined(__sun__) || defined (__CYGWIN__)
-#define RUMPUSER_RANDOM() random()
-#define RUMPUSER_USE_DEVRANDOM
-#else
-#define RUMPUSER_RANDOM() arc4random()
+#if (defined(__NetBSD__) && __NetBSD_Version__ > 600000000) || \
+  defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+#define PLATFORM_HAS_ARC4RANDOM_BUF
 #endif
 
 #ifndef __NetBSD_Prereq__
diff -urN src/lib/librumpuser/rumpuser_random.c 
rsrc/lib/librumpuser/rumpuser_random.c
--- src/lib/librumpuser/rumpuser_random.c       1970-01-01 01:00:00.000000000 
+0100
+++ rsrc/lib/librumpuser/rumpuser_random.c      2014-07-22 21:00:05.342176013 
+0100
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2014 Justin Cormack.  All Rights Reserved.
+ *
+ * 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 ``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 THE 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 "rumpuser_port.h"
+
+#if !defined(lint)
+__RCSID("$NetBSD$");
+#endif /* !lint */
+
+#include <sys/types.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <rump/rumpuser.h>
+
+#include "rumpuser_int.h"
+
+static const size_t random_maxread = 32;
+
+#ifdef PLATFORM_HAS_ARC4RANDOM_BUF
+int
+rumpuser__random_init(void)
+{
+
+       return 0;
+}
+#else
+static const char *random_device = "/dev/urandom";
+static int random_fd = -1;
+
+int
+rumpuser__random_init(void)
+{
+
+       random_fd = open(random_device, O_RDONLY);
+       if (random_fd < 0) {
+               fprintf(stderr, "random init open failed\n");
+               return 1;
+       }
+       return 0;
+}
+#endif
+
+int
+rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
+{
+#ifndef PLATFORM_HAS_ARC4RANDOM_BUF
+       ssize_t rv;
+
+       rv = read(random_fd, buf, buflen > random_maxread ? random_maxread : 
buflen);
+       if (rv < 0) {
+               ET(errno);
+       }
+       *retp = rv;
+#else
+       buflen = buflen > random_maxread ? random_maxread : buflen;
+       arc4random_buf(buf, buflen);
+       *retp = buflen;
+#endif
+
+       ET(0);
+}
------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
rumpkernel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rumpkernel-users

Reply via email to