| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#![allow(clippy::expect_used)] |
| 15 |
#![allow(clippy::unwrap_used)] |
| 16 |
#![allow(clippy::panic)] |
| 17 |
#![allow(clippy::match_wild_err_arm)] |
| 18 |
#![allow(clippy::similar_names)] |
| 19 |
#![allow(clippy::uninlined_format_args)] |
| 20 |
#![allow(clippy::indexing_slicing)] |
| 21 |
|
| 22 |
mod common; |
| 23 |
|
| 24 |
use std::{env, fs, process::exit, thread, time::Duration}; |
| 25 |
|
| 26 |
use fork::{Fork, chdir, fork, getpgrp, getpid, setsid, waitpid}; |
| 27 |
|
| 28 |
use common::{get_test_dir, get_unique_test_dir, setup_test_dir, wait_for_file}; |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_double_fork_daemon_pattern() { |
| 32 |
let test_dir = setup_test_dir(get_unique_test_dir("int_double_fork")); |
| 33 |
let daemon_pid_file = test_dir.join("daemon.pid"); |
| 34 |
|
| 35 |
|
| 36 |
match fork().expect("First fork failed") { |
| 37 |
Fork::Parent(_child) => { |
| 38 |
|
| 39 |
|
| 40 |
assert!( |
| 41 |
wait_for_file(&daemon_pid_file, 3000), |
| 42 |
"Daemon PID file should exist" |
| 43 |
); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
let pid_str = fs::read_to_string(&daemon_pid_file).expect("Failed to read PID file"); |
| 55 |
let daemon_pid: i32 = pid_str.trim().parse().expect("Failed to parse daemon PID"); |
| 56 |
assert!(daemon_pid > 0, "Daemon PID should be positive"); |
| 57 |
|
| 58 |
|
| 59 |
fs::remove_file(&daemon_pid_file).ok(); |
| 60 |
} |
| 61 |
Fork::Child => { |
| 62 |
|
| 63 |
setsid().expect("setsid failed"); |
| 64 |
|
| 65 |
|
| 66 |
match fork().expect("Second fork failed") { |
| 67 |
Fork::Parent(_) => { |
| 68 |
|
| 69 |
exit(0); |
| 70 |
} |
| 71 |
Fork::Child => { |
| 72 |
|
| 73 |
let pid = getpid(); |
| 74 |
let pgid = getpgrp(); |
| 75 |
|
| 76 |
|
| 77 |
fs::write(&daemon_pid_file, format!("{}", pid)) |
| 78 |
.expect("Failed to write daemon PID"); |
| 79 |
|
| 80 |
|
| 81 |
assert!(pgid > 0); |
| 82 |
|
| 83 |
exit(0); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_setsid_creates_new_session() { |
| 92 |
let test_dir = setup_test_dir(get_unique_test_dir("int_setsid")); |
| 93 |
let session_file = test_dir.join("session.info"); |
| 94 |
|
| 95 |
match fork().expect("Fork failed") { |
| 96 |
Fork::Parent(_child) => { |
| 97 |
thread::sleep(Duration::from_millis(50)); |
| 98 |
|
| 99 |
let content = fs::read_to_string(&session_file).expect("Failed to read session file"); |
| 100 |
let parts: Vec<&str> = content.trim().split(',').collect(); |
| 101 |
|
| 102 |
let sid: i32 = parts[0].parse().expect("Failed to parse SID"); |
| 103 |
let pid: i32 = parts[1].parse().expect("Failed to parse PID"); |
| 104 |
let pgid: i32 = parts[2].parse().expect("Failed to parse PGID"); |
| 105 |
|
| 106 |
|
| 107 |
assert_eq!(pid, pgid, "Process should be session leader"); |
| 108 |
assert_eq!(sid, pid, "SID should equal PID for session leader"); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
fs::remove_file(&session_file).ok(); |
| 119 |
} |
| 120 |
Fork::Child => { |
| 121 |
|
| 122 |
let sid = setsid().expect("setsid failed"); |
| 123 |
let pid = unsafe { libc::getpid() }; |
| 124 |
let pgid = getpgrp(); |
| 125 |
|
| 126 |
fs::write(&session_file, format!("{},{},{}", sid, pid, pgid)) |
| 127 |
.expect("Failed to write session info"); |
| 128 |
|
| 129 |
exit(0); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn test_chdir_changes_directory() { |
| 136 |
let test_dir = setup_test_dir(get_test_dir("int_chdir")); |
| 137 |
let dir_file = test_dir.join("directory.info"); |
| 138 |
|
| 139 |
match fork().expect("Fork failed") { |
| 140 |
Fork::Parent(_child) => { |
| 141 |
thread::sleep(Duration::from_millis(50)); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
let content = fs::read_to_string(&dir_file).expect("Failed to read dir file"); |
| 151 |
assert_eq!(content.trim(), "/", "Directory should be root"); |
| 152 |
|
| 153 |
|
| 154 |
fs::remove_file(&dir_file).ok(); |
| 155 |
} |
| 156 |
Fork::Child => { |
| 157 |
|
| 158 |
chdir().expect("chdir failed"); |
| 159 |
|
| 160 |
let current = env::current_dir().expect("Failed to get current dir"); |
| 161 |
fs::write(&dir_file, current.to_str().unwrap()) |
| 162 |
.expect("Failed to write directory info"); |
| 163 |
|
| 164 |
exit(0); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
fn test_process_isolation() { |
| 178 |
let test_dir = setup_test_dir(get_test_dir("int_isolation")); |
| 179 |
let parent_file = test_dir.join("parent.txt"); |
| 180 |
let child_file = test_dir.join("child.txt"); |
| 181 |
|
| 182 |
|
| 183 |
fs::write(&parent_file, "parent data").expect("Failed to write parent file"); |
| 184 |
|
| 185 |
match fork().expect("Fork failed") { |
| 186 |
Fork::Parent(_child) => { |
| 187 |
thread::sleep(Duration::from_millis(50)); |
| 188 |
|
| 189 |
|
| 190 |
assert!(parent_file.exists(), "Parent file should exist"); |
| 191 |
|
| 192 |
|
| 193 |
assert!(child_file.exists(), "Child file should exist"); |
| 194 |
|
| 195 |
let child_content = fs::read_to_string(&child_file).expect("Failed to read child file"); |
| 196 |
assert_eq!(child_content.trim(), "child data"); |
| 197 |
|
| 198 |
|
| 199 |
fs::remove_file(&parent_file).ok(); |
| 200 |
fs::remove_file(&child_file).ok(); |
| 201 |
} |
| 202 |
Fork::Child => { |
| 203 |
|
| 204 |
assert!(parent_file.exists(), "Child should see parent file"); |
| 205 |
|
| 206 |
|
| 207 |
fs::write(&child_file, "child data").expect("Failed to write child file"); |
| 208 |
|
| 209 |
exit(0); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn test_getpgrp_returns_process_group() { |
| 223 |
match fork().expect("Fork failed") { |
| 224 |
Fork::Parent(_child) => { |
| 225 |
let parent_pgid = getpgrp(); |
| 226 |
assert!(parent_pgid > 0, "Parent PGID should be positive"); |
| 227 |
|
| 228 |
thread::sleep(Duration::from_millis(50)); |
| 229 |
} |
| 230 |
Fork::Child => { |
| 231 |
let child_pgid = getpgrp(); |
| 232 |
assert!(child_pgid > 0, "Child PGID should be positive"); |
| 233 |
|
| 234 |
exit(0); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn test_chdir_error_handling() { |
| 241 |
|
| 242 |
match fork().expect("Fork failed") { |
| 243 |
Fork::Parent(child) => { |
| 244 |
waitpid(child).expect("waitpid failed"); |
| 245 |
} |
| 246 |
Fork::Child => { |
| 247 |
|
| 248 |
let result = chdir(); |
| 249 |
assert!(result.is_ok(), "chdir to root should succeed"); |
| 250 |
|
| 251 |
|
| 252 |
let cwd = env::current_dir().expect("Failed to get current dir"); |
| 253 |
assert_eq!(cwd.to_str().unwrap(), "/", "Should be in root directory"); |
| 254 |
|
| 255 |
exit(0); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn test_chdir_returns_io_error() { |
| 262 |
|
| 263 |
match fork().expect("Fork failed") { |
| 264 |
Fork::Parent(child) => { |
| 265 |
waitpid(child).expect("waitpid failed"); |
| 266 |
} |
| 267 |
Fork::Child => { |
| 268 |
|
| 269 |
let result: std::io::Result<()> = chdir(); |
| 270 |
|
| 271 |
|
| 272 |
assert!(result.is_ok()); |
| 273 |
|
| 274 |
|
| 275 |
if let Err(e) = result { |
| 276 |
let _errno = e.raw_os_error(); |
| 277 |
let _msg = format!("{}", e); |
| 278 |
} |
| 279 |
|
| 280 |
exit(0); |
| 281 |
} |
| 282 |
} |
| 283 |
} |