Comparison

util-src/crand.c @ 7944:36a9a4af1873

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 02 Mar 2017 23:03:02 +0100
parent 7934:485b9e66fedf
child 7969:1c6a07606309
comparison
equal deleted inserted replaced
7923:81f3068fc30c 7944:36a9a4af1873
17 * 17 *
18 * Caution! This has not been extensively tested. 18 * Caution! This has not been extensively tested.
19 * 19 *
20 */ 20 */
21 21
22 #define _DEFAULT_SOURCE
23
22 #include "lualib.h" 24 #include "lualib.h"
23 #include "lauxlib.h" 25 #include "lauxlib.h"
24 26
25 #include <string.h> 27 #include <string.h>
26 #include <errno.h> 28 #include <errno.h>
27 29
28 #if defined(WITH_GETRANDOM) 30 #if defined(WITH_GETRANDOM)
31
32 #if ! __GLIBC_PREREQ(2,25)
29 #include <unistd.h> 33 #include <unistd.h>
30 #include <sys/syscall.h> 34 #include <sys/syscall.h>
31 #include <linux/random.h>
32 35
33 #ifndef SYS_getrandom 36 #ifndef SYS_getrandom
34 #error getrandom() requires Linux 3.17 or later 37 #error getrandom() requires Linux 3.17 or later
35 #endif 38 #endif
36 39
37 /* 40 /* This wasn't present before glibc 2.25 */
38 * This acts like a read from /dev/urandom with the exception that it 41 int getrandom(void *buf, size_t buflen, unsigned int flags) {
39 * *does* block if the entropy pool is not yet initialized. 42 return syscall(SYS_getrandom, buf, buflen, flags);
40 */
41 int getrandom(void *buf, size_t len, int flags) {
42 return syscall(SYS_getrandom, buf, len, flags);
43 } 43 }
44 #else
45 #include <sys/random.h>
46 #endif
44 47
45 #elif defined(WITH_ARC4RANDOM) 48 #elif defined(WITH_ARC4RANDOM)
46 #include <stdlib.h> 49 #include <stdlib.h>
47 #elif defined(WITH_OPENSSL) 50 #elif defined(WITH_OPENSSL)
48 #include <openssl/rand.h> 51 #include <openssl/rand.h>
54 int ret = 0; 57 int ret = 0;
55 size_t len = (size_t)luaL_checkinteger(L, 1); 58 size_t len = (size_t)luaL_checkinteger(L, 1);
56 void *buf = lua_newuserdata(L, len); 59 void *buf = lua_newuserdata(L, len);
57 60
58 #if defined(WITH_GETRANDOM) 61 #if defined(WITH_GETRANDOM)
62 /*
63 * This acts like a read from /dev/urandom with the exception that it
64 * *does* block if the entropy pool is not yet initialized.
65 */
59 ret = getrandom(buf, len, 0); 66 ret = getrandom(buf, len, 0);
60 67
61 if(ret < 0) { 68 if(ret < 0) {
62 lua_pushstring(L, strerror(errno)); 69 lua_pushstring(L, strerror(errno));
63 return lua_error(L); 70 return lua_error(L);