如何知道环境变量是否从父进程继承?

时间:2022-03-22 22:41:36

Consider a process running in the PID 100 (it can be an ordinary shell command line). So, we launch a program (fork()/exec()) which starts to run in the PID 101. Now our playground is ready:

考虑在PID 100中运行的进程(它可以是普通的shell命令行)。因此,我们启动一个程序(fork()/ exec()),它开始在PID 101中运行。现在我们的游乐场准备好了:

The program running in the PID 101 exports some environment variables, and do an exec() in a second program, Then, it starts to run in the same PID 101 ("environment").

在PID 101中运行的程序导出一些环境变量,并在第二个程序中执行exec(),然后,它开始在相同的PID 101(“环境”)中运行。

How can this second program know if a certain environment variable was exported from the parent PID 100 or was defined by the predecessor program running in the "reused" current PID ?

第二个程序如何知道某个环境变量是从父PID 100导出还是由在“重用”当前PID中运行的前任程序定义的?

3 个解决方案

#1


2  

How can this second program know if a certain environment variable was exported from the parent PID 100 or was defined by the predecessor program running in the "reused" current PID ?

第二个程序如何知道某个环境变量是从父PID 100导出还是由在“重用”当前PID中运行的前任程序定义的?

There's no way differentiate that (unless the variables themselves contain some additional information -- such as prefix/suffix in their names or values, etc).

没有办法区分它(除非变量本身包含一些额外的信息 - 例如名称或值中的前缀/后缀等)。

As far as, PID 101 is concerned every environment variable is from its parent (a process has only one parent) and it doesn't care and can't know if it's from PID 100 or from the parent of PID 100 or some other process leading upto the exec of PID 101.

就PID 101而言,每个环境变量都来自其父项(一个进程只有一个父项)并且它不关心并且无法知道它是来自PID 100还是来自PID 100的父项或其他一些过程领先于PID 101的执行官。

#2


0  

You can compare environment of 101 with the original environment of father /bin/env

您可以将101的环境与father / bin / env的原始环境进行比较

#3


0  

Seems that there are no ways to do that, but I used the following workaround:

似乎没有办法做到这一点,但我使用了以下解决方法:

The first process exports a variable with its realpath and PID. So, when in the same PID but different executable path I assume the variable was inherited, when in the same PID and exe path, I assume that it's in the process (program) that created the variables.

第一个进程使用realpath和PID导出变量。因此,当处于相同的PID但不同的可执行路径时,我假设变量是继承的,当处于相同的PID和exe路径时,我假设它在创建变量的进程(程序)中。

I know it isn't unfailing. There are no warranties that the PID and realpath will assure if the variables are inherited or no. But, is working.

我知道这不是一成不变的。如果变量是继承的或不是,则不保证PID和realpath将保证。但是,正在努力。

#1


2  

How can this second program know if a certain environment variable was exported from the parent PID 100 or was defined by the predecessor program running in the "reused" current PID ?

第二个程序如何知道某个环境变量是从父PID 100导出还是由在“重用”当前PID中运行的前任程序定义的?

There's no way differentiate that (unless the variables themselves contain some additional information -- such as prefix/suffix in their names or values, etc).

没有办法区分它(除非变量本身包含一些额外的信息 - 例如名称或值中的前缀/后缀等)。

As far as, PID 101 is concerned every environment variable is from its parent (a process has only one parent) and it doesn't care and can't know if it's from PID 100 or from the parent of PID 100 or some other process leading upto the exec of PID 101.

就PID 101而言,每个环境变量都来自其父项(一个进程只有一个父项)并且它不关心并且无法知道它是来自PID 100还是来自PID 100的父项或其他一些过程领先于PID 101的执行官。

#2


0  

You can compare environment of 101 with the original environment of father /bin/env

您可以将101的环境与father / bin / env的原始环境进行比较

#3


0  

Seems that there are no ways to do that, but I used the following workaround:

似乎没有办法做到这一点,但我使用了以下解决方法:

The first process exports a variable with its realpath and PID. So, when in the same PID but different executable path I assume the variable was inherited, when in the same PID and exe path, I assume that it's in the process (program) that created the variables.

第一个进程使用realpath和PID导出变量。因此,当处于相同的PID但不同的可执行路径时,我假设变量是继承的,当处于相同的PID和exe路径时,我假设它在创建变量的进程(程序)中。

I know it isn't unfailing. There are no warranties that the PID and realpath will assure if the variables are inherited or no. But, is working.

我知道这不是一成不变的。如果变量是继承的或不是,则不保证PID和realpath将保证。但是,正在努力。