Is there a way to change the environment variables of another process in Unix?

后端 未结 11 2212
别跟我提以往
别跟我提以往 2020-11-22 15:09

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,

11条回答
  •  爱一瞬间的悲伤
    2020-11-22 15:57

    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).

提交回复
热议问题