blob: 5c835887de7bba14bc2513f20a80372cbde999f4 [file] [log] [blame]
Guido van Rossumed233a51992-06-23 09:07:03 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumed233a51992-06-23 09:07:03 +00004
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 Rossumb6775db1994-08-01 11:34:53 +000031#include <sys/types.h>
32#include "myselect.h" /* Also includes mytime.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{
Guido van Rossumde126a61992-08-06 16:53:58 +000042 int i, len, v, max = -1;
Guido van Rossumed233a51992-06-23 09:07:03 +000043 object *o, *filenomethod, *fno;
Guido van Rossum07432c01995-03-29 16:47:45 +000044
45 for ( i=0; i<FD_SETSIZE; i++ ) {
46 fd2obj[i] = (object*)0;
47 }
Guido van Rossumed233a51992-06-23 09:07:03 +000048
49 FD_ZERO(set);
50 len = getlistsize(list);
51 for( i=0; i<len; i++ ) {
52 o = getlistitem(list, i);
53 if ( is_intobject(o) ) {
54 v = getintvalue(o);
55 } else if ( (filenomethod = getattr(o, "fileno")) != NULL ) {
56 fno = call_object(filenomethod, NULL);
Guido van Rossuma849b831993-05-12 11:35:44 +000057 DECREF(filenomethod);
Guido van Rossumed233a51992-06-23 09:07:03 +000058 if ( fno == NULL )
59 return -1;
60 if ( !is_intobject(fno) ) {
61 err_badarg();
Guido van Rossuma849b831993-05-12 11:35:44 +000062 DECREF(fno);
Guido van Rossumed233a51992-06-23 09:07:03 +000063 return -1;
64 }
65 v = getintvalue(fno);
66 DECREF(fno);
67 } else {
68 err_badarg();
69 return -1;
70 }
Guido van Rossumb6775db1994-08-01 11:34:53 +000071 if ( v < 0 || v >= FD_SETSIZE ) {
72 err_setstr(ValueError, "filedescriptor out of range in select()");
Guido van Rossumed233a51992-06-23 09:07:03 +000073 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 *
Guido van Rossum4fbf7981992-08-04 09:13:45 +000083set2list(set, max, fd2obj)
Guido van Rossumed233a51992-06-23 09:07:03 +000084 fd_set *set;
85 int max;
Guido van Rossum4fbf7981992-08-04 09:13:45 +000086 object *fd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +000087{
88 int i, num=0;
89 object *list, *o;
90
91 for(i=0; i<max; i++)
92 if ( FD_ISSET(i,set) )
93 num++;
94 list = newlistobject(num);
95 num = 0;
96 for(i=0; i<max; i++)
97 if ( FD_ISSET(i,set) ) {
98 if ( i > FD_SETSIZE ) {
Guido van Rossumb6775db1994-08-01 11:34:53 +000099 err_setstr(SystemError,
100 "filedescriptor out of range returned in select()");
Guido van Rossumed233a51992-06-23 09:07:03 +0000101 return NULL;
102 }
103 o = fd2obj[i];
Guido van Rossumb6775db1994-08-01 11:34:53 +0000104 if ( o == NULL ) {
Guido van Rossumed233a51992-06-23 09:07:03 +0000105 err_setstr(SystemError,
106 "Bad filedescriptor returned from select()");
107 return NULL;
108 }
109 INCREF(o);
110 setlistitem(list, num, o);
111 num++;
112 }
113 return list;
114}
115
116static object *
117select_select(self, args)
118 object *self;
119 object *args;
120{
Guido van Rossum07432c01995-03-29 16:47:45 +0000121 object *rfd2obj[FD_SETSIZE], *wfd2obj[FD_SETSIZE], *efd2obj[FD_SETSIZE];
Guido van Rossumed233a51992-06-23 09:07:03 +0000122 object *ifdlist, *ofdlist, *efdlist;
Guido van Rossumc7a22701993-11-01 16:27:16 +0000123 object *ret, *tout;
Guido van Rossumed233a51992-06-23 09:07:03 +0000124 fd_set ifdset, ofdset, efdset;
125 double timeout;
126 struct timeval tv, *tvp;
127 int seconds;
128 int imax, omax, emax, max;
129 int n;
130
131
132 /* Get args. Looks funny because of optional timeout argument */
Guido van Rossumc7a22701993-11-01 16:27:16 +0000133 if ( getargs(args, "(OOOO)", &ifdlist, &ofdlist, &efdlist, &tout) ) {
Guido van Rossumc7a22701993-11-01 16:27:16 +0000134 if (tout == None)
135 tvp = (struct timeval *)0;
136 else {
Sjoerd Mullender78ed4201993-11-02 15:34:23 +0000137 if (!getargs(tout, "d;timeout must be float or None", &timeout))
Guido van Rossumc7a22701993-11-01 16:27:16 +0000138 return NULL;
Sjoerd Mullender78ed4201993-11-02 15:34:23 +0000139 seconds = (int)timeout;
Guido van Rossumc7a22701993-11-01 16:27:16 +0000140 timeout = timeout - (double)seconds;
141 tv.tv_sec = seconds;
142 tv.tv_usec = (int)(timeout*1000000.0);
143 tvp = &tv;
144 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000145 } else {
146 /* Doesn't have 4 args, that means no timeout */
147 err_clear();
148 if (!getargs(args, "(OOO)", &ifdlist, &ofdlist, &efdlist) )
149 return 0;
150 tvp = (struct timeval *)0;
151 }
152 if ( !is_listobject(ifdlist) || !is_listobject(ofdlist) ||
153 !is_listobject(efdlist) ) {
154 err_badarg();
155 return 0;
156 }
157
Guido van Rossumed233a51992-06-23 09:07:03 +0000158 /* Convert lists to fd_sets, and get maximum fd number */
Guido van Rossum07432c01995-03-29 16:47:45 +0000159 if( (imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000160 return 0;
Guido van Rossum07432c01995-03-29 16:47:45 +0000161 if( (omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000162 return 0;
Guido van Rossum07432c01995-03-29 16:47:45 +0000163 if( (emax=list2set(efdlist, &efdset, efd2obj)) < 0 )
Guido van Rossumed233a51992-06-23 09:07:03 +0000164 return 0;
165 max = imax;
166 if ( omax > max ) max = omax;
167 if ( emax > max ) max = emax;
168
Guido van Rossumff4949e1992-08-05 19:58:53 +0000169 BGN_SAVE
Guido van Rossumed233a51992-06-23 09:07:03 +0000170 n = select(max, &ifdset, &ofdset, &efdset, tvp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000171 END_SAVE
Guido van Rossumed233a51992-06-23 09:07:03 +0000172
173 if ( n < 0 ) {
174 err_errno(SelectError);
175 return 0;
176 }
177
178 if ( n == 0 )
179 imax = omax = emax = 0; /* Speedup hack */
180
Guido van Rossum07432c01995-03-29 16:47:45 +0000181 ifdlist = set2list(&ifdset, imax, rfd2obj);
182 ofdlist = set2list(&ofdset, omax, wfd2obj);
183 efdlist = set2list(&efdset, emax, efd2obj);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000184 ret = mkvalue("OOO", ifdlist, ofdlist, efdlist);
185 XDECREF(ifdlist);
186 XDECREF(ofdlist);
187 XDECREF(efdlist);
188 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000189}
190
191
192static struct methodlist select_methods[] = {
193 { "select", select_select },
194 { 0, 0 },
195};
196
197
198void
199initselect()
200{
201 object *m, *d;
202 m = initmodule("select", select_methods);
203 d = getmoduledict(m);
204 SelectError = newstringobject("select.error");
205 if ( SelectError == NULL || dictinsert(d, "error", SelectError) )
206 fatal("Cannot define select.error");
207}