[SCM] Samba Shared Repository - branch master updated

Volker Lendecke vlendec at samba.org
Mon Feb 28 09:27:02 MST 2011


The branch, master has been updated
       via  c1bc916 nsswitch: Eliminate select from wb_common
       via  d163f5d Remove sys_select[_intr]
       via  3254e39 tevent: Add a poll backend
       via  f7bd096 libcli: Eliminate select from smb_readline_replacement
       via  d825b5e s3: Eliminate select from libaddns
       via  5b26cfe s3: Eliminate sys_select from do_smb_resolve() -- untested
       via  b28a2e5 s3: Eliminate sys_select from do_smb_browse() -- untested
       via  89d81b3 s3: Eliminate sys_select_intr from smbfilter
       via  7eedf69 s3: Remove unused receive_packet
       via  a2970df s3: Eliminate sys_select_intr from read_fd_with_timeout
       via  e6f8208 s3: Eliminiate sys_select_intr from client.c
       via  884984a s3: Add poll_intr_one_fd
       via  83becbe s3: Add sys_poll_intr
       via  81bdaaf s3: Eliminate sys_select from g_lock_lock
       via  0f082de s3: Eliminate sys_select from fd_is_readable
       via  2b0057c s3: Eliminate select from packet_fd_read_sync
       via  deb58b2 s3: Add poll_one_fd()
       via  cf7d331 s3: Remove select-based s3 event backend
       via  ada2a5a s3: Use poll in nmbd
       via  c6c666a s3: Use poll in winbind
       via  9758afd s3: Use poll in smbd
       via  ea5e1c5 s3: Use poll for the event loop
       via  be33e2e s3: Add sys_poll
       via  0fc1650 libreplace: poll based on select
       via  5e93e3e s3: Add timeval_to_msec
      from  9bfbf65 s3:msg_idmap: The solaris cc compiler does not like unnamed unions as struct members - fix the build

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit c1bc916507d4afcf6763fc3ffa651e1ad3a8e931
Author: Volker Lendecke <vl at samba.org>
Date:   Fri Feb 18 13:57:35 2011 +0100

    nsswitch: Eliminate select from wb_common
    
    Autobuild-User: Volker Lendecke <vlendec at samba.org>
    Autobuild-Date: Mon Feb 28 17:26:18 CET 2011 on sn-devel-104

commit d163f5d67cf461c77749a82b0f83138271ffe333
Author: Volker Lendecke <vl at samba.org>
Date:   Fri Feb 18 12:34:08 2011 +0100

    Remove sys_select[_intr]

commit 3254e392208ac06c7cce3d89ca6369a224f7f481
Author: Volker Lendecke <vl at samba.org>
Date:   Wed Feb 9 15:28:10 2011 +0100

    tevent: Add a poll backend

commit f7bd09665ae1dfed2a503314f4b4946fe0ee1bf5
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 14 12:14:12 2011 +0100

    libcli: Eliminate select from smb_readline_replacement

commit d825b5e2cb960fdc348d068a7af3ffbd54cc1c3f
Author: Volker Lendecke <vl at samba.org>
Date:   Thu Feb 10 11:01:54 2011 +0100

    s3: Eliminate select from libaddns

commit 5b26cfe14395ce09461de9423bd745570702fd9e
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 17:19:03 2011 +0100

    s3: Eliminate sys_select from do_smb_resolve() -- untested

commit b28a2e519a0f36da800b0fdab295c1875401ac84
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 17:17:26 2011 +0100

    s3: Eliminate sys_select from do_smb_browse() -- untested

commit 89d81b310ec0f1bc7987f452eadc544403ee77ec
Author: Volker Lendecke <vl at samba.org>
Date:   Wed Feb 9 13:38:06 2011 +0100

    s3: Eliminate sys_select_intr from smbfilter

commit 7eedf69bc9c9d2a5305f0c889e5f81139a537a29
Author: Stefan Metzmacher <metze at samba.org>
Date:   Fri Feb 18 12:32:12 2011 +0100

    s3: Remove unused receive_packet

commit a2970dfaef9ceac6af2784cbffe9e2d8ac14b188
Author: Volker Lendecke <vl at samba.org>
Date:   Tue Feb 8 17:57:12 2011 +0100

    s3: Eliminate sys_select_intr from read_fd_with_timeout

commit e6f820835874e6bbf103768665952b7884cd8452
Author: Volker Lendecke <vl at samba.org>
Date:   Tue Feb 8 17:56:10 2011 +0100

    s3: Eliminiate sys_select_intr from client.c

