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. Recover XDG_CURRENT_DESKTOP's value?
QtWS25 Last Chance

Recover XDG_CURRENT_DESKTOP's value?

Scheduled Pinned Locked Moved Unsolved General and Desktop
desktopenvironment varxdgnon-qtuser experience
8 Posts 3 Posters 610 Views
  • 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.
  • C Offline
    C Offline
    candy76041820
    wrote on last edited by candy76041820
    #1

    On linux (a weird private distro moified after Ubuntu):

    pkexec myprog
    

    gets myprog running as root, but loses environment variables.
    I made a org.myorg.myprog.policy under /usr/share/polkit-1/actions, specified allow_gui to true, and myprog gets DISPLAY and XAUTHORITY back.
    Question: myprog's GUI theme looks different from when it's not sudo-ed due to absence of XDG_CURRENT_DESKTOP variable. Can I recover it WITHOUT use of argv or some IPC trick?

    (Also, I don't want to

    pkexec env VAR=VALUE myprog
    

    because it's too ugly, and the password dialog would be showing the user's launching env instead of myprog.)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Since it's an environment variable you can use
      qEnvironmentVariable.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Since it's an environment variable you can use
        qEnvironmentVariable.

        C Offline
        C Offline
        candy76041820
        wrote on last edited by
        #3

        @SGaist No.

        jsulmJ 1 Reply Last reply
        0
        • C candy76041820

          @SGaist No.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @candy76041820 said in Recover XDG_CURRENT_DESKTOP's value?:

          No

          Why not?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          1
          • jsulmJ jsulm

            @candy76041820 said in Recover XDG_CURRENT_DESKTOP's value?:

            No

            Why not?

            C Offline
            C Offline
            candy76041820
            wrote on last edited by
            #5

            @jsulm Didn't I say that pkexec-ing myprog peels the environment variables off?

            candy@vbuntv:~/T$ more T.cpp
            #include <iostream>
            #include <string>
            
            int main(int argc, char **argv, char **envp){
                std::string keys[]={"DISPLAY=", "XAUTHORITY=", "XDG_CURRENT_DESKTOP="};
                while(envp && *envp){
                    for(const std::string &key: keys){
                        std::string var=*envp;
                        if(var.starts_with(key)){
                            std::cout<<var<<std::endl;
                            break;
                        }
                    }
                    envp++;
                }
                return 0;
            }
            
            candy@vbuntv:~/T$ g++ -std=c++20 T.cpp && ./a.out
            XDG_CURRENT_DESKTOP=ubuntu:GNOME
            DISPLAY=:10.0
            candy@vbuntv:~/T$ pkexec `pwd`/a.out
            #No output at all.
            
            candy@vbuntv:~/T$ env | grep -E "DISPLAY|XAUTHORITY|XDG_CURRENT_DESKTOP"
            XDG_CURRENT_DESKTOP=ubuntu:GNOME
            DISPLAY=:10.0
            candy@vbuntv:~/T$ pkexec env | grep -E "DISPLAY|XAUTHORITY|XDG_CURRENT_DESKTOP"
            #Nothing.
            
            1 Reply Last reply
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              If I'm not mistaken @SGaist wanted to suggest that your application changes/sets the env variable using https://doc.qt.io/qt-5/qtglobal.html#qputenv

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply
              0
              • jsulmJ jsulm

                If I'm not mistaken @SGaist wanted to suggest that your application changes/sets the env variable using https://doc.qt.io/qt-5/qtglobal.html#qputenv

                C Offline
                C Offline
                candy76041820
                wrote on last edited by
                #7

                @jsulm How can I set it back when I don't know its old value??

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Based on the pkexec documentation you can't do what you are trying. The environment is set to a minimal state that excludes the variables you are interested in.

                  If you want their values in your application, you will have to pass them as command line parameters.

                  However, based on the variable you are looking for, it seems you want to run a GUI as root, which is usually a bad idea.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

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