You are not logged in.

#1 2015-07-21 06:51:40

darkfoon
Member
Registered: 2007-08-29
Posts: 42

[SOLVED] getrandom() is not in the latest glibc or linux-api-headers

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

#2 2015-07-21 08:04:19

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [SOLVED] getrandom() is not in the latest glibc or linux-api-headers

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)


About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

#3 2015-07-22 05:52:55

darkfoon
Member
Registered: 2007-08-29
Posts: 42

Re: [SOLVED] getrandom() is not in the latest glibc or linux-api-headers

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

#4 2015-07-22 06:00:10

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [SOLVED] getrandom() is not in the latest glibc or linux-api-headers

You're welcome smile
Remember to mark the thread as [SOLVED]: https://bbs.archlinux.org/viewtopic.php … 3#p1016643


About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

Board footer

Powered by FluxBB