Flush (work in progress)
[mono.git] / mono / io-layer / daemon-messages.c
index 391932e949af0821e316e8dc8bf1c7457dc5c5a7..812d435de5b7735dfc799e8b5d42c1017ef9a2d1 100644 (file)
@@ -15,7 +15,9 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 /* Freebsd needs this included explicitly, but it doesn't hurt on Linux */
-#include <sys/uio.h>
+#ifdef HAVE_SYS_UIO_H
+#    include <sys/uio.h>
+#endif
 
 #ifndef HAVE_MSG_NOSIGNAL
 #include <signal.h>
@@ -32,6 +34,9 @@
 #define CMSG_SPACE(size)  (sizeof (struct cmsghdr) + (size))
 #endif
 
+#define LOGDEBUG(...)
+// #define LOGDEBUG(...) g_message(__VA_ARGS__)
+
 static mono_mutex_t req_mutex;
 static mono_once_t attr_key_once = MONO_ONCE_INIT;
 static mono_mutexattr_t attr;
@@ -69,46 +74,59 @@ static void _wapi_daemon_request_response_internal (int fd,
         * rely on request turnaround time being minimal anyway, so
         * performance shouldnt suffer from the mutex.
         */
-       pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
+       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
                              (void *)&req_mutex);
-       ret = pthread_mutex_lock (&req_mutex);
+       ret = mono_mutex_lock (&req_mutex);
        g_assert (ret == 0);
        
 #ifdef HAVE_MSG_NOSIGNAL
-       ret=sendmsg (fd, msg, MSG_NOSIGNAL);
+       do {
+               ret=sendmsg (fd, msg, MSG_NOSIGNAL);
+       }
+       while (ret==-1 && errno==EINTR);
 #else
        old_sigpipe = signal (SIGPIPE, SIG_IGN);
-       ret=sendmsg (fd, msg, 0);
+       do {
+               ret=sendmsg (fd, msg, 0);
+       }
+       while (ret==-1 && errno==EINTR);
 #endif
