|
| 1 | +use std::{ |
| 2 | + fs::{ |
| 3 | + File, |
| 4 | + OpenOptions, |
| 5 | + }, |
| 6 | + io::Write, |
| 7 | + path::{ |
| 8 | + Path, |
| 9 | + PathBuf, |
| 10 | + }, |
| 11 | + process, |
| 12 | +}; |
| 13 | + |
| 14 | +#[cfg(unix)] use nix::fcntl::{ |
| 15 | + Flock, |
| 16 | + FlockArg, |
| 17 | +}; |
| 18 | + |
| 19 | +#[cfg(not(unix))] |
| 20 | +compile_error!("watt is only supported on Unix-like systems"); |
| 21 | + |
| 22 | +pub struct LockFile { |
| 23 | + lock: Flock<File>, |
| 24 | + path: PathBuf, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug)] |
| 28 | +pub struct LockFileError { |
| 29 | + pub path: PathBuf, |
| 30 | + pid: u32, |
| 31 | +} |
| 32 | + |
| 33 | +impl std::fmt::Display for LockFileError { |
| 34 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 35 | + if self.pid == 0 { |
| 36 | + write!(f, "failed to acquire lock at {}", self.path.display()) |
| 37 | + } else { |
| 38 | + write!(f, "another watt daemon is running (PID: {})", self.pid) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl std::error::Error for LockFileError {} |
| 44 | + |
| 45 | +impl std::ops::Deref for LockFile { |
| 46 | + type Target = File; |
| 47 | + |
| 48 | + fn deref(&self) -> &Self::Target { |
| 49 | + &self.lock |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl std::ops::DerefMut for LockFile { |
| 54 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 55 | + &mut self.lock |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl LockFile { |
| 60 | + pub fn path(&self) -> &Path { |
| 61 | + &self.path |
| 62 | + } |
| 63 | + |
| 64 | + pub fn acquire( |
| 65 | + lock_path: &Path, |
| 66 | + force: bool, |
| 67 | + ) -> Result<Option<Self>, LockFileError> { |
| 68 | + let pid = process::id(); |
| 69 | + |
| 70 | + #[allow(clippy::suspicious_open_options)] |
| 71 | + let file = match OpenOptions::new() |
| 72 | + .create(true) |
| 73 | + .read(true) |
| 74 | + .write(true) |
| 75 | + .open(lock_path) |
| 76 | + { |
| 77 | + Ok(file) => file, |
| 78 | + Err(error) => { |
| 79 | + log::error!( |
| 80 | + "failed to open lock file at {}: {}", |
| 81 | + lock_path.display(), |
| 82 | + error |
| 83 | + ); |
| 84 | + return Err(LockFileError { |
| 85 | + path: lock_path.to_owned(), |
| 86 | + pid: 0, |
| 87 | + }); |
| 88 | + }, |
| 89 | + }; |
| 90 | + |
| 91 | + let mut lock = match Flock::lock(file, FlockArg::LockExclusiveNonblock) { |
| 92 | + Ok(lock) => lock, |
| 93 | + Err((_, nix::errno::Errno::EWOULDBLOCK)) => { |
| 94 | + let existing_pid = Self::read_pid(lock_path); |
| 95 | + |
| 96 | + if let Some(existing_pid) = existing_pid { |
| 97 | + if force { |
| 98 | + log::warn!( |
| 99 | + "another watt instance is running (PID: {existing_pid}), \ |
| 100 | + starting anyway", |
| 101 | + ); |
| 102 | + return Ok(None); |
| 103 | + } |
| 104 | + |
| 105 | + return Err(LockFileError { |
| 106 | + path: lock_path.to_owned(), |
| 107 | + pid: existing_pid, |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + if force { |
| 112 | + log::warn!( |
| 113 | + "could not determine PID of existing watt instance, starting \ |
| 114 | + anyway", |
| 115 | + ); |
| 116 | + return Ok(None); |
| 117 | + } |
| 118 | + |
| 119 | + return Err(LockFileError { |
| 120 | + path: lock_path.to_owned(), |
| 121 | + pid: 0, |
| 122 | + }); |
| 123 | + }, |
| 124 | + |
| 125 | + Err((_, error)) => { |
| 126 | + log::error!("failed to acquire lock: {}", error); |
| 127 | + return Err(LockFileError { |
| 128 | + path: lock_path.to_owned(), |
| 129 | + pid: 0, |
| 130 | + }); |
| 131 | + }, |
| 132 | + }; |
| 133 | + |
| 134 | + if let Err(e) = lock.set_len(0) { |
| 135 | + log::error!("failed to truncate lock file: {}", e); |
| 136 | + return Err(LockFileError { |
| 137 | + path: lock_path.to_owned(), |
| 138 | + pid: 0, |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + if let Err(e) = lock.write_all(format!("{pid}\n").as_bytes()) { |
| 143 | + log::error!("failed to write PID to lock file: {}", e); |
| 144 | + return Err(LockFileError { |
| 145 | + path: lock_path.to_owned(), |
| 146 | + pid: 0, |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + if let Err(e) = lock.sync_all() { |
| 151 | + log::error!("failed to sync lock file: {}", e); |
| 152 | + return Err(LockFileError { |
| 153 | + path: lock_path.to_owned(), |
| 154 | + pid: 0, |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + Ok(Some(LockFile { |
| 159 | + lock, |
| 160 | + path: lock_path.to_owned(), |
| 161 | + })) |
| 162 | + } |
| 163 | + |
| 164 | + fn read_pid(lock_path: &Path) -> Option<u32> { |
| 165 | + match std::fs::read_to_string(lock_path) { |
| 166 | + Ok(content) => content.trim().parse().ok(), |
| 167 | + Err(_) => None, |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + pub fn release(&mut self) { |
| 172 | + let _ = std::fs::remove_file(&self.path); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl Drop for LockFile { |
| 177 | + fn drop(&mut self) { |
| 178 | + self.release(); |
| 179 | + } |
| 180 | +} |
0 commit comments