~ubuntu-branches/ubuntu/lucid/curl/lucid-201101212007

« back to all changes in this revision

Viewing changes to lib/select.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-02-08 11:20:41 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: james.westby@ubuntu.com-20080208112041-hed7sb5r6ghmjf8v
Tags: upstream-7.18.0
ImportĀ upstreamĀ versionĀ 7.18.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: select.c,v 1.48 2007-10-03 16:58:10 yangtse Exp $
 
21
 * $Id: select.c,v 1.49 2007-11-05 09:45:09 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
52
52
/* Winsock and TPF sockets are not in range [0..FD_SETSIZE-1] */
53
53
 
54
54
#if defined(USE_WINSOCK) || defined(TPF)
55
 
#define VERIFY_SOCK(x) do { } while (0)
 
55
#define VERIFY_SOCK(x) do { } while(0)
56
56
#else
57
57
#define VALID_SOCK(s) (((s) >= 0) && ((s) < FD_SETSIZE))
58
58
#define VERIFY_SOCK(x) do { \
105
105
#endif
106
106
  int r = 0;
107
107
 
108
 
  if (!timeout_ms)
 
108
  if(!timeout_ms)
109
109
    return 0;
110
 
  if (timeout_ms < 0) {
 
110
  if(timeout_ms < 0) {
111
111
    SET_SOCKERRNO(EINVAL);
112
112
    return -1;
113
113
  }
126
126
    pending_tv.tv_usec = (pending_ms % 1000) * 1000;
127
127
    r = select(0, NULL, NULL, NULL, &pending_tv);
128
128
#endif /* HAVE_POLL_FINE */
129
 
    if (r != -1)
 
129
    if(r != -1)
130
130
      break;
131
131
    error = SOCKERRNO;
132
 
    if ((error == EINVAL) || error_is_EINTR)
 
132
    if((error == EINVAL) || error_is_EINTR)
133
133
      break;
134
134
    pending_ms = timeout_ms - elapsed_ms;
135
 
    if (pending_ms <= 0)
 
135
    if(pending_ms <= 0)
136
136
      break;
137
 
  } while (r == -1);
 
137
  } while(r == -1);
138
138
#endif /* USE_WINSOCK */
139
 
  if (r)
 
139
  if(r)
140
140
    r = -1;
141
141
  return r;
142
142
}
189
189
     when function is called with a zero timeout or a negative timeout
190
190
     value indicating a blocking call should be performed. */
191
191
 
