From 87298226d0033a2db699d5bb6b1e1dbd7960c04a Mon Sep 17 00:00:00 2001 From: Scott Lawrence Date: Wed, 27 Nov 2024 22:49:58 -0700 Subject: Removing socket on exit --- Cargo.lock | 26 ++++++++++++++++++++++++++ Cargo.toml | 1 + src/forecast.rs | 24 ++++++++++++++++++++++++ src/main.rs | 23 ++++++++++++++++++----- 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1304ff4..b1b20b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,6 +121,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "libc" +version = "0.2.166" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36" + [[package]] name = "proc-macro2" version = "1.0.84" @@ -176,6 +182,25 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "strsim" version = "0.11.1" @@ -213,6 +238,7 @@ dependencies = [ "cty", "serde", "serde_json", + "signal-hook", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c9a0fe0..72b9cc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ clap = { version = "4.5.4", features = ["derive"] } cty = { version = "0.2.2" } serde = { version = "1.0.203", features = ["derive"] } serde_json = { version = "1.0" } +signal-hook = "0.3.17" diff --git a/src/forecast.rs b/src/forecast.rs index e69de29..5a71665 100644 --- a/src/forecast.rs +++ b/src/forecast.rs @@ -0,0 +1,24 @@ +/* Varanus + * forecast.rs: lightweight time-series forecasting + * + * Copyright (c) 2024 Scott Lawrence. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +pub struct Forecast { +} + diff --git a/src/main.rs b/src/main.rs index fbb23e2..ccff659 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,11 @@ */ /* TODO - * - * Remove socket file when daemon exits. * * Accept command-line arguments to query only part of JSON. (Maybe also a formatting string?) * + * Forecast + * */ use std::error; @@ -37,6 +37,9 @@ use std::time::{Duration,Instant,SystemTime}; use clap::Parser; use serde::{Serialize,Deserialize}; +use signal_hook::{consts::SIGINT, iterator::Signals}; + +mod forecast; #[derive(Default)] #[repr(C)] @@ -180,10 +183,20 @@ impl Listener { fn listen(&self) { let state_mutex = Arc::clone(&self.state); - let sfn = self.file.clone(); + let sfn1 = self.file.clone(); + let sfn2 = self.file.clone(); + spawn(move || { + match Signals::new([SIGINT]) { + Ok(mut signals) => for _sig in signals.forever() { + let _ = remove_file(&sfn1); + std::process::exit(0) + }, + Err(_) => () + } + }); spawn(move || { - let _ = remove_file(&sfn); - let listener = UnixListener::bind(sfn).unwrap(); + let _ = remove_file(&sfn2); + let listener = UnixListener::bind(sfn2).unwrap(); loop { match listener.accept() { Ok((mut socket,accept)) => { -- cgit v1.2.3-54-g00ecf