Merge pull request #2433 from alexrp/thread-volatile-semantics
[mono.git] / mono / io-layer / sockets.c
index 9121f86731c3923586f7824b4f1b8c18bad09217..aaa53ffe16d157b16554960ba51f2ab35c430ab8 100644 (file)
 #include <mono/io-layer/socket-private.h>
 #include <mono/io-layer/handles-private.h>
 #include <mono/io-layer/socket-wrappers.h>
+#include <mono/io-layer/io-trace.h>
 #include <mono/utils/mono-poll.h>
+#include <mono/utils/mono-once.h>
+#include <mono/utils/mono-logger-internals.h>
 
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <netdb.h>
 #endif
 
-#if 0
-#define DEBUG(...) g_message(__VA_ARGS__)
-#else
-#define DEBUG(...)
-#endif
-
 static guint32 in_cleanup = 0;
 
 static void socket_close (gpointer handle, gpointer data);
@@ -84,7 +81,7 @@ static void socket_close (gpointer handle, gpointer data)
        int ret;
        struct _WapiHandle_socket *socket_handle = (struct _WapiHandle_socket *)data;
 
-       DEBUG ("%s: closing socket handle %p", __func__, handle);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing socket handle %p", __func__, handle);
 
        /* Shutdown the socket for reading, to interrupt any potential
         * receives that may be blocking for data.  See bug 75705.
@@ -98,7 +95,7 @@ static void socket_close (gpointer handle, gpointer data)
        
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: close error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: close error: %s", __func__, strerror (errno));
                errnum = errno_to_WSA (errnum, __func__);
                if (!in_cleanup)
                        WSASetLastError (errnum);
@@ -117,7 +114,7 @@ cleanup_close (gpointer handle, gpointer data)
 
 void _wapi_cleanup_networking(void)
 {
-       DEBUG ("%s: cleaning up", __func__);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: cleaning up", __func__);
 
        in_cleanup = 1;
        _wapi_handle_foreach (WAPI_HANDLE_SOCKET, cleanup_close, NULL);
@@ -182,7 +179,7 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
 
        if (new_fd == -1) {
                gint errnum = errno;
-               DEBUG ("%s: accept error: %s", __func__, strerror(errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: accept error: %s", __func__, strerror(errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -191,7 +188,7 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
        }
 
        if (new_fd >= _wapi_fd_reserve) {
-               DEBUG ("%s: File descriptor is too big", __func__);
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
 
                WSASetLastError (WSASYSCALLFAILURE);
                
@@ -213,7 +210,7 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
                return(INVALID_SOCKET);
        }
 
-       DEBUG ("%s: returning newly accepted socket handle %p with",
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning newly accepted socket handle %p with",
                   __func__, new_handle);
        
        return(new_fd);
@@ -232,7 +229,7 @@ int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
        ret = bind (fd, my_addr, addrlen);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: bind error: %s", __func__, strerror(errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: bind error: %s", __func__, strerror(errno));
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
                
@@ -262,7 +259,7 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                errnum = errno;
                
                if (errno != EINTR) {
-                       DEBUG ("%s: connect error: %s", __func__,
+                       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect error: %s", __func__,
                                   strerror (errnum));
 
                        errnum = errno_to_WSA (errnum, __func__);
@@ -300,7 +297,7 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                        if (errno != EINTR) {
                                errnum = errno_to_WSA (errno, __func__);
 
-                               DEBUG ("%s: connect poll error: %s",
+                               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect poll error: %s",
                                           __func__, strerror (errno));
 
                                WSASetLastError (errnum);
@@ -313,7 +310,7 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                                &len) == -1) {
                        errnum = errno_to_WSA (errno, __func__);
 
-                       DEBUG ("%s: connect getsockopt error: %s",
+                       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect getsockopt error: %s",
                                   __func__, strerror (errno));
 
                        WSASetLastError (errnum);
@@ -332,7 +329,7 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                                socket_handle->saved_error = errnum;
                        }
                        
-                       DEBUG ("%s: connect getsockopt returned error: %s",
+                       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect getsockopt returned error: %s",
                                   __func__, strerror (so_error));
 
                        WSASetLastError (errnum);
@@ -356,7 +353,7 @@ int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
        ret = getpeername (fd, name, namelen);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: getpeername error: %s", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: getpeername error: %s", __func__,
                           strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
@@ -381,7 +378,7 @@ int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
        ret = getsockname (fd, name, namelen);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: getsockname error: %s", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: getsockname error: %s", __func__,
                           strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
@@ -418,7 +415,7 @@ int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
        ret = getsockopt (fd, level, optname, tmp_val, optlen);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: getsockopt error: %s", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: getsockopt error: %s", __func__,
                           strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
@@ -470,7 +467,7 @@ int _wapi_listen(guint32 fd, int backlog)
        ret = listen (fd, backlog);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: listen error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: listen error: %s", __func__, strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -533,7 +530,7 @@ int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
        
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: recv error: %s", __func__, strerror(errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: recv error: %s", __func__, strerror(errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -573,7 +570,7 @@ _wapi_recvmsg(guint32 fd, struct msghdr *msg, int recv_flags)
        
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: recvmsg error: %s", __func__, strerror(errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: recvmsg error: %s", __func__, strerror(errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -600,7 +597,7 @@ int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
 
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: send error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: send error: %s", __func__, strerror (errno));
 
 #ifdef O_NONBLOCK
                /* At least linux returns EAGAIN/EWOULDBLOCK when the timeout has been set on
@@ -637,7 +634,7 @@ int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
 
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: send error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: send error: %s", __func__, strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -665,7 +662,7 @@ _wapi_sendmsg(guint32 fd,  const struct msghdr *msg, int send_flags)
 
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: sendmsg error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: sendmsg error: %s", __func__, strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -700,8 +697,9 @@ int _wapi_setsockopt(guint32 fd, int level, int optname,
                tv.tv_usec = (ms % 1000) * 1000;        // micro from milli
                tmp_val = &tv;
                optlen = sizeof (tv);
+       }
 #if defined (__linux__)
-       else if (level == SOL_SOCKET &&
+       else if (level == SOL_SOCKET &&
                   (optname == SO_SNDBUF || optname == SO_RCVBUF)) {
                /* According to socket(7) the Linux kernel doubles the
                 * buffer sizes "to allow space for bookkeeping
@@ -711,13 +709,13 @@ int _wapi_setsockopt(guint32 fd, int level, int optname,
 
                bufsize /= 2;
                tmp_val = &bufsize;
-#endif
        }
+#endif
                
        ret = setsockopt (fd, level, optname, tmp_val, optlen);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: setsockopt error: %s", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setsockopt error: %s", __func__,
                           strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
@@ -733,7 +731,7 @@ int _wapi_setsockopt(guint32 fd, int level, int optname,
                socklen_t type_len = sizeof (type);
 
                if (!getsockopt (fd, level, SO_TYPE, &type, &type_len)) {
-                       if (type == SOCK_DGRAM)
+                       if (type == SOCK_DGRAM || type == SOCK_STREAM)
                                setsockopt (fd, level, SO_REUSEPORT, tmp_val, optlen);
                }
        }
@@ -771,7 +769,7 @@ int _wapi_shutdown(guint32 fd, int how)
        ret = shutdown (fd, how);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: shutdown error: %s", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: shutdown error: %s", __func__,
                           strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
@@ -806,7 +804,7 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
        
        if (fd == -1) {
                gint errnum = errno;
-               DEBUG ("%s: socket error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: socket error: %s", __func__, strerror (errno));
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
 
@@ -814,7 +812,7 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
        }
 
        if (fd >= _wapi_fd_reserve) {
-               DEBUG ("%s: File descriptor is too big (%d >= %d)",
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big (%d >= %d)",
                           __func__, fd, _wapi_fd_reserve);
 
                WSASetLastError (WSASYSCALLFAILURE);
@@ -839,14 +837,14 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
         * https://bugzilla.novell.com/show_bug.cgi?id=MONO53992
         */
        {
-               int ret, true = 1;
+               int ret, true_ = 1;
        
-               ret = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true,
-                                 sizeof (true));
+               ret = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true_,
+                                 sizeof (true_));
                if (ret == -1) {
                        int errnum = errno;
 
-                       DEBUG ("%s: Error setting SO_REUSEADDR", __func__);
+                       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error setting SO_REUSEADDR", __func__);
                        
                        errnum = errno_to_WSA (errnum, __func__);
                        WSASetLastError (errnum);
@@ -868,7 +866,7 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
                return(INVALID_SOCKET);
        }
 