+
        if(ret!=sizeof(WapiHandleRequest)) {
                if(errno==EPIPE) {
-                       g_critical (G_GNUC_PRETTY_FUNCTION ": The handle daemon vanished!");
+                       g_critical ("%s: The handle daemon vanished!", __func__);
                        exit (-1);
                } else {
-                       g_warning (G_GNUC_PRETTY_FUNCTION ": Send error: %s",
+                       g_warning ("%s: Send error: %s", __func__,
                                   strerror (errno));
                        g_assert_not_reached ();
                }
        }
 
 #ifdef HAVE_MSG_NOSIGNAL
-       ret=recv (fd, resp, sizeof(WapiHandleResponse), MSG_NOSIGNAL);
+       do {
+               ret=recv (fd, resp, sizeof(WapiHandleResponse), MSG_NOSIGNAL);
+       }
+       while (ret==-1 && errno==EINTR);
 #else
-       ret=recv (fd, resp, sizeof(WapiHandleResponse), 0);
+       do {
+               ret=recv (fd, resp, sizeof(WapiHandleResponse), 0);
+       }
+       while (ret==-1 && errno==EINTR);
        signal (SIGPIPE, old_sigpipe);
 #endif
+
        if(ret==-1) {
                if(errno==EPIPE) {
-                       g_critical (G_GNUC_PRETTY_FUNCTION ": The handle daemon vanished!");
+                       g_critical ("%s: The handle daemon vanished!", __func__);
                        exit (-1);
                } else {
-                       g_warning (G_GNUC_PRETTY_FUNCTION ": Send error: %s",
-                                  strerror (errno));
+                       g_warning ("%s: Send error: %s", __func__, strerror (errno));
                        g_assert_not_reached ();
                }
        }
-
-       ret = pthread_mutex_unlock (&req_mutex);
+               
+       ret = mono_mutex_unlock (&req_mutex);
        g_assert (ret == 0);
        
        pthread_cleanup_pop (0);
@@ -195,56 +213,45 @@ int _wapi_daemon_request (int fd, WapiHandleRequest *req, int *fds,
        iov.iov_base=req;
        iov.iov_len=sizeof(WapiHandleRequest);
        
+       do {
 #ifdef HAVE_MSG_NOSIGNAL
-       ret=recvmsg (fd, &msg, MSG_NOSIGNAL);
+               ret=recvmsg (fd, &msg, MSG_NOSIGNAL);
 #else
-       ret=recvmsg (fd, &msg, 0);
+               ret=recvmsg (fd, &msg, 0);
 #endif
+       }
+       while (ret==-1 && errno==EINTR);
+
        if(ret==-1 || ret!= sizeof(WapiHandleRequest)) {
                /* Make sure we dont do anything with this response */
                req->type=WapiHandleRequestType_Error;
                
-#ifdef DEBUG
-               g_warning (G_GNUC_PRETTY_FUNCTION ": Recv error: %s",
-                          strerror (errno));
-#endif
+               g_warning ("%s: Recv error: %s", __func__, strerror (errno));
                /* The next loop around poll() should tidy up */
        }
 
 #ifdef DEBUG
        if(msg.msg_flags & MSG_OOB) {
-               g_message (G_GNUC_PRETTY_FUNCTION ": OOB data received");
+               g_message ("%s: OOB data received", __func__);
        }
        if(msg.msg_flags & MSG_CTRUNC) {
-               g_message (G_GNUC_PRETTY_FUNCTION ": ancillary data was truncated");
+               g_message ("%s: ancillary data was truncated", __func__);
        }
-       g_message (G_GNUC_PRETTY_FUNCTION ": msg.msg_controllen=%d",
-                  msg.msg_controllen);
+       g_message ("%s: msg.msg_controllen=%d", __func__, msg.msg_controllen);
 #endif
 
        cmsg=CMSG_FIRSTHDR (&msg);
        if(cmsg!=NULL && cmsg->cmsg_level==SOL_SOCKET &&
           cmsg->cmsg_type==SCM_RIGHTS) {
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": cmsg->cmsg_len=%d",
-                          cmsg->cmsg_len);
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": cmsg->level=%d cmsg->type=%d", cmsg->cmsg_level,
-                          cmsg->cmsg_type);
-#endif
+               LOGDEBUG ("%s: cmsg->cmsg_len=%d", __func__, cmsg->cmsg_len);
+               LOGDEBUG ("%s: cmsg->level=%d cmsg->type=%d", __func__, cmsg->cmsg_level, cmsg->cmsg_type);
 
                memcpy (fds, (int *)CMSG_DATA (cmsg), sizeof(int)*3);
                *has_fds=TRUE;
 
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": fd[0]=%d, fd[1]=%d, fd[2]=%d", fds[0], fds[1],
-                          fds[2]);
-#endif
+               LOGDEBUG ("%s: fd[0]=%d, fd[1]=%d, fd[2]=%d", __func__, fds[0], fds[1], fds[2]);
        } else {
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": no ancillary data");
-#endif
+               LOGDEBUG ("%s: no ancillary data", __func__);
                *has_fds=FALSE;
        }
 
@@ -255,16 +262,19 @@ int _wapi_daemon_request (int fd, WapiHandleRequest *req, int *fds,
 int _wapi_daemon_response (int fd, WapiHandleResponse *resp)
 {
        int ret;
-       
+
+       do {
 #ifdef HAVE_MSG_NOSIGNAL
-       ret=send (fd, resp, sizeof(WapiHandleResponse), MSG_NOSIGNAL);
+               ret=send (fd, resp, sizeof(WapiHandleResponse), MSG_NOSIGNAL);
 #else
-       ret=send (fd, resp, sizeof(WapiHandleResponse), 0);
+               ret=send (fd, resp, sizeof(WapiHandleResponse), 0);
 #endif
+       }
+       while (ret==-1 && errno==EINTR);
+
 #ifdef DEBUG
        if(ret==-1 || ret != sizeof(WapiHandleResponse)) {
-               g_warning (G_GNUC_PRETTY_FUNCTION ": Send error: %s",
-                          strerror (errno));
+               g_warning ("%s: Send error: %s", __func__, strerror (errno));
                /* The next loop around poll() should tidy up */
        }
 #endif