aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-11-15 19:10:59 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-11-15 19:10:59 +0100
commit0d0b11a9ae84e5df9f77f2d2340fa80c32aebcd8 (patch)
treed94744eb9294d8cdec8055e110b26e31eb6810fa
parent7f554a96d54aebdcaf4fd9e68af17f099a015b9d (diff)
use new shutdown system using signals to init
-rw-r--r--Cargo.lock66
-rw-r--r--Cargo.toml3
-rw-r--r--src/main.rs28
3 files changed, 58 insertions, 39 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bc9bafe..ec50563 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8,7 +8,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a7559404a7f3573127aab53c08ce37a6c6a315c374a31070f3c91cd1b4a7fe"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
"bytes",
"futures-core",
"futures-sink",
@@ -32,7 +32,7 @@ dependencies = [
"actix-utils",
"ahash 0.8.3",
"base64 0.21.0",
- "bitflags",
+ "bitflags 1.3.2",
"brotli",
"bytes",
"bytestring",
@@ -311,6 +311,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
+name = "bitflags"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
+
+[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -492,7 +498,7 @@ version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05669f8e7e2d7badc545c513710f0eba09c2fbef683eb859fd79c46c355048e0"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
"byteorder",
"chrono",
"log",
@@ -716,9 +722,9 @@ checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
[[package]]
name = "libc"
-version = "0.2.142"
+version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317"
+checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]]
name = "local-channel"
@@ -764,15 +770,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
-name = "memoffset"
-version = "0.7.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
-dependencies = [
- "autocfg",
-]
-
-[[package]]
name = "mime"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -801,16 +798,22 @@ dependencies = [
[[package]]
name = "nix"
-version = "0.26.2"
+version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a"
+checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
- "bitflags",
+ "bitflags 2.4.1",
"cfg-if",
"libc",
- "memoffset",
- "pin-utils",
- "static_assertions",
+]
+
+[[package]]
+name = "ntapi"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
+dependencies = [
+ "winapi",
]
[[package]]
@@ -951,7 +954,7 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
]
[[package]]
@@ -1008,6 +1011,7 @@ dependencies = [
"rustls",
"rustls-pemfile",
"serde",
+ "sysinfo",
"thiserror",
]
@@ -1155,12 +1159,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
-name = "static_assertions"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
-
-[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1183,6 +1181,20 @@ dependencies = [
]
[[package]]
+name = "sysinfo"
+version = "0.29.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0a18d114d420ada3a891e6bc8e96a2023402203296a47cdd65083377dad18ba5"
+dependencies = [
+ "cfg-if",
+ "core-foundation-sys",
+ "libc",
+ "ntapi",
+ "once_cell",
+ "winapi",
+]
+
+[[package]]
name = "thiserror"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index a7669ea..a86c04c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,8 +11,9 @@ actix-web-httpauth = "0.8.0"
constant_time_eq = "0.2.5"
fatfs = "0.3.6"
fscommon = "0.1.1"
-nix = "0.26.2"
+nix = { version = "0.27.1", features = ["fs"] }
rustls = "0.20.0"
rustls-pemfile = "1.0.2"
serde = { version = "1.0", features = ["derive"] }
+sysinfo = { version = "0.29.10", default-features = false }
thiserror = "1.0"
diff --git a/src/main.rs b/src/main.rs
index 76bd213..287009f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,10 +11,10 @@ use actix_web_httpauth::extractors::AuthenticationError;
use actix_web_httpauth::middleware::HttpAuthentication;
use constant_time_eq::constant_time_eq;
use fscommon::BufStream;
-use nix::sys::reboot::{reboot, RebootMode};
use rustls::{Certificate, PrivateKey, ServerConfig};
use rustls_pemfile::{certs, pkcs8_private_keys};
use serde::Deserialize;
+use sysinfo::{Pid, ProcessExt, Signal, System, SystemExt};
#[allow(non_upper_case_globals)]
const KiB: usize = 1024;
@@ -29,26 +29,32 @@ struct DataRequest {
async fn handle_reboot() -> HttpResponse {
println!("request reboot");
- match reboot(RebootMode::RB_AUTOBOOT) {
- Ok(_) => HttpResponse::Ok()
+ if let Some(init) = System::new_all().process(Pid::from(1)) {
+ init.kill_with(Signal::User1);
+
+ HttpResponse::Ok()
.content_type(ContentType::plaintext())
- .body("rebooting..."),
- Err(e) => HttpResponse::InternalServerError()
+ .body("rebooting...")
+ } else {
+ HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
- .body(format!("can't reboot: {}", e)),
+ .body("can't reboot: no pid1")
}
}
async fn handle_shutdown() -> HttpResponse {
println!("request shutdown");
- match reboot(RebootMode::RB_POWER_OFF) {
- Ok(_) => HttpResponse::Ok()
+ if let Some(init) = System::new_all().process(Pid::from(1)) {
+ init.kill_with(Signal::User2);
+
+ HttpResponse::Ok()
.content_type(ContentType::plaintext())
- .body("shutting down..."),
- Err(e) => HttpResponse::InternalServerError()
+ .body("shutting down...")
+ } else {
+ HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
- .body(format!("can't shut down: {}", e)),
+ .body("can't shut down: {}")
}
}