Skip to content

Commit

Permalink
Use libc's declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
bugaevc committed Jul 13, 2016
1 parent bf796cc commit b19afe5
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/sys/reboot.rs
Original file line number Diff line number Diff line change
@@ -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<Void> {
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;
}
}

0 comments on commit b19afe5

Please sign in to comment.