简体   繁体   English

c99中不赞成使用getresuid吗?

[英]Is getresuid deprecated in c99?

I'm trying to compile a function containing a call to getresuid . 我正在尝试编译包含对getresuid的调用的函数。 However it generates the following warning: 但是,它将生成以下警告:

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

Why is there a linker error saying symbol not found for architecture x86_64? 为什么出现链接器错误,指出未为体系结构x86_64找到符号? How do you get it to successfully link? 如何获得成功链接?

If it helps, my original code was: 如果有帮助,我的原始代码是:

#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;
}

I'm trying to compile on Mac OS X 10.10.2 (Yosemite). 我正在Mac OS X 10.10.2(Yosemite)上进行编译。

You need to define before all the headers 您需要在所有标题之前定义

#define _GNU_SOURCE

as getresuid is a GNU extension function. 因为getresuid是GNU扩展功能。

The direct answer is "No; getresuid() is simply not part of C99 — nor of POSIX 2008 (2013)". 直接答案是“不; getresuid()根本不是C99的一部分,也不是POSIX 2008(2013)的一部分”。 So to use it, you have to use a platform-specific extension, and it is not available on all platforms. 因此,要使用它,您必须使用特定于平台的扩展,并且并非在所有平台上都可用。

Incidentally, you don't need #include <sys/types.h> in your code. 顺便说一句,您不需要在代码中包含#include <sys/types.h>

Your code compiles OK for me on Ubuntu 14.04. 您的代码可以在Ubuntu 14.04上为我编译。 However, it fails to compile on Mac OS X 10.10.3 (Yosemite), which is derived from BSD rather than Linux. 但是,它无法在Mac OS X 10.10.3(Yosemite)上编译,后者是从BSD而不是Linux派生的。 The getresuid() function is simply not available — not implemented — on the platform. getresuid()函数在平台上根本不可用(未实现)。

You'll need to use getuid() and geteuid() for the 'r' (real UID) and 'e' (effective UID) information; 您需要将getuid()geteuid()用于“ r”(真实UID)和“ e”(有效UID)信息; I don't think there's a sensible way to get the 's' (saved UID) information. 我认为没有明智的方法来获取“ s”(保存的UID)信息。 Most of the time, the three UID values are the same. 大多数情况下,三个UID值相同。 Only a program that expects to be run with the 'setuid' bit set needs to worry about these, and such programs might want to worry about the saved UID. 只有希望以“ setuid”位设置运行的程序才需要担心这些,而此类程序可能要担心保存的UID。 However, you're probably limited to inspecting the effective UID when the program starts; 但是,您可能仅限于在程序启动时检查有效的UID。 that's the saved UID. 那就是保存的UID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM