[PATCH v2 13/14] daemon: use select() instead of poll()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Windows doesn't have poll(), and the poll-emulation in
compat/mingw.c doesn't support checking multiple sockets.

Signed-off-by: Erik Faye-Lund <kusmabite@xxxxxxxxx>
---
 compat/mingw.h |    7 +++++++
 daemon.c       |   27 ++++++++++++---------------
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/compat/mingw.h b/compat/mingw.h
index 173bec5..e515726 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -269,6 +269,13 @@ int mingw_accept(int sockfd, struct sockaddr *sa, socklen_t *sz);
 int mingw_rename(const char*, const char*);
 #define rename mingw_rename
 
+#undef FD_SET
+#define FD_SET(fd, set) do { \
+	((fd_set*)(set))->fd_array[((fd_set *)(set))->fd_count++] = _get_osfhandle(fd); \
+	} while(0)
+#undef FD_ISSET
+#define FD_ISSET(fd, set) __WSAFDIsSet(_get_osfhandle(fd), (fd_set *)(set))
+
 #if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
 int mingw_getpagesize(void);
 #define getpagesize mingw_getpagesize
diff --git a/daemon.c b/daemon.c
index cdf5c72..95cf299 100644
--- a/daemon.c
+++ b/daemon.c
@@ -818,26 +818,23 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
 
 static int service_loop(int socknum, int *socklist)
 {
-	struct pollfd *pfd;
-	int i;
-
-	pfd = xcalloc(socknum, sizeof(struct pollfd));
-
-	for (i = 0; i < socknum; i++) {
-		pfd[i].fd = socklist[i];
-		pfd[i].events = POLLIN;
-	}
-
 	signal(SIGCHLD, child_handler);
 
 	for (;;) {
-		int i;
+		int i, maxfd = 0;
+		fd_set fds;
 
 		check_dead_children();
 
-		if (poll(pfd, socknum, -1) < 0) {
+		FD_ZERO(&fds);
+		for (i = 0; i < socknum; i++) {
+			FD_SET(socklist[i], &fds);
+			maxfd = socklist[i] > maxfd ? socklist[i] : maxfd;
+		}
+
+		if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 0) {
 			if (errno != EINTR) {
-				logerror("Poll failed, resuming: %s",
+				logerror("select() failed, resuming: %s",
 				      strerror(errno));
 				sleep(1);
 			}
@@ -845,10 +842,10 @@ static int service_loop(int socknum, int *socklist)
 		}
 
 		for (i = 0; i < socknum; i++) {
-			if (pfd[i].revents & POLLIN) {
+			if (FD_ISSET(socklist[i], &fds)) {
 				struct sockaddr_storage ss;
 				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
+				int incoming = accept(socklist[i], (struct sockaddr *)&ss, &sslen);
 				if (incoming < 0) {
 					switch (errno) {
 					case EAGAIN:
-- 
1.6.6.211.g26720

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]