procfs 0.17.0

Interface to the linux procfs pseudo-filesystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use super::*;
use rustix::process::Resource;

fn check_unwrap<T>(prc: &Process, val: ProcResult<T>) -> Option<T> {
    match val {
        Ok(t) => Some(t),
        Err(ProcError::PermissionDenied(_)) if !rustix::process::geteuid().is_root() => {
            // we are not root, and so a permission denied error is OK
            None
        }
        Err(ProcError::NotFound(path)) => {
            // a common reason for this error is that the process isn't running anymore
            if prc.is_alive() {
                panic!("{:?} not found", path)
            }
            None
        }
        Err(err) => panic!("check_unwrap error for {} {:?}", prc.pid, err),
    }
}

fn check_unwrap_task<T>(prc: &Process, val: ProcResult<T>) -> Option<T> {
    match val {
        Ok(t) => Some(t),
        Err(ProcError::PermissionDenied(_)) if !rustix::process::geteuid().is_root() => {
            // we are not root, and so a permission denied error is OK
            None
        }
        Err(ProcError::NotFound(_path)) => {
            // tasks can be more short-lived thanks processes, and it seems that accessing
            // the /status and /stat files for tasks is quite unreliable
            None
        }
        Err(err) => panic!("check_unwrap error for {} {:?}", prc.pid, err),
    }
}

#[test]
fn test_main_thread_task() {
    let myself = Process::myself().unwrap();
    let task = myself.task_main_thread().unwrap();
    check_unwrap(&myself, task.stat());
}

#[allow(clippy::cognitive_complexity)]
#[test]
fn test_self_proc() {
    let myself = Process::myself().unwrap().stat().unwrap();
    println!("{:#?}", myself);
    println!("state: {:?}", myself.state());
    println!("tty: {:?}", myself.tty_nr());
    println!("flags: {:?}", myself.flags());

    #[cfg(feature = "chrono")]
    println!("starttime: {:#?}", myself.starttime().get());

    let kernel = KernelVersion::current().unwrap();

    if kernel >= KernelVersion::new(2, 1, 22) {
        assert!(myself.exit_signal.is_some());
    } else {
        assert!(myself.exit_signal.is_none());
    }

    if kernel >= KernelVersion::new(2, 2, 8) {
        assert!(myself.processor.is_some());
    } else {
        assert!(myself.processor.is_none());
    }

    if kernel >= KernelVersion::new(2, 5, 19) {
        assert!(myself.rt_priority.is_some());
    } else {
        assert!(myself.rt_priority.is_none());
    }

    if kernel >= KernelVersion::new(2, 5, 19) {
        assert!(myself.rt_priority.is_some());
        assert!(myself.policy.is_some());
    } else {
        assert!(myself.rt_priority.is_none());
        assert!(myself.policy.is_none());
    }

    if kernel >= KernelVersion::new(2, 6, 18) {
        assert!(myself.delayacct_blkio_ticks.is_some());
    } else {
        assert!(myself.delayacct_blkio_ticks.is_none());
    }

    if kernel >= KernelVersion::new(2, 6, 24) {
        assert!(myself.guest_time.is_some());
        assert!(myself.cguest_time.is_some());
    } else {
        assert!(myself.guest_time.is_none());
        assert!(myself.cguest_time.is_none());
    }

    if kernel >= KernelVersion::new(3, 3, 0) {
        assert!(myself.start_data.is_some());
        assert!(myself.end_data.is_some());
        assert!(myself.start_brk.is_some());
    } else {
        assert!(myself.start_data.is_none());
        assert!(myself.end_data.is_none());
        assert!(myself.start_brk.is_none());
    }

    if kernel >= KernelVersion::new(3, 5, 0) {
        assert!(myself.arg_start.is_some());
        assert!(myself.arg_end.is_some());
        assert!(myself.env_start.is_some());
        assert!(myself.env_end.is_some());
        assert!(myself.exit_code.is_some());
    } else {
        assert!(myself.arg_start.is_none());
        assert!(myself.arg_end.is_none());
        assert!(myself.env_start.is_none());
        assert!(myself.env_end.is_none());
        assert!(myself.exit_code.is_none());
    }
}

