31 releases

0.7.2 Jul 16, 2021
0.6.2 Jan 29, 2021
0.5.1 Nov 9, 2020
0.4.12 Jul 14, 2020
0.1.0 Jul 26, 2019

#59 in Operating systems

Download history 35/week @ 2021-04-04 50/week @ 2021-04-11 79/week @ 2021-04-18 18/week @ 2021-04-25 51/week @ 2021-05-02 11/week @ 2021-05-09 36/week @ 2021-05-16 9/week @ 2021-05-23 11/week @ 2021-05-30 50/week @ 2021-06-06 32/week @ 2021-06-13 27/week @ 2021-06-20 64/week @ 2021-06-27 50/week @ 2021-07-04 66/week @ 2021-07-11 214/week @ 2021-07-18

157 downloads per month
Used in 2 crates

Apache-2.0

3MB
46K SLoC

nc

Build Status Latest version Documentation Minimum rustc version License

Execute system call directly. nc do not depend on std.

Usage

Add this to Cargo.toml:

[dependencies]
nc = "0.7"

Examples

Get file stat:

let mut statbuf = nc::stat_t::default();
match nc::stat("/etc/passwd", &mut statbuf) {
    Ok(_) => println!("s: {:?}", statbuf),
    Err(errno) => eprintln!("Failed to get file status, got errno: {}", errno),
}

Fork process:

let pid = nc::fork();
match pid {
    Ok(pid) => {
        if pid == 0 {
            println!("child process: {}", pid);
            let args = [""];
            let env = [""];
            match nc::execve("/bin/ls", &args, &env) {
                Ok(_) => {},
                Err(errno) => eprintln!("`ls` got err: {}", errno),
            }
        } else if pid < 0 {
            eprintln!("fork() error!");
        } else {
            println!("parent process!");
        }
    },
    Err(errno) => eprintln!("errno: {}", errno),
}

Kill self:

let pid = nc::getpid();
let ret = nc::kill(pid, nc::SIGTERM);
// Never reach here.
println!("ret: {:?}", ret);

Or handle signals:

fn handle_alarm(signum: i32) {
    assert_eq!(signum, nc::SIGALRM);
}

fn main() {
    let ret = nc::signal(nc::SIGALRM, handle_alarm as nc::sighandler_t);
    assert!(ret.is_ok());
    let remaining = nc::alarm(1);
    let ret = nc::pause();
    assert!(ret.is_err());
    assert_eq!(ret, Err(nc::EINTR));
    assert_eq!(remaining, 0);
}

Stable version

For stable version of rustc, please install a C compiler (gcc or clang) first. As asm! feature is unavailable in stable version.

Related projects

License

This library is release in Apache License.

No runtime deps