title: select and poll not work correctly
body: <p>I writing a auto stream multiplexer in java which, must read vaious amount of FileDescriptors. To read the file descriptors (or socket etc. which have FileDescriptor), I need a precise soultion, if I have some descriptors and there is no event don't iterate trouth all. And here is som method in linux: select,poll. I tried to use this methods but without success. the native codes is:</p>
  
  <pre><code>JNIEXPORT jint JNICALL Java_hu_ddsi_java_Native_JUnix_select(JNIEnv * env,jobject obj,jobjectArray fds,long timeout_s,long timeout_us)
  {
  
      int len = env-&gt;GetArrayLength(fds);
      jclass cls = env-&gt;FindClass("java/io/FileDescriptor");
      jfieldID fid = env-&gt;GetFieldID(cls, "fd", "I");
      fd_set watch;
      int buf = 0;
      int max = 0;
  
      for(int i=0;i &lt;  len;i++)
      {
          FD_SET( buf=(int) env-&gt;GetIntField(env-&gt;GetObjectArrayElement(fds, i),fid),&amp;watch);
          if(buf&gt;max)
              max = buf;
      }
  
      struct timeval *timeout = (timeval*) malloc(sizeof(struct timeval));
      timeout-&gt;tv_sec = timeout_s;
      timeout-&gt;tv_usec = timeout_us;
  
      return select(max+1, &amp;watch, NULL,NULL,timeout);
  }
  
  JNIEXPORT jint JNICALL Java_hu_ddsi_java_Native_JUnix_poll(JNIEnv * env,jobject obj,jlongArray pollfds,int timeout_ms)
  {
      unsigned int nfds = env-&gt;GetArrayLength(pollfds);
  
      void* pointerfor = malloc(nfds*(sizeof(void*)));
  
      for(int i=0;i &lt;  nfds;i++)
          env-&gt;GetLongArrayRegion(pollfds,i,1,(jlong*) pointerfor+(sizeof(void*)*i));
  
      return poll( ((struct pollfd*)pointerfor), nfds,timeout_ms);
  }
  
  JNIEXPORT jlong JNICALL Java_hu_ddsi_java_Native_JUnix_pollfd(JNIEnv * env,jobject obj,jobject fd,jshort events,jshort revents)
  {
      jclass cls = env-&gt;FindClass("java/io/FileDescriptor");
      jfieldID fid = env-&gt;GetFieldID(cls, "fd", "I");
  
      struct pollfd *pfd = (pollfd*) malloc(sizeof( pollfd));
          pfd-&gt;fd = env-&gt;GetIntField(fd,fid);
          pfd-&gt;events = events;
          pfd-&gt;revents = 0x0;
  
      return (jlong) pfd; 
  }
  
  JNIEXPORT jint JNICALL Java_hu_ddsi_java_Native_JUnix_pollfdfd(JNIEnv * env,jobject obj,jlong pfd_struct)
  {
      return ((struct pollfd*)pfd_struct)-&gt;fd;
  }
  
  JNIEXPORT jshort JNICALL Java_hu_ddsi_java_Native_JUnix_pollfdevents(JNIEnv * env,jobject obj,jlong pfd_struct)
  {
      return ((struct pollfd*)pfd_struct)-&gt;events;
  }
  
  JNIEXPORT jshort JNICALL Java_hu_ddsi_java_Native_JUnix_pollfdrevents(JNIEnv * env,jobject obj,jlong pfd_struct)
  {
      return ((struct pollfd*)pfd_struct)-&gt;revents;
  }
  </code></pre>
  
  <p>And a test code in java:</p>
  
  <pre><code>    public static void main(String args[]) throws Throwable
  
          final FileInputStream is = new FileInputStream("/var/log/apache2/access.log");
          long st = JUnix.pollfd(is.getFD(),(short)( JUnix.POLLIN|JUnix.POLLPRI ));
          int len = 0;
          while(true)
          {
  
              System.out.println("ava:"+(len = is.available()));
  
              for(int i=0;i &lt; len;i++)
                  System.out.print((char) is.read());
  
  //          JUnix.select(new FileDescriptor[]{is.getFD()},5,0);
  
              System.out.println("pollretval: "+JUnix.poll(new long[]{st},-1));
              System.out.println("revent: "+JUnix.pollfdrevents(st));
              System.out.println("pollfd: "+JUnix.pollfdfd(st));
          }
      }
  </code></pre>
  
  <p>And when I browse, I should see new lines in the terminal, but it will be blocked forever...
  if I modify the timeout I se the now lines in my terminal.</p>
  
  <p>Sometimes the code getting crazy and print infintly:</p>
  
  <pre><code>ava:0
  pollretval: 1
  revent: 0
  pollfd: 10
  </code></pre>
  
  <p>this result is interesting, fileDescriptor is same, native poll returned with the modified fd numbers, but in the pollfd stuct the revent field is... it should modified if event occurred.
   I tested, pointers is are in good location (as result shows), and a simple C code is do same result (i not finded InputStream.available like method in C for FD so I don't see how many byte are availble in stream, but its wait forever)</p>
  
  <p>What do I wrong?</p>