Question

How do I know the difference in the 'execution env' of a process in two different contexts?

To articulate the question properly, I have plan9port installed in /opt/plan9/ and when I run the fortune program from /opt/plan9/bin/fortune it works fine. (reads the list of fortunes from /opt/plan9/lib/fortune and /opt/plan9/lib/fortune.index ). When I call it from inside of a c code (test.c) with

char* opts[] = {"fortune"};
execve("/opt/plan9/bin/fortune", opts, NULL);

It doesn't read the fortune list. I used strace to see what is the difference when I call these two binaries.

strace -f -eopen ./test

open("/usr/local/plan9/lib/fortunes", O_RDONLY) = -1 ENOENT (No such file or directory)
Misfortune!
+++ exited with 1 +++

Gives out the default message "Misfortune".

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 +++

which works perfectly fine.

How do I change ./test read fortunes file. It must have something to do with the exec environment, from where the binary reads the libraries from.

Was it helpful?

Solution

When you call execve(), you are explicitly setting up a NULL environment. So the fortune program is probably depending on some environment variable to find /opt/plan9/.... Type env at a shell prompt to find out which environment variables are set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top