poll_using_select.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * poll_using_select.c
  14. *
  15. * poll_using_select() is an implementation of poll using select.
  16. * I borrowed this code from the client source, ns/nspr/src/mdunix.c
  17. * from the Nav404_OEM_BRANCH
  18. *
  19. * The reason we need this is on Linux 2.0 (and possibly other platforms)
  20. * there is no poll. ldapserver/ldap/servers/slapd/daemon.c calls poll
  21. *
  22. * In Linux 2.1 there will be a native poll, so this will go away for Linux
  23. * but other platforms may need it.
  24. *
  25. * 1-30-98
  26. * sspitzer
  27. */
  28. #include "poll_using_select.h"
  29. int
  30. poll_using_select(struct my_pollfd *filed, int nfds, int timeout)
  31. {
  32. int width;
  33. fd_set rd;
  34. fd_set wr;
  35. fd_set ex;
  36. struct timeval tv;
  37. int a, b, retval;
  38. #ifdef DEBUG_POLL_AS_SELECT
  39. LDAPDebug(LDAP_DEBUG_ANY, "poll convert nfds=%d, timeout=%d\n", nfds, timeout,0);
  40. #endif /* DEBUG_POLL_AS_SELECT */
  41. FD_ZERO( &rd );
  42. FD_ZERO( &wr );
  43. FD_ZERO( &ex );
  44. /*
  45. * If the fd member of all pollfd structures is less than 0,
  46. * the poll() function will return 0 and have no other results.
  47. */
  48. for(a=0, b=0; a<nfds; a++){
  49. if( filed[a].fd >= 0 ) b++;
  50. }
  51. if( b == 0 ){ return 0; }
  52. for( width=0,a=0; a<nfds; a++ ){
  53. short temp_events;
  54. #ifdef DEBUG_POLL_AS_SELECT
  55. if (filed[a].events != 0) {
  56. LDAPDebug(LDAP_DEBUG_ANY, "poll events = %d for fd=%d\n", filed[a].events, filed[a].fd,0);
  57. }
  58. #endif /* DEBUG_POLL_AS_SELECT */
  59. temp_events = filed[a].events;
  60. filed[a].revents = 0;
  61. if( temp_events & POLLIN ) FD_SET( filed[a].fd, &rd );
  62. if( temp_events & POLLOUT ) FD_SET( filed[a].fd, &wr );
  63. if( temp_events & POLLPRI ) FD_SET( filed[a].fd, &ex );
  64. temp_events &= ~(POLLIN|POLLOUT|POLLPRI);
  65. #ifdef DEBUG_POLL_AS_SELECT
  66. if( temp_events != 0 ){
  67. LDAPDebug(LDAP_DEBUG_ANY, "Unhandled poll event type=0x%x on FD(%d)\n",filed[a].events,filed[a].fd,0);
  68. }
  69. #endif /* DEBUG_POLL_AS_SELECT */
  70. width = width>(filed[a].fd+1)?width:(filed[a].fd+1);
  71. }
  72. if( timeout != -1 ){
  73. tv.tv_sec = timeout/MSECONDS;
  74. tv.tv_usec= timeout%MSECONDS;
  75. retval = select ( width, &rd, &wr, &ex, &tv );
  76. }
  77. else
  78. {
  79. retval = select ( width, &rd, &wr, &ex, 0 );
  80. }
  81. if( retval <= 0 ) return( retval );
  82. #ifdef DEBUG_POLL_AS_SELECT
  83. LDAPDebug(LDAP_DEBUG_ANY, "For\n",0,0,0);
  84. #endif /* DEBUG_POLL_AS_SELECT */
  85. for( b=0; b<nfds;b++){
  86. if( filed[b].fd >= 0 ){
  87. a = filed[b].fd;
  88. if( FD_ISSET( a, &rd )) filed[b].revents |= POLLIN;
  89. if( FD_ISSET( a, &wr )) filed[b].revents |= POLLOUT;
  90. if( FD_ISSET( a, &ex )) filed[b].revents |= POLLPRI;
  91. #ifdef DEBUG_POLL_AS_SELECT
  92. if ((filed[b].events != 0) || (filed[b].revents != 0)) {
  93. LDAPDebug(LDAP_DEBUG_ANY, " file des=%d, events=0x%x, revents=0x%x\n", filed[b].fd, filed[b].events ,filed[b].revents);
  94. }
  95. #endif /* DEBUG_POLL_AS_SELECT */
  96. }
  97. }
  98. for( a=0, retval=0 ; a<nfds; a++ ){
  99. if( filed[a].revents ) retval++;
  100. }
  101. #ifdef DEBUG_POLL_AS_SELECT
  102. LDAPDebug(LDAP_DEBUG_ANY, "poll returns %d (converted poll)\n",retval,0,0);
  103. #endif /* DEBUG_POLL_AS_SELECT */
  104. return( retval );
  105. }