You are not logged in.
I'm working on a project in C++11 and I'm trying to call getrandom(), but when I build the project, I get the following error:
error: 'getrandom' was not declared in this scope
The top of the file with the call has
#include <linux/random.h>
I manually inspected the header file random.h at /usr/include/linux/random.h and it does not include a declaration for getrandom().
Is this a bug, or was the function ever declared in that header file? The documentation I linked to would lead me to believe that is the only header I need to include to use the syscall.
I have glibc 2.21-4 and linux-api-headers 4.0-1 installed on my Arch box.
Any suggestions are appreciated as I am stumped.
Thanks!
Last edited by darkfoon (2015-07-23 04:39:31)
Offline
getrandom is a system call, so you can access it trough the syscall function. The header <linux/random.h> only defines the constans GRND_RANDOM and GRND_NONBLOCK.
Here there is a baisc example:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <syscall.h>
#include <errno.h>
#include <linux/random.h>
int main()
{
unsigned char buff[4];
/*
* Here we invoke the getrandom system call. The fourth argument is the flag that will
* be passed to getrandom. Available falgs are 0, GRND_RANDOM, GRND_NONBLOCK
* and GRND_RANDOM | GRND_NONBLOCK.
*/
int n = syscall(SYS_getrandom, buff, 4, GRND_NONBLOCK);
printf("Random bytes read: %d\n", n);
if(n == -1)
{
// something went wrong!
printf("Reading random bytes failed: errno %d\n", errno);
}
else
{
// everything is fine, printing the result
printf("Hello: %x-%x-%x-%x\n", buff[0], buff[1], buff[2], buff[3]);
}
}
Last edited by mauritiusdadd (2015-07-22 06:00:21)
Offline
Thanks Mauritiusdadd!
That was exactly what I needed to do.
I didn't realize syscalls needed to be invoked in a special way like that.
Offline
You're welcome
Remember to mark the thread as [SOLVED]: https://bbs.archlinux.org/viewtopic.php … 3#p1016643
Offline