|
7 | 7 | #![allow(unsafe_code)] |
8 | 8 |
|
9 | 9 | use crate::backend; |
| 10 | +#[cfg(target_os = "linux")] |
| 11 | +use crate::backend::c; |
10 | 12 | use crate::ffi::CStr; |
11 | 13 | #[cfg(not(any(target_os = "espidf", target_os = "emscripten")))] |
12 | 14 | use crate::io; |
@@ -154,3 +156,59 @@ pub fn sysinfo() -> Sysinfo { |
154 | 156 | pub fn sethostname(name: &[u8]) -> io::Result<()> { |
155 | 157 | backend::system::syscalls::sethostname(name) |
156 | 158 | } |
| 159 | + |
| 160 | +/// Reboot command to be used with [`reboot`]. |
| 161 | +/// |
| 162 | +/// See [`reboot`] documentation for more info |
| 163 | +/// |
| 164 | +/// [`reboot`]: crate::system::reboot |
| 165 | +#[cfg(target_os = "linux")] |
| 166 | +#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 167 | +#[repr(i32)] |
| 168 | +#[non_exhaustive] |
| 169 | +pub enum RebootCommand { |
| 170 | + /// CAD is disabled. |
| 171 | + /// This means that the CAD keystroke will cause a SIGINT signal to be sent to init (process 1), |
| 172 | + /// whereupon this process may decide upon a proper action (maybe: kill all processes, sync, reboot). |
| 173 | + /// Disables the Ctrl-Alt-Del keystroke. |
| 174 | + /// |
| 175 | + /// When disabled, the keystroke will send a SIGINT signal to pid 1 |
| 176 | + CadOff = c::LINUX_REBOOT_CMD_CAD_OFF, |
| 177 | + /// Enables the Ctrl-Alt-Del keystroke. |
| 178 | + /// |
| 179 | + /// When enabled, the keystroke will trigger LINUX_REBOOT_CMD_RESTART |
| 180 | + CadOn = c::LINUX_REBOOT_CMD_CAD_ON, |
| 181 | + /// Prints the message "System halted" and halts the system |
| 182 | + Halt = c::LINUX_REBOOT_CMD_HALT, |
| 183 | + /// Execute a kernel that has been loaded earlier with [`kexec_load`]. |
| 184 | + /// |
| 185 | + /// [`kexec_load`]: https://man7.org/linux/man-pages/man2/kexec_load.2.html |
| 186 | + Kexec = c::LINUX_REBOOT_CMD_KEXEC, |
| 187 | + /// Prints the message "Power down.", stops the system and tries to remove all power |
| 188 | + PowerOff = c::LINUX_REBOOT_CMD_POWER_OFF, |
| 189 | + /// Prints the message "Restarting system." and triggers a restart |
| 190 | + Restart = c::LINUX_REBOOT_CMD_RESTART, |
| 191 | + /// Hibernate the system by suspending to disk |
| 192 | + SwSuspend = c::LINUX_REBOOT_CMD_SW_SUSPEND, |
| 193 | +} |
| 194 | + |
| 195 | +/// `reboot`—Reboot or enable/disable Ctrl-Alt-Del |
| 196 | +/// |
| 197 | +/// The reboot syscall, despite the name, can actually do much more than reboot. |
| 198 | +/// |
| 199 | +/// Among other things it can |
| 200 | +/// - Restart, Halt, Power Off and Suspend the system |
| 201 | +/// - Enable and disable the Ctrl-Alt-Del keystroke |
| 202 | +/// - Execute other kernels |
| 203 | +/// - Terminate init inside PID namespaces |
| 204 | +/// |
| 205 | +/// It is highly reccomended to carefully read the kernel documentation before calling this function. |
| 206 | +/// |
| 207 | +/// # References |
| 208 | +/// - [Linux] |
| 209 | +/// |
| 210 | +/// [Linux]: https://man7.org/linux/man-pages/man2/reboot.2.html |
| 211 | +#[cfg(target_os = "linux")] |
| 212 | +pub fn reboot(cmd: RebootCommand) -> io::Result<()> { |
| 213 | + backend::system::syscalls::reboot(cmd) |
| 214 | +} |
0 commit comments