os.geteuid() 是做什么的?

What's os.geteuid() do?

我试图在 Raspbian(Raspberry Pi 风格的 Debian Linux)上调试 a Python 2 script 运行,它的代码类似于

euid = os.geteuid()
if euid != 0:
    print("you must be root!")
    exit(1)

似乎在用户环境中,即使使用 sudo 调用脚本,euid 有时也会是非零值。

为了调查这是否是实际的情况,我试图弄清楚os.geteuid()实际上在做什么。

由于 os 模块本质上非常 OS-specific,the source doesn't actually have a clear definition for os.geteuid().

我也试过 hg clone 获取源代码并编译它,然后使用 inspect.findsource(os.geteuid),但是:

TypeError: <built-in function geteuid> is not a module, class, method, function, traceback, frame, or code object

这是...内置?那么"geteuid" in dir(__import__("__builtin__"))应该是True,但不是

geteuid's definition hidden because it could be spoofed into returning the wrong thing (and that would be bad)? Where can I see these sorts of functions'实际来源吗?

ASCII傻问题,搞个傻ANSI。


做了尝试全文搜索源码,但显然我第一次使用错误的命令并放弃了。

$ grep -rnw '.' -e "geteuid"
./Misc/setuid-prog.c:129:    uid_t euid = geteuid();
./Lib/site.py:209:    if hasattr(os, "getuid") and hasattr(os, "geteuid"):
./Lib/site.py:211:        if os.geteuid() != os.getuid():
./Lib/test/test_shutil.py:84:    @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
./Lib/test/test_httpservers.py:339:            if os.name == 'posix' and os.geteuid() != 0:
./Lib/test/test_httpservers.py:395:@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
./Lib/test/test_spwd.py:8:@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() == 0,
./Lib/test/test_posix.py:44:                             "getegid", "geteuid", "getgid", "getgroups",
./Lib/test/test_argparse.py:1532:@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
Binary file ./Lib/tarfile.py matches
./Lib/rexec.py:148:                      'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')
Binary file ./Modules/posixmodule.o matches
./Modules/posixmodule.c:4047:"geteuid() -> euid\n\n\
./Modules/posixmodule.c:4053:    return _PyInt_FromUid(geteuid());
./Modules/posixmodule.c:8944:    {"geteuid",         posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
./Doc/library/rexec.rst:234:   'times', 'uname', 'getpid', 'getppid', 'getcwd', 'getuid', 'getgid', 'geteuid',
./Doc/library/os.rst:136:.. function:: geteuid()
Binary file ./python matches
Binary file ./libpython2.7.a matches

./Modules/posixmodule.c:4053,确实:

#ifdef HAVE_GETEUID
PyDoc_STRVAR(posix_geteuid__doc__,
"geteuid() -> euid\n\n\
Return the current process's effective user id.");

static PyObject *
posix_geteuid(PyObject *self, PyObject *noargs)
{
    return _PyInt_FromUid(geteuid());
}
#endif

我不知道我期待什么,subprocess.check_output(["id"])

用的是C标准库,绝对不会错