The problem was resolved by upgrading the C library.
I would like to use the syscall getrandom (http://man7.org/linux/man-pages/man2/getrandom.2.html)
gcc-5 -std=c11 test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <linux/random.h>
#include <sys/syscall.h>
int main(void)
{
void *buf = NULL;
size_t l = 5;
unsigned int o = 1;
int r = syscall(SYS_getrandom, buf, l, o);
return 0;
}
or
int main(void)
{
void *buf = NULL;
size_t l = 5;
unsigned int o = 1;
int r = getrandom(buf, l, o);
return 0;
}
Anyway when I try to compile it with gcc-5:
test.c: In function ‘main’:
test.c:14:17: warning: implicit declaration of function ‘getrandom’ [-Wimplicit-function-declaration]
int r = getrandom(buf, l, o);
^
/tmp/ccqFdJAJ.o: In function `main':
test.c:(.text+0x36): undefined reference to `getrandom'
collect2: error: ld returned 1 exit status
I am using Ubuntu 14.04, what can I do to use getrandom? As it is a "new" syscall, how can I use it?
edit:
uname -r
-> 4.0.3-040003-generic #201505131441 SMP Wed May 13 13:43:16 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
when I replace r by int r = syscall(SYS_getrandom, buf, l, o);
or r = getrandom(buf, l, o) it is the same..
I think with your program you will get a segmentation fault. The following code should work:
getrandom
andgetentropy
were added to glibc in version 2.25. As of July 2017, most Linux distributions have not yet updated to this version (e.g. Debian's most recent release, which just came out, has 2.24) but they should soon.Here is how to use the glibc wrappers if available and fall back to the raw system call if not:
(As pointed out in other answers, it is also necessary to ensure you have kernel 3.17 or newer. Both the above versions of
my_getentropy
will fail and seterrno
toENOSYS
if run on an older kernel.)Here, I compiled some code shown before, and corrected the bugs, included is my output from gcc -v for comparison.
So, it seems that
getrandom
is not a function, just a syscall.Hence this is needed:
The
getrandom()
syscall was introduced in the linux kernel 3.17. Ubuntu 14.04 gets shipped with kernel 3.13, so you have to update to a more recent kernel to get the syscall.To get .deb packages of the linux kernel for Ubuntu, have a look at kernel.ubuntu.com. This problem was also discussed at askubuntu.com.