com.sun.jna.LastErrorException Java Examples

The following examples show how to use com.sun.jna.LastErrorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * @see NativeCalls#killProcess(int)
 */
@Override
public boolean killProcess(final int processId) {
  try {
    final Pointer procHandle = Kernel32.OpenProcess(
        Kernel32.PROCESS_TERMINATE, false, processId);
    final long hval;
    if (procHandle == null || (hval = Pointer.nativeValue(procHandle)) ==
        Kernel32.INVALID_HANDLE || hval == 0) {
      return false;
    }
    else {
      final boolean result = Kernel32.TerminateProcess(procHandle, -1);
      Kernel32.CloseHandle(procHandle);
      return result;
    }
  } catch (LastErrorException le) {
    // some problem in killing the process
    return false;
  }
}
 
Example #2
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * @see NativeCalls#isProcessActive(int)
 */
@Override
public boolean isProcessActive(final int processId) {
  try {
    final Pointer procHandle = Kernel32.OpenProcess(
        Kernel32.PROCESS_QUERY_INFORMATION, false, processId);
    final long hval;
    if (procHandle == null || (hval = Pointer.nativeValue(procHandle)) ==
        Kernel32.INVALID_HANDLE || hval == 0) {
      return false;
    }
    else {
      final IntByReference status = new IntByReference();
      final boolean result = Kernel32.GetExitCodeProcess(procHandle, status)
          && status != null && status.getValue() == Kernel32.STILL_ACTIVE;
      Kernel32.CloseHandle(procHandle);
      return result;
    }
  } catch (LastErrorException le) {
    // some problem in getting process status
    return false;
  }
}
 
Example #3
Source File: OwnedDisplay.java    From ev3dev-lang-java with MIT License 6 votes vote down vote up
/**
 * <p>Switch the display to a graphics mode.</p>
 *
 * <p>It switches VT to graphics mode with keyboard turned off.
 * Then, it tells kernel to notify Java when VT switch occurs.
 * Also, framebuffer contents are restored and write access is enabled.</p>
 *
 * @throws RuntimeException when the switch fails
 */
public void switchToGraphicsMode() {
    LOGGER.trace("Switching console to graphics mode");
    try {
        ttyfd.setKeyboardMode(K_OFF);
        ttyfd.setConsoleMode(KD_GRAPHICS);

        NativeTTY.vt_mode vtm = new NativeTTY.vt_mode();
        vtm.mode = VT_PROCESS;
        vtm.relsig = SIGUSR2;
        vtm.acqsig = SIGUSR2;
        ttyfd.setVTmode(vtm);
    } catch (LastErrorException e) {
        throw new RuntimeException("Switch to graphics mode failed", e);
    }

    if (fbInstance != null) {
        fbInstance.restoreData();
        fbInstance.setFlushEnabled(true);
    }
}
 
Example #4
Source File: EmulatedFramebuffer.java    From ev3dev-lang-java with MIT License 5 votes vote down vote up
/**
 * Invoke i/o control action with pointer argument.
 *
 * @param fd  Not used.
 * @param cmd IO control command.
 * @param arg IO control argument ({@link fb_con2fbmap}, {@link fb_fix_screeninfo}, {@link fb_var_screeninfo})
 * @return Zero.
 * @throws LastErrorException EINVAL when the argument or command is invalid.
 */
@Override
public int ioctl(int fd, int cmd, Pointer arg) throws LastErrorException {
    fb_con2fbmap map;
    byte[] buf;
    fb_var_screeninfo vInfo;
    switch (cmd) {
        case NativeConstants.FBIOGET_CON2FBMAP:
            map = new fb_con2fbmap(arg);
            if (!con2fb.containsKey(map.console)) {
                throw new LastErrorException(NativeConstants.EINVAL);
            }
            map.framebuffer = con2fb.get(map.console);
            map.write();
            break;
        case NativeConstants.FBIOGET_FSCREENINFO:
            buf = fixinfo.getPointer().getByteArray(0, fixinfo.size());
            arg.write(0, buf, 0, buf.length);
            break;
        case NativeConstants.FBIOGET_VSCREENINFO:
            buf = varinfo.getPointer().getByteArray(0, varinfo.size());
            arg.write(0, buf, 0, buf.length);
            break;
        case NativeConstants.FBIOPUT_VSCREENINFO:
            vInfo = new fb_var_screeninfo(arg);
            takeVarInfo(vInfo);
            break;
        default:
            throw new LastErrorException(NativeConstants.EINVAL);
    }
    return 0;
}
 
