Linux ramdom syscall not found

Leave a Comment

According linux man page, I wrote codes as below:

#include <linux/random.h>
......
#ifdef __linux__
    unsigned char buf[2 * LOOP_MAX * sizeof(int)];
    int *p = (int *)buf;
    result = getrandom(buf, LOOP_MAX * sizeof(int), 0);
#else
    srand((unsigned) time(0));
#endif

When compiling with gcc, I got this error message:

......c: In functionmain’:
......c:43:5: warning: implicit declaration of functiongetrandom’ [-Wimplicit-function-declaration]
     getrandom( buf, LOOP_MAX * sizeof(int), 0);
     ^
/tmp/ccng3Ckd.o: In function `main':
/......c:43: undefined reference to `getrandom'

I’m using the newest kernel and I’m sure that the syscall exists. I haven’t find the reason yet. But a workaroud:

#ifdef __linux__
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>
#else
......
#endif
......
#ifdef __linux__
    unsigned char buf[2 * LOOP_MAX * sizeof(int)];
    int *p = (int *)buf;
    result = syscall(SYS_getrandom, buf, LOOP_MAX * sizeof(int), 0);
#else
......

References:

http://man7.org/linux/man-pages/man2/getrandom.2.html

https://stackoverflow.com/questions/30800331/getrandom-syscall-in-c-not-found

0 comments:

Post a Comment