Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getrandom syscall in C not found

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..

like image 583
anothertest Avatar asked Jun 12 '15 09:06

anothertest


1 Answers

So, it seems that getrandom is not a function, just a syscall.

Hence this is needed:

/* Note that this define is required for syscalls to work. */
#define _GNU_SOURCE

#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>

int main(int arg, char *argv[])
{
        void *buf = NULL;
        size_t l = 5;
        unsigned int o = 1;
        int r = syscall(SYS_getrandom, buf, l, o);
        return 0;
}
like image 122
Florian Margaine Avatar answered Sep 20 '22 22:09

Florian Margaine