50 releases

Uses new Rust 2021

0.8.4 Jun 19, 2022
0.8.1 Apr 19, 2022
0.7.17 Mar 24, 2022
0.7.14 Dec 27, 2021
0.1.0 Jul 26, 2019

#33 in Operating systems

Download history 156/week @ 2022-03-10 197/week @ 2022-03-17 115/week @ 2022-03-24 79/week @ 2022-03-31 84/week @ 2022-04-07 76/week @ 2022-04-14 155/week @ 2022-04-21 235/week @ 2022-04-28 769/week @ 2022-05-05 916/week @ 2022-05-12 341/week @ 2022-05-19 222/week @ 2022-05-26 648/week @ 2022-06-02 67/week @ 2022-06-09 102/week @ 2022-06-16 89/week @ 2022-06-23

914 downloads per month
Used in 3 crates

Apache-2.0

3.5MB
52K SLoC

nc

Build status Latest version Documentation Minimum rustc version License

Access system calls directly without std or libc.

Features:

  • No glibc or musl required
  • Access syscalls directly, via assembly
  • No global errno variable, every function returns an errno instead
  • Support latest kernel APIs, like io-uring and pidfd, introduced in linux 5.0+

Usage

Add this to Cargo.toml:

[dependencies]
nc = "0.8"

Examples

Get file stat:

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

Get human-readable error string:

let errno = nc::EPERM;
println!("err: {:?}", nc::strerror(errno);

Fork process:

let pid = unsafe { nc::fork() };
match pid {
    Ok(pid) => {
        if pid == 0 {
            println!("child process: {}", pid);
            let args = [""];
            let env = [""];
            match unsafe { 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 = unsafe { nc::getpid() };
let ret = unsafe { 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 = unsafe { nc::signal(nc::SIGALRM, handle_alarm as nc::sighandler_t) };
    assert!(ret.is_ok());
    let remaining = unsafe {nc::alarm(1) };
    let ret = unsafe { 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