通过升级C库解决了这个问题。
我想使用syscall getrandom (http://man7.org/linux/man-pages/man2/getrandom.2.html)
gcc-5 -std=c11测试.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;
}或
int main(void)
{
void *buf = NULL;
size_t l = 5;
unsigned int o = 1;
int r = getrandom(buf, l, o);
return 0;
}总之,当我试图用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我正在使用Ubuntu14.04,我能做些什么来使用getrandom?由于它是一个“新的”syscall,我如何使用它?
编辑:
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时,它是相同的。
发布于 2017-07-21 14:09:04
在2.25版中添加到glibc中。。到2017年7月,大多数Linux发行版还没有更新到这个版本(例如Debian最近发布的版本,刚刚发布,有2.24个版本),但它们很快就会更新。
下面是如何在可用的情况下使用glibc包装器,如果没有,则返回到原始系统调用:
#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。)
发布于 2015-09-19 15:10:29
因此,getrandom似乎是一个而不是一个函数,只是一个syscall。
因此,需要这样做:
/* 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;
}发布于 2015-06-12 10:09:02
linux是在linux 3.17内核中引入的。Ubuntu14.04随内核3.13一起发布,所以您必须更新到最新的内核,才能获得syscall。
要获得用于Ubuntu的linux内核的.deb包,请查看kernel.ubuntu.com。askubuntu.com也讨论了这个问题。
https://stackoverflow.com/questions/30800331
复制相似问题