[英]syscall(SYS_getuid) returns different result from getuid()
我想使用系统调用来获取当前用户的 id。 我试过这样:
#include <unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int uid = syscall(SYS_getuid);
printf("%d\n", uid);
return 0;
}
我用 root 执行它,但它打印-1
而不是0
。
如果我将其替换为int uid = syscall(SYS_getuid);
,然后它正确返回0
。 我错了什么? 如何使用syscall
获取当前用户 ID?
我在i686/ubuntu
docker 映像上运行它,因为我必须创建 32 位可执行文件。
最小的可重现示例:
Dockerfile
FROM i686/ubuntu
RUN apt-get update
RUN apt-get install --assume-yes --no-install-recommends --quiet \
gcc libc6-dev
RUN apt-get clean all
main.c
#include <unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[])
{
int uid = syscall(SYS_getuid);//getuid();//
if(uid == -1)
printf("Error: %s\n", strerror(errno));
printf("%d\n", uid);
return 0;
}
运行(在 x64 Windows10 上):
docker build --platform linux/386 -t my-gcc .
docker run --platform linux/386 --rm -v ${pwd}:/usr/local/src/:rw my-gcc gcc -m32 -xc /usr/local/src/main.c -o /usr/local/src/main
docker run --platform linux/386 --rm -v ${pwd}:/usr/local/src/:rw my-gcc /usr/local/src/main
结果是:
Error: Function not implemented
-1
每个getuid(2) :
最初的 Linux getuid() 和 geteuid() 系统调用仅支持 16 位用户 ID。 随后,Linux 2.4 增加了 getuid32() 和 geteuid32(),支持 32 位 ID。 glibc getuid() 和 geteuid() 包装函数透明地处理 kernel 版本之间的变化。
显然,您正在 kernel 上运行程序,该程序已编译出旧的getuid
系统调用,并且 x86-32 上只有getuid32
可用。 如果您运行fgrep CONFIG_UID16 "/boot/config-$(uname -r)"
,您将能够查看您运行的 kernel 是否支持 16 位系统调用。 如果此命令打印CONFIG_UID16=y
以外的任何内容,则表示旧的系统调用不可用。
如果您改为调用SYS_getuid32
,它应该可以正常工作。 请注意, SYS_getuid32
可能无法在其他架构上使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.