VCL-LDPRELOAD: enable accept4() wrapper

Change-Id: I56567953fb8329315393047913e30fc7f2242bdb
Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
diff --git a/src/vcl/sock_test_server.c b/src/vcl/sock_test_server.c
index 753a7da..728a6a7 100644
--- a/src/vcl/sock_test_server.c
+++ b/src/vcl/sock_test_server.c
@@ -304,6 +304,8 @@
 				     -1.0 /* wait forever */ );
   if (client_fd < 0)
     errno = -client_fd;
+#elif HAVE_ACCEPT4
+  client_fd = accept4 (ssm->listen_fd, (struct sockaddr *) NULL, NULL, NULL);
 #else
   client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
 #endif
diff --git a/src/vcl/vcom.c b/src/vcl/vcom.c
index b215771..ed52bfa 100644
--- a/src/vcl/vcom.c
+++ b/src/vcl/vcom.c
@@ -2568,7 +2568,6 @@
   return libc_accept (__fd, __addr, __addr_len);
 }
 
-#ifdef __USE_GNU
 /*
  * Similar to 'accept' but takes an additional parameter to specify
  * flags.
@@ -2592,9 +2591,14 @@
 accept4 (int __fd, __SOCKADDR_ARG __addr,
 	 socklen_t * __restrict __addr_len, int __flags)
 {
-  int rv;
+  int rv = 0;
   pid_t pid = getpid ();
 
+  fprintf (stderr,
+	   "[%d] accept4: in the beginning... "
+	   "'%04d'='%04d', '%p', '%p', '%04x'\n",
+	   pid, rv, __fd, __addr, __addr_len, __flags);
+
   if (is_vcom_socket_fd (__fd))
     {
       if (VCOM_DEBUG > 0)
@@ -2602,7 +2606,7 @@
       rv = vcom_accept4 (__fd, __addr, __addr_len, __flags);
       if (VCOM_DEBUG > 0)
 	fprintf (stderr,
-		 "[%d] accept4: "
+		 "[%d] accept4: VCL "
 		 "'%04d'='%04d', '%p', '%p', '%04x'\n",
 		 pid, rv, __fd, __addr, __addr_len, __flags);
       if (VCOM_DEBUG > 0)
@@ -2614,11 +2618,14 @@
 	}
       return rv;
     }
+  fprintf (stderr,
+	   "[%d] accept4: libc "
+	   "'%04d'='%04d', '%p', '%p', '%04x'\n",
+	   pid, rv, __fd, __addr, __addr_len, __flags);
+
   return libc_accept4 (__fd, __addr, __addr_len, __flags);
 }
 
-#endif
-
 /*
  * Shut down all or part of the connection open on socket FD.
  * HOW determines what to shut down:
@@ -2822,7 +2829,7 @@
 
   rv =
     vcom_socket_epoll_pwait (__epfd, __events, __maxevents, __timeout, NULL);
-  if (VCOM_DEBUG > 0)
+  if (VCOM_DEBUG > 1)
     fprintf (stderr,
 	     "[%d] epoll_wait: "
 	     "'%04d'='%04d', '%p', "
diff --git a/src/vcl/vcom_glibc_socket.h b/src/vcl/vcom_glibc_socket.h
index 4eb60fb..969029f 100644
--- a/src/vcl/vcom_glibc_socket.h
+++ b/src/vcl/vcom_glibc_socket.h
@@ -249,7 +249,6 @@
 extern int
 accept (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len);
 
-#ifdef __USE_GNU
 /* Similar to 'accept' but takes an additional parameter to specify flags.
 
    This function is a cancellation point and therefore not marked with
@@ -258,7 +257,6 @@
 extern int
 accept4 (int __fd, __SOCKADDR_ARG __addr,
 	 socklen_t * __restrict __addr_len, int __flags);
-#endif
 
 /* Shut down all or part of the connection open on socket FD.
    HOW determines what to shut down:
diff --git a/src/vcl/vcom_socket.c b/src/vcl/vcom_socket.c
index ece9b69..6fcc4e5 100644
--- a/src/vcl/vcom_socket.c
+++ b/src/vcl/vcom_socket.c
@@ -781,10 +781,24 @@
 {
   int rv;
 
-  if (__cmd == FIONREAD)
-    rv = vppcom_session_attr (__sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
-  else
-    rv = -EOPNOTSUPP;
+  switch (__cmd)
+    {
+    case FIONREAD:
+      rv = vppcom_session_attr (__sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
+      break;
+
+    case FIONBIO:
+      {
+	u32 flags = va_arg (__ap, int) ? O_NONBLOCK : 0;
+	u32 len = sizeof (flags);
+	rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_FLAGS, &flags, &len);
+      }
+      break;
+
+    default:
+      rv = -EOPNOTSUPP;
+      break;
+    }
   return rv;
 }
 
@@ -1015,7 +1029,6 @@
 		    struct timeval *__restrict timeout)
 {
   static unsigned long vcom_nsid_fds = 0;
-  vcom_socket_main_t *vsm = &vcom_socket_main;
   int vcom_nsid = 0;
   int rv = -EBADF;
 
@@ -1086,14 +1099,14 @@
 	  if (VCOM_DEBUG > 0)
 	    fprintf (stderr,
 		     "[%d] vcom_socket_select called to "
-		     "emulate delay_ns()!\n", vsm->my_pid);
+		     "emulate delay_ns()!\n", getpid ());
 	  rv = vppcom_select (0, NULL, NULL, NULL, time_to_wait);
 	}
       else
 	{
 	  fprintf (stderr, "[%d] vcom_socket_select called vcom_nfds = 0 "
 		   "and invalid time_to_wait (%f)!\n",
-		   vsm->my_pid, time_to_wait);
+		   getpid (), time_to_wait);
 	}
       return 0;
     }
@@ -1128,7 +1141,7 @@
 		      NULL, time_to_wait);
   if (VCOM_DEBUG > 2)
     fprintf (stderr, "[%d] called vppcom_select(): "
-	     "'%04d'='%04d'\n", vsm->my_pid, rv, (int) vcom_nsid_fds);
+	     "'%04d'='%04d'\n", getpid (), rv, (int) vcom_nsid_fds);
 
   /* check if any file descriptors changed status */
   if (rv > 0)
