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

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(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/39/3239/1

diff --git a/src/socket.c b/src/socket.c
index 688fcb3..372051f 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: newchange
Gerrit-Change-Id: I7e8dbe3c0486bb3b9810b0add1331e93fc106d82
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <[email protected]>

Reply via email to