本文介绍了未找到C中的getrandom系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用系统调用getrandom()

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);
返回0;
}

  int main(void)
{
void * buf = NULL;
size_t l = 5;
unsigned int o = 1;
int r = getrandom(buf,l,o);
返回0;
}

无论如何,当我尝试使用gcc-5进行编译时:

  test.c:在函数'main'中:
test.c:14:17:warning:隐式声明函数'getrandom '[-Wimplicit-function-declaration]
int r = getrandom(buf,l,o);
^
/tmp/ccqFdJAJ.o:在函数`main'中:
test.c :( .text + 0x36):未定义对`getrandom'的引用
collect2:错误:ld返回1退出状态

我正在使用Ubuntu 14.04,我能做些什么来使用getrandom?
因为它是一个新系统调用,我该如何使用它?

编辑:

  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

当我用 int r = syscall(SYS_getrandom,buf,l,o)替换r;
或r = getrandom(buf,l, o)它是一样的。

解决方案

所以,似乎 getrandom 不是函数,只是一个系统调用。



因此需要:

  / *注意,这个定义是syscalls工作所必需的。 * / 
#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);
返回0;
}


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

解决方案

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;
}

这篇关于未找到C中的getrandom系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 18:49