-
Notifications
You must be signed in to change notification settings - Fork 6
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
Labels
enhancement
New feature or request
Comments
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")
} |
Ref -> #5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be nice to add a new API:
[force_]hibernate()
.The text was updated successfully, but these errors were encountered: