[MERGED] libosmocore[master]: socket: Unify listen() calls and check for erroneous returns

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Thu Jul 13 15:11:16 UTC 2017


Harald Welte has submitted this change and it was merged.

Change subject: socket: Unify listen() calls and check for erroneous returns
......................................................................


socket: Unify listen() calls and check for erroneous returns

We had three places at the end of socket initialization functions
calling listen().  Let's unify that and fix some bugs:
* close + return error in case of bad listen() result
* don't call listen() on AF_UNIX SOCK_DGRAM sockets

Change-Id: I7e8dbe3c0486bb3b9810b0add1331e93fc106d82
---
M src/socket.c
1 file changed, 33 insertions(+), 20 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/src/socket.c b/src/socket.c
index d18f756..d0e4c24 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -104,6 +104,25 @@
 }
 
 
+static int osmo_sock_init_tail(int fd, uint16_t type, unsigned int flags)
+{
+	int rc = 0;
+
+	/* Make sure to call 'listen' on a bound, connection-oriented sock */
+	if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) {
+		switch (type) {
+		case SOCK_STREAM:
+		case SOCK_SEQPACKET:
+			rc = listen(fd, 10);
+		}
+	}
+
+	if (rc < 0)
+		LOGP(DLGLOBAL, LOGL_ERROR, "unable to listen on socket: %s\n", strerror(errno));
+
+	return rc;
+}
+
 /*! Initialize a socket (including bind and/or connect)
  *  \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC
  *  \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
@@ -219,15 +238,12 @@
 		}
 	}
 
-	/* Make sure to call 'listen' on a bound, connection-oriented sock */
-	if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) {
-		switch (type) {
-		case SOCK_STREAM:
-		case SOCK_SEQPACKET:
-			listen(sfd, 10);
-			break;
-		}
+	rc = osmo_sock_init_tail(sfd, type, flags);
+	if (rc < 0) {
+		close(sfd);
+		sfd = -1;
 	}
+
 	return sfd;
 }
 
@@ -305,15 +321,12 @@
 
 	setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
 
-	/* Make sure to call 'listen' on a bound, connection-oriented sock */
-	if (flags & OSMO_SOCK_F_BIND) {
-		switch (type) {
-		case SOCK_STREAM:
-		case SOCK_SEQPACKET:
-			listen(sfd, 10);
-			break;
-		}
+	rc = osmo_sock_init_tail(sfd, type, flags);
+	if (rc < 0) {
+		close(sfd);
+		sfd = -1;
 	}
+
 	return sfd;
 }
 
@@ -545,10 +558,10 @@
 		}
 	}
 
-	if (flags & OSMO_SOCK_F_BIND) {
-		rc = listen(sfd, 10);
-		if (rc < 0)
-			goto err;
+	rc = osmo_sock_init_tail(sfd, type, flags);
+	if (rc < 0) {
+		close(sfd);
+		sfd = -1;
 	}
 
 	return sfd;

-- 
To view, visit https://gerrit.osmocom.org/3239
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7e8dbe3c0486bb3b9810b0add1331e93fc106d82
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list