#[test]
fn test_all() {
    let is_wsl2 = KernelConfig::current()
        .ok()
        .and_then(|KernelConfig(cfg)| {
            cfg.get("CONFIG_LOCALVERSION").and_then(|ver| {
                if let ConfigSetting::Value(s) = ver {
                    Some(s == "\"-microsoft-standard\"")
                } else {
                    None
                }
            })
        })
        .unwrap_or(false);
    for p in all_processes().unwrap() {
        // note: this test doesn't unwrap, since some of this data requires root to access
        // so permission denied errors are common.  The check_unwrap helper function handles
        // this.

        let prc = p.unwrap();
        let stat = prc.stat().unwrap();
        println!("{} {}", prc.pid(), stat.comm);
        stat.flags().unwrap();
        stat.state().unwrap();
        #[cfg(feature = "chrono")]
        stat.starttime().get().unwrap();

        // if this process is defunct/zombie, don't try to read any of the below data
        // (some might be successful, but not all)
        if stat.state().unwrap() == ProcState::Zombie {
            continue;
        }

        check_unwrap(&prc, prc.cmdline());
        check_unwrap(&prc, prc.environ());
        check_unwrap(&prc, prc.fd());
        check_unwrap(&prc, prc.io());
        check_unwrap(&prc, prc.maps());
        check_unwrap(&prc, prc.coredump_filter());
        // The WSL2 kernel doesn't have autogroup, even though this should be present since linux
        // 2.6.36
        if is_wsl2 {
            assert!(prc.autogroup().is_err());
        } else {
            check_unwrap(&prc, prc.autogroup());
        }
        check_unwrap(&prc, prc.auxv());
        check_unwrap(&prc, prc.cgroups());
        check_unwrap(&prc, prc.wchan());
        check_unwrap(&prc, prc.status());
        check_unwrap(&prc, prc.mountinfo());
        check_unwrap(&prc, prc.mountstats());
        check_unwrap(&prc, prc.oom_score());
        if let Some(oom_score_adj) = check_unwrap(&prc, prc.oom_score_adj()) {
            assert!(oom_score_adj >= -1000 && oom_score_adj <= 1000);
            check_unwrap(&prc, prc.set_oom_score_adj(oom_score_adj));
        }

        if let Some(tasks) = check_unwrap(&prc, prc.tasks()) {
            for task in tasks {
                let task = task.unwrap();
                check_unwrap_task(&prc, task.stat());
                check_unwrap_task(&prc, task.status());
                check_unwrap_task(&prc, task.io());
                check_unwrap_task(&prc, task.schedstat());
            }
        }
    }
}

#[test]
fn test_smaps() {
    let me = Process::myself().unwrap();
    let smaps = match me.smaps() {
        Ok(x) => x,
        Err(ProcError::NotFound(_)) => {
            // ignored because not all kernerls have smaps
            return;
        }
        Err(e) => panic!("{}", e),
    };
    println!("{:#?}", smaps);
}

#[test]
fn test_smaps_rollup() {
    let me = Process::myself().unwrap();
    let smaps_rollup = match me.smaps_rollup() {
        Ok(x) => x,
        Err(ProcError::NotFound(_)) => {
            // ignored because not all kernerls have smaps_rollup
            return;
        }
        Err(e) => panic!("{}", e),
    };
    println!("{:#?}", smaps_rollup);
}

#[test]
fn test_proc_alive() {
    let myself = Process::myself().unwrap();
    assert!(myself.is_alive());

    // zombies should not be considered alive
    let mut command = std::process::Command::new("sleep");
    command
        .arg("0")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null());
    let mut child = command.spawn().unwrap();
    let child_pid = child.id() as i32;

    // sleep very briefly to allow the child to start and then exit
    std::thread::sleep(std::time::Duration::from_millis(30));

    let child_proc = Process::new(child_pid).unwrap();
    assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat());
    assert!(child_proc.stat().unwrap().state().unwrap() == ProcState::Zombie);
    child.wait().unwrap();
    assert!(Process::new(child_pid).is_err());
    assert!(!child_proc.is_alive(), "Child state is: {:?}", child_proc.stat());
}

