pub const fn strerror(errno: Errno) -> &'static str
Expand description
Get errno description.
Examples found in repository?
More examples
examples/bind_device.rs (line 18)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() -> Result<(), nc::Errno> {
let socket_fd = unsafe { nc::socket(nc::AF_INET, nc::SOCK_STREAM, 0)? };
let interface_name = "lo";
let ret = unsafe {
nc::setsockopt(
socket_fd,
nc::SOL_SOCKET,
nc::SO_BINDTODEVICE,
interface_name.as_ptr() as usize,
interface_name.len() as nc::socklen_t,
)
};
match ret {
Err(errno) => eprintln!("socket() err: {}", nc::strerror(errno)),
Ok(_) => println!("Now socket is bind to {}", interface_name),
}
let ret = unsafe { nc::close(socket_fd) };
assert!(ret.is_ok());
Ok(())
}
examples/stat.rs (line 38)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() {
let mut statbuf = nc::stat_t::default();
let filepath = "/dev/fd/0";
#[cfg(target_os = "linux")]
let ret = {
#[cfg(not(any(
target_arch = "aarch64",
target_arch = "loongarch64",
target_arch = "riscv64"
)))]
unsafe {
nc::stat(filepath, &mut statbuf)
}
#[cfg(any(
target_arch = "aarch64",
target_arch = "loongarch64",
target_arch = "riscv64"
))]
unsafe {
nc::fstatat(nc::AT_FDCWD, filepath, &mut statbuf, 0)
}
};
#[cfg(any(target_os = "android", target_os = "freebsd"))]
let ret = unsafe { nc::fstatat(nc::AT_FDCWD, filepath, &mut statbuf, 0) };
match ret {
Ok(_) => {
println!("s: {:?}", statbuf);
}
Err(errno) => {
eprintln!(
"Failed to get file status, got errno: {}",
nc::strerror(errno)
);
}
}
}
examples/mq.rs (line 46)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
let name = "nc-mq-notify";
let ret = unsafe {
nc::mq_open(
name,
nc::O_CREAT | nc::O_RDWR | nc::O_EXCL,
(nc::S_IRUSR | nc::S_IWUSR) as nc::umode_t,
None,
)
};
assert!(ret.is_ok());
let mq_id = ret.unwrap();
let mut attr = nc::mq_attr_t::default();
let ret = unsafe { nc::mq_getsetattr(mq_id, None, Some(&mut attr)) };
assert!(ret.is_ok());
println!("attr: {:?}", attr);
let msg = "Hello, Rust";
let prio = 42;
let timeout = nc::timespec_t {
tv_sec: 1,
tv_nsec: 0,
};
let ret = unsafe { nc::mq_timedsend(mq_id, msg.as_bytes(), msg.len(), prio, &timeout) };
assert!(ret.is_ok());
let ret = unsafe { nc::mq_getsetattr(mq_id, None, Some(&mut attr)) };
assert!(ret.is_ok());
assert_eq!(attr.mq_curmsgs, 1);
let mut buf = vec![0_u8; attr.mq_msgsize as usize];
let buf_len = buf.len();
let mut recv_prio = 0;
let read_timeout = nc::timespec_t {
tv_sec: 1,
tv_nsec: 0,
};
let ret =
unsafe { nc::mq_timedreceive(mq_id, &mut buf, buf_len, &mut recv_prio, &read_timeout) };
if let Err(errno) = ret {
eprintln!("mq_timedreceive() error: {}", nc::strerror(errno));
}
assert!(ret.is_ok());
let n_read = ret.unwrap() as usize;
assert_eq!(n_read, msg.len());
let ret = unsafe { nc::close(mq_id) };
assert!(ret.is_ok());
let ret = unsafe { nc::mq_unlink(name) };
assert!(ret.is_ok());
}
examples/tee.rs (line 90)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
fn main() {
let output_file = "/tmp/nc-splice";
let ret = unsafe {
nc::openat(
nc::AT_FDCWD,
output_file,
nc::O_WRONLY | nc::O_CREAT | nc::O_TRUNC,
0o644,
)
};
assert!(ret.is_ok());
let fd = ret.unwrap();
// Tee stdin to stdout
loop {
let stdin_fileno = 0;
let stdout_fileno = 1;
let ret = unsafe {
nc::tee(
stdin_fileno,
stdout_fileno,
usize::MAX,
nc::SPLICE_F_NONBLOCK,
)
};
let mut tee_len = match ret {
Ok(0) => break,
Err(nc::EAGAIN) => continue,
Err(errno) => {
eprintln!("tee error: {}", nc::strerror(errno));
unsafe { nc::exit(1) };
}
Ok(len) => len,
};
// Consume stdin by splicing it to a file.
while tee_len > 0 {
let ret = unsafe {
nc::splice(
stdin_fileno,
None,
fd,
None,
tee_len as usize,
nc::SPLICE_F_MOVE,
)
};
match ret {
Err(errno) => {
eprintln!("splice error: {}", nc::strerror(errno));
break;
}
Ok(len) => tee_len -= len,
}
}
}
let ret = unsafe { nc::close(fd) };
assert!(ret.is_ok());
}
Additional examples can be found in: