예제 #1
0
  /** @inheritDoc */
  public int writeBuf(int fd, byte buffer[], int off, int len) throws IOException {
    int result = 0;
    byte[] buf = buffer;
    if (off != 0) {
      buf = new byte[len];
      System.arraycopy(buffer, off, buf, 0, len);
    }

    if (DEBUG) {
      System.err.println("writeBuf(" + fd + ") before write.");
    }

    result = libc.write(fd, buf, len); // We rely on open0() for setting the socket to non-blocking

    if (result < 0) {
      int err_code = LibCUtil.errno();
      if (err_code == LibC.EWOULDBLOCK) {
        VMThread.getSystemEvents().waitForWriteEvent(fd);
        if (DEBUG) {
          System.err.println("writeBuf(" + fd + ") returned from select. retry.");
        }
        result =
            libc.write(fd, buf, len); // We rely on open0() for setting the socket to non-blocking
      }
      if (DEBUG) {
        System.err.println("writeBuf(" + fd + ") error:");
      }
      LibCUtil.errCheckNeg(result);
    }

    return result;
  }
예제 #2
0
  /** @inheritDoc */
  public int readBuf(int fd, byte b[], int offset, int length) throws IOException {
    byte[] buf = b;
    if (offset != 0) {
      buf = new byte[length];
    }
    int result =
        libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking

    if (result < 0) {
      int err_code = LibCUtil.errno();
      if (err_code == LibC.EWOULDBLOCK) {
        VMThread.getSystemEvents().waitForReadEvent(fd);
        result =
            libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking
      }
      LibCUtil.errCheckNeg(result);
    }

    if (result == 0) {
      // If remote side has shut down the connection gracefully, and all
      // data has been received, recv() will complete immediately with
      // zero bytes received.
      //
      // This is true for Win32/CE and Linux
      result = -1;
    }

    return result;
  }
예제 #3
0
 @Test
 public void testEnvironment() {
   String testProperty = "test_property";
   String testValue = "value";
   Assert.assertEquals(LibC.getenv(testProperty), null);
   Assert.assertEquals(LibC.setenv(testProperty, testValue, 1), 0);
   Assert.assertEquals(LibC.getenv(testProperty), testValue);
   Assert.assertEquals(LibC.unsetenv(testProperty), 0);
   Assert.assertEquals(LibC.getenv(testProperty), null);
 }
예제 #4
0
 /** @inheritDoc */
 public void close(int fd) throws IOException {
   // NOTE: this would block the VM. A real implementation should
   // make this a async native method.
   sockets.shutdown(fd, 2);
   libc.close(fd);
   if (DEBUG) {
     System.out.println("close(" + fd + ")");
   }
 }
예제 #5
0
  private void set_blocking_flags(int fd, boolean is_blocking) throws IOException {
    if (tryFcntl) {
      int flags = libc.fcntl(fd, LibC.F_GETFL, 0);
      if (DEBUG) {
        System.out.println("set_blocking_flags: fcntl F_GETFL = " + flags);
      }

      if (flags >= 0) {
        if (is_blocking == true) {
          flags &= ~LibC.O_NONBLOCK;
        } else {
          flags |= LibC.O_NONBLOCK;
        }
        if (DEBUG) {
          System.out.println("set_blocking_flags: calling fcntl F_SETFL flags: " + flags);
        }
        int res = libc.fcntl(fd, LibC.F_SETFL, flags);
        if (res != -1) {
          return;
        }
      } else if (LibCUtil.errno() == LibC.EOPNOTSUPP) {
        tryFcntl = false; // once this fails, don't try again
      }
    }

    if (!tryFcntl) {
      if (DEBUG) {
        System.out.println("set_blocking_flags: calling ioctl FIONBIO = " + !is_blocking);
      }
      IntByReference setting =
          new IntByReference(is_blocking ? 0 : 1); /* if "is_blocking"==true, NBIO = FALSE */
      int res = ioctl.ioctl(fd, Ioctl.FIONBIO, setting);
      setting.free();
      if (DEBUG) {
        System.out.println("set_blocking_flags: ioctl returned: " + res);
      }
      if (res >= 0) {
        return;
      }
    }
    throw newError(fd, "set_blocking_flags");
  }
예제 #6
0
  private void set_blocking_flags(int fd, boolean is_blocking) throws IOException {
    int flags = libc.fcntl(fd, LibC.F_GETFL, 0);
    if (DEBUG) {
      System.out.println("set_blocking_flags: fcntl F_GETFL = " + flags);
    }

    if (flags >= 0) {
      if (is_blocking == true) {
        flags &= ~LibC.O_NONBLOCK;
      } else {
        flags |= LibC.O_NONBLOCK;
      }
      if (DEBUG) {
        System.out.println("set_blocking_flags: calling fcntl F_SETFL flags: " + flags);
      }
      int res = libc.fcntl(fd, LibC.F_SETFL, flags);
      if (res != -1) {
        return;
      }
    }
    throw newError(fd, "set_blocking_flags");
  }
