From b11b57723b963fb5d0ab11748b7957f0b7fa5d37 Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Mon, 23 Oct 2017 19:24:07 -0200 Subject: [PATCH] lib: optimize sockunion_connect() This function is only called with non-blocking sockets [1], so there's no need to worry about setting O_NONBLOCK and unsetting it later if the given fd was a blocking socket. This saves us 4 syscalls per connect, which is not much but is something. Also, remove an outdated comment about the return values of this function. It returns a 'connect_result' enum now, whose values are self-explanatory (connect_error, connect_success and connect_in_progress). This also fixes a coverity scan warning where we weren't checking the return value of the fcntl() syscall. [1] bgp_connect() and pim_msdp_sock_connect(). Signed-off-by: Renato Westphal --- lib/sockunion.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/sockunion.c b/lib/sockunion.c index 559ae37ffb..ab8d8be3e5 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -175,15 +175,11 @@ static int sockunion_sizeof(const union sockunion *su) return ret; } -/* sockunion_connect returns - -1 : error occured - 0 : connect success - 1 : connect is in progress */ +/* Performs a non-blocking connect(). */ enum connect_result sockunion_connect(int fd, const union sockunion *peersu, unsigned short port, ifindex_t ifindex) { int ret; - int val; union sockunion su; memcpy(&su, peersu, sizeof(union sockunion)); @@ -203,18 +199,12 @@ enum connect_result sockunion_connect(int fd, const union sockunion *peersu, break; } - /* Make socket non-block. */ - val = fcntl(fd, F_GETFL, 0); - fcntl(fd, F_SETFL, val | O_NONBLOCK); - /* Call connect function. */ ret = connect(fd, (struct sockaddr *)&su, sockunion_sizeof(&su)); /* Immediate success */ - if (ret == 0) { - fcntl(fd, F_SETFL, val); + if (ret == 0) return connect_success; - } /* If connect is in progress then return 1 else it's real error. */ if (ret < 0) { @@ -227,8 +217,6 @@ enum connect_result sockunion_connect(int fd, const union sockunion *peersu, } } - fcntl(fd, F_SETFL, val); - return connect_in_progress; } -- 2.39.5