poll_using_select.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * poll_using_select.c
  43. *
  44. * poll_using_select() is an implementation of poll using select.
  45. * I borrowed this code from the client source, ns/nspr/src/mdunix.c
  46. * from the Nav404_OEM_BRANCH
  47. *
  48. * The reason we need this is on Linux 2.0 (and possibly other platforms)
  49. * there is no poll. ldapserver/ldap/servers/slapd/daemon.c calls poll
  50. *
  51. * In Linux 2.1 there will be a native poll, so this will go away for Linux
  52. * but other platforms may need it.
  53. *
  54. * 1-30-98
  55. * sspitzer
  56. */
  57. #include "poll_using_select.h"
  58. int
  59. poll_using_select(struct my_pollfd *filed, int nfds, int timeout)
  60. {
  61. int width;
  62. fd_set rd;
  63. fd_set wr;
  64. fd_set ex;
  65. struct timeval tv;
  66. int a, b, retval;
  67. #ifdef DEBUG_POLL_AS_SELECT
  68. LDAPDebug(LDAP_DEBUG_ANY, "poll convert nfds=%d, timeout=%d\n", nfds, timeout,0);
  69. #endif /* DEBUG_POLL_AS_SELECT */
  70. FD_ZERO( &rd );
  71. FD_ZERO( &wr );
  72. FD_ZERO( &ex );
  73. /*
  74. * If the fd member of all pollfd structures is less than 0,
  75. * the poll() function will return 0 and have no other results.
  76. */
  77. for(a=0, b=0; a<nfds; a++){
  78. if( filed[a].fd >= 0 ) b++;
  79. }
  80. if( b == 0 ){ return 0; }
  81. for( width=0,a=0; a<nfds; a++ ){
  82. short temp_events;
  83. #ifdef DEBUG_POLL_AS_SELECT
  84. if (filed[a].events != 0) {
  85. LDAPDebug(LDAP_DEBUG_ANY, "poll events = %d for fd=%d\n", filed[a].events, filed[a].fd,0);
  86. }
  87. #endif /* DEBUG_POLL_AS_SELECT */
  88. temp_events = filed[a].events;
  89. filed[a].revents = 0;
  90. if( temp_events & POLLIN ) FD_SET( filed[a].fd, &rd );
  91. if( temp_events & POLLOUT ) FD_SET( filed[a].fd, &wr );
  92. if( temp_events & POLLPRI ) FD_SET( filed[a].fd, &ex );
  93. temp_events &= ~(POLLIN|POLLOUT|POLLPRI);
  94. #ifdef DEBUG_POLL_AS_SELECT
  95. if( temp_events != 0 ){
  96. LDAPDebug(LDAP_DEBUG_ANY, "Unhandled poll event type=0x%x on FD(%d)\n",filed[a].events,filed[a].fd,0);
  97. }
  98. #endif /* DEBUG_POLL_AS_SELECT */
  99. width = width>(filed[a].fd+1)?width:(filed[a].fd+1);
  100. }
  101. if( timeout != -1 ){
  102. tv.tv_sec = timeout/MSECONDS;
  103. tv.tv_usec= timeout%MSECONDS;
  104. retval = select ( width, &rd, &wr, &ex, &tv );
  105. }
  106. else
  107. {
  108. retval = select ( width, &rd, &wr, &ex, 0 );
  109. }
  110. if( retval <= 0 ) return( retval );
  111. #ifdef DEBUG_POLL_AS_SELECT
  112. LDAPDebug(LDAP_DEBUG_ANY, "For\n",0,0,0);
  113. #endif /* DEBUG_POLL_AS_SELECT */
  114. for( b=0; b<nfds;b++){
  115. if( filed[b].fd >= 0 ){
  116. a = filed[b].fd;
  117. if( FD_ISSET( a, &rd )) filed[b].revents |= POLLIN;
  118. if( FD_ISSET( a, &wr )) filed[b].revents |= POLLOUT;
  119. if( FD_ISSET( a, &ex )) filed[b].revents |= POLLPRI;
  120. #ifdef DEBUG_POLL_AS_SELECT
  121. if ((filed[b].events != 0) || (filed[b].revents != 0)) {
  122. LDAPDebug(LDAP_DEBUG_ANY, " file des=%d, events=0x%x, revents=0x%x\n", filed[b].fd, filed[b].events ,filed[b].revents);
  123. }
  124. #endif /* DEBUG_POLL_AS_SELECT */
  125. }
  126. }
  127. for( a=0, retval=0 ; a<nfds; a++ ){
  128. if( filed[a].revents ) retval++;
  129. }
  130. #ifdef DEBUG_POLL_AS_SELECT
  131. LDAPDebug(LDAP_DEBUG_ANY, "poll returns %d (converted poll)\n",retval,0,0);
  132. #endif /* DEBUG_POLL_AS_SELECT */
  133. return( retval );
  134. }