Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ categories = ['development-tools']
[dependencies]
thiserror = '2.0'

[target.'cfg(target_os = "windows")'.dependencies]
[target.'cfg(windows)'.dependencies]
windows = { version = '0.62', features = [
'Win32_Foundation',
'Win32_Security',
'Win32_System_Memory',
'Win32_System_Threading',
] }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
[target.'cfg(unix)'.dependencies]
nix = { version = '0.30', features = ['fs', 'signal'] }

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ When subseqent process joins, if user chooses to favor the new process, current

On Windows, an unnamed file mapping is created on the [`Global\` namespace](https://learn.microsoft.com/en-us/windows/win32/termserv/kernel-object-namespaces). It is then memory mapped and used to hold the distinguished process ID. The handle of the file mapping is held as long as the `SingletonProcess` object is alive. The terminated process exit code is set to 0.

On Linux, a lock file, created in the temp directory, is used to hold the process ID. The lock is held in sync with the object lifetime.
On Unix platforms (Linux, Android, and macOS), a lock file, created in the temp directory, is used to hold the process ID. The lock is held in sync with the object lifetime.

In both platforms, since the mechanism is tied to kernel objects (file mapping handle on Windows, file lock on Linux), and these kernel objects are automatically released upon process termination, it is resilient to process crash.
In both platform families, since the mechanism is tied to kernel objects (file mapping handle on Windows, file lock on Unix), and these kernel objects are automatically released upon process termination, it is resilient to process crash.

### Platform-specific group name requirement

* Windows: The name can contain any character except the backslash character (\\), with no maximum length.
* Linux: Any name satisfying the underlying file system's requirement.
* Unix (Linux, Android, and macOS): Any name satisfying the underlying file system's requirement.

### Minimum Supported Rust Version

Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ pub enum SingletonProcessError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),

#[cfg(target_os = "windows")]
#[cfg(windows)]
#[error("Windows error: {0}")]
Windows(windows::core::Error),

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(unix)]
#[error("POSIX error: {0}")]
Posix(#[from] nix::errno::Errno),
}

type Result<T> = std::result::Result<T, SingletonProcessError>;

#[cfg(target_os = "windows")]
#[cfg(windows)]
mod inner {
use std::env::current_exe;
use std::mem::size_of_val;
Expand Down Expand Up @@ -77,7 +77,7 @@ mod inner {
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(unix)]
mod inner {
use std::env::{current_exe, temp_dir};
use std::fs::{File, OpenOptions};
Expand Down Expand Up @@ -209,7 +209,7 @@ mod tests {
assert!(get_parent_process_exe(&mut system).is_none());
} else {
// make process exit with code 0 on SIGTERM to avoid test failure
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(unix)]
{
use nix::sys::signal::*;

Expand Down