feat: use /run/user to store socket and pid file

This commit is contained in:
Moritz Böhme 2023-07-29 18:11:32 +02:00
parent 0727b3e053
commit 67b3be4c4a
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9
5 changed files with 20 additions and 4 deletions

View file

@ -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,
}

7
src/helper.rs Normal file
View file

@ -0,0 +1,7 @@
pub fn getuid() -> u32 {
unsafe { libc::getuid() }
}
pub fn run_path() -> String {
format!("/run/user/{}", getuid())
}

View file

@ -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 } => {