-       DEBUG ("%s: returning socket handle %p", __func__, handle);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning socket handle %p", __func__, handle);
 
        return(fd);
 }
@@ -894,7 +892,7 @@ static gboolean socket_disconnect (guint32 fd)
        if (newsock == -1) {
                gint errnum = errno;
 
-               DEBUG ("%s: socket error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: socket error: %s", __func__, strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -914,7 +912,7 @@ static gboolean socket_disconnect (guint32 fd)
        if (ret == -1) {
                gint errnum = errno;
                
-               DEBUG ("%s: dup2 error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: dup2 error: %s", __func__, strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -930,7 +928,7 @@ static gboolean socket_disconnect (guint32 fd)
 static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
                                   guint32 flags, guint32 reserved)
 {
-       DEBUG ("%s: called on socket %d!", __func__, fd);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: called on socket %d!", __func__, fd);
        
        if (reserved != 0) {
                WSASetLastError (WSAEINVAL);
@@ -1163,13 +1161,13 @@ WSAIoctl (guint32 fd, gint32 command,
        }
 
        if (i_len > 0) {
-               buffer = g_memdup (input, i_len);
+               buffer = (char *)g_memdup (input, i_len);
        }
 
        ret = ioctl (fd, command, buffer);
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG("%s: WSAIoctl error: %s", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: WSAIoctl error: %s", __func__,
                          strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
@@ -1262,7 +1260,7 @@ int ioctlsocket(guint32 fd, unsigned long command, gpointer arg)
 
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: ioctl error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: ioctl error: %s", __func__, strerror (errno));
 
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);
@@ -1299,7 +1297,7 @@ int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
 
        if (ret == -1) {
                gint errnum = errno;
-               DEBUG ("%s: select error: %s", __func__, strerror (errno));
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: select error: %s", __func__, strerror (errno));
                errnum = errno_to_WSA (errnum, __func__);
                WSASetLastError (errnum);