From 67b3be4c4a2ded95dd604bc9cbe67286712abd4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 29 Jul 2023 18:11:32 +0200 Subject: [PATCH] feat: use /run/user to store socket and pid file --- Cargo.lock | 5 +++-- Cargo.toml | 1 + src/cli.rs | 3 ++- src/helper.rs | 7 +++++++ src/main.rs | 8 +++++++- 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/helper.rs diff --git a/Cargo.lock b/Cargo.lock index f817b60..2a2cb5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -621,9 +621,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.146" +version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f92be4933c13fd498862a9e02a3055f8a8d9c039ce33db97306fd5a6caa7f29b" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "linux-raw-sys" @@ -1134,6 +1134,7 @@ dependencies = [ "clap", "daemonize", "humantime", + "libc", "notify-rust", "serde", "serde_cbor", diff --git a/Cargo.toml b/Cargo.toml index 6626413..52842b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ anyhow = "1.0.71" clap = { version = "4.3.4", features = ["derive"] } daemonize = "0.5.0" humantime = "2.1.0" +libc = "0.2.147" notify-rust = "4.8.0" serde = { version = "1.0.164", features = ["derive"] } serde_cbor = "0.11.2" diff --git a/src/cli.rs b/src/cli.rs index 51fe842..025d30e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,5 @@ use crate::daemon::{Answer, AnswerErr, Command as OtherCommand}; +use crate::run_path; use anyhow::{Context, Result}; use clap::{Parser, Subcommand}; use std::net::Shutdown; @@ -12,7 +13,7 @@ pub struct Cli { #[command(subcommand)] pub command: Command, #[arg(short, long)] - #[clap(default_value = "/tmp/timers.socket")] + #[clap(default_value_t = format!("{}/timers.socket", run_path()))] pub socket: String, } diff --git a/src/helper.rs b/src/helper.rs new file mode 100644 index 0000000..164b5e9 --- /dev/null +++ b/src/helper.rs @@ -0,0 +1,7 @@ +pub fn getuid() -> u32 { + unsafe { libc::getuid() } +} + +pub fn run_path() -> String { + format!("/run/user/{}", getuid()) +} diff --git a/src/main.rs b/src/main.rs index 3143354..48222fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,14 @@ mod cli; mod daemon; +mod helper; mod notification; mod pomodoro; mod timer; +use crate::helper::getuid; use crate::cli::{send_command, Cli, Command as CliCommand}; use crate::daemon::{Command as DaemonCommand, Daemon}; +use crate::helper::run_path; use clap::Parser; use cli::PomodoroCommand; @@ -13,7 +16,10 @@ fn main() -> Result<(), anyhow::Error> { let args = Cli::parse(); let daemon_command = match args.command { CliCommand::Daemon { no_notify } => { - daemonize::Daemonize::new().start()?; + daemonize::Daemonize::new() + .pid_file(format!("{}/timers.pid", run_path())) + .user(getuid()) + .start()?; return Daemon::new(args.socket, no_notify)?.run(); } CliCommand::Add { name, duration } => {