| 1 |
use super::*; |
| 2 |
use rustix::process::Resource; |
| 3 |
use std::convert::TryInto; |
| 4 |
|
| 5 |
#[track_caller] |
| 6 |
fn check_unwrap<T>(prc: &Process, val: ProcResult<T>) -> Option<T> { |
| 7 |
match val { |
| 8 |
Ok(t) => Some(t), |
| 9 |
Err(ProcError::PermissionDenied(_)) if !rustix::process::geteuid().is_root() => { |
| 10 |
// we are not root, and so a permission denied error is OK |
| 11 |
None |
| 12 |
} |
| 13 |
Err(ProcError::NotFound(path)) => { |
| 14 |
// a common reason for this error is that the process isn't running anymore |
| 15 |
if prc.is_alive() { |
| 16 |
panic!("{:?} not found", path) |
| 17 |
} |
| 18 |
None |
| 19 |
} |
| 20 |
Err(err) => panic!("check_unwrap error for {} {:?}", prc.pid, err), |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
fn check_unwrap_task<T>(prc: &Process, val: ProcResult<T>) -> Option<T> { |
| 25 |
match val { |
| 26 |
Ok(t) => Some(t), |
| 27 |
Err(ProcError::PermissionDenied(_)) if !rustix::process::geteuid().is_root() => { |
| 28 |
// we are not root, and so a permission denied error is OK |
| 29 |
None |
| 30 |
} |
| 31 |
Err(ProcError::NotFound(_path)) => { |
| 32 |
// tasks can be more short-lived thanks processes, and it seems that accessing |
| 33 |
// the /status and /stat files for tasks is quite unreliable |
| 34 |
None |
| 35 |
} |
| 36 |
Err(err) => panic!("check_unwrap error for {} {:?}", prc.pid, err), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn test_main_thread_task() { |
| 42 |
let myself = Process::myself().unwrap(); |
| 43 |
let task = myself.task_main_thread().unwrap(); |
| 44 |
check_unwrap(&myself, task.stat()); |
| 45 |
} |
| 46 |
|
| 47 |
#[allow(clippy::cognitive_complexity)] |
| 48 |
#[test] |
| 49 |
fn test_self_proc() { |
| 50 |
let myself = Process::myself().unwrap().stat().unwrap(); |
| 51 |
println!("{:#?}", myself); |
| 52 |
println!("state: {:?}", myself.state()); |
| 53 |
println!("tty: {:?}", myself.tty_nr()); |
| 54 |
println!("flags: {:?}", myself.flags()); |
| 55 |
|
| 56 |
#[cfg(feature = "chrono")] |
| 57 |
println!("starttime: {:#?}", myself.starttime().get()); |
| 58 |
|
| 59 |
let kernel = KernelVersion::current().unwrap(); |
| 60 |
|
| 61 |
if kernel >= KernelVersion::new(2, 1, 22) { |
| 62 |
assert!(myself.exit_signal.is_some()); |
| 63 |
} else { |
| 64 |
assert!(myself.exit_signal.is_none()); |
| 65 |
} |
| 66 |
|
| 67 |
if kernel >= KernelVersion::new(2, 2, 8) { |
| 68 |
assert!(myself.processor.is_some()); |
| 69 |
} else { |
| 70 |
assert!(myself.processor.is_none()); |
| 71 |
} |
| 72 |
|
| 73 |
if kernel >= KernelVersion::new(2, 5, 19) { |
| 74 |
assert!(myself.rt_priority.is_some()); |
| 75 |
} else { |
| 76 |
assert!(myself.rt_priority.is_none()); |
| 77 |
} |
| 78 |
|
| 79 |
if kernel >= KernelVersion::new(2, 5, 19) { |
| 80 |
assert!(myself.rt_priority.is_some()); |
| 81 |
assert!(myself.policy.is_some()); |
| 82 |
} else { |
| 83 |
assert!(myself.rt_priority.is_none()); |
| 84 |
assert!(myself.policy.is_none()); |
| 85 |
} |
| 86 |
|
| 87 |
if kernel >= KernelVersion::new(2, 6, 18) { |
| 88 |
assert!(myself.delayacct_blkio_ticks.is_some()); |
| 89 |
} else { |
| 90 |
assert!(myself.delayacct_blkio_ticks.is_none()); |
| 91 |
} |
| 92 |
|
| 93 |
if kernel >= KernelVersion::new(2, 6, 24) { |
| 94 |
assert!(myself.guest_time.is_some()); |
| 95 |
assert!(myself.cguest_time.is_some()); |
| 96 |
} else { |
| 97 |
assert!(myself.guest_time.is_none()); |
| 98 |
assert!(myself.cguest_time.is_none()); |
| 99 |
} |
| 100 |
|
| 101 |
if kernel >= KernelVersion::new(3, 3, 0) { |
| 102 |
assert!(myself.start_data.is_some()); |
| 103 |
assert!(myself.end_data.is_some()); |
| 104 |
assert!(myself.start_brk.is_some()); |
| 105 |
} else { |
| 106 |
assert!(myself.start_data.is_none()); |
| 107 |
assert!(myself.end_data.is_none()); |
| 108 |
assert!(myself.start_brk.is_none()); |
| 109 |
} |
| 110 |
|
| 111 |
if kernel >= KernelVersion::new(3, 5, 0) { |
| 112 |
assert!(myself.arg_start.is_some()); |
| 113 |
assert!(myself.arg_end.is_some()); |
| 114 |
assert!(myself.env_start.is_some()); |
| 115 |
assert!(myself.env_end.is_some()); |
| 116 |
assert!(myself.exit_code.is_some()); |
| 117 |
} else { |
| 118 |
assert!(myself.arg_start.is_none()); |
| 119 |
assert!(myself.arg_end.is_none()); |
| 120 |
assert!(myself.env_start.is_none()); |
| 121 |
assert!(myself.env_end.is_none()); |
| 122 |
assert!(myself.exit_code.is_none()); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_all() { |
| 128 |
let is_wsl2 = KernelConfig::current() |
| 129 |
.ok() |
| 130 |
.and_then(|KernelConfig(cfg)| { |
| 131 |
cfg.get("CONFIG_LOCALVERSION").and_then(|ver| { |
| 132 |
if let ConfigSetting::Value(s) = ver { |
| 133 |
Some(s == "\"-microsoft-standard\"") |
| 134 |
} else { |
| 135 |
None |
| 136 |
} |
| 137 |
}) |
| 138 |
}) |
| 139 |
.unwrap_or(false); |
| 140 |
for p in all_processes().unwrap() { |
| 141 |
// note: this test doesn't unwrap, since some of this data requires root to access |
| 142 |
// so permission denied errors are common. The check_unwrap helper function handles |
| 143 |
// this. |
| 144 |
|
| 145 |
let prc = p.unwrap(); |
| 146 |
let stat = prc.stat().unwrap(); |
| 147 |
println!("{} {}", prc.pid(), stat.comm); |
| 148 |
stat.flags().unwrap(); |
| 149 |
stat.state().unwrap(); |
| 150 |
#[cfg(feature = "chrono")] |
| 151 |
stat.starttime().get().unwrap(); |
| 152 |
|
| 153 |
// if this process is defunct/zombie, don't try to read any of the below data |
| 154 |
// (some might be successful, but not all) |
| 155 |
if stat.state().unwrap() == ProcState::Zombie { |
| 156 |
continue; |
| 157 |
} |
| 158 |
|
| 159 |
check_unwrap(&prc, prc.cmdline()); |
| 160 |
check_unwrap(&prc, prc.environ()); |
| 161 |
check_unwrap(&prc, prc.fd()); |
| 162 |
check_unwrap(&prc, prc.io()); |
| 163 |
check_unwrap(&prc, prc.maps()); |
| 164 |
check_unwrap(&prc, prc.coredump_filter()); |
| 165 |
// The WSL2 kernel doesn't have autogroup, even though this should be present since linux |
| 166 |
// 2.6.36 |
| 167 |
if is_wsl2 { |
| 168 |
assert!(prc.autogroup().is_err()); |
| 169 |
} else { |
| 170 |
check_unwrap(&prc, prc.autogroup()); |
| 171 |
} |
| 172 |
check_unwrap(&prc, prc.auxv()); |
| 173 |
check_unwrap(&prc, prc.cgroups()); |
| 174 |
check_unwrap(&prc, prc.wchan()); |
| 175 |
check_unwrap(&prc, prc.status()); |
| 176 |
check_unwrap(&prc, prc.mountinfo()); |
| 177 |
check_unwrap(&prc, prc.mountstats()); |
| 178 |
check_unwrap(&prc, prc.oom_score()); |
| 179 |
if let Some(oom_score_adj) = check_unwrap(&prc, prc.oom_score_adj()) { |
| 180 |
assert!(oom_score_adj >= -1000 && oom_score_adj <= 1000); |
| 181 |
check_unwrap(&prc, prc.set_oom_score_adj(oom_score_adj)); |
| 182 |
} |
| 183 |
|
| 184 |
if let Some(tasks) = check_unwrap(&prc, prc.tasks()) { |
| 185 |
for task in tasks { |
| 186 |
let task = task.unwrap(); |
| 187 |
check_unwrap_task(&prc, task.stat()); |
| 188 |
check_unwrap_task(&prc, task.status()); |
| 189 |
check_unwrap_task(&prc, task.io()); |
| 190 |
check_unwrap_task(&prc, task.schedstat()); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
#[test] |
| 197 |
fn test_smaps() { |
| 198 |
let me = Process::myself().unwrap(); |
| 199 |
let smaps = match me.smaps() { |
| 200 |
Ok(x) => x, |
| 201 |
Err(ProcError::NotFound(_)) => { |
| 202 |
// ignored because not all kernerls have smaps |
| 203 |
return; |
| 204 |
} |
| 205 |
Err(e) => panic!("{}", e), |
| 206 |
}; |
| 207 |
println!("{:#?}", smaps); |
| 208 |
} |
| 209 |
|
| 210 |
#[test] |
| 211 |
fn test_smaps_rollup() { |
| 212 |
let me = Process::myself().unwrap(); |
| 213 |
let smaps_rollup = match me.smaps_rollup() { |
| 214 |
Ok(x) => x, |
| 215 |
Err(ProcError::NotFound(_)) => { |
| 216 |
// ignored because not all kernerls have smaps_rollup |
| 217 |
return; |
| 218 |
} |
| 219 |
Err(e) => panic!("{}", e), |
| 220 |
}; |
| 221 |
println!("{:#?}", smaps_rollup); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn test_proc_alive() { |
| 226 |
let myself = Process::myself().unwrap(); |
| 227 |
assert!(myself.is_alive()); |
| 228 |
|
| 229 |
// zombies should not be considered alive |
| 230 |
let mut command = std::process::Command::new("sleep"); |
| 231 |
command |
| 232 |
.arg("0") |
| 233 |
.stdout(std::process::Stdio::null()) |
| 234 |
.stderr(std::process::Stdio::null()); |
| 235 |
let mut child = command.spawn().unwrap(); |
| 236 |
let child_pid = child.id() as i32; |
| 237 |
|
| 238 |
// sleep very briefly to allow the child to start and then exit |
| 239 |
std::thread::sleep(std::time::Duration::from_millis(30)); |
| 240 |
|
| 241 |
let child_proc = Process::new(child_pid).unwrap(); |
| 242 |
assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat()); |
| 243 |
assert!(child_proc.stat().unwrap().state().unwrap() == ProcState::Zombie); |
| 244 |
child.wait().unwrap(); |
| 245 |
assert!(Process::new(child_pid).is_err()); |
| 246 |
assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat()); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn test_proc_environ() { |
| 251 |
let myself = Process::myself().unwrap(); |
| 252 |
let proc_environ = myself.environ().unwrap(); |
| 253 |
|
| 254 |
let std_environ: HashMap<_, _> = std::env::vars_os().collect(); |
| 255 |
assert_eq!(proc_environ, std_environ); |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn test_error_handling() { |
| 260 |
// getting the proc struct should be OK |
| 261 |
let init = Process::new(1).unwrap(); |
| 262 |
|
| 263 |
let i_have_access = rustix::process::geteuid().as_raw() == init.uid().unwrap(); |
| 264 |
|
| 265 |
if !i_have_access { |
| 266 |
// but accessing data should result in an error (unless we are running as root!) |
| 267 |
assert!(init.cwd().is_err()); |
| 268 |
assert!(init.environ().is_err()); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn test_proc_exe() { |
| 274 |
let myself = Process::myself().unwrap(); |
| 275 |
let proc_exe = myself.exe().unwrap(); |
| 276 |
let std_exe = std::env::current_exe().unwrap(); |
| 277 |
assert_eq!(proc_exe, std_exe); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn test_proc_io() { |
| 282 |
let myself = Process::myself().unwrap(); |
| 283 |
let kernel = KernelVersion::current().unwrap(); |
| 284 |
let io = myself.io(); |
| 285 |
println!("{:?}", io); |
| 286 |
if io.is_ok() { |
| 287 |
assert!(kernel >= KernelVersion::new(2, 6, 20)); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn test_proc_maps() { |
| 293 |
let myself = Process::myself().unwrap(); |
| 294 |
let maps = myself.maps().unwrap(); |
| 295 |
for map in maps { |
| 296 |
println!("{:?}", map); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
#[test] |
| 301 |
fn test_proc_pagemap() { |
| 302 |
let myself = Process::myself().unwrap(); |
| 303 |
let maps = myself.maps().unwrap(); |
| 304 |
|
| 305 |
let stack_map = maps.iter().find(|m| matches!(m.pathname, MMapPath::Stack)).unwrap(); |
| 306 |
let page_size = crate::page_size() as usize; |
| 307 |
let start_page = stack_map.address.0 as usize / page_size; |
| 308 |
let end_page = stack_map.address.1 as usize / page_size; |
| 309 |
|
| 310 |
let mut pagemap = myself.pagemap().unwrap(); |
| 311 |
let page_infos = pagemap.get_range_info(start_page..end_page).unwrap(); |
| 312 |
|
| 313 |
let present_pages = page_infos.iter().filter(|info| { |
| 314 |
if let PageInfo::MemoryPage(flags) = info { |
| 315 |
flags.contains(MemoryPageFlags::PRESENT) |
| 316 |
} else { |
| 317 |
false |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
for present_page in present_pages { |
| 322 |
println!("{:?}", present_page); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn test_mmap_path() { |
| 328 |
assert_eq!(MMapPath::from("[stack]").unwrap(), MMapPath::Stack); |
| 329 |
assert_eq!(MMapPath::from("[foo]").unwrap(), MMapPath::Other("foo".to_owned())); |
| 330 |
assert_eq!(MMapPath::from("").unwrap(), MMapPath::Anonymous); |
| 331 |
assert_eq!(MMapPath::from("[stack:154]").unwrap(), MMapPath::TStack(154)); |
| 332 |
assert_eq!( |
| 333 |
MMapPath::from("/lib/libfoo.so").unwrap(), |
| 334 |
MMapPath::Path(PathBuf::from("/lib/libfoo.so")) |
| 335 |
); |
| 336 |
} |
| 337 |
#[test] |
| 338 |
fn test_proc_fds() { |
| 339 |
let myself = Process::myself().unwrap(); |
| 340 |
for f in myself.fd().unwrap() { |
| 341 |
let fd = f.unwrap(); |
| 342 |
println!("{:?} {:?}", fd, fd.mode()); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
#[test] |
| 347 |
fn test_proc_fd_count_runsinglethread() { |
| 348 |
let myself = Process::myself().unwrap(); |
| 349 |
|
| 350 |
let before = myself.fd_count().unwrap(); |
| 351 |
|
| 352 |
let one = File::open("/proc/self").unwrap(); |
| 353 |
let two = File::open("/proc/self/status").unwrap(); |
| 354 |
|
| 355 |
let after = myself.fd_count().unwrap(); |
| 356 |
|
| 357 |
assert_eq!( |
| 358 |
before + 2, |
| 359 |
after, |
| 360 |
"opened two files and expected {} open fds, saw {}", |
| 361 |
before + 2, |
| 362 |
after |
| 363 |
); |
| 364 |
|
| 365 |
drop(one); |
| 366 |
drop(two); |
| 367 |
|
| 368 |
let after_closing = myself.fd_count().unwrap(); |
| 369 |
|
| 370 |
assert_eq!(before, after_closing); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn test_proc_fd() { |
| 375 |
let myself = Process::myself().unwrap(); |
| 376 |
let raw_fd = myself.fd().unwrap().next().unwrap().unwrap().fd as i32; |
| 377 |
let fd = FDInfo::from_raw_fd(myself.pid, raw_fd).unwrap(); |
| 378 |
println!("{:?} {:?}", fd, fd.mode()); |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn test_proc_coredump() { |
| 383 |
let myself = Process::myself().unwrap(); |
| 384 |
let flags = myself.coredump_filter(); |
| 385 |
println!("{:?}", flags); |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn test_proc_auxv() { |
| 390 |
let myself = Process::myself().unwrap(); |
| 391 |
let auxv = myself.auxv().unwrap(); |
| 392 |
println!("{:?}", auxv); |
| 393 |
for (k, v) in auxv { |
| 394 |
// See bits/auxv.h |
| 395 |
match k { |
| 396 |
2 => println!("File descriptor of program: {}", v), |
| 397 |
3 => println!("Address of the program headers of the executable: 0x{:x}", v), |
| 398 |
4 => println!("Size of program header entry: {}", v), |
| 399 |
5 => println!("Number of program headers: {}", v), |
| 400 |
6 => { |
| 401 |
println!("System page size: {}", v); |
| 402 |
assert!(v > 0); |
| 403 |
} |
| 404 |
7 => { |
| 405 |
println!("Base address: 0x{:x}", v); |
| 406 |
assert!(v > 0); |
| 407 |
} |
| 408 |
8 => println!("Flags: 0x{:x}", v), |
| 409 |
9 => { |
| 410 |
println!("Entry address of the executable: 0x{:x}", v); |
| 411 |
assert!(v > 0); |
| 412 |
} |
| 413 |
11 => { |
| 414 |
println!("Real UID: {}", v); |
| 415 |
assert_eq!(v as u32, rustix::process::getuid().as_raw()); |
| 416 |
} |
| 417 |
12 => { |
| 418 |
println!("Effective UID: {}", v); |
| 419 |
assert!(v > 0); |
| 420 |
} |
| 421 |
13 => { |
| 422 |
println!("Real GID: {}", v); |
| 423 |
assert!(v > 0); |
| 424 |
} |
| 425 |
14 => { |
| 426 |
println!("Effective GID: {}", v); |
| 427 |
assert!(v > 0); |
| 428 |
} |
| 429 |
15 => { |
| 430 |
println!("Platform string address: 0x{:x}", v); |
| 431 |
let platform = unsafe { std::ffi::CStr::from_ptr(v as *const _) }; |
| 432 |
println!("Platform string: {:?}", platform); |
| 433 |
} |
| 434 |
16 => println!("HW Cap: 0x{:x}", v), |
| 435 |
17 => { |
| 436 |
println!("Clock ticks per second: {}", v); |
| 437 |
assert_eq!(v, crate::ticks_per_second()); |
| 438 |
} |
| 439 |
19 => println!("Data cache block size: {}", v), |
| 440 |
23 => println!("Run as setuid?: {}", v), |
| 441 |
25 => println!("Address of 16 random bytes: 0x{:x}", v), |
| 442 |
26 => println!("HW Cap2: 0x{:x}", v), |
| 443 |
31 => { |
| 444 |
println!("argv[0] address: 0x{:x}", v); |
| 445 |
let argv0 = unsafe { std::ffi::CStr::from_ptr(v as *const _) }; |
| 446 |
println!("argv[0]: {:?}", argv0); |
| 447 |
} |
| 448 |
33 => { |
| 449 |
println!("Base addr of vDSO: 0x{:x}", v); |
| 450 |
// confirm that this base addr from the aux vector matches what our maps file says: |
| 451 |
let maps = myself.maps().unwrap(); |
| 452 |
let vsdo = maps |
| 453 |
.iter() |
| 454 |
.find(|map| map.pathname == MMapPath::Vdso) |
| 455 |
.expect("Failed to find mapping for the vdso"); |
| 456 |
assert_eq!(vsdo.address.0, v); |
| 457 |
} |
| 458 |
k => println!("Unknown key {}: {:x}", k, v), |
| 459 |
} |
| 460 |
if k != 16 { |
| 461 |
// for reasons i do not understand, getauxval(AT_HWCAP) doesn't return the expected |
| 462 |
// value |
| 463 |
assert_eq!(v, unsafe { libc::getauxval(k.try_into().unwrap()).into() }); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
#[test] |
| 469 |
fn test_proc_wchan() { |
| 470 |
let myself = Process::myself().unwrap(); |
| 471 |
let wchan = myself.wchan().unwrap(); |
| 472 |
println!("{:?}", wchan); |
| 473 |
} |
| 474 |
|
| 475 |
#[test] |
| 476 |
fn test_proc_loginuid() { |
| 477 |
if !Path::new("/proc/self/loginuid").exists() { |
| 478 |
return; |
| 479 |
} |
| 480 |
|
| 481 |
let myself = Process::myself().unwrap(); |
| 482 |
let loginuid = myself.loginuid().unwrap(); |
| 483 |
println!("{:?}", loginuid); |
| 484 |
} |
| 485 |
|
| 486 |
#[test] |
| 487 |
fn test_nopanic() { |
| 488 |
fn inner() -> ProcResult<u8> { |
| 489 |
let a = vec!["xyz"]; |
| 490 |
from_iter(a) |
| 491 |
} |
| 492 |
assert!(inner().is_err()); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn test_procinfo() { |
| 497 |
// test to see that this crate and procinfo give mostly the same results |
| 498 |
|
| 499 |
fn diff_mem(a: f32, b: f32) { |
| 500 |
let diff = (a - b).abs(); |
| 501 |
assert!(diff < 20000.0, "diff:{}", diff); |
| 502 |
} |
| 503 |
|
| 504 |
// take a pause to let things "settle" before getting data. By default, cargo will run |
| 505 |
// tests in parallel, which can cause disturbences |
| 506 |
std::thread::sleep(std::time::Duration::from_secs(1)); |
| 507 |
|
| 508 |
let procinfo_stat = procinfo::pid::stat_self().unwrap(); |
| 509 |
let me = Process::myself().unwrap(); |
| 510 |
let me_stat = me.stat().unwrap(); |
| 511 |
|
| 512 |
diff_mem(procinfo_stat.vsize as f32, me_stat.vsize as f32); |
| 513 |
|
| 514 |
assert_eq!(me_stat.priority, procinfo_stat.priority as i64); |
| 515 |
assert_eq!(me_stat.nice, procinfo_stat.nice as i64); |
| 516 |
// flags seem to change during runtime, with PF_FREEZER_SKIP coming and going... |
| 517 |
//assert_eq!(me_stat.flags, procinfo_stat.flags, "procfs:{:?} procinfo:{:?}", crate::StatFlags::from_bits(me_stat.flags), crate::StatFlags::from_bits(procinfo_stat.flags)); |
| 518 |
assert_eq!(me_stat.pid, procinfo_stat.pid); |
| 519 |
assert_eq!(me_stat.ppid, procinfo_stat.ppid); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
fn test_statm() { |
| 524 |
let me = Process::myself().unwrap(); |
| 525 |
let statm = me.statm().unwrap(); |
| 526 |
println!("{:#?}", statm); |
| 527 |
} |
| 528 |
|
| 529 |
#[test] |
| 530 |
fn test_schedstat() { |
| 531 |
let me = Process::myself().unwrap(); |
| 532 |
let schedstat = me.schedstat().unwrap(); |
| 533 |
println!("{:#?}", schedstat); |
| 534 |
} |
| 535 |
|
| 536 |
#[test] |
| 537 |
fn test_fdtarget() { |
| 538 |
// https://github.com/eminence/procfs/issues/352 |
| 539 |
let x = FDTarget::from_str("onload:[udp:1:2032]").unwrap(); |
| 540 |
assert!(matches!(x, FDTarget::Unknown(..))); |
| 541 |
|
| 542 |
// none of these below values are valid, but were found by a fuzzer to crash procfs. this |
| 543 |
// test ensures that the crashes have been fixed |
| 544 |
|
| 545 |
let _ = FDTarget::from_str(":"); |
| 546 |
let _ = FDTarget::from_str("n:ǟF"); |
| 547 |
let _ = FDTarget::from_str("pipe:"); |
| 548 |
} |
| 549 |
|
| 550 |
#[test] |
| 551 |
fn test_fdtarget_memfd() { |
| 552 |
let memfd = FDTarget::from_str("/memfd:test").unwrap(); |
| 553 |
assert!(matches!(memfd, FDTarget::MemFD(s) if s == "test")); |
| 554 |
} |
| 555 |
|
| 556 |
#[test] |
| 557 |
fn test_network_stuff() { |
| 558 |
let myself = Process::myself().unwrap(); |
| 559 |
let _tcp = myself.tcp().unwrap(); |
| 560 |
let _tcp6 = myself.tcp().unwrap(); |
| 561 |
let _udp = myself.udp().unwrap(); |
| 562 |
let _udp6 = myself.udp6().unwrap(); |
| 563 |
let _arp = myself.arp().unwrap(); |
| 564 |
let _route = myself.route().unwrap(); |
| 565 |
let _dev = myself.dev_status().unwrap(); |
| 566 |
let _unix = myself.unix().unwrap(); |
| 567 |
} |
| 568 |
|
| 569 |
trait LimitValueAsLimit { |
| 570 |
fn as_limit(&self) -> Option<u64>; |
| 571 |
} |
| 572 |
|
| 573 |
impl LimitValueAsLimit for LimitValue { |
| 574 |
fn as_limit(&self) -> Option<u64> { |
| 575 |
match self { |
| 576 |
LimitValue::Unlimited => None, |
| 577 |
LimitValue::Value(v) => Some(*v), |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
#[test] |
| 583 |
fn test_limits() { |
| 584 |
let me = process::Process::myself().unwrap(); |
| 585 |
let limits = me.limits().unwrap(); |
| 586 |
println!("{:#?}", limits); |
| 587 |
|
| 588 |
// Max cpu time |
| 589 |
let lim = rustix::process::getrlimit(Resource::Cpu); |
| 590 |
assert_eq!(lim.current, limits.max_cpu_time.soft_limit.as_limit()); |
| 591 |
assert_eq!(lim.maximum, limits.max_cpu_time.hard_limit.as_limit()); |
| 592 |
|
| 593 |
// Max file size |
| 594 |
let lim = rustix::process::getrlimit(Resource::Fsize); |
| 595 |
assert_eq!(lim.current, limits.max_file_size.soft_limit.as_limit()); |
| 596 |
assert_eq!(lim.maximum, limits.max_file_size.hard_limit.as_limit()); |
| 597 |
|
| 598 |
// Max data size |
| 599 |
let lim = rustix::process::getrlimit(Resource::Data); |
| 600 |
assert_eq!(lim.current, limits.max_data_size.soft_limit.as_limit()); |
| 601 |
assert_eq!(lim.maximum, limits.max_data_size.hard_limit.as_limit()); |
| 602 |
|
| 603 |
// Max stack size |
| 604 |
let lim = rustix::process::getrlimit(Resource::Stack); |
| 605 |
assert_eq!(lim.current, limits.max_stack_size.soft_limit.as_limit()); |
| 606 |
assert_eq!(lim.maximum, limits.max_stack_size.hard_limit.as_limit()); |
| 607 |
|
| 608 |
// Max core file size |
| 609 |
let lim = rustix::process::getrlimit(Resource::Core); |
| 610 |
assert_eq!(lim.current, limits.max_core_file_size.soft_limit.as_limit()); |
| 611 |
assert_eq!(lim.maximum, limits.max_core_file_size.hard_limit.as_limit()); |
| 612 |
|
| 613 |
// Max resident set |
| 614 |
let lim = rustix::process::getrlimit(Resource::Rss); |
| 615 |
assert_eq!(lim.current, limits.max_resident_set.soft_limit.as_limit()); |
| 616 |
assert_eq!(lim.maximum, limits.max_resident_set.hard_limit.as_limit()); |
| 617 |
|
| 618 |
// Max processes |
| 619 |
let lim = rustix::process::getrlimit(Resource::Nproc); |
| 620 |
assert_eq!(lim.current, limits.max_processes.soft_limit.as_limit()); |
| 621 |
assert_eq!(lim.maximum, limits.max_processes.hard_limit.as_limit()); |
| 622 |
|
| 623 |
// Max open files |
| 624 |
let lim = rustix::process::getrlimit(Resource::Nofile); |
| 625 |
assert_eq!(lim.current, limits.max_open_files.soft_limit.as_limit()); |
| 626 |
assert_eq!(lim.maximum, limits.max_open_files.hard_limit.as_limit()); |
| 627 |
|
| 628 |
// Max locked memory |
| 629 |
let lim = rustix::process::getrlimit(Resource::Memlock); |
| 630 |
assert_eq!(lim.current, limits.max_locked_memory.soft_limit.as_limit()); |
| 631 |
assert_eq!(lim.maximum, limits.max_locked_memory.hard_limit.as_limit()); |
| 632 |
|
| 633 |
// Max address space |
| 634 |
let lim = rustix::process::getrlimit(Resource::As); |
| 635 |
assert_eq!(lim.current, limits.max_address_space.soft_limit.as_limit()); |
| 636 |
assert_eq!(lim.maximum, limits.max_address_space.hard_limit.as_limit()); |
| 637 |
|
| 638 |
// Max file locks |
| 639 |
let lim = rustix::process::getrlimit(Resource::Locks); |
| 640 |
assert_eq!(lim.current, limits.max_file_locks.soft_limit.as_limit()); |
| 641 |
assert_eq!(lim.maximum, limits.max_file_locks.hard_limit.as_limit()); |
| 642 |
|
| 643 |
// Max pending signals |
| 644 |
let lim = rustix::process::getrlimit(Resource::Sigpending); |
| 645 |
assert_eq!(lim.current, limits.max_pending_signals.soft_limit.as_limit()); |
| 646 |
assert_eq!(lim.maximum, limits.max_pending_signals.hard_limit.as_limit()); |
| 647 |
|
| 648 |
// Max msgqueue size |
| 649 |
let lim = rustix::process::getrlimit(Resource::Msgqueue); |
| 650 |
assert_eq!(lim.current, limits.max_msgqueue_size.soft_limit.as_limit()); |
| 651 |
assert_eq!(lim.maximum, limits.max_msgqueue_size.hard_limit.as_limit()); |
| 652 |
|
| 653 |
// Max nice priority |
| 654 |
let lim = rustix::process::getrlimit(Resource::Nice); |
| 655 |
assert_eq!(lim.current, limits.max_nice_priority.soft_limit.as_limit()); |
| 656 |
assert_eq!(lim.maximum, limits.max_nice_priority.hard_limit.as_limit()); |
| 657 |
|
| 658 |
// Max realtime priority |
| 659 |
let lim = rustix::process::getrlimit(Resource::Rtprio); |
| 660 |
assert_eq!(lim.current, limits.max_realtime_priority.soft_limit.as_limit()); |
| 661 |
assert_eq!(lim.maximum, limits.max_realtime_priority.hard_limit.as_limit()); |
| 662 |
|
| 663 |
// Max realtime timeout |
| 664 |
let lim = rustix::process::getrlimit(Resource::Rttime); |
| 665 |
assert_eq!(lim.current, limits.max_realtime_timeout.soft_limit.as_limit()); |
| 666 |
assert_eq!(lim.maximum, limits.max_realtime_timeout.hard_limit.as_limit()); |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn test_mountinfo_live() { |
| 671 |
let me = Process::myself().unwrap(); |
| 672 |
let MountInfos(mounts) = me.mountinfo().unwrap(); |
| 673 |
println!("{:#?}", mounts); |
| 674 |
} |
| 675 |
|
| 676 |
#[test] |
| 677 |
fn test_proc_mountstats_live() { |
| 678 |
// this tries to parse a live mountstats file |
| 679 |
// there are no assertions, but we still want to check for parsing errors (which can |
| 680 |
// cause panics) |
| 681 |
|
| 682 |
let MountStats(stats) = FromRead::from_file("/proc/self/mountstats").unwrap(); |
| 683 |
for stat in stats { |
| 684 |
println!("{:#?}", stat); |
| 685 |
if let Some(nfs) = stat.statistics { |
| 686 |
println!(" {:?}", nfs.server_caps().unwrap()); |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
#[test] |
| 692 |
fn test_proc_status() { |
| 693 |
let myself = Process::myself().unwrap(); |
| 694 |
let stat = myself.stat().unwrap(); |
| 695 |
let status = myself.status().unwrap(); |
| 696 |
println!("{:?}", status); |
| 697 |
|
| 698 |
assert_eq!(status.name, stat.comm); |
| 699 |
assert_eq!(status.pid, stat.pid); |
| 700 |
assert_eq!(status.ppid, stat.ppid); |
| 701 |
} |
| 702 |
|
| 703 |
#[test] |
| 704 |
fn test_proc_status_for_kthreadd() { |
| 705 |
// when running in a container, pid2 probably isn't kthreadd, so check |
| 706 |
let kthreadd = match process::Process::new(2) { |
| 707 |
Ok(p) => p, |
| 708 |
Err(ProcError::NotFound(_)) => { |
| 709 |
return; // ok we can ignore |
| 710 |
} |
| 711 |
Err(e) => { |
| 712 |
panic!("{}", e); |
| 713 |
} |
| 714 |
}; |
| 715 |
let status = kthreadd.status().unwrap(); |
| 716 |
println!("{:?}", status); |
| 717 |
|
| 718 |
// actually check that pid2 is kthreadd |
| 719 |
if status.name != "kthreadd" { |
| 720 |
return; // ok we can still ignore |
| 721 |
} |
| 722 |
|
| 723 |
assert_eq!(status.pid, 2); |
| 724 |
assert_eq!(status.vmpeak, None); |
| 725 |
assert_eq!(status.vmsize, None); |
| 726 |
assert_eq!(status.vmlck, None); |
| 727 |
assert_eq!(status.vmpin, None); |
| 728 |
assert_eq!(status.vmhwm, None); |
| 729 |
assert_eq!(status.vmrss, None); |
| 730 |
assert_eq!(status.rssanon, None); |
| 731 |
assert_eq!(status.rssfile, None); |
| 732 |
assert_eq!(status.rssshmem, None); |
| 733 |
assert_eq!(status.vmdata, None); |
| 734 |
assert_eq!(status.vmstk, None); |
| 735 |
assert_eq!(status.vmexe, None); |
| 736 |
assert_eq!(status.vmlib, None); |
| 737 |
assert_eq!(status.vmpte, None); |
| 738 |
assert_eq!(status.vmswap, None); |
| 739 |
assert_eq!(status.hugetlbpages, None); |
| 740 |
} |