Skip to content

Commit

Permalink
pre-release: airup v0.10.0-rc.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sisungo committed Apr 21, 2024
1 parent a803dd8 commit 0ecc0fd
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 45 deletions.
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Supported Versions
The `HEAD` and the latest release are supported by the Airup developers. The maintained versions are:
- Mainline: `0.10.0-rc.1`
- Mainline: `0.10.0-rc.2`
- Stable: `0.9.4`

## Reporting a Vulnerability
Expand Down
2 changes: 1 addition & 1 deletion airup-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors = ["sisungo <[email protected]>"]
description = "SDK library of Airup"
documentation = "https://docs.rs/airup-sdk"
repository = "https://github.com/sisungo/airup"
version = "0.10.0-rc.1"
version = "0.10.0-rc.2"
edition = "2021"
license = "MIT"

Expand Down
2 changes: 1 addition & 1 deletion airup/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "airup"
authors = ["sisungo <[email protected]>"]
version = "0.10.0-rc.1"
version = "0.10.0-rc.2"
edition = "2021"
license = "MIT"
publish = false
Expand Down
2 changes: 1 addition & 1 deletion airupd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "airupd"
authors = ["sisungo <[email protected]>"]
version = "0.10.0-rc.1"
version = "0.10.0-rc.2"
edition = "2021"
license = "MIT"
publish = false
Expand Down
9 changes: 9 additions & 0 deletions airupd/src/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl System {
self.send(Event::Halt);
}

/// Reboots the system's userspace.
pub fn userspace_reboot(&self) {
self.send(Event::UserspaceReboot);
}

