0

I have a python program that's run with pkexec and I guess because of that I'm having a really hard time getting os.environ.get('XDG_CURRENT_DESKTOP') or os.environ.get('DESKTOP_SESSION') to output anything. One of the program's function is to get the Linux Desktop environment, and that's mainly what I'm trying to accomplish right now. I've decided to use os.setupid('my_username') getting it from pwd to switch to my user and try to get the environment variables since would no longer be root but the problem is I can not go back and run the script as root for other functions. How can I go back to being root after all that?

In order to get the environment variables I'm trying this:

def getDesktopEnvironment(self):
    os.seteuid(self.uidChange)
    desktops = subprocess.Popen(['bash', 'desktopenv.sh'],  stdout=subprocess.PIPE)
    desktops.wait()
    a = desktops.stdout.read()
    print a
    if a == "X-Cinnamon":
       #do this
    elif a == "Unity":
        #do that

The bash script is below

#!/bin/bash

echo $XDG_CURRENT_DESKTOP

trying to go back to root get me this: os.seteuid(0)OSError: [Errno 1] Operation not permitted

1
  • Use os.fork to start a child process, and in that process call os.setuid. Use a pipe to pass any information back to the parent. See, e.g., stackoverflow.com/questions/218935/… Commented Mar 23, 2017 at 1:23

1 Answer 1

1

I would recommend the following change:

Modify the code that sets the UID for the user that you want to the username corresponding to the UID that you want.

Modify your getDesktopEnvironment to look like the code below. NOTE: The script path doesn't have to be in the users home directory it just has to bereadable by the user specified by username.

def getDesktopEnvironment(self):
    # You can set the script_path can be located anywhere you want.
    # as long as the user you want to invoke the script has permission
    # to read the file.
    script_path = os.path.join('~', self.username, 'desktopenv.sh')
    args = args = ['sudo', '--login', '-u', self.username, '/bin/bash', script_path]
    desktops = subprocess.Popen(args,  stdout=subprocess.PIPE)
    desktops.wait()
    a = desktops.stdout.read()
    print a
    if a == "X-Cinnamon":
       #do this
    elif a == "Unity":
        #do that

Just because you change the UID of the current process doesn't mean that you inherit the environment associated with that UID. Using sudo with the -i option ensures that the user's startup scripts are run and ensures that all relevant environment variables are set.

Using sudo also ensures that you can continue the running the rest of your application as root.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.