1992-06-23 06:07:03 -03:00
|
|
|
/***********************************************************
|
1995-01-04 15:10:35 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1992-06-23 06:07:03 -03:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1992-06-23 06:07:03 -03:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1992-06-23 06:07:03 -03:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
1992-06-23 06:07:03 -03:00
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1992-06-23 06:07:03 -03:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1996-06-12 01:22:53 -03:00
|
|
|
/* select - Module containing unix select(2) call.
|
|
|
|
Under Unix, the file descriptors are small integers.
|
|
|
|
Under Win32, select only exists for sockets, and sockets may
|
|
|
|
have any value except INVALID_SOCKET.
|
|
|
|
*/
|
1992-06-23 06:07:03 -03:00
|
|
|
|
|
|
|
#include "allobjects.h"
|
|
|
|
#include "modsupport.h"
|
|
|
|
#include "ceval.h"
|
|
|
|
|
1996-12-05 19:43:35 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1994-08-01 08:34:53 -03:00
|
|
|
#include <sys/types.h>
|
1996-06-12 01:22:53 -03:00
|
|
|
|
1996-06-28 17:15:15 -03:00
|
|
|
#ifdef MS_WINDOWS
|
1996-06-12 01:22:53 -03:00
|
|
|
#include <winsock.h>
|
|
|
|
#else
|
1994-08-01 08:34:53 -03:00
|
|
|
#include "myselect.h" /* Also includes mytime.h */
|
1996-06-12 01:22:53 -03:00
|
|
|
#define SOCKET int
|
|
|
|
#endif
|
1992-06-23 06:07:03 -03:00
|
|
|
|
|
|
|
static object *SelectError;
|
|
|
|
|
1996-06-12 01:22:53 -03:00
|
|
|
typedef struct { /* list of Python objects and their file descriptor */
|
|
|
|
object *obj;
|
|
|
|
SOCKET fd;
|
|
|
|
} pylist;
|
|
|
|
|
|
|
|
static int
|
1992-08-04 06:13:45 -03:00
|
|
|
list2set(list, set, fd2obj)
|
1992-06-23 06:07:03 -03:00
|
|
|
object *list;
|
|
|
|
fd_set *set;
|
1996-06-12 01:22:53 -03:00
|
|
|
pylist fd2obj[FD_SETSIZE + 3];
|
1992-06-23 06:07:03 -03:00
|
|
|
{
|
1996-06-12 01:22:53 -03:00
|
|
|
int i, len, index, max = -1;
|
1992-06-23 06:07:03 -03:00
|
|
|
object *o, *filenomethod, *fno;
|
1996-06-12 01:22:53 -03:00
|
|
|
SOCKET v;
|
1995-03-29 12:47:45 -04:00
|
|
|
|
1996-06-12 01:22:53 -03:00
|
|
|
index = 0;
|
|
|
|
fd2obj[0].obj = (object*)0; /* set list to zero size */
|
1992-06-23 06:07:03 -03:00
|
|
|
|
|
|
|
FD_ZERO(set);
|
|
|
|
len = getlistsize(list);
|
|
|
|
for( i=0; i<len; i++ ) {
|
|
|
|
o = getlistitem(list, i);
|
|
|
|
if ( is_intobject(o) ) {
|
|
|
|
v = getintvalue(o);
|
|
|
|
} else if ( (filenomethod = getattr(o, "fileno")) != NULL ) {
|
|
|
|
fno = call_object(filenomethod, NULL);
|
1993-05-12 08:35:44 -03:00
|
|
|
DECREF(filenomethod);
|
1992-06-23 06:07:03 -03:00
|
|
|
if ( fno == NULL )
|
|
|
|
return -1;
|
|
|
|
if ( !is_intobject(fno) ) {
|
|
|
|
err_badarg();
|
1993-05-12 08:35:44 -03:00
|
|
|
DECREF(fno);
|
1992-06-23 06:07:03 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v = getintvalue(fno);
|
|
|
|
DECREF(fno);
|
|
|
|
} else {
|
|
|
|
err_badarg();
|
|
|
|
return -1;
|
|
|
|
}
|
1996-06-12 01:22:53 -03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
max = 0; /* not used for Win32 */
|
|
|
|
#else
|
1994-08-01 08:34:53 -03:00
|
|
|
if ( v < 0 || v >= FD_SETSIZE ) {
|
|
|
|
err_setstr(ValueError, "filedescriptor out of range in select()");
|
1992-06-23 06:07:03 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( v > max ) max = v;
|
1996-06-12 01:22:53 -03:00
|
|
|
#endif
|
1992-06-23 06:07:03 -03:00
|
|
|
FD_SET(v, set);
|
1996-06-12 01:22:53 -03:00
|
|
|
/* add object and its file descriptor to the list */
|
|
|
|
if ( index >= FD_SETSIZE ) {
|
|
|
|
err_setstr(ValueError, "too many file descriptors in select()");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd2obj[index].obj = o;
|
|
|
|
fd2obj[index].fd = v;
|
|
|
|
fd2obj[++index].obj = (object *)0; /* sentinel */
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|
|
|
|
return max+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1996-06-12 01:22:53 -03:00
|
|
|
set2list(set, fd2obj)
|
1992-06-23 06:07:03 -03:00
|
|
|
fd_set *set;
|
1996-06-12 01:22:53 -03:00
|
|
|
pylist fd2obj[FD_SETSIZE + 3];
|
1992-06-23 06:07:03 -03:00
|
|
|
{
|
1996-06-12 01:22:53 -03:00
|
|
|
int j, num=0;
|
1992-06-23 06:07:03 -03:00
|
|
|
object *list, *o;
|
1996-06-12 01:22:53 -03:00
|
|
|
SOCKET fd;
|
1992-06-23 06:07:03 -03:00
|
|
|
|
1996-06-12 01:22:53 -03:00
|
|
|
for(j=0; fd2obj[j].obj; j++)
|
|
|
|
if ( FD_ISSET(fd2obj[j].fd, set) )
|
1992-06-23 06:07:03 -03:00
|
|
|
num++;
|
|
|
|
list = newlistobject(num);
|
|
|
|
num = 0;
|
1996-06-12 01:22:53 -03:00
|
|
|
for(j=0; fd2obj[j].obj; j++) {
|
|
|
|
fd = fd2obj[j].fd;
|
|
|
|
if ( FD_ISSET(fd, set) ) {
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
if ( fd > FD_SETSIZE ) {
|
1994-08-01 08:34:53 -03:00
|
|
|
err_setstr(SystemError,
|
|
|
|
"filedescriptor out of range returned in select()");
|
1992-06-23 06:07:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-06-12 01:22:53 -03:00
|
|
|
#endif
|
|
|
|
o = fd2obj[j].obj;
|
1992-06-23 06:07:03 -03:00
|
|
|
INCREF(o);
|
|
|
|
setlistitem(list, num, o);
|
|
|
|
num++;
|
1996-06-12 01:22:53 -03:00
|
|
|
}
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
select_select(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1996-06-12 01:22:53 -03:00
|
|
|
pylist rfd2obj[FD_SETSIZE + 3], wfd2obj[FD_SETSIZE + 3], efd2obj[FD_SETSIZE + 3];
|
1992-06-23 06:07:03 -03:00
|
|
|
object *ifdlist, *ofdlist, *efdlist;
|
1993-11-01 12:27:16 -04:00
|
|
|
object *ret, *tout;
|
1992-06-23 06:07:03 -03:00
|
|
|
fd_set ifdset, ofdset, efdset;
|
|
|
|
double timeout;
|
|
|
|
struct timeval tv, *tvp;
|
|
|
|
int seconds;
|
|
|
|
int imax, omax, emax, max;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
|
|
/* Get args. Looks funny because of optional timeout argument */
|
1993-11-01 12:27:16 -04:00
|
|
|
if ( getargs(args, "(OOOO)", &ifdlist, &ofdlist, &efdlist, &tout) ) {
|
|
|
|
if (tout == None)
|
|
|
|
tvp = (struct timeval *)0;
|
|
|
|
else {
|
1993-11-02 11:34:23 -04:00
|
|
|
if (!getargs(tout, "d;timeout must be float or None", &timeout))
|
1993-11-01 12:27:16 -04:00
|
|
|
return NULL;
|
1993-11-02 11:34:23 -04:00
|
|
|
seconds = (int)timeout;
|
1993-11-01 12:27:16 -04:00
|
|
|
timeout = timeout - (double)seconds;
|
|
|
|
tv.tv_sec = seconds;
|
|
|
|
tv.tv_usec = (int)(timeout*1000000.0);
|
|
|
|
tvp = &tv;
|
|
|
|
}
|
1992-06-23 06:07:03 -03:00
|
|
|
} else {
|
|
|
|
/* Doesn't have 4 args, that means no timeout */
|
|
|
|
err_clear();
|
|
|
|
if (!getargs(args, "(OOO)", &ifdlist, &ofdlist, &efdlist) )
|
|
|
|
return 0;
|
|
|
|
tvp = (struct timeval *)0;
|
|
|
|
}
|
|
|
|
if ( !is_listobject(ifdlist) || !is_listobject(ofdlist) ||
|
|
|
|
!is_listobject(efdlist) ) {
|
|
|
|
err_badarg();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert lists to fd_sets, and get maximum fd number */
|
1995-03-29 12:47:45 -04:00
|
|
|
if( (imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0 )
|
1992-06-23 06:07:03 -03:00
|
|
|
return 0;
|
1995-03-29 12:47:45 -04:00
|
|
|
if( (omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0 )
|
1992-06-23 06:07:03 -03:00
|
|
|
return 0;
|
1995-03-29 12:47:45 -04:00
|
|
|
if( (emax=list2set(efdlist, &efdset, efd2obj)) < 0 )
|
1992-06-23 06:07:03 -03:00
|
|
|
return 0;
|
|
|
|
max = imax;
|
|
|
|
if ( omax > max ) max = omax;
|
|
|
|
if ( emax > max ) max = emax;
|
|
|
|
|
1992-08-05 16:58:53 -03:00
|
|
|
BGN_SAVE
|
1992-06-23 06:07:03 -03:00
|
|
|
n = select(max, &ifdset, &ofdset, &efdset, tvp);
|
1992-08-05 16:58:53 -03:00
|
|
|
END_SAVE
|
1992-06-23 06:07:03 -03:00
|
|
|
|
|
|
|
if ( n < 0 ) {
|
|
|
|
err_errno(SelectError);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-06-12 01:22:53 -03:00
|
|
|
if ( n == 0 ) { /* Speedup hack */
|
|
|
|
ifdlist = newlistobject(0);
|
|
|
|
ret = mkvalue("OOO", ifdlist, ifdlist, ifdlist);
|
|
|
|
XDECREF(ifdlist);
|
|
|
|
return ret;
|
|
|
|
}
|
1992-06-23 06:07:03 -03:00
|
|
|
|
1996-06-12 01:22:53 -03:00
|
|
|
ifdlist = set2list(&ifdset, rfd2obj);
|
|
|
|
ofdlist = set2list(&ofdset, wfd2obj);
|
|
|
|
efdlist = set2list(&efdset, efd2obj);
|
1993-02-05 05:46:15 -04:00
|
|
|
ret = mkvalue("OOO", ifdlist, ofdlist, efdlist);
|
|
|
|
XDECREF(ifdlist);
|
|
|
|
XDECREF(ofdlist);
|
|
|
|
XDECREF(efdlist);
|
|
|
|
return ret;
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct methodlist select_methods[] = {
|
|
|
|
{ "select", select_select },
|
|
|
|
{ 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
initselect()
|
|
|
|
{
|
|
|
|
object *m, *d;
|
|
|
|
m = initmodule("select", select_methods);
|
|
|
|
d = getmoduledict(m);
|
|
|
|
SelectError = newstringobject("select.error");
|
|
|
|
if ( SelectError == NULL || dictinsert(d, "error", SelectError) )
|
|
|
|
fatal("Cannot define select.error");
|
|
|
|
}
|