Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF 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 Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 29 | #include "ceval.h" |
| 30 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include "myselect.h" /* Also includes mytime.h */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 33 | |
| 34 | static object *SelectError; |
| 35 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 36 | static |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 37 | list2set(list, set, fd2obj) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 38 | object *list; |
| 39 | fd_set *set; |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 40 | object *fd2obj[FD_SETSIZE]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 41 | { |
Guido van Rossum | de126a6 | 1992-08-06 16:53:58 +0000 | [diff] [blame] | 42 | int i, len, v, max = -1; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 43 | object *o, *filenomethod, *fno; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 44 | |
| 45 | for ( i=0; i<FD_SETSIZE; i++ ) { |
| 46 | fd2obj[i] = (object*)0; |
| 47 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 48 | |
| 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 Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 57 | DECREF(filenomethod); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 58 | if ( fno == NULL ) |
| 59 | return -1; |
| 60 | if ( !is_intobject(fno) ) { |
| 61 | err_badarg(); |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 62 | DECREF(fno); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 63 | return -1; |
| 64 | } |
| 65 | v = getintvalue(fno); |
| 66 | DECREF(fno); |
| 67 | } else { |
| 68 | err_badarg(); |
| 69 | return -1; |
| 70 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | if ( v < 0 || v >= FD_SETSIZE ) { |
| 72 | err_setstr(ValueError, "filedescriptor out of range in select()"); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 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 | |
| 82 | static object * |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 83 | set2list(set, max, fd2obj) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 84 | fd_set *set; |
| 85 | int max; |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 86 | object *fd2obj[FD_SETSIZE]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 87 | { |
| 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 99 | err_setstr(SystemError, |
| 100 | "filedescriptor out of range returned in select()"); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
| 103 | o = fd2obj[i]; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 104 | if ( o == NULL ) { |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 105 | 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 | |
| 116 | static object * |
| 117 | select_select(self, args) |
| 118 | object *self; |
| 119 | object *args; |
| 120 | { |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 121 | object *rfd2obj[FD_SETSIZE], *wfd2obj[FD_SETSIZE], *efd2obj[FD_SETSIZE]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 122 | object *ifdlist, *ofdlist, *efdlist; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 123 | object *ret, *tout; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 124 | 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 Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 133 | if ( getargs(args, "(OOOO)", &ifdlist, &ofdlist, &efdlist, &tout) ) { |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 134 | if (tout == None) |
| 135 | tvp = (struct timeval *)0; |
| 136 | else { |
Sjoerd Mullender | 78ed420 | 1993-11-02 15:34:23 +0000 | [diff] [blame] | 137 | if (!getargs(tout, "d;timeout must be float or None", &timeout)) |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 138 | return NULL; |
Sjoerd Mullender | 78ed420 | 1993-11-02 15:34:23 +0000 | [diff] [blame] | 139 | seconds = (int)timeout; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 140 | timeout = timeout - (double)seconds; |
| 141 | tv.tv_sec = seconds; |
| 142 | tv.tv_usec = (int)(timeout*1000000.0); |
| 143 | tvp = &tv; |
| 144 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 145 | } 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 Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 158 | /* Convert lists to fd_sets, and get maximum fd number */ |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 159 | if( (imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 160 | return 0; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 161 | if( (omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 162 | return 0; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 163 | if( (emax=list2set(efdlist, &efdset, efd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 164 | return 0; |
| 165 | max = imax; |
| 166 | if ( omax > max ) max = omax; |
| 167 | if ( emax > max ) max = emax; |
| 168 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 169 | BGN_SAVE |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 170 | n = select(max, &ifdset, &ofdset, &efdset, tvp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 171 | END_SAVE |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 172 | |
| 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 Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 181 | ifdlist = set2list(&ifdset, imax, rfd2obj); |
| 182 | ofdlist = set2list(&ofdset, omax, wfd2obj); |
| 183 | efdlist = set2list(&efdset, emax, efd2obj); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 184 | ret = mkvalue("OOO", ifdlist, ofdlist, efdlist); |
| 185 | XDECREF(ifdlist); |
| 186 | XDECREF(ofdlist); |
| 187 | XDECREF(efdlist); |
| 188 | return ret; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | static struct methodlist select_methods[] = { |
| 193 | { "select", select_select }, |
| 194 | { 0, 0 }, |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | void |
| 199 | initselect() |
| 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 | } |