Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3 | Amsterdam, 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; |
| 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); |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 53 | DECREF(filenomethod); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 54 | if ( fno == NULL ) |
| 55 | return -1; |
| 56 | if ( !is_intobject(fno) ) { |
| 57 | err_badarg(); |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 58 | DECREF(fno); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | v = getintvalue(fno); |
| 62 | DECREF(fno); |
| 63 | } else { |
| 64 | err_badarg(); |
| 65 | return -1; |
| 66 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | if ( v < 0 || v >= FD_SETSIZE ) { |
| 68 | err_setstr(ValueError, "filedescriptor out of range in select()"); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | if ( v > max ) max = v; |
| 72 | FD_SET(v, set); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | XDECREF(fd2obj[v]); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 74 | fd2obj[v] = o; |
| 75 | } |
| 76 | return max+1; |
| 77 | } |
| 78 | |
| 79 | static object * |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 80 | set2list(set, max, fd2obj) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 81 | fd_set *set; |
| 82 | int max; |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 83 | object *fd2obj[FD_SETSIZE]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 84 | { |
| 85 | int i, num=0; |
| 86 | object *list, *o; |
| 87 | |
| 88 | for(i=0; i<max; i++) |
| 89 | if ( FD_ISSET(i,set) ) |
| 90 | num++; |
| 91 | list = newlistobject(num); |
| 92 | num = 0; |
| 93 | for(i=0; i<max; i++) |
| 94 | if ( FD_ISSET(i,set) ) { |
| 95 | if ( i > FD_SETSIZE ) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 96 | err_setstr(SystemError, |
| 97 | "filedescriptor out of range returned in select()"); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 98 | return NULL; |
| 99 | } |
| 100 | o = fd2obj[i]; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 101 | if ( o == NULL ) { |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 102 | err_setstr(SystemError, |
| 103 | "Bad filedescriptor returned from select()"); |
| 104 | return NULL; |
| 105 | } |
| 106 | INCREF(o); |
| 107 | setlistitem(list, num, o); |
| 108 | num++; |
| 109 | } |
| 110 | return list; |
| 111 | } |
| 112 | |
| 113 | static object * |
| 114 | select_select(self, args) |
| 115 | object *self; |
| 116 | object *args; |
| 117 | { |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 118 | object *fd2obj[FD_SETSIZE]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 119 | object *ifdlist, *ofdlist, *efdlist; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 120 | object *ret, *tout; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 121 | fd_set ifdset, ofdset, efdset; |
| 122 | double timeout; |
| 123 | struct timeval tv, *tvp; |
| 124 | int seconds; |
| 125 | int imax, omax, emax, max; |
| 126 | int n; |
| 127 | |
| 128 | |
| 129 | /* Get args. Looks funny because of optional timeout argument */ |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 130 | if ( getargs(args, "(OOOO)", &ifdlist, &ofdlist, &efdlist, &tout) ) { |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 131 | if (tout == None) |
| 132 | tvp = (struct timeval *)0; |
| 133 | else { |
Sjoerd Mullender | 78ed420 | 1993-11-02 15:34:23 +0000 | [diff] [blame] | 134 | if (!getargs(tout, "d;timeout must be float or None", &timeout)) |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 135 | return NULL; |
Sjoerd Mullender | 78ed420 | 1993-11-02 15:34:23 +0000 | [diff] [blame] | 136 | seconds = (int)timeout; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 137 | timeout = timeout - (double)seconds; |
| 138 | tv.tv_sec = seconds; |
| 139 | tv.tv_usec = (int)(timeout*1000000.0); |
| 140 | tvp = &tv; |
| 141 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 142 | } else { |
| 143 | /* Doesn't have 4 args, that means no timeout */ |
| 144 | err_clear(); |
| 145 | if (!getargs(args, "(OOO)", &ifdlist, &ofdlist, &efdlist) ) |
| 146 | return 0; |
| 147 | tvp = (struct timeval *)0; |
| 148 | } |
| 149 | if ( !is_listobject(ifdlist) || !is_listobject(ofdlist) || |
| 150 | !is_listobject(efdlist) ) { |
| 151 | err_badarg(); |
| 152 | return 0; |
| 153 | } |
| 154 | |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 155 | memset((char *)fd2obj, '\0', sizeof(fd2obj)); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 156 | |
| 157 | /* Convert lists to fd_sets, and get maximum fd number */ |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 158 | if( (imax=list2set(ifdlist, &ifdset, fd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 159 | return 0; |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 160 | if( (omax=list2set(ofdlist, &ofdset, fd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 161 | return 0; |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 162 | if( (emax=list2set(efdlist, &efdset, fd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 163 | return 0; |
| 164 | max = imax; |
| 165 | if ( omax > max ) max = omax; |
| 166 | if ( emax > max ) max = emax; |
| 167 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 168 | BGN_SAVE |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 169 | n = select(max, &ifdset, &ofdset, &efdset, tvp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 170 | END_SAVE |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 171 | |
| 172 | if ( n < 0 ) { |
| 173 | err_errno(SelectError); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | if ( n == 0 ) |
| 178 | imax = omax = emax = 0; /* Speedup hack */ |
| 179 | |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 180 | ifdlist = set2list(&ifdset, imax, fd2obj); |
| 181 | ofdlist = set2list(&ofdset, omax, fd2obj); |
| 182 | efdlist = set2list(&efdset, emax, fd2obj); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 183 | ret = mkvalue("OOO", ifdlist, ofdlist, efdlist); |
| 184 | XDECREF(ifdlist); |
| 185 | XDECREF(ofdlist); |
| 186 | XDECREF(efdlist); |
| 187 | return ret; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | static struct methodlist select_methods[] = { |
| 192 | { "select", select_select }, |
| 193 | { 0, 0 }, |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | void |
| 198 | initselect() |
| 199 | { |
| 200 | object *m, *d; |
| 201 | m = initmodule("select", select_methods); |
| 202 | d = getmoduledict(m); |
| 203 | SelectError = newstringobject("select.error"); |
| 204 | if ( SelectError == NULL || dictinsert(d, "error", SelectError) ) |
| 205 | fatal("Cannot define select.error"); |
| 206 | } |