Is there a way to change the environment variables

2019-01-01 03:40发布

On Unix, is there any way that one process can change another's environment variables (assuming they're all being run by the same user)? A general solution would be best, but if not, what about the specific case where one is a child of the other?

Edit: How about via gdb?

10条回答
梦该遗忘
2楼-- · 2019-01-01 04:23

I could think of the rather contrived way to do that, and it will not work for arbitrary processes.

Suppose that you write your own shared library which implements 'char *getenv'. Then, you set up 'LD_PRELOAD' or 'LD_LIBRARY_PATH' env. vars so that both your processes are run with your shared library preloaded.

This way, you will essentially have a control over the code of the 'getenv' function. Then, you could do all sorts of nasty tricks. Your 'getenv' could consult external config file or SHM segment for alternate values of env vars. Or you could do regexp search/replace on the requested values. Or ...

I can't think of an easy way to do that for arbitrary running processes (even if you are root), short of rewriting dynamic linker (ld-linux.so).

查看更多
大哥的爱人
3楼-- · 2019-01-01 04:24

Or get your process to update a config file for the new process and then either:

  • perform a kill -HUP on the new process to reread the updated config file, or
  • have the process check the config file for updates every now and then. If changes are found, then reread the config file.

HTH.

cheers,

Rob

查看更多
步步皆殇っ
4楼-- · 2019-01-01 04:31

If your unix supports the /proc filesystem, then it's trivial to READ the env - you can read the environment, commandline, and many other attributes of any process you own that way. Changing it... Well, I can think of a way, but it's a BAD idea.

The more general case... I don't know, but I doubt there's a portable answer.

(Edited: my original answer assumed the OP wanted to READ the env, not change it)

查看更多
伤终究还是伤i
5楼-- · 2019-01-01 04:31

Not a direct answer but... Raymond Chen had a [Windows-based] rationale around this only the other day :-

... Although there are certainly unsupported ways of doing it or ways that work with the assistance of a debugger, there’s nothing that is supported for programmatic access to another process’s command line, at least nothing provided by the kernel. ...

That there isn’t is a consequence of the principle of not keeping track of information which you don’t need. The kernel has no need to obtain the command line of another process. It takes the command line passed to the CreateProcess function and copies it into the address space of the process being launched, in a location where the GetCommandLine function can retrieve it. Once the process can access its own command line, the kernel’s responsibilities are done.

Since the command line is copied into the process’s address space, the process might even write to the memory that holds the command line and modify it. If that happens, then the original command line is lost forever; the only known copy got overwritten.

In other words, any such kernel facilities would be

  • difficult to implement
  • potentially a security concern

However the most likely reason is simply that there's limited use cases for such a facility.

查看更多
登录 后发表回答