@@ -2278,9 +2291,17 @@
   /* flags can be 0 or can be bitwise OR
    * of any of SOCK_NONBLOCK and SOCK_CLOEXEC */
 
+  if (VCOM_DEBUG > 2)
+    fprintf (stderr, "[%d] vcom_socket_accept_flags: "
+	     "fd = %d, __addr = %p, __addr_len = %p  flags = %d (0x%x)\n",
+	     getpid (), __fd, __addr, __addr_len, flags, flags);
+
   if (!(!flags || (flags & (SOCK_NONBLOCK | SOCK_CLOEXEC))))
     {
       /* TBD: return proper error code */
+      fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
+	       "invalid flags = %d (0x%x)\n", getpid (), flags, flags);
+
       return -1;
     }
 
@@ -2288,6 +2309,8 @@
 
   if (!vcom_socket_is_connection_mode_socket (__fd))
     {
+      fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
+	       "connection mode socket support TBD!\n", getpid ());
       return -EOPNOTSUPP;
     }
 
@@ -2300,6 +2323,8 @@
       rv = vcom_fcntl (vsock->fd, F_GETFL, 0);
       if (rv < 0)
 	{
+	  fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
+		   "vcom_fcnt() returned %d!\n", getpid (), rv);
 	  return rv;
 	}
 
@@ -2334,6 +2359,9 @@
 	}
       if (rv < 0)
 	{
+	  if (rv != VPPCOM_EAGAIN)
+	    fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
+		     "vppcom_session_accept() returned %d!", getpid (), rv);
 	  return rv;
 	}
 
@@ -2347,6 +2375,9 @@
 					 &domain, &type, &protocol, flags);
       if (fd < 0)
 	{
+	  fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
+		   "vcom_socket_connected_socket() returned %d!",
+		   getpid (), rv);
 	  return fd;
 	}
 
@@ -2426,18 +2457,6 @@
 		}
 	    }
 	}
-      else
-	{
-	  /* when __addr is NULL, nothing is filled in,
-	   * in this case, __addr_len is not used,
-	   * and should also be null
-	   * */
-	  if (__addr_len)
-	    {
-	      /* TBD: return proper error code */
-	      return -1;
-	    }
-	}
     }
 
   return rv;
@@ -2451,7 +2470,6 @@
   return vcom_socket_accept_flags (__fd, __addr, __addr_len, 0);
 }
 
-#ifdef __USE_GNU
 int
 vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
 		     socklen_t * __restrict __addr_len, int __flags)
@@ -2459,7 +2477,6 @@
   /*  SOCK_NONBLOCK and SOCK_CLOEXEC can be bitwise ORed in flags */
   return vcom_socket_accept_flags (__fd, __addr, __addr_len, __flags);
 }
-#endif
 
 /* TBD: move it to vppcom */
 static inline int
@@ -2757,7 +2774,6 @@
 				struct epoll_event *__event,
 				int free_vepitem_on_del)
 {
-  vcom_socket_main_t *vsm = &vcom_socket_main;
   int rv = -1;
   i32 cnt;
   vcom_epoll_t *vepoll;
@@ -2790,7 +2806,7 @@
 		 "[%d] vcom_socket_epoll_ctl_i: vppcom_epoll_ctl() "
 		 "returned %d\n\tepfd %d, vep_idx %d, fd %d sid %d op %d"
 		 "\n\tcount %d, vcl_cnt %d, libc_cnt %d\n",
-		 vsm->my_pid, rv, __epfd, vep_idx, __fd, sid, __op,
+		 getpid (), rv, __epfd, vep_idx, __fd, sid, __op,
 		 vepoll->count, vepoll->vcl_cnt, vepoll->libc_cnt);
     }
   else
