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