我如何知道一个进程在两个不同的上下文中的“执行环境”的区别?
为了正确地表达这个问题,我在/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读取财富文件。它一定与exec环境有关,二进制文件从exec环境中读取库。
发布于 2012-11-10 15:47:41
当您调用execve()时,您是在显式地设置NULL环境。因此,fortune程序可能依赖于某个环境变量来查找/opt/plan9/...。在shell提示符下输入env,查看设置了哪些环境变量。
https://stackoverflow.com/questions/13320277
复制相似问题