Giter Club home page Giter Club logo

nc's Introduction

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.9"

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(0) => {
        println!("child process");
        let args = ["ls", "-l", "-a"];
        let env = ["FOO=BAR"];
        match unsafe { nc::execve("/bin/ls", &args, &env) } {
            Ok(_) => {},
            Err(errno) => eprintln!("`ls` got err: {}", errno),
        }
    }
    Ok(pid) => println!("[parent] child pid: {pid} "),
    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() {
    #[cfg(nc_has_sa_restorer)]
    let sa = nc::sigaction_t {
        sa_handler: handle_alarm as nc::sighandler_t,
        sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
        sa_restorer: nc::restore::get_sa_restorer(),
        ..nc::sigaction_t::default()
    };
    #[cfg(not(nc_has_sa_restorer))]
    let sa = nc::sigaction_t {
        sa_handler: handle_alarm as nc::sighandler_t,
        sa_flags: nc::SA_RESTART,
        ..nc::sigaction_t::default()
    };
    let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
    assert!(ret.is_ok());
    let remaining = unsafe { nc::alarm(1) };
    let mask = nc::sigset_t::default();
    let ret = unsafe { nc::rt_sigsuspend(&mask) };
    assert!(ret.is_err());
    assert_eq!(ret, Err(nc::EINTR));
    assert_eq!(remaining, Ok(0));
}

Supported Operating Systems and Architectures

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

Related projects

License

This library is govered by Apache-2.0 License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.