Environment variables set with setenv() aren't shown in /proc/<PID>/environ

481 Views Asked by At

The app with setenv() C function sets values of three environment variables. By the app behavior it is clear that env. vars are set properly. They configure paths for OpenSSL config file, config files and modules folder.

Below is a minimum example that illustrates the issue.

// setenv_example.c
#include <stdlib.h>
#include <stdio.h>

int main() {
    setenv("my_var", "my_value", 1);
    printf("Press any key to exit");
    getchar();
    return 0;
}

In one terminal window ($ is command prompt):

$ gcc setenv_example.c -o setenv_example
$ ./setenv_example
Press any key to exit

In another terminal window

$ ps -a | grep setenv
164615 pts/5    00:00:00 setenv_example
$ cat /proc/164615/environ | tr '\0' '\n' | grep my
<empty output>

How does one see environment variables set with setenv() of the running process on Linux (Debian 11)?

1

There are 1 best solutions below

0
KamilCuk On

How does one see environment variables set with setenv() of the running process

It is not (that easily) possible.

/proc/<pid>/environ contains initial program environment, as passed when invoked exec.

If your program is compiled with debugging symbols, you can attach a debugger to your program and read the global environ variable.