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

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

    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.

提交回复
热议问题