pidlock
A library for creating and managing PID-based file locks, providing a simple and reliable way to ensure only one instance of a program runs at a time.
Features
- Cross-platform: Works on Unix-like systems and Windows
- Stale lock detection: Automatically detects and cleans up locks from dead processes
- Path validation: Ensures lock file paths are valid across platforms
- Safe cleanup: Automatically releases locks when the
Pidlock
is dropped - Comprehensive error handling: Detailed error types for different failure scenarios
Quick Start
Add this to your Cargo.toml
:
[]
= "0.2"
Basic Usage
use Pidlock;
Advanced Usage
Checking Lock Status
use Pidlock;
Error Handling
use ;
Safe Cleanup with RAII
use Pidlock;
Migration from 0.1.x
If you're upgrading from version 0.1.x:
- Replace
Pidlock::new()
withPidlock::new_validated()
for better error handling - Handle the additional
Result
type returned bynew_validated()
- Consider the improved error types for more specific error handling
// Old (0.1.x)
let mut lock = new;
// New (0.2.x)
let mut lock = new_validated?;
Platform Considerations
- Unix/Linux: Uses POSIX signals for process detection, respects umask for permissions
- Windows: Uses Win32 APIs for process detection, handles reserved filenames
- File permissions: Lock files are created with restrictive permissions (600 on Unix)
- Path validation: Automatically validates paths for cross-platform compatibility
Safety
This library uses unsafe code for platform-specific process detection, but all unsafe operations are carefully validated and documented. The library ensures that:
- PID values are validated before use in system calls
- Windows handles are properly managed and cleaned up
- Unix signals are used safely without affecting target processes
License
pidlock is licensed under the MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)