- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Setting ENVIRONMENT variable from C program
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 09:27 AM
тАО01-31-2003 09:27 AM
Setting ENVIRONMENT variable from C program
When I use PUTENV inside a C program, the new value is visible by using GETENV. But when the executable program ends and I am back to shell prompt, the new value is not visible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 09:32 AM
тАО01-31-2003 09:32 AM
Re: Setting ENVIRONMENT variable from C program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 09:33 AM
тАО01-31-2003 09:33 AM
Re: Setting ENVIRONMENT variable from C program
KSH/BSH/POSIX
. /dir/yourcode
CSH
source /dir/yourcode
Regards,
Shannon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 09:48 AM
тАО01-31-2003 09:48 AM
Re: Setting ENVIRONMENT variable from C program
.
rm
etc. etc.
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-25-2004 04:12 PM
тАО03-25-2004 04:12 PM
Re: Setting ENVIRONMENT variable from C program
Only a very opinionated person would 'design' such a feature/restriction IMHO. Why is it so easy in MPE to do things like this - because its USEFUL!!!! If you have a child process that changes the environment, if necessary, its his job to restore it to the old state.
GRRRRRRRRRRRRRRRRRRRRRRRRRRRR.
jp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-28-2004 12:27 PM
тАО03-28-2004 12:27 PM
Re: Setting ENVIRONMENT variable from C program
Anybody know whats going on here please?? is there something special I am not doing in my putenv? I am terminating the string with a \r and then null.
TIA,
jp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-28-2004 05:03 PM
тАО03-28-2004 05:03 PM
Re: Setting ENVIRONMENT variable from C program
So it seems to me that if a child process calls getenv on one of these variables, the ptr it gets back is actually a ptr to static space in the parent!!! Isnt that a security breach? what if the parent stack contains a list of passwords? COuldnt a rogue child find these by playing with the ptr?
GRRRRRRRRRRRRRRRRRRRRR again.
jp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 03:47 AM
тАО03-29-2004 03:47 AM
Re: Setting ENVIRONMENT variable from C program
The warning in the putenv manual is about the use of a stack variable like this-
void setnum(int num)
{
char v[40];
sprintf(v, "NUM=%d", num);
putenv(v);
}
There is no possibility that this would cause the environment to contain anything from another process. It will cause the environment of this process to point to the address of variable v. That stack address is likely be reused by other function calls. The stack pointer moves up and down as the process calls functions and returns from functions. The stack holds parameters, return pointers, saved registers, and local variables. The manual is warning that putenv does not make a copy of the string passed in. You need to make sure that string will remain valid. (That could be done with strdup(v)).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 12:52 PM
тАО03-29-2004 12:52 PM
Re: Setting ENVIRONMENT variable from C program
jp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 01:08 PM
тАО03-29-2004 01:08 PM
Re: Setting ENVIRONMENT variable from C program
YES, or NO?
jp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 01:49 PM
тАО03-29-2004 01:49 PM
Re: Setting ENVIRONMENT variable from C program
This means that putenv() must use either a static variable (as opposed to the default "auto" storage class) when declared within a function. The difference is that a static storage class variable retains its value after the function has gone out of scope. You could also safely use a global variable whether declared "static" or not because in this context "static" does not refer to storage class but rather whether or not a symbol is visible to other modules for linking purposes.
The only way to "out-bushwhack" this in a child process is the deliberate misuse of the vfork() system call --- and that doesn't work in many flavors of UNIX these days and is guaranteed to be less than portable.
If you are really interested in this topic, you should probably start a new thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2004 03:22 AM
тАО03-30-2004 03:22 AM
Re: Setting ENVIRONMENT variable from C program
There is a variation of fork called vfork. It takes a shortcut by not copying all the private data. It requires that a process call exec before the parent process can run again. The intention is that using vfork correctly will be the same as using fork, but a little faster.
The exec call causes an existing process id to load a completely different program. Exec will get rid of all memory regions in the process. They are replaced by the text, data, and stack regions of a new process. There are several versions of exec that take different parameters. They vary in how the new program file is found, how arguments are passed, and how the environment variables are passed on. All versions of exec will either explicity or implicitly pass on a particular set of environment variables that the new program will use. Those are copied from the process' old address space into its new one.
Let's look at your question about calling putenv() in a parent process and getenv() in a child process. The putenv call will modify the environment variable list in the current process. It will have no effect on already existing child processes. Getenv in other processes will continue to return the old value.
Calling fork to create a new process will make a child that inherits copies of all memory in the parent, including the environment strings. Calling execl() or execlp() or execv() or execvp() will copy the current environment variables into new addresses in the new program image that is started in the same process. Calling execle() or execve() will change the environment of the process to be copies of the string values passed in as parameters to that call.
So, a getenv call returns either NULL or a pointer to an address in the process that calls getenv. That address may have been set up by an exec call, a fork call, or a putenv call.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2004 03:45 AM
тАО03-30-2004 03:45 AM
Re: Setting ENVIRONMENT variable from C program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2004 08:27 PM
тАО03-31-2004 08:27 PM
Re: Setting ENVIRONMENT variable from C program
C program
void main()
{
puts("VAR1=newvalue");
}
Shell script:
echo "Before: $VAR1"
eval `a.out`
echo "After: $VAR1"