DFHack / dfhack

Memory hacking library for Dwarf Fortress and a set of tools that use it
Other
1.84k stars 462 forks source link

Use OS-specific methods to read commandline for --disable-dfhack #3802

Open myk002 opened 9 months ago

myk002 commented 9 months ago

so missing globals or misaligned structures won't immediately crash the game

Use GetCommandLineA on Windows https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinea

on unix, scan the output of ps -p <pid> -o command=

lethosor commented 7 months ago

I do not recommend scanning ps output because that won't handle whitespace in arguments properly.

quietust commented 7 months ago

On Linux, we'd probably be better off reading /proc/<pid>/cmdline.

myk002 commented 6 months ago

proc works for Linux. I was hoping to have a single solution for all of Unix, and scanning ps output is what was recommended by various internet advice posts, e.g. https://stackoverflow.com/questions/821837/how-to-get-the-command-line-args-passed-to-a-running-process-on-unix-linux-syste#:~:text=For%20Linux%20%26%20Unix%20System%20you,get%20the%20full%20command%20line.

ab9rf commented 6 months ago

also a reminder that on all remotely recent versions of linux /proc/self refers to the running process, so you don't have to do sprintf(buf, "/proc/%d/cmdline", getpid()) if you don't want to (and you absolutely shouldn't want to), you can just grab /proc/self/cmdline

i found a suggestion on the web of using _NSGetArgc and _NSGetArgv for this purpose in OSX; the source where i found this says that these are undocumented so ymmv

scanning ps output is the only solution known for solaris or freebsd (and on solaris at least it's imperfect, and to get the true command line you have to be super-user), but these platforms aren't relevant to our use case so i wouldn't worry about it