@@ -2808,7 +2824,7 @@
 		 "[%d] vcom_socket_epoll_ctl_i: libc_epoll_ctl() "
 		 "returned %d\n\tepfd %d, vep_idx %d, fd %d sid %d op %d"
 		 "\n\tcount %d, vcl_cnt %d, libc_cnt %d\n",
-		 vsm->my_pid, rv, __epfd, vep_idx, __fd, sid, __op,
+		 getpid (), rv, __epfd, vep_idx, __fd, sid, __op,
 		 vepoll->count, vepoll->vcl_cnt, vepoll->libc_cnt);
     }
 
@@ -2853,7 +2869,7 @@
   if (!__events || (__timeout < -1))
     {
       fprintf (stderr, "[%d] ERROR: vcom_socket_epoll_pwait: "
-	       "Bad args __events %p, __timeout %d\n", vsm->my_pid,
+	       "Bad args __events %p, __timeout %d\n", getpid (),
 	       __events, __timeout);
       rv = -EFAULT;
       goto out;
@@ -2866,7 +2882,7 @@
   if (vep_idx == INVALID_VEP_IDX)
     {
       fprintf (stderr, "[%d] ERROR: vcom_socket_epoll_pwait: "
-	       "Bad epoll fd %d\n", vsm->my_pid, __epfd);
+	       "Bad epoll fd %d\n", getpid (), __epfd);
       return -EBADF;
     }
 
@@ -2874,21 +2890,31 @@
     {
       fprintf (stderr, "[%d] ERROR: vcom_socket_epoll_pwait: No events"
 	       " in epfd!\n\tcount %d, vcl_cnt %d, libc_cnt %d\n",
-	       vsm->my_pid, vepoll->count, vepoll->vcl_cnt, vepoll->libc_cnt);
+	       getpid (), vepoll->count, vepoll->vcl_cnt, vepoll->libc_cnt);
       rv = -EINVAL;
       goto out;
     }
 
   if (vepoll->libc_cnt == 0)
     {
+      if (VCOM_DEBUG > 2)
+	fprintf (stderr, "[%d] vcom_socket_epoll_pwait: libc_cnt = 0, "
+		 "calling vppcom_epoll_wait()\n", getpid ());
       rv = vppcom_epoll_wait (vep_idx, __events, __maxevents, time_to_wait);
     }
   else if (vepoll->vcl_cnt == 0)
     {
+      if (VCOM_DEBUG > 2)
+	fprintf (stderr, "[%d] vcom_socket_epoll_pwait: vcl_cnt = 0, "
+		 "calling libc_epoll_pwait()\n", getpid ());
       rv = libc_epoll_pwait (__epfd, __events, __maxevents, __timeout, __ss);
     }
   else
     {
+      if (VCOM_DEBUG > 2)
+	fprintf (stderr, "[%d] vcom_socket_epoll_pwait: vcl_cnt = %d, "
+		 "libc_cnt = %d -> mixed polling\n", getpid (),
+		 vepoll->vcl_cnt, vepoll->libc_cnt);
       vec_validate (libc_ev, __maxevents);
       timeout = clib_time_now (&vsm->clib_time) + time_to_wait;
       do
@@ -2897,6 +2923,9 @@
 	  rv2 = libc_epoll_pwait (__epfd, libc_ev, __maxevents, 1, __ss);
 	  if ((rv > 0) || (rv2 > 0))
 	    {
+	      if (VCOM_DEBUG > 2)
+		fprintf (stderr, "[%d] vcom_socket_epoll_pwait: "
+			 "rv = %d, rv2 = %d\n", getpid (), rv, rv2);
 	      int n = __maxevents - rv;
 	      n = rv2 <= n ? rv2 : n;
 	      rv = (rv > 0) ? rv : 0;
@@ -2910,12 +2939,12 @@
 	      if (rv < 0)
 		fprintf (stderr,
 			 "[%d] ERROR: vppcom_epoll_wait() returned %d\n",
-			 vsm->my_pid, rv);
+			 getpid (), rv);
 	      if (rv2 < 0)
 		{
 		  fprintf (stderr,
 			   "[%d] ERROR: libc_epoll_wait() failed, errno %d\n",
-			   vsm->my_pid, errno);
+			   getpid (), errno);
 		  rv = (rv < 0) ? rv : -errno;
 		}
 	      goto out;
