Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to correctly set/get environment variables
Forum Updated to NodeBB v4.3 + New Features

How to correctly set/get environment variables

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 14.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robcont_
    wrote on last edited by
    #1

    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 and CURR_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?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      R 1 Reply Last reply
      3
      • sierdzioS sierdzio

        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.

        R Offline
        R Offline
        robcont_
        wrote on last edited by
        #3

        @sierdzio
        Thanks for your help. I think I'll use QDbus to do my job.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved