logo
down
shadow

How do I close a Unix socket in Rust?


How do I close a Unix socket in Rust?

Content Index :

How do I close a Unix socket in Rust?
Tag : sockets , By : Matthew Steed
Date : November 28 2020, 11:01 PM

it helps some times Please learn to create a minimal reproducible example and then take the time to do so. In this case, there's no need for threads or functions or testing frameworks; running this entire program twice reproduces the error:
use std::os::unix::net::UnixListener;

fn main() {
    UnixListener::bind("/tmp/my_socket.sock").unwrap();
}
use std::path::{Path, PathBuf};

struct DeleteOnDrop {
    path: PathBuf,
    listener: UnixListener,
}

impl DeleteOnDrop {
    fn bind(path: impl AsRef<Path>) -> std::io::Result<Self> {
        let path = path.as_ref().to_owned();
        UnixListener::bind(&path).map(|listener| DeleteOnDrop { path, listener })
    }
}

impl Drop for DeleteOnDrop {
    fn drop(&mut self) {
        // There's no way to return a useful error here
        let _ = std::fs::remove_file(&self.path).unwrap();
    }
}
impl std::ops::Deref for DeleteOnDrop {
    type Target = UnixListener;

    fn deref(&self) -> &Self::Target {
        &self.listener
    }
}

impl std::ops::DerefMut for DeleteOnDrop {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.listener
    }
}
use std::path::Path;

fn bind(path: impl AsRef<Path>) -> std::io::Result<UnixListener> {
    let path = path.as_ref();
    std::fs::remove_file(path)?;
    UnixListener::bind(path)
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Is there a way to close a Unix socket for only reading or writing?


Tag : unix , By : apple
Date : March 29 2020, 07:55 AM
help you fix your problem You can shutdown a socket for read or write using the second parameter to the method:
shutdown(sock, SHUT_RD)
shutdown(sock, SHUT_WR)

Porting Windows socket program to Unix: Alternative for winsock32 APIs in unix


Tag : cpp , By : LinnheCreative
Date : March 29 2020, 07:55 AM
wish helps you You can track the closure event when performing reading. The socket is closed when read returns 0 (talking about "Berkeley sockets" of course).
//EDIT: Use poll or select to wait for some event to occur (data arrival, socket closure ...).

why netty can not make a distinction between socket close and socket half close


Tag : development , By : RichAA
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Nobody can tell the difference. Shutdown for output sends a FIN. Close sends a FIN, unless it has already been sent, i.e. by a shutdown. FIN appears at the receiver as the EOS (EOF) condition. The server got the FIN from the shutdown, saw that as EOS, closed the socket, your client got the FIN, saw that as EOS, closed the socket, and sent ... nothing to the server, because the FIN had already been sent.

Error: Get http://unix.socket/1.0: dial unix /var/snap/lxd/common/lxd/unix.socket: connect: permission denied


Tag : development , By : Stephen Dewar
Date : March 29 2020, 07:55 AM
This might help you You will have to execute the following command in order to provide access to the current (non-root) user:
newgrp lxd

sudo usermod -a -G lxd $(whoami)
/snap/bin/lxc query --wait -X GET /1.0

How to communicate a Rust and a Ruby process using a Unix socket pair


Tag : ruby , By : Feroz
Date : March 29 2020, 07:55 AM
wish helps you The problem seems to be that Rust sets all file descriptors to be closed when a child is created by setting the FD_CLOEXEC flag. The only way to get around this seems to be using libc to call fcntl.
Here’s some code that seems to work, but I don’t know Rust, so use at your own risk. Another issue you will have is you need to close the parent side of rust_socket after spawing the child, otherwise read_to_string will block forever waiting for the stream to be closed. You can do this with drop, but you will also need to use AsRawFd rather than IntoRawFd:
use std::process::Command;
use std::os::unix::net::UnixStream;
use std::os::unix::io::AsRawFd;
use std::io::Read;

extern crate libc;

fn main() {
    // Create the socket pair.
    let (rust_socket, mut ruby_socket) = UnixStream::pair().unwrap();

    // Unset FD_CLOEXEC on the socket to be passed to the child.
    let fd = rust_socket.as_raw_fd();
    unsafe {
        let flags = libc::fcntl(fd, libc::F_GETFD);
        libc::fcntl(fd, libc::F_SETFD, flags & !libc::FD_CLOEXEC);
    }

    // Spawn the child
    let _output = Command::new("ruby")
        .args(&["client.rb", &fd.to_string()])
        .spawn();

    // After spawning, close the parents side of rust_socket.
    // If we use IntoRawFd, rust_socket would have been moved by this point
    // so we need AsRawFD instead.
    drop(rust_socket);

    let mut response = String::new();
    ruby_socket.read_to_string(&mut response).unwrap();

    println!("Ruby said '{}'", response);
}
Related Posts Related QUESTIONS :
shadow
Privacy Policy - Terms - Contact Us © scrbit.com