192
 
  if (timeout_ms > 0) {
 
192
  if(timeout_ms > 0) {
193
193
    pending_ms = timeout_ms;
194
194
    initial_tv = curlx_tvnow();
195
195
  }
197
197
#ifdef HAVE_POLL_FINE
198
198
 
199
199
  num = 0;
200
 
  if (readfd != CURL_SOCKET_BAD) {
 
200
  if(readfd != CURL_SOCKET_BAD) {
201
201
    pfd[num].fd = readfd;
202
202
    pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
203
203
    pfd[num].revents = 0;
204
204
    num++;
205
205
  }
206
 
  if (writefd != CURL_SOCKET_BAD) {
 
206
  if(writefd != CURL_SOCKET_BAD) {
207
207
    pfd[num].fd = writefd;
208
208
    pfd[num].events = POLLWRNORM|POLLOUT;
209
209
    pfd[num].revents = 0;
211
211
  }
212
212
 
213
213
  do {
214
 
    if (timeout_ms < 0)
 
214
    if(timeout_ms < 0)
215
215
      pending_ms = -1;
216
 
    else if (!timeout_ms)
 
216
    else if(!timeout_ms)
217
217
      pending_ms = 0;
218
218
    r = poll(pfd, num, pending_ms);
219
 
    if (r != -1)
 
219
    if(r != -1)
220
220
      break;
221
221
    error = SOCKERRNO;
222
 
    if ((error == EINVAL) || error_is_EINTR)
 
222
    if((error == EINVAL) || error_is_EINTR)
223
223
      break;
224
 
    if (timeout_ms > 0) {
 
224
    if(timeout_ms > 0) {
225
225
      pending_ms = timeout_ms - elapsed_ms;
226
 
      if (pending_ms <= 0)
 
226
      if(pending_ms <= 0)
227
227
        break;
228
228
    }
229
 
  } while (r == -1);
 
229
  } while(r == -1);
230
230
 
231
 
  if (r < 0)
 
231
  if(r < 0)
232
232
    return -1;
233
 
  if (r == 0)
 
233
  if(r == 0)
234
234
    return 0;
235
235
 
236
236
  ret = 0;
237
237
  num = 0;
238
 
  if (readfd != CURL_SOCKET_BAD) {
239
 
    if (pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
 
238
  if(readfd != CURL_SOCKET_BAD) {
 
239
    if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
240
240
      ret |= CURL_CSELECT_IN;
241
 
    if (pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
 
241
    if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
242
242
      ret |= CURL_CSELECT_ERR;
243
243
    num++;
244
244
  }
245
 
  if (writefd != CURL_SOCKET_BAD) {
246
 
    if (pfd[num].revents & (POLLWRNORM|POLLOUT))
 
245
  if(writefd != CURL_SOCKET_BAD) {
 
246
    if(pfd[num].revents & (POLLWRNORM|POLLOUT))
247
247
      ret |= CURL_CSELECT_OUT;
248
 
    if (pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
 
248
    if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
249
249
      ret |= CURL_CSELECT_ERR;
250
250
  }
251
251
 
257
257
  maxfd = (curl_socket_t)-1;
258
258
 
259
259
  FD_ZERO(&fds_read);
260
 
  if (readfd != CURL_SOCKET_BAD) {
 
260
  if(readfd != CURL_SOCKET_BAD) {
261
261
    VERIFY_SOCK(readfd);
262
262
    FD_SET(readfd, &fds_read);
263
263
    FD_SET(readfd, &fds_err);
265
265
  }
266
266
 
267
267
  FD_ZERO(&fds_write);
268
 
  if (writefd != CURL_SOCKET_BAD) {
 
268
  if(writefd != CURL_SOCKET_BAD) {
269
269
    VERIFY_SOCK(writefd);
270
270
    FD_SET(writefd, &fds_write);
271
271
    FD_SET(writefd, &fds_err);
272
 
    if (writefd > maxfd)
 
272
    if(writefd > maxfd)
273
273
      maxfd = writefd;
274
274
  }
275
275
 
276
276
  ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
277
277
 
278
278
  do {
279
 
    if (timeout_ms > 0) {
 
279
    if(timeout_ms > 0) {
280
280
      pending_tv.tv_sec = pending_ms / 1000;
281
281
      pending_tv.tv_usec = (pending_ms % 1000) * 1000;
282
282
    }
283
 
    else if (!timeout_ms) {
 
283
    else if(!timeout_ms) {
284
284
      pending_tv.tv_sec = 0;
285
285
      pending_tv.tv_usec = 0;
286
286
    }
287
287
    r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
288
 
    if (r != -1)
 
288
    if(r != -1)
289
289
      break;
290
290
    error = SOCKERRNO;
291
 
    if ((error == EINVAL) || (error == EBADF) || error_is_EINTR)
 
291
    if((error == EINVAL) || (error == EBADF) || error_is_EINTR)
292
292
      break;
293
 
    if (timeout_ms > 0) {
 
293
    if(timeout_ms > 0) {
294
294
      pending_ms = timeout_ms - elapsed_ms;
295
 
      if (pending_ms <= 0)
 
295
      if(pending_ms <= 0)
296
296
        break;
297
297
    }
298
 
  } while (r == -1);
 
298
  } while(r == -1);
299
299
 
300
 
  if (r < 0)
 
300
  if(r < 0)
301
301
    return -1;
302
 
  if (r == 0)
 
302
  if(r == 0)
303
303
    return 0;
304
304
 
305
305
  ret = 0;
306
 
  if (readfd != CURL_SOCKET_BAD) {
307
 
    if (FD_ISSET(readfd, &fds_read))
 
306
  if(readfd != CURL_SOCKET_BAD) {
 
307
    if(FD_ISSET(readfd, &fds_read))
308
308
      ret |= CURL_CSELECT_IN;
309
 
    if (FD_ISSET(readfd, &fds_err))
 
309
    if(FD_ISSET(readfd, &fds_err))
310
310
      ret |= CURL_CSELECT_ERR;
311
311
  }
312
 
  if (writefd != CURL_SOCKET_BAD) {
313
 
    if (FD_ISSET(writefd, &fds_write))
 
312
  if(writefd != CURL_SOCKET_BAD) {
 
313
    if(FD_ISSET(writefd, &fds_write))
314
314
      ret |= CURL_CSELECT_OUT;
315
 
    if (FD_ISSET(writefd, &fds_err))
 
315
    if(FD_ISSET(writefd, &fds_err))
316
316
      ret |= CURL_CSELECT_ERR;
317
317
  }
318
318
 
355
355
  int error;
356
356
  int r;
357
357
 
358
 
  if (ufds) {
 
358
  if(ufds) {
359
359
    for (i = 0; i < nfds; i++) {
360
 
      if (ufds[i].fd != CURL_SOCKET_BAD) {
 
360
      if(ufds[i].fd != CURL_SOCKET_BAD) {
361
361
        fds_none = FALSE;
362
362
        break;
363
363
      }
364
364
    }
365
365
  }
366
 
  if (fds_none) {
 
366
  if(fds_none) {
367
367
    r = wait_ms(timeout_ms);
368
368
    return r;
369
369
  }
373
373
     when function is called with a zero timeout or a negative timeout
374
374
     value indicating a blocking call should be performed. */
375
375
 
376
 
  if (timeout_ms > 0) {
 
376
  if(timeout_ms > 0) {
377
377
    pending_ms = timeout_ms;
378
378
    initial_tv = curlx_tvnow();
379
379
  }
381
381
#ifdef HAVE_POLL_FINE
382
382
 
383
383
  do {
384
 
    if (timeout_ms < 0)
 
384
    if(timeout_ms < 0)
385
385
      pending_ms = -1;
386
 
    else if (!timeout_ms)
 
386
    else if(!timeout_ms)
387
387
      pending_ms = 0;
388
388
    r = poll(ufds, nfds, pending_ms);
389
 
    if (r != -1)
 
389
    if(r != -1)
390
390
      break;
391
391
    error = SOCKERRNO;
392
 
    if ((error == EINVAL) || error_is_EINTR)
 
392
    if((error == EINVAL) || error_is_EINTR)
393
393
      break;
394
 
    if (timeout_ms > 0) {
 
394
    if(timeout_ms > 0) {
395
395
      pending_ms = timeout_ms - elapsed_ms;
396
 
      if (pending_ms <= 0)
 
396
      if(pending_ms <= 0)
397
397
        break;
398
398
    }
399
 
  } while (r == -1);
 
399
  } while(r == -1);
400
400
 
401
401
#else  /* HAVE_POLL_FINE */
402
402
 
407
407
 
408
408
  for (i = 0; i < nfds; i++) {
409
409
    ufds[i].revents = 0;
410
 
    if (ufds[i].fd == CURL_SOCKET_BAD)
 
410
    if(ufds[i].fd == CURL_SOCKET_BAD)
411
411
      continue;
412
412
    VERIFY_SOCK(ufds[i].fd);
413
 
    if (ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
 
413
    if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
414
414
                          POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
415
 
      if (ufds[i].fd > maxfd)
 
415
      if(ufds[i].fd > maxfd)
416
416
        maxfd = ufds[i].fd;
417
 
      if (ufds[i].events & (POLLRDNORM|POLLIN))
 
417
      if(ufds[i].events & (POLLRDNORM|POLLIN))
418
418
        FD_SET(ufds[i].fd, &fds_read);
419
 
      if (ufds[i].events & (POLLWRNORM|POLLOUT))
 
419
      if(ufds[i].events & (POLLWRNORM|POLLOUT))
420
420
        FD_SET(ufds[i].fd, &fds_write);
421
 
      if (ufds[i].events & (POLLRDBAND|POLLPRI))
 
421
      if(ufds[i].events & (POLLRDBAND|POLLPRI))
422
422
        FD_SET(ufds[i].fd, &fds_err);
423
423
    }
424
424
  }
426
426
  ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
427
427
 
428
428
  do {
429
 
    if (timeout_ms > 0) {
 
429
    if(timeout_ms > 0) {
430
430
      pending_tv.tv_sec = pending_ms / 1000;
431
431
      pending_tv.tv_usec = (pending_ms % 1000) * 1000;
432
432
    }
433
 
    else if (!timeout_ms) {
 
433
    else if(!timeout_ms) {
434
434
      pending_tv.tv_sec = 0;
435
435
      pending_tv.tv_usec = 0;
436
436
    }
437
437
    r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
438
 
    if (r != -1)
 
438
    if(r != -1)
439
439
      break;
440
440
    error = SOCKERRNO;
441
 
    if ((error == EINVAL) || (error == EBADF) || error_is_EINTR)
 
441
    if((error == EINVAL) || (error == EBADF) || error_is_EINTR)
442
442
      break;
443
 
    if (timeout_ms > 0) {
 
443
    if(timeout_ms > 0) {
444
444
      pending_ms = timeout_ms - elapsed_ms;
445
 
      if (pending_ms <= 0)
 
445
      if(pending_ms <= 0)
446
446
        break;
447
447
    }
448
 
  } while (r == -1);
 
448
  } while(r == -1);
449
449
 
450
 
  if (r < 0)
 
450
  if(r < 0)
451
451
    return -1;
452
 
  if (r == 0)
 
452
  if(r == 0)
453
453
    return 0;
454
454
 
455
455
  r = 0;
456
456
  for (i = 0; i < nfds; i++) {
457
457
    ufds[i].revents = 0;
458
 
    if (ufds[i].fd == CURL_SOCKET_BAD)
 
458
    if(ufds[i].fd == CURL_SOCKET_BAD)
459
459
      continue;
460
 
    if (FD_ISSET(ufds[i].fd, &fds_read))
 
460
    if(FD_ISSET(ufds[i].fd, &fds_read))
461
461
      ufds[i].revents |= POLLIN;
462
 
    if (FD_ISSET(ufds[i].fd, &fds_write))
 
462
    if(FD_ISSET(ufds[i].fd, &fds_write))
463
463
      ufds[i].revents |= POLLOUT;
464
 
    if (FD_ISSET(ufds[i].fd, &fds_err))
 
464
    if(FD_ISSET(ufds[i].fd, &fds_err))
465
465
      ufds[i].revents |= POLLPRI;
466
 
    if (ufds[i].revents != 0)
 
466
    if(ufds[i].revents != 0)
467
467
      r++;
468
468
  }
469
469