Skip to content
This repository has been archived by the owner on May 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #126 from vbmithr/CA-85761
Browse files Browse the repository at this point in the history
Use unbuffered I/O to read from /dev/(u)random
  • Loading branch information
Jon Ludlam committed Mar 19, 2013
2 parents 7a0e40e + 3e56de9 commit 2b61fac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion close-and-exec/Makefile
Expand Up @@ -16,7 +16,7 @@ bins: $(PROGRAMS)
libs: $(LIBS)

closeandexec: closeandexec.cmxa closeandexec_main.cmx all
$(OCAMLOPT) $(OCAMLOPTFLAGS) -thread -I ../stdext -I ../uuid uuid.cmxa unix.cmxa threads.cmxa stdext.cmxa closeandexec.cmxa closeandexec_main.cmx -o $@
$(OCAMLOPT) $(OCAMLOPTFLAGS) -thread -I ../stdext -I ../uuid unix.cmxa threads.cmxa uuid.cmxa stdext.cmxa closeandexec.cmxa closeandexec_main.cmx -o $@

closeandexec.cmxa: $(foreach obj,$(OBJS),$(obj).cmx)
$(OCAMLOPT) $(OCAMLOPTFLAGS) -a -o $@ $(foreach obj,$(OBJS),$(obj).cmx)
Expand Down
4 changes: 2 additions & 2 deletions forking_executioner/Makefile
Expand Up @@ -17,10 +17,10 @@ bins: $(PROGRAMS)
libs: $(LIBS)

test_forker: test_forker.cmx
$(OCAMLOPT) $(OCAMLOPTFLAGS) -I ../rpc-light -I ../uuid -I ../stdext uuid.cmxa rpc.cmx jsonrpc.cmx -I ../log unix.cmxa stdext.cmxa test_forker.cmx -o $@
$(OCAMLOPT) $(OCAMLOPTFLAGS) -I ../rpc-light -I ../uuid -I ../stdext unix.cmxa uuid.cmxa rpc.cmx jsonrpc.cmx -I ../log stdext.cmxa test_forker.cmx -o $@

fe: fe_debug.cmx child.cmx fe_main.cmx
$(OCAMLOPT) $(OCAMLOPTFLAGS) -I ../rpc-light -I ../stdext -I ../uuid -I ../log log.cmxa uuid.cmxa unix.cmxa rpc.cmx jsonrpc.cmx stdext.cmxa fe_debug.cmx child.cmx fe_main.cmx -o $@
$(OCAMLOPT) $(OCAMLOPTFLAGS) -I ../rpc-light -I ../stdext -I ../uuid -I ../log log.cmxa unix.cmxa uuid.cmxa rpc.cmx jsonrpc.cmx stdext.cmxa fe_debug.cmx child.cmx fe_main.cmx -o $@

%.cmo: %.ml
$(OCAMLC) -c -I ../log -I ../uuid -I ../stdext -thread -o $@ $<
Expand Down
22 changes: 14 additions & 8 deletions uuid/uuid.ml
Expand Up @@ -57,14 +57,20 @@ let rnd_array n =
Array.of_list (rnd_list n [])

let read_array dev n =
let ic = open_in_bin dev in
try
let result = Array.init n (fun _ -> input_byte ic) in
close_in ic;
result
with e ->
close_in ic;
raise e
let fd = Unix.openfile dev [Unix.O_RDONLY] 0o640 in
let finally body_f clean_f =
try
let ret = body_f () in clean_f (); ret
with e -> clean_f (); raise e in
finally
(fun () ->
let buf = String.create n in
let read = Unix.read fd buf 0 n in
if read <> n then raise End_of_file
else
Array.init n (fun i -> Char.code buf.[i])
)
(fun () -> Unix.close fd)

let uuid_of_int_array uuid =
Printf.sprintf "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
Expand Down

0 comments on commit 2b61fac

Please sign in to comment.