c++ - setuid 和 getuid 似乎不起作用

标签 c++ c linux qt

我正在编写一个访问 Linux 原始套接字的 Qt 应用程序,因此我需要 root 权限才能运行该程序。所以我这样做了:

在我的程序中,开头是这样的:

if (getuid() != 0)
{
    fprintf(stderr, "You must be root to run this program. UID = %i\n", getuid());
    exit(-1);
}

然后,我以 root 身份执行“chmod 6777 myProgram”。

但是,当我尝试以普通用户身份运行它时,它说: “你必须是 root 才能运行这个程序。UID = 1002”,其中 1002 是我当前使用的用户 ID。

任何人都可以发布线索吗?

谢谢

最佳答案

您混淆了 getuid()geteuid()。来自 getuid() 的手册页:

The getuid() function returns the real user ID of the calling process. The geteuid() function returns the effective user ID of the calling process.

The real user ID is that of the user who has invoked the program. As the effective user ID gives the process additional permissions during execution of set-user-ID mode processes, getuid() is used to determine the real-user-id of the calling process.

Linux 的手册页更加简洁(上一个来自 Mac OS X):

When a normal program is executed, the effective and real user ID of the process are set to the ID of the user executing the file. When a set ID program is executed the real user ID is set to the calling user and the effective user ID corresponds to the set ID bit on the file being executed.

对于 setuid 程序,该文件需要由您要为其设置 setuid 的用户标识所有,在大多数情况下为 root。

关于c++ - setuid 和 getuid 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101413/

相关文章:

c++ - 为什么 C++11 字符串新函数(stod、stof)不是字符串类的成员函数?

c++ - 如何通过扩展以下类型特征来删除 decltype(& MyClass::func) 部分?

c++ - 为什么 std::cin 不能隐式转换为 bool?

c - 整数立方根

linux - 创建与文件同名但在不同文件夹中的 tar

c++ - 在远程服务器上执行命令行命令

c++ - 如何将字符串传递给 GPU 并从内核中取回以显示它? (C++ OpenCL)

c++ - 用于在 linux 中获取 CPU 负载的 C API

C - 将字符串输入到 int 变量中?

c# - 在 Linux 中使用 Windows 窗体编译 C# 代码