[MERGED] libosmocore[master]: osmo_sock_init(): include host and port in error messages

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
Sat Aug 27 02:01:20 UTC 2016


Harald Welte has submitted this change and it was merged.

Change subject: osmo_sock_init(): include host and port in error messages
......................................................................


osmo_sock_init(): include host and port in error messages

For programs like osmo-hnbgw with numerous sockets, the message that some
unspecified connection was refused is not very helpful. Also output the host
and port where an error occured.

Instead of perror, use fprintf(stderr, ..., strerror()) to be able to include a
format string and print host and port as passed to osmo_sock_init().

Change-Id: I8d0343f51310699b78fcb83fd76fd93764acf3dc
---
M src/socket.c
1 file changed, 16 insertions(+), 5 deletions(-)

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



diff --git a/src/socket.c b/src/socket.c
index 7e610bf..a36aee7 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -71,8 +71,11 @@
 	char portbuf[16];
 
 	if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) ==
-		     (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT))
+		     (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) {
+		fprintf(stderr, "invalid: both bind and connect flags set:"
+			" %s:%u: %s\n", host, port);
 		return -EINVAL;
+	}
 
 	sprintf(portbuf, "%u", port);
 	memset(&hints, 0, sizeof(struct addrinfo));
@@ -93,7 +96,8 @@
 
 	rc = getaddrinfo(host, portbuf, &hints, &result);
 	if (rc != 0) {
-		perror("getaddrinfo returned NULL");
+		fprintf(stderr, "getaddrinfo returned NULL: %s:%u: %s\n",
+			host, port, strerror(errno));
 		return -EINVAL;
 	}
 
@@ -109,7 +113,10 @@
 			continue;
 		if (flags & OSMO_SOCK_F_NONBLOCK) {
 			if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
-				perror("cannot set this socket unblocking");
+				fprintf(stderr,
+					"cannot set this socket unblocking:"
+					" %s:%u: %s\n",
+					host, port, strerror(errno));
 				close(sfd);
 				return -EINVAL;
 			}
@@ -122,7 +129,10 @@
 			rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
 							&on, sizeof(on));
 			if (rc < 0) {
-				perror("cannot setsockopt socket");
+				fprintf(stderr,
+					"cannot setsockopt socket:"
+					" %s:%u: %s\n",
+					host, port, strerror(errno));
 				break;
 			}
 			if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
@@ -133,7 +143,8 @@
 	freeaddrinfo(result);
 
 	if (rp == NULL) {
-		perror("unable to connect/bind socket");
+		fprintf(stderr, "unable to connect/bind socket: %s:%u: %s\n",
+			host, port, strerror(errno));
 		return -ENODEV;
 	}
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I8d0343f51310699b78fcb83fd76fd93764acf3dc
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list