如何知道两个不同环境中进程的“执行环境”的差异?了解进程的执行环境
为了正确表达问题,我在/opt/plan9/
中安装了plan9port,当我从/opt/plan9/bin/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读取fortunes文件。它必须与exec环境有关,从二进制文件读取库的位置。
虽然这看起来没有引起你的问题,但你没有正确调用execve(),因为在第二个参数('opts')中传递的数组必须以'NULL'结尾。 –