[Lazarus] SetEnv on Linux 64bit

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Wed Apr 4 13:40:35 CEST 2012



On Wed, 4 Apr 2012, Honza wrote:

> 2012/4/4  <michael.vancanneyt at wisa.be>:
>
> Thank you for the help. Unfortunately I'm now confused even more. I
> believe it's my fault, not yours.
>
>> You can always modify them (it's just data in memory), but modifying them
>> will only change your private view of the variables. The changes you make
>> will not propagate to libraries or newly started processes.
>
> If "library" means a .so (or .dll, but Windows are not that much of a
> Posix system), then it is in in-process space, so I would think the
> change *does* propagate, i.e. the change is observable by a call to
> getenv() from inside a library function. About started processes see
> below.

Typically, libraries will read the environment variables at startup.
Any subsequent change to the environment will not have any effect.

>> Therefor it makes no sense to change them, and the FPC runtime does not
>> allow
>> it. If we did allow it, people would expect DOS/Windows like behaviour,
>> which
>> we simply cannot provide.
>>
>> If you read the second part of the provided URL, you'll see that the POSIX
>> group recognises that the behaviour of setenv() is problematical.
>>
>> The URL points - for restrictions - to the exec function
>>
>> http://pubs.opengroup.org/onlinepubs/007904975/functions/exec.html
>>
>> Where it says:
>>
>> "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 extrn char **environ (just environ from now on) points to the
> environment variables of the executing process. Setenv may mutates
> this environment. The various forms of exec do have either explicit
> envp, so one can create any required environment vars set, or pass the
> current environ, effectively enabling the inheritance of the (posibly
> mutated) environment as an option.
>
> The other forms, w/o envp, are defined to do exactly that, i.e. they
> will use the current environ as their environment vars pointer,

Not the current.

The EXTERNAL variable environ, i.e. the one that the kernel passed on,
which cannot be modified. You can only modify your local copy.

Michael.




More information about the Lazarus mailing list