blob: 3a5644b446a17ebeb683d339a30bbad3c89d0fb6 [file] [log] [blame]
Guido van Rossumed233a51992-06-23 09:07:03 +00001/***********************************************************
2Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* select - Module containing unix select(2) call */
26
27#include "allobjects.h"
28#include "modsupport.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000029#include "ceval.h"
30
Guido van Rossum9d479921992-06-23 15:23:28 +000031#include "myselect.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000032
33static object *SelectError;
34
Guido van Rossumed233a51992-06-23 09:07:03 +000035static
Guido van Rossum4fbf7981992-08-04 09:13:45 +000036list2set(list, set, fd2obj)
Guido van Rossumed233a51992-06-23 09:07:03 +000037 object *list;
38 fd_set *set;
Guido van Rossum4fbf7981992-08-04 09:13:45 +000039 object *fd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +000040{
41 int i, len, v, max=-1;
42 object *o, *filenomethod, *fno;
43
44 FD_ZERO(set);
45 len = getlistsize(list);
46 for( i=0; i<len; i++ ) {
47 o = getlistitem(list, i);
48 if ( is_intobject(o) ) {
49 v = getintvalue(o);
50 } else if ( (filenomethod = getattr(o, "fileno")) != NULL ) {
51 fno = call_object(filenomethod, NULL);
52 if ( fno == NULL )
53 return -1;
54 if ( !is_intobject(fno) ) {
55 err_badarg();
56 return -1;
57 }
58 v = getintvalue(fno);
59 DECREF(fno);
60 } else {
61 err_badarg();
62 return -1;
63 }
64 if ( v >= FD_SETSIZE ) {
65 err_setstr(SystemError, "FD_SETSIZE too low in select()");
66 return -1;
67 }
68 if ( v > max ) max = v;
69 FD_SET(v, set);
70 fd2obj[v] = o;
71 }
72 return max+1;
73}
74
75static object *
Guido van Rossum4fbf7981992-08-04 09:13:45 +000076set2list(set, max, fd2obj)
Guido van Rossumed233a51992-06-23 09:07:03 +000077 fd_set *set;
78 int max;
Guido van Rossum4fbf7981992-08-04 09:13:45 +000079 object *fd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +000080{
81 int i, num=0;
82 object *list, *o;
83
84 for(i=0; i<max; i++)
85 if ( FD_ISSET(i,set) )
86 num++;
87 list = newlistobject(num);
88 num = 0;
89 for(i=0; i<max; i++)
90 if ( FD_ISSET(i,set) ) {
91 if ( i > FD_SETSIZE ) {
92 err_setstr(SystemError, "FD_SETSIZE too low in select()");
93 return NULL;
94 }
95 o = fd2obj[i];
96 if ( o == 0 ) {
97 err_setstr(SystemError,
98 "Bad filedescriptor returned from select()");
99 return NULL;
100 }
101 INCREF(o);
102 setlistitem(list, num, o);
103 num++;
104 }
105 return list;
106}
107
108static object *
109select_select(self, args)
110 object *self;
111 object *args;
112{
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000113 object *fd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +0000114 object *ifdlist, *ofdlist, *efdlist;
115 fd_set ifdset, ofdset, efdset;
116 double timeout;
117 struct timeval tv, *tvp;
118 int seconds;
119 int imax, omax, emax, max;
120 int n;
121
122
123 /* Get args. Looks funny because of optional timeout argument */
124 if ( getargs(args, "(OOOd)", &ifdlist, &ofdlist, &efdlist, &timeout) ) {
125 seconds = (int)timeout;
126 timeout = timeout - (double)seconds;
127 tv.tv_sec = seconds;
128 tv.tv_usec = (int)(timeout*1000000.0);
129 tvp = &tv;
130 } else {
131 /* Doesn't have 4 args, that means no timeout */
132 err_clear();
133 if (!getargs(args, "(OOO)", &ifdlist, &ofdlist, &efdlist) )
134 return 0;
135 tvp = (struct timeval *)0;
136 }
137 if ( !is_listobject(ifdlist) || !is_listobject(ofdlist) ||
138 !is_listobject(efdlist) ) {
139 err_badarg();
140 return 0;
141 }
142
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000143 memset((char *)fd2obj, '\0', sizeof(fd2obj));
Guido van Rossumed233a51992-06-23 09:07:03 +0000144
145 /* Convert lists to fd_sets, and get maximum fd number */
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000146 if( (imax=list2set(ifdlist, &ifdset, fd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000147 return 0;
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000148 if( (omax=list2set(ofdlist, &ofdset, fd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000149 return 0;
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000150 if( (emax=list2set(efdlist, &efdset, fd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000151 return 0;
152 max = imax;
153 if ( omax > max ) max = omax;
154 if ( emax > max ) max = emax;
155
Guido van Rossumff4949e1992-08-05 19:58:53 +0000156 BGN_SAVE
Guido van Rossumed233a51992-06-23 09:07:03 +0000157 n = select(max, &ifdset, &ofdset, &efdset, tvp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000158 END_SAVE
Guido van Rossumed233a51992-06-23 09:07:03 +0000159
160 if ( n < 0 ) {
161 err_errno(SelectError);
162 return 0;
163 }
164
165 if ( n == 0 )
166 imax = omax = emax = 0; /* Speedup hack */
167
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000168 ifdlist = set2list(&ifdset, imax, fd2obj);
169 ofdlist = set2list(&ofdset, omax, fd2obj);
170 efdlist = set2list(&efdset, emax, fd2obj);
Guido van Rossumed233a51992-06-23 09:07:03 +0000171
172 return mkvalue("OOO", ifdlist, ofdlist, efdlist);
173}
174
175
176static struct methodlist select_methods[] = {
177 { "select", select_select },
178 { 0, 0 },
179};
180
181
182void
183initselect()
184{
185 object *m, *d;
186 m = initmodule("select", select_methods);
187 d = getmoduledict(m);
188 SelectError = newstringobject("select.error");
189 if ( SelectError == NULL || dictinsert(d, "error", SelectError) )
190 fatal("Cannot define select.error");
191}