40 releases

0.7.11 Sep 17, 2021
0.7.9 Aug 27, 2021
0.7.2 Jul 16, 2021
0.6.2 Jan 29, 2021
0.1.0 Jul 26, 2019

#28 in Operating systems

Download history 60/week @ 2021-06-23 59/week @ 2021-06-30 39/week @ 2021-07-07 240/week @ 2021-07-14 32/week @ 2021-07-21 131/week @ 2021-07-28 137/week @ 2021-08-04 94/week @ 2021-08-11 566/week @ 2021-08-18 155/week @ 2021-08-25 108/week @ 2021-09-01 102/week @ 2021-09-08 113/week @ 2021-09-15 128/week @ 2021-09-22 89/week @ 2021-09-29 83/week @ 2021-10-06

591 downloads per month
Used in 3 crates (2 directly)

Apache-2.0

3.5MB
51K SLoC

nc

Build status Latest version Documentation Minimum rustc version License

Execute system call directly without std or libc.

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 llvm_asm! feature is unavailable in stable version.

Platforms and Architectures

  • linux
    • x86
    • x86-64
    • arm
    • aarch64
    • mips
    • mipsel
    • mips64
    • mips64el
    • powerpc64
    • s390x
  • freebsd
    • x86-64

Related projects

License

This library is release in Apache License.

No runtime deps