blob: 8afd208ca9ec3ab8d55175d868cc1e8db22dfcee [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
Guido van Rossumed233a51992-06-23 09:07:03 +000036static
Guido van Rossum4fbf7981992-08-04 09:13:45 +000037list2set(list, set, fd2obj)
Guido van Rossumed233a51992-06-23 09:07:03 +000038 object *list;
39 fd_set *set;
Guido van Rossum4fbf7981992-08-04 09:13:45 +000040 object *fd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +000041{
42 int i, len, v, max=-1;
43 object *o, *filenomethod, *fno;
44
45 FD_ZERO(set);
46 len = getlistsize(list);
47 for( i=0; i<len; i++ ) {
48 o = getlistitem(list, i);
49 if ( is_intobject(o) ) {
50 v = getintvalue(o);
51 } else if ( (filenomethod = getattr(o, "fileno")) != NULL ) {
52 fno = call_object(filenomethod, NULL);
53 if ( fno == NULL )
54 return -1;
55 if ( !is_intobject(fno) ) {
56 err_badarg();
57 return -1;
58 }
59 v = getintvalue(fno);
60 DECREF(fno);
61 } else {
62 err_badarg();
63 return -1;
64 }
65 if ( v >= FD_SETSIZE ) {
66 err_setstr(SystemError, "FD_SETSIZE too low in select()");
67 return -1;
68 }
69 if ( v > max ) max = v;
70 FD_SET(v, set);
71 fd2obj[v] = o;
72 }
73 return max+1;
74}
75
76static object *
Guido van Rossum4fbf7981992-08-04 09:13:45 +000077set2list(set, max, fd2obj)
Guido van Rossumed233a51992-06-23 09:07:03 +000078 fd_set *set;
79 int max;
Guido van Rossum4fbf7981992-08-04 09:13:45 +000080 object *fd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +000081{
82 int i, num=0;
83 object *list, *o;
84
85 for(i=0; i<max; i++)
86 if ( FD_ISSET(i,set) )
87 num++;
88 list = newlistobject(num);
89 num = 0;
90 for(i=0; i<max; i++)
91 if ( FD_ISSET(i,set) ) {
92 if ( i > FD_SETSIZE ) {
93 err_setstr(SystemError, "FD_SETSIZE too low in select()");
94 return NULL;
95 }
96 o = fd2obj[i];
97 if ( o == 0 ) {
98 err_setstr(SystemError,
99 "Bad filedescriptor returned from select()");
100 return NULL;
101 }
102 INCREF(o);
103 setlistitem(list, num, o);
104 num++;
105 }
106 return list;
107}
108
109static object *
110select_select(self, args)
111 object *self;
112 object *args;
113{
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000114 object *fd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +0000115 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
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000144 memset((char *)fd2obj, '\0', sizeof(fd2obj));
Guido van Rossumed233a51992-06-23 09:07:03 +0000145
146 /* Convert lists to fd_sets, and get maximum fd number */
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000147 if( (imax=list2set(ifdlist, &ifdset, fd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000148 return 0;
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000149 if( (omax=list2set(ofdlist, &ofdset, fd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000150 return 0;
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000151 if( (emax=list2set(efdlist, &efdset, fd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000152 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
Guido van Rossum4fbf7981992-08-04 09:13:45 +0000167 ifdlist = set2list(&ifdset, imax, fd2obj);
168 ofdlist = set2list(&ofdset, omax, fd2obj);
169 efdlist = set2list(&efdset, emax, fd2obj);
Guido van Rossumed233a51992-06-23 09:07:03 +0000170
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}