Sequel to my previous post here:
http://www.linuxquestions.org/questi...u-8.04-662853/ I discovered a separate issue. I'm working with the same book, however now I'm trying to get the memory address of an environment variable. I googled around, and found this:
Quote:
There is not one single environment space. In fact, every program has their own unique environment space. By default, the environment from one program is copied into the next under it via fork and execs. A fork will make an (almost) exact copy of a program/process, environment and all. Most libc exec calls also will copy the current environment into the new program imagine. There are a few versions of the libc exec calls that allow you to pass in an envirment and it can be anything you want it to be (hopefully a list of X=Y pairs though).
|
from here:
http://www.developerweb.net/forum/showthread.php?t=3291
What I have done is this:
export MYVAR='test'
echo $MYVAR
And of course it echos 'test'. Here is the C source I'm trying to run (without the includes).
Code:
int main(int argc, char *argv[]) {
printf("%s is located at: %p\n", argv[1], getenv(argv[1]));
printf("%s was found at the address.\n", ptr);
}
And the problem is that every time I run the program I get a different address. As per the above quote, I gather that the environment variables are
copied each time the given program runs, and that they will be in a different address each time, so the results would make sense. However, the book I am reading, which has thus far proven itself to be credible, and has coding examples tested on Ubuntu 7.10, said to use the above C source to find the memory address of the environment variable and then enter it as the argument for another, precompiled binary using perl. I tried it and obviously it doesn't work.
I added the second printf so that I can verify that the given address does actually contain the information, and it does every time.
From what I've been reading, I gather that what I'm looking for is a thread safe version of getenv(), but I haven't found one thus far. I found this:
https://www.securecoding.cert.org/co...ed+by+getenv() but either I don't understand it or it's not what I'm looking for.
I'd appreciate if someone could give me a hand with this, I'm a little stuck.
Thanks,
-Peter