Debian Codegraph @

examples/visual_fd_demo.rs

raw symbols SCIP
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 /// Visual demonstration of file descriptor reuse bug
10 ///
11 /// This creates output files showing what fd numbers files receive
12 /// Run with: cargo run --example visual_fd_demo
13 use std::{fs::File, io::Write, os::unix::io::AsRawFd, process::exit};
14
15 use fork::{Fork, close_fd, fork, redirect_stdio, waitpid};
16
17 fn main() {
18 println!("\nRunning demonstration...\n");
19
20 demo_close_fd();
21 demo_redirect_stdio();
22
23 println!("═══════════════════════════════════════════════════════════════");
24 println!("Results written to /tmp/fd_demo_*.txt");
25 println!("═══════════════════════════════════════════════════════════════\n");
26
27 // Show results
28 show_results();
29 }
30
31 fn demo_close_fd() {
32 match fork() {
33 Ok(Fork::Parent(child)) => {
34 waitpid(child).unwrap();
35 }
36 Ok(Fork::Child) => {
37 // Write to file before closing stdio (so we can see output)
38 let mut report = File::create("/tmp/fd_demo_close_fd.txt").unwrap();
39
40 writeln!(report, "SCENARIO 1: Using close_fd()").unwrap();
41 writeln!(report, "══════════════════════════════════════").unwrap();
42 writeln!(report).unwrap();
43
44 // Close stdio
45 close_fd().unwrap();
46
47 writeln!(report, "After close_fd():").unwrap();
48 writeln!(report, " fd 0, 1, 2 are now FREE").unwrap();
49 writeln!(report).unwrap();
50
51 // Open test files
52 let f1 = File::create("/tmp/fd_demo_file1.txt").unwrap();
53 let f2 = File::create("/tmp/fd_demo_file2.txt").unwrap();
54 let f3 = File::create("/tmp/fd_demo_file3.txt").unwrap();
55
56 writeln!(report, "Opened files:").unwrap();
57 writeln!(
58 report,
59 " file1.txt → fd {} ⚠️ BUG: Reused stdin!",
60 f1.as_raw_fd()
61 )
62 .unwrap();
63 writeln!(
64 report,
65 " file2.txt → fd {} ⚠️ BUG: Reused stdout!",
66 f2.as_raw_fd()
67 )
68 .unwrap();
69 writeln!(
70 report,
71 " file3.txt → fd {} ⚠️ BUG: Reused stderr!",
72 f3.as_raw_fd()
73 )
74 .unwrap();
75 writeln!(report).unwrap();
76
77 writeln!(report, "DANGER:").unwrap();
78 writeln!(report, " - println!() would write to file2.txt").unwrap();
79 writeln!(report, " - panic!() would write to file3.txt").unwrap();
80 writeln!(report, " - Silent file corruption!").unwrap();
81
82 exit(0);
83 }
84 Err(_) => panic!("Fork failed"),
85 }
86 }
87
88 fn demo_redirect_stdio() {
89 match fork() {
90 Ok(Fork::Parent(child)) => {
91 waitpid(child).unwrap();
92 }
93 Ok(Fork::Child) => {
94 // Write to file before redirecting (so we can see output)
95 let mut report = File::create("/tmp/fd_demo_redirect_stdio.txt").unwrap();
96
97 writeln!(report, "SCENARIO 2: Using redirect_stdio()").unwrap();
98 writeln!(report, "══════════════════════════════════════").unwrap();
99 writeln!(report).unwrap();
100
101 // Redirect stdio to /dev/null
102 redirect_stdio().unwrap();
103
104 writeln!(report, "After redirect_stdio():").unwrap();
105 writeln!(report, " fd 0 → /dev/null").unwrap();
106 writeln!(report, " fd 1 → /dev/null").unwrap();
107 writeln!(report, " fd 2 → /dev/null").unwrap();
108 writeln!(report, " (fds 0,1,2 remain OCCUPIED)").unwrap();
109 writeln!(report).unwrap();
110
111 // Open test files
112 let f1 = File::create("/tmp/fd_demo_file1_safe.txt").unwrap();
113 let f2 = File::create("/tmp/fd_demo_file2_safe.txt").unwrap();
114 let f3 = File::create("/tmp/fd_demo_file3_safe.txt").unwrap();
115
116 writeln!(report, "Opened files:").unwrap();
117 writeln!(
118 report,
119 " file1.txt → fd {} ✅ SAFE: Higher fd!",
120 f1.as_raw_fd()
121 )
122 .unwrap();
123 writeln!(
124 report,
125 " file2.txt → fd {} ✅ SAFE: Higher fd!",
126 f2.as_raw_fd()
127 )
128 .unwrap();
129 writeln!(
130 report,
131 " file3.txt → fd {} ✅ SAFE: Higher fd!",
132 f3.as_raw_fd()
133 )
134 .unwrap();
135 writeln!(report).unwrap();
136
137 writeln!(report, "SAFETY:").unwrap();
138 writeln!(report, " - println!() goes to /dev/null (discarded)").unwrap();
139 writeln!(report, " - panic!() goes to /dev/null (discarded)").unwrap();
140 writeln!(report, " - Files are protected!").unwrap();
141
142 exit(0);
143 }
144 Err(_) => panic!("Fork failed"),
145 }
146 }
147
148 fn show_results() {
149 println!("BUG (close_fd):");
150 println!("───────────────────────────────────────────────────────────────");
151 if let Ok(content) = std::fs::read_to_string("/tmp/fd_demo_close_fd.txt") {
152 print!("{}", content);
153 }
154
155 println!("\n");
156 println!("FIX (redirect_stdio):");
157 println!("───────────────────────────────────────────────────────────────");
158 if let Ok(content) = std::fs::read_to_string("/tmp/fd_demo_redirect_stdio.txt") {
159 print!("{}", content);
160 }
161
162 // Cleanup
163 let _ = std::fs::remove_file("/tmp/fd_demo_close_fd.txt");
164 let _ = std::fs::remove_file("/tmp/fd_demo_redirect_stdio.txt");
165 let _ = std::fs::remove_file("/tmp/fd_demo_file1.txt");
166 let _ = std::fs::remove_file("/tmp/fd_demo_file2.txt");
167 let _ = std::fs::remove_file("/tmp/fd_demo_file3.txt");
168 let _ = std::fs::remove_file("/tmp/fd_demo_file1_safe.txt");
169 let _ = std::fs::remove_file("/tmp/fd_demo_file2_safe.txt");
170 let _ = std::fs::remove_file("/tmp/fd_demo_file3_safe.txt");
171 }