C语言 检查用户是否是 C 中的 root 用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4159910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if user is root in C?
提问by Mohit Deshpande
How can I verify if the user is root?
如何验证用户是否为root?
回答by R.. GitHub STOP HELPING ICE
Usually it's a mistake to test if the user is root. POSIX does not even require a root user, but leaves it to the implementation to determine how permissions work. Code such as:
通常测试用户是否为 root 是错误的。POSIX 甚至不需要 root 用户,而是让实现来确定权限如何工作。代码如:
if (i_am_root) do_privileged_op(); else print_error();
will really annoy users with advanced privilege models where root is not necessary to perform the necessary privileged operations. I remember back in the early days of cd burning on Linux, I had to hack all over the cdrecordsource to remove all the useless checks to see if it was running as root, when it worked just fine with permission to read /dev/sga.
使用高级权限模型确实会惹恼用户,因为不需要 root 来执行必要的特权操作。我记得在早期在 Linux 上刻录 cd 时,我不得不破解所有cdrecord源代码以删除所有无用的检查,以查看它是否以 root 身份运行,当它允许读取/dev/sga.
Instead, you should always attemptthe privileged operation you need to perform, and check for EPERMor similar if it fails to notify the user that they have insufficient privileges (and perhaps should retry running as root).
相反,您应该始终尝试您需要执行的特权操作,并检查EPERM或类似操作是否未能通知用户他们没有足够的权限(并且可能应该以 root 身份重试运行)。
The one case where it's useful to check for root is checking if your program was invoked "suid-root". A reasonable test would be:
检查 root 有用的一种情况是检查您的程序是否被调用了“suid-root”。一个合理的测试是:
uid_t uid=getuid(), euid=geteuid();
if (uid<0 || uid!=euid) {
/* We might have elevated privileges beyond that of the user who invoked
* the program, due to suid bit. Be very careful about trusting any data! */
} else {
/* Anything goes. */
}
Note that I allowed for the possibility (far-fetched, but best to be paranoid) that either of the calls to get uid/euid could fail, and that in the failure case we should assume we're suid and a malicious user has somehow caused the syscalls to fail in an attempt to hide that we're suid.
请注意,我考虑了获取 uid/euid 的任一调用可能失败的可能性(牵强,但最好是偏执的),并且在失败的情况下,我们应该假设我们是 suid 并且恶意用户以某种方式导致系统调用失败以试图隐藏我们是 suid。
回答by Matthew Flaschen
getuidor geteuid, depending on what you really mean. In either case, 0 means root.
getuid或者geteuid,取决于你的真正意思。在任何一种情况下,0 都表示根。
if(geteuid() != 0)
{
// Tell user to run app as root, then exit.
}
The point made by R is valid. You should consider trial and error, or another approach that does not explicitly require root.
R 提出的观点是有效的。您应该考虑反复试验,或其他不明确要求 root 的方法。
回答by mohamadali abasnejad
better to use getuid or geteuid but it is in zconf.h header file and you must enter that like bellow :
最好使用 getuid 或 geteuid 但它在 zconf.h 头文件中,您必须像下面这样输入:
#include <zconf.h>
#include <stdio.h>
int main()
{
int a;
a=getuid();
//or you can use a=geteuid();
//euid is effective user id and uid is user id
// both euid and uid are zero when you are root user
if (a==0){
printf("you are root user");
//so you can do what`enter code here`ever `enter code here` you want as root user
}
else
printf("please run the script as root user !");
return 0;
}