#[test]
fn test_proc_environ() {
    let myself = Process::myself().unwrap();
    let proc_environ = myself.environ().unwrap();

    let std_environ: HashMap<_, _> = std::env::vars_os().collect();
    assert_eq!(proc_environ, std_environ);
}

#[test]
fn test_error_handling() {
    // getting the proc struct should be OK
    let init = Process::new(1).unwrap();

    let i_have_access = rustix::process::geteuid().as_raw() == init.uid().unwrap();

    if !i_have_access {
        // but accessing data should result in an error (unless we are running as root!)
        assert!(init.cwd().is_err());
        assert!(init.environ().is_err());
    }
}

#[test]
fn test_proc_exe() {
    let myself = Process::myself().unwrap();
    let proc_exe = myself.exe().unwrap();
    let std_exe = std::env::current_exe().unwrap();
    assert_eq!(proc_exe, std_exe);
}

#[test]
fn test_proc_io() {
    let myself = Process::myself().unwrap();
    let kernel = KernelVersion::current().unwrap();
    let io = myself.io();
    println!("{:?}", io);
    if io.is_ok() {
        assert!(kernel >= KernelVersion::new(2, 6, 20));
    }
}

#[test]
fn test_proc_maps() {
    let myself = Process::myself().unwrap();
    let maps = myself.maps().unwrap();
    for map in maps {
        println!("{:?}", map);
    }
}

#[test]
fn test_proc_pagemap() {
    let myself = Process::myself().unwrap();
    let maps = myself.maps().unwrap();

    let stack_map = maps.iter().find(|m| matches!(m.pathname, MMapPath::Stack)).unwrap();
    let page_size = crate::page_size() as usize;
    let start_page = stack_map.address.0 as usize / page_size;
    let end_page = stack_map.address.1 as usize / page_size;

    let mut pagemap = myself.pagemap().unwrap();
    let page_infos = pagemap.get_range_info(start_page..end_page).unwrap();

    let present_pages = page_infos.iter().filter(|info| {
        if let PageInfo::MemoryPage(flags) = info {
            flags.contains(MemoryPageFlags::PRESENT)
        } else {
            false
        }
    });

    for present_page in present_pages {
        println!("{:?}", present_page);
    }
}

#[test]
fn test_mmap_path() {
    assert_eq!(MMapPath::from("[stack]").unwrap(), MMapPath::Stack);
    assert_eq!(MMapPath::from("[foo]").unwrap(), MMapPath::Other("foo".to_owned()));
    assert_eq!(MMapPath::from("").unwrap(), MMapPath::Anonymous);
    assert_eq!(MMapPath::from("[stack:154]").unwrap(), MMapPath::TStack(154));
    assert_eq!(
        MMapPath::from("/lib/libfoo.so").unwrap(),
        MMapPath::Path(PathBuf::from("/lib/libfoo.so"))
    );
}
#[test]
fn test_proc_fds() {
    let myself = Process::myself().unwrap();
    for f in myself.fd().unwrap() {
        let fd = f.unwrap();
        println!("{:?} {:?}", fd, fd.mode());
    }
}

#[test]
fn test_proc_fd_count_runsinglethread() {
    let myself = Process::myself().unwrap();

    let before = myself.fd_count().unwrap();

    let one = File::open("/proc/self").unwrap();
    let two = File::open("/proc/self/status").unwrap();

    let after = myself.fd_count().unwrap();

    assert_eq!(
        before + 2,
        after,
        "opened two files and expected {} open fds, saw {}",
        before + 2,
        after
    );

    drop(one);
    drop(two);

    let after_closing = myself.fd_count().unwrap();

    assert_eq!(before, after_closing);
}

#[test]
fn test_proc_fd() {
    let myself = Process::myself().unwrap();
    let raw_fd = myself.fd().unwrap().next().unwrap().unwrap().fd as i32;
    let fd = FDInfo::from_raw_fd(myself.pid, raw_fd).unwrap();
    println!("{:?} {:?}", fd, fd.mode());
}

