Skip to content

Commit

Permalink
caml_unix_random_seed: clarify fallback logic
Browse files Browse the repository at this point in the history
Hopefully the new structure should make it easy to add new elements to
the random seed in the non-/dev/urandom case, in particular a domain
identifier for Multicore OCaml.
  • Loading branch information
gasche committed Jun 24, 2021
1 parent 26d3ca4 commit 644584b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Working version
64-bit floats in Cmm.memory_chunk.
(Greta Yorsh, review by Xavier Leroy)

- #????: refactor caml_sys_random_seed to ease future Multicore changes
(Gabriel Scherer, review by ???)

- #10434: Pun labelled arguments with type constraint in function applications.
(Greta Yorsh, review by Nicolas Chataing and Nicolás Ojeda Bär)

Expand Down
11 changes: 6 additions & 5 deletions runtime/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ int caml_unix_random_seed(intnat data[16])
{
int fd;
int n = 0;

/* Try /dev/urandom first */
fd = open("/dev/urandom", O_RDONLY, 0);
if (fd != -1) {
Expand All @@ -581,14 +582,14 @@ int caml_unix_random_seed(intnat data[16])
#ifdef HAS_GETTIMEOFDAY
struct timeval tv;
gettimeofday(&tv, NULL);
data[n++] = tv.tv_usec;
data[n++] = tv.tv_sec;
if (n < 16) data[n++] = tv.tv_usec;
if (n < 16) data[n++] = tv.tv_sec;
#else
data[n++] = time(NULL);
if (n < 16) data[n++] = time(NULL);
#endif
#ifdef HAS_UNISTD
data[n++] = getpid();
data[n++] = getppid();
if (n < 16) data[n++] = getpid();
if (n < 16) data[n++] = getppid();
#endif
}
return n;
Expand Down

0 comments on commit 644584b

Please sign in to comment.