File: pid.rs

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (35 lines) | stat: -rw-r--r-- 788 bytes parent folder | download | duplicates (8)
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
#![allow(clippy::print_stdout, reason = "This is an example file")]

use std::env;
use std::io::Result;

fn watch_pid(pid: libc::pid_t) -> Result<()> {
    let mut watcher = kqueue::Watcher::new()?;

    watcher.add_pid(
        pid,
        kqueue::EventFilter::EVFILT_PROC,
        kqueue::FilterFlag::NOTE_EXIT,
    )?;

    watcher.watch()?;

    println!("Watching for events, press Ctrl+C to stop...");
    for ev in watcher.iter() {
        println!("{ev:?}");
    }

    Ok(())
}

fn main() {
    if let Some(pid) = env::args().nth(1) {
        if let Ok(npid) = pid.parse::<libc::pid_t>() {
            if let Err(err) = watch_pid(npid) {
                println!("{err:?}");
            }
        }
    } else {
        println!("Usage: cargo run --example pid <pid>");
    }
}