在Linux C++中,如何读取指定用户的环境变量?
我知道 getenv() 返回当前用户指定环境变量的值,但我的代码需要 root 权限,因此 getenv() 只会使用 sudo 环境变量。我还知道 SUDO_USER 告诉哪个用户正在调用 sudo,这是我想要用于 getenv() 的用户环境。
char* gnome_env_var = getenv("GDMSESSION"); //returns null as not found in sudo env
char* usr = getenv("SUDO_USER");
有没有办法获取登录用户的环境变量的值,而不是 sudo 环境的值?
编辑 好的,所以我听到的是,环境变量集对于每个进程都是唯一的,而不是用户,并且使用 sudo 调用具有 root 权限的进程调用 execve ,这可以为该进程创建一组全新的环境变量。因此,换句话来说,除了弄乱 sudoers 文件之外,还有在当前进程中查找调用进程的环境变量的方法吗?
我特别需要 GDMSession 环境变量。
I know that getenv() returns a value of specified environment variable of the current user, but my code requires root privileges so getenv() would only use the sudo environment variables. I also know that SUDO_USER tells which user is invoking sudo, which is the user environment I want use for getenv().
char* gnome_env_var = getenv("GDMSESSION"); //returns null as not found in sudo env
char* usr = getenv("SUDO_USER");
Is there a way I can get the value of an environment variable for the logged in user, not the sudo environment?
EDIT
Okay, so what I'm hearing is that the set of environment variables are unique to each process, not user and using sudo to invoke a process with root privileges calls execve which can create an entirely new set of environment variables for that process. So to rephrase, is there a way besides messing with the sudoers file, and within the current process, of finding the calling process's environment variables?
I particularly need the GDMSession environment variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getenv
不会告诉您当前用户的环境变量,而是当前进程的环境变量。用户可以自由地拥有任意数量的环境(并且可以创建进程),例如使用内置的export
shell。在每次调用execve
时,调用程序可以自由地为执行的流程创建一个全新的环境。因此,无法获取用户的环境变量,甚至无法获取执行sudo的进程的环境变量。 为什么你还是想要那个?
不过,您可以通过
/etc/sudoers
。getenv
doesn't tell you about the environment variables of the current user, but the current process. Users are free to have as many environments as they want(and can create processes), for example with theexport
shell built-in. In every call toexecve
, the calling program is free to create an entirely new environment for the executed process.Therefore, there is no way to get the environment variables of the user, or even those of the process executing sudo. Why do you want that anyways?
You can, however, configure sudo to keep some or all environment variables, via the
keep_env
andreset_env
directives in/etc/sudoers
.不存在“用户环境”。 每个进程都有自己的环境变量副本。它们甚至不会自动继承——它们看起来是由 shell 和 C 库维护的幻觉。更准确的说法是,将它们视为每个程序的第二组命令行参数。
所以在回答你的问题之前,你需要先弄清楚你的意思!可能性是有的——注意,它们都不是优雅的,但它们确实存在——但它们关键取决于您想要在哪个进程的状态下获取哪个环境变量以及为什么。
There isn't a "user environment." Every process has its own copy of the environment variables. They don't even automatically inherit -- that they appear to is an illusion maintained by the shell and the C library. It is more accurate to think of them as a second set of command line arguments to every program.
So before we can answer your question, you need to clear up what you mean! There are possibilities - none of them are elegant, mind, but they do exist - but they depend crucially on which environment variable you want to get at in which process's state and why.