commit 884984ae0a9b551dcd92d6d0486b2d741eabd20f
Author: Volker Lendecke <vl at samba.org>
Date:   Tue Feb 8 17:33:59 2011 +0100

    s3: Add poll_intr_one_fd

commit 83becbe369bcf3478063662fc2626ccbf4530b20
Author: Volker Lendecke <vl at samba.org>
Date:   Tue Feb 8 17:28:27 2011 +0100

    s3: Add sys_poll_intr

commit 81bdaafab34995522fc9a7b16fcabf9cd09cb528
Author: Volker Lendecke <vl at samba.org>
Date:   Tue Feb 8 11:59:04 2011 +0100

    s3: Eliminate sys_select from g_lock_lock

commit 0f082de561342b88ffd239b6cc3e11e059225380
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 17:02:44 2011 +0100

    s3: Eliminate sys_select from fd_is_readable

commit 2b0057c2cb4f05005354008044049fc3dc2bd1fd
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 16:59:38 2011 +0100

    s3: Eliminate select from packet_fd_read_sync

commit deb58b2e941f6d307f28f7b909f388c39fe915e8
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 16:55:16 2011 +0100

    s3: Add poll_one_fd()

commit cf7d331511437cc0be7d2dfa9263da5658d96740
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 15:24:07 2011 +0100

    s3: Remove select-based s3 event backend

commit ada2a5a2457ad0f91fc19e5de9391abbf059a93b
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 15:20:39 2011 +0100

    s3: Use poll in nmbd

commit c6c666aa07760757d0491da6acf96c9b7794ec25
Author: Volker Lendecke <vl at samba.org>
Date:   Thu Feb 3 22:05:30 2011 +0100

    s3: Use poll in winbind

commit 9758afd47e12513c966aab7431dd674ce954cc59
Author: Volker Lendecke <vl at samba.org>
Date:   Thu Feb 3 21:55:02 2011 +0100

    s3: Use poll in smbd

commit ea5e1c5ecb7ab897a684bda88a49bb26fb557eb0
Author: Volker Lendecke <vl at samba.org>
Date:   Thu Feb 3 16:34:07 2011 +0100

    s3: Use poll for the event loop

commit be33e2e0a7b18c6d659d976afc507407904a69ed
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Feb 7 16:29:13 2011 +0100

    s3: Add sys_poll

commit 0fc1650e091727971b3e3017f9f06cae6cdf71bb
Author: Volker Lendecke <vl at samba.org>
Date:   Wed Feb 9 19:22:25 2011 +0100

    libreplace: poll based on select

commit 5e93e3e88fb4a219492c23c18067f358b54311d0
Author: Volker Lendecke <vl at samba.org>
Date:   Tue Feb 8 12:55:02 2011 +0100

    s3: Add timeval_to_msec

-----------------------------------------------------------------------

Summary of changes:
 lib/addns/dnssock.c              |   24 ++--
 lib/replace/poll.c               |  133 +++++++++++++++++
 lib/replace/replace.h            |    5 +
 lib/replace/system/config.m4     |    4 +
 lib/replace/system/select.h      |   36 +++++
 lib/replace/wscript              |    4 +
 lib/tevent/libtevent.m4          |    1 +
 lib/tevent/tevent.c              |    1 +
 lib/tevent/tevent_internal.h     |    1 +
 lib/tevent/tevent_poll.c         |  306 ++++++++++++++++++++++++++++++++++++++
 lib/tevent/wscript               |    1 +
 lib/util/select.c                |  142 ++++++------------
 lib/util/select.h                |    6 +-
 libcli/smbreadline/smbreadline.c |   12 +-
 nsswitch/wb_common.c             |   55 ++++----
 source3/client/client.c          |   16 +--
 source3/client/dnsbrowse.c       |   35 +----
 source3/configure.in             |    2 +-
 source3/include/event.h          |   10 +-
 source3/include/packet.h         |    3 +-
 source3/include/proto.h          |    4 +-
 source3/lib/ctdbd_conn.c         |   11 +-
 source3/lib/events.c             |  222 ++++++++++++++++++++++-----
 source3/lib/g_lock.c             |   42 ++++--
 source3/lib/packet.c             |   21 ++--
 source3/lib/util.c               |    5 +
 source3/lib/util_sock.c          |   63 +++++++--
 source3/libsmb/nmblib.c          |   33 ----
 source3/nmbd/nmbd_packets.c      |  215 ++++++++++++++-------------
 source3/smbd/globals.h           |    8 +
 source3/smbd/process.c           |   49 +++----
 source3/utils/smbfilter.c        |   60 ++++++--
 source3/winbindd/winbindd_dual.c |   51 ++++---
 33 files changed, 1102 insertions(+), 479 deletions(-)
 create mode 100644 lib/replace/poll.c
 create mode 100644 lib/tevent/tevent_poll.c


