|
Home > Archive > Unix Programming > May 2006 > when is environment sorted?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
when is environment sorted?
|
|
| Henry Townsend 2006-05-12, 1:15 pm |
| In researching a bug I've had to dig deep into how environments work. On
Windows, apparently, a process must supply a *sorted* environment to a
child process. On Unix there's no such sorting restriction. However, I
could have sworn I've observed in the past that Unix tends to sort the
environment at exec anyway, such that the environment of a newly started
program is always sorted. It can then get out of order as you putenv etc
and gets cleaned up again at the next exec.
However, a test program I just wrote does not show this to be the case
on Solaris:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
char *envp[3];
argv++, argc--;
envp[0] = "BBB=2";
envp[1] = "AAA=1";
envp[2] = NULL;
execve(argv[0], argv, envp);
perror(argv[0]);
return 2;
}
% ./texecve /bin/env
BBB=2
AAA=1
Am I remembering wrong, or is this a platform difference, or what?
Thanks,
HT
| |
| Thomas Maier-Komor 2006-05-12, 1:15 pm |
| Henry Townsend wrote:
> In researching a bug I've had to dig deep into how environments work. On
> Windows, apparently, a process must supply a *sorted* environment to a
> child process. On Unix there's no such sorting restriction. However, I
> could have sworn I've observed in the past that Unix tends to sort the
> environment at exec anyway, such that the environment of a newly started
> program is always sorted. It can then get out of order as you putenv etc
> and gets cleaned up again at the next exec.
>
> However, a test program I just wrote does not show this to be the case
> on Solaris:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> int
> main(int argc, char *argv[])
> {
> char *envp[3];
>
> argv++, argc--;
>
> envp[0] = "BBB=2";
> envp[1] = "AAA=1";
> envp[2] = NULL;
>
> execve(argv[0], argv, envp);
> perror(argv[0]);
> return 2;
> }
>
> % ./texecve /bin/env
> BBB=2
> AAA=1
>
> Am I remembering wrong, or is this a platform difference, or what?
>
> Thanks,
> HT
The opengroup says on
http://www.opengroup.org/onlinepubs...ions/exec.html:
<quotes>
The argument envp is an array of character pointers to null-terminated
strings. These strings shall constitute the environment for the new
process image. The envp array is terminated by a null pointer.
For those forms not containing an envp pointer ( execl(), execv(),
execlp(), and execvp()), the environment for the new process image shall
be taken from the external variable environ in the calling process.
The argv[] and envp[] arrays of pointers and the strings to which those
arrays point shall not be modified by a call to one of the exec
functions, except as a consequence of replacing the process image.
Some implementations provide a third argument to main() called envp.
This is defined as a pointer to the environment. The ISO C standard
specifies invoking main() with two arguments, so implementations must
support applications written this way. Since this volume of IEEE Std
1003.1-2001 defines the global variable environ, which is also provided
by historical implementations and can be used anywhere that envp could
be used, there is no functional need for the envp argument. Applications
should use the getenv() function rather than accessing the environment
directly via either envp or environ. Implementations are required to
support the two-argument calling sequence, but this does not prohibit an
implementation from supporting envp as an optional third argument.
</quotes>
No word about sorting - so this is implementation defined behavior.
Tom
| |
| Henry Townsend 2006-05-12, 1:15 pm |
| Thomas Maier-Komor wrote:
> Henry Townsend wrote:
> The opengroup says on
> http://www.opengroup.org/onlinepubs...ions/exec.html:
>
> No word about sorting - so this is implementation defined behavior.
Right, I'm quite clear that there's no sorting *requirement* on Unix.
Just wondering if other implementations tend to do so.
| |
| Lew Pitcher 2006-05-12, 1:15 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Henry Townsend wrote:
> In researching a bug I've had to dig deep into how environments work. On
> Windows, apparently, a process must supply a *sorted* environment to a
> child process. On Unix there's no such sorting restriction. However, I
> could have sworn I've observed in the past that Unix tends to sort the
> environment at exec anyway, such that the environment of a newly started
> program is always sorted.
My /guess/ is that the sort is a byproduct of the implementation of the
list management techniques used by putenv() (or by the shell).
putenv() and friends likely need some sort of ordering internally to
simplify data management, otherwise the processing overhead of a
search/replace of environment variables would vary directly with the
size of the environment variable list. That could get to be a very
expensive operation with large environment variable lists. OTOH, with an
ordered list, the search overhead could be significantly reduced, making
the management of the list much more efficient.
That the new process receives a sorted environment variable list is a
byproduct of the process used to build the list, and not a requirement.
- --
Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEZImeagVFX4UWr64RAhIZAJ0T4NqiocxD
KQ61aQu6CMIcXkQ3ewCgtax5
bqKU+0oErbzxzGQ5H5BLAkk=
=RzSr
-----END PGP SIGNATURE-----
| |
| Sven Mascheck 2006-05-12, 1:15 pm |
| Henry Townsend wrote:
> I could have sworn I've observed in the past that Unix tends to sort the
> environment at exec anyway, such that the environment of a newly started
> program is always sorted.
As you crossposted this to c.u.solaris:
Especially the traditional Bourne shell, /bin/sh on Solaris,
(and some releases of ksh93, but not dtksh) export a sorted
environment.
Might this have given you such an impression?
| |
| Joerg Schilling 2006-05-12, 7:15 pm |
| In article < VfydnRTTdIhxGvnZnZ2dnUVZ_u2dnZ2d@comcast
.com>,
Henry Townsend <henry.townsend@not.here> wrote:
>
>Right, I'm quite clear that there's no sorting *requirement* on Unix.
>Just wondering if other implementations tend to do so.
Ther is no need to sort the environment and it is only sorted by the fact that
the shell creates a new version from a tree for each child.
--
EMail:joerg@schily.isdn.cs.tu-berlin.de (home) Jörg Schilling D-13353 Berlin
js@cs.tu-berlin.de (uni)
schilling@fokus.fraunhofer.de (work) Blog: http://schily.blogspot.com/
URL: http://cdrecord.berlios.de/old/private/ ftp://ftp.berlios.de/pub/schily
| |
| Gordon Burditt 2006-05-12, 7:15 pm |
| >>>> child process. On Unix there's no such sorting restriction.
>
>Ther is no need to sort the environment and it is only sorted by the fact that
>the shell creates a new version from a tree for each child.
Can you give an example of any specific code (e.g. Bourne shell on Solaris,
if that happens to be the case) which sorts the environment, other
than Windows? I don't think I've observed environment sorting on
any UNIX.
Gordon L. Burditt
| |
| Henry Townsend 2006-05-12, 7:15 pm |
| Gordon Burditt wrote:
>
> Can you give an example of any specific code (e.g. Bourne shell on Solaris,
> if that happens to be the case) which sorts the environment, other
> than Windows? I don't think I've observed environment sorting on
> any UNIX.
As you say, the Bourne shell was already mentioned and functions as a
fine example. Try "/bin/sh -c env" - the output is sorted, at least on
my Sol 9 box. Then try "env" alone: not sorted.
HT
|
|
|
|
|