66 releases

0.8.20 Mar 18, 2024
0.8.18 Sep 22, 2023
0.8.17 Jun 6, 2023
0.8.14 Mar 23, 2023
0.1.0 Jul 26, 2019

#110 in Operating systems

Download history 86/week @ 2024-01-03 60/week @ 2024-01-10 155/week @ 2024-01-17 165/week @ 2024-01-24 400/week @ 2024-01-31 250/week @ 2024-02-07 321/week @ 2024-02-14 464/week @ 2024-02-21 438/week @ 2024-02-28 340/week @ 2024-03-06 775/week @ 2024-03-13 588/week @ 2024-03-20 526/week @ 2024-03-27 601/week @ 2024-04-03 568/week @ 2024-04-10 484/week @ 2024-04-17

2,226 downloads per month
Used in 5 crates (3 directly)

Apache-2.0

4.5MB
69K 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.

Supported Operating Systems and Architectures

  • linux
    • aarch64
    • arm
    • loongarch64
    • mips
    • mips64
    • mips64el
    • mipsel
    • powerpc64
    • riscv64
    • s390x
    • x86
    • x86-64
  • android
    • aarch64
  • freebsd
    • x86-64
  • netbsd
    • x86-64
  • mac os
    • x86-64

License

This library is release in Apache License.

No runtime deps