Sample Header Ad - 728x90

Get the unresolved pwd of a shell from another process

1 vote
1 answer
210 views
I'm [hitting an issue](https://github.com/microsoft/vscode/issues/129023) where I need to get the unresolved symlink of a shell process. For example given a symlink ~/link -> ~/actual, if bash is launched with a $PWD of ~/link, I need to fetch that from outside the bash process. Getting the _resolved_ cwd is possible using lsof or /proc as called out in https://unix.stackexchange.com/a/94359/115410 but I'm beginning to think it's not possible to get the _unresolved_ path. I have tried to use lsof -b to not use readlink but the logging says that path never tries to use readlink anyway. It does appear possible to read the environment via /proc/.../environ and parse out PWD but this is slow, /proc may not exist on the system and I believe there are some security implications to trying to read a processes' environment. Here is the code in question I'm trying to fix: [lsof on macOS](https://github.com/microsoft/vscode/blob/723ce3f4be196696c195f6c05511b853b05c9e84/src/vs/platform/terminal/node/terminalProcess.ts#L489) :
lang-ts
exec('lsof -OPln -p ' + this._ptyProcess.pid + ' | grep cwd', (error, stdout, stderr) => {
  ...
});
[/proc on Linux](https://github.com/microsoft/vscode/blob/723ce3f4be196696c195f6c05511b853b05c9e84/src/vs/platform/terminal/node/terminalProcess.ts#L507) :
lang-ts
Promises.readlink(/proc/${this._ptyProcess.pid}/cwd);
Asked by Daniel Imms (113 rep)
Jul 22, 2021, 12:26 PM
Last activity: Jul 22, 2021, 06:50 PM