Bug 217144

Summary: procstat -e fails to report changes to environment.
Product: Base System Reporter: mrT <mrT1188123>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed Works As Intended    
Severity: Affects Many People CC: amd64, kib
Priority: ---    
Version: 10.3-RELEASE   
Hardware: amd64   
OS: Any   

Description mrT 2017-02-16 15:37:18 UTC
procstat -e (PID) (for printing the environment variables of specified PID) 
fails to show updated environments.

This was noticed in the following scenario:
konsole-A  --- 
   procstat -e pidOf-konsole-B  > baseline.txt

konsole-B ---
   export MYTEST="ThisIsMy test string. mrT"

konsole-A ---
   procstat -e pidOf-konsole-B  > test1.txt

---> Both outputs was identical.
     Therefore, any environment changes are NOT reflected in 'procstat -e' output.
     Got same result even when I ran the same 'procstat -e' on konsole-B.
     Note: other options of procstat, such as '-f' and '-r' (file descriptor, resource usage)
           did sow updated info as expected.

konsole version 2.14.2
Using KDE Development Platform 4.14.10
kern.osrelease: 10.3-RELEASE-p5
kern.osrevision: 199506
kern.version: FreeBSD 10.3-RELEASE-p5 #0: Thu Jun 30 03:52:15 UTC 2016
    root@amd64-builder.pcbsd.org:/usr/obj/usr/src/sys/GENERIC
Comment 1 mrT 2017-02-16 15:45:53 UTC
I was using shell: ksh93 version sh (AT&T Research) 93u+ 2012-08-01
and also tested using '/bin/sh' and got the same results.
Comment 2 Konstantin Belousov freebsd_committer freebsd_triage 2017-02-16 23:15:48 UTC
This is perfectly normal.  Kernel can reliably see the environment for a process only during execve(2), when old program passes the environment to a new executed program through the syscall.  New program (address space) gets the envirnment as a set of strings on top of the main thread stack.  procstat -e best guess is to access these strings and show them as good enough approximation.

During the normal operations, the environment changes do not need to be reflected into the strings and they are not, as you discovered.  Still procstat -e is useful because typical program only consumes the environment without changing it.  Shells of course do change env vars, but maintaining env as externally visible strings set is not needed until something is execed.