Changeset truncated at 500 lines:

diff --git a/lib/addns/dnssock.c b/lib/addns/dnssock.c
index 7c8bd41..42b4e2d 100644
--- a/lib/addns/dnssock.c
+++ b/lib/addns/dnssock.c
@@ -22,9 +22,11 @@
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "replace.h"
 #include "dns.h"
 #include <sys/time.h>
 #include <unistd.h>
+#include "system/select.h"
 
 static int destroy_dns_connection(struct dns_connection *conn)
 {
@@ -103,7 +105,7 @@ static DNS_ERROR dns_udp_open( const char *nameserver,
 		}
 		memcpy( &ulAddress, pHost->h_addr, pHost->h_length );
 	}
-	
+
 	/* Create a socket for sending data */
 
 	conn->s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
@@ -143,7 +145,7 @@ DNS_ERROR dns_open_connection( const char *nameserver, int32 dwType,
 	case DNS_UDP:
 		return dns_udp_open( nameserver, mem_ctx, conn );
 	}
-	
+
 	return ERROR_DNS_INVALID_PARAMETER;
 }
 
@@ -212,21 +214,17 @@ DNS_ERROR dns_send(struct dns_connection *conn, const struct dns_buffer *buf)
 static DNS_ERROR read_all(int fd, uint8 *data, size_t len)
 {
 	size_t total = 0;
-	fd_set rfds;
-	struct timeval tv;
 
 	while (total < len) {
+		struct pollfd pfd;
 		ssize_t ret;
 		int fd_ready;
-		
-		FD_ZERO( &rfds );
-		FD_SET( fd, &rfds );
-
-		/* 10 second timeout */
-		tv.tv_sec = 10;
-		tv.tv_usec = 0;
-		
-		fd_ready = select( fd+1, &rfds, NULL, NULL, &tv );
+
+		ZERO_STRUCT(pfd);
+		pfd.fd = fd;
+		pfd.events = POLLIN|POLLHUP;
+
+		fd_ready = poll(&pfd, 1, 10000);
 		if ( fd_ready == 0 ) {
 			/* read timeout */
 			return ERROR_DNS_SOCKET_ERROR;
diff --git a/lib/replace/poll.c b/lib/replace/poll.c
new file mode 100644
index 0000000..e41548d
--- /dev/null
+++ b/lib/replace/poll.c
@@ -0,0 +1,133 @@
+/*
+   Unix SMB/CIFS implementation.
+   poll.c - poll wrapper
+
+   This file is based on code from libssh (LGPLv2.1+ at the time it
+   was downloaded), thus the following copyrights:
+
+   Copyright (c) 2009-2010 by Andreas Schneider <mail at cynapses.org>
+   Copyright (c) 2003-2009 by Aris Adamantiadis
+   Copyright (c) 2009 Aleksandar Kanchev
+   Copyright (C) Volker Lendecke 2011
+
+     ** NOTE! The following LGPL license applies to the replace
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include "system/select.h"
+
+
+int rep_poll(struct pollfd *fds, nfds_t nfds, int timeout)
+{
+	fd_set rfds, wfds, efds;
+	struct timeval tv, *ptv;
+	int max_fd;
+	int rc;
+	nfds_t i;
+
+	if (fds == NULL) {
+		errno = EFAULT;
+		return -1;
+	}
+
+	FD_ZERO(&rfds);
+	FD_ZERO(&wfds);
+	FD_ZERO(&efds);
+
+	rc = 0;
+	max_fd = 0;
+
+	/* compute fd_sets and find largest descriptor */
+	for (i = 0; i < nfds; i++) {
+		if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
+			fds[i].revents = POLLNVAL;
+			continue;
+		}
+
+		if (fds[i].events & (POLLIN | POLLRDNORM)) {
+			FD_SET(fds[i].fd, &rfds);
+		}
+		if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
+			FD_SET(fds[i].fd, &wfds);
+		}
+		if (fds[i].events & (POLLPRI | POLLRDBAND)) {
+			FD_SET(fds[i].fd, &efds);
+		}
+		if (fds[i].fd > max_fd &&
+		    (fds[i].events & (POLLIN | POLLOUT | POLLPRI |
+				      POLLRDNORM | POLLRDBAND |
+				      POLLWRNORM | POLLWRBAND))) {
+			max_fd = fds[i].fd;
+		}
+	}
+
+	if (timeout < 0) {
+		ptv = NULL;
+	} else {
+		ptv = &tv;
+		if (timeout == 0) {
+			tv.tv_sec = 0;
+			tv.tv_usec = 0;
+		} else {
+			tv.tv_sec = timeout / 1000;
+			tv.tv_usec = (timeout % 1000) * 1000;
+		}
+	}
+
+	rc = select(max_fd + 1, &rfds, &wfds, &efds, ptv);
+	if (rc < 0) {
+		return -1;
+	}
+
+	for (rc = 0, i = 0; i < nfds; i++) {
+		if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
+			continue;
+		}
+
+		fds[i].revents = 0;
+
+		if (FD_ISSET(fds[i].fd, &rfds)) {
+			int err = errno;
+			int available = 0;
+			int ret;
+
+			/* support for POLLHUP */
+			ret = ioctl(fds[i].fd, FIONREAD, &available);
+			if ((ret == -1) || (available == 0)) {
+				fds[i].revents |= POLLHUP;
+			} else {
+				fds[i].revents |= fds[i].events
+					& (POLLIN | POLLRDNORM);
+			}
+
+			errno = err;
+		}
+		if (FD_ISSET(fds[i].fd, &wfds)) {
+			fds[i].revents |= fds[i].events
+				& (POLLOUT | POLLWRNORM | POLLWRBAND);
+		}
+		if (FD_ISSET(fds[i].fd, &efds)) {
+			fds[i].revents |= fds[i].events
+				& (POLLPRI | POLLRDBAND);
+		}
+		if (fds[i].revents & ~POLLHUP) {
+			rc++;
+		}
+	}
+	return rc;
+}
diff --git a/lib/replace/replace.h b/lib/replace/replace.h
index 60aa3d4..c081f23 100644
--- a/lib/replace/replace.h
+++ b/lib/replace/replace.h
@@ -797,4 +797,9 @@ int fdatasync(int );
 #endif
 #endif
 
