| 1 | ## 0.6.0 |
| 2 | |
| 3 | ### Breaking Changes |
| 4 | * **`getpgrp()` signature changed** - Now returns `libc::pid_t` directly instead of `io::Result<libc::pid_t>` |
| 5 | - `getpgrp()` always succeeds per POSIX specification and cannot fail |
| 6 | - **Migration guide:** |
| 7 | - Change `getpgrp()?` to `getpgrp()` |
| 8 | - Change `getpgrp().expect("...")` to `getpgrp()` |
| 9 | - Change `match getpgrp() { Ok(pgid) => ... }` to `let pgid = getpgrp();` |
| 10 | - Rationale: Aligns with POSIX.1 specification and matches `getpid()`/`getppid()` patterns |
| 11 | - Verified on Linux, macOS, FreeBSD, OpenBSD per POSIX.1 specification |
| 12 | - Updated all tests and documentation to reflect this guarantee |
| 13 | |
| 14 | ### Improved |
| 15 | * **Enhanced documentation** - Comprehensive improvements to library documentation |
| 16 | - Added "Common Patterns" section with practical examples: |
| 17 | - Process supervisor using HashMap with Fork |
| 18 | - Inter-process communication via pipes |
| 19 | - Daemon with PID file creation |
| 20 | - Added "Safety and Best Practices" guidelines |
| 21 | - Added detailed "Common Pitfalls and Safety Considerations" to `fork()`: |
| 22 | - Mutexes and locks (deadlock risks) |
| 23 | - File descriptors (shared state issues) |
| 24 | - Signal handlers (inheritance behavior) |
| 25 | - Async-signal-safety between fork and exec |
| 26 | - Memory usage (copy-on-write behavior) |
| 27 | - Enhanced `Fork` enum documentation with helper method examples |
| 28 | - Added "Platform Compatibility" information |
| 29 | * **Test quality improvements** |
| 30 | - Replaced deprecated `signal()` with `sigaction()` in EINTR tests |
| 31 | - More portable signal handling for cross-platform compatibility |
| 32 | - Renamed `test_getpgrp_returns_io_error_type` to `test_getpgrp_returns_pid_type` |
| 33 | - Updated test README to reflect current test descriptions |
| 34 | |
| 35 | ### Fixed |
| 36 | * **Documentation warnings** - Resolved doctest warnings about main function wrapping |
| 37 | * **EINTR resilience** - `close_fd` and `redirect_stdio` now retry on `EINTR` for `close/open/dup2`, preventing spurious failures under signal-heavy conditions on Linux, macOS, and BSD |
| 38 | * **Daemon exit safety** - Replaced `std::process::exit` in post-fork parents with `libc::_exit` to avoid running non-async-signal-safe destructors, preventing undefined behavior between `fork()` and `exec()` |
| 39 | |
| 40 | ### Code Quality |
| 41 | * **Modernized C string handling** - Replaced runtime `CString::new()` allocations with compile-time `c""` string literals (Rust 2024 feature) |
| 42 | - `chdir()` now uses `c"/"` instead of `CString::new("/")` |
| 43 | - `redirect_stdio()` now uses `c"/dev/null"` instead of `CString::new("/dev/null")` |
| 44 | - Benefits: Eliminated dead error handling code, zero runtime overhead, compile-time validation |
| 45 | - No API changes, fully backward compatible |
| 46 | * **Enhanced code clarity** - Added clarifying comments to `redirect_stdio()` error handling logic explaining conditional cleanup of file descriptors |
| 47 | * **Comprehensive test coverage** - Added 12 dedicated tests for `chdir()` function (346 lines) |
| 48 | - Tests idempotent behavior, process isolation, concurrent usage |
| 49 | - Validates modern `c""` string literal implementation |
| 50 | - Tests integration with `setsid()` (daemon pattern) |
| 51 | - Total test count increased from 107 to 119 tests |
| 52 | |
| 53 | ## 0.5.0 |
| 54 | |
| 55 | ### Breaking Changes |
| 56 | * **`waitpid()` return type changed** - Now returns `io::Result<libc::c_int>` instead of `io::Result<()>` |
| 57 | - Returns the raw status code for inspection with `WIFEXITED`, `WEXITSTATUS`, `WIFSIGNALED`, `WTERMSIG`, etc. |
| 58 | - Migration: Change `waitpid(pid)?` to `let status = waitpid(pid)?; assert!(WIFEXITED(status));` |
| 59 | - Enables proper exit code checking and signal detection |
| 60 | - See updated examples in documentation |
| 61 | |
| 62 | ### Added |
| 63 | * **Fork helper methods** - Added convenience methods to `Fork` enum |
| 64 | - `is_parent()` - Check if this is the parent process |
| 65 | - `is_child()` - Check if this is the child process |
| 66 | - `child_pid()` - Get child PID if parent, otherwise None |
| 67 | * **Hash trait** - `Fork` now derives `Hash`, enabling use in `HashMap` and `HashSet` |
| 68 | - Useful for process supervisors and tracking multiple children |
| 69 | - Examples: `supervisor.rs` and `supervisor_advanced.rs` |
| 70 | * **must_use attributes** - Added `#[must_use]` to critical functions to prevent accidental misuse |
| 71 | - `fork()` - Must check if parent or child |
| 72 | - `daemon()` - Must check daemon result |
| 73 | - `setsid()` - Must use session ID |
| 74 | - `getpgrp()` - Must use process group ID |
| 75 | * **`waitpid_nohang()` function** - Non-blocking variant of `waitpid()` |
| 76 | - Returns `Ok(Some(status))` if child has exited |
| 77 | - Returns `Ok(None)` if child is still running |
| 78 | - Essential for process supervisors and event loops |
| 79 | - Enables polling patterns without blocking |
| 80 | - Includes 7 comprehensive tests |
| 81 | * **PID helper functions** - Convenience wrappers for getting process IDs |
| 82 | - `getpid()` - Get current process ID (always succeeds, hides unsafe) |
| 83 | - `getppid()` - Get parent process ID (always succeeds, hides unsafe) |
| 84 | * **Status macro re-exports** - Convenient access to status inspection macros |
| 85 | - Re-export `WIFEXITED`, `WEXITSTATUS`, `WIFSIGNALED`, `WTERMSIG` from libc |
| 86 | - Users can now `use fork::{waitpid, WIFEXITED, WEXITSTATUS}` instead of separate libc import |
| 87 | * **Comprehensive test suite** - Added extensive tests covering critical edge cases |
| 88 | - `tests/waitpid_tests.rs` - Exit codes, signals, error handling, and non-blocking waits |
| 89 | - `tests/error_handling_tests.rs` - Error paths and type verification |
| 90 | - `tests/pid_tests.rs` - PID helper functions (getpid, getppid) |
| 91 | - `tests/status_macro_tests.rs` - Status macro re-exports |
| 92 | |
| 93 | ### Improved |
| 94 | * **Performance** - Added `#[inline]` hints to thin wrapper functions (`chdir`, `setsid`, `getpgrp`, `getpid`, `getppid`) |
| 95 | * **Documentation** - Enhanced with comprehensive examples and safety considerations |
| 96 | - Added doc test for `setsid()` - Session creation example |
| 97 | - Added doc test for `getpgrp()` - Process group query example |
| 98 | - Added doc test for `getpid()` - Current PID example |
| 99 | - Added doc test for `getppid()` - Parent PID example |
| 100 | - Enhanced `fork()` with safety considerations (file descriptors, mutexes, async-signal-safety, signals, memory) |
| 101 | - Enhanced `waitpid()` with status inspection examples |
| 102 | - Added `waitpid_nohang()` with polling patterns and process supervisor examples |
| 103 | * **Daemon correctness** - `daemon()` now performs the full double-fork, exiting the intermediate session leader so only the daemon continues |
| 104 | - Docs clarified the numbered double-fork stages |
| 105 | - Examples updated (`example_daemon.rs`, `example_touch_pid.rs`) to reflect that only the daemon process returns `Fork::Child` |
| 106 | * **waitpid robustness** - Automatic retry on `EINTR` (signal interruption) |
| 107 | - Takes `pid_t` instead of `i32` for better type safety |
| 108 | - Returns raw status code enabling exit code inspection and signal detection |
| 109 | * **Code quality** - Simplified `daemon()` implementation using `?` operator consistently |
| 110 | * **Test coverage** - Comprehensive coverage of all error paths and edge cases |
| 111 | - Error handling: Invalid PID (ECHILD), double-wait, session leader errors (EPERM) |
| 112 | - Exit codes: 0, 1, 42, 127, 255, and multiple code variations |
| 113 | - Signal termination: SIGKILL, SIGTERM, SIGABRT detection |
| 114 | - Status inspection: WIFEXITED vs WIFSIGNALED distinction |
| 115 | - Fork helper methods: `is_parent()`, `is_child()`, `child_pid()` |
| 116 | - io::Error type verification for all functions |
| 117 | * **CI** - GitHub Actions now run tests serially (`RUST_TEST_THREADS=1`) and use the latest checkout action |
| 118 | |
| 119 | ### Examples |
| 120 | * Added `supervisor.rs` - Basic process supervisor example |
| 121 | * Added `supervisor_advanced.rs` - Production-ready supervisor with restart policies |
| 122 | |
| 123 | ### Migration Guide (0.4.x → 0.5.0) |
| 124 | |
| 125 | #### Before (0.4.x): |
| 126 | ```rust |
| 127 | match fork() { |
| 128 | Ok(Fork::Parent(child)) => { |
| 129 | waitpid(child)?; // Just waits, no status |
| 130 | } |
| 131 | Ok(Fork::Child) => exit(0), |
| 132 | Err(e) => eprintln!("Fork failed: {}", e), |
| 133 | } |
| 134 | ``` |
| 135 | |
| 136 | #### After (0.5.0): |
| 137 | ```rust |
| 138 | use libc::{WIFEXITED, WEXITSTATUS}; |
| 139 | |
| 140 | match fork() { |
| 141 | Ok(Fork::Parent(child)) => { |
| 142 | let status = waitpid(child)?; // Returns status code |
| 143 | assert!(WIFEXITED(status), "Child should exit normally"); |
| 144 | let exit_code = WEXITSTATUS(status); |
| 145 | println!("Child exited with code: {}", exit_code); |
| 146 | } |
| 147 | Ok(Fork::Child) => exit(0), |
| 148 | Err(e) => eprintln!("Fork failed: {}", e), |
| 149 | } |
| 150 | ``` |
| 151 | |
| 152 | ## 0.4.0 |
| 153 | |
| 154 | ### Breaking Changes |
| 155 | * **Improved error handling** - All functions now return `io::Result` instead of `Result<T, i32>` |
| 156 | - `fork()` now returns `io::Result<Fork>` (was `Result<Fork, i32>`) |
| 157 | - `daemon()` now returns `io::Result<Fork>` (was `Result<Fork, i32>`) |
| 158 | - `setsid()` now returns `io::Result<libc::pid_t>` (was `Result<libc::pid_t, i32>`) |
| 159 | - `getpgrp()` now returns `io::Result<libc::pid_t>` (was `Result<libc::pid_t, i32>`) |
| 160 | - `waitpid()` now returns `io::Result<()>` (was `Result<(), i32>`) |
| 161 | - `chdir()` now returns `io::Result<()>` (was `Result<libc::c_int, i32>`) |
| 162 | - `close_fd()` now returns `io::Result<()>` (was `Result<(), i32>`) |
| 163 | |
| 164 | ### Major Improvements |
| 165 | * **Fixed file descriptor reuse bug** (Issue #2) |
| 166 | - Added `redirect_stdio()` function that redirects stdio to `/dev/null` instead of closing |
| 167 | - Prevents silent file corruption when daemon opens files after stdio is closed |
| 168 | - `daemon()` now uses `redirect_stdio()` instead of `close_fd()` |
| 169 | - Matches industry standard implementations (libuv, systemd, BSD daemon(3)) |
| 170 | |
| 171 | ### Benefits |
| 172 | * **Better error diagnostics** - Errors now capture and preserve `errno` values |
| 173 | * **Rich error messages** - Error display shows descriptive text (e.g., "Permission denied") instead of `-1` |
| 174 | * **Rust idioms** - Integrates seamlessly with `?` operator, `anyhow`, `thiserror`, and other error handling crates |
| 175 | * **Type safety** - Can match on `ErrorKind` variants for specific error handling |
| 176 | * **Debugging** - `.raw_os_error()` provides access to underlying errno when needed |
| 177 | * **Correctness** - No more file descriptor reuse bugs that could corrupt data files |
| 178 | |
| 179 | ### Added |
| 180 | * `Fork` enum now derives `Debug`, `Clone`, `Copy`, `PartialEq`, `Eq` for better usability |
| 181 | * `redirect_stdio()` function - Safer alternative to `close_fd()` |
| 182 | * Comprehensive tests for stdio redirection (`tests/stdio_redirect_tests.rs`) |
| 183 | - Test demonstrating the fd reuse bug with `close_fd()` |
| 184 | - Tests verifying `redirect_stdio()` prevents fd reuse |
| 185 | - Tests confirming `daemon()` uses correct behavior |
| 186 | |
| 187 | ### Improved |
| 188 | * Simplified `close_fd()` implementation using iterator pattern |
| 189 | * Enhanced documentation with detailed error descriptions for all functions |
| 190 | * Updated all examples to use proper error handling patterns |
| 191 | * Added warnings to `close_fd()` documentation about fd reuse risks |
| 192 | |
| 193 | ### Security |
| 194 | * **CRITICAL FIX**: `daemon()` no longer vulnerable to file descriptor reuse bugs |
| 195 | - Previously, files opened after `daemon(false, false)` could get fd 0, 1, or 2 |
| 196 | - Any `println!`, `eprintln!`, or panic would write to those files, corrupting them |
| 197 | - Now stdio is redirected to `/dev/null`, keeping fd 0,1,2 occupied |
| 198 | - New files always get fd >= 3 |
| 199 | |
| 200 | ## 0.3.1 |
| 201 | * Added comprehensive test coverage for `getpgrp()` function |
| 202 | - Unit tests in `src/lib.rs` (`test_getpgrp`, `test_getpgrp_in_parent`) |
| 203 | - Integration test `test_getpgrp_returns_process_group` in `tests/integration_tests.rs` |
| 204 | * Added `coverage` recipe to `.justfile` for generating coverage reports with grcov |
| 205 | |
| 206 | ## 0.3.0 |
| 207 | |
| 208 | ### Changed |
| 209 | * Updated Rust edition from 2021 to 2024 |
| 210 | * Applied edition 2024 formatting standards (alphabetical import ordering) |
| 211 | |
| 212 | ### Added |
| 213 | * **Integration tests directory** - Added `tests/` directory with comprehensive integration tests |
| 214 | - `daemon_tests.rs` - 5 tests for daemon functionality (detached process, nochdir, process groups, command execution, no controlling terminal) |
| 215 | - `fork_tests.rs` - 7 tests for fork functionality (basic fork, parent-child communication, multiple children, environment inheritance, command execution, different PIDs, waitpid) |
| 216 | - `integration_tests.rs` - 5 tests for advanced patterns (double-fork daemon, setsid, chdir, process isolation, getpgrp) |
| 217 | |
| 218 | ### Improved |
| 219 | * Significantly expanded test coverage from 1 to 13 comprehensive unit tests |
| 220 | * Added tests for all public API functions: |
| 221 | - `fork()` - Multiple test scenarios including child execution |
| 222 | - `daemon()` - Daemon pattern tested (double-fork with setsid) |
| 223 | - `waitpid()` - Proper parent-child synchronization |
| 224 | - `setsid()` - Session management and verification |
| 225 | - `getpgrp()` - Process group queries |
| 226 | - `chdir()` - Directory changes with verification |
| 227 | - `close_fd()` - File descriptor management |
| 228 | * Added real-world usage pattern tests: |
| 229 | - Classic double-fork daemon pattern |
| 230 | - Multiple sequential forks |
| 231 | - Command execution in child processes |
| 232 | * Improved test quality with proper cleanup and zombie process prevention |
| 233 | * Enhanced CI/CD integration with LLVM coverage instrumentation |
| 234 | * **Total test count: 35 tests** (13 unit + 17 integration + 5 doc tests) |
| 235 | |
| 236 | ### Fixed |
| 237 | * Daemon tests now properly test the daemon pattern without calling `daemon()` directly |
| 238 | (which would call `exit(0)` and terminate the test runner) |
| 239 | |
| 240 | ### Updated |
| 241 | * GitHub Actions: codecov/codecov-action from v4 to v5 |
| 242 | |
| 243 | ## 0.2.0 |
| 244 | * Added waitpid(pid: i32) |