OS X: Attach to a running process and read its environment variables - macos

Is it possible (and if so, how?) to read a running process's environment variables. To be more specific, I am concerned with environment variables set by the process itself (using setenv() or similar), not the general user session environment variables set when the process is launched.
I figured that DTrace would be the tool for this job, but the only solution I've found online (from this mailing list thread) appears to be specific to Solaris because the script fails to execute on my Mac.
Perhaps Instruments exposes this functionality somehow and I just haven't found it yet?
I have also tried Tasks Explorer, which is a nice little application, and it shows process info including environment variables, but it does not show any environment variables set by the process itself, which is what I'm after.
Any help would be much appreciated!

I suppose you could try next way:
Inject you dylib into running process.
To read environment variables from injected dylib.
Via IPC (RPC, Shared Memory, etc.) send Environment variable into you application.
By the way, I have a plan to add this type of environment variables exploring into my Tasks Explorer.
Next links will help you with injection:
https://github.com/comex/inject_and_interpose
SIMBL/Bundle/dylib injection into Dock.app

Related

How to make PowerShell recognize changes in environment variables?

When I change any environment variable in Windows using GUI (System Properties/Environment Variables/System Variables) changes are reflected immediately in cmd terminal, but PowerShell shows an old value.
Here cmd recognized that I changed JAVA_HOME value from C:\Users\nikif\.jdks\openjdk-19.0.1 to C:\Users\nikif\.jdks\corretto-16.0.2
PowerShell still shows the previous value - C:\Users\nikif\.jdks\openjdk-19.0.1
Is there any command that will make PowerShell recognize the environment variable change made in GUI?
Or maybe there is a better way to change the environment variable from PowerShell?
Thank you.
This all should really be a comment (I tried above), but attempting to explain why the question, as it stands, is too vage requires just more explaination.
Processes get their environment when started by the OS. They basically get a copy the environment as it was at that point in time. When you start a process you can (using the respective APIs) pass additional or altered environment variables.
During the existance (runtime) of a process you cannot change the environment.
Shells are a common exception, as they provide specific syntax that does that (set NAME=VALUE in CMD.EXE and $env:NAME="VALUE" in PowerShell, for example). An application itself could also do such stuff from its code using respective APIs. But in general, there is no way to change variables from the outside.
One process will never see the changes done to the environment in a different process. For example, if you have to separate CMD.EXE sessions running and change a variable in one of them, the other will not know. The environment is private to each process.
A potential exception is the (global/system) environment variables you can set using the Computer/Properties/Environment setting (applet). The system will send a WM_SETTINGCHANGE window message indicating the that environment has changed. Applications can register to this message and act accordingly.
However, neither PowerShell nor CMD.EXE do seem to listen to this message (it would require a (hidden) Window anyway and both are console applications). And frankly, it would be not good if they did. Consider CMD.EXE just execuing a batch file and a variable (say PATH) changes underneath - security issues and general havoc all over.

change environment variable on a running process in mac

I have a running process on mac and I want to change is environment variable from out side the process, using some command line utility.
How can I do that?
You cannot change the environment variables of a running process via an external utility. Doing so would require the utility to modify the address space of the process. Note that this is not a limitation of macOS. This is a limitation of the UNIX process model. When a UNIX process is created by the kernel the environment variables are put in its address space; typically near the top of the stack. They are not stored in the kernel data structures for the process. And thus there are no system calls for getting or setting those vars. Which means there is no way for one process to affect the env vars of a second process other than at the time the second process is created via execve() or a related syscall.

Electron get or set Environment variables in Windows?

I know that from JavaScript it is not possible to get or set Windows Environment Variables. I have deployed a Electron app as a executable (as suggested here), is it possible now to get or modify a Windows Enviroment Variable. If it is can someone point me in the right direction ?
You can use NodeJs to read the enviroment variables via process.env you can read all methods and possabilities on the process documentation.
Example:
console.log(process.env.PATH)
Writing environment variables in a running process is always temporary. You can set environment variables by calling/spawning system commands/tools like setx under windows.
Persisting them does not change the running environment in the process for that you have to restart the process. So you need both, set the variable as described above and in addition persist it with system tools/commands.

Ruby: Setting environment variables using system()

I am creating the following method which sets my environment variables:
def set_time
puts "setting_time"
system("export GIT_COMMITTER_DATE='#{#date}'")
system("export GIT_AUTHOR_DATE='#{#date}'")
end
But for some reason when I go into my console after running this, the environment variables have not been added! I am able to run other command line interface keywords and it works. How can I set environment variables from Ruby?
The reason they don't 'stick' is because when you run a shell command from Ruby it opens a new process. And that process, as a child to your current Ruby process is not persistent, as eventually it dies.
A good resource on this is Jesse Storimer's blog post, with much more information about environment and processes than I will type here.
Depending on your operating system, you can use Ruby to write to your 'rc' files, such as ~/.bashrc or on Windows change your registry, if you really want to have these be persistent over logins.
So the answer is, you ARE setting and exporting environment variables. They simply aren't surviving past the life of the child process.

Is there a way we can check the Environment variables of a live process?

I have a production process that i would like to check its environment variables. Is there a way to check this from a debugger (ollydbg or windbg) or another tool?
Thx
Process Explorer displays the per process environment in one of its property tabs

Resources