+#ifndef HAVE_POLL
+#define poll rep_poll
+/* prototype is in "system/network.h" */
+#endif
+
 #endif /* _LIBREPLACE_REPLACE_H */
diff --git a/lib/replace/system/config.m4 b/lib/replace/system/config.m4
index 04364bc..b7cdf14 100644
--- a/lib/replace/system/config.m4
+++ b/lib/replace/system/config.m4
@@ -6,6 +6,10 @@ AC_CHECK_HEADERS(sys/acl.h acl/libacl.h sys/file.h)
 # select
 AC_CHECK_HEADERS(sys/select.h)
 
+# poll
+AC_CHECK_HEADERS(poll.h)
+AC_CHECK_FUNCS(poll,[],[LIBREPLACEOBJ="${LIBREPLACEOBJ} $libreplacedir/poll.o"])
+
 # time
 AC_CHECK_HEADERS(sys/time.h utime.h)
 AC_HEADER_TIME
diff --git a/lib/replace/system/select.h b/lib/replace/system/select.h
index da18de0..11c5390 100644
--- a/lib/replace/system/select.h
+++ b/lib/replace/system/select.h
@@ -38,4 +38,40 @@
 #define SELECT_CAST
 #endif
 
+#ifdef HAVE_POLL
+
+#include <poll.h>
+
+#else
+
+/* Type used for the number of file descriptors.  */
+typedef unsigned long int nfds_t;
+
+/* Data structure describing a polling request.  */
+struct pollfd
+{
+	int fd;		   /* File descriptor to poll.  */
+	short int events;  /* Types of events poller cares about.  */
+	short int revents; /* Types of events that actually occurred.  */
+};
+
+/* Event types that can be polled for.  These bits may be set in `events'
+   to indicate the interesting event types; they will appear in `revents'
+   to indicate the status of the file descriptor.  */
+#define POLLIN		0x001		/* There is data to read.  */
+#define POLLPRI		0x002		/* There is urgent data to read.  */
+#define POLLOUT		0x004		/* Writing now will not block.  */
+#define POLLRDNORM	0x040		/* Normal data may be read.  */
+#define POLLRDBAND	0x080		/* Priority data may be read.  */
+#define POLLWRNORM	0x100		/* Writing now will not block.  */
+#define POLLWRBAND	0x200		/* Priority data may be written.  */
+#define POLLERR		0x008		/* Error condition.  */
+#define POLLHUP		0x010		/* Hung up.  */
+#define POLLNVAL	0x020		/* Invalid polling request.  */
+
+/* define is in "replace.h" */
+int rep_poll(struct pollfd *fds, nfds_t nfds, int timeout);
+
+#endif
+
 #endif
