'Can I delete a UDS Socket file using "rm" command

If a process was abruptly stopped by using "kill" command and due to that it wouldn't delete the UDS sockets that were created by that process. In such cases, can we use "rm" command to delete the socket file.

By any chance even after deleting the socket file using "rm" command, will there be any stale entities present in the kernel which might cause "Bad file descriptor" error if any other process tries to create the socket file with same name.?



Solution 1:[1]

Yes, you can delete it. It would be sensible for a program that uses UNIX domain sockets to delete its own leftover socket, if it is still there when the program starts.

"Bad file descriptor" is always a bug in the program. It means your program tried to use some file descriptor number that wasn't actually a file descriptor.

Solution 2:[2]

The "Notes" section of the Man 7 unix page says:

Binding to a socket with a filename creates a socket in the file system that must be deleted by the caller when it is no longer needed (using unlink(2)). The usual UNIX close-behind semantics apply; the socket can be unlinked at any time and will be finally removed from the file system when the last reference to it is closed.

According to the man page, you should be able to unlink() the file of the file system immediately after bind():ing the filename for the socket. So the file will be removed automatically when it becomes unusable.

With Linux, you can also bind "Abstract names" for Unix domain sockets. That way you can avoid usage of filesystem with Unix domain sockets.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user253751
Solution 2 SKi