Example #5
Source File: HostnameFetching.java    From buck with Apache License 2.0 5 votes vote down vote up
private static String getHostnamePosix() throws IOException {
  byte[] hostnameBuf = new byte[HOSTNAME_MAX_LEN];
  try {
    // Subtract 1 to ensure NUL termination.
    HostnameFetchingPosixLibrary.gethostname(hostnameBuf, hostnameBuf.length - 1);
    return Native.toString(hostnameBuf);
  } catch (LastErrorException e) {
    throw new IOException(e);
  }
}
 
Example #6
Source File: LibCBackedProcessEnvironment.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setNativeEnvironmentVariable(String name, String value) {
    try {
        libc.setenv(name, value, 1);
    } catch (LastErrorException lastErrorException) {
        throw new NativeIntegrationException(String.format("Could not set environment variable '%s'. errno: %d", name, lastErrorException.getErrorCode()));
    }
}
 
Example #7
Source File: LibcChmod.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void chmod(File f, int mode) throws IOException {
    try {
        byte[] encodedFilePath = encoder.encode(f);
        libc.chmod(encodedFilePath, mode);
    } catch (LastErrorException exception) {
        throw new IOException(String.format("Failed to set file permissions %s on file %s. errno: %d", mode, f.getName(), exception.getErrorCode()));
    }
}
 
Example #8
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * @see NativeCalls#killProcess(int)
 */
@Override
public boolean killProcess(final int processId) {
  try {
    return kill(processId, 9) == 0;
  } catch (LastErrorException le) {
    return false;
  }
}
 
Example #9
Source File: LibCBackedProcessEnvironment.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setNativeEnvironmentVariable(String name, String value) {
    try {
        libc.setenv(name, value, 1);
    } catch (LastErrorException lastErrorException) {
        throw new NativeIntegrationException(String.format("Could not set environment variable '%s'. errno: %d", name, lastErrorException.getErrorCode()));
    }
}
 
Example #10
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected int setPlatformSocketOption(int sockfd, int level, int optName,
    TCPSocketOptions opt, Integer optVal, int optSize)
    throws NativeErrorException {
  try {
    return setsockopt(sockfd, level, optName,
        new IntByReference(optVal.intValue()), optSize);
  } catch (LastErrorException le) {
    throw new NativeErrorException(le.getMessage(), le.getErrorCode(),
        le.getCause());
  }
}
 
Example #11
Source File: NativeFramebuffer.java    From ev3dev-lang-java with MIT License 5 votes vote down vote up
/**
 * Fetch fixed screen info.
 *
 * @return Non-changing info about the display.
 */
public fb_fix_screeninfo getFixedScreenInfo() throws LastErrorException {
    fb_fix_screeninfo info = new fb_fix_screeninfo();
    super.ioctl(FBIOGET_FSCREENINFO, info.getPointer());
    info.read();
    return info;
}
 
Example #12
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * @see NativeCalls#setEnvironment(String, String)
 */
@Override
public synchronized void setEnvironment(final String name,
    final String value) {
  if (name == null) {
    throw new UnsupportedOperationException(
        "setEnvironment() for name=NULL");
  }
  int res = -1;
  Throwable cause = null;
  try {
    if (value != null) {
      res = setenv(name, value, 1);
    }
    else {
      res = unsetenv(name);
    }
  } catch (LastErrorException le) {
    cause = new NativeErrorException(le.getMessage(), le.getErrorCode(),
        le.getCause());
  }
  if (res != 0) {
    throw new IllegalArgumentException("setEnvironment: given name=" + name
        + " (value=" + value + ')', cause);
  }
  // also change in java cached map
  if (javaEnv != null) {
    if (value != null) {
      javaEnv.put(name, value);
    }
    else {
      javaEnv.remove(name);
    }
  }
}
 
