From 45eff808cc9b0778ddcb51a6070916fdedeac3e7 Mon Sep 17 00:00:00 2001 From: GjergjiShkurti Date: Tue, 3 Dec 2024 13:09:06 +0100 Subject: [PATCH] Implement daemonizer component (#9) * integrate daemonizer component * fix dependency conflicts between windows and unix builds * add canape project for daemon example * rewrite deamonizer component with nix * Minor changes * Minor changes * install daemon with shell script * add logir cfg item for daemonization * split syslog logging from process logging * minor fix * update cargo lock * make xcp loglvl configurable in xcp daemon example * append to daemon logfile instead of overwrite * prevent unintentional daemonizer builds outside unix platforms * re-export xcp dependencies * ignore snake case warnings in IdlGenrator output * fix xcp daemon example --------- Co-authored-by: Rainer Zaiser --- Cargo.lock | 22 ----------- examples/hello_xcp/Cargo.toml | 7 +--- examples/hello_xcp/src/main.rs | 1 - examples/multi_thread_demo/Cargo.toml | 4 +- examples/multi_thread_demo/src/main.rs | 1 - examples/point_cloud_demo/Cargo.toml | 4 -- examples/point_cloud_demo/src/main.rs | 2 - examples/protobuf_demo/Cargo.toml | 4 -- examples/rayon_demo/Cargo.toml | 4 -- examples/rayon_demo/src/main.rs | 1 - examples/scoped_threads/Cargo.toml | 5 +-- examples/scoped_threads/src/main.rs | 1 - examples/single_thread_demo/Cargo.toml | 7 +--- examples/single_thread_demo/src/main.rs | 1 - examples/tokio_demo/Cargo.toml | 3 -- examples/tokio_demo/src/main.rs | 1 - examples/type_description_demo/Cargo.toml | 4 -- examples/type_description_demo/src/main.rs | 3 +- examples/xcp_daemon/Cargo.toml | 8 +++- examples/xcp_daemon/README.md | 2 +- examples/xcp_daemon/src/main.rs | 37 ++++++++++++------- examples/xcp_daemon/src/platform.rs | 9 +++++ src/lib.rs | 3 ++ .../xcp_idl_generator_derive/src/lib.rs | 4 +- 24 files changed, 50 insertions(+), 88 deletions(-) create mode 100644 examples/xcp_daemon/src/platform.rs diff --git a/Cargo.lock b/Cargo.lock index ea7b881..a63ae60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -862,8 +862,6 @@ dependencies = [ "serde", "serde_json", "xcp", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -1368,8 +1366,6 @@ dependencies = [ "serde", "serde_json", "xcp", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -1645,10 +1641,6 @@ dependencies = [ "serde", "serde_json", "xcp", - "xcp_idl_generator", - "xcp_idl_generator_derive", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -1891,8 +1883,6 @@ dependencies = [ "serde", "serde_json", "xcp", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -2095,10 +2085,6 @@ dependencies = [ "serde", "serde_json", "xcp", - "xcp_idl_generator", - "xcp_idl_generator_derive", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -2354,8 +2340,6 @@ dependencies = [ "serde_json", "tokio", "xcp", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -2397,8 +2381,6 @@ name = "type_description_demo" version = "0.1.0" dependencies = [ "xcp", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -2782,8 +2764,6 @@ dependencies = [ "syslog", "thiserror 2.0.3", "xcp", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] @@ -2799,8 +2779,6 @@ dependencies = [ "serde", "serde_json", "xcp", - "xcp_type_description", - "xcp_type_description_derive", ] [[package]] diff --git a/examples/hello_xcp/Cargo.toml b/examples/hello_xcp/Cargo.toml index dbdd0a8..7e17794 100644 --- a/examples/hello_xcp/Cargo.toml +++ b/examples/hello_xcp/Cargo.toml @@ -13,9 +13,4 @@ lazy_static = "1.4.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } - - - +xcp = { path = "../../", features = [] } \ No newline at end of file diff --git a/examples/hello_xcp/src/main.rs b/examples/hello_xcp/src/main.rs index 9c64a59..7d184d0 100644 --- a/examples/hello_xcp/src/main.rs +++ b/examples/hello_xcp/src/main.rs @@ -12,7 +12,6 @@ use log::{debug, error, info, trace, warn}; use std::{fmt::Debug, thread, time::Duration}; use xcp::*; -use xcp_type_description::prelude::*; //----------------------------------------------------------------------------- // Calibration parameters diff --git a/examples/multi_thread_demo/Cargo.toml b/examples/multi_thread_demo/Cargo.toml index 178f240..5895cd7 100644 --- a/examples/multi_thread_demo/Cargo.toml +++ b/examples/multi_thread_demo/Cargo.toml @@ -11,6 +11,4 @@ lazy_static = "1.4.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } +xcp = { path = "../../", features = [] } \ No newline at end of file diff --git a/examples/multi_thread_demo/src/main.rs b/examples/multi_thread_demo/src/main.rs index 0fc6359..977899f 100644 --- a/examples/multi_thread_demo/src/main.rs +++ b/examples/multi_thread_demo/src/main.rs @@ -12,7 +12,6 @@ use std::{ use anyhow::Result; use xcp::*; -use xcp_type_description::prelude::*; // Static application start time lazy_static::lazy_static! { diff --git a/examples/point_cloud_demo/Cargo.toml b/examples/point_cloud_demo/Cargo.toml index b0240c6..3cbfd61 100644 --- a/examples/point_cloud_demo/Cargo.toml +++ b/examples/point_cloud_demo/Cargo.toml @@ -17,10 +17,6 @@ lazy_static = "1.4.0" cdr = "0.2.4" xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } -xcp_idl_generator = { path = "../../xcp_idl_generator/"} -xcp_idl_generator_derive = { path = "../../xcp_idl_generator/xcp_idl_generator_derive/"} diff --git a/examples/point_cloud_demo/src/main.rs b/examples/point_cloud_demo/src/main.rs index f561615..41ae379 100644 --- a/examples/point_cloud_demo/src/main.rs +++ b/examples/point_cloud_demo/src/main.rs @@ -14,8 +14,6 @@ use std::{ // XCP use xcp::*; -use xcp_idl_generator::prelude::*; -use xcp_type_description::prelude::*; //----------------------------------------------------------------------------- // Defaults diff --git a/examples/protobuf_demo/Cargo.toml b/examples/protobuf_demo/Cargo.toml index e9e0fa3..bdf9281 100644 --- a/examples/protobuf_demo/Cargo.toml +++ b/examples/protobuf_demo/Cargo.toml @@ -16,8 +16,4 @@ prost-types = "0.13.1" xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } - - diff --git a/examples/rayon_demo/Cargo.toml b/examples/rayon_demo/Cargo.toml index f8a6f24..365e729 100644 --- a/examples/rayon_demo/Cargo.toml +++ b/examples/rayon_demo/Cargo.toml @@ -16,9 +16,5 @@ serde_json = "1.0" lazy_static = "1.4.0" xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } - - diff --git a/examples/rayon_demo/src/main.rs b/examples/rayon_demo/src/main.rs index f49be32..d1f3057 100644 --- a/examples/rayon_demo/src/main.rs +++ b/examples/rayon_demo/src/main.rs @@ -13,7 +13,6 @@ use num::Complex; use rayon::prelude::*; use std::{thread, time::Duration}; use xcp::*; -use xcp_type_description::prelude::*; //--------------------------------------------------------------------------------------- // Calibratable parameters diff --git a/examples/scoped_threads/Cargo.toml b/examples/scoped_threads/Cargo.toml index 6ba7f45..db46bb8 100644 --- a/examples/scoped_threads/Cargo.toml +++ b/examples/scoped_threads/Cargo.toml @@ -11,7 +11,4 @@ lazy_static = "1.4.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } - +xcp = { path = "../../", features = [] } \ No newline at end of file diff --git a/examples/scoped_threads/src/main.rs b/examples/scoped_threads/src/main.rs index ab35a0c..7ef46a8 100644 --- a/examples/scoped_threads/src/main.rs +++ b/examples/scoped_threads/src/main.rs @@ -6,7 +6,6 @@ use log::{debug, error, info, trace, warn}; use std::{fmt::Debug, thread, time::Duration}; use xcp::*; -use xcp_type_description::prelude::*; //----------------------------------------------------------------------------- diff --git a/examples/single_thread_demo/Cargo.toml b/examples/single_thread_demo/Cargo.toml index 2e4cf3f..5d60222 100644 --- a/examples/single_thread_demo/Cargo.toml +++ b/examples/single_thread_demo/Cargo.toml @@ -11,9 +11,4 @@ lazy_static = "1.4.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } -xcp_idl_generator = { path = "../../xcp_idl_generator/"} -xcp_idl_generator_derive = { path = "../../xcp_idl_generator/xcp_idl_generator_derive/"} - +xcp = { path = "../../", features = [] } \ No newline at end of file diff --git a/examples/single_thread_demo/src/main.rs b/examples/single_thread_demo/src/main.rs index eb1d4c8..f5fa5f4 100644 --- a/examples/single_thread_demo/src/main.rs +++ b/examples/single_thread_demo/src/main.rs @@ -11,7 +11,6 @@ use std::{ }; use xcp::*; -use xcp_type_description::prelude::*; //----------------------------------------------------------------------------- // Demo calibration parameters diff --git a/examples/tokio_demo/Cargo.toml b/examples/tokio_demo/Cargo.toml index cf3f124..c279b70 100644 --- a/examples/tokio_demo/Cargo.toml +++ b/examples/tokio_demo/Cargo.toml @@ -16,6 +16,3 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } - diff --git a/examples/tokio_demo/src/main.rs b/examples/tokio_demo/src/main.rs index fc1c085..45685af 100644 --- a/examples/tokio_demo/src/main.rs +++ b/examples/tokio_demo/src/main.rs @@ -10,7 +10,6 @@ use core::f64::consts::PI; use std::error::Error; use xcp::*; -use xcp_type_description::prelude::*; include!("../../../tests/xcp_server_task.rs"); diff --git a/examples/type_description_demo/Cargo.toml b/examples/type_description_demo/Cargo.toml index 73519f0..c4cf17b 100644 --- a/examples/type_description_demo/Cargo.toml +++ b/examples/type_description_demo/Cargo.toml @@ -5,8 +5,4 @@ edition = "2021" [dependencies] xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description" } -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } - - diff --git a/examples/type_description_demo/src/main.rs b/examples/type_description_demo/src/main.rs index 4106dd8..776295a 100644 --- a/examples/type_description_demo/src/main.rs +++ b/examples/type_description_demo/src/main.rs @@ -1,5 +1,4 @@ -//use xcp::*; -use xcp_type_description::prelude::*; +use xcp::*; #[derive(Clone, Copy, XcpTypeDescription, Debug)] struct Parent { diff --git a/examples/xcp_daemon/Cargo.toml b/examples/xcp_daemon/Cargo.toml index 71dfc9d..6ac1eb9 100644 --- a/examples/xcp_daemon/Cargo.toml +++ b/examples/xcp_daemon/Cargo.toml @@ -3,10 +3,14 @@ name = "xcp_daemon" version = "0.1.0" edition = "2021" +# In windows we simply log an error message and exit +# So we don't need to link any dependencies [dependencies] +log = "0.4.22" + +# In unix we link our dependencies as per usual +[target.'cfg(unix)'.dependencies] xcp = { path = "../../", features = [] } -xcp_type_description = { path = "../../xcp_type_description/"} -xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" } log = "0.4.22" signal-hook = "0.3.17" diff --git a/examples/xcp_daemon/README.md b/examples/xcp_daemon/README.md index cbbff5e..e20f332 100644 --- a/examples/xcp_daemon/README.md +++ b/examples/xcp_daemon/README.md @@ -13,7 +13,7 @@ sudo chmod +x ./install.sh; # Make the install script executable sudo ./install.sh; # Install the example (binaries and configuration files) # edit parameters -sudo nano /etc/xcpd/xcpd.conf +sudo nano /etc/xcpd/xcpd.conf # run sudo systemctl start xcpd; # Start the daemon diff --git a/examples/xcp_daemon/src/main.rs b/examples/xcp_daemon/src/main.rs index 89fb5d6..0201042 100644 --- a/examples/xcp_daemon/src/main.rs +++ b/examples/xcp_daemon/src/main.rs @@ -1,17 +1,14 @@ -use xcp::*; -use xcp_type_description::prelude::*; - +#[cfg(unix)] +mod platform; +#[cfg(unix)] +use platform::*; +#[cfg(unix)] use log::info; -use serde::{Deserialize, Serialize}; -use signal_hook::{ - consts::{SIGHUP, SIGINT, SIGTERM}, - iterator::Signals, -}; -use thiserror::Error; -use std::{thread, time::Duration}; +use log::error; -#[derive(Serialize, Deserialize, Debug, Clone, Copy, XcpTypeDescription)] +#[cfg(unix)] +#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, XcpTypeDescription)] struct CalPage1 { #[type_description(comment = "Max counter value", min = "0", max = "1023")] counter_max: u32, @@ -21,6 +18,7 @@ struct CalPage1 { delay: u32, } +#[cfg(unix)] // Default value for the calibration parameters const CAL_PAGE: CalPage1 = CalPage1 { counter_min: 5, @@ -28,6 +26,7 @@ const CAL_PAGE: CalPage1 = CalPage1 { delay: 100000, }; +#[cfg(unix)] #[derive(Error, Debug)] enum XcpProcessError { #[error("An XCP error occurred: {0}")] @@ -38,16 +37,19 @@ enum XcpProcessError { IoError(#[from] std::io::Error), } +#[cfg(unix)] struct XcpProcess { cfg: ProcessConfig, } +#[cfg(unix)] impl XcpProcess { fn new(config: ProcessConfig) -> Self { XcpProcess { cfg: config } } } +#[cfg(unix)] impl Process for XcpProcess { type Error = XcpProcessError; @@ -179,8 +181,8 @@ impl Process for XcpProcess { &self.cfg } } - -fn main() { +#[cfg(unix)] +fn _main() { let cfg = ProcessConfig::new( "xcpd", "/var/run/xcpd.pid", @@ -195,3 +197,12 @@ fn main() { let mut daemon = Daemon::new(XcpProcess::new(cfg)); daemon.run().expect("Failed to run daemon"); } + +#[cfg(not(unix))] +fn _main() { + error!("Daemonization is only supported for Unix platforms."); +} + +fn main() { + _main(); +} diff --git a/examples/xcp_daemon/src/platform.rs b/examples/xcp_daemon/src/platform.rs new file mode 100644 index 0000000..272cb9d --- /dev/null +++ b/examples/xcp_daemon/src/platform.rs @@ -0,0 +1,9 @@ +#![cfg(unix)] + +pub use signal_hook::{ + consts::{SIGHUP, SIGINT, SIGTERM}, + iterator::Signals, +}; +pub use std::{thread, time::Duration}; +pub use thiserror::Error; +pub use xcp::*; diff --git a/src/lib.rs b/src/lib.rs index 51ad8e1..dc3a1ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,6 +126,9 @@ mod daemon; #[cfg(unix)] pub use daemon::unix::*; +pub use xcp_type_description::prelude::*; +pub use xcp_idl_generator::prelude::*; + //---------------------------------------------------------------------------------------------- // Manually register a static measurement and calibration variables diff --git a/xcp_idl_generator/xcp_idl_generator_derive/src/lib.rs b/xcp_idl_generator/xcp_idl_generator_derive/src/lib.rs index 831b79d..79f5d78 100644 --- a/xcp_idl_generator/xcp_idl_generator_derive/src/lib.rs +++ b/xcp_idl_generator/xcp_idl_generator_derive/src/lib.rs @@ -17,7 +17,6 @@ pub fn idl_generator_derive(input: TokenStream) -> TokenStream { .fields .iter() .map(|field| { - //TODO Error handling let field_name = &field.ident.as_ref().unwrap(); let field_type = &field.ty; @@ -44,6 +43,7 @@ pub fn idl_generator_derive(input: TokenStream) -> TokenStream { } #[ctor::ctor] + #[allow(non_snake_case)] fn #register_function_name() { let mut struct_fields = FieldList::new(); #(#field_handlers)* @@ -51,7 +51,7 @@ pub fn idl_generator_derive(input: TokenStream) -> TokenStream { static mut STRUCT_INSTANCE: Option = None; static mut INITIALIZED: bool = false; - // @@@@ - unsafe - Mutable static, todo + // @@@@ - unsafe - Mutable static unsafe { // Prevent the user from calling the register function multiple times if INITIALIZED {