Function nc::close

source ·
pub unsafe fn close(fd: i32) -> Result<(), Errno>
Expand description

Close a file descriptor.

§Example

const STDERR_FD: i32 = 2;
let ret = unsafe { nc::close(STDERR_FD) };
assert!(ret.is_ok());
Examples found in repository?
examples/file.rs (line 48)
45
46
47
48
49
50
51
    fn drop(&mut self) {
        if self.fd > -1 {
            println!("closing fd: {}", self.fd);
            let _ = unsafe { nc::close(self.fd) };
            self.fd = -1;
        }
    }
More examples
Hide additional examples
examples/unlink.rs (line 14)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    #[cfg(feature = "std")]
    let path = std::path::Path::new("/tmp/nc-unlink");
    #[cfg(not(feature = "std"))]
    let path = "/tmp/nc.unlink";

    let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_WRONLY | nc::O_CREAT, 0o644) };
    assert!(ret.is_ok());
    let fd = ret.unwrap();
    let ret = unsafe { nc::close(fd) };
    assert!(ret.is_ok());
    let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, path, 0) };
    assert!(ret.is_ok());
}
examples/bind_device.rs (line 21)
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/open.rs (line 31)
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
fn main() -> Result<(), nc::Errno> {
    let path = "/tmp/hello.rs";

    #[cfg(target_os = "freebsd")]
    let fd = unsafe {
        nc::open(
            path,
            nc::O_CREAT | nc::O_RDWR,
            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
        )?
    };

    #[cfg(any(target_os = "linux", target_os = "android"))]
    let fd = unsafe {
        nc::openat(
            nc::AT_FDCWD,
            path,
            nc::O_CREAT | nc::O_RDWR,
            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
        )?
    };

    let msg = "fn main() { println!(\"Hello, world\");}";

    let n_write = unsafe { nc::write(fd, msg.as_ptr() as usize, msg.len()) };
    assert!(n_write.is_ok());
    let ret = unsafe { nc::close(fd) };
    assert!(ret.is_ok());
    Ok(())
}
examples/tcp_fast_open.rs (line 28)
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
fn main() -> Result<(), nc::Errno> {
    let socket_fd = unsafe { nc::socket(nc::AF_INET, nc::SOCK_STREAM, 0)? };

    // For Linux, value is the queue length of pending packets.
    // See https://github.com/rust-lang/socket2/issues/49
    #[cfg(any(target_os = "linux", target_os = "android"))]
    let queue_len: i32 = 5;
    // For the others, just a boolean value for enable and disable.
    #[cfg(target_os = "freebsd")]
    let queue_len: i32 = 1;
    let queue_len_ptr = &queue_len as *const i32 as usize;

    let ret = unsafe {
        nc::setsockopt(
            socket_fd,
            nc::IPPROTO_TCP,
            nc::TCP_FASTOPEN,
            queue_len_ptr,
            std::mem::size_of_val(&queue_len) as u32,
        )
    };
    assert!(ret.is_ok());

    unsafe { nc::close(socket_fd) }
}
examples/read.rs (line 35)
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
fn main() -> Result<(), nc::Errno> {
    #[cfg(any(target_os = "linux", target_os = "android"))]
    let fd = unsafe { nc::openat(nc::AT_FDCWD, "/etc/passwd", nc::O_RDONLY, 0)? };

    #[cfg(target_os = "freebsd")]
    let fd = unsafe { nc::open("/etc/passwd", nc::O_RDONLY, 0)? };

    let mut buf: [u8; 256] = [0; 256];
    loop {
        let n_read = unsafe { nc::read(fd, buf.as_mut_ptr() as usize, buf.len()) };
        match n_read {
            Ok(n) => {
                if n == 0 {
                    break;
                }
                // FIXME(Shaohua): Read buf with len(n).
                if let Ok(s) = std::str::from_utf8(&buf) {
                    print!("s: {}", s);
                } else {
                    eprintln!("Failed to read buf as UTF-8!");
                    break;
                }
            }
            Err(errno) => {
                eprintln!("Failed to read, got errno: {}", errno);
                break;
            }
        }
    }

    unsafe { nc::close(fd) }
}