From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 1/4] lib: add function to seed random()
Date: Wed, 9 Jan 2013 19:36:53 +0000 [thread overview]
Message-ID: <1357760216-4068-1-git-send-email-kerolasa@iki.fi> (raw)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
include/randutils.h | 1 +
lib/randutils.c | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/include/randutils.h b/include/randutils.h
index dec5e35..9d4d41c 100644
--- a/include/randutils.h
+++ b/include/randutils.h
@@ -8,5 +8,6 @@
extern int random_get_fd(void);
extern void random_get_bytes(void *buf, size_t nbytes);
+extern void seed_random(void);
#endif
diff --git a/lib/randutils.c b/lib/randutils.c
index 85cb1a9..44d996e 100644
--- a/lib/randutils.c
+++ b/lib/randutils.c
@@ -104,6 +104,24 @@ void random_get_bytes(void *buf, size_t nbytes)
return;
}
+/* Seed random(3). */
+void seed_random(void)
+{
+ struct timeval tv;
+ unsigned int seed;
+ FILE *fd;
+
+ if ((fd = fopen("/dev/urandom", "r"))) {
+ fread(&seed, sizeof seed, 1, fd);
+ fclose(fd);
+ } else {
+ gettimeofday(&tv, NULL);
+ /* NOTE: intentional use of uninitialized variable */
+ seed ^= (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
+ }
+ srandom(seed);
+}
+
#ifdef TEST_PROGRAM
int main(int argc __attribute__ ((__unused__)),
char *argv[] __attribute__ ((__unused__)))
--
1.8.1
next reply other threads:[~2013-01-09 19:37 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-09 19:36 Sami Kerola [this message]
2013-01-09 19:36 ` [PATCH 2/4] ipcmk: remove unnecessary create_key() function Sami Kerola
2013-01-10 10:19 ` Karel Zak
2013-01-24 15:32 ` Karel Zak
2013-01-09 19:36 ` [PATCH 3/4] ipcmk: allow user to create more ipc resources at a time Sami Kerola
2013-01-10 9:25 ` Karel Zak
2013-01-09 19:36 ` [PATCH 4/4] include: add missing values to sysfs_cxt initializer definition Sami Kerola
2013-01-24 15:35 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1357760216-4068-1-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox