Environ() returns multiple empty strings
Environ() returns a list of strings, but a lot of returned entries are the empty string instead of a valid environment variable in the form A=B.
This is really not enough detail to be actionable. The function is straight forward: https://github.com/prometheus/procfs/blob/54b2b569290aad467dfbb973dbb1ac619c8ae2aa/proc_environ.go#L23 - I doubt the issue is in the code but more likely in your env
Thanks for taking the time to respond.
I think the issue happens for processes which have a lot of NUL bytes in /proc/PID/environ. For example, I have the following Chrome process:
❯ sudo hexdump -C /proc/38905/environ
00000000 6d 61 67 65 73 20 2d 2d 63 68 61 6e 67 65 2d 73 |mages --change-s|
00000010 74 61 63 6b 2d 67 75 61 72 64 2d 6f 6e 2d 66 6f |tack-guard-on-fo|
00000020 72 6b 3d 65 6e 61 62 6c 65 20 2d 2d 6c 61 6e 67 |rk=enable --lang|
00000030 3d 65 6e 2d 55 53 20 2d 2d 6e 75 6d 2d 72 61 73 |=en-US --num-ras|
00000040 74 65 72 2d 74 68 72 65 61 64 73 3d 34 20 2d 2d |ter-threads=4 --|
00000050 65 6e 61 62 6c 65 2d 6d 61 69 6e 2d 66 72 61 6d |enable-main-fram|
00000060 65 2d 62 65 66 6f 72 65 2d 61 63 74 69 76 61 74 |e-before-activat|
00000070 69 6f 6e 20 2d 2d 72 65 6e 64 65 72 65 72 2d 63 |ion --renderer-c|
00000080 6c 69 65 6e 74 2d 69 64 3d 31 31 32 20 2d 2d 74 |lient-id=112 --t|
00000090 69 6d 65 2d 74 69 63 6b 73 2d 61 74 2d 75 6e 69 |ime-ticks-at-uni|
000000a0 78 2d 65 70 6f 63 68 3d 2d 31 37 33 30 32 38 31 |x-epoch=-1730281|
000000b0 33 39 39 37 34 31 31 38 36 20 2d 2d 6c 61 75 6e |399741186 --laun|
000000c0 63 68 2d 74 69 6d 65 2d 74 69 63 6b 73 3d 32 32 |ch-time-ticks=22|
000000d0 30 36 37 32 36 30 32 20 2d 2d 73 68 61 72 65 64 |0672602 --shared|
000000e0 2d 66 69 6c 65 73 3d 76 38 5f 63 6f 6e 74 65 78 |-files=v8_contex|
000000f0 74 5f 73 6e 61 70 73 68 6f 74 5f 64 61 74 61 3a |t_snapshot_data:|
00000100 31 30 30 20 2d 2d 66 69 65 6c 64 2d 74 72 69 61 |100 --field-tria|
00000110 6c 2d 68 61 6e 64 6c 65 3d 33 2c 69 2c 36 36 35 |l-handle=3,i,665|
00000120 32 34 35 38 35 30 35 36 38 34 30 37 32 30 34 33 |2458505684072043|
00000130 2c 35 35 37 35 39 37 36 35 34 35 39 31 32 36 37 |,557597654591267|
00000140 33 30 36 32 2c 32 36 32 31 34 34 20 2d 2d 76 61 |3062,262144 --va|
00000150 72 69 61 74 69 6f 6e 73 2d 73 65 65 64 2d 76 65 |riations-seed-ve|
00000160 72 73 69 6f 6e 3d 32 30 32 34 31 30 32 39 2d 30 |rsion=20241029-0|
00000170 35 30 30 35 39 2e 31 39 38 30 30 30 00 00 00 00 |50059.198000....|
00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000880 00 00 00 00 00 00 00 00 00 00 00 00 |............|
0000088c
As you can see, it contains a lot of NUL bytes at the end of the file.
This means that the Environ() function will return a lot of empty strings, as shown in this example: https://go.dev/play/p/tAxwKy6rGsZ
Are you open to a Pull Request that makes Environ skip the empty strings and only return non-empty strings?
Hrm wondering why chrome would do this? Not sure how to handle this. If env is defined to be NUL seperated strings, then technically returning an empty list is correct.
You are right, quoting from https://manpages.ubuntu.com/manpages/trusty/en/man5/proc.5.html
This file contains the environment for the process. The entries are separated by null bytes ('\0'), and there may be a null byte at the end. Thus, to print out the environment of process 1, you would do: $ strings /proc/1/environ
I found stackoverflow post that says processes can overwrite their environment strings, for example by writing zeroes to hide them.
From a practical standpoint, I don't think returning a bunch of empty strings is useful in any way.
In this case I think procfs is handling this properly. The caller can easily skip empty strings and there might be some cases where you actually want to know if there are empty/overwritten env vars.