有没有办法使用Perl以及在Linux或Solaris环境中读取其他进程的环境变量?

时间:2021-05-23 22:58:43

In Perl, I need to read the environment of other processes.

在Perl中,我需要阅读其他进程的环境。

  • The script is running with root privileges.
  • 该脚本以root权限运行。
  • The script will be running in both Linux and Solaris.
  • 该脚本将在Linux和Solaris中运行。
  • I would like a solution that's mostly platform agnostic, at least between Linux and Solaris. In Linux, examining the /env/<proc_id>/environ can get me the answer.
  • 我想要一个主要与平台无关的解决方案,至少在Linux和Solaris之间。在Linux中,检查/ env / / environ可以得到答案。
  • I would like to avoid having to fork. I already have a solution forking "/usr/ucb/ps -auxwwwe $pid"
  • 我想避免不得不分叉。我已经有一个解决方法分叉“/ usr / ucb / ps -auxwwwe $ pid”

Any ideas?

有任何想法吗?

6 个解决方案

#1


7  

For Solaris, you could try the procfs module from CPAN. Even though this module still seems quite young, this quote sounds hopeful:

对于Solaris,您可以从CPAN尝试procfs模块。虽然这个模块看起来还很年轻,但这句话听起来很有希望:

Brian Farrell sent a very useful patch which handles inspection of argv and environment of processes other than the currently running process.

Brian Farrell发送了一个非常有用的补丁,用于处理argv和当前正在运行的进程以外的进程环境的检查。

I imagine that this is probably just the initial environment (just like the environ file under linux), but that seems to be what you want?

我想这可能只是初始环境(就像linux下的environ文件一样),但这似乎是你想要的?

Otherwise, although I see you say you don't want to fork, a simple solution would probably to crank ~20 lines of C to produce a small program that just spits out the environment on Solaris as the exact equivalent of the Linux environ file. I have something very similar in C already. If you're interested, I can post it.

否则,虽然我看到你说你不想分叉,但是一个简单的解决方案可能会产生一个小的程序来生成一个小程序,它只是吐出Solaris上的环境,就像Linux environ文件的完全等价。我已经在C中有类似的东西。如果你有兴趣,我可以发布。

EDIT (after reading OpenSolaris pargs.c): The environment buffer is reallocated under Solaris when the environment changes, so the psinfo pointer may be invalid. For a bullet proof solution, you need to hunt down _environ. That's all probably more hassle than you need... pargs -e <pid> might be a nicer alterative to UCB ps(1) if you do go the fork route, though.

编辑(读取OpenSolaris pargs.c之后):环境更改时,在Solaris下重新分配环境缓冲区,因此psinfo指针可能无效。对于防弹解决方案,您需要追捕_environ。这可能比你需要的更麻烦......但是,如果你选择了叉路线,那么pargs -e 可能是UCB ps(1)的更好的替代品。

#2


12  

In linux it looks like the /proc/<pid>/environ psuedofiles contain the environ variable passed when the process was created. If you have sufficient permission, you can read those.

在linux中,看起来/ proc / / environ psuedofiles包含创建进程时传递的environ变量。如果您有足够的权限,则可以阅读这些权限。

They do not appear to track changes in the processes environment after launch.

它们似乎不会在启动后跟踪进程环境中的更改。

That suggests that you would have to disect the processes memory dump to get what you're asking for.

这表明您必须通过进程内存转储来获取您要求的内容。

Tricky.

棘手。

#3


9  

The GNU 'binutils' package includes a CLI utility called strings. See http://www.gnu.org/software/binutils/ for more info.

GNU'binutils'包中包含一个名为strings的CLI实用程序。有关详细信息,请参阅http://www.gnu.org/software/binutils/。

strings /proc/pid/environ - prints out a nice list of the environment variables much like env.

strings / proc / pid / environ - 打印出很好的环境变量列表,就像env一样。

#4


5  

The first thing that comes to my mind is using GDB to attach to the process in question, and then asking GDB to get the environment for you. You can do this with the "show environment" command in the GDB shell.

我想到的第一件事就是使用GDB附加到相关进程,然后让GDB为您获取环境。您可以使用GDB shell中的“show environment”命令执行此操作。

It looks like there is a Perl module that can do this for you, Devel::GDB. I have not tried it yet, but it looks like a Simple Matter Of Programming to create the Devel::GDB object, connect to the process you want to inspect, send the "show environment" command, and then parse the results.

看起来有一个Perl模块可以为你做这个,Devel :: GDB。我还没有尝试过,但它看起来像一个简单的编程问题来创建Devel :: GDB对象,连接到你想要检查的进程,发送“show environment”命令,然后解析结果。

I do have to say though... when the solution is this complicated, you are probably doing something else wrong. Why do you need the environment for a random process, anyway?

我不得不说...虽然解决方案很复杂,但你可能做错了什么。无论如何,为什么你需要一个随机过程的环境?

