VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/rand-posix.cpp@ 1507

Last change on this file since 1507 was 891, checked in by vboxsync, 18 years ago

RTRand API (feel free to improve the fallback code).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1/* $Id: rand-posix.cpp 891 2007-02-14 09:35:20Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Random Numbers and Byte Streams, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <errno.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <sys/ioctl.h>
30#include <sys/fcntl.h>
31#include <fcntl.h>
32#ifdef _MSC_VER
33# include <io.h>
34# include <stdio.h>
35#else
36# include <unistd.h>
37# include <sys/time.h>
38#endif
39
40#include <iprt/rand.h>
41#include <iprt/err.h>
42#include <iprt/assert.h>
43#include "internal/rand.h"
44
45
46/*******************************************************************************
47* Global Variables *
48*******************************************************************************/
49/** File handle of /dev/random. */
50static int g_fhDevRandom = -1;
51
52
53void rtRandLazyInitNative(void)
54{
55 if (g_fhDevRandom != -1)
56 return;
57
58 int fh = open("/dev/urandom", O_RDONLY);
59 if (fh <= 0)
60 fh = open("/dev/random", O_RDONLY | O_NONBLOCK);
61 if (fh >= 0)
62 {
63 fcntl(fh, F_SETFD, FD_CLOEXEC);
64 g_fhDevRandom = fh;
65 }
66}
67
68
69int rtRandGenBytesNative(void *pv, size_t cb)
70{
71 int fh = g_fhDevRandom;
72 if (fh == -1)
73 return VERR_NOT_SUPPORTED;
74
75 ssize_t cbRead = read(fh, pv, cb);
76 if ((size_t)cbRead != cb)
77 {
78 /*
79 * Use the fallback for the remainder if /dev/urandom / /dev/random
80 * is out to lunch.
81 */
82 if (cbRead <= 0)
83 rtRandGenBytesFallback(pv, cb);
84 else
85 {
86 AssertRelease((size_t)cbRead < cb);
87 rtRandGenBytesFallback((uint8_t *)pv + cbRead, cb - cbRead);
88 }
89 }
90 return VINF_SUCCESS;
91}
92
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette