Skip to content

Commit b453df4

Browse files
committed
watt: move lock to /run/watt/lock
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I3b724cbdf8c584c7b23c97ca2328efb96a6a6964
1 parent 2bcf1ee commit b453df4

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

watt/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ pub fn main() -> anyhow::Result<()> {
4040

4141
log::info!("starting watt daemon");
4242

43-
let lock_path = PathBuf::from("/run/watt.lock");
44-
let _lock = lock::LockFile::acquire(&lock_path).with_context(|| {
45-
format!(
46-
"failed to acquire exclusive lock at {}",
47-
lock_path.display()
48-
)
49-
})?;
43+
let lock_path = PathBuf::from("/run/watt/lock");
44+
let _lock = lock::LockFile::acquire(&lock_path)?;
5045

5146
system::run_daemon(config)
5247
}

watt/lock.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ use std::{
22
error::Error,
33
fmt,
44
fs::{
5+
self,
56
File,
67
OpenOptions,
78
},
89
ops,
9-
os::unix::fs::OpenOptionsExt,
10+
os::unix::fs::{
11+
DirBuilderExt,
12+
OpenOptionsExt,
13+
},
1014
path::{
1115
Path,
1216
PathBuf,
@@ -64,6 +68,31 @@ impl LockFile {
6468
}
6569

6670
pub fn acquire(lock_path: &Path) -> Result<Self, LockFileError> {
71+
// Ensure parent directory exists with proper permissions
72+
if let Some(parent) = lock_path.parent() {
73+
if !parent.exists() {
74+
fs::DirBuilder::new()
75+
.mode(0o755)
76+
.recursive(true)
77+
.create(parent)
78+
.map_err(|error| {
79+
log::error!(
80+
"failed to create lock directory {}: {}",
81+
parent.display(),
82+
error
83+
);
84+
LockFileError {
85+
path: lock_path.to_owned(),
86+
message: Some(format!(
87+
"cannot create directory {}: {}",
88+
parent.display(),
89+
error
90+
)),
91+
}
92+
})?;
93+
}
94+
}
95+
6796
#[allow(clippy::suspicious_open_options)]
6897
let file = OpenOptions::new()
6998
.create(true)

0 commit comments

Comments
 (0)