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

后端 未结 11 2069
别跟我提以往
别跟我提以往 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:48

    You probably can do it technically (see other answers), but it might not help you.

    Most programs will expect that env vars cannot be changed from the outside after startup, hence most will probably just read the vars they are interested in at startup and initialize based on that. So changing them afterwards will not make a difference, since the program will never re-read them.

    If you posted this as a concrete problem, you should probably take a different approach. If it was just out of curiosity: Nice question :-).

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-11-22 15:57

    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)

    0 讨论(0)
  • 2020-11-22 15:59

    UNIX is full of Inter-process communication. Check if your target instance has some. Dbus is becoming a standard in "desktop" IPC.

    I change environment variables inside of Awesome window manager using awesome-client with is a Dbus "sender" of lua code.

    0 讨论(0)
  • 2020-11-22 16:01

    Substantially, no. If you had sufficient privileges (root, or thereabouts) and poked around /dev/kmem (kernel memory), and you made changes to the process's environment, and if the process actually re-referenced the environment variable afterwards (that is, the process had not already taken a copy of the env var and was not using just that copy), then maybe, if you were lucky and clever, and the wind was blowing in the right direction, and the phase of the moon was correct, perhaps, you might achieve something.

    0 讨论(0)
提交回复
热议问题