Example #13
Source File: UnixDomainSocket.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws IOException {
    if (!closeLock.getAndSet(true)) {
        try {
            close(fd);
        } catch (LastErrorException lee) {
            throw new IOException("native close() failed : " + formatError(lee));
        }
        connected = false;
    }
}
 
Example #14
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected int setPlatformSocketOption(int sockfd, int level, int optName,
    TCPSocketOptions opt, Integer optVal, int optSize)
    throws NativeErrorException {
  try {
    switch (optName) {
      case OPT_TCP_KEEPALIVE_THRESHOLD:
        // value required is in millis
        final IntByReference timeout = new IntByReference(
            optVal.intValue() * 1000);
        int result = setsockopt(sockfd, level, optName, timeout, optSize);
        if (result == 0) {
          // setting ABORT_THRESHOLD to be same as KEEPALIVE_THRESHOLD
          return setsockopt(sockfd, level,
              OPT_TCP_KEEPALIVE_ABORT_THRESHOLD, timeout, optSize);
        }
        else {
          return result;
        }
      default:
        throw new UnsupportedOperationException("unsupported option " + opt);
    }
  } catch (LastErrorException le) {
    throw new NativeErrorException(le.getMessage(), le.getErrorCode(),
        le.getCause());
  }
}
 
Example #15
Source File: UnixDomainSocket.java    From buck with Apache License 2.0 5 votes vote down vote up
private int doRead(ByteBuffer buf) throws IOException {
  try {
    int fdToRead = fd.acquire();
    if (fdToRead == -1) {
      return -1;
    }
    return UnixDomainSocketLibrary.read(fdToRead, buf, buf.remaining());
  } catch (LastErrorException e) {
    throw new IOException(e);
  } finally {
    fd.release();
  }
}
 
Example #16
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * @see NativeCalls#setEnvironment(String, String)
 */
@Override
public synchronized void setEnvironment(final String name,
    final String value) {
  if (name == null) {
    throw new UnsupportedOperationException(
        "setEnvironment() for name=NULL");
  }
  int res = -1;
  Throwable cause = null;
  try {
    if (value != null) {
      res = setenv(name, value, 1);
    }
    else {
      res = unsetenv(name);
    }
  } catch (LastErrorException le) {
    cause = new NativeErrorException(le.getMessage(), le.getErrorCode(),
        le.getCause());
  }
  if (res != 0) {
    throw new IllegalArgumentException("setEnvironment: given name=" + name
        + " (value=" + value + ')', cause);
  }
  // also change in java cached map
  if (javaEnv != null) {
    if (value != null) {
      javaEnv.put(name, value);
    }
    else {
      javaEnv.remove(name);
    }
  }
}
 
Example #17
Source File: LibcChmod.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void chmod(File f, int mode) throws IOException {
    try {
        byte[] encodedFilePath = encoder.encode(f);
        libc.chmod(encodedFilePath, mode);
    } catch (LastErrorException exception) {
        throw new IOException(String.format("Failed to set file permissions %s on file %s. errno: %d", mode, f.getName(), exception.getErrorCode()));
    }
}
 
Example #18
Source File: UnixDomainSocket.java    From docker-java with Apache License 2.0 5 votes vote down vote up
UnixDomainSocket(String path) throws IOException {
    if (Platform.isWindows() || Platform.isWindowsCE()) {
        throw new IOException("Unix domain sockets are not supported on Windows");
    }
    sockaddr = new SockAddr(path);
    closeLock.set(false);
    try {
        fd = socket(AF_UNIX, SOCK_STREAM, PROTOCOL);
    } catch (LastErrorException lee) {
        throw new IOException("native socket() failed : " + formatError(lee));
    }
}
 