#[test]
fn test_proc_coredump() {
    let myself = Process::myself().unwrap();
    let flags = myself.coredump_filter();
    println!("{:?}", flags);
}

#[test]
fn test_proc_auxv() {
    let myself = Process::myself().unwrap();
    let auxv = myself.auxv().unwrap();
    println!("{:?}", auxv);
    for (k, v) in auxv {
        // See bits/auxv.h
        match k {
            2 => println!("File descriptor of program: {}", v),
            3 => println!("Address of the program headers of the executable: 0x{:x}", v),
            4 => println!("Size of program header entry: {}", v),
            5 => println!("Number of program headers: {}", v),
            6 => {
                println!("System page size: {}", v);
                assert!(v > 0);
            }
            7 => {
                println!("Base address: 0x{:x}", v);
                assert!(v > 0);
            }
            8 => println!("Flags: 0x{:x}", v),
            9 => {
                println!("Entry address of the executable: 0x{:x}", v);
                assert!(v > 0);
            }
            11 => {
                println!("Real UID: {}", v);
                assert_eq!(v as u32, rustix::process::getuid().as_raw());
            }
            12 => {
                println!("Effective UID: {}", v);
                assert!(v > 0);
            }
            13 => {
                println!("Real GID: {}", v);
                assert!(v > 0);
            }
            14 => {
                println!("Effective GID: {}", v);
                assert!(v > 0);
            }
            15 => {
                println!("Platform string address: 0x{:x}", v);
                let platform = unsafe { std::ffi::CStr::from_ptr(v as *const _) };
                println!("Platform string: {:?}", platform);
            }
            16 => println!("HW Cap: 0x{:x}", v),
            17 => {
                println!("Clock ticks per second: {}", v);
                assert_eq!(v, crate::ticks_per_second());
            }
            19 => println!("Data cache block size: {}", v),
            23 => println!("Run as setuid?: {}", v),
            25 => println!("Address of 16 random bytes: 0x{:x}", v),
            26 => println!("HW Cap2: 0x{:x}", v),
            31 => {
                println!("argv[0] address: 0x{:x}", v);
                let argv0 = unsafe { std::ffi::CStr::from_ptr(v as *const _) };
                println!("argv[0]: {:?}", argv0);
            }
            33 => {
                println!("Base addr of vDSO: 0x{:x}", v);
                // confirm that this base addr from the aux vector matches what our maps file says:
                let maps = myself.maps().unwrap();
                let vsdo = maps
                    .iter()
                    .find(|map| map.pathname == MMapPath::Vdso)
                    .expect("Failed to find mapping for the vdso");
                assert_eq!(vsdo.address.0, v);
            }
            k => println!("Unknown key {}: {:x}", k, v),
        }
        if k != 16 {
            // for reasons i do not understand, getauxval(AT_HWCAP) doesn't return the expected
            // value
            assert_eq!(v, unsafe { libc::getauxval(k) });
        }
    }
}

#[test]
fn test_proc_wchan() {
    let myself = Process::myself().unwrap();
    let wchan = myself.wchan().unwrap();
    println!("{:?}", wchan);
}

#[test]
fn test_proc_loginuid() {
    if !Path::new("/proc/self/loginuid").exists() {
        return;
    }

    let myself = Process::myself().unwrap();
    let loginuid = myself.loginuid().unwrap();
    println!("{:?}", loginuid);
}

#[test]
fn test_nopanic() {
    fn inner() -> ProcResult<u8> {
        let a = vec!["xyz"];
        from_iter(a)
    }
    assert!(inner().is_err());
}

