我如何知道两个不同上下文中进程的“执行环境”的差异?
为了正确阐明问题,我在 /opt/plan9/
中安装了 plan9port,当我从 /opt/plan9/bin/fortune
运行 Fortune 程序时,它工作正常。 (从 /opt/plan9/lib/fortune
和 /opt/plan9/lib/fortune.index
读取财富列表)。当我从 c 代码 (test.c) 内部调用它时
char* opts[] = {"fortune"};
execve("/opt/plan9/bin/fortune", opts, NULL);
它不读取财富榜。我使用 strace 来看看调用这两个二进制文件时有什么区别。
strace -f -eopen ./test
open("/usr/local/plan9/lib/fortunes", O_RDONLY) = -1 ENOENT (No such file or directory)
Misfortune!
+++ exited with 1 +++
给出默认消息“不幸”。
strace -f -eopen Fortune
open("/opt/plan9/lib/fortunes", O_RDONLY) = 3
Snob intellectual bachelors can't have fun in San Antonio. -Ted Nelson
+++ exited with 0 +++
效果非常好。
如何更改 ./test 读取运气文件。它必须与二进制文件从中读取库的执行环境有关。
最佳答案
当您调用 execve()
时,您正在显式设置 NULL
环境。因此,fortune
程序可能依赖于某些环境变量来查找 /opt/plan9/...
。在 shell 提示符下键入 env
以查找设置了哪些环境变量。
关于c - 了解进程的执行环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13320277/