Example #19
Source File: UnixDomainSocket.java    From mariadb-connector-j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public UnixDomainSocket(String path) throws IOException {
  if (Platform.isWindows() || Platform.isWindowsCE()) {
    throw new IOException("Unix domain sockets are not supported on Windows");
  }
  sockaddr = new SockAddr(path);
  closeLock.set(false);
  try {
    fd = socket(AF_UNIX, SOCK_STREAM, PROTOCOL);
  } catch (LastErrorException lee) {
    throw new IOException("native socket() failed : " + formatError(lee));
  }
}
 
Example #20
Source File: NativeFile.java    From ev3dev-lang-java with MIT License 5 votes vote down vote up
/**
 * Close the associated file
 *
 * @throws LastErrorException when operations fails
 */
@Override
public void close() throws LastErrorException {
    if (fd != -1) {
        int copy = fd;
        fd = -1;
        libc.close(copy);
    }
}
 
Example #21
Source File: NativeTTY.java    From ev3dev-lang-java with MIT License 5 votes vote down vote up
/**
 * Get current TTY state.
 *
 * @return TTY state.
 * @throws LastErrorException when the operation fails.
 */
public vt_stat getVTstate() throws LastErrorException {
    vt_stat stat = new vt_stat();
    super.ioctl(VT_GETSTATE, stat.getPointer());
    stat.read();
    return stat;
}
 
Example #22
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected void fallocateFD(int fd, long offset, long len)
    throws LastErrorException {
  int errno = posix_fallocate64(fd, offset, len);
  if (errno != 0) {
    throw new LastErrorException(errno);
  }
}
 
Example #23
Source File: UnixDomainSocket.java    From docker-java with Apache License 2.0 5 votes vote down vote up
UnixDomainSocket(String path) throws IOException {
    if (Platform.isWindows() || Platform.isWindowsCE()) {
        throw new IOException("Unix domain sockets are not supported on Windows");
    }
    sockaddr = new SockAddr(path);
    closeLock.set(false);
    try {
        fd = socket(AF_UNIX, SOCK_STREAM, PROTOCOL);
    } catch (LastErrorException lee) {
        throw new IOException("native socket() failed : " + formatError(lee));
    }
}
 
Example #24
Source File: EmulatedLibc.java    From ev3dev-lang-java with MIT License 5 votes vote down vote up
@Override
public int close(int fd) throws LastErrorException {
    try {
        return impl(path(fd)).close(fd);
    } finally {
        opened.remove(fd);
    }
}
 
Example #25
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected void fallocateFD(int fd, long offset, long len)
    throws LastErrorException {
  int errno = posix_fallocate64(fd, offset, len);
  if (errno != 0) {
    throw new LastErrorException(errno);
  }
}
 
Example #26
Source File: CountingFile.java    From ev3dev-lang-java with MIT License 4 votes vote down vote up
@Override
public int ioctl(int fd, int cmd, @NonNull Pointer arg) throws LastErrorException {
    countIoctl_ptr++;
    return sub.ioctl(fd, cmd, arg);
}
 
Example #27
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
protected void fallocateFD(int fd, long offset, long len)
    throws LastErrorException {
  throw new UnsupportedOperationException("not expected to be invoked");
}
 
Example #28
Source File: CountingFile.java    From ev3dev-lang-java with MIT License 4 votes vote down vote up
@Override
public int open(int fd, @NonNull String path, int flags, int mode) throws LastErrorException {
    countOpen++;
    return sub.open(fd, path, flags, mode);
}
 
Example #29
Source File: EmulatedLibc.java    From ev3dev-lang-java with MIT License 4 votes vote down vote up
@Override
public int msync(@NonNull Pointer addr, @NonNull NativeLong len, int flags) throws LastErrorException {
    return impl(path(addr)).msync(addr, len, flags);
}
 
Example #30
Source File: NativeCallsJNAImpl.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static native boolean CloseHandle(Pointer handle)
throws LastErrorException;