#[test]
fn test_procinfo() {
    // test to see that this crate and procinfo give mostly the same results

    fn diff_mem(a: f32, b: f32) {
        let diff = (a - b).abs();
        assert!(diff < 20000.0, "diff:{}", diff);
    }

    // take a pause to let things "settle" before getting data.  By default, cargo will run
    // tests in parallel, which can cause disturbences
    std::thread::sleep(std::time::Duration::from_secs(1));

    let procinfo_stat = procinfo::pid::stat_self().unwrap();
    let me = Process::myself().unwrap();
    let me_stat = me.stat().unwrap();

    diff_mem(procinfo_stat.vsize as f32, me_stat.vsize as f32);

    assert_eq!(me_stat.priority, procinfo_stat.priority as i64);
    assert_eq!(me_stat.nice, procinfo_stat.nice as i64);
    // flags seem to change during runtime, with PF_FREEZER_SKIP coming and going...
    //assert_eq!(me_stat.flags, procinfo_stat.flags, "procfs:{:?} procinfo:{:?}", crate::StatFlags::from_bits(me_stat.flags), crate::StatFlags::from_bits(procinfo_stat.flags));
    assert_eq!(me_stat.pid, procinfo_stat.pid);
    assert_eq!(me_stat.ppid, procinfo_stat.ppid);
}

#[test]
fn test_statm() {
    let me = Process::myself().unwrap();
    let statm = me.statm().unwrap();
    println!("{:#?}", statm);
}

#[test]
fn test_schedstat() {
    let me = Process::myself().unwrap();
    let schedstat = me.schedstat().unwrap();
    println!("{:#?}", schedstat);
}

#[test]
fn test_fdtarget() {
    // none of these values are valid, but were found by a fuzzer to crash procfs.  this
    // test ensures that the crashes have been fixed

    let _ = FDTarget::from_str(":");
    let _ = FDTarget::from_str("n:ÇŸF");
    let _ = FDTarget::from_str("pipe:");
}

#[test]
fn test_fdtarget_memfd() {
    let memfd = FDTarget::from_str("/memfd:test").unwrap();
    assert!(matches!(memfd, FDTarget::MemFD(s) if s == "test"));
}

#[test]
fn test_network_stuff() {
    let myself = Process::myself().unwrap();
    let _tcp = myself.tcp().unwrap();
    let _tcp6 = myself.tcp().unwrap();
    let _udp = myself.udp().unwrap();
    let _udp6 = myself.udp6().unwrap();
    let _arp = myself.arp().unwrap();
    let _route = myself.route().unwrap();
    let _dev = myself.dev_status().unwrap();
    let _unix = myself.unix().unwrap();
}

trait LimitValueAsLimit {
    fn as_limit(&self) -> Option<u64>;
}

impl LimitValueAsLimit for LimitValue {
    fn as_limit(&self) -> Option<u64> {
        match self {
            LimitValue::Unlimited => None,
            LimitValue::Value(v) => Some(*v),
        }
    }
}