/// Sends an process-wide lifetime event.
fn send(&self, event: Event) {
self.0.send(event).ok();
Expand All @@ -62,6 +67,9 @@ pub enum Event {

/// Halts the device.
Halt,

/// Reboots the system's userspace.
UserspaceReboot,
}
impl Event {
/// Handles the event.
Expand All @@ -71,6 +79,7 @@ impl Event {
Self::Poweroff => power_manager().poweroff().await.ok(),
Self::Reboot => power_manager().reboot().await.ok(),
Self::Halt => power_manager().halt().await.ok(),
Self::UserspaceReboot => power_manager().userspace().await.ok(),
};

std::process::exit(1);
Expand Down
2 changes: 1 addition & 1 deletion airupd/src/milestones/reboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async fn enter_userspace_reboot() -> Result<(), Error> {

let reboot_timeout = airupd().storage.config.system_conf.system.reboot_timeout;
stop_all_services(Duration::from_millis(reboot_timeout as _)).await;
airupd().bootstrap_milestone(crate::env::cmdline().milestone.to_string());
airupd().lifetime.userspace_reboot();

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion airupfx/airupfx-power/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "airupfx-power"
authors = ["sisungo <[email protected]>"]
version = "0.6.1"
version = "0.10.0"
edition = "2021"
license = "MIT"
publish = false
Expand Down
19 changes: 8 additions & 11 deletions airupfx/airupfx-power/src/freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@ use std::{convert::Infallible, time::Duration};

#[derive(Default)]
pub struct Power;
impl Power {
async fn prepare(&self) {
crate::unix::kill_all(Duration::from_millis(5000)).await;
unsafe {
libc::sync();
}
}
}
#[async_trait::async_trait]
impl PowerManager for Power {
async fn poweroff(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
reboot(RB_POWEROFF)
}

async fn reboot(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
reboot(RB_AUTOBOOT)
}

async fn halt(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
reboot(RB_HALT)
}

async fn userspace(&self) -> std::io::Result<Infallible> {
crate::unix::prepare().await;
airupfx_process::reload_image()
}
}

fn reboot(cmd: libc::c_int) -> std::io::Result<Infallible> {
Expand Down
10 changes: 10 additions & 0 deletions airupfx/airupfx-power/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub trait PowerManager: Send + Sync {
/// # Errors
/// An `Err(_)` is returned if the underlying OS function failed.
async fn halt(&self) -> std::io::Result<Infallible>;

/// Performs an userspace reboot.
///
/// # Errors
/// An `Err(_)` is returned if it failed to perform an userspace reboot.
async fn userspace(&self) -> std::io::Result<Infallible>;
}

/// A fallback implementation of `AirupFX` power management.
Expand All @@ -66,6 +72,10 @@ impl PowerManager for Fallback {
async fn halt(&self) -> std::io::Result<Infallible> {
Self::halt_process();
}

async fn userspace(&self) -> std::io::Result<Infallible> {
Self::halt_process();
}
}
impl Fallback {
/// Prints "It's now safe to turn off the device." to standard error stream and parks current thread.
Expand Down
19 changes: 8 additions & 11 deletions airupfx/airupfx-power/src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,30 @@ use std::{convert::Infallible, ptr::NonNull, time::Duration};

#[derive(Default)]
pub struct Power;
impl Power {
async fn prepare(&self) {
crate::unix::kill_all(Duration::from_millis(5000)).await;
unsafe {
libc::sync();
}
}
}
#[async_trait::async_trait]
impl PowerManager for Power {
async fn poweroff(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
reboot(LINUX_REBOOT_CMD_POWER_OFF, None)?;
unreachable!()
}

async fn reboot(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
reboot(LINUX_REBOOT_CMD_RESTART, None)?;
unreachable!()
}

async fn halt(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
reboot(LINUX_REBOOT_CMD_HALT, None)?;
unreachable!()
}

async fn userspace(&self) -> std::io::Result<Infallible> {
crate::unix::prepare().await;
airupfx_process::reload_image()
}
}

fn reboot(cmd: libc::c_int, arg: Option<NonNull<c_void>>) -> std::io::Result<()> {
Expand Down
21 changes: 9 additions & 12 deletions airupfx/airupfx-power/src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,34 @@ extern "C" {
}

use crate::PowerManager;
use std::{convert::Infallible, time::Duration};
use std::convert::Infallible;

const RB_AUTOBOOT: libc::c_int = 0;
const RB_HALT: libc::c_int = 0x08;

#[derive(Default)]
pub struct Power;
impl Power {
async fn prepare(&self) {
crate::unix::kill_all(Duration::from_millis(5000)).await;
unsafe {
libc::sync();
}
}
}
#[async_trait::async_trait]
impl PowerManager for Power {
async fn poweroff(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
system_reboot(RB_HALT)
}

async fn reboot(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
system_reboot(RB_AUTOBOOT)
}

async fn halt(&self) -> std::io::Result<Infallible> {
self.prepare().await;
crate::unix::prepare().await;
system_reboot(RB_HALT)
}

async fn userspace(&self) -> std::io::Result<Infallible> {
crate::unix::prepare().await;
airupfx_process::reload_image()
}
}

fn system_reboot(cmd: libc::c_int) -> std::io::Result<Infallible> {
Expand Down
43 changes: 38 additions & 5 deletions airupfx/airupfx-power/src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#![allow(unused)]

use std::time::Duration;

/// Prepares for system reboot, including killing all processes, syncing disks and unmounting filesystems.
pub(crate) async fn prepare() {
kill_all(Duration::from_millis(5000)).await;
sync_disks();
umount_all_filesystems();
}

/// Sends a signal to all running processes, then wait for them to be terminated. If the timeout expired, the processes are
/// force-killed.
#[allow(unused)]
pub(crate) async fn kill_all(timeout: Duration) {
eprintln!("Sending SIGTERM to all processes");
unsafe {
libc::kill(-1, libc::SIGTERM);
}
_ = kill_all_processes(libc::SIGTERM);

eprintln!("Waiting for all processes to be terminated");
let _lock = airupfx_process::lock().await;
Expand All @@ -23,7 +29,34 @@ pub(crate) async fn kill_all(timeout: Duration) {
drop(_lock);

eprintln!("Sending SIGKILL to all processes");
_ = kill_all_processes(libc::SIGKILL);
}

/// Flushes the block buffer cache and synchronizes disk caches.
fn sync_disks() {
unsafe {
libc::kill(-1, libc::SIGKILL);
libc::sync();
}
}

/// Sends the specified signal to all processes in the system.
fn kill_all_processes(signum: libc::c_int) -> std::io::Result<()> {
let x = unsafe { libc::kill(-1, signum) };
match x {
0 => Ok(()),
_ => Err(std::io::Error::last_os_error()),
}
}

/// Unmounts all filesystems.
async fn umount_all_filesystems() -> std::io::Result<()> {
airupfx_process::Command::new("umount")
.arg("-a")
.spawn()
.await?
.wait()
.await
.map_err(|_| std::io::Error::from(std::io::ErrorKind::PermissionDenied))?;

Ok(())
}

0 comments on commit 0ecc0fd

Please sign in to comment.