Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API: [force_]hibernate() #3

Open
silvioprog opened this issue Dec 19, 2019 · 2 comments
Open

New API: [force_]hibernate() #3

silvioprog opened this issue Dec 19, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@silvioprog
Copy link
Member

It would be nice to add a new API: [force_]hibernate().

@silvioprog silvioprog added the enhancement New feature or request label Dec 19, 2019
@wineee
Copy link

wineee commented Feb 26, 2022

I tried to implement it

fn get_sessionid() -> Result<String, Box<dyn std::error::Error>> {
    let file = File::open("/proc/self/sessionid")?;
    let mut buffered = BufReader::new(file);
    let mut sessionid = String::new();
    buffered.read_line(&mut sessionid)?;
    Ok(sessionid)
}

fn dbus_send(singnal: &str, context: &str) -> ShutdownResult {
    let mut cmd = Command::new("dbus-send");
    cmd.arg("--system")
       .arg("--print-reply")
       .arg("--dest=org.freedesktop.login1")
       .arg("/org/freedesktop/login1")
       .arg("org.freedesktop.login1.Manager.".to_owned() + singnal)
       .arg(context);
    match cmd.output() {
        Ok(output) => {
            if output.status.success() && output.stderr.is_empty() {
                return Ok(());
            }
            Err(Error::new(
                ErrorKind::Other,
                String::from_utf8(output.stderr).unwrap(),
            ))
        }
        Err(error) => Err(error),
    }
}

pub fn logout() -> ShutdownResult {
    let sessionid = get_sessionid().unwrap();
    let mut cmd = Command::new("loginctl");
    cmd.arg("terminate-session").arg(sessionid);
    match cmd.output() {
        Ok(output) => {
            if output.status.success() && output.stderr.is_empty() {
                return Ok(());
            }
            Err(Error::new(
                ErrorKind::Other,
                String::from_utf8(output.stderr).unwrap(),
            ))
        }
        Err(error) => Err(error),
    }
}

pub fn hibernate() -> ShutdownResult {
    dbus_send("Hibernate", "boolean:true")
}

pub fn suspend() -> ShutdownResult {
    dbus_send("Suspend", "boolean:true")
}

pub fn lock() -> ShutdownResult {
    let sessionid = get_sessionid().unwrap();
    let mut context = String::from("string:");
    context.push_str(&sessionid);
    dbus_send("LockSession", context.as_str())
}

pub fn shutdown() -> ShutdownResult {
    dbus_send("PowerOff", "boolean:true")
}

pub fn reboot() -> ShutdownResult {
    dbus_send("Reboot", "boolean:true")
}

@silvioprog
Copy link
Member Author

Ref -> #5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants