blob: 0a102bd0eaf38792a12ca5847adb57b1d59bdf72 [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"
29#include "compile.h"
30#include "ceval.h"
31
32#include <sys/types.h>
33#include <sys/time.h>
34#include <sys/param.h>
35
36/* XXX Maybe you need to define the FD_* macros here when porting this code */
37
38static object *SelectError;
39
40/* XXX This module should be re-entrant! */
41static object *fd2obj[FD_SETSIZE];
42
43static
44list2set(list, set)
45 object *list;
46 fd_set *set;
47{
48 int i, len, v, max=-1;
49 object *o, *filenomethod, *fno;
50
51 FD_ZERO(set);
52 len = getlistsize(list);
53 for( i=0; i<len; i++ ) {
54 o = getlistitem(list, i);
55 if ( is_intobject(o) ) {
56 v = getintvalue(o);
57 } else if ( (filenomethod = getattr(o, "fileno")) != NULL ) {
58 fno = call_object(filenomethod, NULL);
59 if ( fno == NULL )
60 return -1;
61 if ( !is_intobject(fno) ) {
62 err_badarg();
63 return -1;
64 }
65 v = getintvalue(fno);
66 DECREF(fno);
67 } else {
68 err_badarg();
69 return -1;
70 }
71 if ( v >= FD_SETSIZE ) {
72 err_setstr(SystemError, "FD_SETSIZE too low in select()");
73 return -1;
74 }
75 if ( v > max ) max = v;
76 FD_SET(v, set);
77 fd2obj[v] = o;
78 }
79 return max+1;
80}
81
82static object *
83set2list(set, max)
84 fd_set *set;
85 int max;
86{
87 int i, num=0;
88 object *list, *o;
89
90 for(i=0; i<max; i++)
91 if ( FD_ISSET(i,set) )
92 num++;
93 list = newlistobject(num);
94 num = 0;
95 for(i=0; i<max; i++)
96 if ( FD_ISSET(i,set) ) {
97 if ( i > FD_SETSIZE ) {
98 err_setstr(SystemError, "FD_SETSIZE too low in select()");
99 return NULL;
100 }
101 o = fd2obj[i];
102 if ( o == 0 ) {
103 err_setstr(SystemError,
104 "Bad filedescriptor returned from select()");
105 return NULL;
106 }
107 INCREF(o);
108 setlistitem(list, num, o);
109 num++;
110 }
111 return list;
112}
113
114static object *
115select_select(self, args)
116 object *self;
117 object *args;
118{
119 object *ifdlist, *ofdlist, *efdlist;
120 fd_set ifdset, ofdset, efdset;
121 double timeout;
122 struct timeval tv, *tvp;
123 int seconds;
124 int imax, omax, emax, max;
125 int n;
126
127
128 /* Get args. Looks funny because of optional timeout argument */
129 if ( getargs(args, "(OOOd)", &ifdlist, &ofdlist, &efdlist, &timeout) ) {
130 seconds = (int)timeout;
131 timeout = timeout - (double)seconds;
132 tv.tv_sec = seconds;
133 tv.tv_usec = (int)(timeout*1000000.0);
134 tvp = &tv;
135 } else {
136 /* Doesn't have 4 args, that means no timeout */
137 err_clear();
138 if (!getargs(args, "(OOO)", &ifdlist, &ofdlist, &efdlist) )
139 return 0;
140 tvp = (struct timeval *)0;
141 }
142 if ( !is_listobject(ifdlist) || !is_listobject(ofdlist) ||
143 !is_listobject(efdlist) ) {
144 err_badarg();
145 return 0;
146 }
147
148 bzero((char *)fd2obj, sizeof(fd2obj)); /* Not really needed */
149
150 /* Convert lists to fd_sets, and get maximum fd number */
151 if( (imax=list2set(ifdlist, &ifdset)) < 0 )
152 return 0;
153 if( (omax=list2set(ofdlist, &ofdset)) < 0 )
154 return 0;
155 if( (emax=list2set(efdlist, &efdset)) < 0 )
156 return 0;
157 max = imax;
158 if ( omax > max ) max = omax;
159 if ( emax > max ) max = emax;
160
161 n = select(max, &ifdset, &ofdset, &efdset, tvp);
162
163 if ( n < 0 ) {
164 err_errno(SelectError);
165 return 0;
166 }
167
168 if ( n == 0 )
169 imax = omax = emax = 0; /* Speedup hack */
170
171 ifdlist = set2list(&ifdset, imax);
172 ofdlist = set2list(&ofdset, omax);
173 efdlist = set2list(&efdset, emax);
174
175 return mkvalue("OOO", ifdlist, ofdlist, efdlist);
176}
177
178
179static struct methodlist select_methods[] = {
180 { "select", select_select },
181 { 0, 0 },
182};
183
184
185void
186initselect()
187{
188 object *m, *d;
189 m = initmodule("select", select_methods);
190 d = getmoduledict(m);
191 SelectError = newstringobject("select.error");
192 if ( SelectError == NULL || dictinsert(d, "error", SelectError) )
193 fatal("Cannot define select.error");
194}