1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Luis Liu. All rights reserved.
 *  Licensed under the MIT License. See License in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

use crate::Command;
use anyhow::Result;
use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::{Command as StdCommand, Output};
use std::str::FromStr;

impl Command {
    pub fn is_elevated() -> bool {
        let uid = unsafe { 
            libc::getuid()
        };
        if uid == 0 { 
            true 
        } else { 
            false 
        }
    }

    pub fn output(&self) -> Result<Output> {
        let pkexec = PathBuf::from_str("/bin/pkexec")?;
        let mut command = StdCommand::new(pkexec);
        let display = env::var("DISPLAY");
        let xauthority = env::var("XAUTHORITY");
        let home = env::var("HOME");

        command.arg("--disable-internal-agent");
        if display.is_ok() || xauthority.is_ok() || home.is_ok() {
            command.arg("env");
            if let Ok(display) = display {
                command.arg(format!("DISPLAY={}", display));
            }
            if let Ok(xauthority) = xauthority {
                command.arg(format!("XAUTHORITY={}", xauthority));
            }
            if let Ok(home) = home {
                command.arg(format!("HOME={}", home));
            }
        }
        command.arg(self.cmd.get_program());
        let args: Vec<&OsStr> = self.cmd.get_args().collect();
        if !args.is_empty() {
            command.args(args);
        }

        let output = command.output()?;
        Ok(output)
    }
}