| 1 |
#![allow(clippy::expect_used)] |
| 2 |
#![allow(clippy::unwrap_used)] |
| 3 |
#![allow(clippy::panic)] |
| 4 |
#![allow(clippy::uninlined_format_args)] |
| 5 |
#![allow(clippy::match_wild_err_arm)] |
| 6 |
#![allow(clippy::cast_ptr_alignment)] |
| 7 |
#![allow(clippy::doc_markdown)] |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use std::{fs::File, os::unix::io::AsRawFd, process::exit}; |
| 13 |
|
| 14 |
use fork::{Fork, close_fd, fork, redirect_stdio, waitpid}; |
| 15 |
|
| 16 |
fn main() { |
| 17 |
println!("\n═══════════════════════════════════════════════════════════════"); |
| 18 |
println!("DEMONSTRATION: File Descriptor Numbers"); |
| 19 |
println!("═══════════════════════════════════════════════════════════════\n"); |
| 20 |
|
| 21 |
println!("Scenario 1: Using close_fd() - THE BUG"); |
| 22 |
println!("───────────────────────────────────────────────────────────────"); |
| 23 |
|
| 24 |
match fork() { |
| 25 |
Ok(Fork::Parent(child)) => { |
| 26 |
waitpid(child).unwrap(); |
| 27 |
} |
| 28 |
Ok(Fork::Child) => { |
| 29 |
println!("\nBefore close_fd():"); |
| 30 |
println!(" stdin = fd 0"); |
| 31 |
println!(" stdout = fd 1"); |
| 32 |
println!(" stderr = fd 2"); |
| 33 |
|
| 34 |
|
| 35 |
close_fd().unwrap(); |
| 36 |
|
| 37 |
println!("\nAfter close_fd():"); |
| 38 |
println!(" fd 0, 1, 2 are now FREE!\n"); |
| 39 |
|
| 40 |
|
| 41 |
let f1 = File::create("/tmp/test1.txt").unwrap(); |
| 42 |
let f2 = File::create("/tmp/test2.txt").unwrap(); |
| 43 |
let f3 = File::create("/tmp/test3.txt").unwrap(); |
| 44 |
|
| 45 |
println!("Opening files:"); |
| 46 |
println!(" test1.txt got fd = {} ⚠️ (was stdin!)", f1.as_raw_fd()); |
| 47 |
println!(" test2.txt got fd = {} ⚠️ (was stdout!)", f2.as_raw_fd()); |
| 48 |
println!(" test3.txt got fd = {} ⚠️ (was stderr!)", f3.as_raw_fd()); |
| 49 |
|
| 50 |
println!("\nPROBLEM:"); |
| 51 |
println!(" If code does println!() → writes to test2.txt!"); |
| 52 |
println!(" If code panics → panic message goes to test3.txt!"); |
| 53 |
println!(" → SILENT FILE CORRUPTION!\n"); |
| 54 |
|
| 55 |
|
| 56 |
drop(f1); |
| 57 |
drop(f2); |
| 58 |
drop(f3); |
| 59 |
let _ = std::fs::remove_file("/tmp/test1.txt"); |
| 60 |
let _ = std::fs::remove_file("/tmp/test2.txt"); |
| 61 |
let _ = std::fs::remove_file("/tmp/test3.txt"); |
| 62 |
|
| 63 |
exit(0); |
| 64 |
} |
| 65 |
Err(_) => panic!("Fork failed"), |
| 66 |
} |
| 67 |
|
| 68 |
println!("\n═══════════════════════════════════════════════════════════════"); |
| 69 |
println!("Scenario 2: Using redirect_stdio() - THE FIX"); |
| 70 |
println!("───────────────────────────────────────────────────────────────"); |
| 71 |
|
| 72 |
match fork() { |
| 73 |
Ok(Fork::Parent(child)) => { |
| 74 |
waitpid(child).unwrap(); |
| 75 |
} |
| 76 |
Ok(Fork::Child) => { |
| 77 |
println!("\nBefore redirect_stdio():"); |
| 78 |
println!(" stdin = fd 0"); |
| 79 |
println!(" stdout = fd 1"); |
| 80 |
println!(" stderr = fd 2"); |
| 81 |
|
| 82 |
|
| 83 |
redirect_stdio().unwrap(); |
| 84 |
|
| 85 |
println!("\nAfter redirect_stdio():"); |
| 86 |
println!(" fd 0 → /dev/null"); |
| 87 |
println!(" fd 1 → /dev/null"); |
| 88 |
println!(" fd 2 → /dev/null"); |
| 89 |
println!(" (fds 0,1,2 remain OCCUPIED!)\n"); |
| 90 |
|
| 91 |
|
| 92 |
let f1 = File::create("/tmp/test1.txt").unwrap(); |
| 93 |
let f2 = File::create("/tmp/test2.txt").unwrap(); |
| 94 |
let f3 = File::create("/tmp/test3.txt").unwrap(); |
| 95 |
|
| 96 |
println!("Opening files:"); |
| 97 |
println!(" test1.txt got fd = {} ✅ (safe!)", f1.as_raw_fd()); |
| 98 |
println!(" test2.txt got fd = {} ✅ (safe!)", f2.as_raw_fd()); |
| 99 |
println!(" test3.txt got fd = {} ✅ (safe!)", f3.as_raw_fd()); |
| 100 |
|
| 101 |
println!("\nSOLUTION:"); |
| 102 |
println!(" If code does println!() → goes to /dev/null (discarded)"); |
| 103 |
println!(" If code panics → panic message goes to /dev/null"); |
| 104 |
println!(" → Files are SAFE!\n"); |
| 105 |
|
| 106 |
|
| 107 |
drop(f1); |
| 108 |
drop(f2); |
| 109 |
drop(f3); |
| 110 |
let _ = std::fs::remove_file("/tmp/test1.txt"); |
| 111 |
let _ = std::fs::remove_file("/tmp/test2.txt"); |
| 112 |
let _ = std::fs::remove_file("/tmp/test3.txt"); |
| 113 |
|
| 114 |
exit(0); |
| 115 |
} |
| 116 |
Err(_) => panic!("Fork failed"), |
| 117 |
} |
| 118 |
|
| 119 |
println!("\n═══════════════════════════════════════════════════════════════"); |
| 120 |
println!("KEY INSIGHT:"); |
| 121 |
println!("───────────────────────────────────────────────────────────────"); |
| 122 |
println!("The OS kernel ALWAYS allocates the LOWEST available fd number."); |
| 123 |
println!(); |
| 124 |
println!("close_fd(): Frees 0,1,2 → next open() returns 0,1,2"); |
| 125 |
println!("redirect_stdio(): Keeps 0,1,2 busy → next open() returns 3,4,5"); |
| 126 |
println!("═══════════════════════════════════════════════════════════════\n"); |
| 127 |
} |