Search code examples
qnxqnx-neutrino

How to read environment variables value from /proc file system in QNX?


I want to read an environment variable value of a running process from /proc file system. I could not find /proc/<pid>/environ file on QNX. I know how we can read it from getenv API but I want to read it from /proc/<pid>.


Solution

  • To the best of my knowledge there's no easy-to-use API on QNX for accessing another program's environment programmatically.

    If your application permits, the easiest solution is to use QNX's pidin -- run pidin -p <pid> environment to dump the environment to the standard output.

    If you cannot spawn additional processes, what I believe pidin is doing internally is something like:

    • Use DCMD_PROC_INFO to retrive the virtual address of the initial stack of the target process;
    • Open the target processes' address space via /proc/<pid>/as, seek to the inital stack address, and parse through it:
      • First word is argc
      • Next is argv, so skip past that many char*s, plus one
      • Next is an array of char*s to the environment variables, terminated by a null; you can seek to each address and read the contents.