@@ -3116,7 +3145,6 @@
 			      int __timeout)
 {
   int rv;
-  vcom_socket_main_t *vsm = &vcom_socket_main;
 
   nfds_t fds_idx = 0;
   int nfd = 0;
@@ -3205,7 +3233,7 @@
   if (VCOM_DEBUG > 2)
     fprintf (stderr,
 	     "[%d] vcom_socket_select: "
-	     "'%04d'='%04d'\n", vsm->my_pid, vcom_nfd, vcom_nfds);
+	     "'%04d'='%04d'\n", getpid (), vcom_nfd, vcom_nfds);
 
   if (vcom_nfd < 0)
     {
@@ -3339,7 +3367,6 @@
       vsm->epitemidxs_by_fd = hash_create (0, sizeof (i32 *));
 
       clib_time_init (&vsm->clib_time);
-      vsm->my_pid = getpid ();
 
       vsm->init = 1;
     }
diff --git a/src/vcl/vcom_socket.h b/src/vcl/vcom_socket.h
index e86bab3..0a5335f 100644
--- a/src/vcl/vcom_socket.h
+++ b/src/vcl/vcom_socket.h
@@ -432,11 +432,9 @@
 vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
 		    socklen_t * __restrict __addr_len);
 
-#ifdef __USE_GNU
 int
 vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
 		     socklen_t * __restrict __addr_len, int __flags);
-#endif
 
 int vcom_socket_shutdown (int __fd, int __how);
 
diff --git a/src/vcl/vcom_socket_wrapper.c b/src/vcl/vcom_socket_wrapper.c
index 8d54692..d73f4b2 100644
--- a/src/vcl/vcom_socket_wrapper.c
+++ b/src/vcl/vcom_socket_wrapper.c
@@ -154,14 +154,11 @@
  * SWRAP LOADING LIBC FUNCTIONS
  *********************************************************/
 
-#ifdef HAVE_ACCEPT4
 typedef int (*__libc_accept4) (int sockfd,
 			       struct sockaddr * addr,
 			       socklen_t * addrlen, int flags);
-#else
 typedef int (*__libc_accept) (int sockfd,
 			      struct sockaddr * addr, socklen_t * addrlen);
-#endif
 typedef int (*__libc_bind) (int sockfd,
 			    const struct sockaddr * addr, socklen_t addrlen);
 typedef int (*__libc_close) (int fd);
@@ -282,11 +279,8 @@
 
 struct swrap_libc_symbols
 {
-#ifdef HAVE_ACCEPT4
   SWRAP_SYMBOL_ENTRY (accept4);
-#else
   SWRAP_SYMBOL_ENTRY (accept);
-#endif
   SWRAP_SYMBOL_ENTRY (bind);
   SWRAP_SYMBOL_ENTRY (close);
   SWRAP_SYMBOL_ENTRY (connect);
@@ -474,7 +468,6 @@
  * has probably something todo with with the linker.
  * So we need load each function at the point it is called the first time.
  */
-#ifdef HAVE_ACCEPT4
 int
 libc_accept4 (int sockfd,
 	      struct sockaddr *addr, socklen_t * addrlen, int flags)
@@ -484,8 +477,6 @@
   return swrap.libc.symbols._libc_accept4.f (sockfd, addr, addrlen, flags);
 }
 
-#else /* HAVE_ACCEPT4 */
-
 int
 libc_accept (int sockfd, struct sockaddr *addr, socklen_t * addrlen)
 {
@@ -493,7 +484,6 @@
 
   return swrap.libc.symbols._libc_accept.f (sockfd, addr, addrlen);
 }
-#endif /* HAVE_ACCEPT4 */
 
 int
 libc_bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
diff --git a/src/vcl/vcom_socket_wrapper.h b/src/vcl/vcom_socket_wrapper.h
index 9e85ecf..f44b5a8 100644
--- a/src/vcl/vcom_socket_wrapper.h
+++ b/src/vcl/vcom_socket_wrapper.h
@@ -66,6 +66,7 @@
 #include <poll.h>
 #include <sys/uio.h>
 #include <stdlib.h>
+#include <vcl/vcom.h>
 
 
 /* GCC have printf type attribute check. */
@@ -104,13 +105,10 @@
  * has probably something todo with with the linker.
  * So we need load each function at the point it is called the first time.
  */
-#ifdef HAVE_ACCEPT4
-int
-libc_accept4 (int sockfd,
-	      struct sockaddr *addr, socklen_t * addrlen, int flags);
-#else /* HAVE_ACCEPT4 */
+int libc_accept4 (int sockfd, struct sockaddr *addr, socklen_t * addrlen,
+		  int flags);
+
 int libc_accept (int sockfd, struct sockaddr *addr, socklen_t * addrlen);
-#endif /* HAVE_ACCEPT4 */
 
 int libc_bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen);