在 c99 中是否弃用了 getresuid?

Is getresuid deprecated in c99?

我正在尝试编译一个包含对 getresuid 的调用的函数。但是它会生成以下警告:

setuid.c:8:3: warning: implicit declaration of function 'getresuid' is invalid
in C99 [-Wimplicit-function-declaration]
getresuid(&ruid, &euid, &suid);
^

Undefined symbols for architecture x86_64:
"_getresuid", referenced from:
_main in setuid-ba46f8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

为什么会出现 link er error not found for architecture x86_64 符号?怎么才能成功link?

如果有帮助,我的原始代码是:

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main() {
    uid_t euid, ruid, suid;
    getresuid(&ruid, &euid, &suid);
    printf("EUID: %d, RUID: %d, SUID: %d\n", euid, ruid, suid);
    return 0;
}

我正在尝试在 Mac OS X 10.10.2 (Yosemite) 上编译。

你需要在所有的headers之前定义

#define _GNU_SOURCE

因为getresuid是一个GNU扩展函数。

直接的答案是"No; getresuid() is simply not part of C99 — nor of POSIX 2008 (2013)"。所以要使用它,你必须使用特定于平台的扩展,而且它并不是在所有平台上都可用。

顺便说一句,您的代码中不需要 #include <sys/types.h>

你的代码在 Ubuntu 14.04 上对我来说编译正常。但是,它无法在 Mac OS X 10.10.3 (Yosemite) 上编译,它源自 BSD 而不是 Linux。 getresuid() 功能在平台上根本不可用——未实现。

您需要使用getuid()geteuid()来获取'r'(真实UID)和'e'(有效UID)信息;我认为没有明智的方法来获取 's'(已保存的 UID)信息。大多数时候,三个 UID 值是相同的。只有设置了 'setuid' 位的 运行 程序才需要担心这些,并且此类程序可能需要担心保存的 UID。但是,您可能仅限于在程序启动时检查有效的 UID;这是保存的 UID。