aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-11-22 13:32:07 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-11-22 13:32:07 +0100
commit867bf01f3e8cb179088d1eaf9a6cc7d2b1037a2f (patch)
tree5059ced91eeb7a81edfb7166cfcd1dc9edd52cde /src
parentabd5de6bef661ffc41c5ef36c55b4c10916b64a0 (diff)
add process killing api
Diffstat (limited to 'src')
-rw-r--r--src/main.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index a0fde4c..430518b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -46,6 +46,12 @@ struct DataRequest {
path: String,
}
+#[derive(Clone, Debug, Deserialize)]
+struct KillRequest {
+ process: String,
+ signal: String,
+}
+
async fn handle_reboot() -> HttpResponse {
println!("request reboot");
@@ -207,6 +213,32 @@ async fn handle_proc_top() -> HttpResponse {
.body(out)
}
+async fn handle_proc_kill(info: web::Query<KillRequest>) -> HttpResponse {
+ let query = info.into_inner();
+
+ let signal = match query.signal.as_str() {
+ "usr1" => Signal::User1,
+ "usr2" => Signal::User2,
+ "term" => Signal::Term,
+ "kill" => Signal::Kill,
+ _ => {
+ return HttpResponse::BadRequest()
+ .content_type(ContentType::plaintext())
+ .body("invalid signal, need usr1, usr2, term or kill")
+ }
+ };
+
+ println!("kill process {} signal {}", query.process, query.signal);
+
+ for process in System::new_all().processes_by_exact_name(&query.process) {
+ process.kill_with(signal);
+ }
+
+ HttpResponse::Ok()
+ .content_type(ContentType::plaintext())
+ .body("kill successful")
+}
+
#[actix_web::main]
async fn main() -> io::Result<()> {
match start().await {
@@ -239,6 +271,7 @@ async fn start() -> Result<()> {
.service(web::resource("/data/read").to(handle_data_read))
.service(web::resource("/data/write").to(handle_data_write))
.service(web::resource("/proc/top").to(handle_proc_top))
+ .service(web::resource("/proc/kill").to(handle_proc_kill))
})
.bind_rustls("[::]:8443", config)?
.run()