s3: Eliminate sys_select from g_lock_lock
authorVolker Lendecke <vl@samba.org>
Tue, 8 Feb 2011 10:59:04 +0000 (11:59 +0100)
committerVolker Lendecke <vlendec@samba.org>
Mon, 28 Feb 2011 15:40:19 +0000 (16:40 +0100)
source3/lib/g_lock.c

index 378e464b5ce3732143def20524c4170d9e6ff793..dfbcf8445c0d21b1395d99a0b7fc0c5371afbd96 100644 (file)
@@ -22,6 +22,7 @@
 #include "librpc/gen_ndr/messaging.h"
 #include "ctdbd_conn.h"
 #include "../lib/util/select.h"
+#include "system/select.h"
 
 static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
                                    struct server_id pid);
@@ -335,11 +336,9 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
        timeout_end = timeval_sum(&time_now, &timeout);
 
        while (true) {
-#ifdef CLUSTER_SUPPORT
-               fd_set _r_fds;
-#endif
-               fd_set *r_fds = NULL;
-               int max_fd = 0;
+               struct pollfd *pollfds;
+               int num_pollfds;
+               int saved_errno;
                int ret;
                struct timeval timeout_remaining, select_timeout;
 
@@ -387,15 +386,27 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
                 * events here but have to handcode a timeout.
                 */
 
+               /*
+                * We allocate 2 entries here. One is needed anyway for
+                * sys_poll and in the clustering case we might have to add
+                * the ctdb fd. This avoids the realloc then.
+                */
+               pollfds = TALLOC_ARRAY(talloc_tos(), struct pollfd, 2);
+               if (pollfds == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       break;
+               }
+               num_pollfds = 1;
+
 #ifdef CLUSTER_SUPPORT
                if (lp_clustering()) {
                        struct ctdbd_connection *conn;
                        conn = messaging_ctdbd_connection();
 
-                       r_fds = &_r_fds;
-                       FD_ZERO(r_fds);
-                       max_fd = ctdbd_conn_get_fd(conn);
-                       FD_SET(max_fd, r_fds);
+                       pollfds[0].fd = ctdbd_conn_get_fd(conn);
+                       pollfds[0].events = POLLIN|POLLHUP;
+
+                       num_pollfds += 1;
                }
 #endif
 
@@ -406,8 +417,17 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
                select_timeout = timeval_min(&select_timeout,
                                             &timeout_remaining);
 
-               ret = sys_select(max_fd + 1, r_fds, NULL, NULL,
-                                &select_timeout);
+               ret = sys_poll(pollfds, num_pollfds,
+                              timeval_to_msec(select_timeout));
+
+               /*
+                * We're not *really interested in the actual flags. We just
+                * need to retry this whole thing.
+                */
+               saved_errno = errno;
+               TALLOC_FREE(pollfds);
+               errno = saved_errno;
+
                if (ret == -1) {
                        if (errno != EINTR) {
                                DEBUG(1, ("error calling select: %s\n",