Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
enviroment and execve.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
neuron
Advocate
Advocate


Joined: 28 May 2002
Posts: 2371

PostPosted: Thu May 26, 2005 3:52 pm    Post subject: enviroment and execve. Reply with quote

Just a quick programming question in the programming forum (!).

Very simply,

if I execve(bash "export ASD=5"), how do I get the enviroment from the executed program?

getenv("ASD") after that (and after wait()) doesn't work, gives me NULL.

And searching for it I find about 5 billion examples on how pass variables to a program, not getting variables from the program.


(if you didn't completely understand, I need to execute a lot of bash scripts, and variables exported from the first one has to be in the next ones aswell).


Also if I putenv("WHATEVER=5"); in a program, compile it, run it and then do echo $WHATEVER from the shell that doesn't work. How do I work with "global" enviroment variables?
Back to top
View user's profile Send private message
chiwi
Tux's lil' helper
Tux's lil' helper


Joined: 25 Apr 2004
Posts: 135
Location: Buenos Aires, Argentina

PostPosted: Thu May 26, 2005 6:17 pm    Post subject: Reply with quote

as far as i know...when you execve in the system, it "creates" a new shell and passes the command to execute.
So the second time you've executed execve, it wont see changes made to the first environment of (1st) execve.

At least, this happened to me when programming in C under Linux.

dont know what language you are using.

regards..


chiwi.
Back to top
View user's profile Send private message
neuron
Advocate
Advocate


Joined: 28 May 2002
Posts: 2371

PostPosted: Thu May 26, 2005 7:20 pm    Post subject: Reply with quote

yeah your right, seems it's no way of doing what I need to :/
Back to top
View user's profile Send private message
bendagr8
Tux's lil' helper
Tux's lil' helper


Joined: 04 Sep 2003
Posts: 135
Location: OR

PostPosted: Fri May 27, 2005 3:09 am    Post subject: Reply with quote

I'm sorry that I am confused about what you are trying to do, and I hate when people post who don't even know what the OP meant, but, I've been doing a lot of stuff with execve recently, and I don't understand what your problem is.
Code:

int  execve(const  char  *filename,  char  *const  argv [], char *const envp[]);


you can just exec bash with whatever args you want, and then set the environment variables you need in envp[]. If you need variables from the currently running process, just use getenv.
Code:
char *getenv(const char *name);


It SOUNDS like your problem is that you want to get all of the environment variables from the original proc. I'm not sure how to do that, but I'm sure there are only specific variables you will need for a given script.

So there is no way to GET the variables from the previous process because exec has basically overlayed that original process with the new bash, but you can just pass in what you need at the time of exec.

Sorry if this didn't help at all.:)
Back to top
View user's profile Send private message
hjnenc
Veteran
Veteran


Joined: 15 Aug 2004
Posts: 1599
Location: Vienna, Austria

PostPosted: Fri May 27, 2005 5:53 am    Post subject: Reply with quote

Two things to note:

There are no global environment variables. A copy of the environment is passed to the child process when it is created. Any changes the child does to its environment are invisible to the parent.

A successful call to execve does not return. If you are able to do a getenv afterwards, the call failed and the child process was not run at all.
Back to top
View user's profile Send private message
transienteagle
Apprentice
Apprentice


Joined: 24 Dec 2003
Posts: 190
Location: UK

PostPosted: Fri May 27, 2005 8:03 am    Post subject: Reply with quote

neuron,

The easiest way that I know to do what you require is to set up your own control file, writing to it and reading from it as needed.

rgds

TE
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum