Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libjava / gnu / java / security / jce / prng / natVMSecureRandomPosix.cc
bloba44f3f4573bb2238a3d10628d73efac6f96b531f
1 // natVMSecureRandomPosix.cc - Native part of VMSecureRandom class for POSIX.
3 /* Copyright (C) 2009 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
20 #include <gcj/cni.h>
21 #include <java/lang/InternalError.h>
22 #include <gnu/java/security/jce/prng/VMSecureRandom.h>
24 jint
25 gnu::java::security::jce::prng::VMSecureRandom::natGenerateSeed(jbyteArray byte_array, jint offset, jint length)
27 int a, fd;
28 jbyte *bytes = elements (byte_array);
29 ssize_t count;
31 for (a = 0; a < offset; ++a)
32 bytes++;
33 fd = open ("/dev/random", O_RDONLY);
35 if (fd == -1)
37 jstring oserr = JvNewStringLatin1 (strerror (errno));
38 throw new ::java::lang::InternalError
39 (JvNewStringLatin1 ("Error opening /dev/random: ")->concat(oserr));
42 count = read (fd, bytes, length);
43 close (fd);
45 if (count == -1)
47 jstring oserr = JvNewStringLatin1 (strerror (errno));
48 throw new ::java::lang::InternalError
49 (JvNewStringLatin1 ("Error reading /dev/random: ")->concat(oserr));
52 return count;