Skip to content

Commit 277e7bd

Browse files
authored
Merge pull request #24 from fasuizu-br/fix/issue-18-force-reboot-macos
Fix force_reboot() on macOS using Linux-only /proc paths
2 parents 3506bb2 + 663d759 commit 277e7bd

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/macos.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::fs::File;
2-
use std::io::{Error, ErrorKind, Write};
1+
use std::io::{Error, ErrorKind};
32
use std::process::Command;
43

54
use super::not_implemented;
@@ -53,14 +52,16 @@ pub fn reboot() -> ShutdownResult {
5352
invoke_script("tell application \"System Events\" to restart")
5453
}
5554

56-
/// Unix specific function to force reboot the machine using the magic SysRq key.
55+
/// macOS specific function to force reboot the system using `shutdown -r now`.
5756
pub fn force_reboot() -> ShutdownResult {
58-
// Reference: https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html
59-
let mut file = File::create("/proc/sys/kernel/sysrq")?;
60-
file.write_all(b"128")?;
61-
file = File::create("/proc/sysrq-trigger")?;
62-
file.write_all(b"b")?;
63-
Ok(())
57+
let output = Command::new("shutdown").args(["-r", "now"]).output()?;
58+
if output.status.success() {
59+
return Ok(());
60+
}
61+
Err(Error::new(
62+
ErrorKind::Other,
63+
String::from_utf8_lossy(&output.stderr).into_owned(),
64+
))
6465
}
6566

6667
/// macOS specific function to logout with a confirmation dialog using AppleScript and "System Events" call "log out".

0 commit comments

Comments
 (0)