#5


2  

If ps can do it, like you say, then your answer can be found somewhere in the source code of ps. That would avoid the spawning of a new process.

如果ps可以像你说的那样做,那么你的答案可以在ps的源代码中找到。这样可以避免产生新流程。

#6


0  

On Linux it may be enough to resolve the /proc/[pid]/cwd symlink, see procfs(5).

在Linux上,解析/ proc / [pid] / cwd符号链接可能就足够了,请参阅procfs(5)。

#1


7  

For Solaris, you could try the procfs module from CPAN. Even though this module still seems quite young, this quote sounds hopeful:

对于Solaris,您可以从CPAN尝试procfs模块。虽然这个模块看起来还很年轻,但这句话听起来很有希望:

Brian Farrell sent a very useful patch which handles inspection of argv and environment of processes other than the currently running process.

Brian Farrell发送了一个非常有用的补丁,用于处理argv和当前正在运行的进程以外的进程环境的检查。

I imagine that this is probably just the initial environment (just like the environ file under linux), but that seems to be what you want?

我想这可能只是初始环境(就像linux下的environ文件一样),但这似乎是你想要的?

Otherwise, although I see you say you don't want to fork, a simple solution would probably to crank ~20 lines of C to produce a small program that just spits out the environment on Solaris as the exact equivalent of the Linux environ file. I have something very similar in C already. If you're interested, I can post it.

否则,虽然我看到你说你不想分叉,但是一个简单的解决方案可能会产生一个小的程序来生成一个小程序,它只是吐出Solaris上的环境,就像Linux environ文件的完全等价。我已经在C中有类似的东西。如果你有兴趣,我可以发布。

EDIT (after reading OpenSolaris pargs.c): The environment buffer is reallocated under Solaris when the environment changes, so the psinfo pointer may be invalid. For a bullet proof solution, you need to hunt down _environ. That's all probably more hassle than you need... pargs -e <pid> might be a nicer alterative to UCB ps(1) if you do go the fork route, though.

编辑(读取OpenSolaris pargs.c之后):环境更改时,在Solaris下重新分配环境缓冲区,因此psinfo指针可能无效。对于防弹解决方案,您需要追捕_environ。这可能比你需要的更麻烦......但是,如果你选择了叉路线,那么pargs -e 可能是UCB ps(1)的更好的替代品。

#2


12  

In linux it looks like the /proc/<pid>/environ psuedofiles contain the environ variable passed when the process was created. If you have sufficient permission, you can read those.

在linux中,看起来/ proc / / environ psuedofiles包含创建进程时传递的environ变量。如果您有足够的权限,则可以阅读这些权限。

They do not appear to track changes in the processes environment after launch.

它们似乎不会在启动后跟踪进程环境中的更改。

That suggests that you would have to disect the processes memory dump to get what you're asking for.

这表明您必须通过进程内存转储来获取您要求的内容。

Tricky.

棘手。

#3


9  

The GNU 'binutils' package includes a CLI utility called strings. See http://www.gnu.org/software/binutils/ for more info.

GNU'binutils'包中包含一个名为strings的CLI实用程序。有关详细信息,请参阅http://www.gnu.org/software/binutils/。

strings /proc/pid/environ - prints out a nice list of the environment variables much like env.

strings / proc / pid / environ - 打印出很好的环境变量列表,就像env一样。

#4


5  

The first thing that comes to my mind is using GDB to attach to the process in question, and then asking GDB to get the environment for you. You can do this with the "show environment" command in the GDB shell.

我想到的第一件事就是使用GDB附加到相关进程,然后让GDB为您获取环境。您可以使用GDB shell中的“show environment”命令执行此操作。

It looks like there is a Perl module that can do this for you, Devel::GDB. I have not tried it yet, but it looks like a Simple Matter Of Programming to create the Devel::GDB object, connect to the process you want to inspect, send the "show environment" command, and then parse the results.

看起来有一个Perl模块可以为你做这个,Devel :: GDB。我还没有尝试过,但它看起来像一个简单的编程问题来创建Devel :: GDB对象,连接到你想要检查的进程,发送“show environment”命令,然后解析结果。

I do have to say though... when the solution is this complicated, you are probably doing something else wrong. Why do you need the environment for a random process, anyway?

我不得不说...虽然解决方案很复杂,但你可能做错了什么。无论如何,为什么你需要一个随机过程的环境?

#5


2  

If ps can do it, like you say, then your answer can be found somewhere in the source code of ps. That would avoid the spawning of a new process.

如果ps可以像你说的那样做,那么你的答案可以在ps的源代码中找到。这样可以避免产生新流程。

#6


0  

On Linux it may be enough to resolve the /proc/[pid]/cwd symlink, see procfs(5).

在Linux上,解析/ proc / [pid] / cwd符号链接可能就足够了,请参阅procfs(5)。