How to correctly set/get environment variables
-
Hi all. For my project I have two applications running: a "system app" and a "user app". To do its job, the system app must to know what user app is currently running. I thought about setting up 2 environment variables, named
CURR_APP_NAME
andCURR_APP_PID
, defined in/etc/profile
. Now, the desired is to set them at runtime from the user app, and get their value from the system app.# /etc/profile export CURR_APP_NAME="" export CURR_APP_PID=""
# user app - main.cpp QGuiApplication app(argc, argv); qputenv("CURR_APP_NAME", QByteArray(app.applicationName().toUtf8())); qputenv("CURR_APP_PID", QByteArray(app.applicationPid().toUtf8()));
# system app - main.cpp [... when a certain signal is triggered ] QString name = qgetenv("CURR_APP_NAME"); QString pid = qgetenv("CURR_APP_PID");
The problem is that the value of my environment variables is not set "globally", the system app always read an empty value.
What am I doing wrong? -
Environment is separate for each user. So when you set the env var in your user app, it does not set that value for root. This is not a bug or anything - rather a (among others) security feature.
You can save your PID to a file, then system app will be able to read that file from the same path.
Or use some proper IPC communication techniques, like DBus, QtRemoteObjects, QLocalSocket etc.