From f3d7b013be7f5ae948045cdee6accd7a64e8ae4c Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Mon, 11 Jul 2016 19:01:15 +0300 Subject: [PATCH 01/10] Add the initial implementation of reboot() --- src/sys/mod.rs | 3 +++ src/sys/reboot.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/sys/reboot.rs diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 82934164b9..b59ca9b14c 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -31,6 +31,9 @@ pub mod stat; #[cfg(any(target_os = "linux", target_os = "android"))] pub mod syscall; +#[cfg(any(target_os = "linux", target_os = "android"))] +pub mod reboot; + #[cfg(not(target_os = "ios"))] pub mod termios; diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs new file mode 100644 index 0000000000..d537401f6f --- /dev/null +++ b/src/sys/reboot.rs @@ -0,0 +1,36 @@ +use {Errno, Error, Result}; +use libc::c_int; +use void::Void; +use std::mem::drop; + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum RebootMode { + Halt = 0xcdef0123, + kexec = 0x45584543, + PowerOff = 0x4321fedc, + Restart = 0x1234567, + // we do not support Restart2, + Suspend = 0xd000fce1, +} + +pub fn reboot(how: RebootMode) -> Result { + unsafe { + ext::reboot(how as c_int) + }; + Err(Error::Sys(Errno::last())) +} + +#[allow(overflowing_literals)] +pub fn set_cad_enabled(enable: bool) -> Result<()> { + let res = unsafe { + ext::reboot(if enable { 0x89abcdef } else { 0 }) + }; + Errno::result(res).map(drop) +} + +mod ext { + use libc::c_int; + extern { + pub fn reboot(cmd: c_int) -> c_int; + } +} From 91b29abf648a53a16280c7c6d7547f30b8268a87 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Mon, 11 Jul 2016 20:37:23 +0300 Subject: [PATCH 02/10] Add some basic docs for reboot::set_cad_enabled() --- src/sys/reboot.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index d537401f6f..eedbf5ae0a 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -20,6 +20,10 @@ pub fn reboot(how: RebootMode) -> Result { Err(Error::Sys(Errno::last())) } + +/// Enable or disable the reboot keystroke (Ctrl-Alt-Delete). +/// +/// Corresponds to calling `reboot(RB_ENABLE_CAD)` or `reboot(RB_DISABLE_CAD)` in C. #[allow(overflowing_literals)] pub fn set_cad_enabled(enable: bool) -> Result<()> { let res = unsafe { From c63f89cb47405329260c36ec905e7cfa92d239a0 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Mon, 11 Jul 2016 21:28:06 +0300 Subject: [PATCH 03/10] Ignore the overflow for constants Force using the constants even on x86 where they do not fit into isize (c_int) --- src/sys/reboot.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index eedbf5ae0a..10abef91ab 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -3,6 +3,7 @@ use libc::c_int; use void::Void; use std::mem::drop; +#[allow(overflowing_literals)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum RebootMode { Halt = 0xcdef0123, From bf796cce6e73f6e5518c76e013ded7518ff14a61 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Wed, 13 Jul 2016 10:26:31 +0300 Subject: [PATCH 04/10] Stop targeting Android --- src/sys/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sys/mod.rs b/src/sys/mod.rs index b59ca9b14c..793bc70e05 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -31,7 +31,7 @@ pub mod stat; #[cfg(any(target_os = "linux", target_os = "android"))] pub mod syscall; -#[cfg(any(target_os = "linux", target_os = "android"))] +#[cfg(any(target_os = "linux"))] pub mod reboot; #[cfg(not(target_os = "ios"))] From b19afe56ab55cc2aa3d7e7937d33e4525d860e29 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Wed, 13 Jul 2016 10:38:59 +0300 Subject: [PATCH 05/10] Use libc's declarations --- src/sys/reboot.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index 10abef91ab..82a31380f8 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -1,41 +1,37 @@ use {Errno, Error, Result}; -use libc::c_int; +use libc; use void::Void; use std::mem::drop; -#[allow(overflowing_literals)] +#[repr(i32)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum RebootMode { - Halt = 0xcdef0123, - kexec = 0x45584543, - PowerOff = 0x4321fedc, - Restart = 0x1234567, + Halt = libc::RB_HALT_SYSTEM, + kexec = libc::RB_KEXEC, + PowerOff = libc::RB_POWER_OFF, + Restart = libc::RB_AUTOBOOT, // we do not support Restart2, - Suspend = 0xd000fce1, + Suspend = libc::RB_SW_SUSPEND, } pub fn reboot(how: RebootMode) -> Result { unsafe { - ext::reboot(how as c_int) + libc::reboot(how as libc::c_int) }; Err(Error::Sys(Errno::last())) } - /// Enable or disable the reboot keystroke (Ctrl-Alt-Delete). /// /// Corresponds to calling `reboot(RB_ENABLE_CAD)` or `reboot(RB_DISABLE_CAD)` in C. -#[allow(overflowing_literals)] pub fn set_cad_enabled(enable: bool) -> Result<()> { + let cmd = if enable { + libc::RB_ENABLE_CAD + } else { + libc::RB_DISABLE_CAD + }; let res = unsafe { - ext::reboot(if enable { 0x89abcdef } else { 0 }) + libc::reboot(cmd) }; Errno::result(res).map(drop) } - -mod ext { - use libc::c_int; - extern { - pub fn reboot(cmd: c_int) -> c_int; - } -} From 012c6623c9c50aca91b81e6d30800613f515f898 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Wed, 13 Jul 2016 21:06:39 +0300 Subject: [PATCH 06/10] Manually match on RebootMode::* --- src/sys/reboot.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index 82a31380f8..45f9ec9862 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -3,20 +3,27 @@ use libc; use void::Void; use std::mem::drop; -#[repr(i32)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum RebootMode { - Halt = libc::RB_HALT_SYSTEM, - kexec = libc::RB_KEXEC, - PowerOff = libc::RB_POWER_OFF, - Restart = libc::RB_AUTOBOOT, + Halt, + kexec, + PowerOff, + Restart, // we do not support Restart2, - Suspend = libc::RB_SW_SUSPEND, + Suspend, } pub fn reboot(how: RebootMode) -> Result { + let cmd = match how { + RebootMode::Halt => libc::RB_HALT_SYSTEM, + RebootMode::kexec => libc::RB_KEXEC, + RebootMode::PowerOff => libc::RB_POWER_OFF, + RebootMode::Restart => libc::RB_AUTOBOOT, + // we do not support Restart2, + RebootMode::Suspend => libc::RB_SW_SUSPEND, + }; unsafe { - libc::reboot(how as libc::c_int) + libc::reboot(cmd) }; Err(Error::Sys(Errno::last())) } From cce6bce7fc85d8c44a101ae164a4604ab5adf6d4 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Thu, 14 Jul 2016 00:40:17 +0300 Subject: [PATCH 07/10] Switch Clone and Copy --- src/sys/reboot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index 45f9ec9862..0f9d5e8847 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -3,7 +3,7 @@ use libc; use void::Void; use std::mem::drop; -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum RebootMode { Halt, kexec, From f682458c829e6d715cabd9e8ef6730ff05567966 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Thu, 14 Jul 2016 00:51:52 +0300 Subject: [PATCH 08/10] Add some documentation --- src/sys/reboot.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index 0f9d5e8847..81502d01ab 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -1,8 +1,14 @@ +//! Reboot/shutdown or enable/disable Ctrl-Alt-Delete. + use {Errno, Error, Result}; use libc; use void::Void; use std::mem::drop; +/// How exactly should the system be rebooted. +/// +/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for +/// enabling/disabling Ctrl-Alt-Delete. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum RebootMode { Halt, From 8f6b162fb189d092afc7e6a6162e00d5d5332f4a Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Thu, 14 Jul 2016 14:54:21 +0300 Subject: [PATCH 09/10] Revert "Manually match on RebootMode::*" This reverts commit 012c6623c9c50aca91b81e6d30800613f515f898. --- src/sys/reboot.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index 81502d01ab..d593ee78fb 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -9,27 +9,20 @@ use std::mem::drop; /// /// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for /// enabling/disabling Ctrl-Alt-Delete. +#[repr(i32)] #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum RebootMode { - Halt, - kexec, - PowerOff, - Restart, + Halt = libc::RB_HALT_SYSTEM, + kexec = libc::RB_KEXEC, + PowerOff = libc::RB_POWER_OFF, + Restart = libc::RB_AUTOBOOT, // we do not support Restart2, - Suspend, + Suspend = libc::RB_SW_SUSPEND, } pub fn reboot(how: RebootMode) -> Result { - let cmd = match how { - RebootMode::Halt => libc::RB_HALT_SYSTEM, - RebootMode::kexec => libc::RB_KEXEC, - RebootMode::PowerOff => libc::RB_POWER_OFF, - RebootMode::Restart => libc::RB_AUTOBOOT, - // we do not support Restart2, - RebootMode::Suspend => libc::RB_SW_SUSPEND, - }; unsafe { - libc::reboot(cmd) + libc::reboot(how as libc::c_int) }; Err(Error::Sys(Errno::last())) } From 215f3872b1ead677e5f942f86e334db5f01a4eb3 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Thu, 14 Jul 2016 14:56:21 +0300 Subject: [PATCH 10/10] Name enum variants after the libc constants --- src/sys/reboot.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index d593ee78fb..94f30f6217 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -12,12 +12,12 @@ use std::mem::drop; #[repr(i32)] #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum RebootMode { - Halt = libc::RB_HALT_SYSTEM, - kexec = libc::RB_KEXEC, - PowerOff = libc::RB_POWER_OFF, - Restart = libc::RB_AUTOBOOT, + RB_HALT_SYSTEM = libc::RB_HALT_SYSTEM, + RB_KEXEC = libc::RB_KEXEC, + RB_POWER_OFF = libc::RB_POWER_OFF, + RB_AUTOBOOT = libc::RB_AUTOBOOT, // we do not support Restart2, - Suspend = libc::RB_SW_SUSPEND, + RB_SW_SUSPEND = libc::RB_SW_SUSPEND, } pub fn reboot(how: RebootMode) -> Result {