Fix error when PF_UNIX is passed as protocol to create unix socket

Zero and PF_UNIX are both supported as protocol when creating unix
socket.
This commit is contained in:
He Sun 2020-05-08 17:09:28 +08:00 committed by Tate, Hongliang Tian
parent e166382923
commit 7a87d77509
2 changed files with 3 additions and 2 deletions

@ -184,12 +184,13 @@ enum Status {
impl UnixSocket { impl UnixSocket {
/// C/S 1: Create a new unix socket /// C/S 1: Create a new unix socket
pub fn new(socket_type: c_int, protocol: c_int) -> Result<Self> { pub fn new(socket_type: c_int, protocol: c_int) -> Result<Self> {
if socket_type == libc::SOCK_STREAM && protocol == 0 { if socket_type == libc::SOCK_STREAM && (protocol == 0 || protocol == libc::PF_UNIX) {
Ok(UnixSocket { Ok(UnixSocket {
obj: None, obj: None,
status: Status::None, status: Status::None,
}) })
} else { } else {
// Return different error numbers according to input
return_errno!(ENOSYS, "unimplemented unix socket type") return_errno!(ENOSYS, "unimplemented unix socket type")
} }
} }

@ -34,7 +34,7 @@ int create_connected_sockets(int *sockets, char *sock_path) {
THROW_ERROR("failed to listen"); THROW_ERROR("failed to listen");
} }
int client_fd = socket(AF_UNIX, SOCK_STREAM, 0); int client_fd = socket(AF_UNIX, SOCK_STREAM, PF_UNIX);
if (client_fd == -1) { if (client_fd == -1) {
close(listen_fd); close(listen_fd);
THROW_ERROR("failed to create a unix socket"); THROW_ERROR("failed to create a unix socket");