什么是os.geteuid()呢?

时间:2016-03-21 02:27:55

标签: python-2.x raspbian

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

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

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

为了调查这是否实际,我试图找出os.geteuid()实际上在做什么。

由于os模块本质上是特定于操作系统的,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的定义是否被隐藏,因为它可能被欺骗以返回错误的东西(那会很糟糕)?我在哪里可以看到这些功能&#39; 实际来源?

1 个答案:

答案 0 :(得分:1)

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标准库,它永远不会错。