| Crates.io | nc |
| lib.rs | nc |
| version | 0.9.6 |
| created_at | 2019-07-26 03:07:55.568483+00 |
| updated_at | 2025-04-02 12:06:10.346218+00 |
| description | Access system calls directly |
| homepage | https://github.com/xushaohua/nc |
| repository | https://github.com/xushaohua/nc |
| max_upload_size | |
| id | 151702 |
| size | 5,634,241 |
Access system calls directly without std or libc.
Features:
Add this to Cargo.toml:
[dependencies]
nc = "0.9"
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 {
Err(errno) => eprintln!("Failed to call fork(), err: {}", nc::strerror(errno)),
Ok(0) => {
// Child process
println!("[child] pid: {}", unsafe { nc::getpid() });
let args = ["ls", "-l", "-a"];
let env = ["DISPLAY=wayland"];
let ret = unsafe { nc::execve("/bin/ls", &args, &env) };
assert!(ret.is_ok());
}
Ok(child_pid) => {
// Parent process
println!("[main] child pid is: {child_pid}");
}
}
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 sa = nc::new_sigaction(handle_alarm);
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));
}
This library is govered by Apache-2.0 License.