[Asterisk-Dev] Some select(2) usage Linux-specific

Thorsten Lockert tholo at sigmasoft.com
Thu Apr 24 14:45:26 MST 2003


A couple of calls to select(2) are assuming a Linux-specific side-
effect of the passed timeval being updated with how much time was
left in before a timeout would occur.

This patch makes this work (at the expense of two more system calls)
on many other systems.

Index: channel.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channel.c,v
retrieving revision 1.11
diff -u -r1.11 channel.c
--- channel.c	17 Apr 2003 02:52:53 -0000	1.11
+++ channel.c	24 Apr 2003 21:46:10 -0000
@@ -739,6 +739,9 @@
 {
 	/* Wait for x amount of time on a file descriptor to have input.  */
 	struct timeval tv;
+#ifndef __linux__
+	struct timeval tvstart, tvend, tvlen;
+#endif
 	fd_set rfds, efds;
 	int res;
 	int x, max=-1;
@@ -756,8 +759,23 @@
 				max = fds[x];
 		}
 	}
-	if (*ms >= 0) 
+	if (*ms >= 0)  {
+#ifndef __linux__
+		gettimeofday(&tvstart, NULL);
+#endif
 		res = select(max + 1, &rfds, NULL, &efds, &tv);
+#ifndef __linux__
+		gettimeofday(&tvend, NULL);
+		timersub(&tvend, &tvstart, &tvlen);
+		tvend.tv_sec = *ms / 1000;
+		tvend.tv_usec = (*ms % 1000) * 1000;
+		timersub(&tvend, &tvlen, &tv);
+		if (tv.tv_sec < 0 || tv.tv_usec < 0) {
+			tv.tv_sec = 0;
+			tv.tv_usec = 0;
+		}
+#endif
+	}
 	else
 		res = select(max + 1, &rfds, NULL, &efds, NULL);
 
@@ -786,6 +804,9 @@
 {
 	/* Wait for x amount of time on a file descriptor to have input.  */
 	struct timeval tv;
+#ifndef __linux__
+	struct timeval tvstart, tvend, tvlen;
+#endif
 	fd_set rfds, efds;
 	int res;
 	int x, y, max=-1;
@@ -828,8 +849,23 @@
 		if (fds[x] > max)
 			max = fds[x];
 	}
-	if (*ms >= 0) 
+	if (*ms >= 0) {
+#ifndef __linux__
+		gettimeofday(&tvstart, NULL);
+#endif
 		res = select(max + 1, &rfds, NULL, &efds, &tv);
+#ifndef __linux__
+		gettimeofday(&tvend, NULL);
+		timersub(&tvend, &tvstart, &tvlen);
+		tvend.tv_sec = *ms / 1000;
+		tvend.tv_usec = (*ms % 1000) * 1000;
+		timersub(&tvend, &tvlen, &tv);
+		if (tv.tv_sec < 0 || tv.tv_usec < 0) {
+			tv.tv_sec = 0;
+			tv.tv_usec = 0;
+		}
+#endif
+	}
 	else
 		res = select(max + 1, &rfds, NULL, &efds, NULL);
 
--
Thorsten Lockert      | tholo at sigmasoft.com | Universe, n.:
2121 N. Lakeshore Dr. | tholo at openbsd.org   |         The problem.
Chapel Hill, NC 27514 |                     |



More information about the asterisk-dev mailing list