[PATCH] libosmocore[master]: socket.c: osmo_sock_init2: Several logic fixes and log impro...

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/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Thu Apr 5 15:08:38 UTC 2018


Review at  https://gerrit.osmocom.org/7645

socket.c: osmo_sock_init2: Several logic fixes and log improvements

After investigating osmo-msc showing this log message and looking at the
code, it's a bit difficult to find out what's going on in the code:
socket.c:224 unable to bind socket: (null):0: Protocol not supported

The root cause was not yet found, but probably SCTP is not enabled in
the kernel of the host running it.

The cod eis most probably failing during socket() and not due to bind
error as the log says, so let's print an error if socket() fails.

Then, if setsockopt fails, we want to still keep trying in case an extra
addr was offered by addrinfo_helper. It is definetly wrong to continue
if setsockopt fails, because then we are skipping the bind(), which is a
fundamental part of what osmo_sock_init2 does.

Then, let's print the bind error when it really happens, and re-write
the extra log at the end if we reach the point at which no suitable addr
is found.

Change-Id: I1854422ad92dadf33ed4d849e15c0380c3bf1626
---
M src/socket.c
1 file changed, 15 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/45/7645/1

diff --git a/src/socket.c b/src/socket.c
index 0378970..60369b1 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -91,8 +91,11 @@
 	int sfd, on = 1;
 
 	sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
-	if (sfd == -1)
+	if (sfd == -1) {
+		LOGP(DLGLOBAL, LOGL_ERROR,
+			"unable to create socket: %s\n", strerror(errno));
 		return sfd;
+	}
 	if (flags & OSMO_SOCK_F_NONBLOCK) {
 		if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
 			LOGP(DLGLOBAL, LOGL_ERROR,
@@ -212,16 +215,21 @@
 					"cannot setsockopt socket:"
 					" %s:%u: %s\n",
 					local_host, local_port, strerror(errno));
-				break;
+				close(sfd);
+				continue;
 			}
-			if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
-				break;
-			close(sfd);
+			if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) {
+				LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n",
+					local_host, local_port, strerror(errno));
+				close(sfd);
+				continue;
+			}
+			break;
 		}
 		freeaddrinfo(result);
 		if (rp == NULL) {
-			LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n",
-				local_host, local_port, strerror(errno));
+			LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n",
+				local_host, local_port);
 			return -ENODEV;
 		}
 	}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1854422ad92dadf33ed4d849e15c0380c3bf1626
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list