#[test]
fn test_limits() {
    let me = process::Process::myself().unwrap();
    let limits = me.limits().unwrap();
    println!("{:#?}", limits);

    // Max cpu time
    let lim = rustix::process::getrlimit(Resource::Cpu);
    assert_eq!(lim.current, limits.max_cpu_time.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_cpu_time.hard_limit.as_limit());

    // Max file size
    let lim = rustix::process::getrlimit(Resource::Fsize);
    assert_eq!(lim.current, limits.max_file_size.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_file_size.hard_limit.as_limit());

    // Max data size
    let lim = rustix::process::getrlimit(Resource::Data);
    assert_eq!(lim.current, limits.max_data_size.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_data_size.hard_limit.as_limit());

    // Max stack size
    let lim = rustix::process::getrlimit(Resource::Stack);
    assert_eq!(lim.current, limits.max_stack_size.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_stack_size.hard_limit.as_limit());

    // Max core file size
    let lim = rustix::process::getrlimit(Resource::Core);
    assert_eq!(lim.current, limits.max_core_file_size.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_core_file_size.hard_limit.as_limit());

    // Max resident set
    let lim = rustix::process::getrlimit(Resource::Rss);
    assert_eq!(lim.current, limits.max_resident_set.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_resident_set.hard_limit.as_limit());

    // Max processes
    let lim = rustix::process::getrlimit(Resource::Nproc);
    assert_eq!(lim.current, limits.max_processes.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_processes.hard_limit.as_limit());

    // Max open files
    let lim = rustix::process::getrlimit(Resource::Nofile);
    assert_eq!(lim.current, limits.max_open_files.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_open_files.hard_limit.as_limit());

    // Max locked memory
    let lim = rustix::process::getrlimit(Resource::Memlock);
    assert_eq!(lim.current, limits.max_locked_memory.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_locked_memory.hard_limit.as_limit());

    // Max address space
    let lim = rustix::process::getrlimit(Resource::As);
    assert_eq!(lim.current, limits.max_address_space.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_address_space.hard_limit.as_limit());

    // Max file locks
    let lim = rustix::process::getrlimit(Resource::Locks);
    assert_eq!(lim.current, limits.max_file_locks.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_file_locks.hard_limit.as_limit());

    // Max pending signals
    let lim = rustix::process::getrlimit(Resource::Sigpending);
    assert_eq!(lim.current, limits.max_pending_signals.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_pending_signals.hard_limit.as_limit());

    // Max msgqueue size
    let lim = rustix::process::getrlimit(Resource::Msgqueue);
    assert_eq!(lim.current, limits.max_msgqueue_size.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_msgqueue_size.hard_limit.as_limit());

    // Max nice priority
    let lim = rustix::process::getrlimit(Resource::Nice);
    assert_eq!(lim.current, limits.max_nice_priority.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_nice_priority.hard_limit.as_limit());

    // Max realtime priority
    let lim = rustix::process::getrlimit(Resource::Rtprio);
    assert_eq!(lim.current, limits.max_realtime_priority.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_realtime_priority.hard_limit.as_limit());

    // Max realtime timeout
    let lim = rustix::process::getrlimit(Resource::Rttime);
    assert_eq!(lim.current, limits.max_realtime_timeout.soft_limit.as_limit());
    assert_eq!(lim.maximum, limits.max_realtime_timeout.hard_limit.as_limit());
}

#[test]
fn test_mountinfo_live() {
    let me = Process::myself().unwrap();
    let MountInfos(mounts) = me.mountinfo().unwrap();
    println!("{:#?}", mounts);
}

#[test]
fn test_proc_mountstats_live() {
    // this tries to parse a live mountstats file
    // there are no assertions, but we still want to check for parsing errors (which can
    // cause panics)

    let MountStats(stats) = FromRead::from_file("/proc/self/mountstats").unwrap();
    for stat in stats {
        println!("{:#?}", stat);
        if let Some(nfs) = stat.statistics {
            println!("  {:?}", nfs.server_caps().unwrap());
        }
    }
}

#[test]
fn test_proc_status() {
    let myself = Process::myself().unwrap();
    let stat = myself.stat().unwrap();
    let status = myself.status().unwrap();
    println!("{:?}", status);

    assert_eq!(status.name, stat.comm);
    assert_eq!(status.pid, stat.pid);
    assert_eq!(status.ppid, stat.ppid);
}

#[test]
fn test_proc_status_for_kthreadd() {
    // when running in a container, pid2 probably isn't kthreadd, so check
    let kthreadd = match process::Process::new(2) {
        Ok(p) => p,
        Err(ProcError::NotFound(_)) => {
            return; // ok we can ignore
        }
        Err(e) => {
            panic!("{}", e);
        }
    };
    let status = kthreadd.status().unwrap();
    println!("{:?}", status);

    assert_eq!(status.pid, 2);
    assert_eq!(status.vmpeak, None);
    assert_eq!(status.vmsize, None);
    assert_eq!(status.vmlck, None);
    assert_eq!(status.vmpin, None);
    assert_eq!(status.vmhwm, None);
    assert_eq!(status.vmrss, None);
    assert_eq!(status.rssanon, None);
    assert_eq!(status.rssfile, None);
    assert_eq!(status.rssshmem, None);
    assert_eq!(status.vmdata, None);
    assert_eq!(status.vmstk, None);
    assert_eq!(status.vmexe, None);
    assert_eq!(status.vmlib, None);
    assert_eq!(status.vmpte, None);
    assert_eq!(status.vmswap, None);
    assert_eq!(status.hugetlbpages, None);
}