首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到C中的getrandom syscall

找不到C中的getrandom syscall
EN

Stack Overflow用户
提问于 2015-06-12 09:59:47
回答 5查看 20.1K关注 0票数 14

通过升级C库解决了这个问题。

我想使用syscall getrandom (http://man7.org/linux/man-pages/man2/getrandom.2.html)

gcc-5 -std=c11测试.c

代码语言:javascript
复制
#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;
}

代码语言:javascript
复制
 int main(void)
    {
            void *buf = NULL;
            size_t l = 5;
            unsigned int o = 1;
            int r = getrandom(buf, l, o);
            return 0;
    }

总之,当我试图用gcc-5编译它时:

代码语言:javascript
复制
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

我正在使用Ubuntu14.04,我能做些什么来使用getrandom?由于它是一个“新的”syscall,我如何使用它?

编辑:

代码语言:javascript
复制
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= getrandom(buf,l,o)代替r时,它是相同的。

EN

回答 5

Stack Overflow用户

发布于 2017-07-21 14:09:04

在2.25版中添加到glibc中。。到2017年7月,大多数Linux发行版还没有更新到这个版本(例如Debian最近发布的版本,刚刚发布,有2.24个版本),但它们很快就会更新。

下面是如何在可用的情况下使用glibc包装器,如果没有,则返回到原始系统调用:

代码语言:javascript
复制
#define _GNU_SOURCE 1
#include <sys/types.h>
#include <unistd.h>

#if defined __GLIBC__ && defined __linux__

# if __GLIBC__ > 2 || __GLIBC_MINOR__ > 24
#  include <sys/random.h>

int
my_getentropy(void *buf, size_t buflen)
{
    return getentropy(buf, buflen);
}

# else /* older glibc */
#  include <sys/syscall.h>
#  include <errno.h>

int
my_getentropy(void *buf, size_t buflen)
{
    if (buflen > 256) {
        errno = EIO;
        return -1;
    }
    return syscall(SYS_getrandom, buf, buflen, 0);
}

# endif

#else /* not linux or not glibc */
#error "Need implementation for whatever operating system this is"

#endif

(正如其他答案所指出的,确保内核3.17或更高版本也是必要的。如果在较旧的内核上运行,上述版本的my_getentropy都将失败,并将errno设置为ENOSYS。)

票数 12
EN

Stack Overflow用户

发布于 2015-09-19 15:10:29

因此,getrandom似乎是一个而不是一个函数,只是一个syscall。

因此,需要这样做:

代码语言:javascript
复制
/* 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;
}
票数 11
EN

Stack Overflow用户

发布于 2015-06-12 10:09:02

linux是在linux 3.17内核中引入的。Ubuntu14.04随内核3.13一起发布,所以您必须更新到最新的内核,才能获得syscall。

要获得用于Ubuntu的linux内核的.deb包,请查看kernel.ubuntu.comaskubuntu.com也讨论了这个问题。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30800331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档