예제 #7
0
  @Test
  public void testDifftime() throws Exception {
    // Pointer to hold the times
    NativeLongByReference ref = new NativeLongByReference();

    // time() returns -1 on error.
    NativeLong err = new NativeLong(-1L);

    LibC.time(ref);
    NativeLong time0 = ref.getValue();
    Assert.assertNotSame(time0, err, "Time 0 returned an error (-1).");

    Thread.sleep(5000L);

    LibC.time(ref);
    NativeLong time1 = ref.getValue();
    Assert.assertNotSame(time1, err, "Time 1 returned an error (-1).");

    Assert.assertNotSame(time1, time0, "Time 1 returned same time as Time 0.");

    double diff = LibC.difftime(time1, time0);
    Assert.assertTrue(diff >= 5, "Time difference was not greater than 5 seconds: " + diff);
  }
예제 #8
0
 /** Read errno, try to clean up fd, and create exception. */
 private IOException newError(int fd, String msg) {
   if (DEBUG) {
     VM.print(msg);
     VM.print(": errno: ");
   }
   int err_code = LibCUtil.errno();
   if (DEBUG) {
     VM.print(err_code);
     VM.println();
   }
   sockets.shutdown(fd, 2);
   libc.close(fd);
   return new IOException(" errno: " + err_code + " on fd: " + fd + " during " + msg);
 }
예제 #9
0
  public boolean isTerminal(FileDescriptor fileDescriptor) {
    int osFileDesc;
    try {
      Field fdField = FileDescriptor.class.getDeclaredField("fd");
      fdField.setAccessible(true);
      osFileDesc = fdField.getInt(fileDescriptor);
    } catch (Exception e) {
      throw UncheckedException.asUncheckedException(e);
    }

    // Determine if we're connected to a terminal
    if (libC.isatty(osFileDesc) == 0) {
      return false;
    }

    // Dumb terminal doesn't support ANSI control codes. Should really be using termcap database.
    String term = System.getenv("TERM");
    if (term != null && term.equals("dumb")) {
      return false;
    }

    // Assume a terminal
    return true;
  }
예제 #10
0
  /** @inheritDoc */
  public int readBuf(int fd, byte b[], int offset, int length) throws IOException {
    byte[] buf = b;
    int result;

    if (offset != 0) {
      if (DEBUG) {
        System.err.println("readBuf() into temp buf");
      }
      buf = new byte[length];
    }

    if (NBIO_WORKS) {
      result =
          libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking
      if (result < 0) {
        int err_code = LibCUtil.errno();
        if (err_code == LibC.EWOULDBLOCK) {
          if (DEBUG) {
            System.err.println("Wait for read in select...");
          }
          VMThread.getSystemEvents().waitForReadEvent(fd);
          result =
              libc.read(
                  fd, buf, length); // We rely on open0() for setting the socket to non-blocking
        }
        LibCUtil.errCheckNeg(result);
      }
    } else {
      // If non-blocking IO doesn't seems to be working, try this hack...

      int bAvail = available(fd); // may throw IOException

      if (bAvail == 0) {
        if (DEBUG) {
          System.err.println("Wait for read in select...");
        }
        VMThread.getSystemEvents().waitForReadEvent(fd);
        bAvail = available(fd);
        if (bAvail == 0) { // woke up because connection is closed
          if (DEBUG) {
            System.err.println("readBuf(" + fd + ") signalling EOF.");
          }
          return -1; // signal EOF
        }
      }
      if (DEBUG) {
        System.err.println("readBuf(" + fd + ") returned from select. retry.");
      }

      int n = Math.min(bAvail, length); // don't read more than is asked for...
      result = libc.read(fd, buf, n); // only read what we know is there...
      LibCUtil.errCheckNeg(result);
    }

    if (result == 0) {
      // If remote side has shut down the connection gracefully, and all
      // data has been received, recv() will complete immediately with
      // zero bytes received.
      //
      // This is true for Win32/CE and Linux
      if (DEBUG) {
        System.err.println("readBuf(" + fd + ") saw remote side shutdown.");
      }
      result = -1;
    }

    if (offset != 0 && result > 0) {
      System.arraycopy(buf, 0, b, offset, result);
    }
    if (DEBUG) {
      System.out.println("readBuf(" + fd + ") = " + result);
    }

    return result;
  }