2008-08-13 06:23:06     put and get global env variable

Document created by Aaronwu Employee on Aug 8, 2013
Version 1Show Document
  • View in full screen mode

2008-08-13 06:23:06     put and get global env variable

Hadi Boulfelfel (FRANCE)

Message: 60387   

 

Hi,

 

I need some help about geting and seting a global permenent env variable from C code.  I use getenv() and setenv() but the variable is not permanent!

 

Thank you for help.

TranslateQuoteReplyEditDelete

 

 

2008-08-13 07:14:05     Re: put and get global env variable

Mike Frysinger (UNITED STATES)

Message: 60389   

 

i really dont know what you mean by "permanent".  putenv/getenv does exactly that: it modifies the active environment.  please read the man pages for the associated functions.

QuoteReplyEditDelete

 

 

2008-08-13 08:30:10     Re: put and get global env variable

Hadi Boulfelfel (FRANCE)

Message: 60392   

 

Mike, for exemple,  when I try to get the SHELL env variable with getenv("SHELL") it returns NULL, whereas it does it correctly with the PATH env variable.

 

root:/> env

HOME=/

SHELL=/bin/sh

PATH=/bin:/usr/bin:/etc:/sbin:/usr/sbin

TERM=linux

 

TranslateQuoteReplyEditDelete

 

 

2008-08-13 08:54:18     Re: put and get global env variable

Mike Frysinger (UNITED STATES)

Message: 60393   

 

environments are inherited, not some global resource.  the shell sets the SHELL variable.

QuoteReplyEditDelete

 

 

2008-08-13 09:00:16     Re: put and get global env variable

Andrea Federico Grisotto (ITALY)

Message: 60394   

 

I use busybox v1.4.1 (uClinux 2008R1) and this source code:

 

-----------------------------------------------------

 

#include <stdlib.h>

#include <stdio.h>

 

int main()

{

printf("shell=%s\n", getenv("SHELL"));

return 0;

}

-----------------------------------------------------

 

issue the correct result: "shell=/bin/sh"

 

 

QuoteReplyEditDelete

 

 

2008-08-13 09:34:33     Re: put and get global env variable

Hadi Boulfelfel (FRANCE)

Message: 60398   

 

Andrea Federico Grisotto, Thank you I works fine like you say. In fact I call getenv() and setenv() from a cgi program and from there I cannot get the $SHELL variable, I got only the $PATH variable.

 

Thank you Mike, could you tell me how to write some global variables or some thing like to communicate between 2 threads?

TranslateQuoteReplyEditDelete

 

 

2008-08-13 09:45:17     Re: put and get global env variable

Mike Frysinger (UNITED STATES)

Message: 60400   

 

cgi programs are not children of the shell, therefore it makes sense that SHELL isnt in their environment

 

threads already have shared memory ... there's no point in cramming things into the environment

 

as for inter-process communication, the environment would never work for two-way ... each process has its own environment that no other process can modify

QuoteReplyEditDelete

 

 

2008-08-13 10:12:27     Re: put and get global env variable

Hadi Boulfelfel (FRANCE)

Message: 60403   

 

Ok Mike, all I want to do is to put some thing on the system in order that when the cgi runs the next time It can check it if it was modified or not. I could do that by wrting some file on the fs but I want to see if there is any other mathod to do it.

TranslateQuoteReplyEditDelete

 

 

2008-08-13 10:28:39     Re: put and get global env variable

Mike Frysinger (UNITED STATES)

Message: 60405   

 

you can either use POSIX shared memory (shm) functions or use temporary files ... they'll be about the same performance wise (assuming you store the files on tmpfs), but files will probably be simpler for you to use unless you already know the shm API ...

QuoteReplyEditDelete

 

 

2008-08-13 10:38:32     Re: put and get global env variable

Hadi Boulfelfel (FRANCE)

Message: 60407   

 

Ok thank you.

Attachments

    Outcomes