Kiril Kirov
Kiril Kirov

Reputation: 38163

Can the child process affect parent process' environment?

What does "the child inherits the parent's environment" mean? Inherits by copying the whole environment, or inherits by receiving pointer to the same environment (somehow)?


Here's my scenario:

  1. I have a running process P with its own environment (variables)
  2. At some point, P executes fork
  3. In the 0-clone of the if-statement (a.k.a. in the child process C), an execv is executed
  4. Both processes continue running independently.

So, in some moment, the application stops working fine. And the reason is - "broken" environment.

The interesting part is, that both environments are changed.. When I start the parent process and execute

$ cat /proc/PID/environ

for both - the parent and the process, everything is fine. Some hours later, the app stops working and when I execute the line above again (to check the environment), both are changed and a lot of environment variables are missing - only the standard ones are there (like PWD, HOME, USER, etc.).

How is this possible? And where could the the problem - in the child or in the parent?


EDIT: Thanks all for the answers, +1 from me, as they were all correct ( @caf, @Saphrosit and @R..). The reason for this issue is really silly..

All environment variables were placed in /etc/profile which is executed AFTER LOGIN (that.. I didn't know).

Well, it appeared, that the issue have happened on restart of the machine. So, on start-up, the application is started again, but /etc/profile/ is not executed/read. And this causes the bad behavior. And that's why the problem disappears on manual restart - once a root is logged in (through ssh), the environment variables from /etc/profiles are read, and when the parent process is restarted (by root), it's all fine - the environment variables are inherited.

Stupid mistake.

Upvotes: 2

Views: 3648

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215241

This question is really nearly impossible to answer without more details, but I'm going to take a stab at it anyway?

Are you sure that the contents of the environment are actually valid at the time you call fork? It's certainly possible that you corrupted memory but that the parent has already obtained and cached copies of the variables it cares about at this point, and that only later does it break when it tries to re-get one. If this is the case, the environment should be broken in the child too, but the child might not care...

If that's not the issue, the only possibilities left on a working system seem to be that either the parent is getting restarted and you're not aware of it, or the parent corrupted its own environment after forking.

Otherwise, perhaps you have a swap partition on a broken device and the environment is getting swapped out and corrupted when it's swapped back in...?

Upvotes: 1

Manlio
Manlio

Reputation: 10865

...Or in both?

The child start with a duplicate of his parent environment, so cannot affect his parent one. So I don't think the child changed the environment of the father, but maybe the father changed his own.

Without knowing what exactly the program does it's very hard to say where is the problem...

Upvotes: 2

caf
caf

Reputation: 239041

The child inherits a copy of the parent's environment at the moment of the fork(). Subsequent changes to the environment in either process do not affect the other.

The only way you could alter this is by doing something very strange, like placing the environment in a MAP_SHARED area, or using ptrace(). You'd know it if you did something like this, though.

Upvotes: 5

Related Questions