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 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 32 | /* select - Module containing unix select(2) call. |
| 33 | Under Unix, the file descriptors are small integers. |
| 34 | Under Win32, select only exists for sockets, and sockets may |
| 35 | have any value except INVALID_SOCKET. |
| 36 | */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 37 | |
| 38 | #include "allobjects.h" |
| 39 | #include "modsupport.h" |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 40 | #include "ceval.h" |
| 41 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 42 | #ifdef HAVE_UNISTD_H |
| 43 | #include <unistd.h> |
| 44 | #endif |
| 45 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | #include <sys/types.h> |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 49 | #include <winsock.h> |
| 50 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #include "myselect.h" /* Also includes mytime.h */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 52 | #define SOCKET int |
| 53 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 54 | |
| 55 | static object *SelectError; |
| 56 | |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 57 | typedef struct { /* list of Python objects and their file descriptor */ |
| 58 | object *obj; |
| 59 | SOCKET fd; |
| 60 | } pylist; |
| 61 | |
| 62 | static int |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 63 | list2set(list, set, fd2obj) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 64 | object *list; |
| 65 | fd_set *set; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 66 | pylist fd2obj[FD_SETSIZE + 3]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 67 | { |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 68 | int i, len, index, max = -1; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 69 | object *o, *filenomethod, *fno; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 70 | SOCKET v; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 72 | index = 0; |
| 73 | fd2obj[0].obj = (object*)0; /* set list to zero size */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 74 | |
| 75 | FD_ZERO(set); |
| 76 | len = getlistsize(list); |
| 77 | for( i=0; i<len; i++ ) { |
| 78 | o = getlistitem(list, i); |
| 79 | if ( is_intobject(o) ) { |
| 80 | v = getintvalue(o); |
| 81 | } else if ( (filenomethod = getattr(o, "fileno")) != NULL ) { |
| 82 | fno = call_object(filenomethod, NULL); |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 83 | DECREF(filenomethod); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 84 | if ( fno == NULL ) |
| 85 | return -1; |
| 86 | if ( !is_intobject(fno) ) { |
| 87 | err_badarg(); |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 88 | DECREF(fno); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | v = getintvalue(fno); |
| 92 | DECREF(fno); |
| 93 | } else { |
| 94 | err_badarg(); |
| 95 | return -1; |
| 96 | } |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 97 | #ifdef _MSC_VER |
| 98 | max = 0; /* not used for Win32 */ |
| 99 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 100 | if ( v < 0 || v >= FD_SETSIZE ) { |
| 101 | err_setstr(ValueError, "filedescriptor out of range in select()"); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 102 | return -1; |
| 103 | } |
| 104 | if ( v > max ) max = v; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 105 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 106 | FD_SET(v, set); |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 107 | /* add object and its file descriptor to the list */ |
| 108 | if ( index >= FD_SETSIZE ) { |
| 109 | err_setstr(ValueError, "too many file descriptors in select()"); |
| 110 | return -1; |
| 111 | } |
| 112 | fd2obj[index].obj = o; |
| 113 | fd2obj[index].fd = v; |
| 114 | fd2obj[++index].obj = (object *)0; /* sentinel */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 115 | } |
| 116 | return max+1; |
| 117 | } |
| 118 | |
| 119 | static object * |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 120 | set2list(set, fd2obj) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 121 | fd_set *set; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 122 | pylist fd2obj[FD_SETSIZE + 3]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 123 | { |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 124 | int j, num=0; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 125 | object *list, *o; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 126 | SOCKET fd; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 128 | for(j=0; fd2obj[j].obj; j++) |
| 129 | if ( FD_ISSET(fd2obj[j].fd, set) ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 130 | num++; |
| 131 | list = newlistobject(num); |
| 132 | num = 0; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 133 | for(j=0; fd2obj[j].obj; j++) { |
| 134 | fd = fd2obj[j].fd; |
| 135 | if ( FD_ISSET(fd, set) ) { |
| 136 | #ifndef _MSC_VER |
| 137 | if ( fd > FD_SETSIZE ) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 138 | err_setstr(SystemError, |
| 139 | "filedescriptor out of range returned in select()"); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 142 | #endif |
| 143 | o = fd2obj[j].obj; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 144 | INCREF(o); |
| 145 | setlistitem(list, num, o); |
| 146 | num++; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 147 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 148 | } |
| 149 | return list; |
| 150 | } |
| 151 | |
| 152 | static object * |
| 153 | select_select(self, args) |
| 154 | object *self; |
| 155 | object *args; |
| 156 | { |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 157 | pylist rfd2obj[FD_SETSIZE + 3], wfd2obj[FD_SETSIZE + 3], efd2obj[FD_SETSIZE + 3]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 158 | object *ifdlist, *ofdlist, *efdlist; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 159 | object *ret, *tout; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 160 | fd_set ifdset, ofdset, efdset; |
| 161 | double timeout; |
| 162 | struct timeval tv, *tvp; |
| 163 | int seconds; |
| 164 | int imax, omax, emax, max; |
| 165 | int n; |
| 166 | |
| 167 | |
| 168 | /* Get args. Looks funny because of optional timeout argument */ |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 169 | if ( getargs(args, "(OOOO)", &ifdlist, &ofdlist, &efdlist, &tout) ) { |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 170 | if (tout == None) |
| 171 | tvp = (struct timeval *)0; |
| 172 | else { |
Sjoerd Mullender | 78ed420 | 1993-11-02 15:34:23 +0000 | [diff] [blame] | 173 | if (!getargs(tout, "d;timeout must be float or None", &timeout)) |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 174 | return NULL; |
Sjoerd Mullender | 78ed420 | 1993-11-02 15:34:23 +0000 | [diff] [blame] | 175 | seconds = (int)timeout; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 176 | timeout = timeout - (double)seconds; |
| 177 | tv.tv_sec = seconds; |
| 178 | tv.tv_usec = (int)(timeout*1000000.0); |
| 179 | tvp = &tv; |
| 180 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 181 | } else { |
| 182 | /* Doesn't have 4 args, that means no timeout */ |
| 183 | err_clear(); |
| 184 | if (!getargs(args, "(OOO)", &ifdlist, &ofdlist, &efdlist) ) |
| 185 | return 0; |
| 186 | tvp = (struct timeval *)0; |
| 187 | } |
| 188 | if ( !is_listobject(ifdlist) || !is_listobject(ofdlist) || |
| 189 | !is_listobject(efdlist) ) { |
| 190 | err_badarg(); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 194 | /* Convert lists to fd_sets, and get maximum fd number */ |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 195 | if( (imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 196 | return 0; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 197 | if( (omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 198 | return 0; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 199 | if( (emax=list2set(efdlist, &efdset, efd2obj)) < 0 ) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 200 | return 0; |
| 201 | max = imax; |
| 202 | if ( omax > max ) max = omax; |
| 203 | if ( emax > max ) max = emax; |
| 204 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 205 | BGN_SAVE |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 206 | n = select(max, &ifdset, &ofdset, &efdset, tvp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 207 | END_SAVE |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 208 | |
| 209 | if ( n < 0 ) { |
| 210 | err_errno(SelectError); |
| 211 | return 0; |
| 212 | } |
| 213 | |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 214 | if ( n == 0 ) { /* Speedup hack */ |
| 215 | ifdlist = newlistobject(0); |
| 216 | ret = mkvalue("OOO", ifdlist, ifdlist, ifdlist); |
| 217 | XDECREF(ifdlist); |
| 218 | return ret; |
| 219 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 221 | ifdlist = set2list(&ifdset, rfd2obj); |
| 222 | ofdlist = set2list(&ofdset, wfd2obj); |
| 223 | efdlist = set2list(&efdset, efd2obj); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 224 | ret = mkvalue("OOO", ifdlist, ofdlist, efdlist); |
| 225 | XDECREF(ifdlist); |
| 226 | XDECREF(ofdlist); |
| 227 | XDECREF(efdlist); |
| 228 | return ret; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | static struct methodlist select_methods[] = { |
| 233 | { "select", select_select }, |
| 234 | { 0, 0 }, |
| 235 | }; |
| 236 | |
| 237 | |
| 238 | void |
| 239 | initselect() |
| 240 | { |
| 241 | object *m, *d; |
| 242 | m = initmodule("select", select_methods); |
| 243 | d = getmoduledict(m); |
| 244 | SelectError = newstringobject("select.error"); |
| 245 | if ( SelectError == NULL || dictinsert(d, "error", SelectError) ) |
| 246 | fatal("Cannot define select.error"); |
| 247 | } |