diff --git a/lib/replace/wscript b/lib/replace/wscript
index b1aad69..d0bbe96 100644
--- a/lib/replace/wscript
+++ b/lib/replace/wscript
@@ -254,6 +254,9 @@ def configure(conf):
     if conf.CONFIG_SET('HAVE_EPOLL_CREATE') and conf.CONFIG_SET('HAVE_SYS_EPOLL_H'):
         conf.DEFINE('HAVE_EPOLL', 1)
 
+    conf.CHECK_HEADERS('poll.h')
+    conf.CHECK_FUNCS('poll')
+
     if not conf.CHECK_CODE('''#define LIBREPLACE_CONFIGURE_TEST_STRPTIME
                            #include "test/strptime.c"''',
                            define='HAVE_STRPTIME',
@@ -411,6 +414,7 @@ def build(bld):
     if bld.CONFIG_SET('REPLACE_GETPASS'):        REPLACE_SOURCE += ' getpass.c'
     if not bld.CONFIG_SET('HAVE_CRYPT'):         REPLACE_SOURCE += ' crypt.c'
     if not bld.CONFIG_SET('HAVE_DLOPEN'):        REPLACE_SOURCE += ' dlfcn.c'
+    if not bld.CONFIG_SET('HAVE_POLL'):          REPLACE_SOURCE += ' poll.c'
 
     if not bld.CONFIG_SET('HAVE_SOCKETPAIR'):    REPLACE_SOURCE += ' socketpair.c'
     if not bld.CONFIG_SET('HAVE_CONNECT'):       REPLACE_SOURCE += ' socket.c'
diff --git a/lib/tevent/libtevent.m4 b/lib/tevent/libtevent.m4
index 4162ba3..5c5969b 100644
--- a/lib/tevent/libtevent.m4
+++ b/lib/tevent/libtevent.m4
@@ -30,6 +30,7 @@ TEVENT_OBJ="tevent.o tevent_debug.o tevent_util.o"
 TEVENT_OBJ="$TEVENT_OBJ tevent_fd.o tevent_timed.o tevent_immediate.o tevent_signal.o"
 TEVENT_OBJ="$TEVENT_OBJ tevent_req.o tevent_wakeup.o tevent_queue.o"
 TEVENT_OBJ="$TEVENT_OBJ tevent_standard.o tevent_select.o"
+TEVENT_OBJ="$TEVENT_OBJ tevent_poll.o"
 
 AC_CHECK_HEADERS(sys/epoll.h)
 AC_CHECK_FUNCS(epoll_create)
diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c
index 4849bc1..87e5aff 100644
--- a/lib/tevent/tevent.c
+++ b/lib/tevent/tevent.c
@@ -113,6 +113,7 @@ void tevent_set_default_backend(const char *backend)
 static void tevent_backend_init(void)
 {
 	tevent_select_init();
+	tevent_poll_init();
 	tevent_standard_init();
 #ifdef HAVE_EPOLL
 	tevent_epoll_init();
diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h
index 7f5fd64..3d71bfd 100644
--- a/lib/tevent/tevent_internal.h
+++ b/lib/tevent/tevent_internal.h
@@ -303,6 +303,7 @@ void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se);
 
 bool tevent_standard_init(void);
 bool tevent_select_init(void);
+bool tevent_poll_init(void);
 #ifdef HAVE_EPOLL
 bool tevent_epoll_init(void);
 #endif
diff --git a/lib/tevent/tevent_poll.c b/lib/tevent/tevent_poll.c
new file mode 100644
index 0000000..c133194
--- /dev/null
+++ b/lib/tevent/tevent_poll.c
@@ -0,0 +1,306 @@
+/*
+   Unix SMB/CIFS implementation.
+   main select loop and event handling
+   Copyright (C) Andrew Tridgell	2003-2005
+   Copyright (C) Stefan Metzmacher	2005-2009
+
+     ** NOTE! The following LGPL license applies to the tevent
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/filesys.h"
+#include "system/select.h"
+#include "tevent.h"
+#include "tevent_util.h"
+#include "tevent_internal.h"
+
+struct poll_event_context {
+	/*
+	 * These two arrays are maintained together.
+	 */
+	struct pollfd *fds;
+	struct tevent_fd **fd_events;
+	int num_fds;
+
+	/* information for exiting from the event loop */
+	int exit_code;
+};
+
+/*
+  create a select_event_context structure.
+*/
+static int poll_event_context_init(struct tevent_context *ev)
+{
+	struct poll_event_context *poll_ev;
+
+	poll_ev = talloc_zero(ev, struct poll_event_context);
+	if (poll_ev == NULL) {
+		return -1;
+	}
+	ev->additional_data = poll_ev;
+	return 0;
+}
+
+/*
+  destroy an fd_event
+*/
+static int poll_event_fd_destructor(struct tevent_fd *fde)
+{
+	struct tevent_context *ev = fde->event_ctx;
+	struct poll_event_context *poll_ev = NULL;
+	struct tevent_fd *moved_fde;
+	int del_idx;
+
+	if (ev == NULL) {
+		goto done;
+	}
+
+	poll_ev = talloc_get_type_abort(
+		ev->additional_data, struct poll_event_context);
+
+	/*
+	 * Assume a void * can carry enough bits to hold num_fds.
+	 */
+	del_idx = (int)(fde->additional_data);
+
+	moved_fde = poll_ev->fd_events[poll_ev->num_fds-1];
+	poll_ev->fd_events[del_idx] = moved_fde;
+	poll_ev->fds[del_idx] = poll_ev->fds[poll_ev->num_fds-1];
+	moved_fde->additional_data = (void *)del_idx;
+
+	poll_ev->num_fds -= 1;
+done:
+	return tevent_common_fd_destructor(fde);
+}
+
+/*
+  add a fd based event
+  return NULL on failure (memory allocation error)
+*/
+static struct tevent_fd *poll_event_add_fd(struct tevent_context *ev,
+					   TALLOC_CTX *mem_ctx,
+					   int fd, uint16_t flags,
+					   tevent_fd_handler_t handler,
+					   void *private_data,
+					   const char *handler_name,
+					   const char *location)
+{
+	struct poll_event_context *poll_ev = talloc_get_type_abort(
+		ev->additional_data, struct poll_event_context);
+	struct pollfd *pfd;
+	struct tevent_fd *fde;
+
+	fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
+				   handler, private_data,
+				   handler_name, location);
+	if (fde == NULL) {
+		return NULL;
+	}
+
+	/* we allocate 16 slots to avoid a lot of reallocations */
+	if (talloc_array_length(poll_ev->fds) == poll_ev->num_fds) {
+		struct pollfd *tmp_fds;
+		struct tevent_fd **tmp_fd_events;
+		tmp_fds = talloc_realloc(
+			poll_ev, poll_ev->fds, struct pollfd,
+			poll_ev->num_fds + 16);
+		if (tmp_fds == NULL) {
+			TALLOC_FREE(fde);
+			return NULL;
+		}
+		poll_ev->fds = tmp_fds;
+
+		tmp_fd_events = talloc_realloc(
+			poll_ev, poll_ev->fd_events, struct tevent_fd *,
+			poll_ev->num_fds + 16);
+		if (tmp_fd_events == NULL) {
+			TALLOC_FREE(fde);
+			return NULL;
+		}
+		poll_ev->fd_events = tmp_fd_events;
+	}
+
+	pfd = &poll_ev->fds[poll_ev->num_fds];
+
+	pfd->fd = fd;
+
+	pfd->events = 0;
+	pfd->revents = 0;
+
+	if (flags & TEVENT_FD_READ) {
+		pfd->events |= (POLLIN|POLLHUP);
+	}
+	if (flags & TEVENT_FD_WRITE) {
+		pfd->events |= (POLLOUT);
+	}
+
+	/*
+	 * Assume a void * can carry enough bits to hold num_fds.
+	 */
+	fde->additional_data = (void *)poll_ev->num_fds;
+	poll_ev->fd_events[poll_ev->num_fds] = fde;
+
+	poll_ev->num_fds += 1;
+
+	talloc_set_destructor(fde, poll_event_fd_destructor);


-- 
Samba Shared Repository


More information about the samba-cvs mailing list